[llvm-bugs] [Bug 42674] New: [SIMD opt of checksum func] Smart, but could be smarter

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Jul 18 10:31:41 PDT 2019


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

            Bug ID: 42674
           Summary: [SIMD opt of checksum func] Smart, but could be
                    smarter
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: LLVM Codegen
          Assignee: unassignedclangbugs at nondot.org
          Reporter: bisqwit at iki.fi
                CC: llvm-bugs at lists.llvm.org, neeilans at live.com,
                    richard-llvm at metafoo.co.uk

#include <string.h>
    unsigned char calculate_checksum(const void* ptr)
    {
        unsigned char bytes[16], result = 0;
        memcpy(bytes, ptr, 16); // Endianess does not matter.
        for(unsigned n=0; n<16; ++n) result += bytes[n];
        return result;
    }

For this code, Clang (generates the following SIMD code, which is not too bad.

        vmovdqu xmm0, xmmword ptr [rdi]
        vmovdqa xmmword ptr [rsp - 24], xmm0
        vpshufd xmm1, xmm0, 78          # xmm1 = xmm0[2,3,0,1]
        vpaddb  xmm0, xmm0, xmm1
        vpshufd xmm1, xmm0, 229         # xmm1 = xmm0[1,1,2,3]
        vpaddb  xmm0, xmm0, xmm1
        vpsrld  xmm1, xmm0, 16
        vpaddb  xmm0, xmm0, xmm1
        vpsrlw  xmm1, xmm0, 8
        vpaddb  xmm0, xmm0, xmm1
        vpextrb eax, xmm0, 0
        ret

But here is what ICC (version 18 or newer) generates for it.

        vmovups   xmm0, XMMWORD PTR [rdi]                       #5.9
        vpxor     xmm2, xmm2, xmm2                              #4.41
        vpaddb    xmm0, xmm2, xmm0                              #4.41
        vpsrldq   xmm1, xmm0, 8                                 #4.41
        vpaddb    xmm3, xmm0, xmm1                              #4.41
        vpsadbw   xmm4, xmm2, xmm3                              #4.41
        vmovd     eax, xmm4                                     #4.41
        movsx     rax, al                                       #4.41
        ret                                                     #7.16

Hence the title of this report: Smart, but could be smarter.

For the record, both GCC and MSVC generates almost identical code as Clang for
this testcase. They use VPSRLDQ instead of VPSHUFD rthough. Compiler Explorer
link: https://godbolt.org/z/hA4J94

-- 
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/20190718/36c342b8/attachment.html>


More information about the llvm-bugs mailing list