public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* why not optimize static local variables
@ 2023-10-07 13:07 Hanke Zhang
  2023-10-09  7:58 ` Richard Biener
  0 siblings, 1 reply; 2+ messages in thread
From: Hanke Zhang @ 2023-10-07 13:07 UTC (permalink / raw)
  To: gcc

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?

Thanks
Hanke Zhang

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: why not optimize static local variables
  2023-10-07 13:07 why not optimize static local variables Hanke Zhang
@ 2023-10-09  7:58 ` Richard Biener
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2023-10-09  7:58 UTC (permalink / raw)
  To: Hanke Zhang; +Cc: gcc

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-10-09  8:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-07 13:07 why not optimize static local variables Hanke Zhang
2023-10-09  7:58 ` Richard Biener

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).