https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129010
Bug ID: 129010
Summary: [17 Regression] wrong code at -O1, -O2, and -Os:
combine folds (long)((__int128 x >> 33) & -2147483648)
!= 0 to false
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: wrong-code
Severity: normal
Priority: P3
Component: rtl-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: haoxintu at gmail dot com
Target Milestone: ---
Hi,
$cat repro-small.c
int a;
int main(void) {
long b = 0;
for (int c = 0; c < 112; c += 9) {
int *d = &a;
unsigned __int128 v = (unsigned __int128)(unsigned long)((unsigned
__int128)c >> 2) << 64 | (d != 0);
b += (long)((__int128)v >> 33) != 0; /* nonzero iff c >> 2 != 0 */
}
if (b != 12)
__builtin_abort();
return 0;
}
$ gcc -O0 repro-small.c && ./a.out # ok
$ gcc -O1 repro-small.c && ./a.out # also -O2, -Os
Aborted
$ gcc -O1 -fdisable-rtl-combine repro-small.c && ./a.out # ok (also
-fno-tree-ter)
The optimized GIMPLE is correct. ivopts turns `(c >> 2) << 64` into an IV that
steps by `9 << 62`, and the test becomes
Reproduced in Godbolt (gcc trunk, -O1 vs -O0): https://godbolt.org/z/5q3c4hGTj
## Checked versions
- gcc trunk 1ed04a18ef6 (2026-11-23, --enable-checking=yes): -O1, -O2, -Os
abort; -O0, -O3 ok
- gcc 16.0.1 20260315 (r16-8100), gcc 15.2.0: ok
- clang: ok
Thanks.