https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122637

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |pinskia at gcc dot 
gnu.org
   Last reconfirmed|                            |2025-11-11
             Status|UNCONFIRMED                 |ASSIGNED
     Ever confirmed|0                           |1

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
analyze_scalar_evolution_1 (loop=0x7ffff7468190, var=0x7ffff7216000) at
/home/apinski/src/upstream-gcc-git/gcc/gcc/tree-scalar-evolution.cc:1989
1989      def_loop = bb->loop_father;


#1  0x00000000013776d3 in analyze_scalar_evolution (loop=0x7ffff7468190,
var=0x7ffff7216000) at
/home/apinski/src/upstream-gcc-git/gcc/gcc/tree-scalar-evolution.cc:2081
#2  0x000000000144117a in idx_infer_loop_bounds
(base=base@entry=0x7ffff77e5000, idx=idx@entry=0x7ffff77e5020,
dta=dta@entry=0x7fffffffa7b0) at
/home/apinski/src/upstream-gcc-git/gcc/gcc/tree-ssa-loop-niter.cc:4304
#3  0x0000000001456cd9 in for_each_index (addr_p=<optimized out>,
addr_p@entry=0x7fffffffa760, cbck=cbck@entry=0x1441100
<idx_infer_loop_bounds(tree, tree*, void*)>, data=data@entry=0x7fffffffa7b0) at
/home/apinski/src/upstream-gcc-git/gcc/gcc/tree-ssa-loop.cc:576
#4  0x000000000143d3d4 in infer_loop_bounds_from_ref (loop=0x7ffff7468190,
stmt=0x7ffff721b6e0, ref=0x7ffff77e5000) at
/home/apinski/src/upstream-gcc-git/gcc/gcc/tree-ssa-loop-niter.cc:4385
#5  infer_loop_bounds_from_array (loop=<optimized out>, stmt=0x7ffff721b6e0) at
/home/apinski/src/upstream-gcc-git/gcc/gcc/tree-ssa-loop-niter.cc:4406
#6  infer_loop_bounds_from_undefined (loop=<optimized out>, bbs=<optimized
out>) at /home/apinski/src/upstream-gcc-git/gcc/gcc/tree-ssa-loop-niter.cc:4565
#7  estimate_numbers_of_iterations (loop=0x7ffff7468190) at
/home/apinski/src/upstream-gcc-git/gcc/gcc/tree-ssa-loop-niter.cc:5030
#8  0x00000000014404f4 in loop_exits_before_overflow (base=0x7ffff781f150,
step=0x7ffff7714720, at_stmt=0x7ffff7233300, loop=0x7ffff7468190) at
/home/apinski/src/upstream-gcc-git/gcc/gcc/tree-ssa-loop-niter.cc:5417
#9  scev_probably_wraps_p (var=var@entry=0x0, base=base@entry=0x7ffff781f150,
step=step@entry=0x7ffff7714720, at_stmt=at_stmt@entry=0x7ffff7233300,
loop=0x7ffff7468190, use_overflow_semantics=use_overflow_semantics@entry=true)
at /home/apinski/src/upstream-gcc-git/gcc/gcc/tree-ssa-loop-niter.cc:5669
#10 0x00000000016b0153 in get_scev_info (r=..., name=0x7ffff76d89c0,
stmt=<optimized out>, l=0x7ffff7468190, init=<synthetic pointer>:
0x7ffff781f150, step=@0x7fffffffaa98: 0x7ffff7714720, dir=@0x7fffffffaaa8:
EV_DIR_GROWS) at /home/apinski/src/upstream-gcc-git/gcc/gcc/vr-values.cc:237
#11 range_of_var_in_loop (v=..., name=name@entry=0x7ffff7212420,
l=0x7ffff7468190, stmt=0x7ffff7233300, query=0x39b3c90) at
/home/apinski/src/upstream-gcc-git/gcc/gcc/vr-values.cc:304
#12 0x0000000000e82e7c in fold_using_range::range_of_ssa_name_with_loop_info
(this=<optimized out>, r=..., name=0x7ffff7212420, l=<optimized out>,
phi=<optimized out>, src=...) at
/home/apinski/src/upstream-gcc-git/gcc/gcc/gimple-range-fold.cc:1278
#13 0x0000000000e83625 in fold_using_range::range_of_phi (this=0x7fffffffbfc0,
r=..., phi=0x7ffff7233300, src=...) at
/home/apinski/src/upstream-gcc-git/gcc/gcc/gimple-range-fold.cc:1032
#14 0x0000000000e87461 in fold_using_range::fold_stmt (this=0x7fffffffbfc0,
r=..., s=0x7ffff7233300, src=..., name=0x7ffff7212420) at
/home/apinski/src/upstream-gcc-git/gcc/gcc/gimple-range-fold.cc:651
#15 0x0000000000e70706 in gimple_ranger::fold_range_internal (this=0x39b3c90,
r=..., s=0x7ffff7233300, name=<optimized out>) at
/home/apinski/src/upstream-gcc-git/gcc/gcc/gimple-range.cc:282
#16 gimple_ranger::prefill_stmt_dependencies (this=0x39b3c90,
ssa=0x7ffff721bc60) at
/home/apinski/src/upstream-gcc-git/gcc/gcc/gimple-range.cc:421
#17 0x0000000000e7160b in gimple_ranger::range_of_stmt (this=0x39b3c90, r=...,
s=0x7ffff77c4420, name=<optimized out>) at
/home/apinski/src/upstream-gcc-git/gcc/gcc/gimple-range.cc:339
#18 0x0000000000e7311e in gimple_ranger::range_of_expr (this=0x39b3c90, r=...,
expr=0x7ffff721bc60, stmt=<optimized out>) at
/home/apinski/src/upstream-gcc-git/gcc/gcc/gimple-range.cc:112
#19 0x00000000012d13c8 in get_ref_base_and_extent (exp=0x7ffff77e5498,
exp@entry=0x7ffff77e5460, poffset=poffset@entry=0x7fffffffd400,
psize=psize@entry=0x7fffffffd408, pmax_size=pmax_size@entry=0x7fffffffd410,
preverse=preverse@entry=0x7fffffffd3ff) at
/home/apinski/src/upstream-gcc-git/gcc/gcc/tree-dfa.cc:542
#20 0x0000000000e580a2 in fold_const_aggregate_ref_1 (t=0x7ffff77e5460,
valueize=0x0) at
/home/apinski/src/upstream-gcc-git/gcc/gcc/gimple-fold.cc:10285
#21 0x0000000000e597a6 in fold_const_aggregate_ref (t=<optimized out>) at
/home/apinski/src/upstream-gcc-git/gcc/gcc/gimple-fold.cc:10382
#22 maybe_fold_reference (expr=<optimized out>) at
/home/apinski/src/upstream-gcc-git/gcc/gcc/gimple-fold.cc:326
#23 0x0000000000e5bd3d in fold_gimple_assign (si=0x7fffffffd620) at
/home/apinski/src/upstream-gcc-git/gcc/gcc/gimple-fold.cc:474
#24 fold_stmt_1 (gsi=0x7fffffffd620, inplace=false, valueize=<optimized out>,
dce_worklist=<optimized out>) at
/home/apinski/src/upstream-gcc-git/gcc/gcc/gimple-fold.cc:6809
#25 0x00000000012b48f1 in replace_uses_by (name=name@entry=0x7ffff7216000,
val=val@entry=0x7ffff781f300) at
/home/apinski/src/upstream-gcc-git/gcc/gcc/tree-cfg.cc:1992

Getting the order correct this time I think.
```
diff --git a/gcc/tree-scalar-evolution.cc b/gcc/tree-scalar-evolution.cc
index 180df75311f..8b100b733b7 100644
--- a/gcc/tree-scalar-evolution.cc
+++ b/gcc/tree-scalar-evolution.cc
@@ -3952,6 +3952,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. */
@@ -3960,11 +3965,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.  */
```

So gimplify, then replace and then remova the phi.

Reply via email to