From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 27594 invoked by alias); 30 Sep 2011 16:33:30 -0000 Received: (qmail 27569 invoked by uid 22791); 30 Sep 2011 16:33:23 -0000 X-SWARE-Spam-Status: No, hits=-2.8 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00,TW_PX X-Spam-Check-By: sourceware.org Received: from localhost (HELO gcc.gnu.org) (127.0.0.1) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 30 Sep 2011 16:33:07 +0000 From: "hjl.tools at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug target/50583] New: Many __sync_XXX builtin functions are incorrect Date: Fri, 30 Sep 2011 17:05:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: target X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: hjl.tools at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2011-09/txt/msg02413.txt.bz2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50583 Bug #: 50583 Summary: Many __sync_XXX builtin functions are incorrect Classification: Unclassified Product: gcc Version: 4.7.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target AssignedTo: unassigned@gcc.gnu.org ReportedBy: hjl.tools@gmail.com CC: kirill.yukhin@intel.com, ubizjak@gmail.com We have `TYPE __sync_fetch_and_add (TYPE *ptr, TYPE value, ...)' `TYPE __sync_fetch_and_sub (TYPE *ptr, TYPE value, ...)' `TYPE __sync_fetch_and_or (TYPE *ptr, TYPE value, ...)' `TYPE __sync_fetch_and_and (TYPE *ptr, TYPE value, ...)' `TYPE __sync_fetch_and_xor (TYPE *ptr, TYPE value, ...)' `TYPE __sync_fetch_and_nand (TYPE *ptr, TYPE value, ...)' These builtins perform the operation suggested by the name, and returns the value that had previously been in memory. That is, { tmp = *ptr; *ptr OP= value; return tmp; } { tmp = *ptr; *ptr = ~(tmp & value); return tmp; } // nand On x86, they may be implemented as [hjl@gnu-33 gcc]$ cat /tmp/x.c int foo (int *p) { return __sync_fetch_and_and(p, 7); } [hjl@gnu-33 gcc]$ gcc -S -O2 /tmp/x.c [hjl@gnu-33 gcc]$ cat x.s .file "x.c" .text .p2align 4,,15 .globl foo .type foo, @function foo: .LFB0: .cfi_startproc movl (%rdi), %eax .L2: movl %eax, %edx movl %eax, %ecx andl $7, %edx lock cmpxchgl %edx, (%rdi) jne .L2 movl %ecx, %eax ret .cfi_endproc .LFE0: .size foo, .-foo .ident "GCC: (GNU) 4.6.0 20110603 (Red Hat 4.6.0-10)" .section .note.GNU-stack,"",@progbits [hjl@gnu-33 gcc]$ This isn't equivalent to { tmp = *ptr; *ptr OP= value; return tmp; } It is { tmp = *ptr; tmp1 = tmp OP value; return __sync_val_compare_and_swap (ptr, tmp, tmp1); } The only thing supported on x86 are [hjl@gnu-33 gcc]$ cat /tmp/y.c int foo1 (int *p) { return __sync_fetch_and_add (p, 7); } int foo2 (int *p) { return __sync_fetch_and_sub (p, 7); } [hjl@gnu-33 gcc]$ gcc -S -O2 /tmp/y.c [hjl@gnu-33 gcc]$ cat y.s .file "y.c" .text .p2align 4,,15 .globl foo1 .type foo1, @function foo1: .LFB0: .cfi_startproc movl $7, %eax lock xaddl %eax, (%rdi) ret .cfi_endproc .LFE0: .size foo1, .-foo1 .p2align 4,,15 .globl foo2 .type foo2, @function foo2: .LFB1: .cfi_startproc movl $-7, %eax lock xaddl %eax, (%rdi) ret .cfi_endproc .LFE1: .size foo2, .-foo2 .ident "GCC: (GNU) 4.6.0 20110603 (Red Hat 4.6.0-10)" .section .note.GNU-stack,"",@progbits [hjl@gnu-33 gcc]$