public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Biener <richard.guenther@gmail.com>
To: Hanke Zhang <hkzhang455@gmail.com>
Cc: gcc@gcc.gnu.org
Subject: Re: Question about merging if-else blocks
Date: Wed, 27 Sep 2023 09:28:16 +0200	[thread overview]
Message-ID: <CAFiYyc2c4hTB_UQCQVOw7_-L9oU0GjZ-V-AJ+n7W0bAAFSTVYw@mail.gmail.com> (raw)
In-Reply-To: <CAM_DAs9C1GMHgjiggC6wWRGrKRVyMidbuR1vpQgO4U_Oyig5vA@mail.gmail.com>

On Wed, Sep 27, 2023 at 7:21 AM Hanke Zhang via Gcc <gcc@gcc.gnu.org> wrote:
>
> Thanks! I understand what you mean, then can I think that if the
> function here is not an external function, but a function visible to
> the compiler and the function doesn't modify `a`, then these two
> blocks can be merged?

Yes.  The key transform you'd see before any of the merging is
CSE of the loads from 'a', then the rest is equivalent to the local
variable case.

Richard.

> Marc Glisse <marc.glisse@inria.fr> 于2023年9月27日周三 12:51写道:
> >
> > On Wed, 27 Sep 2023, Hanke Zhang via Gcc wrote:
> >
> > > 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?
> >
> > If 'a' is a global variable, how do you know 'printf' doesn't modify its
> > value? (you could know it for printf, but it really depends on the
> > function that is called)
> >
> > --
> > Marc Glisse

  reply	other threads:[~2023-09-27  7:30 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-27  3:47 Hanke Zhang
2023-09-27  4:51 ` Marc Glisse
2023-09-27  5:20   ` Hanke Zhang
2023-09-27  7:28     ` Richard Biener [this message]
2023-10-01  4:12       ` Hanke Zhang
2023-10-04  7:54         ` Richard Biener
2023-10-04  9:12           ` Florian Weimer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAFiYyc2c4hTB_UQCQVOw7_-L9oU0GjZ-V-AJ+n7W0bAAFSTVYw@mail.gmail.com \
    --to=richard.guenther@gmail.com \
    --cc=gcc@gcc.gnu.org \
    --cc=hkzhang455@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).