[BUG?] GCC inline assembler optimization issue

Andrew Haley aph@redhat.com
Sat Dec 26 23:43:00 GMT 2009


On 12/26/2009 09:56 PM, Dan Kruchinin wrote:

> I'm not sure if I found out a bug or I just made a mistake in gcc inline assembler.
> I think that more probably it's a bug, because my code works well when I don't use compile time optimization.
> I have the following code for x86_64 target:
> ===
> #include <stdio.h>
> 
> #define SPIN_LOCKED_V   1
> #define SPIN_UNLOCKED_V 0
> 
> struct spin {
>     unsigned long val;
> };
> 
> static inline void spin_lock(struct spin *lock)
> {
>     __asm__ volatile ("movq %2, %%rax\n\t"
>                       "1: lock cmpxchg %1, %0\n\t"
>                       "cmpq %2, %%rax\n\t"
>                       "jnz 1b\n"
>                       : "+m" (lock->val)
>                       : "r" ((volatile long)SPIN_LOCKED_V),
>                         "rI" ((volatile long)SPIN_UNLOCKED_V)
>                       : "%rax", "memory");
> }

> static inline void spin_unlock(struct spin *lock)
> {
>   __asm__ volatile ("lock xchgq %1, %0\n"
>                     : "+m" (lock->val)
>                     : "r" ((volatile long)SPIN_UNLOCKED_V));
> }

You need a memory clobber here.
You need to add a clobber to Operand 1.

Andrew.



More information about the Gcc-help mailing list