On Sat, Oct 7, 2023 at 3:08 PM Hanke Zhang via Gcc <gcc@gcc.gnu.org> wrote: > > Hi, I've recently been working on static local variables in C. I would > like to ask about some questions about that. > > For example, for the following program, > > void foo() { > static int x = 0; > x++; > } > > int main() { > foo(); > } > > After optimization with the -O3 -flto option, the entire program will > look something like this: > > int main() { > x_foo++; > } > > The question I want to ask is, why not optimize the 'x_foo++' line of > code out? Because its scope will only be in foo, and it will not be > read at all for the entire program. Is it because it is stored in the > global data area? Or are there other security issues?
I think there's bugreports tracking our weak ability to remove TU local not escaping globals that are both written and read (we can handle write-only and const fine). Richard. > > Thanks > Hanke Zhang