Issue 238376
Summary [O2] Missed constant folding of a umin comparison under branch conditions
Labels new issue
Assignees
Reporter maple-syntax
    The default O2 pipeline fails to fold the following function to `ret i1 false`.

The `if.then` branch is reachable only when `y != x && w != z`. Therefore, both wrapping differences `y - x` and `w - z` are nonzero, so their unsigned minimum cannot be zero.

Since the other branch also returns false, the entire function can be reduced to `ret i1 false`.

The upstream test [`[test_nonequal_domcond1](https://github.com/llvm/llvm-project/blob/8b5ece820daa3736362ce4144d79b3b586978592/llvm/test/Transforms/InstCombine/icmp-dom.ll)`](https://github.com/llvm/llvm-project/blob/8b5ece820daa3736362ce4144d79b3b586978592/llvm/test/Transforms/InstCombine/icmp-dom.ll) is fully folded by O2, but this equivalent input retains the `umin` computation.

### Input Program

**repro.ll**

```llvm
declare i64 @llvm.umin.i64(i64, i64)

define i1 @test_nonequal_domcond1(i64 %x, i64 %y, i64 %z, i64 %w) {
entry:
  %cond1 = icmp eq i64 %y, %x
  %mut.inverse = icmp ne i64 %w, %z
  %mut.cmp = xor i1 %mut.inverse, true
  %or.cond = select i1 %cond1, i1 true, i1 %mut.cmp
  br i1 %or.cond, label %if.end, label %if.then

if.then:
  %sub1 = sub i64 %w, %z
  %sub2 = sub i64 %y, %x
  %umin = call i64 @llvm.umin.i64(i64 %sub1, i64 %sub2)
  %cmp = icmp eq i64 %umin, 0
  ret i1 %cmp

if.end:
  ret i1 false
}
```

### Actual Result

After `default<O2>`, LLVM retains:

```llvm
%cond1 = icmp ne i64 %y, %x
%mut.inverse.not = icmp ne i64 %w, %z
%or.cond.not = select i1 %cond1, i1 %mut.inverse.not, i1 false
%sub1 = sub i64 %w, %z
%sub2 = sub i64 %y, %x
%umin = tail call i64 @llvm.umin.i64(i64 %sub1, i64 %sub2)
%cmp = icmp eq i64 %umin, 0
%common.ret.op = select i1 %or.cond.not, i1 %cmp, i1 false
ret i1 %common.ret.op
```

### Expected Result

The function can be reduced to:

```llvm
define i1 @test_nonequal_domcond1(i64 %x, i64 %y, i64 %z, i64 %w) {
  ret i1 false
}
```

This eliminates all eight computational IR instructions retained by O2.

Interestingly, standalone InstCombine already simplifies the `if.then` computation to `ret i1 false`, leaving both branches returning false. However, the full O2 pipeline still retains the `umin` computation.

### Command

```bash
opt -passes=verify -disable-output repro.ll
opt -S -passes=instcombine repro.ll -o -
opt -S -passes='default<O2>' repro.ll -o -
```

### LLVM Version

- LLVM `24.0.0git`
- Commit: [`[8b5ece820daa3736362ce4144d79b3b586978592](https://github.com/llvm/llvm-project/commit/8b5ece820daa3736362ce4144d79b3b586978592)`](https://github.com/llvm/llvm-project/commit/8b5ece820daa3736362ce4144d79b3b586978592)
- Release build with assertions enabled
- Default target: `x86_64-unknown-linux-gnu`
- No explicit target triple in the input
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to