| commit | a43c49f41e7cb6794e2f49abe8d14085948fe88c | [log] [tgz] |
|---|---|---|
| author | Pedro Lobo <pedro.lobo@tecnico.ulisboa.pt> | Fri Aug 22 23:23:14 2025 +0100 |
| committer | GitHub <noreply@github.com> | Fri Aug 22 23:23:14 2025 +0100 |
| tree | 3be1fb5a836daa77ca8b95444129830fe5b31f58 | |
| parent | 085d0b001674944613f61a143060350f138859e3 [diff] |
[InstCombine] Fold `(x == A) || (x & -Pow2) == A + 1` into range check (#153842)
Extends `foldAndOrOfICmpsUsingRanges` to recognize and fold idioms of
the form `(x == A) || (x & -Pow2) == A + 1`, where A + 1 is aligned to
the mask and A > |mask|.
These patterns represent a special case of contiguous range checks and
can be optimized into an addition and comparison.
```llvm
define i1 @src(i32 %x) {
%icmp1 = icmp eq i32 %x, 127
%and = and i32 %x, -32
%icmp2 = icmp eq i32 %and, 128
%ret = or i1 %icmp1, %icmp2
ret i1 %ret
}
define i1 @tgt(i32 %x) {
%1 = add i32 %x, -127
%2 = icmp ult i32 %1, 33
ret i1 %2
}
```
The same can be done for a conjunction of inequalities, of the form
`(x != A) && (x & -Pow2) != A + 1`.
```llvm
define i1 @src(i32 %x) {
%icmp1 = icmp ne i32 %x, 127
%and = and i32 %x, -32
%icmp2 = icmp ne i32 %and, 128
%result = and i1 %icmp1, %icmp2
ret i1 %result
}
define i1 @tgt(i32 %x) {
%1 = add i32 %x, -160
%2 = icmp ult i32 %1, -33
ret i1 %2
}
```
Alive2 Proof:
- Equality: https://alive2.llvm.org/ce/z/gwELqs
- Inequality: https://alive2.llvm.org/ce/z/ZPdSDt
Closes #152948.Welcome to the LLVM project!
This repository contains the source code for LLVM, a toolkit for the construction of highly optimized compilers, optimizers, and run-time environments.
The LLVM project has multiple components. The core of the project is itself called “LLVM”. This contains all of the tools, libraries, and header files needed to process intermediate representations and convert them into object files. Tools include an assembler, disassembler, bitcode analyzer, and bitcode optimizer.
C-like languages use the Clang frontend. This component compiles C, C++, Objective-C, and Objective-C++ code into LLVM bitcode -- and from there into object files, using LLVM.
Other components include: the libc++ C++ standard library, the LLD linker, and more.
Consult the Getting Started with LLVM page for information on building and running LLVM.
For information on how to contribute to the LLVM project, please take a look at the Contributing to LLVM guide.
Join the LLVM Discourse forums, Discord chat, LLVM Office Hours or Regular sync-ups.
The LLVM project has adopted a code of conduct for participants to all modes of communication within the project.