]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
sccp: Fix order of removal of phi (again) [PR122599]
authorAndrew Pinski <andrew.pinski@oss.qualcomm.com>
Fri, 7 Nov 2025 22:01:33 +0000 (14:01 -0800)
committerAndrew Pinski <andrew.pinski@oss.qualcomm.com>
Sat, 8 Nov 2025 02:11:30 +0000 (18:11 -0800)
This time we are gimplifying the expression and call
fold_stmt during the gimplification (which is fine) but
since we removed the phi and the expression references ssa
names in the phi indirectly, things just fall over inside the ranger.

This moves the removal of the phi until gimplification happens as it
might refer back to the ssa name that the phi defines.

Pushed as obvious after bootstrap test on x86_64-linux-gnu.

PR tree-optimization/122599

gcc/ChangeLog:

* tree-scalar-evolution.cc (final_value_replacement_loop): Move
the removal of the phi until after the gimplification of the final
value expression.

gcc/testsuite/ChangeLog:

* gcc.dg/torture/pr122599-1.c: New test.

Signed-off-by: Andrew Pinski <andrew.pinski@oss.qualcomm.com>
gcc/testsuite/gcc.dg/torture/pr122599-1.c [new file with mode: 0644]
gcc/tree-scalar-evolution.cc

diff --git a/gcc/testsuite/gcc.dg/torture/pr122599-1.c b/gcc/testsuite/gcc.dg/torture/pr122599-1.c
new file mode 100644 (file)
index 0000000..5f2356f
--- /dev/null
@@ -0,0 +1,8 @@
+/* { dg-do compile } */
+/* PR tree-optimization/122599 */
+
+void f(int *x, unsigned n) {
+  for (int i = 0; i < 5; i++)
+    while ((int)--n >= 0)
+      x[0] = 0;
+}
index 9f82abc4b81f121fd72f9349f940ea1ec7656193..180df75311f5c313b56c69b1723aaa6c6b7f689d 100644 (file)
@@ -3947,11 +3947,15 @@ final_value_replacement_loop (class loop *loop)
         GENERIC interface).  */
       def = unshare_expr (def);
       auto loc = gimple_phi_arg_location (phi, exit->dest_idx);
-      remove_phi_node (&psi, false);
 
       /* Create the replacement statements.  */
       gimple_seq stmts;
       def = force_gimple_operand (def, &stmts, false, NULL_TREE);
+
+      /* Remove the old phi after the gimplification to make sure the
+        SSA name is defined by a statement so that fold_stmt during
+        the gimplification does not crash. */
+      remove_phi_node (&psi, false);
       gassign *ass = gimple_build_assign (rslt, def);
       gimple_set_location (ass, loc);
       gimple_seq_add_stmt (&stmts, ass);