<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 - [x86] shuffle lowering creates strange shuffle mask"
   href="https://bugs.llvm.org/show_bug.cgi?id=40306">40306</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[x86] shuffle lowering creates strange shuffle mask
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </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>spatel+llvm@rotateright.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>This manifested in:
<a href="https://reviews.llvm.org/D56281">https://reviews.llvm.org/D56281</a>
...but it's independent of that patch as shown with this example:

define <8 x i16> @shuf_zeros_undefs(<8 x i16> %x) {
  %r = shufflevector <8 x i16> zeroinitializer, <8 x i16> %x, <8 x i32> <i32 9,
i32 1, i32 2, i32 3, i32 undef, i32 undef, i32 undef, i32 undef>
  ret <8 x i16> %r
}

Clearly, we have undefs in the high elements of the result, but we get this
unexpected pshufb lowering:

$ llc -o - weird_shufb.ll -mattr=avx
        vpshufb LCPI0_0(%rip), %xmm0, %xmm0 ## xmm0 =
xmm0[2,3],zero,zero,zero,zero,zero,zero,zero,zero,zero,zero,zero,zero,xmm0[6,7]

-------------------------------------------------------------------------------

That's not a miscompile, but why are the high elements choosing from the input?

Debug output shows that we created this intermediate state:

              t12: v8i16 = X86ISD::UNPCKL t17, t2
            t19: v8i16 = X86ISD::PSHUFHW t12, Constant:i8<-24>
          t20: v4i32 = bitcast t19
        t22: v4i32 = X86ISD::PSHUFD t20, Constant:i8<-26>
      t23: v8i16 = bitcast t22
    t25: v8i16 = X86ISD::PSHUFLW t23, Constant:i8<75>

-------------------------------------------------------------------------------

So this leads to a question that may not be answerable in the DAG or
statically: what is the ideal x86 lowering for that shuffle? 

The pshufb is obviously the smallest code, but should we favor a solution that
doesn't need to load anything?

That could be a shift+blend immediate with zero:
        vpsrld  $16, %xmm0, %xmm0
        vpxor   %xmm1, %xmm1, %xmm1
        vpblendw        $1, %xmm0, %xmm1, %xmm0 ## xmm0 =
xmm0[0],xmm1[1,2,3,4,5,6,7]

Or shift+zext:
        vpsrld  $16, %xmm0, %xmm0
        vpmovzxdq %xmm0, %xmm0

Or if we're ok with a load, but just want to avoid pshufb, it could be a
shift+mask:
        vpsrld  $16, %xmm0, %xmm0
        vpand   LCPI0_0(%rip), %xmm0, %xmm0</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>