| Issue |
202576
|
| Summary |
Fold `select (x == -1), y, umin(x, y)` to `umin(x, y)`
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
ParkHanbum
|
LLVM can miss a small redundant select around an unsigned min in NPOS-style
string-position code.
Representative shape:
```llvm
%is_npos = icmp eq i64 %pos, -1
%clamped = call i64 @llvm.umin.i64(i64 %pos, i64 %len)
%out = select i1 %is_npos, i64 %len, i64 %clamped
```
This can be folded to:
```llvm
%out = call i64 @llvm.umin.i64(i64 %pos, i64 %len)
```
The reason is the unsigned all-ones sentinel:
```text
%pos == -1 -> umin(%pos, %len) == %len
%pos != -1 -> select already returns umin(%pos, %len)
```
So both select arms are equivalent to the existing `umin` result.
Observed benchmark-backed evidence: 98 hits in 50 unique files across 6
projects. Representative hit locations by project:
- LLVM: `llvm/optimized/COFFImportFile.ll:179`
- Hermes: `hermes/optimized/FileCheck.ll:1670`
- HDF5: `hdf5/optimized/H5FDmulti.ll:3485`
- OIIO: `oiio/optimized/strutil.ll:8527`
- Assimp: `assimp/optimized/MD3Loader.ll:10239`
- OpenSSL: `openssl/optimized/quic_channel.ll:2748`
The pattern appears in `StringRef::find`-style code, where `-1` represents
`npos` and the result is clamped against a remaining length.
The intended fold should stay narrow:
- unsigned min only, such as `llvm.umin`;
- all-ones sentinel only, such as `icmp eq X, -1`;
- the selected sentinel arm must be the same bound operand used by the `umin`;
- the false arm must be the same `umin` result;
- the commuted `umin(Y, X)` spelling is equivalent;
- the inverted condition spelling is also equivalent:
```llvm
%is_not_npos = icmp ne i64 %pos, -1
%out = select i1 %is_not_npos, i64 %clamped, i64 %len
```
This should not be generalized to signed min/max, non-all-ones sentinels, or
unrelated select arms without separate proof.
AliveProof : https://alive2.llvm.org/ce/z/JeHF6i
Compiler-explorer sample & perf : https://compiler-explorer.com/z/M967hGTh5
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs