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.gli...@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 via email to