<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </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 --- - Passing 0 to __atomic_fetch_add generates mfence instead of lock xadd"
   href="https://llvm.org/bugs/show_bug.cgi?id=28498">28498</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Passing 0 to __atomic_fetch_add generates mfence instead of lock xadd
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </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>normal
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>LLVM Codegen
          </td>
        </tr>

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

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

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Using clang 3.9 on trunk at r273567
Target: x86_64-unknown-linux-gnu

When passing a '0' constant to '__atomic_fetch_add', an mfence instruction is
generated instead of the usual 'lock xadd' which seems less efficient.

---
#include <stdint.h>

int64_t * g_ptr;

int foo() {
  return __atomic_fetch_add(g_ptr, 0, __ATOMIC_RELAXED);
}
---

clang -O3 -S test.c

Generates:

movq    %rdi, -8(%rsp)
movq    g_ptr(%rip), %rax
mfence
movq    (%rax), %rax
retq

Using gcc:

movq    g_ptr(%rip), %rdx
movq    %rdi, -8(%rsp)
xorl    %eax, %eax
lock xaddq      %rax, (%rdx)
ret</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>