public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc(refs/vendors/vrull/heads/for-upstream)] ifcombine: fold two bit tests with different polarity
@ 2022-11-15 14:01 Philipp Tomsich
  0 siblings, 0 replies; 5+ messages in thread
From: Philipp Tomsich @ 2022-11-15 14:01 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:62b72e91cc70dc6f454322ba9a1164434b6c6c9a

commit 62b72e91cc70dc6f454322ba9a1164434b6c6c9a
Author: Philipp Tomsich <philipp.tomsich@vrull.eu>
Date:   Sun Oct 16 22:35:08 2022 +0200

    ifcombine: fold two bit tests with different polarity
    
    Our ifcombine pass combines 2 single-bit tests into a single test of
    the form "(a & T) == T", requiring the same polarity (i.e., tests for
    bit set/cleared) for both bit-tests.  However some applications test
    against two bits expecting one set and the other cleared.
    
    This adds support for the case "(a & T) == C" where T is a constant
    with 2 bits set and C has only one of those bits set.
    
    gcc/ChangeLog:
    
            * tree-ssa-ifcombine.cc (ifcombine_ifandif): Add support for
              combining two bit-tests that test for bits of different
              polarity.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/tree-ssa/ssa-ifcombine-15.c: New test.
    
    Series-to: gcc-patches@gcc.gnu.org
    Series-cc: Richard Biener <rguenther@suse.de>
    Series-cc: Tamar Christina <tamar.christina@arm.com>
    Series-cc: Jiang-Ning Liu <jiangning.liu@amperecomputing.com>
    Series-cc: Christoph Muellner <christoph.muellner@vrull.eu>
    Series-cc: Jeff Law <jlaw@ventanamicro.com>

Diff:
---
 gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-15.c | 14 ++++++
 gcc/tree-ssa-ifcombine.cc                        | 56 ++++++++++++++++++++++++
 2 files changed, 70 insertions(+)

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-15.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-15.c
new file mode 100644
index 00000000000..081faa39628
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-15.c
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-ifcombine-details-blocks" } */
+
+void sink();
+
+void reversed(unsigned char *a)
+{
+  if (*a & 0x60)
+    if (!(*a & 0x02))
+      g();
+}
+
+/* { dg-final { scan-tree-dump "optimizing double bit test" } } */
+
diff --git a/gcc/tree-ssa-ifcombine.cc b/gcc/tree-ssa-ifcombine.cc
index cd6331f84db..ea49cc2bff1 100644
--- a/gcc/tree-ssa-ifcombine.cc
+++ b/gcc/tree-ssa-ifcombine.cc
@@ -498,6 +498,62 @@ ifcombine_ifandif (basic_block inner_cond_bb, bool inner_inv,
       return true;
     }
 
