[llvm] [InstCombine] Optimize and of overflow checks (PR #141962)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 14 18:56:39 PST 2026


mikaseianatsu wrote:

I noticed a missed optimization that seems to depend on operand ordering.

**First Input Program**

```
define i1 @test_pass_tt(i32 %0, i32 %1) {
common.ret:
  %2 = xor i32 %0, -1
  %3 = icmp ult i32 %1, %2
  %4 = xor i32 %1, -1
  %5 = icmp ugt i32 %0, %4
  %common.ret.op = and i1 %3, %5
  ret i1 %common.ret.op
}
```

**Second Input Program**

```
define i1 @test_pass_tt(i32 %0, i32 %1) {
common.ret:
  %2 = xor i32 %0, -1
  %3 = icmp ult i32 %1, %2
  %4 = xor i32 %1, -1
  %5 = icmp ugt i32 %0, %4
  %common.ret.op = and i1 %5, %3
  ret i1 %common.ret.op
}
```

The first input program can be optimized by LLVM into:

```
define i1 @test_pass_tt(i32 %0, i32 %1) {
common.ret:
  ret i1 false
}
```

The second remains unchanged. Since `and` is commutative and the two programs are semantically equivalent, it looks like a canonicalization-sensitive missed optimization.



https://github.com/llvm/llvm-project/pull/141962


More information about the llvm-commits mailing list