Skip to content

Commit edcc7fe

Browse files
committed
[LVI] Reuse LatticeValue to ConstantRange conversion more
Use the helper in the getConstantRange() and getConstantRangeAtUse() APIs as well. For that purpose move the handling of isUnknown() into the helper as well.
1 parent 17b8f87 commit edcc7fe

File tree

1 file changed

+13
-30
lines changed

1 file changed

+13
-30
lines changed

llvm/lib/Analysis/LazyValueInfo.cpp

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -801,12 +801,15 @@ void LazyValueInfoImpl::intersectAssumeOrGuardBlockValueConstantRange(
801801
}
802802
}
803803

804-
static ConstantRange getConstantRangeOrFull(const ValueLatticeElement &Val,
805-
Type *Ty) {
804+
static ConstantRange toConstantRange(const ValueLatticeElement &Val,
805+
Type *Ty, bool UndefAllowed = false) {
806806
assert(Ty->isIntOrIntVectorTy() && "Must be integer type");
807-
if (Val.isConstantRange(/*UndefAllowed*/ false))
807+
if (Val.isConstantRange(UndefAllowed))
808808
return Val.getConstantRange();
809-
return ConstantRange::getFull(Ty->getScalarSizeInBits());
809+
unsigned BW = Ty->getScalarSizeInBits();
810+
if (Val.isUnknown())
811+
return ConstantRange::getEmpty(BW);
812+
return ConstantRange::getFull(BW);
810813
}
811814

812815
std::optional<ValueLatticeElement>
@@ -825,10 +828,8 @@ LazyValueInfoImpl::solveBlockValueSelect(SelectInst *SI, BasicBlock *BB) {
825828
ValueLatticeElement &FalseVal = *OptFalseVal;
826829

827830
if (TrueVal.isConstantRange() || FalseVal.isConstantRange()) {
828-
const ConstantRange &TrueCR =
829-
getConstantRangeOrFull(TrueVal, SI->getType());
830-
const ConstantRange &FalseCR =
831-
getConstantRangeOrFull(FalseVal, SI->getType());
831+
const ConstantRange &TrueCR = toConstantRange(TrueVal, SI->getType());
832+
const ConstantRange &FalseCR = toConstantRange(FalseVal, SI->getType());
832833
Value *LHS = nullptr;
833834
Value *RHS = nullptr;
834835
SelectPatternResult SPR = matchSelectPattern(SI, LHS, RHS);
@@ -899,7 +900,7 @@ LazyValueInfoImpl::getRangeFor(Value *V, Instruction *CxtI, BasicBlock *BB) {
899900
std::optional<ValueLatticeElement> OptVal = getBlockValue(V, BB, CxtI);
900901
if (!OptVal)
901902
return std::nullopt;
902-
return getConstantRangeOrFull(*OptVal, V->getType());
903+
return toConstantRange(*OptVal, V->getType());
903904
}
904905

905906
std::optional<ValueLatticeElement>
@@ -1624,19 +1625,10 @@ Constant *LazyValueInfo::getConstant(Value *V, Instruction *CxtI) {
16241625
ConstantRange LazyValueInfo::getConstantRange(Value *V, Instruction *CxtI,
16251626
bool UndefAllowed) {
16261627
assert(V->getType()->isIntegerTy());
1627-
unsigned Width = V->getType()->getIntegerBitWidth();
16281628
BasicBlock *BB = CxtI->getParent();
16291629
ValueLatticeElement Result =
16301630
getOrCreateImpl(BB->getModule()).getValueInBlock(V, BB, CxtI);
1631-
if (Result.isUnknown())
1632-
return ConstantRange::getEmpty(Width);
1633-
if (Result.isConstantRange(UndefAllowed))
1634-
return Result.getConstantRange(UndefAllowed);
1635-
// We represent ConstantInt constants as constant ranges but other kinds
1636-
// of integer constants, i.e. ConstantExpr will be tagged as constants
1637-
assert(!(Result.isConstant() && isa<ConstantInt>(Result.getConstant())) &&
1638-
"ConstantInt value must be represented as constantrange");
1639-
return ConstantRange::getFull(Width);
1631+
return toConstantRange(Result, V->getType(), UndefAllowed);
16401632
}
16411633

16421634
ConstantRange LazyValueInfo::getConstantRangeAtUse(const Use &U,
@@ -1709,20 +1701,11 @@ ConstantRange LazyValueInfo::getConstantRangeOnEdge(Value *V,
17091701
BasicBlock *FromBB,
17101702
BasicBlock *ToBB,
17111703
Instruction *CxtI) {
1712-
unsigned Width = V->getType()->getIntegerBitWidth();
17131704
Module *M = FromBB->getModule();
17141705
ValueLatticeElement Result =
17151706
getOrCreateImpl(M).getValueOnEdge(V, FromBB, ToBB, CxtI);
1716-
1717-
if (Result.isUnknown())
1718-
return ConstantRange::getEmpty(Width);
1719-
if (Result.isConstantRange())
1720-
return Result.getConstantRange();
1721-
// We represent ConstantInt constants as constant ranges but other kinds
1722-
// of integer constants, i.e. ConstantExpr will be tagged as constants
1723-
assert(!(Result.isConstant() && isa<ConstantInt>(Result.getConstant())) &&
1724-
"ConstantInt value must be represented as constantrange");
1725-
return ConstantRange::getFull(Width);
1707+
// TODO: Should undef be allowed here?
1708+
return toConstantRange(Result, V->getType(), /*UndefAllowed*/ true);
17261709
}
17271710

17281711
static LazyValueInfo::Tristate

0 commit comments

Comments
 (0)