Bug 77705 - Optimize away some static constructors
Summary: Optimize away some static constructors
Status: RESOLVED DUPLICATE of bug 4131
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 7.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2016-09-23 09:38 UTC by Jakub Jelinek
Modified: 2024-11-19 18:04 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2016-09-23 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jakub Jelinek 2016-09-23 09:38:04 UTC
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.
Comment 1 Richard Biener 2016-09-23 09:43:56 UTC
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.
Comment 2 Drea Pinski 2024-11-19 18:04:54 UTC
Dup.

*** This bug has been marked as a duplicate of bug 4131 ***