https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128641
Bug ID: 128641
Summary: [14/15/16/17 Regression] wrong code at -O2: predictive
commoning reads stale value (long store + long long
load, modref summary of a call)
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: alias, needs-bisection, wrong-code
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: jeffchen006 at gmail dot com
Target Milestone: ---
int printf(const char *, ...);
void *malloc(unsigned long);
int n;
__attribute__((noinline)) void f(char *p) { *(long long *)p = 7; }
int main(void) {
char *b = malloc(72);
f(b);
long long s = 0;
for (int k = 0; k < (n & 7) + 1; k++) {
s = s * 31 + *(long long *)(b + k * 8);
*(long *)(b + 8 + k * 8) = k;
}
printf("%lld\n", s);
}
$ gcc -O0 t.c && ./a.out
7
$ gcc -O2 t.c && ./a.out
0
Expected: 7. f stores the long long 7 at b. The loop runs once and reads it: s
= 7. -O2 gives 0.
Suspected cause: predcom puts a long store and a long long load in one chain
and makes the init load with the store type; with the IPA modref summary of f,
TBAA then skips the call.
Avoided by: -fno-predictive-commoning, -fno-ipa-modref, -fno-strict-aliasing.
Known to fail: 11.4.0, 12.5.0, 13.4.0, 14.3.0, 15.2.0, 16.2.0, 17.0.
Known to work: 7.5.0, 8.5.0, 10.5.0.
-O0 -fsanitize=undefined,address is clean.
gcc version 17.0.0 20261111 (experimental) (a2a11abfad3), x86_64-pc-linux-gnu
Configured with: --enable-languages=c,c++ --disable-multilib
--disable-bootstrap --enable-checking=release