https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128958
Bug ID: 128958
Summary: [14/15/16/17 Regression] wrong code at -O1 and above:
SCCP final value replacement (chrec_apply) checks that
the wrapped unsigned increment fits the signed type,
so a loop with a negative step gets an overflowing
signed add
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: 0599jiangyc at gmail dot com
Target Milestone: ---
The following code:
```c
/* GCC trunk wrong code at -O1+ (GCC 14+ regression; 13.4 OK).
Godbolt: compiler cgsnapshot (x86-64 gcc trunk, C), flags: -O1 (also
-O2/-O3/-Os)
Expected output 1, exit 0. Miscompiled: prints 0, exits 1.
x drops from 2000000000 to -2000000000 in 100 steps of -40000000: no
signed overflow. SCCP final value replacement (chrec_apply) computes the
total increment 100 * -40000000 in unsigned int, gets 294967296 (wrapped),
sees that it "fits" int and emits the signed x_12 = x_4 + 294967296,
which overflows for x = 2000000000; the following compare is then folded
to false. */
__attribute__((noipa)) int h(int x)
{
for (int i = 0; i < 100; i++)
x -= 40000000;
return x < -1900000000; /* x == -2000000000 here */
}
int main(void)
{
int r = h(2000000000);
__builtin_printf("%d\n", r);
return r != 1;
}
```
Resulted in this output:
```
$ gcc -O1 repro.c && ./a.out (also -O2, -O3, -Os)
0
(exit 1; gcc -O0, GCC 13.4 -O2 and clang -O2 print 1; gcc -O0
-fsanitize=undefined is clean)
```
Trunk: https://godbolt.org/z/EfG6YETcd (x86-64 gcc trunk, -O1, executed;
reproduces as of 2026-11-19; GCC 11-13 OK, 14.2/15.2/16.1/trunk wrong)