https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128132
Bug ID: 128132
Summary: wrong code at -O2 and -O3 summing linear and
multiplicative induction variables
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: kyuwoncho18 at gmail dot com
Target Milestone: ---
Target: x86_64-pc-linux-gnu
#include <stdio.h>
int f(int j)
{
int s = 0, k = 1;
for (int i = 0; i < 16; i++) {
s += j + k;
j += 1;
k *= 2;
}
return s;
}
int main(void)
{
printf("%d\n", f(0));
}
godbolt: https://godbolt.org/z/oaha6G3sj
The sum is (0 + ... + 15) + (1 + 2 + ... + 32768) = 65655, with all arithmetic
in range for f(0). On x86-64, GCC trunk (17.0.0 20261023) prints 376 with -O2
or -O3, while -O0, -O1 or -O2 -fno-tree-loop-vectorize prints 65655. GCC 16.2.0
also prints 376 with -O3, whereas GCC 15.3.0 prints 65655.