Skip to content

Commit 0162386

Browse files
authored
[NFC] Add CmpIntrinsic class to represent calls to UCMP/SCMP intrinsics (#98177)
1 parent 5b92713 commit 0162386

File tree

2 files changed

+52
-17
lines changed

2 files changed

+52
-17
lines changed

llvm/include/llvm/IR/IntrinsicInst.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,43 @@ class MinMaxIntrinsic : public IntrinsicInst {
834834
}
835835
};
836836

837+
/// This class represents a ucmp/scmp intrinsic
838+
class CmpIntrinsic : public IntrinsicInst {
839+
public:
840+
static bool classof(const IntrinsicInst *I) {
841+
switch (I->getIntrinsicID()) {
842+
case Intrinsic::scmp:
843+
case Intrinsic::ucmp:
844+
return true;
845+
default:
846+
return false;
847+
}
848+
}
849+
static bool classof(const Value *V) {
850+
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
851+
}
852+
853+
Value *getLHS() const { return const_cast<Value *>(getArgOperand(0)); }
854+
Value *getRHS() const { return const_cast<Value *>(getArgOperand(1)); }
855+
856+
static bool isSigned(Intrinsic::ID ID) { return ID == Intrinsic::scmp; }
857+
bool isSigned() const { return isSigned(getIntrinsicID()); }
858+
859+
static CmpInst::Predicate getGTPredicate(Intrinsic::ID ID) {
860+
return isSigned(ID) ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
861+
}
862+
CmpInst::Predicate getGTPredicate() const {
863+
return getGTPredicate(getIntrinsicID());
864+
}
865+
866+
static CmpInst::Predicate getLTPredicate(Intrinsic::ID ID) {
867+
return isSigned(ID) ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
868+
}
869+
CmpInst::Predicate getLTPredicate() const {
870+
return getLTPredicate(getIntrinsicID());
871+
}
872+
};
873+
837874
/// This class represents an intrinsic that is based on a binary operation.
838875
/// This includes op.with.overflow and saturating add/sub intrinsics.
839876
class BinaryOpIntrinsic : public IntrinsicInst {

llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -538,29 +538,28 @@ static bool processAbsIntrinsic(IntrinsicInst *II, LazyValueInfo *LVI) {
538538
return false;
539539
}
540540

541-
static bool processCmpIntrinsic(IntrinsicInst *II, LazyValueInfo *LVI) {
542-
bool IsSigned = II->getIntrinsicID() == Intrinsic::scmp;
543-
ConstantRange LHS_CR = LVI->getConstantRangeAtUse(II->getOperandUse(0),
544-
/*UndefAllowed*/ false);
545-
ConstantRange RHS_CR = LVI->getConstantRangeAtUse(II->getOperandUse(1),
546-
/*UndefAllowed*/ false);
541+
static bool processCmpIntrinsic(CmpIntrinsic *CI, LazyValueInfo *LVI) {
542+
ConstantRange LHS_CR =
543+
LVI->getConstantRangeAtUse(CI->getOperandUse(0), /*UndefAllowed*/ false);
544+
ConstantRange RHS_CR =
545+
LVI->getConstantRangeAtUse(CI->getOperandUse(1), /*UndefAllowed*/ false);
547546

548-
if (LHS_CR.icmp(IsSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT, RHS_CR)) {
547+
if (LHS_CR.icmp(CI->getGTPredicate(), RHS_CR)) {
549548
++NumCmpIntr;
550-
II->replaceAllUsesWith(ConstantInt::get(II->getType(), 1));
551-
II->eraseFromParent();
549+
CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
550+
CI->eraseFromParent();
552551
return true;
553552
}
554-
if (LHS_CR.icmp(IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, RHS_CR)) {
553+
if (LHS_CR.icmp(CI->getLTPredicate(), RHS_CR)) {
555554
++NumCmpIntr;
556-
II->replaceAllUsesWith(ConstantInt::getSigned(II->getType(), -1));
557-
II->eraseFromParent();
555+
CI->replaceAllUsesWith(ConstantInt::getSigned(CI->getType(), -1));
556+
CI->eraseFromParent();
558557
return true;
559558
}
560559
if (LHS_CR.icmp(ICmpInst::ICMP_EQ, RHS_CR)) {
561560
++NumCmpIntr;
562-
II->replaceAllUsesWith(ConstantInt::get(II->getType(), 0));
563-
II->eraseFromParent();
561+
CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 0));
562+
CI->eraseFromParent();
564563
return true;
565564
}
566565

@@ -658,9 +657,8 @@ static bool processCallSite(CallBase &CB, LazyValueInfo *LVI) {
658657
return processAbsIntrinsic(&cast<IntrinsicInst>(CB), LVI);
659658
}
660659

661-
if (CB.getIntrinsicID() == Intrinsic::scmp ||
662-
CB.getIntrinsicID() == Intrinsic::ucmp) {
663-
return processCmpIntrinsic(&cast<IntrinsicInst>(CB), LVI);
660+
if (auto *CI = dyn_cast<CmpIntrinsic>(&CB)) {
661+
return processCmpIntrinsic(CI, LVI);
664662
}
665663

666664
if (auto *MM = dyn_cast<MinMaxIntrinsic>(&CB)) {

0 commit comments

Comments
 (0)