[llvm-bugs] [Bug 31367] New: X86: Generate smaller code for atomic refcount decrement

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Dec 13 16:45:35 PST 2016


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

            Bug ID: 31367
           Summary: X86: Generate smaller code for atomic refcount
                    decrement
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: Backend: X86
          Assignee: unassignedbugs at nondot.org
          Reporter: hans at chromium.org
                CC: llvm-bugs at lists.llvm.org, mkuper at google.com,
                    rnk at google.com
            Blocks: 26299
    Classification: Unclassified

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 at 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 at 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?

-- 
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/20161214/97433362/attachment.html>


More information about the llvm-bugs mailing list