This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Can a float volatile asm be used as optimization barrier?
- From: Ian Lance Taylor <iant at google dot com>
- To: Parmenides <mobile dot parmenides at gmail dot com>
- Cc: Gcc mailing list <gcc-help at gcc dot gnu dot org>
- Date: Fri, 01 Jul 2011 10:24:11 -0700
- Subject: Re: Can a float volatile asm be used as optimization barrier?
- References: <BANLkTimAxWqon69u=weK-32WcdZbJAeEMg@mail.gmail.com> <mcr39iz80w4.fsf@coign.corp.google.com> <BANLkTin1i4OTNAi_oDbrV9E1Sj5CMxn7Lg@mail.gmail.com>
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