https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129080
Bug ID: 129080
Summary: [14/15/16/17 Regression] wrong code at -O1 and above:
match.pd folds ((T)bswap(x)) >> C to a byte shift
although T is narrower than the bswap (bit-field /
_BitInt)
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,
$cat repro.c
struct S { int c : 13; };
__attribute__((noinline)) int f(unsigned short b) {
struct S s;
s.c = __builtin_bswap16(b); /* 0xD8F7 -> low 13 bits 0x18F7 = -1801 */
return s.c >> 12; /* -1801 >> 12 == -1 */
}
int main(void) {
if (f(0xF7D8) != -1)
__builtin_abort();
return 0;
}
$ gcc -O0 repro.c && ./a.out # ok
$ gcc -O1 repro.c && ./a.out # also -O2, -O3, -Os
Aborted
Reproduced in Godbolt (gcc trunk, -O1 vs -O0): https://godbolt.org/z/eYWxMTMEW
## Checked versions
- gcc trunk 4eca40b54dd (2026-11-25, --enable-checking=yes): -O1 and above
abort; -O0 ok (all three testcases)
- gcc 16.0.1, 15.2.0: abort at -O1 and above
- gcc 12.2, 12.3, 13.4, 14.4 (Godbolt): aborts at -O1
- gcc 11.4, 9.5, 7.5 (Godbolt): ok
- clang 21: ok; -fsanitize=address,undefined clean
Thanks.