<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/65068>65068</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[RISCV] Vector binary ops of splats aren't scalarized on fixed vectors
</td>
</tr>
<tr>
<th>Labels</th>
<td>
backend:RISC-V,
llvm:codegen,
missed-optimization,
llvm:SelectionDAG
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
lukel97
</td>
</tr>
</table>
<pre>
The functions below multiply a vector with a splatted scalar, computed from an add:
```llvm
define <4 x i64> @f_v4i64(<4 x i64> %x, i64 %y) {
%1 = insertelement <4 x i64> poison, i64 %y, i32 0
%2 = shufflevector <4 x i64> %1, <4 x i64> poison, <4 x i32> zeroinitializer
%3 = add <4 x i64> %2, <i64 3, i64 3, i64 3, i64 3>
%4 = mul <4 x i64> %x, %3
ret <4 x i64> %4
}
define <vscale x 4 x i64> @f_nxv4i64(<vscale x 4 x i64> %x, i64 %y) {
%1 = insertelement <vscale x 4 x i64> poison, i64 %y, i32 0
%2 = shufflevector <vscale x 4 x i64> %1, <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer
%3 = add <vscale x 4 x i64> %2, shufflevector(<vscale x 4 x i64> insertelement(<vscale x 4 x i64> poison, i64 3, i32 0), <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer)
%4 = mul <vscale x 4 x i64> %x, %3
ret <vscale x 4 x i64> %4
}
```
When compiled with `llc -o - -mattr=+v`, the splatted add is scalarized for the scalable version:
```asm
f_nxv4i64:
addi a0, a0, 3
vsetvli a1, zero, e64, m4, ta, ma
vmul.vx v8, v8, a0
ret
```
But the fixed version doesn't:
```asm
f_v4i64:
vsetivli zero, 4, e64, m2, ta, ma
vmv.v.x v10, a0
vadd.vi v10, v10, 3
vmul.vv v8, v8, v10
ret
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVs1u4z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