Skip to content

Commit 9b75467

Browse files
committed
[ValueLattice] Add asConstantRange() helper (NFC)
Move the toConstantRange() helper from LVI into ValueLattice, so it can be reused in other places like SCCP.
1 parent 11f7c89 commit 9b75467

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

llvm/include/llvm/Analysis/ValueLattice.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,18 @@ class ValueLatticeElement {
281281
return std::nullopt;
282282
}
283283

284+
ConstantRange asConstantRange(Type *Ty, bool UndefAllowed = false) {
285+
assert(Ty->isIntOrIntVectorTy() && "Must be integer type");
286+
if (isConstantRange(UndefAllowed))
287+
return getConstantRange();
288+
if (isConstant())
289+
return getConstant()->toConstantRange();
290+
unsigned BW = Ty->getScalarSizeInBits();
291+
if (isUnknown())
292+
return ConstantRange::getEmpty(BW);
293+
return ConstantRange::getFull(BW);
294+
}
295+
284296
bool markOverdefined() {
285297
if (isOverdefined())
286298
return false;

llvm/lib/Analysis/LazyValueInfo.cpp

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -836,19 +836,6 @@ void LazyValueInfoImpl::intersectAssumeOrGuardBlockValueConstantRange(
836836
}
837837
}
838838

839-
static ConstantRange toConstantRange(const ValueLatticeElement &Val,
840-
Type *Ty, bool UndefAllowed = false) {
841-
assert(Ty->isIntOrIntVectorTy() && "Must be integer type");
842-
if (Val.isConstantRange(UndefAllowed))
843-
return Val.getConstantRange();
844-
unsigned BW = Ty->getScalarSizeInBits();
845-
if (Val.isUnknown())
846-
return ConstantRange::getEmpty(BW);
847-
if (Val.isConstant())
848-
return Val.getConstant()->toConstantRange();
849-
return ConstantRange::getFull(BW);
850-
}
851-
852839
std::optional<ValueLatticeElement>
853840
LazyValueInfoImpl::solveBlockValueSelect(SelectInst *SI, BasicBlock *BB) {
854841
// Recurse on our inputs if needed
@@ -865,8 +852,8 @@ LazyValueInfoImpl::solveBlockValueSelect(SelectInst *SI, BasicBlock *BB) {
865852
ValueLatticeElement &FalseVal = *OptFalseVal;
866853

867854
if (TrueVal.isConstantRange() || FalseVal.isConstantRange()) {
868-
const ConstantRange &TrueCR = toConstantRange(TrueVal, SI->getType());
869-
const ConstantRange &FalseCR = toConstantRange(FalseVal, SI->getType());
855+
const ConstantRange &TrueCR = TrueVal.asConstantRange(SI->getType());
856+
const ConstantRange &FalseCR = FalseVal.asConstantRange(SI->getType());
870857
Value *LHS = nullptr;
871858
Value *RHS = nullptr;
872859
SelectPatternResult SPR = matchSelectPattern(SI, LHS, RHS);
@@ -941,7 +928,7 @@ LazyValueInfoImpl::getRangeFor(Value *V, Instruction *CxtI, BasicBlock *BB) {
941928
std::optional<ValueLatticeElement> OptVal = getBlockValue(V, BB, CxtI);
942929
if (!OptVal)
943930
return std::nullopt;
944-
return toConstantRange(*OptVal, V->getType());
931+
return OptVal->asConstantRange(V->getType());
945932
}
946933

947934
std::optional<ValueLatticeElement>
@@ -1119,7 +1106,7 @@ LazyValueInfoImpl::getValueFromSimpleICmpCondition(CmpInst::Predicate Pred,
11191106
getBlockValue(RHS, CxtI->getParent(), CxtI);
11201107
if (!R)
11211108
return std::nullopt;
1122-
RHSRange = toConstantRange(*R, RHS->getType());
1109+
RHSRange = R->asConstantRange(RHS->getType());
11231110
}
11241111

11251112
ConstantRange TrueValues =
@@ -1734,15 +1721,15 @@ ConstantRange LazyValueInfo::getConstantRange(Value *V, Instruction *CxtI,
17341721
BasicBlock *BB = CxtI->getParent();
17351722
ValueLatticeElement Result =
17361723
getOrCreateImpl(BB->getModule()).getValueInBlock(V, BB, CxtI);
1737-
return toConstantRange(Result, V->getType(), UndefAllowed);
1724+
return Result.asConstantRange(V->getType(), UndefAllowed);
17381725
}
17391726

17401727
ConstantRange LazyValueInfo::getConstantRangeAtUse(const Use &U,
17411728
bool UndefAllowed) {
17421729
auto *Inst = cast<Instruction>(U.getUser());
17431730
ValueLatticeElement Result =
17441731
getOrCreateImpl(Inst->getModule()).getValueAtUse(U);
1745-
return toConstantRange(Result, U->getType(), UndefAllowed);
1732+
return Result.asConstantRange(U->getType(), UndefAllowed);
17461733
}
17471734

17481735
/// Determine whether the specified value is known to be a
@@ -1772,7 +1759,7 @@ ConstantRange LazyValueInfo::getConstantRangeOnEdge(Value *V,
17721759
ValueLatticeElement Result =
17731760
getOrCreateImpl(M).getValueOnEdge(V, FromBB, ToBB, CxtI);
17741761
// TODO: Should undef be allowed here?
1775-
return toConstantRange(Result, V->getType(), /*UndefAllowed*/ true);
1762+
return Result.asConstantRange(V->getType(), /*UndefAllowed*/ true);
17761763
}
17771764

17781765
static Constant *getPredicateResult(CmpInst::Predicate Pred, Constant *C,

0 commit comments

Comments
 (0)