https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129291
Bug ID: 129291
Summary: [8/9/10/11/12/13/14/15/16/17 Regression] wrong code
with -O3 -floop-nest-optimize: original schedule drops
an intermediate loop after loop distribution
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
/* Godbolt: x86-64 gcc (trunk), flags: -O3 -floop-nest-optimize */
unsigned B[2048], C[2048], RB[2048], RC[2048];
__attribute__((noipa)) void f(int m)
{
for (int i0 = 4; i0 <= m + 2; i0++)
for (unsigned short i1 = 20; i1 > 0; i1 -= 2)
{
C[i0 - i1 + 862] = 8u + i1;
for (unsigned i2 = 5; i2 < 24; i2++)
{
C[-2 * i0 + (int) i2 + 19] = C[0];
B[3 * i0 - 2 * i1 + 2 * (int) i2 + 18] = 7u + i2;
}
}
}
__attribute__((noipa, optimize("no-loop-nest-optimize"))) void g(int m)
{
for (int i0 = 4; i0 <= m + 2; i0++)
for (unsigned short i1 = 20; i1 > 0; i1 -= 2)
{
RC[i0 - i1 + 862] = 8u + i1;
for (unsigned i2 = 5; i2 < 24; i2++)
{
RC[-2 * i0 + (int) i2 + 19] = RC[0];
RB[3 * i0 - 2 * i1 + 2 * (int) i2 + 18] = 7u + i2;
}
}
}
volatile int vm = 10;
int main(void)
{
for (int i = 0; i < 2048; i++) { B[i] = RB[i] = i * 13u ^ 5; C[i] = RC[i] = i
* 3u + 2; }
f(vm);
g(vm);
for (int i = 0; i < 2048; i++)
if (B[i] != RB[i] || C[i] != RC[i])
{
__builtin_printf("B[%d] = %u (expected %u), C = %u (expected %u)\n", i,
B[i], RB[i], C[i], RC[i]);
return 1;
}
return 0;
}
```
Resulted in this output:
```
$ gcc -O3 -floop-nest-optimize repro.c && ./a.out
B[4] = 14 (expected 12), C = 2 (expected 2) (exit 1)
(correct: -O0, -O3, -O2 -floop-nest-optimize, -O3 -floop-nest-optimize
-fno-tree-loop-distribution;
UBSan/ASan -O0 clean)
```
Trunk: https://godbolt.org/z/7snETsYdo (x86-64 gcc trunk, -O3
-floop-nest-optimize, executed; wrong as of 2026-11-17;
GCC 9.3, 11.4, 12.1, 13.3, 14.2, 15.1 also wrong; GCC 7.5 correct)