<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </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 - Poor lowering of SSE4.2 string intrinsics"
   href="https://bugs.llvm.org/show_bug.cgi?id=37246">37246</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Poor lowering of SSE4.2 string intrinsics
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </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>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>new bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>chandlerc@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>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:
<a href="https://godbolt.org/g/LiHXxk">https://godbolt.org/g/LiHXxk</a>


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:
<a href="https://godbolt.org/g/UtYqu2">https://godbolt.org/g/UtYqu2</a>


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.</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>