https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120944
Bug ID: 120944 Summary: Incorrect optimization with accessing a volatile structure member Product: gcc Version: 15.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: bmei at broadcom dot com Target Milestone: --- For the following snippet, in function foo, v is wrongly optimized to always true and abort is called unconditionally despite the volatile structure and pointer. Function bar is compiled correctly. I checked that GCC has this behaviour since 10.1.0. #include <stdlib.h> typedef union { int u32; struct { int A:1; int B:2; int C:3; }; } u_t; typedef union { volatile int u[3]; volatile struct { u_t a; int b; int c; }; } DATA; void foo (volatile DATA *d) { d->a.u32 = ~0; u_t u = d->a; int v = u.A; if (v) abort(); } void bar (volatile DATA *d) { d->a.u32 = ~0; if (d->a.A) abort(); } in the tree .optimized dump: void foo (volatile union DATA * d) { union u_t u; <bb 2> [count: 0]: d_2(D)->D.3445.a.u32 ={v} -1; u ={v} d_2(D)->D.3445.a; abort (); } ;; Function bar (bar, funcdef_no=12, decl_uid=3453, cgraph_uid=13, symbol_order=12) void bar (volatile union DATA * d) { <unnamed-signed:1> _1; <bb 2> [local count: 1073741824]: d_3(D)->D.3445.a.u32 ={v} -1; _1 ={v} d_3(D)->D.3445.a.D.3437.A; if (_1 != 0) goto <bb 3>; [0.00%] else goto <bb 4>; [100.00%] <bb 3> [count: 0]: abort (); <bb 4> [local count: 1073741824]: return; }