[llvm] 9b75467 - [ValueLattice] Add asConstantRange() helper (NFC)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 8 01:50:55 PDT 2024


Author: Nikita Popov
Date: 2024-07-08T10:49:15+02:00
New Revision: 9b754675d3e0861039cf87cf4a0488ecc121e530

URL: https://github.com/llvm/llvm-project/commit/9b754675d3e0861039cf87cf4a0488ecc121e530
DIFF: https://github.com/llvm/llvm-project/commit/9b754675d3e0861039cf87cf4a0488ecc121e530.diff

LOG: [ValueLattice] Add asConstantRange() helper (NFC)

Move the toConstantRange() helper from LVI into ValueLattice,
so it can be reused in other places like SCCP.

Added: 
    

Modified: 
    llvm/include/llvm/Analysis/ValueLattice.h
    llvm/lib/Analysis/LazyValueInfo.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Analysis/ValueLattice.h b/llvm/include/llvm/Analysis/ValueLattice.h
index 2898cdd3d7b0c..c0a875ed7efa3 100644
--- a/llvm/include/llvm/Analysis/ValueLattice.h
+++ b/llvm/include/llvm/Analysis/ValueLattice.h
@@ -281,6 +281,18 @@ class ValueLatticeElement {
     return std::nullopt;
   }
 
+  ConstantRange asConstantRange(Type *Ty, bool UndefAllowed = false) {
+    assert(Ty->isIntOrIntVectorTy() && "Must be integer type");
+    if (isConstantRange(UndefAllowed))
+      return getConstantRange();
+    if (isConstant())
+      return getConstant()->toConstantRange();
+    unsigned BW = Ty->getScalarSizeInBits();
+    if (isUnknown())
+      return ConstantRange::getEmpty(BW);
+    return ConstantRange::getFull(BW);
+  }
+
   bool markOverdefined() {
     if (isOverdefined())
       return false;

diff  --git a/llvm/lib/Analysis/LazyValueInfo.cpp b/llvm/lib/Analysis/LazyValueInfo.cpp
index 877898f6daeef..92389f2896b8e 100644
--- a/llvm/lib/Analysis/LazyValueInfo.cpp
+++ b/llvm/lib/Analysis/LazyValueInfo.cpp
@@ -836,19 +836,6 @@ void LazyValueInfoImpl::intersectAssumeOrGuardBlockValueConstantRange(
   }
 }
 
-static ConstantRange toConstantRange(const ValueLatticeElement &Val,
-                                     Type *Ty, bool UndefAllowed = false) {
-  assert(Ty->isIntOrIntVectorTy() && "Must be integer type");
-  if (Val.isConstantRange(UndefAllowed))
-    return Val.getConstantRange();
-  unsigned BW = Ty->getScalarSizeInBits();
-  if (Val.isUnknown())
-    return ConstantRange::getEmpty(BW);
-  if (Val.isConstant())
-    return Val.getConstant()->toConstantRange();
-  return ConstantRange::getFull(BW);
-}
-
 std::optional<ValueLatticeElement>
 LazyValueInfoImpl::solveBlockValueSelect(SelectInst *SI, BasicBlock *BB) {
   // Recurse on our inputs if needed
@@ -865,8 +852,8 @@ LazyValueInfoImpl::solveBlockValueSelect(SelectInst *SI, BasicBlock *BB) {
   ValueLatticeElement &FalseVal = *OptFalseVal;
 
   if (TrueVal.isConstantRange() || FalseVal.isConstantRange()) {
-    const ConstantRange &TrueCR = toConstantRange(TrueVal, SI->getType());
-    const ConstantRange &FalseCR = toConstantRange(FalseVal, SI->getType());
+    const ConstantRange &TrueCR = TrueVal.asConstantRange(SI->getType());
+    const ConstantRange &FalseCR = FalseVal.asConstantRange(SI->getType());
     Value *LHS = nullptr;
     Value *RHS = nullptr;
     SelectPatternResult SPR = matchSelectPattern(SI, LHS, RHS);
@@ -941,7 +928,7 @@ LazyValueInfoImpl::getRangeFor(Value *V, Instruction *CxtI, BasicBlock *BB) {
   std::optional<ValueLatticeElement> OptVal = getBlockValue(V, BB, CxtI);
   if (!OptVal)
     return std::nullopt;
-  return toConstantRange(*OptVal, V->getType());
+  return OptVal->asConstantRange(V->getType());
 }
 
 std::optional<ValueLatticeElement>
@@ -1119,7 +1106,7 @@ LazyValueInfoImpl::getValueFromSimpleICmpCondition(CmpInst::Predicate Pred,
         getBlockValue(RHS, CxtI->getParent(), CxtI);
     if (!R)
       return std::nullopt;
-    RHSRange = toConstantRange(*R, RHS->getType());
+    RHSRange = R->asConstantRange(RHS->getType());
   }
 
   ConstantRange TrueValues =
@@ -1734,7 +1721,7 @@ ConstantRange LazyValueInfo::getConstantRange(Value *V, Instruction *CxtI,
   BasicBlock *BB = CxtI->getParent();
   ValueLatticeElement Result =
       getOrCreateImpl(BB->getModule()).getValueInBlock(V, BB, CxtI);
-  return toConstantRange(Result, V->getType(), UndefAllowed);
+  return Result.asConstantRange(V->getType(), UndefAllowed);
 }
 
 ConstantRange LazyValueInfo::getConstantRangeAtUse(const Use &U,
@@ -1742,7 +1729,7 @@ ConstantRange LazyValueInfo::getConstantRangeAtUse(const Use &U,
   auto *Inst = cast<Instruction>(U.getUser());
   ValueLatticeElement Result =
       getOrCreateImpl(Inst->getModule()).getValueAtUse(U);
-  return toConstantRange(Result, U->getType(), UndefAllowed);
+  return Result.asConstantRange(U->getType(), UndefAllowed);
 }
 
 /// Determine whether the specified value is known to be a
@@ -1772,7 +1759,7 @@ ConstantRange LazyValueInfo::getConstantRangeOnEdge(Value *V,
   ValueLatticeElement Result =
       getOrCreateImpl(M).getValueOnEdge(V, FromBB, ToBB, CxtI);
   // TODO: Should undef be allowed here?
-  return toConstantRange(Result, V->getType(), /*UndefAllowed*/ true);
+  return Result.asConstantRange(V->getType(), /*UndefAllowed*/ true);
 }
 
 static Constant *getPredicateResult(CmpInst::Predicate Pred, Constant *C,


        


More information about the llvm-commits mailing list