-fmerge-all-constants misses a very simple optimization scenario involving an array: extern void g(const char* c); void f() { const char c[] = "12345"; g(c); } produces a local array (with or without the flag). If `const char c[]` is replaced by `const char* c`, the above code produces a global array as expected. This seems to be the case in every GCC version I have tested, starting from 4.9.
It isn't so much about -fmerge-all-constants, this is about not eliding the automatic variable for the string constant. Note it may be the language standard doesn't permit changing the linkage of 'c', code may end up writing to it, expecting automatic storage. So this eliding could be done by the frontend already. Note there's heuristic in the gimplifier to eventually emit a copy from a string constant rather than doing inline initialization but the gimplifier cannot really elide the variable. Also this must have come up in the past.
Just use static if you want to mege the constant...
(In reply to Richard Biener from comment #1) > It isn't so much about -fmerge-all-constants, this is about not eliding > the automatic variable for the string constant. > > Note it may be the language standard doesn't permit changing the > linkage of 'c', code may end up writing to it, expecting automatic > storage. > Richard, the same optimization is performed by Clang but it requires -fmerge-all-constants (GCC indeed doesn't, in the pointer case). So, perhaps, this optimization should be still related to this flag since the language standard might not allow for such (and the flag breaks compliance anyway)? > So this eliding could be done by the frontend already. Note there's > heuristic > in the gimplifier to eventually emit a copy from a string constant rather > than doing inline initialization but the gimplifier cannot really elide the > variable. > > Also this must have come up in the past.
See bug 59863, with discussion of the requirement for distinct copies of the array in recursive calls to have distinct addresses. (Thus, this would definitely not be valid without -fmerge-all-constants, as the example in this bug could involve a recursive call - whereas the example in bug 59863 does not involve the address of the const array escaping, so the transformation would be valid in that example even without -fmerge-all-constants. It's not valid for code to write to an array defined as const, however.)
duplicate then. *** This bug has been marked as a duplicate of bug 59863 ***