In this case we originally had:
```
# g_4.3_21 = PHI <g_4.3_12(3)>
_1 = g_4.3_21 + 2;
_2 = g_2[_1][g_4.3_21];
```
SCCP figures out g_4.3_12/g_4.3_21 is 1.
final_value_replacement_loop would remove the phi defining _12.
do the constant prop of the const 1 but that would ICE as we try
to fold the reference `g_2[_1][g_4.3_21]` but that would try to see
the range of `_1` but since `_1` refers back to _21 there would be an
ICE as the phi is already been removed.
The obvious fix is to move the constant prop until after the new statement
for _21 is created.
This fixes the change done by r14-6010-g2dde9f326ded84 and
r14-6114-gde0ab339a79535.
This does not fix gcc.dg/graphite/pr82819.c nor tr2/dynamic_bitset/pr92059.cc
though;
I will look into those issues in a few.
Pushed as obvious after bootstrap/test.
PR tree-optimization/122497
gcc/ChangeLog:
* tree-scalar-evolution.cc (final_value_replacement_loop): Call
replace_uses_by
only after the replacement statement was created.
gcc/testsuite/ChangeLog:
* gcc.dg/torture/pr122497-1.c: New test.
Signed-off-by: Andrew Pinski <[email protected]>
---
gcc/testsuite/gcc.dg/torture/pr122497-1.c | 13 +++++++++++++
gcc/tree-scalar-evolution.cc | 10 +++++-----
2 files changed, 18 insertions(+), 5 deletions(-)
create mode 100644 gcc/testsuite/gcc.dg/torture/pr122497-1.c
diff --git a/gcc/testsuite/gcc.dg/torture/pr122497-1.c
b/gcc/testsuite/gcc.dg/torture/pr122497-1.c
new file mode 100644
index 00000000000..8b027ca3aaa
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr122497-1.c
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* PR tree-optimization/122497 */
+
+/* This was ICEing during SCCP
+ trying to simplify a reference back to the phi
+ which was removed. */
+
+char g_2[1][2];
+int g_4, g_5;
+void main() {
+ for (; g_4; g_4 -= 1)
+ g_5 = g_2[g_4 + 2][g_4];
+}
diff --git a/gcc/tree-scalar-evolution.cc b/gcc/tree-scalar-evolution.cc
index ecdef7529a6..7907893b916 100644
--- a/gcc/tree-scalar-evolution.cc
+++ b/gcc/tree-scalar-evolution.cc
@@ -3949,11 +3949,6 @@ final_value_replacement_loop (class loop *loop)
auto loc = gimple_phi_arg_location (phi, exit->dest_idx);
remove_phi_node (&psi, false);
- /* 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);
-
/* Create the replacement statements. */
gimple_seq stmts;
def = force_gimple_operand (def, &stmts, false, NULL_TREE);
@@ -3961,6 +3956,11 @@ 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. */
--
2.43.0