public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Andrew Haley <aph@redhat.com>
To: gcc-help@gcc.gnu.org
Subject: Re: [BUG?] GCC inline assembler optimization issue
Date: Sat, 26 Dec 2009 23:43:00 -0000	[thread overview]
Message-ID: <4B3696EC.5030301@redhat.com> (raw)
In-Reply-To: <20091227005655.560be13d@godel>

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.

  reply	other threads:[~2009-12-26 23:06 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-12-26 23:06 Dan Kruchinin
2009-12-26 23:43 ` Andrew Haley [this message]
2009-12-27  0:28 Dan Kruchinin
2009-12-28  3:22 ` Andrew Haley
2009-12-27  0:28 Dan Kruchinin
2009-12-27  0:32 ` Andrew Haley

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4B3696EC.5030301@redhat.com \
    --to=aph@redhat.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).