https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77706
Bug ID: 77706 Summary: Optimize away some local static constructors Product: gcc Version: 7.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: jakub at gcc dot gnu.org Target Milestone: --- Similar to PR77705, but even harder: struct S { int a; }; void baz (S *); #if __cpp_constexpr >= 200704 constexpr #endif inline S foo () { return (S) { 2 }; } void bar () { static S t = foo (); baz (&t); } inline void bar2 () { static S t = foo (); baz (&t); } void bar3 () { bar2 (); } Here we have: static struct S t; unsigned char _1; int _2; <bb 2>: _1 = __atomic_load_1 (&_ZGVZ3barvE1t, 2); if (_1 == 0) goto <bb 4>; else goto <bb 3>; <bb 3>: goto <bb 6>; <bb 4>: _2 = __cxa_guard_acquire (&_ZGVZ3barvE1t); if (_2 != 0) goto <bb 5>; else goto <bb 3>; <bb 5>: MEM[(struct S *)&t] = 2; __cxa_guard_release (&_ZGVZ3barvE1t); <bb 6>: it would be nice to turn that into static struct S t = { 2 }; and get rid of the guard var and atomics/__cxa_guard*, but we need to consider ABI issues if the guard var is visible outside of the TU.