https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128714
Bug ID: 128714
Summary: [12/13/14/15/16/17 Regression] wrong code: SLP vector
load built from a restrict and a non-restrict load
keeps the restrict clique/base, DSE deletes a store
the other lane reads
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: -O2
* GCC trunk wrong code: the basic-block SLP vectorizer groups the loads a[0]
(restrict, MR_DEPENDENCE_CLIQUE/BASE
* from g's `int *restrict a`) and p[1] (plain) into one vector load, and gives
the vector load the restrict
* clique/base of the first group member (vect_copy_ref_info). DSE then
deletes `*q = 5` as dead although the
* p[1] lane of the vector load reads it.
* Flags: -O2, -O3, -O2 -fno-strict-aliasing, -O1 -ftree-slp-vectorize, -O2
-fno-tree-dse (RTL also drops it).
* OK: -O0, -O1, -Os, -O2 -fno-tree-slp-vectorize, gcc 11.4 -O3, clang -O3.
* No UB: a[0] (m[0]) is read through a but never modified, so restrict imposes
nothing; p[1] (m[1]) is written
* through q and read through p, neither of which is based on a.
* EXPECTED: "3 15 7", exit 0. BUGGY: "3 6 7", exit 1.
*/
#include <stdio.h>
int m[2];
int r[2];
static inline void g (int *restrict a, int *p, int *q, int *o)
{
*q = 5; /* q == p + 1: writes p[1] */
int x0 = a[0]; /* restrict load; m[0] is never modified */
int x1 = p[1]; /* must read 5 */
o[0] = x0 * 3;
o[1] = x1 * 3;
*q = 7;
}
__attribute__((noipa)) void h (int *p, int *q, int *o) { g (p, p, q, o); }
int main (void)
{
m[0] = 1; m[1] = 2;
h (m, m + 1, r);
printf ("%d %d %d\n", r[0], r[1], m[1]);
return r[1] != 15;
}
```
Resulted in this output:
```
$ gcc -O2 repro.c && ./a.out
3 6 7 (exit 1)
(expected 3 15 7: -O0, -O1, -Os, -O2 -fno-tree-slp-vectorize, GCC 11.4 and
clang print it)
```
A loop variant (attached variant-loop-lv1.c, stride-2 group {a[2i], p[2i+1]})
fails the same way at -O2/-O3.
Trunk: https://godbolt.org/z/3rPc8xToY (x86-64 gcc trunk, -O2, executed; prints
3 6 7 as of 2026-11-14; GCC 12.3 through 16.1 also wrong, GCC 11.4 OK)
Root cause:
After g is inlined into h, a[0] carries MR_DEPENDENCE_CLIQUE 2 / BASE 1 (from
`int *restrict a`) and p[1] carries
clique 2 / base 0. BB SLP groups the two loads into one `vector(2) int` load;
vectorizable_load copies the ref info of
the first group member onto the vector access (vect_copy_ref_info (data_ref,
DR_REF (first_dr_info->dr)) in
tree-vect-stmts.cc), so the vector load is `MEM <vector(2) int> [(int *)p
clique 2 base 1]`. DSE then disambiguates
the earlier store `*q = 5` (clique 2 base 0) against it as "restrict, cannot
alias" and deletes it, although lane 1
(p[1], with q == p + 1) reads it. Group formation never compares
MR_DEPENDENCE_CLIQUE/BASE of the members.
No UB: m[0] is read through a but never modified, so the restrict qualification
imposes no constraint.
Fix: when the members of a data-ref group differ in MR_DEPENDENCE_CLIQUE/BASE,
clear them on the vector access
(as store-merging's get_alias_type_for_stmts does), or refuse to group them.