https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99091

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org
          Component|c++                         |tree-optimization

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
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.

Reply via email to