<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Suboptimal machine code for atomic bool load + test"
   href="https://bugs.llvm.org/show_bug.cgi?id=38405">38405</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Suboptimal machine code for atomic bool load + test
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>new bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>gonzalobg88@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>chandlerc@gmail.com, efriedma@codeaurora.org, hfinkel@anl.gov, llvm-bugs@lists.llvm.org, llvm-dev@redking.me.uk, spatel+llvm@rotateright.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The following C++ code compiled with clang -O3 -std=c++17
(<a href="https://godbolt.org/g/CAMK9k">https://godbolt.org/g/CAMK9k</a>):

#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</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>