https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128740
Bug ID: 128740
Summary: wrong code at -O2: phiprop moves an aggregate copy
above a load of another type of the same memory (TBAA
used for an anti-dependence) since r7-103
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: jeffchen006 at gmail dot com
Target Milestone: ---
Target: x86_64-pc-linux-gnu
int printf(const char *, ...);
void *malloc(unsigned long);
struct S { int a, b; };
struct S s1 = {1, 2}, s2 = {3, 4};
int n = 1;
int main(void) {
char *m = malloc(8 * n);
for (int i = 0; i < n; i++)
*(float *)(m + 8 * i) = 1;
float s = 0;
for (int i = 0; i < n; i++) {
struct S *d = (struct S *)(m + 8 * i);
if (i == n) s = 1;
struct S *p = i & 1 ? &s1 : &s2;
s += *(float *)d;
*d = *p;
}
printf("%d\n", (int)s);
}
$ gcc -O0 t.c && ./a.out
1
$ gcc -O2 t.c && ./a.out
0
-O2 and -O3 fail; -O1, -O2 -fno-tree-phiprop and -O2 -fno-strict-aliasing give
1.
Expected 1. The float read of each slot reads the float stored before it. Only
after that read, the struct copy gives the 8 bytes the effective type struct S.
No read uses the wrong type (C11 6.5p6).
phiprop replaces *d = *p (p = PHI <&s1, &s2>) by *d = s1 and *d = s2 on the
incoming edges, which are above the float load s += *(float *)d.
Cause: propagate_with_phi (tree-ssa-phiprop.cc), aggregate-copy case:
if (ref_maybe_used_by_stmt_p (vuse_stmt, gimple_assign_lhs (use_stmt)))
goto next;
This uses the default tbaa_p = true (tree-ssa-alias.h), so a float load does
not "use" a struct S lhs, and the store is moved above the load (a
write-after-read reorder). TBAA is not valid for anti-dependences: the later
struct store may change the effective type of the storage. Compare
refs_anti_dependent_p (tree-ssa-alias.cc), which disables TBAA for this
direction, and the same file's stmt_may_clobber_ref_p_1 (other_store, &read,
false). The phiprop1 dump shows `*d = s1` and `*d = s2` inserted before
`MEM[(float *)d]`; -fdisable-tree-phiprop1 also gives 1.
History: the aggregate-copy motion came with r7-103-gea8927ea15c (PR70171,
2016), without a read check. r8-2013-ge8dd1313132 (PR81365, 2017) added this
read check, but with TBAA.
The loop form above fails on 14.3 to trunk (12.5 and 13.4 hide it in another
pass). A function form fails on every release 7.5 to trunk:
int printf(const char *, ...);
void *malloc(unsigned long);
struct S { int a, b; };
struct S s1 = {1, 2}, s2 = {3, 4};
int n = 1;
float f(struct S *d) {
struct S *p = n ? &s1 : &s2;
float s = *(float *)d;
*d = *p;
return s;
}
int main(void) {
struct S *d = malloc(8);
*(float *)d = 1;
printf("%d\n", (int)f(d));
}
$ gcc -O0 t.c && ./a.out
1
$ gcc -O2 -fno-inline t.c && ./a.out
0
-O0 -fsanitize=undefined,address is clean.
gcc -Wall -Wextra gives no warnings. -fno-strict-aliasing hides it, as expected
for a TBAA bug. The program is still valid: each slot is read as float only
after a float store, and only then the struct copy gives the 8 bytes the
effective type struct S (C11 6.5p6); no read uses the wrong type.
Possible fix: One possible fix, which we tested locally (not bootstrapped), is
to pass false as the third argument (tbaa_p) of ref_maybe_used_by_stmt_p in
propagate_with_phi, so that a type difference alone no longer lets the
aggregate copy move above a read. It fixes both tests here, and pr81365.C and
pr70171.C still pass.
gcc version 17.0.0 20261114 (experimental), r17-6275-gfceddda853f,
x86_64-pc-linux-gnu
Configured with: --enable-languages=c,c++ --disable-multilib
--disable-bootstrap --enable-checking=release