<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/115651>115651</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[InstSimplify][InstCombine] miscompilation of comparison with `min` instrinsic
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
bongjunj
</td>
</tr>
</table>
<pre>
In `simplify` case, the function retruns `%sel5 > @umin(undef, 1)`, which always produces `true` before optimization.
After optimization, it produces `undef` which can be either of 0 or 1.
The miscompilation seems to happen in `instcombine` too, but I am not sure at the moment.
Alive2 report: https://alive2.llvm.org/ce/z/nHEYp4
```llvm
; instsimplify
----------------------------------------
define i1 @potential_test14.2(i1 %c0, i1 %c2, i1 %c3) {
#0:
%shl1 = shl nsw i1 %c3, 0
%sel1 = select i1 %c2, i1 %shl1, i1 %c3
%sel2 = select i1 %sel1, i32 0, i32 1
%sel3 = select i1 %c0, i32 %sel2, i32 undef
%sel4 = select i1 %c2, i32 0, i32 7
%max = umax i32 3, %sel4
%sel5 = select i1 %c3, i32 %sel3, i32 %max
%min = umin i32 %sel3, 1
%cmp1 = icmp ugt i32 %sel5, %min
ret i1 %cmp1
}
=>
define i1 @potential_test14.2(i1 %c0, i1 %c2, i1 %c3) {
#0:
%sel2 = select i1 %c3, i32 0, i32 1
%sel3 = select i1 %c0, i32 %sel2, i32 undef
%sel4 = select i1 %c2, i32 0, i32 7
%max = umax i32 3, %sel4
%sel5 = select i1 %c3, i32 %sel3, i32 %max
%cmp1 = icmp ugt i32 %sel5, %sel3
ret i1 %cmp1
}
Transformation doesn't verify!
ERROR: Target's return value is more undefined
Example:
i1 %c0 = #x0 (0)
i1 %c2 = #x1 (1)
i1 %c3 = #x0 (0)
Source:
i1 %shl1 = #x0 (0)
i1 %sel1 = #x0 (0)
i32 %sel2 = #x00000001 (1)
i32 %sel3 = #x00000000 (0) [based on undef value]
i32 %sel4 = #x00000000 (0)
i32 %max = #x00000003 (3)
i32 %sel5 = #x00000003 (3)
i32 %min = #x00000000 (0) [based on undef value]
i1 %cmp1 = #x1 (1)
Target:
i32 %sel2 = #x00000001 (1)
i32 %sel3 = #x00000000 (0) [based on undef value]
i32 %sel4 = #x00000000 (0)
i32 %max = #x00000003 (3)
i32 %sel5 = #x00000003 (3)
i1 %cmp1 = #x1 (1)
Source value: #x1 (1)
Target value: #x1 (1)
Summary:
0 correct transformations
1 incorrect transformations
0 failed-to-prove transformations
0 Alive2 errors
```
```llvm
; instcombine
----------------------------------------
define i1 @potential_test14.2(i1 %c0, i1 %c2, i1 %c3) {
#0:
%shl1 = shl nsw i1 %c3, 0
%sel1 = select i1 %c2, i1 %shl1, i1 %c3
%sel2 = select i1 %sel1, i32 0, i32 1
%sel3 = select i1 %c0, i32 %sel2, i32 undef
%sel4 = select i1 %c2, i32 0, i32 7
%max = umax i32 3, %sel4
%sel5 = select i1 %c3, i32 %sel3, i32 %max
%min = umin i32 %sel3, 1
%cmp1 = icmp ugt i32 %sel5, %min
ret i1 %cmp1
}
=>
define i1 @potential_test14.2(i1 %c0, i1 %c2, i1 %c3) {
#0:
%not.c3 = xor i1 %c3, 1
%cmp1 = select i1 %not.c3, i1 %c0, i1 0
ret i1 %cmp1
}
Transformation doesn't verify!
ERROR: Value mismatch
Example:
i1 %c0 = #x0 (0)
i1 %c2 = #x0 (0)
i1 %c3 = #x0 (0)
Source:
i1 %shl1 = #x0 (0)
i1 %sel1 = #x0 (0)
i32 %sel2 = #x00000001 (1)
i32 %sel3 = #x00000000 (0) [based on undef value]
i32 %sel4 = #x00000007 (7)
i32 %max = #x00000007 (7)
i32 %sel5 = #x00000007 (7)
i32 %min = #x00000000 (0) [based on undef value]
i1 %cmp1 = #x1 (1)
Target:
i1 %not.c3 = #x1 (1)
i1 %cmp1 = #x0 (0)
Source value: #x1 (1)
Target value: #x0 (0)
Summary:
0 correct transformations
1 incorrect transformations
0 failed-to-prove transformations
0 Alive2 errors
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWE1v2zgT_jX0ZRCDpCzLPviQxDHenl6gLRbY04KSRhYLkRRIKh_99QtSsiN5ldYoit0CqRE4ouaZZ4Yz4-GAwjl51Ig7kt6RdL8Qna-N3eVGH790-ssiN-XL7oMGsqZOqraR1QtZUyiEQ8LvwdcIVacLL40Gi9522gUs4anDJgWSPABZ0U5JTfim0yVWQY0Rvo2oe3iqZVGDaJ7Ei4PWmrIrMFJ422EwlWNlLIJpvVTyqwiWloTuCb29rTzaiSAQSj-h6W2u6WCoEBpyBJS-DroVUDAW2MDYf3-uEZR0hVGtbCIvOETlwBuoRduiBhkjIrXzhVG51NFTb0xwIO88fAChQBsPrrMIwsdAKaNQ-4mp20Y-IgeLrbGeJLdQe986ktwSfiD8IKJ42TSPamnskfBDgYQfvhJ-0P97-LNdjblCQONfgA-vkjsITp5TF9_eXPnp0SVWUiNIFhLZGo_aS9H85dF5tlpywjdBxNMipnN45qPnhPAtkOxucIknNOwvLiAAXN0wIMkeXN2Adk8jvXugYyCegNhg4WdsBaqJ5Yky_6dyoIwKCQd6emBTtWTG5hk7MJ-WfbX16r1o9abHY5PZyKQSz1GnCw9BGAMxsE1dS2fIk6lr46USz2M7oYijHakvFcYhKFTbh10WqoXu6EfgdHAt_L4HDYtnV1Q78JBsfyrIPUke_q3Cmk35a4B-dsJ_-Yxfk8lI0Kt8M5OfrdCuMlb1DbI06DThmYdHtKHTcDbuTQ8fP_7_Y-hvn4U9oic8c4G-sxoeRdMhSAcqtPkYT6mxnGg_C9U2eM7uKSdxL4QnzxQI39BwqIzl_CwP6w27lCdv6fffn0xni0uj52b1ptlzl5pDvBbQK6T_XLr4ms5L5IhzS9K7XDgsweg-dH04Sbq_pFl9g2aEPJXiCJgEYDLjWXod8tRnfnQLbFK6M-kcCrKvrHO63leovxukvpoHn5PbOUgfwW9CBq5OKWFfRt2WQmGsDT3JT_qCOwEYSP0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