https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128092
Bug ID: 128092
Summary: [15/16/17 Regression] wrong code at -O2: IPA bit-CP
through a MAX_EXPR pass-through removes a loop
Product: gcc
Version: 15.3.0
Status: UNCONFIRMED
Keywords: wrong-code
Severity: normal
Priority: P3
Component: ipa
Assignee: unassigned at gcc dot gnu.org
Reporter: adamant.pwn at gmail dot com
Target Milestone: ---
Host: x86_64-pc-linux-gnu
Target: x86_64-pc-linux-gnu
Build: x86_64-pc-linux-gnu
int calls;
__attribute__((noinline)) static void loop(unsigned n) {
for (unsigned i = 0; i < n; i++)
calls++;
}
__attribute__((noinline)) static void run(int n) { loop(n > 0 ? n : 0); }
__attribute__((noinline)) void a(void) { run(100); }
__attribute__((noinline)) void b(void) { run(10); }
__attribute__((noinline)) void c(void) { run(0); }
int main(void) {
a();
b();
c();
if (calls != 110)
__builtin_abort();
return 0;
}
$ gcc -O2 t.c && ./a.out
Aborted
loop runs zero iterations for run(100) and run(10). Expected: calls == 110 and
exit status 0.
Fails at -O2: GCC 15.1, 15.3, 16.2 and trunk (Compiler Explorer, 2026-10-22),
and locally with 15.3.0 and 16.2.1 20260810 on x86_64 Linux.
Works: GCC 14.4, Clang, and -O2 -fno-ipa-bit-cp.
An equivalent C++ testcase (std::max(n, 0), plus a printf of the counts) fails
on 15.1, 15.2, 15.3, 16.1, 16.2 and trunk, and works on 10.5, 11.4, 12.5, 13.4
and 14.4. With local GCC 15.3 it also works at -O1, -O3, or -O2 -fno-ipa-cp;
-fno-ipa-vrp and -fno-ipa-cp-clone don't help. Without the run(0) caller, the C
version passes on 16.2 but still fails on 15.3.
In -fdump-ipa-cp-details for the original C++ code, the call has the jump
function "PASS THROUGH: 5, op max_expr 0 (in type int)" into an unsigned
parameter. The caller's propagated bits are "value = 0x0, mask = 0x...6e" (the
union of 0, 10 and 100), but the callee's are "value = 0x0, mask =
0xf..f00000000": all 32 bits of the parameter are treated as known zero, so the
loop is removed as dead.
This report was written with LLM assistance; the testcase and all results above
were run and checked.