Issue 238370
Summary [InstCombine] Missed unsigned comparison fold through bitreverse and a complemented mask
Labels new issue
Assignees
Reporter maple-syntax
    InstCombine misses an unsigned comparison simplification involving a contiguous low-bit mask constructed using `llvm.bitreverse`.

For valid i8 shift amounts, `bitreverse(0xff << y)` produces `0xff >> y`. Therefore, `x <=u (x & mask)` is equivalent to `x <=u mask`.

The existing upstream test [`[src_is_mask_bitreverse_not_mask](https://github.com/llvm/llvm-project/blob/8b5ece820daa3736362ce4144d79b3b586978592/llvm/test/Transforms/InstCombine/icmp-and-lowbit-mask.ll)`](https://github.com/llvm/llvm-project/blob/8b5ece820daa3736362ce4144d79b3b586978592/llvm/test/Transforms/InstCombine/icmp-and-lowbit-mask.ll) already performs this fold, but the following equivalent input is not simplified to the same form under InstCombine or O2.

### Input Program

**repro.ll**

```llvm
declare i8 @llvm.bitreverse.i8(i8)

define i1 @src_is_mask_bitreverse_not_mask(i8 %x_in, i8 %y) {
  %x = xor i8 %x_in, 123
  %nmask = shl i8 -1, %y
  %mask = call i8 @llvm.bitreverse.i8(i8 %nmask)
  %mut.not = xor i8 %x, -1
 %mut.not1 = xor i8 %mask, -1
  %mut.or = or i8 %mut.not, %mut.not1
 %mut.not2 = xor i8 %mut.or, -1
  %r = icmp ule i8 %x, %mut.not2
  ret i1 %r
}
```

### Actual Result

After `default<O2>`, LLVM retains:

```llvm
%nmask = shl nsw i8 -1, %y
%1 = xor i8 %nmask, -1
%mut.not1 = tail call i8 @llvm.bitreverse.i8(i8 %1)
%2 = xor i8 %x_in, 123
%3 = and i8 %mut.not1, %2
%r = icmp eq i8 %3, 0
ret i1 %r
```

### Expected Result

The input can be simplified to the form already produced for the upstream test:

```llvm
%nmask = shl nsw i8 -1, %y
%mask = tail call i8 @llvm.bitreverse.i8(i8 %nmask)
%1 = xor i8 %x_in, 123
%r = icmp ule i8 %1, %mask
ret i1 %r
```

This reduces the number of computational IR instructions from six to four. The original shift and its poison behavior are preserved.

### 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