Can a float volatile asm be used as optimization barrier?

Ian Lance Taylor iant@google.com
Fri Jul 1 17:24:00 GMT 2011


Parmenides <mobile.parmenides@gmail.com> writes:

> I think it necessary to get my feet wet for side effects. I try to
> modify an example in gcc manual to make it adapt to IA32.
>
> #define add(new)                                \
>      ({ int __old;                              \
>           asm ("addl %1, %0"                    \
>                : "=g" (__old) : "g" (new));     \
>           __old;                                \
>      })
>
> void tst(void)
> {
>      int x = 15;
>      add(x);
> }
>
> Then, I invoked gcc to compile it :
> gcc -S -O tst.c
>
> and, get the following asm code :
> tst:
>         pushl   %ebp
>         movl    %esp, %ebp
>         popl    %ebp
>         ret
>
> It seems that the asm instruction has been considered having no side
> effects and deleted from tst(), because the __old is declared as
> output operand and it is not used later.
>
> But according to gcc manual, "if an asm has output operands, GCC
> assumes for optimization purposes the instruction has no side effects
> except to change the output operands."
>
> In the above example, it is obvious that the asm has an output
> operand. But, in terms of the inline instruction, namely the addl, the
> output operand is **changed** indeed. Why does the gcc consider the
> asm having no side effects?

This is exactly what the manual says.  The asm has no side-effects
except to change __old.  And __old is not used, which means that any
statements which assign a value to __old are unnecessary and may be
removed.  Since the only effect of the asm is to change __old, it is
removed.

Ian



More information about the Gcc-help mailing list