+  /* See if we test polarity-reversed single bits of the same name in
+     both tests.  In that case remove the outer test, merging both
+     else edges, and change the inner one to test for
+       name & (bit1 | bit2) == (bit2).  */
+  else if ((recognize_single_bit_test (inner_cond, &name1, &bit1, !inner_inv)
+	    && recognize_single_bit_test (outer_cond, &name2, &bit2, outer_inv)
+	    && name1 == name2)
+	   || (recognize_single_bit_test (inner_cond, &name2, &bit2, inner_inv)
+	       && recognize_single_bit_test (outer_cond, &name1, &bit1, !outer_inv)
+	       && name1 == name2))
+    {
+      tree t, t2, t3;
+
+      /* Do it.  */
+      gsi = gsi_for_stmt (inner_cond);
+      t = fold_build2 (LSHIFT_EXPR, TREE_TYPE (name1),
+		       build_int_cst (TREE_TYPE (name1), 1), bit1);
+      t2 = fold_build2 (LSHIFT_EXPR, TREE_TYPE (name1),
+			build_int_cst (TREE_TYPE (name1), 1), bit2);
+      t = fold_build2 (BIT_IOR_EXPR, TREE_TYPE (name1), t, t2);
+      t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
+				    true, GSI_SAME_STMT);
+      t3 = fold_build2 (BIT_AND_EXPR, TREE_TYPE (name1), name1, t);
+      t3 = force_gimple_operand_gsi (&gsi, t3, true, NULL_TREE,
+				     true, GSI_SAME_STMT);
+      t = fold_build2 (result_inv ? NE_EXPR : EQ_EXPR,
+		       boolean_type_node, t2, t3);
+      t = canonicalize_cond_expr_cond (t);
+      if (!t)
+	return false;
+      gimple_cond_set_condition_from_tree (inner_cond, t);
+      update_stmt (inner_cond);
+
+      /* Leave CFG optimization to cfg_cleanup.  */
+      gimple_cond_set_condition_from_tree (outer_cond,
+	outer_inv ? boolean_false_node : boolean_true_node);
+      update_stmt (outer_cond);
+
+      update_profile_after_ifcombine (inner_cond_bb, outer_cond_bb);
+
+      if (dump_file)
+	{
+	  fprintf (dump_file, "optimizing double bit test to ");
+	  print_generic_expr (dump_file, name1);
+	  fprintf (dump_file, " & T == C\nwith temporary T = (1 << ");
+	  print_generic_expr (dump_file, bit1);
+	  fprintf (dump_file, ") | (1 << ");
+	  print_generic_expr (dump_file, bit2);
+	  fprintf (dump_file, ")\nand temporary C = (1 << ");
+	  print_generic_expr (dump_file, bit2);
+	  fprintf (dump_file, ")\n");
+	}
+
+      return true;
+    }
+
   /* See if we have two bit tests of the same name in both tests.
      In that case remove the outer test and change the inner one to
      test for name & (bits1 | bits2) != 0.  */

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [gcc(refs/vendors/vrull/heads/for-upstream)] ifcombine: fold two bit tests with different polarity
@ 2022-11-18 20:25 Philipp Tomsich
  0 siblings, 0 replies; 5+ messages in thread
From: Philipp Tomsich @ 2022-11-18 20:25 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:d3e3ad74ddf130b9ef225022da1465bf190975f3

commit d3e3ad74ddf130b9ef225022da1465bf190975f3
Author: Philipp Tomsich <philipp.tomsich@vrull.eu>
Date:   Sun Oct 16 22:35:08 2022 +0200

    ifcombine: fold two bit tests with different polarity
    
    Our ifcombine pass combines 2 single-bit tests into a single test of
    the form "(a & T) == T", requiring the same polarity (i.e., tests for
    bit set/cleared) for both bit-tests.  However some applications test
    against two bits expecting one set and the other cleared.
    
    This adds support for the case "(a & T) == C" where T is a constant
    with 2 bits set and C has only one of those bits set.
    
    gcc/ChangeLog:
    
            * tree-ssa-ifcombine.cc (ifcombine_ifandif): Add support for
              combining two bit-tests that test for bits of different
              polarity.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/tree-ssa/ssa-ifcombine-15.c: New test.
    
    Series-to: gcc-patches@gcc.gnu.org
    Series-cc: Richard Biener <rguenther@suse.de>
    Series-cc: Tamar Christina <tamar.christina@arm.com>
    Series-cc: Jiang-Ning Liu <jiangning.liu@amperecomputing.com>
    Series-cc: Christoph Muellner <christoph.muellner@vrull.eu>
    Series-cc: Jeff Law <jlaw@ventanamicro.com>

Diff:
---
 gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-15.c | 14 ++++++
 gcc/tree-ssa-ifcombine.cc                        | 56 ++++++++++++++++++++++++
 2 files changed, 70 insertions(+)

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-15.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-15.c
new file mode 100644
index 00000000000..081faa39628
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-15.c
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-ifcombine-details-blocks" } */
+
+void sink();
+
+void reversed(unsigned char *a)
+{
+  if (*a & 0x60)
+    if (!(*a & 0x02))
+      g();
+}
+
+/* { dg-final { scan-tree-dump "optimizing double bit test" } } */
+
diff --git a/gcc/tree-ssa-ifcombine.cc b/gcc/tree-ssa-ifcombine.cc
index cd6331f84db..ea49cc2bff1 100644
--- a/gcc/tree-ssa-ifcombine.cc
+++ b/gcc/tree-ssa-ifcombine.cc
@@ -498,6 +498,62 @@ ifcombine_ifandif (basic_block inner_cond_bb, bool inner_inv,
       return true;
     }
 
