[llvm] d177a94 - [IR] Add Constant::toConstantRange() (NFC)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Fri Jul 5 07:51:59 PDT 2024
Author: Nikita Popov
Date: 2024-07-05T16:51:49+02:00
New Revision: d177a94fbdc12c82e06dc1c2dc7500c3ce399291
URL: https://github.com/llvm/llvm-project/commit/d177a94fbdc12c82e06dc1c2dc7500c3ce399291
DIFF: https://github.com/llvm/llvm-project/commit/d177a94fbdc12c82e06dc1c2dc7500c3ce399291.diff
LOG: [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.
Added:
Modified:
llvm/include/llvm/Analysis/ValueTracking.h
llvm/include/llvm/IR/Constant.h
llvm/lib/Analysis/LazyValueInfo.cpp
llvm/lib/Analysis/ValueTracking.cpp
llvm/lib/IR/Constants.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Analysis/ValueTracking.h b/llvm/include/llvm/Analysis/ValueTracking.h
index a67ad501982d2..b7b78cb9edab3 100644
--- a/llvm/include/llvm/Analysis/ValueTracking.h
+++ b/llvm/include/llvm/Analysis/ValueTracking.h
@@ -904,9 +904,6 @@ bool isOverflowIntrinsicNoWrap(const WithOverflowInst *WO,
/// based on the vscale_range function attribute.
ConstantRange getVScaleRange(const Function *F, unsigned BitWidth);
-/// Determine the possible constant range of a vector constant.
-ConstantRange getVectorConstantRange(const Constant *C);
-
/// Determine the possible constant range of an integer or vector of integer
/// value. This is intended as a cheap, non-recursive check.
ConstantRange computeConstantRange(const Value *V, bool ForSigned,
diff --git a/llvm/include/llvm/IR/Constant.h b/llvm/include/llvm/IR/Constant.h
index d3171acf7b9ac..a82e37b7e2df2 100644
--- a/llvm/include/llvm/IR/Constant.h
+++ b/llvm/include/llvm/IR/Constant.h
@@ -19,6 +19,7 @@
namespace llvm {
+class ConstantRange;
class APInt;
/// This is an important base class in LLVM. It provides the common facilities
@@ -154,6 +155,10 @@ class Constant : public User {
/// vector of constant integers, all equal, and the common value is returned.
const APInt &getUniqueInteger() const;
+ /// Convert constant to an approximate constant range. For vectors, the
+ /// range is the union over the element ranges. Poison elements are ignored.
+ ConstantRange toConstantRange() const;
+
/// Called if some element of this constant is no longer valid.
/// At this point only other constants may be on the use_list for this
/// constant. Any constants on our Use list must also be destroy'd. The
diff --git a/llvm/lib/Analysis/LazyValueInfo.cpp b/llvm/lib/Analysis/LazyValueInfo.cpp
index 674c47ebe786a..27a25377aa86b 100644
--- a/llvm/lib/Analysis/LazyValueInfo.cpp
+++ b/llvm/lib/Analysis/LazyValueInfo.cpp
@@ -844,8 +844,8 @@ static ConstantRange toConstantRange(const ValueLatticeElement &Val,
unsigned BW = Ty->getScalarSizeInBits();
if (Val.isUnknown())
return ConstantRange::getEmpty(BW);
- if (Val.isConstant() && Ty->isVectorTy())
- return getVectorConstantRange(Val.getConstant());
+ if (Val.isConstant())
+ return Val.getConstant()->toConstantRange();
return ConstantRange::getFull(BW);
}
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 85abf00774a02..7be8a18dd7271 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -9513,39 +9513,6 @@ static void setLimitForFPToI(const Instruction *I, APInt &Lower, APInt &Upper) {
}
}
-ConstantRange llvm::getVectorConstantRange(const Constant *C) {
- assert(C->getType()->isVectorTy() && "Expected vector constant");
- if (auto *CI = dyn_cast_or_null<ConstantInt>(
- C->getSplatValue(/*AllowPoison=*/true)))
- return ConstantRange(CI->getValue());
-
- unsigned BitWidth = C->getType()->getScalarSizeInBits();
- if (auto *CDV = dyn_cast<ConstantDataVector>(C)) {
- ConstantRange CR = ConstantRange::getEmpty(BitWidth);
- for (unsigned I = 0, E = CDV->getNumElements(); I < E; ++I)
- CR = CR.unionWith(CDV->getElementAsAPInt(I));
- return CR;
- }
-
- if (auto *CV = dyn_cast<ConstantVector>(C)) {
- ConstantRange CR = ConstantRange::getEmpty(BitWidth);
- for (unsigned I = 0, E = CV->getNumOperands(); I < E; ++I) {
- Constant *Elem = C->getAggregateElement(I);
- if (!Elem)
- return ConstantRange::getFull(BitWidth);
- if (isa<PoisonValue>(Elem))
- continue;
- auto *CI = dyn_cast<ConstantInt>(Elem);
- if (!CI)
- return ConstantRange::getFull(BitWidth);
- CR = CR.unionWith(CI->getValue());
- }
- return CR;
- }
-
- return ConstantRange::getFull(BitWidth);
-}
-
ConstantRange llvm::computeConstantRange(const Value *V, bool ForSigned,
bool UseInstrInfo, AssumptionCache *AC,
const Instruction *CtxI,
@@ -9556,13 +9523,8 @@ ConstantRange llvm::computeConstantRange(const Value *V, bool ForSigned,
if (Depth == MaxAnalysisRecursionDepth)
return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
- if (auto *C = dyn_cast<Constant>(V)) {
- if (auto *CI = dyn_cast<ConstantInt>(C))
- return ConstantRange(CI->getValue());
- if (C->getType()->isVectorTy())
- return getVectorConstantRange(C);
- return ConstantRange::getFull(C->getType()->getScalarSizeInBits());
- }
+ if (auto *C = dyn_cast<Constant>(V))
+ return C->toConstantRange();
unsigned BitWidth = V->getType()->getScalarSizeInBits();
InstrInfoQuery IIQ(UseInstrInfo);
diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp
index bc91f904d7e87..70803c153d8cb 100644
--- a/llvm/lib/IR/Constants.cpp
+++ b/llvm/lib/IR/Constants.cpp
@@ -1756,6 +1756,44 @@ const APInt &Constant::getUniqueInteger() const {
return cast<ConstantInt>(C)->getValue();
}
+ConstantRange Constant::toConstantRange() const {
+ if (auto *CI = dyn_cast<ConstantInt>(this))
+ return ConstantRange(CI->getValue());
+
+ unsigned BitWidth = getType()->getScalarSizeInBits();
+ if (!getType()->isVectorTy())
+ return ConstantRange::getFull(BitWidth);
+
+ if (auto *CI = dyn_cast_or_null<ConstantInt>(
+ getSplatValue(/*AllowPoison=*/true)))
+ return ConstantRange(CI->getValue());
+
+ if (auto *CDV = dyn_cast<ConstantDataVector>(this)) {
+ ConstantRange CR = ConstantRange::getEmpty(BitWidth);
+ for (unsigned I = 0, E = CDV->getNumElements(); I < E; ++I)
+ CR = CR.unionWith(CDV->getElementAsAPInt(I));
+ return CR;
+ }
+
+ if (auto *CV = dyn_cast<ConstantVector>(this)) {
+ ConstantRange CR = ConstantRange::getEmpty(BitWidth);
+ for (unsigned I = 0, E = CV->getNumOperands(); I < E; ++I) {
+ Constant *Elem = CV->getOperand(I);
+ if (!Elem)
+ return ConstantRange::getFull(BitWidth);
+ if (isa<PoisonValue>(Elem))
+ continue;
+ auto *CI = dyn_cast<ConstantInt>(Elem);
+ if (!CI)
+ return ConstantRange::getFull(BitWidth);
+ CR = CR.unionWith(CI->getValue());
+ }
+ return CR;
+ }
+
+ return ConstantRange::getFull(BitWidth);
+}
+
//---- ConstantPointerNull::get() implementation.
//
More information about the llvm-commits
mailing list