<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/167260>167260</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Failure to reassociate uniform vector transformations with variable shuffles
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          WalterKruger
      </td>
    </tr>
</table>

<pre>
    Since shuffles only change the arrangement of elements within a vector, it should be legal to reassociate operations that transform all lanes in the same manner to occur before/after they are shuffled even if the shuffle pattern isn't known at compile time.

This optimization isn't performed when the indexes are variable. For example, `shuffle(POW_OF_2_LUT, idx) + 1` is implemented naively rather then offsetting the table:

```asm
powOf2P1LUT_clang:
        pand    xmm0, xmmword ptr [rip + .LCPI0_0]
        movdqa  xmm1, xmmword ptr [rip + .LCPI0_1]
        pshufb  xmm1, xmm0
        pcmpeqd xmm0, xmm0
 psubb   xmm1, xmm0
        movdqa  xmm0, xmm1
 ret
```

https://godbolt.org/z/Y78Ws6Tdv

Note that this isn't legal for shuffles that can set lanes to specific constants, or that select between two vectors if only one is transformed.


</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyEVE2L4zgQ_TXKpZggK98HH7pnMCzbbDdsD82eQkkux9qVJY9UTtLz6xfZnnRmYNkQkFDVk149vypMyZ48USk2j2LzZYEDtyGWb-iY4u9xOFFc6FC_l39abwhSOzSNowTBu3cwLfoTAbcEGGPed-QZQgPkxm2Ci-XWekA4k-EQhfoMliG1YXA1aAJHJ3TAASJhSsFYZILQU0S2wSfgFhk4ok9NiB2gc-DQUwLrx3cTdgQdek8x3xKMGSJoakIkoSpsOJ-39A4Yb-xroDN5sM10w3QIPTJT9GCTF2rH8I8PFw_IYELXW0fAtqOlkA9CPry2NkHo2Xb2-0j0huopZqJUw6WliaL1NV0pjQzOGC1qR0uoQgS6Ytc7yqKIrZyJCLV_eX47PldHdXz6-joqVl-FOoBQj1CIrQSbwGZglphq8GjP5N4hIrdTuR5C0yRitv40cuD8qFg9TPTFVk5_TJ2QD324PDfqpXj6-no0Dv1pSoT516Ov83rtOpnZXLvuEmINPUcQm8do-5HZ8unzy2_yKMXmyx24C-f6G47g4v_Bxc_gPkuifwLL-7DpevpW3xMbw30atIb_hN1R-gErcjgS32szSdUy9ynroSqhqlOodXC8DPEkVPVdqOqv3f4tbV_r85T-R2CaPZst8sMVk8ubED8aaEwy6CERz5bmAKknYxtrwASfGD2nzDDEKT2RI8OgiS-UzXUJc1ulbOaxJYOnbI9bx1A9W3ZRl6v6sDrggspit1W71U4dikVbbtbrvSbd6PWKtrppdmZ9OJCRciWNXqnDwpZKqk1RFFIWxXpTLPf7Gk2hm816Y-q93ou1pA6tWzp37rI2C5vSQGWx3amtXDjU5NI4YJTydIExKpTK8yaWGfRJD6ck1tLZxOnjGrbsqKzQuiHSr0Ni8HYcCZMCHxXPgyMPnlu73WRfDNGVv3xTy-2glyZ0QlX56Xn51MfwNxkWqhoJJ6GquaJzqf4NAAD__4TIuHs">