From: Andrew Pinski Date: Sat, 18 Nov 2023 04:06:37 +0000 (-0800) Subject: reassoc vs uninitialized variable [PR112581] X-Git-Tag: basepoints/gcc-15~3025 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=113475d03b0ab1ab18a509e8e5844c1a43983b24;p=thirdparty%2Fgcc.git reassoc vs uninitialized variable [PR112581] Like r14-2293-g11350734240dba and r14-2289-gb083203f053f16, reassociation can combine across a few bb and one of the usage can be an uninitializated variable and if going from an conditional usage to an unconditional usage can cause wrong code. This uses maybe_undef_p like other passes where this can happen. Note if-to-switch uses the function (init_range_entry) provided by ressociation so we need to call mark_ssa_maybe_undefs there; otherwise we assume almost all ssa names are uninitialized. Bootstrapped and tested on x86_64-linux-gnu. gcc/ChangeLog: PR tree-optimization/112581 * gimple-if-to-switch.cc (pass_if_to_switch::execute): Call mark_ssa_maybe_undefs. * tree-ssa-reassoc.cc (can_reassociate_op_p): Uninitialized variables can not be reassociated. (init_range_entry): Check for uninitialized variables too. (init_reassoc): Call mark_ssa_maybe_undefs. gcc/testsuite/ChangeLog: PR tree-optimization/112581 * gcc.c-torture/execute/pr112581-1.c: New test. Signed-off-by: Andrew Pinski --- diff --git a/gcc/gimple-if-to-switch.cc b/gcc/gimple-if-to-switch.cc index 8b3a499ac212..96ce1c380a59 100644 --- a/gcc/gimple-if-to-switch.cc +++ b/gcc/gimple-if-to-switch.cc @@ -54,6 +54,7 @@ along with GCC; see the file COPYING3. If not see #include "alloc-pool.h" #include "tree-switch-conversion.h" #include "tree-ssa-reassoc.h" +#include "tree-ssa.h" using namespace tree_switch_conversion; @@ -494,6 +495,8 @@ pass_if_to_switch::execute (function *fun) auto_vec all_candidates; hash_map conditions_in_bbs; + mark_ssa_maybe_undefs (); + basic_block bb; FOR_EACH_BB_FN (bb, fun) find_conditions (bb, &conditions_in_bbs); diff --git a/gcc/testsuite/gcc.c-torture/execute/pr112581-1.c b/gcc/testsuite/gcc.c-torture/execute/pr112581-1.c new file mode 100644 index 000000000000..14081c96d587 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/pr112581-1.c @@ -0,0 +1,37 @@ +/* { dg-require-effective-target int32plus } */ +/* PR tree-optimization/112581 */ +/* reassociation, used to combine 2 bb to together, + that made an unitialized variable unconditional used + which then at runtime would cause an infinite loop. */ +int a = -1, b = 2501896061, c, d, e, f = 3, g; +int main() { + unsigned h; + int i; + d = 0; + for (; d < 1; d++) { + int j = ~-((6UL ^ a) / b); + if (b) + L: + if (!f) + continue; + if (c) + i = 1; + if (j) { + i = 0; + while (e) + ; + } + g = -1 % b; + h = ~(b || h); + f = g || 0; + a = a || 0; + if (!a) + h = 0; + while (h > 4294967294) + if (i) + break; + if (c) + goto L; + } + return 0; +} diff --git a/gcc/tree-ssa-reassoc.cc b/gcc/tree-ssa-reassoc.cc index 3201f9b4d2e9..61f54f07b577 100644 --- a/gcc/tree-ssa-reassoc.cc +++ b/gcc/tree-ssa-reassoc.cc @@ -647,6 +647,9 @@ can_reassociate_op_p (tree op) { if (TREE_CODE (op) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op)) return false; + /* Uninitialized variables can't participate in reassociation. */ + if (TREE_CODE (op) == SSA_NAME && ssa_name_maybe_undef_p (op)) + return false; /* Make sure asm goto outputs do not participate in reassociation since we have no way to find an insertion place after asm goto. */ if (TREE_CODE (op) == SSA_NAME @@ -2600,7 +2603,8 @@ init_range_entry (struct range_entry *r, tree exp, gimple *stmt) } if (TREE_CODE (arg0) != SSA_NAME - || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (arg0)) + || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (arg0) + || ssa_name_maybe_undef_p (arg0)) break; loc = gimple_location (stmt); switch (code) @@ -7418,6 +7422,7 @@ init_reassoc (void) free (bbs); calculate_dominance_info (CDI_POST_DOMINATORS); plus_negates = vNULL; + mark_ssa_maybe_undefs (); } /* Cleanup after the reassociation pass, and print stats if