<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/146642>146642</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[InstCombine] Missed optimization for icmp of select with constants and invertible binop
</td>
</tr>
<tr>
<th>Labels</th>
<td>
llvm:instcombine,
missed-optimization
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
nikic
</td>
</tr>
</table>
<pre>
We currently fail to fold variations of this pattern:
https://alive2.llvm.org/ce/z/tT3yNU
```llvm
define i1 @src(i64 %a, i1 %c) {
%idx = shl nsw i64 %a, 3
%sel = select i1 %c, i64 8, i64 0
%cmp = icmp eq i64 %idx, %sel
ret i1 %cmp
}
define i1 @tgt(i64 %a, i1 %c) {
%sel = select i1 %c, i64 1, i64 0
%cmp = icmp eq i64 %a, %sel
ret i1 %cmp
}
```
The underlying pattern here is something like `f(x) == y` converted to `x == f^-1(y)` where f invertible and `f^-1(y)` folds because the shift arms are constant.
>From https://github.com/dtcxzyw/llvm-opt-benchmark/pull/2524/files/9ca17b717ceea34b52263bf8b20cf12ad02cd4fd#r2179220735.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyMVE1v5DYM_TX0RchApvwxPviQzdRAD-1pi55liR6zke2pJM9Hfn0hTyab7WERQAfJenwi-R6tQ-DjTNRC-Q3KQ6bXOC6-nfmVTdYv9tb-TcKs3tMc3U0Mmp2IixgWZ8VZe9aRlzmIZRBx5CBOOkbyM6hnkGmNMZ5COmEH2GnHZ8Kdc-dpt_gjYGcIsHsD7OJ3dfvzrxRUyftKKJDPlgaeSXAuoJDBG8A9V4UALDXgy_YdSwPYCKi_gXwW6cz2KkAdRBidmMNFfI5QD1AgdweRIxN_ML1s8P1jIx94M502PKcN_fsgZXtN0DvhhvX0wTadUkn14d6Nn2qJx_ilWn6dZv7VNPXXknx0_57w95HEOlvy7sbz8aGuGMmT4CDCMlEc043jVxJQyQFwf90KUIeUxA0qKcwyn8lHssk5UMnr43aA8renHHB_A2wS8rIxD4K3AO4dCT3bO_H_oMmBQfRk9BpIxJFEGHmIQvspCO0pvRqinuPuXkrnl0n8bMcjx3Htd2aZADsbzfXtdgHskvGellN86mk246T9K2B3Wp0D7LDEArAb2FEA7Bqj87qv89oQaVX0JWKl-mHfozRDjtpKNLYYLKDymNcNoqxVuctsq2yjGp1Rm9dlXhSyyZtsbIfG2kqaRsvaqFrqMq9RFkYNpbJW6SLjFiWWspYoa8zLZkc235sqV1WliroscygkTZrdx5BlHMJKbV5UVYGZ0z25sA074jZh6pnnEM0y9TwTIAK-AOLEIZBNXeCJ37YhT3flIfPt1p9-PQYopOMQw4-3Ike3_Up-n0N8eecsD-KPjU58phPD4u8eXYaHty8cxw_dwib9Jyf0PC-nbPWu_YWMW03vGp788g-ZCNhtPUiCvbfh3OJ_AQAA__-vsXyQ">