+  /* See if we test polarity-reversed single bits of the same name in
+     both tests.  In that case remove the outer test, merging both
+     else edges, and change the inner one to test for
+       name & (bit1 | bit2) == (bit2).  */
+  else if ((recognize_single_bit_test (inner_cond, &name1, &bit1, !inner_inv)
+	    && recognize_single_bit_test (outer_cond, &name2, &bit2, outer_inv)
+	    && name1 == name2)
+	   || (recognize_single_bit_test (inner_cond, &name2, &bit2, inner_inv)
+	       && recognize_single_bit_test (outer_cond, &name1, &bit1, !outer_inv)
+	       && name1 == name2))
+    {
+      tree t, t2, t3;
+
+      /* Do it.  */
+      gsi = gsi_for_stmt (inner_cond);
+      t = fold_build2 (LSHIFT_EXPR, TREE_TYPE (name1),
+		       build_int_cst (TREE_TYPE (name1), 1), bit1);
+      t2 = fold_build2 (LSHIFT_EXPR, TREE_TYPE (name1),
+			build_int_cst (TREE_TYPE (name1), 1), bit2);
+      t = fold_build2 (BIT_IOR_EXPR, TREE_TYPE (name1), t, t2);
+      t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
+				    true, GSI_SAME_STMT);
+      t3 = fold_build2 (BIT_AND_EXPR, TREE_TYPE (name1), name1, t);
+      t3 = force_gimple_operand_gsi (&gsi, t3, true, NULL_TREE,
+				     true, GSI_SAME_STMT);
+      t = fold_build2 (result_inv ? NE_EXPR : EQ_EXPR,
+		       boolean_type_node, t2, t3);
+      t = canonicalize_cond_expr_cond (t);
+      if (!t)
+	return false;
+      gimple_cond_set_condition_from_tree (inner_cond, t);
+      update_stmt (inner_cond);
+
+      /* Leave CFG optimization to cfg_cleanup.  */
+      gimple_cond_set_condition_from_tree (outer_cond,
+	outer_inv ? boolean_false_node : boolean_true_node);
+      update_stmt (outer_cond);
+
+      update_profile_after_ifcombine (inner_cond_bb, outer_cond_bb);
+
+      if (dump_file)
+	{
+	  fprintf (dump_file, "optimizing double bit test to ");
+	  print_generic_expr (dump_file, name1);
+	  fprintf (dump_file, " & T == C\nwith temporary T = (1 << ");
+	  print_generic_expr (dump_file, bit1);
+	  fprintf (dump_file, ") | (1 << ");
+	  print_generic_expr (dump_file, bit2);
+	  fprintf (dump_file, ")\nand temporary C = (1 << ");
+	  print_generic_expr (dump_file, bit2);
+	  fprintf (dump_file, ")\n");
+	}
+
+      return true;
+    }
+
   /* See if we have two bit tests of the same name in both tests.
      In that case remove the outer test and change the inner one to
      test for name & (bits1 | bits2) != 0.  */

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [gcc(refs/vendors/vrull/heads/for-upstream)] ifcombine: fold two bit tests with different polarity
@ 2022-11-18 20:22 Philipp Tomsich
  0 siblings, 0 replies; 5+ messages in thread
From: Philipp Tomsich @ 2022-11-18 20:22 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:e480cf9772fe6b77675d9f06240474386c4a8e0b

