https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129042
Bug ID: 129042
Summary: [16/17 Regression] wrong code at -O2 and -Os: VRP
narrows `(unsigned) x + 1` to a signed char addition
on a never-taken branch, and PRE then uses it on the
taken one
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
__attribute__((noinline)) unsigned f(signed char e) {
return (unsigned)((unsigned char)e < e ? (unsigned char)e : e) + 1;
}
int main(void) {
/* (unsigned char)127 < 127 is false, so f returns (unsigned)127 + 1 */
if (f(127) != 128)
__builtin_abort();
return 0;
}
$ gcc -O0 repro.c && ./a.out # ok
$ gcc -O2 repro.c && ./a.out # also -Os
Aborted
$ gcc -O2 -fno-tree-vrp repro.c && ./a.out # ok (also -fno-tree-pre
-fno-code-hoisting)
```
Reproduced in Godbolt (gcc trunk, -O2 vs -O0): https://godbolt.org/z/xxEfTffG6
## Checked versions
- gcc trunk 4eca40b54dd (2026-11-25, --enable-checking=yes): -O2, -Os abort;
-O0, -O1, -O3 ok
- gcc 16.0.1 20260315 (r16-8100), gcc 16.2 (Godbolt): -O2 aborts
- gcc 15.2.0, 15.3, and 14.4 (Godbolt): ok
- clang 21: ok; -fsanitize=address,undefined clean
Thanks.