[llvm] [InstCombine] Add combines for unsigned comparison of absolute value to constant (PR #176148)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 19 10:20:53 PST 2026
================
@@ -4301,6 +4301,42 @@ Instruction *InstCombinerImpl::foldICmpIntrinsicWithConstant(ICmpInst &Cmp,
II->getArgOperand(1));
}
break;
+ case Intrinsic::abs: {
+ if (!Cmp.getOperand(0)->hasOneUse())
+ return nullptr;
+
+ Value *X = II->getArgOperand(0);
+ bool IntMinIsPoison =
+ dyn_cast<ConstantInt>(II->getArgOperand(1))->getValue().isOne();
+
+ // abs(X) u> C --> C >= 0 ? `X + C u> 2 * C` : `false`
+ if (Pred == CmpInst::ICMP_UGT) {
+ if (C.isNegative()) {
+ Cmp.replaceAllUsesWith(ConstantInt::getFalse(Cmp.getType()));
+ return nullptr;
+ }
+
+ return new ICmpInst(ICmpInst::ICMP_UGT,
+ Builder.CreateAdd(X, ConstantInt::get(Ty, C)),
+ ConstantInt::get(Ty, 2 * C));
+ }
+
+ // If abs(INT_MIN) is poison:
+ // abs(X) u< C --> C >= 1 ? `X + (C - 1) u<= 2 * (C - 1)` : C != 0
+ if (IntMinIsPoison && Pred == CmpInst::ICMP_ULT) {
+ if (C.slt(1)) {
+ Cmp.replaceAllUsesWith(
+ ConstantInt::getBool(Cmp.getType(), !C.isZero()));
+ return nullptr;
+ }
----------------
dtcxzyw wrote:
```suggestion
if (IntMinIsPoison && Pred == CmpInst::ICMP_ULT && C.slt(1)) {
```
https://github.com/llvm/llvm-project/pull/176148
More information about the llvm-commits
mailing list