<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/64099>64099</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[SLPVectorizer] Handling select and insert operations with undef values may lead to unsound transform
</td>
</tr>
<tr>
<th>Labels</th>
<td>
miscompilation,
llvm:SLPVectorizer
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
antoniofrighetto
</td>
</tr>
</table>
<pre>
When handling select and insert operations with at least one undef value over two different vector-typed data as follows, SLPVectorizer seems to perform an unsound transformation, after which the target is more poisonous than source.
```llvm
define <3 x i32> @select_and_insert_values(<3 x i32> %a, <3 x i32> %b, <3 x i32> %c) {
%c0 = extractelement <3 x i32> %c, i32 0
%a0 = extractelement <3 x i32> %a, i32 0
%b0 = extractelement <3 x i32> %b, i32 0
%b1 = extractelement <3 x i32> %b, i32 1
%cmp0 = icmp ne i32 %c0, 0
%s0 = select i1 %cmp0, i32 %a0, i32 %b0
%s1 = select i1 %cmp0, i32 %a0, i32 %b1
%ra = insertelement <3 x i32> <i32 1, i32 1, i32 undef>, i32 %s0, i32 0
%rb = insertelement <3 x i32> %ra, i32 %s1, i32 1
ret <3 x i32> %rb
}
}
```
Alive2 report (https://alive2.llvm.org/ce/z/S5t6pe):
```console
define <3 x i32> @select_and_insert_values(<3 x i32> %a, <3 x i32> %b, <3 x i32> %c) {
%0:
%c0 = extractelement <3 x i32> %c, i32 0
%a0 = extractelement <3 x i32> %a, i32 0
%b0 = extractelement <3 x i32> %b, i32 0
%b1 = extractelement <3 x i32> %b, i32 1
%cmp0 = icmp ne i32 %c0, 0
%s0 = select i1 %cmp0, i32 %a0, i32 %b0
%s1 = select i1 %cmp0, i32 %a0, i32 %b1
%ra = insertelement <3 x i32> { 1, 1, undef }, i32 %s0, i32 0
%rb = insertelement <3 x i32> %ra, i32 %s1, i32 1
ret <3 x i32> %rb
}
=>
define <3 x i32> @select_and_insert_values(<3 x i32> %a, <3 x i32> %b, <3 x i32> %c) {
%0:
%c0 = extractelement <3 x i32> %c, i32 0
%cmp0 = icmp ne i32 %c0, 0
%1 = insertelement <2 x i1> poison, i1 %cmp0, i32 0
%2 = shufflevector <2 x i1> %1, <2 x i1> poison, 0, 0
%3 = shufflevector <3 x i32> %a, <3 x i32> poison, 0, 0
%4 = shufflevector <3 x i32> %b, <3 x i32> poison, 0, 1
%5 = select <2 x i1> %2, <2 x i32> %3, <2 x i32> %4
%6 = shufflevector <2 x i32> %5, <2 x i32> poison, 0, 1, 4294967295
ret <3 x i32> %6
}
Transformation doesn't verify!
ERROR: Target is more poisonous than source
Example:
<3 x i32> %a = < poison, poison, poison >
<3 x i32> %b = < poison, poison, poison >
<3 x i32> %c = < poison, poison, poison >
Source:
i32 %c0 = poison
i32 %a0 = poison
i32 %b0 = poison
i32 %b1 = poison
i1 %cmp0 = poison
i32 %s0 = poison
i32 %s1 = poison
<3 x i32> %ra = < poison, #x00000001 (1), #x00000000 (0) >
<3 x i32> %rb = < poison, poison, #x00000000 (0) >
Target:
i32 %c0 = poison
i1 %cmp0 = poison
<2 x i1> %1 = < poison, poison >
<2 x i1> %2 = < poison, poison >
<2 x i32> %3 = < poison, poison >
<2 x i32> %4 = < poison, poison >
<2 x i32> %5 = < poison, poison >
<3 x i32> %6 = < poison, poison, poison >
Source value: < poison, poison, #x00000000 (0) >
Target value: < poison, poison, poison >
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWF1vqzYY_jXOzatTgQ0kXOSiTRrtYtKm9mi7rAy8BE9gI9v04_z6ySZNIZA27aRpm3Z0lILt5_14_D7-gBsj9hJxTeIbEm8XvLOV0msurZJClVrsK7RWLTJVvKx_r1BCxWVRC7kHgzXmFrgsQEiD2oJqUXMrlDTwJGwF3EKN3FhQEqGTBZbwyOsOQT2iBvukoBBliRqlhUfMrdLf7EuLBRTccuAGSlXX6skQuoH7n3_9zQ8RP1CDQWwMWAUt6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