Skip to content

Commit d177a94

Browse files
committed
[IR] Add Constant::toConstantRange() (NFC)
The logic in llvm::getVectorConstantRange() can be a bit inconvenient to use in some cases because of the need to handle the scalar case separately. Generalize it to handle all constants, and move it to live directly on Constant.
1 parent b0b3c1a commit d177a94

File tree

5 files changed

+47
-45
lines changed

5 files changed

+47
-45
lines changed

llvm/include/llvm/Analysis/ValueTracking.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -904,9 +904,6 @@ bool isOverflowIntrinsicNoWrap(const WithOverflowInst *WO,
904904
/// based on the vscale_range function attribute.
905905
ConstantRange getVScaleRange(const Function *F, unsigned BitWidth);
906906

907-
/// Determine the possible constant range of a vector constant.
908-
ConstantRange getVectorConstantRange(const Constant *C);
909-
910907
/// Determine the possible constant range of an integer or vector of integer
911908
/// value. This is intended as a cheap, non-recursive check.
912909
ConstantRange computeConstantRange(const Value *V, bool ForSigned,

llvm/include/llvm/IR/Constant.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
namespace llvm {
2121

22+
class ConstantRange;
2223
class APInt;
2324

2425
/// This is an important base class in LLVM. It provides the common facilities
@@ -154,6 +155,10 @@ class Constant : public User {
154155
/// vector of constant integers, all equal, and the common value is returned.
155156
const APInt &getUniqueInteger() const;
156157

158+
/// Convert constant to an approximate constant range. For vectors, the
159+
/// range is the union over the element ranges. Poison elements are ignored.
160+
ConstantRange toConstantRange() const;
161+
157162
/// Called if some element of this constant is no longer valid.
158163
/// At this point only other constants may be on the use_list for this
159164
/// constant. Any constants on our Use list must also be destroy'd. The

llvm/lib/Analysis/LazyValueInfo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -844,8 +844,8 @@ static ConstantRange toConstantRange(const ValueLatticeElement &Val,
844844
unsigned BW = Ty->getScalarSizeInBits();
845845
if (Val.isUnknown())
846846
return ConstantRange::getEmpty(BW);
847-
if (Val.isConstant() && Ty->isVectorTy())
848-
return getVectorConstantRange(Val.getConstant());
847+
if (Val.isConstant())
848+
return Val.getConstant()->toConstantRange();
849849
return ConstantRange::getFull(BW);
850850
}
851851

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9513,39 +9513,6 @@ static void setLimitForFPToI(const Instruction *I, APInt &Lower, APInt &Upper) {
95139513
}
95149514
}
95159515

9516-
ConstantRange llvm::getVectorConstantRange(const Constant *C) {
9517-
assert(C->getType()->isVectorTy() && "Expected vector constant");
9518-
if (auto *CI = dyn_cast_or_null<ConstantInt>(
9519-
C->getSplatValue(/*AllowPoison=*/true)))
9520-
return ConstantRange(CI->getValue());
9521-
9522-
unsigned BitWidth = C->getType()->getScalarSizeInBits();
9523-
if (auto *CDV = dyn_cast<ConstantDataVector>(C)) {
9524-
ConstantRange CR = ConstantRange::getEmpty(BitWidth);
9525-
for (unsigned I = 0, E = CDV->getNumElements(); I < E; ++I)
9526-
CR = CR.unionWith(CDV->getElementAsAPInt(I));
9527-
return CR;
9528-
}
9529-
9530-
if (auto *CV = dyn_cast<ConstantVector>(C)) {
9531-
ConstantRange CR = ConstantRange::getEmpty(BitWidth);
9532-
for (unsigned I = 0, E = CV->getNumOperands(); I < E; ++I) {
9533-
Constant *Elem = C->getAggregateElement(I);
9534-
if (!Elem)
9535-
return ConstantRange::getFull(BitWidth);
9536-
if (isa<PoisonValue>(Elem))
9537-
continue;
9538-
auto *CI = dyn_cast<ConstantInt>(Elem);
9539-
if (!CI)
9540-
return ConstantRange::getFull(BitWidth);
9541-
CR = CR.unionWith(CI->getValue());
9542-
}
9543-
return CR;
9544-
}
9545-
9546-
return ConstantRange::getFull(BitWidth);
9547-
}
9548-
95499516
ConstantRange llvm::computeConstantRange(const Value *V, bool ForSigned,
95509517
bool UseInstrInfo, AssumptionCache *AC,
95519518
const Instruction *CtxI,
@@ -9556,13 +9523,8 @@ ConstantRange llvm::computeConstantRange(const Value *V, bool ForSigned,
95569523
if (Depth == MaxAnalysisRecursionDepth)
95579524
return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
95589525

9559-
if (auto *C = dyn_cast<Constant>(V)) {
9560-
if (auto *CI = dyn_cast<ConstantInt>(C))
9561-
return ConstantRange(CI->getValue());
9562-
if (C->getType()->isVectorTy())
9563-
return getVectorConstantRange(C);
9564-
return ConstantRange::getFull(C->getType()->getScalarSizeInBits());
9565-
}
9526+
if (auto *C = dyn_cast<Constant>(V))
9527+
return C->toConstantRange();
95669528

95679529
unsigned BitWidth = V->getType()->getScalarSizeInBits();
95689530
InstrInfoQuery IIQ(UseInstrInfo);

llvm/lib/IR/Constants.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,6 +1756,44 @@ const APInt &Constant::getUniqueInteger() const {
17561756
return cast<ConstantInt>(C)->getValue();
17571757
}
17581758

1759+
ConstantRange Constant::toConstantRange() const {
1760+
if (auto *CI = dyn_cast<ConstantInt>(this))
1761+
return ConstantRange(CI->getValue());
1762+
1763+
unsigned BitWidth = getType()->getScalarSizeInBits();
1764+
if (!getType()->isVectorTy())
1765+
return ConstantRange::getFull(BitWidth);
1766+
1767+
if (auto *CI = dyn_cast_or_null<ConstantInt>(
1768+
getSplatValue(/*AllowPoison=*/true)))
1769+
return ConstantRange(CI->getValue());
1770+
1771+
if (auto *CDV = dyn_cast<ConstantDataVector>(this)) {
1772+
ConstantRange CR = ConstantRange::getEmpty(BitWidth);
1773+
for (unsigned I = 0, E = CDV->getNumElements(); I < E; ++I)
1774+
CR = CR.unionWith(CDV->getElementAsAPInt(I));
1775+
return CR;
1776+
}
1777+
1778+
if (auto *CV = dyn_cast<ConstantVector>(this)) {
1779+
ConstantRange CR = ConstantRange::getEmpty(BitWidth);
1780+
for (unsigned I = 0, E = CV->getNumOperands(); I < E; ++I) {
1781+
Constant *Elem = CV->getOperand(I);
1782+
if (!Elem)
1783+
return ConstantRange::getFull(BitWidth);
1784+
if (isa<PoisonValue>(Elem))
1785+
continue;
1786+
auto *CI = dyn_cast<ConstantInt>(Elem);
1787+
if (!CI)
1788+
return ConstantRange::getFull(BitWidth);
1789+
CR = CR.unionWith(CI->getValue());
1790+
}
1791+
return CR;
1792+
}
1793+
1794+
return ConstantRange::getFull(BitWidth);
1795+
}
1796+
17591797
//---- ConstantPointerNull::get() implementation.
17601798
//
17611799

0 commit comments

Comments
 (0)