tree-optimization/110228 - avoid undefs in ifcombine more thoroughly

The following replaces the simplistic gimple_uses_undefined_value_p
with the conservative mark_ssa_maybe_undefs approach as already
used by LIM and IVOPTs.  This is to avoid exposing an unconditional
uninitialized read on a path from entry by if-combine.

	PR tree-optimization/110228
	* tree-ssa-ifcombine.cc (pass_tree_ifcombine::execute):
	Mark SSA may-undefs.
	(bb_no_side_effects_p): Check stmt uses for undefs.

	* gcc.dg/torture/pr110228.c: New testcase.
	* gcc.dg/uninit-pr101912.c: Un-XFAIL.

(cherry picked from commit b083203f053f1666e9cc1ded2abdf4e1688d1ec0)
This commit is contained in:
Richard Biener 2023-07-04 10:29:26 +02:00
parent c7d995dfb1
commit 79b6a4875f
3 changed files with 42 additions and 2 deletions

View File

@ -0,0 +1,34 @@
/* { dg-do run { target x86_64-*-* i?86-*-* } } */
/* { dg-require-effective-target lp64 } */
unsigned a[4] = {1,1,1,1};
unsigned tt1 = 0;
__attribute__((noipa))
static void bug(unsigned * p, unsigned *t, int n, int t2)
{
for(int i = 0; i < n; i++)
{
_Bool LookupFlags ;
unsigned v = t[i];
unsigned tt = tt1;
if (v == 0)
LookupFlags = 0;
else if (v == 1)
LookupFlags = 1;
if (LookupFlags) {
tt|=3u;
LookupFlags = 0;
}
asm("movq $-1, %q1":"+a"(LookupFlags));
*p = tt;
}
}
int main()
{
unsigned r = 42;
bug(&r,a, sizeof(a)/sizeof(a[0]), 1);
__builtin_printf("%u\n", r);
if (r != 3) __builtin_abort();
}

View File

@ -11,7 +11,7 @@ tzloadbody (void)
for (int i = 0; i < n; i++)
{
int corr = getint ();
if (corr < 1 || (corr == 1 && !(leapcnt == 0 || (prevcorr < corr ? corr == prevcorr + 1 : (corr == prevcorr || corr == prevcorr - 1))))) /* { dg-bogus "uninitialized" "pr101912" { xfail *-*-* } } */
if (corr < 1 || (corr == 1 && !(leapcnt == 0 || (prevcorr < corr ? corr == prevcorr + 1 : (corr == prevcorr || corr == prevcorr - 1))))) /* { dg-bogus "uninitialized" "pr101912" } */
return -1;
prevcorr = corr;

View File

@ -128,7 +128,6 @@ bb_no_side_effects_p (basic_block bb)
gassign *ass;
enum tree_code rhs_code;
if (gimple_has_side_effects (stmt)
|| gimple_uses_undefined_value_p (stmt)
|| gimple_could_trap_p (stmt)
|| gimple_vuse (stmt)
/* We need to rewrite stmts with undefined overflow to use
@ -153,6 +152,12 @@ bb_no_side_effects_p (basic_block bb)
should handle this. */
|| is_gimple_call (stmt))
return false;
ssa_op_iter it;
tree use;
FOR_EACH_SSA_TREE_OPERAND (use, stmt, it, SSA_OP_USE)
if (ssa_name_maybe_undef_p (use))
return false;
}
return true;
@ -842,6 +847,7 @@ pass_tree_ifcombine::execute (function *fun)
bbs = single_pred_before_succ_order ();
calculate_dominance_info (CDI_DOMINATORS);
mark_ssa_maybe_undefs ();
/* Search every basic block for COND_EXPR we may be able to optimize.