re PR tree-optimization/86544 (Popcount detection generates different code on C and...
authorKugan Vivekanandarajah <kuganv@linaro.org>
Wed, 18 Jul 2018 22:11:24 +0000 (22:11 +0000)
committerKugan Vivekanandarajah <kugan@gcc.gnu.org>
Wed, 18 Jul 2018 22:11:24 +0000 (22:11 +0000)
gcc/ChangeLog:

2018-07-18  Kugan Vivekanandarajah  <kuganv@linaro.org>

PR middle-end/86544
* tree-ssa-phiopt.c (cond_removal_in_popcount_pattern): Handle comparision with EQ_EXPR
in last stmt.

gcc/testsuite/ChangeLog:

2018-07-18  Kugan Vivekanandarajah  <kuganv@linaro.org>

PR middle-end/86544
* g++.dg/tree-ssa/pr86544.C: New test.

From-SVN: r262864

gcc/ChangeLog
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/tree-ssa/pr86544.C [new file with mode: 0644]
gcc/tree-ssa-phiopt.c

index 920b105d2bf1fa2d0f8f21e1d53fc553251b62ed..b34ae544a7e867047b24f48760a32793d3eb7b95 100644 (file)
@@ -1,3 +1,9 @@
+2018-07-18  Kugan Vivekanandarajah  <kuganv@linaro.org>
+
+       PR middle-end/86544
+       * tree-ssa-phiopt.c (cond_removal_in_popcount_pattern): Handle comparision with EQ_EXPR
+       in last stmt.
+
 2018-07-18  Kelvin Nilsen  <kelvin@gcc.gnu.org>
 
        * doc/extend.texi (PowerPC AltiVec Built-in Functions): Rename
index 1b7364aa45bdb9e38f8a375706d87d71ba88645f..f0e0825887292fcd06cc03d4daeed421af7f2ee9 100644 (file)
@@ -1,3 +1,8 @@
+2018-07-18  Kugan Vivekanandarajah  <kuganv@linaro.org>
+
+       PR middle-end/86544
+       * g++.dg/tree-ssa/pr86544.C: New test.
+
 2018-07-18  Jakub Jelinek  <jakub@redhat.com>
 
        PR c++/86550
diff --git a/gcc/testsuite/g++.dg/tree-ssa/pr86544.C b/gcc/testsuite/g++.dg/tree-ssa/pr86544.C
new file mode 100644 (file)
index 0000000..8a90089
--- /dev/null
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-phiopt3 -fdump-tree-optimized" } */
+
+int PopCount (long b) {
+    int c = 0;
+
+    while (b) {
+       b &= b - 1;
+       c++;
+    }
+    return c;
+}
+
+/* { dg-final { scan-tree-dump-times "__builtin_popcount" 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "if" 0 "phiopt3" } } */
index 656f8401c586b2e6f8d3ebbe4106ae2e3b937f45..1667bad873b3dff2dca5409a7dc3a09fcad86755 100644 (file)
@@ -1614,8 +1614,22 @@ cond_removal_in_popcount_pattern (basic_block cond_bb, basic_block middle_bb,
       arg = gimple_assign_rhs1 (cast);
     }
 
+  cond = last_stmt (cond_bb);
+
+  /* Cond_bb has a check for b_4 [!=|==] 0 before calling the popcount
+     builtin.  */
+  if (gimple_code (cond) != GIMPLE_COND
+      || (gimple_cond_code (cond) != NE_EXPR
+         && gimple_cond_code (cond) != EQ_EXPR)
+      || !integer_zerop (gimple_cond_rhs (cond))
+      || arg != gimple_cond_lhs (cond))
+    return false;
+
   /* Canonicalize.  */
-  if (e2->flags & EDGE_TRUE_VALUE)
+  if ((e2->flags & EDGE_TRUE_VALUE
+       && gimple_cond_code (cond) == NE_EXPR)
+      || (e1->flags & EDGE_TRUE_VALUE
+         && gimple_cond_code (cond) == EQ_EXPR))
     {
       std::swap (arg0, arg1);
       std::swap (e1, e2);
@@ -1625,16 +1639,6 @@ cond_removal_in_popcount_pattern (basic_block cond_bb, basic_block middle_bb,
   if (lhs != arg0 || !integer_zerop (arg1))
     return false;
 
-  cond = last_stmt (cond_bb);
-
-  /* Cond_bb has a check for b_4 != 0 before calling the popcount
-     builtin.  */
-  if (gimple_code (cond) != GIMPLE_COND
-      || gimple_cond_code (cond) != NE_EXPR
-      || !integer_zerop (gimple_cond_rhs (cond))
-      || arg != gimple_cond_lhs (cond))
-    return false;
-
   /* And insert the popcount builtin and cast stmt before the cond_bb.  */
   gsi = gsi_last_bb (cond_bb);
   if (cast)