https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96875
Bug ID: 96875 Summary: Aliased pointers to union members result in different output with optimisation level. Product: gcc Version: 11.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: iains at gcc dot gnu.org Target Milestone: --- For my 0.02 GBP this is ill-conditioned code, but apparently either the try is still out - or it has been decided to consider it OK. struct s1 { double d; }; struct s2 { double d; }; union u { struct s1 x; struct s2 y; }; __attribute__((__noinline__)) double f(struct s1 *a, struct s2 *b) { a->d = 1.0; return b->d + 1.0; } int main () { union u X; return f (&X.x, &X.y) == 2.0; } ==== O1: f: .LFB0: .cfi_startproc fmov d0, 1.0e+0 str d0, [x0] ldr d1, [x1] fadd d0, d1, d0 ret .cfi_endproc this is the "expected" result. ===== O2: f: .LFB0: .cfi_startproc ldr d1, [x1] fmov d0, 1.0e+0 str d0, [x0] fadd d0, d1, d0 ret .cfi_endproc this is some random answer. ==== I'd hazard a guess that something decides that pointers to different types can't alias, so it's OK to move things around... but the user code bends over backwards to force the different entities to alias.