We need to fold the stmt to canonicalize MEM_REFs which means
we're back to using replace_uses_by.  Which means we need dominators
to not require a CFG cleanup upthread.

Bootstrapped / tested on x86_64-unknown-linux-gnu, pushed.

2020-11-19  Richard Biener  <rguent...@suse.de>

        PR tree-optimization/97901
        * tree-ssa-propagate.c (clean_up_loop_closed_phi): Compute
        dominators and use replace_uses_by.

        * gcc.dg/torture/pr97901.c: New testcase.
---
 gcc/testsuite/gcc.dg/torture/pr97901.c | 15 +++++++++++++++
 gcc/tree-ssa-propagate.c               | 22 +++++-----------------
 2 files changed, 20 insertions(+), 17 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/torture/pr97901.c

diff --git a/gcc/testsuite/gcc.dg/torture/pr97901.c 
b/gcc/testsuite/gcc.dg/torture/pr97901.c
new file mode 100644
index 00000000000..a6a89ef1e27
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr97901.c
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+
+int a[1], b, *c, *d;
+
+int main() {
+L:
+  d = c;
+  for (b = 0; b < 2; b++)
+    d = &a[0];
+  if (c)
+    goto L;
+  if (*d)
+    __builtin_abort ();
+  return 0;
+}
diff --git a/gcc/tree-ssa-propagate.c b/gcc/tree-ssa-propagate.c
index 354057b48bf..bc656ff76b1 100644
--- a/gcc/tree-ssa-propagate.c
+++ b/gcc/tree-ssa-propagate.c
@@ -1569,6 +1569,10 @@ clean_up_loop_closed_phi (function *fun)
   if (!loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
     return 0;
 
+  /* replace_uses_by might purge dead EH edges and we want it to also
+     remove dominated blocks.  */
+  calculate_dominance_info  (CDI_DOMINATORS);
+
   /* Walk over loop in function.  */
   FOR_EACH_LOOP_FN (fun, loop, 0)
     {
@@ -1595,23 +1599,7 @@ clean_up_loop_closed_phi (function *fun)
                      fprintf (dump_file, "'\n");
                    }
 
-                 use_operand_p use_p;
-                 imm_use_iterator iter;
-                 gimple *use_stmt;
-                 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
-                   {
-                     FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
-                       replace_exp (use_p, rhs);
-                     update_stmt (use_stmt);
-
-                     /* Update the invariant flag for ADDR_EXPR if replacing
-                        a variable index with a constant.  */
-                     if (gimple_assign_single_p (use_stmt)
-                         && TREE_CODE (gimple_assign_rhs1 (use_stmt))
-                              == ADDR_EXPR)
-                       recompute_tree_invariant_for_addr_expr (
-                         gimple_assign_rhs1 (use_stmt));
-                   }
+                 replace_uses_by (lhs, rhs);
                  remove_phi_node (&gsi, true);
                }
              else
-- 
2.26.2

Reply via email to