[email protected] wrote:
> Here is an optimization question about gcc compiler, we wonder whether it
> is a bug or not.
>
> A simple test program here:
> ===============================================
> int *p;
>
> int main(void)
> {
> p++;
> __asm__ __volatile__ (""::);
> p++;
> }
> ===============================================
> And it is comiled like this:
> gcc -S -O2 -o xxx.s xxx.c
>
> Because of the '__asm__ __volatile ...' clause, the 1st 'p++' & the 2nd
> 'p++' will not be merged together, so
> assembly code is like:
> 'addl $4, %eax'
> 'addl $4, %eax'
>
>
> But if the program is modified as below:
> ===============================================
> int *p;
>
> int main(void)
> {
> p++;
> __asm__ __volatile__ (""::);
> p+=8;
> }
> ===============================================
>
> Still, it is compiled like this:
> gcc -S -O2 -o xxx.s xxx.c
> According to the assembly code, we found that gcc merge the 'p++' & 'p+=8'
> and generate 'addl $36, p'
>
> We have the intuition that if '__asm__ __volatile ...' is used, the code
> will be separated into two parts and will not be merged together for
> optimization.
> Are we right?
> If so, why should the second case happen? Is it a bug or something else?
If you really need a memory barrier, you have to use one:
int main(void)
{
p++;
__asm__ __volatile__ ("":::"memory");
p+=8;
}
Andrew.