re PR tree-optimization/71947 (x ^ y not folded to 0 if x == y by DOM)
authorJeff Law <law@redhat.com>
Mon, 10 Oct 2016 20:40:59 +0000 (14:40 -0600)
committerJeff Law <law@gcc.gnu.org>
Mon, 10 Oct 2016 20:40:59 +0000 (14:40 -0600)
        PR tree-optimization/71947
* tree-ssa-dom.c (cprop_into_stmt): Avoid replacing A with B, then
B with A within a single statement.

PR tree-optimization/71947
* gcc.dg/tree-ssa/pr71947-1.c: New test.
* gcc.dg/tree-ssa/pr71947-2.c: New test.
* gcc.dg/tree-ssa/pr71947-3.c: New test.
* gcc.dg/tree-ssa/pr71947-4.c: New test.
* gcc.dg/tree-ssa/pr71947-5.c: New test.
* gcc.dg/tree-ssa/pr71947-6.c: New test.

From-SVN: r240947

gcc/ChangeLog
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/tree-ssa/pr71947-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/pr71947-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/pr71947-3.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/pr71947-4.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/pr71947-5.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/pr71947-6.c [new file with mode: 0644]
gcc/tree-ssa-dom.c

index 1738bc7e7feb534112738c1183913c2831c50610..16e25bfe2d3df5892f7c606fcd996160d9eee606 100644 (file)
@@ -1,3 +1,9 @@
+2016-10-10  Jeff Law  <law@redhat.com>
+
+        PR tree-optimization/71947
+       * tree-ssa-dom.c (cprop_into_stmt): Avoid replacing A with B, then
+       B with A within a single statement.
+
 2016-10-10  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>
 
        PR tree-optimization/77824
index 04966cfbe5cd0b67a46c1d978f65fe8a8e36e3f9..e31bcc6245374f3834cf68e54bb521c44f9effc4 100644 (file)
@@ -1,3 +1,13 @@
+2016-10-10  Jeff Law  <law@redhat.com>
+
+       PR tree-optimization/71947
+       * gcc.dg/tree-ssa/pr71947-1.c: New test.
+       * gcc.dg/tree-ssa/pr71947-2.c: New test.
+       * gcc.dg/tree-ssa/pr71947-3.c: New test.
+       * gcc.dg/tree-ssa/pr71947-4.c: New test.
+       * gcc.dg/tree-ssa/pr71947-5.c: New test.
+       * gcc.dg/tree-ssa/pr71947-6.c: New test.
+
 2016-10-10  Thomas Koenig  <tkoenig@gcc.gnu.org>
 
        PR fortran/77915
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr71947-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr71947-1.c
new file mode 100644 (file)
index 0000000..b033495
--- /dev/null
@@ -0,0 +1,19 @@
+/* { dg-do compile } */ 
+/* { dg-options "-O2 -fno-tree-vrp -fdump-tree-dom-details" } */
+
+
+int f(int x, int y)
+{
+   int ret;
+
+   if (x == y)
+     ret = x ^ y;
+   else
+     ret = 1;
+
+   return ret;
+}
+
+/* { dg-final { scan-tree-dump "Folded to: ret_\[0-9\]+ = 0;"  "dom2" } } */
+
+
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr71947-2.c b/gcc/testsuite/gcc.dg/tree-ssa/pr71947-2.c
new file mode 100644 (file)
index 0000000..de8f88b
--- /dev/null
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fno-tree-vrp -fdump-tree-dom-details" } */
+
+
+int f(int x, int y)
+{
+  int ret;
+  if (x == y)
+    ret = x - y;
+  else
+    ret = 1;
+
+  return ret;
+}
+
+/* { dg-final { scan-tree-dump "Folded to: ret_\[0-9\]+ = 0;"  "dom2" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr71947-3.c b/gcc/testsuite/gcc.dg/tree-ssa/pr71947-3.c
new file mode 100644 (file)
index 0000000..e79847f
--- /dev/null
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fno-tree-vrp -fdump-tree-dom-details" } */
+
+int f(int x, int y)
+{
+  int ret = 10;
+  if (x == y)
+    ret = x  -  y;
+  return ret;
+}
+
+/* { dg-final { scan-tree-dump "Folded to: ret_\[0-9\]+ = 0;"  "dom2" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr71947-4.c b/gcc/testsuite/gcc.dg/tree-ssa/pr71947-4.c
new file mode 100644 (file)
index 0000000..a881f0d
--- /dev/null
@@ -0,0 +1,22 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fno-tree-vrp -fdump-tree-dom-details" } */
+
+
+
+static inline long load(long *p)
+{
+        long ret;
+        asm ("movq      %1,%0\n\t" : "=r" (ret) : "m" (*p));
+        if (ret != *p)
+                __builtin_unreachable();
+        return ret;
+}
+
+long foo(long *mem)
+{
+        long ret;
+        ret = load(mem);
+        return ret + *mem;
+}
+
+/* { dg-final { scan-tree-dump "Folded to: _\[0-9\]+ = _\[0-9\]+ \\* 2"  "dom2" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr71947-5.c b/gcc/testsuite/gcc.dg/tree-ssa/pr71947-5.c
new file mode 100644 (file)
index 0000000..fa679f0
--- /dev/null
@@ -0,0 +1,21 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fno-tree-vrp -fdump-tree-dom-details" } */
+
+
+static inline long load(long *p)
+{
+        long ret;
+        asm ("movq      %1,%0\n\t" : "=r" (ret) : "m" (*p));
+        if (ret != *p)
+                __builtin_unreachable();
+        return ret;
+}
+
+long foo(long *mem)
+{
+        long ret;
+        ret = load(mem);
+        return ret - *mem;
+}
+
+/* { dg-final { scan-tree-dump "Folded to: _\[0-9\]+ = 0;"  "dom2" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr71947-6.c b/gcc/testsuite/gcc.dg/tree-ssa/pr71947-6.c
new file mode 100644 (file)
index 0000000..9cb89cb
--- /dev/null
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fno-tree-vrp -fdump-tree-dom-details" } */
+
+
+int f(int x, int y, int a, int b)
+{
+  int ret = 10;
+  if (a == x
+      && b == y
+      && a == b)
+    ret = x - y;
+
+  return ret;
+}
+
+/* { dg-final { scan-tree-dump "Folded to: ret_\[0-9\]+ = 0;"  "dom2" { xfail *-*-* } } } */
index b007388f3eacd07a9d660e0c23a47828fa91bc7a..5376ff9fc90036a4c7ada1bdf2d7f859cb11d59a 100644 (file)
@@ -1731,9 +1731,26 @@ cprop_into_stmt (gimple *stmt)
 {
   use_operand_p op_p;
   ssa_op_iter iter;
+  tree last_copy_propagated_op = NULL;
 
   FOR_EACH_SSA_USE_OPERAND (op_p, stmt, iter, SSA_OP_USE)
-    cprop_operand (stmt, op_p);
+    {
+      tree old_op = USE_FROM_PTR (op_p);
+
+      /* If we have A = B and B = A in the copy propagation tables
+        (due to an equality comparison), avoid substituting B for A
+        then A for B in the trivially discovered cases.   This allows
+        optimization of statements were A and B appear as input
+        operands.  */
+      if (old_op != last_copy_propagated_op)
+       {
+         cprop_operand (stmt, op_p);
+
+         tree new_op = USE_FROM_PTR (op_p);
+         if (new_op != old_op && TREE_CODE (new_op) == SSA_NAME)
+           last_copy_propagated_op = new_op;
+       }
+    }
 }
 
 /* Optimize the statement in block BB pointed to by iterator SI