https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99755
Bug ID: 99755 Summary: failure to fold a conditional that's a subset of another expression Product: gcc Version: 11.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- GCC successfully folds to false the second conditional expression in f1() but it fails to do the same in f2() and f3(). In addition (and likely as a result), it triggers a bogus -Wmaybe-uninitialized in both functions. Clang and ICC fold all three expressions to false and emit optimal code for all three functions. Initializing the local variable to any value lets GCC fold the conditional and avoid the warning. But then, replacing the test for x != i + 1 in f3() with x != 3 as shown at below the first test case, the conditional is again not folded (making the same change in f2() allows the folding to take place). The bogus -Wmaybe-uninitialized first appeared in 4.9. As far as I can tell none of the failures to fold is a regression. $ cat t.c && gcc -O2 -S -Wall t.c void f1 (int i) { int x; if (i > 1) x = i + 1; if (i == 2 && x != i + 1) // folded to false __builtin_abort (); } void f2 (int i, int j) { int x; if (i > 1 && j > 2) x = i + 1; if (i == 2 && j == 3 && x != i + 1) // not folded __builtin_abort (); } void f3 (int i, int j, int k) { int x; if (i > 1 && j > 2 && k > 3) x = i + 1; if (i == 2 && j == 3 && k == 4 && x != i + 1) // not folded __builtin_abort (); } ;; Function f1 (f1, funcdef_no=0, decl_uid=1943, cgraph_uid=1, symbol_order=0) void f1 (int i) { <bb 2> [local count: 1073741824]: return; } t.c: In function ‘f2’: t.c:17:24: warning: ‘x’ may be used uninitialized in this function [-Wmaybe-uninitialized] 17 | if (i == 2 && j == 3 && x != i + 1) | ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~ t.c: In function ‘f3’: t.c:28:34: warning: ‘x’ may be used uninitialized in this function [-Wmaybe-uninitialized] 28 | if (i == 2 && j == 3 && k == 4 && x != i + 1) | ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~ The following is also not folded: void f3 (int i, int j, int k) { int x = 0; if (i > 1 && j > 2 && k > 3) x = i + 1; if (i == 2 && j == 3 && k == 4 && x != 3) // not folded __builtin_abort (); }