<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 - _mm_avg_epu16 preceded by a left shift results in poor code"
   href="https://bugs.llvm.org/show_bug.cgi?id=41316">41316</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>_mm_avg_epu16 preceded by a left shift results in poor code
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>8.0
          </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>Backend: X86
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>fabiang@radgametools.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>craig.topper@gmail.com, llvm-bugs@lists.llvm.org, llvm-dev@redking.me.uk, spatel+llvm@rotateright.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Reproducer:

-

#include <xmmintrin.h>

__m128i f(__m128i a, __m128i b)
{
  __m128i a_shifted = _mm_slli_epi16(a, 2);
  __m128i b_shifted = _mm_slli_epi16(b, 2);
  return _mm_avg_epu16(a_shifted, b_shifted);
}

-

with clang 5.0.0 or older, I get the expected:

-

f(long long __vector(2), long long __vector(2)):
        psllw   xmm0, 2
        psllw   xmm1, 2
        pavgw   xmm0, xmm1
        ret

-

but looks like sometime between 5.0.0 and 6.0.0, things broke and now the same
code results in:

-

.LCPI0_0:
        .short  1                       # 0x1
        .short  1                       # 0x1
        .short  1                       # 0x1
        .short  1                       # 0x1
        .short  1                       # 0x1
        .short  1                       # 0x1
        .short  1                       # 0x1
        .short  1                       # 0x1
f(long long __vector(2), long long __vector(2)):
        psllw   xmm0, 2
        psllw   xmm1, 2
        pxor    xmm2, xmm2
        movdqa  xmm3, xmm1
        punpckhwd       xmm3, xmm2
        punpcklwd       xmm1, xmm2
        por     xmm0, xmmword ptr [rip + .LCPI0_0]
        movdqa  xmm4, xmm0
        punpckhwd       xmm4, xmm2
        paddd   xmm4, xmm3
        punpcklwd       xmm0, xmm2
        paddd   xmm0, xmm1
        pslld   xmm4, 15
        psrad   xmm4, 16
        pslld   xmm0, 15
        psrad   xmm0, 16
        packssdw        xmm0, xmm4
        ret

-

everything after the "psllw xmm1, 2" appears to be a replacement expansion for
pavgw. This particular example was compiled with 8.0.0 but 6.x and 7.x are
similar.

If I just shift one of the inputs, I do get a pavgw; so the left shifts appear
to be important in some way. (Reduced from a more complex example.)</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>