[llvm-bugs] [Bug 37025] New: Suboptimal atomic_fetch_sub

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Apr 5 17:48:34 PDT 2018


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

            Bug ID: 37025
           Summary: Suboptimal atomic_fetch_sub
           Product: libraries
           Version: 6.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: llvm-bugs at lists.llvm.org

Normally, clang/llvm is capable of recognizing atomic_fetch_sub(obj, 1) == 1
pattern by replacing more general 'lock xadd' instruction with 'lock sub' since
if new result is zero it can be obtained from the ZF flag directly.

However, in the following example (which introduces an extra condition) it does
not optimize it as expected:

test.c:

#include <stdatomic.h>
#include <stdbool.h>

void func2();

void func(_Atomic(unsigned long) * obj, void * obj2)
{
        if (atomic_fetch_sub(obj, 1) == 1 && obj2)
                func2();
}

/opt/llvm/bin/clang -Wall -O2 -S test.c

generates the following code (note that it re-ordered comparisons):

func:                                   # @func
        .cfi_startproc
# %bb.0:
        movq    $-1, %rax
        lock            xaddq   %rax, (%rdi)
        testq   %rsi, %rsi
        je      .LBB0_2
# %bb.1:
        cmpq    $1, %rax
        jne     .LBB0_2
# %bb.3:
        xorl    %eax, %eax
        jmp     func2                   # TAILCALL
.LBB0_2:
        retq

-- 
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/20180406/0d381e64/attachment.html>


More information about the llvm-bugs mailing list