https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128688
Bug ID: 128688
Summary: [sccvn] wrong code at -O1 and above: a load value
found via restrict (clique/base) disambiguation is
recorded at a frozen VUSE and reused for the same
reference outside the restrict scope
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: wrong code at -O1 and above (also GCC 11.4; -fno-strict-aliasing does
not help).
* Value numbering reuses a value it found inside an inlined function using
restrict
* (clique/base) disambiguation for a load of the same address outside the
restrict scope.
* Godbolt: x86-64 gcc (trunk), flags: -O2 (correct: -O0, clang -O2)
* EXPECTED: prints 700000001, exit 0. BUGGY: prints 700000005, exit 1.
*/
#include <stdio.h>
typedef unsigned long long u64;
static inline u64 g(u64 *restrict p, unsigned char *q, int c) {
*q = 1; /* byte 0, not through p */
((unsigned char *)p)[4] = 7; /* byte 4, through p */
u64 r = 0;
if (c) r = *p; /* not executed (c == 0): no restrict UB */
return r;
}
__attribute__((noinline)) u64 f(u64 *x, unsigned char *y, int c) {
*x = 5;
u64 r = g(x, y, c);
return r + *x; /* outside g: must see *y = 1 */
}
int main(void) {
u64 a;
u64 r = f(&a, (unsigned char *)&a, 0);
printf("%llx\n", r);
return r != 0x700000001ULL;
}
```
Resulted in this output:
```
$ gcc -O2 repro.c && ./a.out (also -O1, -O3, -O2 -fno-strict-aliasing;
GCC 11.4 too)
700000005
(exit 1; gcc -O0 and clang -O2 print 700000001)
```
Trunk: https://godbolt.org/z/z56nocWKn (x86-64 gcc trunk, -O2, executed;
reproduces as of 2026-11-12)
Root cause: after inlining `g`, the (non-executed) load `r = *p` carries
MR_DEPENDENCE_CLIQUE/BASE from the restrict parameter. When FRE value-numbers
it, the alias walk (refs_may_alias_p_2 in gcc/tree-ssa-alias.cc) skips the
store `*q = 1` because it has a different restrict base in the same clique, and
the value is assembled from the partial definitions `*x = 5` and `((unsigned
char *)p)[4] = 7` (`vn_walk_cb_data::push_partial_def`, which freezes
`last_vuse`). The result is then inserted into the reference hashtable
(`vn_reference_lookup_or_insert_for_pieces`) at the load's own VUSE. The later
load `*x` in `f`, outside the restrict scope (no clique), has the same VUSE and
`vn_reference_eq`/`vn_reference_op_eq` ignore clique/base for MEM_REFs, so it
gets the stale value 0x700000005 although `*y = 1` modified byte 0.
`-fno-tree-fre` does not help either, because PRE/code hoisting do the same
lookup.
This is a sibling of PR128637: the same flaw of recording a value at a frozen
`last_vuse` that was only valid thanks to a disambiguation which the hashtable
key does not capture, but there it is TBAA (hidden by -fno-strict-aliasing),
here it is restrict clique/base with identical types and alias sets.
Suggested fix: when the walk disambiguated a store using information that is
not part of the hashtable key (alias set/TBAA, MR_DEPENDENCE_CLIQUE/BASE), do
not insert the result at the frozen `last_vuse` (insert it at the original VUSE
or not at all), or include clique/base in the MEM_REF operand comparison.