public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Question about merging if-else blocks
@ 2023-09-27  3:47 Hanke Zhang
  2023-09-27  4:51 ` Marc Glisse
  0 siblings, 1 reply; 7+ messages in thread
From: Hanke Zhang @ 2023-09-27  3:47 UTC (permalink / raw)
  To: gcc

Hi, I have recently been working on merging if-else statement blocks,
and I found a rather bizarre phenomenon that I would like to ask
about.
A rough explanation is that for two consecutive if-else blocks, if
their if statements are exactly the same, they should be merged, like
the following program:

int a = atoi(argv[1]);
if (a) {
  printf("if 1");
} else {
  printf("else 1");
}
if (a) {
  printf("if 2");
} else {
  printf("else 2");
}

After using the -O3 -flto optimization option, it can be optimized as follows:

int a = atoi(argv[1]);
if (a) {
  printf("if 1");
  printf("if 2");
} else {
  printf("else 1");
  printf("else 2");
}

But `a` here is a local variable. If I declare a as a global variable,
it cannot be optimized as above. I would like to ask why this is? And
is there any solution?

Thanks.
Hanke Zhang.

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

end of thread, other threads:[~2023-10-04  9:12 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-27  3:47 Question about merging if-else blocks Hanke Zhang
2023-09-27  4:51 ` Marc Glisse
2023-09-27  5:20   ` Hanke Zhang
2023-09-27  7:28     ` Richard Biener
2023-10-01  4:12       ` Hanke Zhang
2023-10-04  7:54         ` Richard Biener
2023-10-04  9:12           ` Florian Weimer

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).