[cfe-commits] [PATCH] Teach atomicrmw nand to emit *a = ~(*a & b), not *a = ~*a & b

Richard Smith richard at metafoo.co.uk
Fri Apr 13 15:26:00 PDT 2012


Hi,

On Fri, Apr 13, 2012 at 1:41 PM, Chris Lattner <clattner at apple.com> wrote:

> On Apr 13, 2012, at 1:20 PM, Richard Smith wrote:
> > Clang lowers __atomic_fetch_nand(&p, 123, 5) to:
> >
> >   %0 = atomicrmw nand i32* @p, i32 123 seq_cst
> >
> > ... which is documented as performing *x = ~(*x & y) operation, but
> actually performs *x = ~*x & y (at least on x86):
>
> Sounds like a (bad!) codegen bug.  It probably dates back to when we did
> support __sync_fetch_and_nand.  I think that GCC 4.4 is old enough that we
> could add support for __sync_fetch_and_nand in, with the proper semantics
> (and fix the codegen bug!)


The attached patch fixes the codegen bug. With this,

  %18 = atomicrmw nand i32* %val2, i32 1401 monotonic
  store i32 %18, i32* %old

generates:

        movl    $1401, %edx             # imm = 0x579
        .align  16, 0x90
.LBB0_23:                               # %entry
                                        # =>This Inner Loop Header: Depth=1
        movl    28(%esp), %ecx
        andl    %edx, %ecx
        notl    %ecx
        movl    %ecx, %eax
        lock
        cmpxchgl        %ecx, 28(%esp)
        jne     .LBB0_23
# BB#24:                                # %entry
        movl    %eax, 12(%esp)

and:

  %19 = atomicrmw nand i64* %temp64, i64 17361641481138401520 monotonic
  store i64 %19, i64* %temp64

generates:

        movl    $-252645136, %ebp       # imm = 0xFFFFFFFFF0F0F0F0
        movl    (%esp), %eax
        movl    4(%esp), %edx
        .align  16, 0x90
.LBB0_25:                               # %entry
                                        # =>This Inner Loop Header: Depth=1
        movl    %eax, %esi
        movl    %edx, %edi
        movl    %esi, %ebx
        andl    %ebp, %ebx
        movl    %edi, %ecx
        andl    %ebp, %ecx
        notl    %ebx
        notl    %ecx
        lock
        cmpxchg8b       (%esp)
        jne     .LBB0_25
# BB#26:                                # %entry
        movl    %edi, 4(%esp)

-- Richard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120413/2dcf7a25/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: x86-atomicrmw-nand.diff
Type: application/octet-stream
Size: 7264 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120413/2dcf7a25/attachment.obj>


More information about the cfe-commits mailing list