Can a float volatile asm be used as optimization barrier?

Segher Boessenkool segher@kernel.crashing.org
Fri Jul 1 16:31:00 GMT 2011


> #define add(new)                                \
>      ({ int __old;                              \
>           asm ("addl %1, %0"                    \
>                : "=g" (__old) : "g" (new));     \
>           __old;                                \
>      })
>
> void tst(void)
> {
>      int x = 15;
>      add(x);
> }

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

It considers the asm as having no side effects _except to change the
output operands_.  You didn't use the output, so GCC is free to remove
the asm altogether: the resulting assembler code will still do the same
thing (namely, nothing).


Segher



More information about the Gcc-help mailing list