[llvm] 3ab2247 - [ConstantRange] Optimize icmp() implementation (NFC)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 3 09:10:32 PDT 2024


Author: Nikita Popov
Date: 2024-07-03T18:10:22+02:00
New Revision: 3ab2247d10673419609333a33bca0eca8a56bf3d

URL: https://github.com/llvm/llvm-project/commit/3ab2247d10673419609333a33bca0eca8a56bf3d
DIFF: https://github.com/llvm/llvm-project/commit/3ab2247d10673419609333a33bca0eca8a56bf3d.diff

LOG: [ConstantRange] Optimize icmp() implementation (NFC)

These are pretty hot code paths, so provide direct implementations
for them, instead of going through makeSatisfyingICmpRegion().

Added: 
    

Modified: 
    llvm/lib/IR/ConstantRange.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/ConstantRange.cpp b/llvm/lib/IR/ConstantRange.cpp
index 50de975d83c0a..0ead677422803 100644
--- a/llvm/lib/IR/ConstantRange.cpp
+++ b/llvm/lib/IR/ConstantRange.cpp
@@ -241,7 +241,36 @@ bool ConstantRange::getEquivalentICmp(CmpInst::Predicate &Pred,
 
 bool ConstantRange::icmp(CmpInst::Predicate Pred,
                          const ConstantRange &Other) const {
-  return makeSatisfyingICmpRegion(Pred, Other).contains(*this);
+  if (isEmptySet() || Other.isEmptySet())
+    return true;
+
+  switch (Pred) {
+  case CmpInst::ICMP_EQ:
+    if (const APInt *L = getSingleElement())
+      if (const APInt *R = Other.getSingleElement())
+        return *L == *R;
+    return false;
+  case CmpInst::ICMP_NE:
+    return inverse().contains(Other);
+  case CmpInst::ICMP_ULT:
+    return getUnsignedMax().ult(Other.getUnsignedMin());
+  case CmpInst::ICMP_ULE:
+    return getUnsignedMax().ule(Other.getUnsignedMin());
+  case CmpInst::ICMP_UGT:
+    return getUnsignedMin().ugt(Other.getUnsignedMax());
+  case CmpInst::ICMP_UGE:
+    return getUnsignedMin().uge(Other.getUnsignedMax());
+  case CmpInst::ICMP_SLT:
+    return getSignedMax().slt(Other.getSignedMin());
+  case CmpInst::ICMP_SLE:
+    return getSignedMax().sle(Other.getSignedMin());
+  case CmpInst::ICMP_SGT:
+    return getSignedMin().sgt(Other.getSignedMax());
+  case CmpInst::ICMP_SGE:
+    return getSignedMin().sge(Other.getSignedMax());
+  default:
+    llvm_unreachable("Invalid ICmp predicate");
+  }
 }
 
 /// Exact mul nuw region for single element RHS.


        


More information about the llvm-commits mailing list