https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77705
Bug ID: 77705 Summary: Optimize away some 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: --- 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.