Issue 76623
Summary [InstCombine] Missed optimization: fold `other_cond && (a s> -1) && a s< b` to `other_cond && a u< b` when `b` is a specific constant
Labels new issue
Assignees
Reporter XChy
    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.**
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to