[llvm] [InstCombine] Fold signed icmp X, (X | C) when C is negative (PR #217805)

via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 22 10:24:31 PDT 2026


milkHongYe wrote:

> 这些模式在实际程序中是否存在?`isTruePredicate`它已经处理了几个使用非负 C 的简单情况。我认为这已经足够了。

Thank you for the review. I discovered this pattern while comparing code generation differences between clang and GCC on real-world programs. I noticed that clang generates more instructions than GCC in certain cases, so I believe there is room for optimization.Regarding the isTruePredicate issue: it handles icmp sle X, (X | C) only when C is non-negative, and explicitly skips the negative-C case via return !C->isNegative(). I think this is insufficient. And here are the assembly instruction count changes before and after the patch on RISC-V:
Before the patch, the assembly for or_sgt_intmin_2 was:
```
add	a1, a1, a3
add	a0, a0, a2
slli	a2, a1, 56
slli	a3, a0, 56
ori	a1, a1, -128
ori	a0, a0, -128
srai	a2, a2, 56
srai	a3, a3, 56
slt	a0, a0, a3
slt	a1, a1, a2
ret 
```
After the patch, the assembly output is:
```
add	a1, a1, a3
add	a0, a0, a2
slli	a1, a1, 56
slli	a0, a0, 56
srli	a0, a0, 63
srli	a1, a1, 63
xori	a0, a0, 1
xori	a1, a1, 1
ret
```
The instruction count drops from 10 to 8. The ori and slt instructions are replaced with simpler sign-bit extraction, and the OR operation is completely eliminated.



https://github.com/llvm/llvm-project/pull/217805


More information about the llvm-commits mailing list