commit e480cf9772fe6b77675d9f06240474386c4a8e0b
Author: Philipp Tomsich <philipp.tomsich@vrull.eu>
Date:   Sun Oct 16 22:35:08 2022 +0200

    ifcombine: fold two bit tests with different polarity
    
    Our ifcombine pass combines 2 single-bit tests into a single test of
    the form "(a & T) == T", requiring the same polarity (i.e., tests for
    bit set/cleared) for both bit-tests.  However some applications test
    against two bits expecting one set and the other cleared.
    
    This adds support for the case "(a & T) == C" where T is a constant
    with 2 bits set and C has only one of those bits set.
    
    gcc/ChangeLog:
    
            * tree-ssa-ifcombine.cc (ifcombine_ifandif): Add support for
              combining two bit-tests that test for bits of different
              polarity.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/tree-ssa/ssa-ifcombine-15.c: New test.
    
    Series-to: gcc-patches@gcc.gnu.org
    Series-cc: Richard Biener <rguenther@suse.de>
    Series-cc: Tamar Christina <tamar.christina@arm.com>
    Series-cc: Jiang-Ning Liu <jiangning.liu@amperecomputing.com>
    Series-cc: Christoph Muellner <christoph.muellner@vrull.eu>
    Series-cc: Jeff Law <jlaw@ventanamicro.com>

Diff:
---
 gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-15.c | 14 ++++++
 gcc/tree-ssa-ifcombine.cc                        | 56 ++++++++++++++++++++++++
 2 files changed, 70 insertions(+)

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-15.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-15.c
new file mode 100644
index 00000000000..081faa39628
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-15.c
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-ifcombine-details-blocks" } */
+
+void sink();
+
+void reversed(unsigned char *a)
+{
+  if (*a & 0x60)
+    if (!(*a & 0x02))
+      g();
+}
+
+/* { dg-final { scan-tree-dump "optimizing double bit test" } } */
+
diff --git a/gcc/tree-ssa-ifcombine.cc b/gcc/tree-ssa-ifcombine.cc
index cd6331f84db..ea49cc2bff1 100644
--- a/gcc/tree-ssa-ifcombine.cc
+++ b/gcc/tree-ssa-ifcombine.cc
@@ -498,6 +498,62 @@ ifcombine_ifandif (basic_block inner_cond_bb, bool inner_inv,
       return true;
     }
 
+  /* See if we test polarity-reversed single bits of the same name in
+     both tests.  In that case remove the outer test, merging both
+     else edges, and change the inner one to test for
+       name & (bit1 | bit2) == (bit2).  */
+  else if ((recognize_single_bit_test (inner_cond, &name1, &bit1, !inner_inv)
+	    && recognize_single_bit_test (outer_cond, &name2, &bit2, outer_inv)
+	    && name1 == name2)
+	   || (recognize_single_bit_test (inner_cond, &name2, &bit2, inner_inv)
+	       && recognize_single_bit_test (outer_cond, &name1, &bit1, !outer_inv)
+	       && name1 == name2))
+    {
+      tree t, t2, t3;
+
+      /* Do it.  */
+      gsi = gsi_for_stmt (inner_cond);
+      t = fold_build2 (LSHIFT_EXPR, TREE_TYPE (name1),
+		       build_int_cst (TREE_TYPE (name1), 1), bit1);
+      t2 = fold_build2 (LSHIFT_EXPR, TREE_TYPE (name1),
+			build_int_cst (TREE_TYPE (name1), 1), bit2);
+      t = fold_build2 (BIT_IOR_EXPR, TREE_TYPE (name1), t, t2);
+      t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
+				    true, GSI_SAME_STMT);
+      t3 = fold_build2 (BIT_AND_EXPR, TREE_TYPE (name1), name1, t);
+      t3 = force_gimple_operand_gsi (&gsi, t3, true, NULL_TREE,
+				     true, GSI_SAME_STMT);
+      t = fold_build2 (result_inv ? NE_EXPR : EQ_EXPR,
+		       boolean_type_node, t2, t3);
+      t = canonicalize_cond_expr_cond (t);
+      if (!t)
+	return false;
+      gimple_cond_set_condition_from_tree (inner_cond, t);
+      update_stmt (inner_cond);
+
+      /* Leave CFG optimization to cfg_cleanup.  */
+      gimple_cond_set_condition_from_tree (outer_cond,
+	outer_inv ? boolean_false_node : boolean_true_node);
+      update_stmt (outer_cond);
+
+      update_profile_after_ifcombine (inner_cond_bb, outer_cond_bb);
+
+      if (dump_file)
+	{
+	  fprintf (dump_file, "optimizing double bit test to ");
+	  print_generic_expr (dump_file, name1);
+	  fprintf (dump_file, " & T == C\nwith temporary T = (1 << ");
+	  print_generic_expr (dump_file, bit1);
+	  fprintf (dump_file, ") | (1 << ");
+	  print_generic_expr (dump_file, bit2);
+	  fprintf (dump_file, ")\nand temporary C = (1 << ");
+	  print_generic_expr (dump_file, bit2);
+	  fprintf (dump_file, ")\n");
+	}
+
+      return true;
+    }
+
   /* See if we have two bit tests of the same name in both tests.
      In that case remove the outer test and change the inner one to
      test for name & (bits1 | bits2) != 0.  */

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [gcc(refs/vendors/vrull/heads/for-upstream)] ifcombine: fold two bit tests with different polarity
@ 2022-11-18 11:34 Philipp Tomsich
  0 siblings, 0 replies; 5+ messages in thread
