https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118970
Bug ID: 118970
Summary: Const local array not considered to be const unless
`static` is added
Product: gcc
Version: 14.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: agriff at tin dot it
Target Milestone: ---
in the following code example
```
int foo(int x) {
static int tt[] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
};
return tt[x & 15];
}
```
the code generated is ok (the array ends up being statically initialized and
just looked up in the function); so the optimizer sees that no one can legally
access and change its content.
In the other hand if `static` is removed this doesn't happen (the data is
copied and the copy is looked up).
Adding `const` or (even `constexpr` in C++) doesn't help.
This happens at -O1 -O2 -O3 and -Os (exactly how is copied changes, but still
looks like a copy is made and then the copy is looked up immediately).