[llvm-bugs] [Bug 37246] New: Poor lowering of SSE4.2 string intrinsics

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Apr 25 17:34:02 PDT 2018


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

            Bug ID: 37246
           Summary: Poor lowering of SSE4.2 string intrinsics
           Product: new-bugs
           Version: unspecified
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: new bugs
          Assignee: unassignedbugs at nondot.org
          Reporter: chandlerc at gmail.com
                CC: llvm-bugs at lists.llvm.org

For hypothetical memcmp code:
----
#include <cstdlib>
#include <x86intrin.h>

extern "C"
int my_memcmp(const void* lhs, const void* rhs, size_t count ) {
  const unsigned char* s1 = reinterpret_cast<const unsigned char*>(lhs);
  const unsigned char* s2 = reinterpret_cast<const unsigned char*>(rhs);

  const __m128i s1chunk =
      _mm_loadu_si128(reinterpret_cast<const __m128i*>(lhs));
  const __m128i s2chunk =
      _mm_loadu_si128(reinterpret_cast<const __m128i*>(rhs));

  constexpr unsigned char mode =
      _SIDD_UBYTE_OPS         |  // compare unsigned 8-bit characters
      _SIDD_CMP_EQUAL_EACH    |  // for equality
      _SIDD_NEGATIVE_POLARITY |  // negate results
      _SIDD_LEAST_SIGNIFICANT;

  unsigned mask = _mm_cmpistri(s1chunk, s2chunk, mode);
  if (mask != 0) {
    return s1[mask] - s2[mask];
  }

  return 0;
}
----
LLVM generates annoyingly suboptimal code:
----
my_memcmp:                              # @my_memcmp
        movdqu  (%rdi), %xmm0
        movdqu  (%rsi), %xmm1
        pcmpistri       $24, %xmm1, %xmm0
        testl   %ecx, %ecx
        je      .LBB0_1
        movl    %ecx, %ecx
        movzbl  (%rdi,%rcx), %eax
        movzbl  (%rsi,%rcx), %ecx
        subl    %ecx, %eax
        retq
.LBB0_1:
        xorl    %eax, %eax
        retq
----

For comparison:
https://godbolt.org/g/LiHXxk


Changing the code to use the flag-setting spelling doesn't really help:
----
  if (_mm_cmpistrc(s1chunk, s2chunk, mode)) {
    unsigned mask = _mm_cmpistri(s1chunk, s2chunk, mode);
    return s1[mask] - s2[mask];
  }
----
LLVM now generates:
----
my_memcmp:                              # @my_memcmp
        movdqu  (%rdi), %xmm0
        movdqu  (%rsi), %xmm1
        pcmpistri       $24, %xmm1, %xmm0
        jae     .LBB0_1
        pcmpistri       $24, %xmm1, %xmm0
        movzbl  (%rdi,%rcx), %eax
        movzbl  (%rsi,%rcx), %ecx
        subl    %ecx, %eax
        retq
.LBB0_1:
        xorl    %eax, %eax
        retq
----

And for comparison:
https://godbolt.org/g/UtYqu2


What we really should be generating:
----
my_memcmp:                              # @my_memcmp
        movdqu  (%rdi), %xmm0
        pcmpistri       $24, (%rsi), %xmm0
        jae      .LBB0_1
        movzbl  (%rdi,%rcx), %eax
        movzbl  (%rsi,%rcx), %ecx
        subl    %ecx, %eax
        retq
.LBB0_1:
        xorl    %eax, %eax
        retq
----

ICC generates a weird self-move for %ecx? Surely that's not needed.

-- 
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/20180426/74a48c0d/attachment-0001.html>


More information about the llvm-bugs mailing list