[llvm] [InstCombine] Implement folds of icmp of UCMP/SCMP call and a constant (PR #96118)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 21 12:09:28 PDT 2024
================
@@ -3926,6 +3926,52 @@ foldICmpUSubSatOrUAddSatWithConstant(ICmpInst::Predicate Pred,
ConstantInt::get(Op1->getType(), EquivInt));
}
+static Instruction *
+foldICmpOfCmpIntrinsicWithConstant(ICmpInst::Predicate Pred, IntrinsicInst *I,
+ const APInt &C,
+ InstCombiner::BuilderTy &Builder) {
+ std::optional<ICmpInst::Predicate> NewPredicate = std::nullopt;
+ switch (Pred) {
+ case ICmpInst::ICMP_EQ:
+ case ICmpInst::ICMP_NE:
+ if (C.isZero())
+ NewPredicate = Pred;
+ else if (C.isOne())
+ NewPredicate =
+ Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_ULE;
+ else if (C.isAllOnes())
+ NewPredicate =
+ Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_UGE;
+ break;
+
+ case ICmpInst::ICMP_SGT:
+ if (C.isAllOnes())
+ NewPredicate = ICmpInst::ICMP_UGE;
+ else if (C.isZero())
+ NewPredicate = ICmpInst::ICMP_UGT;
+ break;
+
+ case ICmpInst::ICMP_SLT:
+ if (C.isZero())
+ NewPredicate = ICmpInst::ICMP_ULT;
+ else if (C.isOne())
+ NewPredicate = ICmpInst::ICMP_ULE;
+ break;
+
+ default:
+ break;
+ }
+
+ if (!NewPredicate)
+ return nullptr;
+
+ if (I->getIntrinsicID() == Intrinsic::scmp)
+ NewPredicate = ICmpInst::getSignedPredicate(NewPredicate.value());
+ Value *LHS = I->getOperand(0);
+ Value *RHS = I->getOperand(1);
+ return new ICmpInst(NewPredicate.value(), LHS, RHS);
----------------
nikic wrote:
```suggestion
if (I->getIntrinsicID() == Intrinsic::scmp)
NewPredicate = ICmpInst::getSignedPredicate(*NewPredicate);
Value *LHS = I->getOperand(0);
Value *RHS = I->getOperand(1);
return new ICmpInst(*NewPredicate, LHS, RHS);
```
Is the preferred style.
https://github.com/llvm/llvm-project/pull/96118
More information about the llvm-commits
mailing list