[llvm-bugs] [Bug 46954] New: Suboptimal __builtin_parity/popcount codegen for generic x86

via llvm-bugs llvm-bugs at lists.llvm.org
Sat Aug 1 18:24:17 PDT 2020


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

            Bug ID: 46954
           Summary: Suboptimal __builtin_parity/popcount codegen for
                    generic x86
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Backend: X86
          Assignee: unassignedbugs at nondot.org
          Reporter: david.bolvansky at gmail.com
                CC: craig.topper at gmail.com, llvm-bugs at lists.llvm.org,
                    llvm-dev at redking.me.uk, spatel+llvm at rotateright.com

unsigned int foo(unsigned int x, unsigned  int y)

{

  return __builtin_parity(x ^ y);

}


Clang

foo(unsigned int, unsigned int):                               # @foo(unsigned
int, unsigned int)
        xor     edi, esi
        mov     ecx, edi
        shr     ecx, 16
        xor     ecx, edi
        mov     edx, ecx
        shr     edx, 8
        xor     eax, eax
        xor     dl, cl
        setnp   al
        ret

GCC:
foo(unsigned int, unsigned int):
        mov     eax, edi
        xor     eax, esi
        mov     edx, eax
        shr     edx, 16
        xor     eax, edx
        xor     al, ah
        setnp   al
        movzx   eax, al
        ret


Eliminate extra shr?

*******************************************

unsigned int fooll(unsigned int x, unsigned  int y)

{

  return __builtin_parityll(x ^ y);

}

GCC produces same code.

Clang produces:


foo(unsigned int, unsigned int):                               # @foo(unsigned
int, unsigned int)
        xor     edi, esi
        mov     rax, rdi
        shr     rax
        and     eax, 1431655765
        sub     rdi, rax
        movabs  rax, 3689348814741910323
        mov     rcx, rdi
        and     rcx, rax
        shr     rdi, 2
        and     rdi, rax
        add     rdi, rcx
        mov     rax, rdi
        shr     rax, 4
        add     rax, rdi
        movabs  rcx, 76296276040158991
        and     rcx, rax
        movabs  rax, 72340172838076673
        imul    rax, rcx
        shr     rax, 56
        and     eax, 1
        ret


unsigned long fooll(unsigned long x, unsigned  long y)

{

  return __builtin_parityll(x ^ y);

}


Codegene: https://godbolt.org/z/G9zbGr

-- 
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/20200802/58873e89/attachment.html>


More information about the llvm-bugs mailing list