From: Andrew Pinski Date: Thu, 15 Jan 2026 00:44:17 +0000 (-0800) Subject: slsr: Fix some undefined/overflow introducing bugs in SLSR [PR106883] X-Git-Tag: basepoints/gcc-17~1804 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=e5bcb08d132bf753178a2b30962b4a9d3452c4f2;p=thirdparty%2Fgcc.git slsr: Fix some undefined/overflow introducing bugs in SLSR [PR106883] This fixes the first part of SLSR incorrectly inserting undefined code (overflow) into the IR. The easiest way is to rewrite the statement after creating it using rewrite_to_defined_unconditional. This fixes the testcases from PR 121347 (and a few others) which all cause an infinite loops to appear. I will be posting the fix for replace_rhs_if_not_dup later and at that point I will add a few testcases. Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR tree-optimization/121347 PR tree-optimization/106883 gcc/ChangeLog: * gimple-ssa-strength-reduction.cc (insert_initializers): Rewrite newly inserted statements for undefinedness (overflow). Signed-off-by: Andrew Pinski --- diff --git a/gcc/gimple-ssa-strength-reduction.cc b/gcc/gimple-ssa-strength-reduction.cc index 70978b295d8..3bb924fa54c 100644 --- a/gcc/gimple-ssa-strength-reduction.cc +++ b/gcc/gimple-ssa-strength-reduction.cc @@ -57,6 +57,7 @@ along with GCC; see the file COPYING3. If not see #include "tree-eh.h" #include "builtins.h" #include "tree-ssa-dce.h" +#include "gimple-fold.h" /* Information about a strength reduction candidate. Each statement in the candidate table represents an expression of one of the @@ -3474,8 +3475,15 @@ insert_initializers (slsr_cand_t c) gimple_set_location (cast_stmt, loc); } - gsi_insert_before (&gsi, init_stmt, GSI_SAME_STMT); gimple_set_location (init_stmt, loc); + if (gimple_needing_rewrite_undefined (init_stmt)) + { + gimple_seq seq; + seq = rewrite_to_defined_unconditional (init_stmt); + gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT); + } + else + gsi_insert_before (&gsi, init_stmt, GSI_SAME_STMT); } else { @@ -3483,6 +3491,7 @@ insert_initializers (slsr_cand_t c) gimple *basis_stmt = lookup_cand (c->basis)->cand_stmt; location_t loc = gimple_location (basis_stmt); + gimple_set_location (init_stmt, gimple_location (basis_stmt)); if (!gsi_end_p (gsi) && stmt_ends_bb_p (gsi_stmt (gsi))) { if (cast_stmt) @@ -3490,7 +3499,14 @@ insert_initializers (slsr_cand_t c) gsi_insert_before (&gsi, cast_stmt, GSI_SAME_STMT); gimple_set_location (cast_stmt, loc); } - gsi_insert_before (&gsi, init_stmt, GSI_SAME_STMT); + if (gimple_needing_rewrite_undefined (init_stmt)) + { + gimple_seq seq; + seq = rewrite_to_defined_unconditional (init_stmt); + gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT); + } + else + gsi_insert_before (&gsi, init_stmt, GSI_SAME_STMT); } else { @@ -3499,10 +3515,15 @@ insert_initializers (slsr_cand_t c) gsi_insert_after (&gsi, cast_stmt, GSI_NEW_STMT); gimple_set_location (cast_stmt, loc); } - gsi_insert_after (&gsi, init_stmt, GSI_NEW_STMT); + if (gimple_needing_rewrite_undefined (init_stmt)) + { + gimple_seq seq; + seq = rewrite_to_defined_unconditional (init_stmt); + gsi_insert_seq_after (&gsi, seq, GSI_SAME_STMT); + } + else + gsi_insert_after (&gsi, init_stmt, GSI_SAME_STMT); } - - gimple_set_location (init_stmt, gimple_location (basis_stmt)); } if (dump_file && (dump_flags & TDF_DETAILS))