<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/54364>54364</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Failure to merge blend of negation and multiply into a single multiply
</td>
</tr>
<tr>
<th>Labels</th>
<td>
llvm:optimizations
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
RKSimon
</td>
</tr>
</table>
<pre>
https://simd.godbolt.org/z/P3jfPz5G6
SLP recognises the mul/sub alt bin opcodes and vectorizes the operations, but InstCombine fails to recognise that the sub is a negation that would allow it to be merged into the multiply.
```
void mul_and_neg(int * __restrict dst, const int * src) {
*dst++ = *src++ * 257;
*dst++ = *src++ * -3;
*dst++ = *src++ * -1;
*dst++ = *src++ * -9;
}
```
clang -g0 -O3 -march=btver2
```
define void @mul_and_neg(i32* noalias nocapture noundef writeonly %dst, i32* nocapture noundef readonly %src) {
entry:
%0 = bitcast i32* %src to <4 x i32>*
%1 = load <4 x i32>, <4 x i32>* %0, align 4
%2 = mul nsw <4 x i32> %1, <i32 257, i32 -3, i32 poison, i32 -9>
%3 = sub nsw <4 x i32> <i32 poison, i32 poison, i32 0, i32 poison>, %1
%4 = shufflevector <4 x i32> %2, <4 x i32> %3, <4 x i32> <i32 0, i32 1, i32 6, i32 3>
%5 = bitcast i32* %dst to <4 x i32>*
store <4 x i32> %4, <4 x i32>* %5, align 4
ret void
}
vs
define void @mul_and_neg(i32* noalias nocapture noundef writeonly %dst, i32* nocapture noundef readonly %src) {
entry:
%0 = bitcast i32* %src to <4 x i32>*
%1 = load <4 x i32>, <4 x i32>* %0, align 4
%2 = mul nsw <4 x i32> %1, <i32 257, i32 -3, i32 -1, i32 -9>
%3 = bitcast i32* %dst to <4 x i32>*
store <4 x i32> %2, <4 x i32>* %3, align 4
ret void
}
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztVtmO2jAU_RrnxQIFO2F5yAMDnapqpY46HzByEid45NiR7UDh63vtsMOMhNS-VQpg3-XcPZdcl9ts5VxrEZ0j8gyPFU05rHWZa-mG2tRA2sHnhb5XL7v06xjFSxTP--_XHy_Y8ELXSlhusVtx3HTSg3Q5ZtLhXCis20KXwGWqxGteOG3Ebi-sW26YE1pZRBY47xz-pqxb6Ab0OK6YkCCnTyZAibmg6Q0IwMSK1wGhZ210J0uwLPUGC-d1c3CJm5qXWCi47l10opXb4XkoaBzvn3Bda1F6wTfw-g1sIDIFfYzIHL-9GW6dEYXDpXXe8QICcPjAt6ZAZIbR5KmHwp4aJJ_gwYguPSFI9QTQIekE0QfkB_Qx8dFj4rOjOJos7-ankEzVeFDHePCT4kHDTLECrNytuSF3NUpe-aKGxKIkvsotJd6w0kwKZuG3YK3rDIdTp0ATb4xwXCu5BQfTfdqPStfChrPyIHtdDa6c2fpmP2YjjUMWcuEK5svYo_aqvoMQXST4d6DTL8A60xwFTalZeS21uFELljwDQqwVTs5gSICBjGBlN5eKwcoeDiihUfrQfQ_sT60WVqsjfeYNntBpQPcDcwe9R70EuLzFV-R9dN6tk42kt7Hqqkryfshv4yA3aQnu3aH2Xh0tjw6H8eFAL0NMP6ggNMpnFbTgJr_1KPmofOmd8hnuQk9fj0v_FrHnt_8T8M8mYDD6vPv_VmvctvAejj7QGof3YlRmtJzRGYuccJJnz7DvfBHBrbCycC457ExdnZac36GH9dUvNIatULU8bbWoM_JqpdfCrbp8WOgGLlKuDz-D1uh3GFa4Cms7Dlv4OU3oOIlWWclimkxylk-qEammRToh0xkhJK2KalZMykiynEuboRTWBgmgdK5bJxqxO6x0gtJlJDISExLTER2ReJyQIVzT6ZjkeUpjTlkBo8AbCH3oQfw_jshkwbu8qy0wpbDOnpjMWsgy58Ey4LPOrbTJfn1_FY1WUQgkC1H8ARpGeR0">