[llvm-bugs] [Bug 43064] New: suboptimal atomic_fetch_sub and atomic_fetch_add

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Aug 20 10:01:48 PDT 2019


https://bugs.llvm.org/show_bug.cgi?id=43064

            Bug ID: 43064
           Summary: suboptimal atomic_fetch_sub and atomic_fetch_add
           Product: libraries
           Version: 8.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Backend: X86
          Assignee: unassignedbugs at nondot.org
          Reporter: nruslan_devel at yahoo.com
                CC: craig.topper at gmail.com, llvm-bugs at lists.llvm.org,
                    llvm-dev at redking.me.uk, spatel+llvm at rotateright.com

I have noticed that if I write code:

#include <stdatomic.h>

int func(_Atomic(int) *a)
{
        return (atomic_fetch_sub(a, 1) - 1 == 0);
}

clang/llvm generates optimized code (-O2):
func:                                   # @func
        .cfi_startproc
# %bb.0:
        xorl    %eax, %eax
        lock            subl    $1, (%rdi)
        sete    %al
        retq

But when I change the condition to <= 0, it does not work. Correct me if I am
wrong, but, I think, it should still be able to use sub:

#include <stdatomic.h>

int func(_Atomic(int) *a)
{
        return (atomic_fetch_sub(a, 1) - 1 <= 0);

}

func:                                   # @func
        .cfi_startproc
# %bb.0:
        movl    $-1, %ecx
        lock            xaddl   %ecx, (%rdi)
        xorl    %eax, %eax
        cmpl    $2, %ecx
        setl    %al
        retq

Seems like the same problem exists for atomic_fetch_add as well.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20190820/eb534ed2/attachment.html>


More information about the llvm-bugs mailing list