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

    <tr>
        <th>Summary</th>
        <td>
            [RISC-V] Insert shuffles could be tail masked
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:RISC-V,
            missed-optimization
      </td>
    </tr>

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

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

<pre>
    ```llvm
define <4 x i32> @insert_subvector_add(<4 x i32> %v1, <4 x i32> %v2) {
  %v3 = add <4 x i32> %v2, <i32 0, i32 1, i32 2, i32 3>
  %v4 = shufflevector <4 x i32> %v3, <4 x i32> %v1, <4 x i32> <i32 0, i32 1, i32 6, i32 7>
  ret <4 x i32> %v4
}
```

Currently with `llc -mattr=+v` we get:
```
insert_subvector_add:
        vsetivli        zero, 4, e32, m1, ta, ma
        vid.v   v10
        vadd.vv v9, v9, v10
 vsetivli        zero, 2, e32, m1, tu, ma
        vslideup.vi     v8, v9, 0
        ret
```

The `vslideup.vi` of zero here acts almost like a `vmerge.vvm`, except instead of using a mask register we're using the VL to merge the first 2 elements of v8 and the last 2 elements of v9.

(https://reviews.llvm.org/D152724 and https://reviews.llvm.org/D152565 turn the `vslideup.vi` of zero into a canonical `vmv.v.v`)

We could instead take advantage of the VL of the `vadd.vv` and only write to the first two elements:
```
insert_subvector_add:
        vsetivli        zero, 2, e32, m1, tu, ma
        vid.v   v10
        vadd.vv v8, v9, v10
 ret
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0VU2P2zYQ_TXUZWBBIvV50CFrx0CAntoiPQa0OLbZpSSDHNFJfn1BWt51vd52LzEMaiiSbx7fzGikc_owInasfGKcm_kZTVszzlm5SeRMx8l2y8tkN6kfHauyy98YP7Bsw7JPCvd6RGBiXcB30IIz8RlYkenRoaVvbt557Gmy36RSjDd3-3jpc8bX98d56TnjLbD66eIF4jsBTGxAKvV4f4TRgkMW7GDkV4NfDcHE51vIIkK647zfG7wwfQAuHnN8RP09BtXVqG8YWKQHsMVlmdWbxbiqvkzjuJ6txZHMDzhrOkKMSQ-rQRJZJjaMP3lWZXBGOCAx8ekh1MMgXffC8vMOSXujr_OfaKdwmSIMKKK0Q7wnyWjLu_NapT488-xuQSqVeg--DceW8WXTe275W7fzQ7fOaIXzKfUXCN-8erkjYpH-Q-k_jxjkvcELwk77yAiOaBFkTw6kGSZHYPQzgownBrQHTL0fAmJg_b3HE4EeHaFUAWJ2ejyAhEG6Z7B40I7QwhkZry0uq3RE-Pob0AQRL8732joCDmhwwJFcwPINyFHFZSPfrrbp7aUYb45EJxeizbeMby16jWeXhspOJ3tgfLvJS17zIqJ-aHNZlUCzHSOH9yXTI00goZfjNOpemotWPvWpj0K1t0T_Quin2agX1UgGfZWXI8kDBtBFoMUKYJfECj4D92kMVWI1YRDxVT46Ty8S_ZIK-XCq_m-FNI8q5G3aJqoTqhWtTLDLq6YqmyarquTYyV1bVSrr93XFS7kXqBpVl3kj5a7JsCoS3fGMi6zKeV4VeValqhVZ2dStzBTWWOasyHCQ2rzEPNHOzdhVgldNYuQOjVv6yE72zzgGmX7_8sd69TU0FL5mnA_aOVSr6UR60D8l6Wlcmo3tAuxqNx8cKzKjHb0mV0KaTOxRC1y5gS8xLNfvtltSZIdAUptYTaiS2Zru33l70HScd2k_DYxvYw-7PFYnO_2NPTG-jbdyjG_jxf4JAAD__19-CP4">