[llvm] 0162386 - [NFC] Add CmpIntrinsic class to represent calls to UCMP/SCMP intrinsics (#98177)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 9 12:05:57 PDT 2024


Author: Poseydon42
Date: 2024-07-09T21:05:53+02:00
New Revision: 01623860c5dfcf922860c63ac7fa3aa07a0a7ac7

URL: https://github.com/llvm/llvm-project/commit/01623860c5dfcf922860c63ac7fa3aa07a0a7ac7
DIFF: https://github.com/llvm/llvm-project/commit/01623860c5dfcf922860c63ac7fa3aa07a0a7ac7.diff

LOG: [NFC] Add CmpIntrinsic class to represent calls to UCMP/SCMP intrinsics (#98177)

Added: 
    

Modified: 
    llvm/include/llvm/IR/IntrinsicInst.h
    llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/IR/IntrinsicInst.h b/llvm/include/llvm/IR/IntrinsicInst.h
index 3963a5c8ab8f9..2a37c06dd2c3c 100644
--- a/llvm/include/llvm/IR/IntrinsicInst.h
+++ b/llvm/include/llvm/IR/IntrinsicInst.h
@@ -834,6 +834,43 @@ class MinMaxIntrinsic : public IntrinsicInst {
   }
 };
 
+/// This class represents a ucmp/scmp intrinsic
+class CmpIntrinsic : public IntrinsicInst {
+public:
+  static bool classof(const IntrinsicInst *I) {
+    switch (I->getIntrinsicID()) {
+    case Intrinsic::scmp:
+    case Intrinsic::ucmp:
+      return true;
+    default:
+      return false;
+    }
+  }
+  static bool classof(const Value *V) {
+    return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
+  }
+
+  Value *getLHS() const { return const_cast<Value *>(getArgOperand(0)); }
+  Value *getRHS() const { return const_cast<Value *>(getArgOperand(1)); }
+
+  static bool isSigned(Intrinsic::ID ID) { return ID == Intrinsic::scmp; }
+  bool isSigned() const { return isSigned(getIntrinsicID()); }
+
+  static CmpInst::Predicate getGTPredicate(Intrinsic::ID ID) {
+    return isSigned(ID) ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
+  }
+  CmpInst::Predicate getGTPredicate() const {
+    return getGTPredicate(getIntrinsicID());
+  }
+
+  static CmpInst::Predicate getLTPredicate(Intrinsic::ID ID) {
+    return isSigned(ID) ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
+  }
+  CmpInst::Predicate getLTPredicate() const {
+    return getLTPredicate(getIntrinsicID());
+  }
+};
+
 /// This class represents an intrinsic that is based on a binary operation.
 /// This includes op.with.overflow and saturating add/sub intrinsics.
 class BinaryOpIntrinsic : public IntrinsicInst {

diff  --git a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
index 20f5dba413212..95de8eceb6be5 100644
--- a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
+++ b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
@@ -538,29 +538,28 @@ static bool processAbsIntrinsic(IntrinsicInst *II, LazyValueInfo *LVI) {
   return false;
 }
 
-static bool processCmpIntrinsic(IntrinsicInst *II, LazyValueInfo *LVI) {
-  bool IsSigned = II->getIntrinsicID() == Intrinsic::scmp;
-  ConstantRange LHS_CR = LVI->getConstantRangeAtUse(II->getOperandUse(0),
-                                                    /*UndefAllowed*/ false);
-  ConstantRange RHS_CR = LVI->getConstantRangeAtUse(II->getOperandUse(1),
-                                                    /*UndefAllowed*/ false);
+static bool processCmpIntrinsic(CmpIntrinsic *CI, LazyValueInfo *LVI) {
+  ConstantRange LHS_CR =
+      LVI->getConstantRangeAtUse(CI->getOperandUse(0), /*UndefAllowed*/ false);
+  ConstantRange RHS_CR =
+      LVI->getConstantRangeAtUse(CI->getOperandUse(1), /*UndefAllowed*/ false);
 
-  if (LHS_CR.icmp(IsSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT, RHS_CR)) {
+  if (LHS_CR.icmp(CI->getGTPredicate(), RHS_CR)) {
     ++NumCmpIntr;
-    II->replaceAllUsesWith(ConstantInt::get(II->getType(), 1));
-    II->eraseFromParent();
+    CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
+    CI->eraseFromParent();
     return true;
   }
-  if (LHS_CR.icmp(IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, RHS_CR)) {
+  if (LHS_CR.icmp(CI->getLTPredicate(), RHS_CR)) {
     ++NumCmpIntr;
-    II->replaceAllUsesWith(ConstantInt::getSigned(II->getType(), -1));
-    II->eraseFromParent();
+    CI->replaceAllUsesWith(ConstantInt::getSigned(CI->getType(), -1));
+    CI->eraseFromParent();
     return true;
   }
   if (LHS_CR.icmp(ICmpInst::ICMP_EQ, RHS_CR)) {
     ++NumCmpIntr;
-    II->replaceAllUsesWith(ConstantInt::get(II->getType(), 0));
-    II->eraseFromParent();
+    CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 0));
+    CI->eraseFromParent();
     return true;
   }
 
@@ -658,9 +657,8 @@ static bool processCallSite(CallBase &CB, LazyValueInfo *LVI) {
     return processAbsIntrinsic(&cast<IntrinsicInst>(CB), LVI);
   }
 
-  if (CB.getIntrinsicID() == Intrinsic::scmp ||
-      CB.getIntrinsicID() == Intrinsic::ucmp) {
-    return processCmpIntrinsic(&cast<IntrinsicInst>(CB), LVI);
+  if (auto *CI = dyn_cast<CmpIntrinsic>(&CB)) {
+    return processCmpIntrinsic(CI, LVI);
   }
 
   if (auto *MM = dyn_cast<MinMaxIntrinsic>(&CB)) {


        


More information about the llvm-commits mailing list