Summary: | local array not prompted to static | ||
---|---|---|---|
Product: | gcc | Reporter: | Barry Revzin <barry.revzin> |
Component: | tree-optimization | Assignee: | Not yet assigned to anyone <unassigned> |
Status: | RESOLVED DUPLICATE | ||
Severity: | enhancement | CC: | davidfromonline, jakub, jakub, jamborm, jason, ville.voutilainen, webrown.cpp |
Priority: | P3 | Keywords: | missed-optimization |
Version: | 10.0 | ||
Target Milestone: | --- | ||
See Also: |
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93102 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59863 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114342 |
||
Host: | Target: | ||
Build: | Known to work: | ||
Known to fail: | Last reconfirmed: | 2021-08-28 00:00:00 |
Description
Barry Revzin
2021-02-13 21:55:26 UTC
I don't see anything wrong on that, the constexpr var is not evaluated at runtime, it has a constant initializer. So in the end it is static const int doy1[] = { 306, 337, 31, 61, 92, 122, 153, 184, 214, 245, 275 }; int foo1(int m) { return doy1[m]; } int foo2(int m) { static const int doy2[] = { 306, 337, 31, 61, 92, 122, 153, 184, 214, 245, 275 }; return doy2[m]; } int foo3(int m) { const int doy3[] = { 306, 337, 31, 61, 92, 122, 153, 184, 214, 245, 275 }; return doy3[m]; } and there is nothing C++ specific on it. The compiler may promote the doy3 variable from automatic variable into static variable if it can prove it is safe to do so (it would be unsafe e.g. if foo3 could be called recursively and compare the addresses of the var between the recursive invocations). GCC does that early (during gimplification) if the variable is not address taken, but the array reference implies address taking. And, especially for the original testcase where operator[] is overloaded, it really can't do better early, so we'd need an optimization that would promote automatic vars to static when possible later (e.g. after inlining) if it can prove it is safe to do so. Wonder if SRA doesn't have the right infrastructure to optimize this. In any case, GCC 12 material. Confirmed, there is another bug which is very similar to this one but filed years ago. |