Why the compiler can not recognize what variables are volatile?

Peng Yu pengyu.ut@gmail.com
Fri Feb 8 14:18:00 GMT 2019


On Wed, Feb 6, 2019 at 8:18 PM Xi Ruoyao <xry111@mengyan1223.wang> wrote:

> On 2019-02-06 18:44 -0600, Peng Yu wrote:
> > Hi,
> >
> > If I compile the following program without -O*, it will print this.
> >
> > $ ./main.exe
> > 2
> > 20
> >
> > If I compile it with -O1 or any other number > 1, it will print this.
> >
> > $ ./main.exe
> > 2
> > 10
> >
> > The optimization clearly changes the semantics of the program. Why the
> > compiler can not figure out local_var2 is volatile on its own to
> > reduce the burdens of the programmers in having to figure out what
> > variables should be specified as volatile?
> >
> > Thanks.
> >
> > #include <stdio.h>
> > #include <setjmp.h>
> >
> > static jmp_buf buf;
> >
> > int main() {
> >     volatile int local_var = 1;
> >     int local_var2 = 10;
> >     if(!setjmp(buf)) {
> >         local_var = 2;
> >         local_var2 = 20;
> >         longjmp(buf, 1);
> >     } else {
> >         printf("%d\n", local_var);
> >         printf("%d\n", local_var2);
> >     }
> >
> >     return 0;
> > }
>
> "I'm a compiler, not a programmer!"
>
> If the setjmp/longjmp can be refactored to some semantical thing, the
> programmer should do it and remove the usage of setjmp/longjmp.
>

How? I don’t think that this is always possible.

>
> Otherwise, the compiler can not see which variable should be volatile,
> because control flow analysis is useless.
> --
> Xi Ruoyao <xry111@mengyan1223.wang>
> School of Aerospace Science and Technology, Xidian University
>
> --
Regards,
Peng



More information about the Gcc-help mailing list