]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
sccp: Fix order of gimplification, removal of the phi and constant prop in sccp ...
authorAndrew Pinski <andrew.pinski@oss.qualcomm.com>
Thu, 13 Nov 2025 05:06:02 +0000 (21:06 -0800)
committerAndrew Pinski <andrew.pinski@oss.qualcomm.com>
Thu, 13 Nov 2025 20:00:18 +0000 (12:00 -0800)
This is 3rd (and hopefully last) time to fix the order here.
The previous times were r16-5093-g77e10b47f25d05 and r16-4905-g7b9d32aa2ffcb5.
The order before these patches were:
* removal of phi
* propagate constants
* gimplification of expr
* create assignment
* rewrite to undefined
* add stmts to bb

The current order before this patch (and after the other 2):
* gimplification of expr
* removal of phi
* create assignment
* propagate constants
* rewrite to undefined
* add stmts to bb

The correct and new order with this patch we have:
* gimplifcation of expr
* propagate constants
* removal of phi
* create the assignment
* rewrite to undefined
* add stmts to bb

This is because the propagate of the constant will cause a fold_stmt which requires
the statement in the IR still. The gimplifcation of expr also calls fold_stmt.
Now with the new order the phi is not removed until right before the creation of the
new assigment so the IR in the basic block is well defined while calling fold_stmt.

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

PR tree-optimization/122637

gcc/ChangeLog:

* tree-scalar-evolution.cc (final_value_replacement_loop): Fix order
of gimplification and constant prop.

gcc/testsuite/ChangeLog:

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

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

diff --git a/gcc/testsuite/gcc.dg/torture/pr122637-1.c b/gcc/testsuite/gcc.dg/torture/pr122637-1.c
new file mode 100644 (file)
index 0000000..22d6d2d
--- /dev/null
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* PR tree-optimization/122637 */
+
+char a[10][3];
+int b;
+void e(short c) {
+  for (; c; c--) {
+    for (int d = 2; d; d--)
+      b = a[d][0] & a[c][d];
+  }
+}
index 626c201df5e1b6e834cf947e77f0942fb8740ddd..307628aa63196a1d5e422596f1d874d36b3acd8f 100644 (file)
@@ -3966,6 +3966,11 @@ final_value_replacement_loop (class loop *loop)
       gimple_seq stmts;
       def = force_gimple_operand (def, &stmts, false, NULL_TREE);
 
+      /* Propagate constants immediately, but leave an unused initialization
+        around to avoid invalidating the SCEV cache.  */
+      if (CONSTANT_CLASS_P (def) && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rslt))
+       replace_uses_by (rslt, def);
+
       /* 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. */
@@ -3974,11 +3979,6 @@ final_value_replacement_loop (class loop *loop)
       gimple_set_location (ass, loc);
       gimple_seq_add_stmt (&stmts, ass);
 
-      /* Propagate constants immediately, but leave an unused initialization
-        around to avoid invalidating the SCEV cache.  */
-      if (CONSTANT_CLASS_P (def) && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rslt))
-       replace_uses_by (rslt, def);
-
       /* If def's type has undefined overflow and there were folded
         casts, rewrite all stmts added for def into arithmetics
         with defined overflow behavior.  */