https://gcc.gnu.org/g:0c69007b1fb98d0fe03d61d81a51e4fc6ac98ce9

commit r16-5246-g0c69007b1fb98d0fe03d61d81a51e4fc6ac98ce9
Author: Andrew Pinski <[email protected]>
Date:   Wed Nov 12 21:06:02 2025 -0800

    sccp: Fix order of gimplification, removal of the phi and constant prop in 
sccp (3rd time) [PR122637]
    
    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 <[email protected]>

Diff:
---
 gcc/testsuite/gcc.dg/torture/pr122637-1.c | 11 +++++++++++
 gcc/tree-scalar-evolution.cc              | 10 +++++-----
 2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/torture/pr122637-1.c 
b/gcc/testsuite/gcc.dg/torture/pr122637-1.c
new file mode 100644
index 000000000000..22d6d2ddec1b
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr122637-1.c
@@ -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];
+  }
+}
diff --git a/gcc/tree-scalar-evolution.cc b/gcc/tree-scalar-evolution.cc
index 626c201df5e1..307628aa6319 100644
--- a/gcc/tree-scalar-evolution.cc
+++ b/gcc/tree-scalar-evolution.cc
@@ -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.  */

Reply via email to