https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128869
Bug ID: 128869
Summary: [17 Regression] wrong code at -O1:
__builtin_sub_overflow into signed _BitInt(255) loses
the overflow flag after a switch on _BitInt(257)
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: wrong-code
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: haoxintu at gmail dot com
Target Milestone: ---
Hi,
The following program aborts at -O1 (passes at -O0, -O2, -O3, -Os):
$cat repro.c
[[gnu::noipa]] int f (unsigned _BitInt(1) a, unsigned _BitInt(1) c)
{
signed _BitInt(257) s = c, y;
switch (s)
{
case 14: y = a; break;
case (signed _BitInt(257)) 0x1e768b9a0533c4b55uwb: y = 5; break;
default: y = 0;
}
signed _BitInt(255) r;
/* 2^255 - 0 does not fit in signed _BitInt(255): must report overflow. */
return __builtin_sub_overflow ((unsigned _BitInt(256)) 1 << 255, y, &r);
}
int main (void)
{
if (f (0, 0) != 1)
__builtin_abort ();
return 0;
}
$ gcc -O1 repro.c && ./a.out
Aborted
With a = c = 0 the switch takes the default arm (y = 0), so the subtraction is
2^255 - 0, which does not fit in
signed _BitInt(255): __builtin_sub_overflow must return 1. At -O1 it returns 0.
## Affected Versions
- gcc 15.2, gcc 16: correct at all levels
- trunk 9571dd0b721 (2026-11-17): abort at -O1
Reproduced in Godbolt: https://godbolt.org/z/hhxf43hxP
Thanks.