diff --git a/gcc/testsuite/gcc.dg/torture/pr122599-1.c b/gcc/testsuite/gcc.dg/torture/pr122599-1.c new file mode 100644 index 000000000000..5f2356f71732 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr122599-1.c @@ -0,0 +1,8 @@ +/* { dg-do compile } */ +/* PR tree-optimization/122599 */ + +void f(int *x, unsigned n) { + for (int i = 0; i < 5; i++) + while ((int)--n >= 0) + x[0] = 0; +} diff --git a/gcc/tree-scalar-evolution.cc b/gcc/tree-scalar-evolution.cc index 9f82abc4b81f..180df75311f5 100644 --- a/gcc/tree-scalar-evolution.cc +++ b/gcc/tree-scalar-evolution.cc @@ -3947,11 +3947,15 @@ final_value_replacement_loop (class loop *loop) GENERIC interface). */ def = unshare_expr (def); auto loc = gimple_phi_arg_location (phi, exit->dest_idx); - remove_phi_node (&psi, false); /* Create the replacement statements. */ gimple_seq stmts; def = force_gimple_operand (def, &stmts, false, NULL_TREE); + + /* 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. */ + remove_phi_node (&psi, false); gassign *ass = gimple_build_assign (rslt, def); gimple_set_location (ass, loc); gimple_seq_add_stmt (&stmts, ass);