From: Philipp Tomsich @ 2022-11-18 11:34 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:f525996c10bd46e436b4826a2f29f0d208624565

commit f525996c10bd46e436b4826a2f29f0d208624565
Author: Philipp Tomsich <philipp.tomsich@vrull.eu>
Date:   Sun Oct 16 22:35:08 2022 +0200

    ifcombine: fold two bit tests with different polarity
    
    Our ifcombine pass combines 2 single-bit tests into a single test of
    the form "(a & T) == T", requiring the same polarity (i.e., tests for
    bit set/cleared) for both bit-tests.  However some applications test
    against two bits expecting one set and the other cleared.
    
    This adds support for the case "(a & T) == C" where T is a constant
    with 2 bits set and C has only one of those bits set.
    
    gcc/ChangeLog:
    
            * tree-ssa-ifcombine.cc (ifcombine_ifandif): Add support for
              combining two bit-tests that test for bits of different
              polarity.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/tree-ssa/ssa-ifcombine-15.c: New test.
    
    Series-to: gcc-patches@gcc.gnu.org
    Series-cc: Richard Biener <rguenther@suse.de>
    Series-cc: Tamar Christina <tamar.christina@arm.com>
    Series-cc: Jiang-Ning Liu <jiangning.liu@amperecomputing.com>
    Series-cc: Christoph Muellner <christoph.muellner@vrull.eu>
    Series-cc: Jeff Law <jlaw@ventanamicro.com>

Diff:
---
 gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-15.c | 14 ++++++
 gcc/tree-ssa-ifcombine.cc                        | 56 ++++++++++++++++++++++++
 2 files changed, 70 insertions(+)

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-15.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-15.c
new file mode 100644
index 00000000000..081faa39628
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-15.c
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-ifcombine-details-blocks" } */
+
+void sink();
+
+void reversed(unsigned char *a)
+{
+  if (*a & 0x60)
+    if (!(*a & 0x02))
+      g();
+}
+
+/* { dg-final { scan-tree-dump "optimizing double bit test" } } */
+
diff --git a/gcc/tree-ssa-ifcombine.cc b/gcc/tree-ssa-ifcombine.cc
index cd6331f84db..ea49cc2bff1 100644
--- a/gcc/tree-ssa-ifcombine.cc
+++ b/gcc/tree-ssa-ifcombine.cc
@@ -498,6 +498,62 @@ ifcombine_ifandif (basic_block inner_cond_bb, bool inner_inv,
       return true;
     }
 
