The original code only propagated into PHI arguments if the value was a constant. This behavior was lost in the conversion, allowing any value (SSAs for instance) to be propagated into PHIs.

Pre-approved by Jeff in the PR.

Aldy
commit 8fb4d1d58362b77da78c09740c6b5562124a369e
Author: Aldy Hernandez <al...@redhat.com>
Date:   Tue Jun 16 13:43:57 2020 +0200

    Fix pasto in the substitute_and_fold_engine merge with evrp.
    
    The original code only propagated into PHI arguments if the value was
    a constant.  This behavior was lost in the conversion, allowing
    any value (SSAs for instance) to be propagated into PHIs.
    
    gcc/ChangeLog:
    
            PR tree-optimization/95649
            * tree-ssa-propagate.c (propagate_into_phi_args): Do not propagate unless
            value is a constant.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/tree-ssa/pr95649.C: New test.
            * gcc.dg/tree-ssa/pr95649.c: New test.

diff --git a/gcc/testsuite/g++.dg/tree-ssa/pr95649.C b/gcc/testsuite/g++.dg/tree-ssa/pr95649.C
new file mode 100644
index 00000000000..139f699ee2e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/tree-ssa/pr95649.C
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+extern unsigned short var_5;
+extern int var_8, var_9;
+extern short arr_7[];
+void test() {
+  for (; 0 < (char)var_5;)
+    for (int a(var_9 ? var_5 : 0); a < 3002972621U + 1291994699;
+         a += 19499 - 19497)
+      for (long b(var_8); b; b += 4)
+        arr_7[a * b] = 0;
+}
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr95649.c b/gcc/testsuite/gcc.dg/tree-ssa/pr95649.c
new file mode 100644
index 00000000000..cc9b7640c19
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr95649.c
@@ -0,0 +1,21 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fno-tree-scev-cprop" } */
+
+char b (void);
+char *d;
+int e;
+int f;
+void
+g (char *h)
+{
+  while (d)
+    {
+      long i = b ();
+      if (h + i > d)
+	break;
+      if (f > 0 || e)
+	do
+	  *h++ = *h;
+	while (--i);
+    }
+}
diff --git a/gcc/tree-ssa-propagate.c b/gcc/tree-ssa-propagate.c
index 4fda296ef9e..01ee7fd33eb 100644
--- a/gcc/tree-ssa-propagate.c
+++ b/gcc/tree-ssa-propagate.c
@@ -1035,7 +1035,8 @@ substitute_and_fold_engine::propagate_into_phi_args (basic_block bb)
 	      || virtual_operand_p (arg))
 	    continue;
 	  tree val = get_value (arg, phi);
-	  if (val && may_propagate_copy (arg, val))
+	  if (val && is_gimple_min_invariant (val)
+	      && may_propagate_copy (arg, val))
 	    propagate_value (use_p, val);
 	}
     }

Reply via email to