<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/76623>76623</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[InstCombine] Missed optimization: fold `other_cond && (a s> -1) && a s< b` to `other_cond && a u< b` when `b` is a specific constant
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
XChy
</td>
</tr>
</table>
<pre>
Alive2 proof: https://alive2.llvm.org/ce/z/DT9eDZ
### Motivating example:
```llvm
define i1 @src(i32 %a, i1 %other_cond) {
entry:
%cmp1 = icmp sgt i32 %a, -1
%or.cond = select i1 %other_cond, i1 %cmp1, i1 false
%cmp2 = icmp slt i32 %a, 10086
%ret = select i1 %or.cond, i1 %cmp2, i1 false
ret i1 %ret
}
```
can be folded to:
```llvm
define i1 @tgt(i32 %a, i1 %other_cond) {
entry:
%or.cond = icmp ult i32 %a, 10086
%ret = select i1 %other_cond, i1 %or.cond, i1 false
ret i1 %ret
}
```
However, it folds if we replace the logical `and` with a bitwise `and`. I thought that Reassociate should be to blame, but [godbolt](https://godbolt.org/z/fY5vPx951) told me that similar fold for bitwise `and` is handled by InstCombine. Therefore, I tag InstCombine here.
### Real-world motivation
This snippet of IR is derived from [qemu/net/util.c@net_parse_macaddr](https://github.com/qemu/qemu/blob/7425b6277f12e82952cede1f531bfc689bf77fb1/net/util.c) (after O3 pipeline).
The example above is a reduced version. If you're interested in the original suboptimal IR and optimal IR, see also:https://godbolt.org/z/Ea8GGxKba
**Let me know if you can confirm that it's an optimization opportunity, thanks.**
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVc2O2zYQfhr6MlhBoizJOviwWcfpog1aBDm0vSxIcWSxoUiVHNnrPH1BSZu1U6Mp1iAskDOcGX7f_IgQ9MEiblnxjhW7lRipc377-0N3Xkmnztt7o4_IYfDOtSy_h45oCCy_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