+  /* See if we test polarity-reversed single bits of the same name in
+     both tests.  In that case remove the outer test, merging both
+     else edges, and change the inner one to test for
+       name & (bit1 | bit2) == (bit2).  */
+  else if ((recognize_single_bit_test (inner_cond, &name1, &bit1, !inner_inv)
+	    && recognize_single_bit_test (outer_cond, &name2, &bit2, outer_inv)
+	    && name1 == name2)
+	   || (recognize_single_bit_test (inner_cond, &name2, &bit2, inner_inv)
+	       && recognize_single_bit_test (outer_cond, &name1, &bit1, !outer_inv)
+	       && name1 == name2))
+    {
+      tree t, t2, t3;
+
+      /* Do it.  */
+      gsi = gsi_for_stmt (inner_cond);
+      t = fold_build2 (LSHIFT_EXPR, TREE_TYPE (name1),
+		       build_int_cst (TREE_TYPE (name1), 1), bit1);
+      t2 = fold_build2 (LSHIFT_EXPR, TREE_TYPE (name1),
+			build_int_cst (TREE_TYPE (name1), 1), bit2);
+      t = fold_build2 (BIT_IOR_EXPR, TREE_TYPE (name1), t, t2);
+      t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
+				    true, GSI_SAME_STMT);
+      t3 = fold_build2 (BIT_AND_EXPR, TREE_TYPE (name1), name1, t);
+      t3 = force_gimple_operand_gsi (&gsi, t3, true, NULL_TREE,
+				     true, GSI_SAME_STMT);
+      t = fold_build2 (result_inv ? NE_EXPR : EQ_EXPR,
+		       boolean_type_node, t2, t3);
+      t = canonicalize_cond_expr_cond (t);
+      if (!t)
+	return false;
+      gimple_cond_set_condition_from_tree (inner_cond, t);
+      update_stmt (inner_cond);
+
+      /* Leave CFG optimization to cfg_cleanup.  */
+      gimple_cond_set_condition_from_tree (outer_cond,
+	outer_inv ? boolean_false_node : boolean_true_node);
+      update_stmt (outer_cond);
+
+      update_profile_after_ifcombine (inner_cond_bb, outer_cond_bb);
+
+      if (dump_file)
+	{
+	  fprintf (dump_file, "optimizing double bit test to ");
+	  print_generic_expr (dump_file, name1);
+	  fprintf (dump_file, " & T == C\nwith temporary T = (1 << ");
+	  print_generic_expr (dump_file, bit1);
+	  fprintf (dump_file, ") | (1 << ");
+	  print_generic_expr (dump_file, bit2);
+	  fprintf (dump_file, ")\nand temporary C = (1 << ");
+	  print_generic_expr (dump_file, bit2);
+	  fprintf (dump_file, ")\n");
+	}
+
+      return true;
+    }
+
   /* See if we have two bit tests of the same name in both tests.
      In that case remove the outer test and change the inner one to
      test for name & (bits1 | bits2) != 0.  */

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [gcc(refs/vendors/vrull/heads/for-upstream)] ifcombine: fold two bit tests with different polarity
@ 2022-11-17 22:25 Philipp Tomsich
  0 siblings, 0 replies; 5+ messages in thread
From: Philipp Tomsich @ 2022-11-17 22:25 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:05980169d616eca7a17d3cac91d98437198bbcdb

commit 05980169d616eca7a17d3cac91d98437198bbcdb
Author: Philipp Tomsich <philipp.tomsich@vrull.eu>
Date:   Sun Oct 16 22:35:08 2022 +0200

    ifcombine: fold two bit tests with different polarity
    
    Our ifcombine pass combines 2 single-bit tests into a single test of
    the form "(a & T) == T", requiring the same polarity (i.e., tests for
    bit set/cleared) for both bit-tests.  However some applications test
    against two bits expecting one set and the other cleared.
    
    This adds support for the case "(a & T) == C" where T is a constant
    with 2 bits set and C has only one of those bits set.
    
    gcc/ChangeLog:
    
            * tree-ssa-ifcombine.cc (ifcombine_ifandif): Add support for
              combining two bit-tests that test for bits of different
              polarity.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/tree-ssa/ssa-ifcombine-15.c: New test.
    
    Series-to: gcc-patches@gcc.gnu.org
    Series-cc: Richard Biener <rguenther@suse.de>
    Series-cc: Tamar Christina <tamar.christina@arm.com>
    Series-cc: Jiang-Ning Liu <jiangning.liu@amperecomputing.com>
    Series-cc: Christoph Muellner <christoph.muellner@vrull.eu>
    Series-cc: Jeff Law <jlaw@ventanamicro.com>

