struct S { int a; }; void baz (S *); #if __cpp_constexpr >= 200704 constexpr #endif inline S foo () { return (S) { 2 }; } S s = foo (); for -std=c++98 -O2 we have the runtime initializer. If we had some pass that would analyze the ctor functions after they were inlined into and if they only write constants into file-scope vars defined in the current TU (except for comdat vars?) turn the ctor function into static initializers for the file-scope vars. Not sure if we can do it generally for any (non-comdat?) vars, or if e.g. the C++ FE wouldn't have to mark such vars some way for us, or if we wouldn't need to analyze if there aren't some ctors in the same TU that would run earlier (higher (or is that lower?) priority) and could access those vars, or for vars visible outside of the TU if some other TU's ctor couldn't access them. An argument for some guidance from the C++ FE would be that say in C: int a __attribute__((nocommon)); static __attribute__((constructor)) void foo (void) { a = 6; } some other TU could say in heither priority ctor expect to see a == 0.
Confirmed. I would like to see us moving towards keeping the individual static ctor functions separate and registered in the varpool (and leave building of the actual CTOR function wrapping them eventually to IPA). This would allow to run early opts over them and then eventually turn them into static initialization.
Dup. *** This bug has been marked as a duplicate of bug 4131 ***