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

* Re: Question about merging if-else blocks
  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
  0 siblings, 1 reply; 7+ messages in thread
From: Marc Glisse @ 2023-09-27  4:51 UTC (permalink / raw)
  To: Hanke Zhang; +Cc: gcc

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

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

* Re: Question about merging if-else blocks
  2023-09-27  4:51 ` Marc Glisse
@ 2023-09-27  5:20   ` Hanke Zhang
  2023-09-27  7:28     ` Richard Biener
  0 siblings, 1 reply; 7+ messages in thread
From: Hanke Zhang @ 2023-09-27  5:20 UTC (permalink / raw)
  To: gcc

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?

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

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

* Re: Question about merging if-else blocks
  2023-09-27  5:20   ` Hanke Zhang
@ 2023-09-27  7:28     ` Richard Biener
  2023-10-01  4:12       ` Hanke Zhang
  0 siblings, 1 reply; 7+ messages in thread
From: Richard Biener @ 2023-09-27  7:28 UTC (permalink / raw)
  To: Hanke Zhang; +Cc: gcc

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

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

* Re: Question about merging if-else blocks
  2023-09-27  7:28     ` Richard Biener
@ 2023-10-01  4:12       ` Hanke Zhang
  2023-10-04  7:54         ` Richard Biener
  0 siblings, 1 reply; 7+ messages in thread
From: Hanke Zhang @ 2023-10-01  4:12 UTC (permalink / raw)
  To: Richard Biener; +Cc: gcc

Richard Biener <richard.guenther@gmail.com> 于2023年9月27日周三 15:30写道:
>
> 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.

Hi, Richard

I'm still a little confused about this.

I want to change the default behavior of gcc. We know that printf
won't change the value of 'a'. I'd like to let the compiler to get
this information as well. How can I do that? Or which pass should I
focus on?

By disassembling the exe file generated by icc, I found that icc will
merge these two blocks with the example code below. So I think there
maybe some ways to make it.

Thanks.
Hanke Zhang.

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

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

* Re: Question about merging if-else blocks
  2023-10-01  4:12       ` Hanke Zhang
@ 2023-10-04  7:54         ` Richard Biener
  2023-10-04  9:12           ` Florian Weimer
  0 siblings, 1 reply; 7+ messages in thread
From: Richard Biener @ 2023-10-04  7:54 UTC (permalink / raw)
  To: Hanke Zhang, Florian Weimer; +Cc: gcc

On Sun, Oct 1, 2023 at 6:13 AM Hanke Zhang <hkzhang455@gmail.com> wrote:
>
> Richard Biener <richard.guenther@gmail.com> 于2023年9月27日周三 15:30写道:
> >
> > 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.
>
> Hi, Richard
>
> I'm still a little confused about this.
>
> I want to change the default behavior of gcc. We know that printf
> won't change the value of 'a'. I'd like to let the compiler to get
> this information as well. How can I do that? Or which pass should I
> focus on?

GCC has a builtin for 'printf' so it can handle those specially in
builtins.cc:builtin_fnspec (see attr-fnspec.h for how the magic
strings are encoded).  If that's taught that printf cannot modify
global memory it should already work, note though ...

> By disassembling the exe file generated by icc, I found that icc will
> merge these two blocks with the example code below. So I think there
> maybe some ways to make it.

... glibc for example allows user-provided printf format callbacks so
printf might call back into the current TU and modify globals in such
callback.  That's a GNU extension to printf that ICC likely doesn't
support (https://www.gnu.org/software/libc/manual/html_node/Customizing-Printf.html),
so that we're currently not doing this is for correctness.

I'm not sure if this extension is much used or if it is maybe deprecated.

With LTO it _might_ be possible to check whether any of the functions
dealing with this (register_printf_function) is used.  Without LTO for
symbols with hidden visibility that do not escape the TU this analysis
can be done TU-local.

Richard.

> Thanks.
> Hanke Zhang.
>
> >
> > > 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

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

* Re: Question about merging if-else blocks
  2023-10-04  7:54         ` Richard Biener
@ 2023-10-04  9:12           ` Florian Weimer
  0 siblings, 0 replies; 7+ messages in thread
From: Florian Weimer @ 2023-10-04  9:12 UTC (permalink / raw)
  To: Richard Biener; +Cc: Hanke Zhang, gcc

* Richard Biener:

>> By disassembling the exe file generated by icc, I found that icc will
>> merge these two blocks with the example code below. So I think there
>> maybe some ways to make it.
>
> ... glibc for example allows user-provided printf format callbacks so
> printf might call back into the current TU and modify globals in such
> callback.  That's a GNU extension to printf that ICC likely doesn't
> support (https://www.gnu.org/software/libc/manual/html_node/Customizing-Printf.html),
> so that we're currently not doing this is for correctness.
>
> I'm not sure if this extension is much used or if it is maybe
> deprecated.

There's also fopencookie, which is more widely available.  The GNU C
library supports assignment to stdout, so an fopencookie stream could be
the target of printf, also triggering callbacks.

But I'm not sure if callbacks updating global variables should prevent
GCC from treating printf et al. as leaf functions.

Thanks,
Florian


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