<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 --- - X86: Generate smaller code for atomic refcount decrement"
   href="https://llvm.org/bugs/show_bug.cgi?id=31367">31367</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>X86: Generate smaller code for atomic refcount decrement
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

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

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

        <tr>
          <th>OS</th>
          <td>Linux
          </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>Backend: X86
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>hans@chromium.org
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, mkuper@google.com, rnk@google.com
          </td>
        </tr>

        <tr>
          <th>Blocks</th>
          <td>26299
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Example inspired by Chromium's RefcountedThreadSafe.

  #include <windows.h>
  void foo();
  void f(long volatile *ptr) {
    if (_InterlockedExchangeAdd(ptr, -1) == 1) {
      foo(); // call delete on ptr
    }
  }

With MSVC:

?f@@YAXPECJ@Z (void __cdecl f(long volatile *)):
  0000000000000000: 83 C8 FF           or          eax,0FFFFFFFFh
  0000000000000003: F0 0F C1 01        lock xadd   dword ptr [rcx],eax
  0000000000000007: 83 F8 01           cmp         eax,1
  000000000000000A: 0F 84 00 00 00 00  je          ?foo@@YAXXZ
  0000000000000010: C3                 ret

With clang-cl:

?f@@YAXPECJ@Z (void __cdecl f(long volatile *)):
  0000000000000000: B8 FF FF FF FF     mov         eax,0FFFFFFFFh
  0000000000000005: F0 0F C1 01        lock xadd   dword ptr [rcx],eax
  0000000000000009: 83 F8 01           cmp         eax,1
  000000000000000C: 75 05              jne         0000000000000013
  000000000000000E: E9 00 00 00 00     jmp         ?foo@@YAXXZ
  0000000000000013: C3                 ret

It annoys me that we spend 5 bytes putting -1 into ecx. We do the "or trick" at
/Os, but what we really want is subtraction, or even better: decrement.

Wouldn't just "lock dec" and then checking eflags work? That is, how about:

lock dec   dword ptr [rcx]
jne        <return label>
...

Those two instructions are 5 bytes total, as opposed to 14 bytes for mov--jne
in clang-cl's code above :-)

Am I missing anything that would make this not correct?</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>