[llvm-bugs] [Bug 38405] New: Suboptimal machine code for atomic bool load + test

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Aug 1 08:59:47 PDT 2018


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

            Bug ID: 38405
           Summary: Suboptimal machine code for atomic bool load + test
           Product: new-bugs
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: new bugs
          Assignee: unassignedbugs at nondot.org
          Reporter: gonzalobg88 at gmail.com
                CC: chandlerc at gmail.com, efriedma at codeaurora.org,
                    hfinkel at anl.gov, llvm-bugs at lists.llvm.org,
                    llvm-dev at redking.me.uk, spatel+llvm at rotateright.com

The following C++ code compiled with clang -O3 -std=c++17
(https://godbolt.org/g/CAMK9k):

#include <atomic>

std::atomic<bool> flag_atomic{false};
bool flag_nonatomic{false};

extern void f1();
extern void f2();

void branchAtomic() {
    if (flag_atomic.load(std::memory_order_relaxed)) {
        f1();
    } else {
        f2();
    }
}

void branchNonatomic() {
    if (flag_nonatomic) {
        f1();
    } else {
        f2();
    }
}

produces different code for the atomic and non-atomic functions, but in this
particular case, it should probably be the same and emit a cmpb instruction on
both (right? I am not 100% sure):

branchAtomic(): # @branchAtomic()
  movb flag_atomic(%rip), %al
  testb $1, %al
  jne .LBB0_1
  jmp _Z2f2v # TAILCALL
.LBB0_1:
  jmp _Z2f1v # TAILCALL
branchNonatomic(): # @branchNonatomic()
  cmpb $0, flag_nonatomic(%rip)
  je .LBB1_2
  jmp _Z2f1v # TAILCALL
.LBB1_2:
  jmp _Z2f2v # TAILCALL
flag_atomic:
  .zero 1

flag_nonatomic:
  .byte 0 # 0x0

-- 
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/20180801/d7485489/attachment.html>


More information about the llvm-bugs mailing list