llvm / llvm/llvm-project

[InstCombine] missed fold for fold sub(C, or(X, C)) for constant C

Open
#202,249 2 comments 0 reactions 0 assignees View on GitHub
llvm:instcombine missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

When the input is first OR'd with `smax` and then subtracted from `smax`, the result is always just the sign bit of X isolated in the high bit position. InstCombine currently misses this fold.

#### Example

```llvm
define i32 @src(i32 %x) {
%a = or i32 %x, 2147483647 ; smax = 0x7FFFFFFF
%b = sub i32 2147483647, %a
ret i32 %b
}
```

Can look more like:
```llvm
define i32 @tgt(i32 %x) {
%a = and i32 %x, 2147483648 ; signbit = 0x80000000
ret i32 %a
}
```

Subtracting from `smax` (after the `or` operation with the same bits set) cancels those bits exactly to zero and leaves the sign bit of `%x` unchanged.

Alive2 link: https://alive2.llvm.org/ce/z/uCFH2K

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.