https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129276
Bug ID: 129276
Summary: [14/15/16/17 Regression] wrong code at -O2: compound
literal assigned in place over its source
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: wrong-code
Severity: normal
Priority: P3
Component: middle-end
Assignee: unassigned at gcc dot gnu.org
Reporter: jeffchen006 at gmail dot com
Target Milestone: ---
Target: x86_64-pc-linux-gnu
Tested with gcc version 17.0.0 20261203 (experimental), r17-7143-g592f605117e,
x86_64-pc-linux-gnu.
```c
struct T { int v[8]; };
struct Q { struct T t; };
struct S { int a; struct T t; };
void h(struct S *p, struct Q *q) { *p = (struct S){ 7, q->t }; }
void (*hp)(struct S *, struct Q *) = h;
union { struct S s; struct Q q; } u;
int main() {
u.q.t.v[4] = 1;
hp(&u.s, &u.q);
return u.s.t.v[4] != 1;
}
```
$ gcc -O0 t.c && ./a.out; echo $?
0
$ gcc -O2 t.c && ./a.out; echo $?
1
-fno-strict-aliasing fixes it (0).
The compound literal is its own object (C11 6.5.2.5p5): its value, with q->t,
is complete before the store to *p, so 6.5.16.1p3 (overlap) does not apply.
Each union member is read only through the member that last stored it.
Bisected to r129743 = 489f2598af8 (2007-10-29, PR33723); its parent is fine
(bisect at -O2 -fno-schedule-insns2). The commit makes c_gimplify_expr replace
`x = (struct T){...}` by `x = {...}` (the CONSTRUCTOR of the literal).
gimplify_init_constructor then builds the CONSTRUCTOR directly in *p (gimple
dump: `p->a = 7; p->t = q->t;`), because gimplify_init_ctor_preeval_1
(gimplify.cc) finds no overlap: its only test for an indirect source is
`alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t))`, and struct S
and struct Q do not conflict. With p == q, `p->a = 7` overwrites q->t.v[0] and
`p->t = q->t` is a shifted overlapping copy. The source comment there says:
"this whole analysis doesn't look conservative".