https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124891
--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> --- 2026-04-15 Jakub Jelinek <[email protected]> PR tree-optimization/124891 * tree-ssa-propagate.cc (substitute_and_fold_dom_walker::before_dom_children): Call update_stmt in the did_replace case before calling fold_stmt. * gcc.dg/torture/pr124891.c: New test. --- gcc/tree-ssa-propagate.cc.jj 2026-03-27 10:17:21.499210556 +0100 +++ gcc/tree-ssa-propagate.cc 2026-04-15 17:03:42.791155358 +0200 @@ -860,6 +860,7 @@ substitute_and_fold_dom_walker::before_d /* If we made a replacement, fold the statement. */ if (did_replace) { + update_stmt (stmt); fold_stmt (&i, follow_single_use_edges); stmt = gsi_stmt (i); gimple_set_modified (stmt, true); --- gcc/testsuite/gcc.dg/torture/pr124891.c.jj 2026-04-15 17:05:24.273462229 +0200 +++ gcc/testsuite/gcc.dg/torture/pr124891.c 2026-04-15 17:05:14.427626500 +0200 @@ -0,0 +1,10 @@ +/* PR tree-optimization/124891 */ +/* { dg-do compile } */ + +void +foo (int x, int y, int z, int *buf) +{ + for (int i = 0; i < x; ++i) + for (int j = z; j < x-z; ++z) + buf[j + (y - z +i) * x] = buf[x]; +} fixes it for me (but otherwise untested so far).