Diff:
---
 gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-15.c | 14 ++++++
 gcc/tree-ssa-ifcombine.cc                        | 56 ++++++++++++++++++++++++
 2 files changed, 70 insertions(+)

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-15.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-15.c
new file mode 100644
index 00000000000..081faa39628
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-15.c
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-ifcombine-details-blocks" } */
+
+void sink();
+
+void reversed(unsigned char *a)
+{
+  if (*a & 0x60)
+    if (!(*a & 0x02))
+      g();
+}
+
+/* { dg-final { scan-tree-dump "optimizing double bit test" } } */
+
diff --git a/gcc/tree-ssa-ifcombine.cc b/gcc/tree-ssa-ifcombine.cc
index cd6331f84db..ea49cc2bff1 100644
--- a/gcc/tree-ssa-ifcombine.cc
+++ b/gcc/tree-ssa-ifcombine.cc
@@ -498,6 +498,62 @@ ifcombine_ifandif (basic_block inner_cond_bb, bool inner_inv,
       return true;
     }
 
+  /* See if we test polarity-reversed single bits of the same name in
+     both tests.  In that case remove the outer test, merging both
+     else edges, and change the inner one to test for
+       name & (bit1 | bit2) == (bit2).  */
+  else if ((recognize_single_bit_test (inner_cond, &name1, &bit1, !inner_inv)
+	    && recognize_single_bit_test (outer_cond, &name2, &bit2, outer_inv)
+	    && name1 == name2)
+	   || (recognize_single_bit_test (inner_cond, &name2, &bit2, inner_inv)
+	       && recognize_single_bit_test (outer_cond, &name1, &bit1, !outer_inv)
+	       && name1 == name2))
+    {
+      tree t, t2, t3;
+
+      /* Do it.  */
+      gsi = gsi_for_stmt (inner_cond);
+      t = fold_build2 (LSHIFT_EXPR, TREE_TYPE (name1),
+		       build_int_cst (TREE_TYPE (name1), 1), bit1);
+      t2 = fold_build2 (LSHIFT_EXPR, TREE_TYPE (name1),
+			build_int_cst (TREE_TYPE (name1), 1), bit2);
+      t = fold_build2 (BIT_IOR_EXPR, TREE_TYPE (name1), t, t2);
+      t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
+				    true, GSI_SAME_STMT);
+      t3 = fold_build2 (BIT_AND_EXPR, TREE_TYPE (name1), name1, t);
+      t3 = force_gimple_operand_gsi (&gsi, t3, true, NULL_TREE,
+				     true, GSI_SAME_STMT);
+      t = fold_build2 (result_inv ? NE_EXPR : EQ_EXPR,
+		       boolean_type_node, t2, t3);
+      t = canonicalize_cond_expr_cond (t);
+      if (!t)
+	return false;
+      gimple_cond_set_condition_from_tree (inner_cond, t);
+      update_stmt (inner_cond);
+
+      /* Leave CFG optimization to cfg_cleanup.  */
+      gimple_cond_set_condition_from_tree (outer_cond,
+	outer_inv ? boolean_false_node : boolean_true_node);
+      update_stmt (outer_cond);
+
+      update_profile_after_ifcombine (inner_cond_bb, outer_cond_bb);
+
+      if (dump_file)
+	{
+	  fprintf (dump_file, "optimizing double bit test to ");
+	  print_generic_expr (dump_file, name1);
+	  fprintf (dump_file, " & T == C\nwith temporary T = (1 << ");
+	  print_generic_expr (dump_file, bit1);
+	  fprintf (dump_file, ") | (1 << ");
+	  print_generic_expr (dump_file, bit2);
+	  fprintf (dump_file, ")\nand temporary C = (1 << ");
+	  print_generic_expr (dump_file, bit2);
+	  fprintf (dump_file, ")\n");
+	}
+
+      return true;
+    }
+
   /* See if we have two bit tests of the same name in both tests.
      In that case remove the outer test and change the inner one to
      test for name & (bits1 | bits2) != 0.  */

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-11-18 20:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-15 14:01 [gcc(refs/vendors/vrull/heads/for-upstream)] ifcombine: fold two bit tests with different polarity Philipp Tomsich
2022-11-17 22:25 Philipp Tomsich
2022-11-18 11:34 Philipp Tomsich
2022-11-18 20:22 Philipp Tomsich
2022-11-18 20:25 Philipp Tomsich

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).