summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Analysis
diff options
context:
space:
mode:
authorSanjoy Das <sanjoy@playingwithpointers.com>2015-10-24 05:37:35 +0000
committerSanjoy Das <sanjoy@playingwithpointers.com>2015-10-24 05:37:35 +0000
commita7e13782f1a6cbc234536e3c642dd6af0d33c76c (patch)
tree0028c79766de1566f49e54e5b97696cc9b350435 /llvm/lib/Analysis
parentbb5ffc50b7bf3ba75d16dd97b91c166f403bb52a (diff)
downloadbcm5719-llvm-a7e13782f1a6cbc234536e3c642dd6af0d33c76c.tar.gz
bcm5719-llvm-a7e13782f1a6cbc234536e3c642dd6af0d33c76c.zip
Extract out getConstantRangeFromMetadata; NFC
The loop idiom creating a ConstantRange is repeated twice in the codebase, time to give it a name and a home. The loop is also repeated in `rangeMetadataExcludesValue`, but using `getConstantRangeFromMetadata` there would not be an NFC -- the range returned by `getConstantRangeFromMetadata` may contain a value that none of the subranges did. llvm-svn: 251180
Diffstat (limited to 'llvm/lib/Analysis')
-rw-r--r--llvm/lib/Analysis/InstructionSimplify.cpp36
-rw-r--r--llvm/lib/Analysis/ScalarEvolution.cpp23
-rw-r--r--llvm/lib/Analysis/ValueTracking.cpp22
3 files changed, 30 insertions, 51 deletions
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index ec50b8a05f4..13812b1175f 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -2176,29 +2176,6 @@ static bool implies(Value *A, Value *B) {
return false;
}
-static ConstantRange GetConstantRangeFromMetadata(MDNode *Ranges, uint32_t BitWidth) {
- const unsigned NumRanges = Ranges->getNumOperands() / 2;
- assert(NumRanges >= 1);
-
- ConstantRange CR(BitWidth, false);
- for (unsigned i = 0; i < NumRanges; ++i) {
- auto *Low =
- mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
- auto *High =
- mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
-
- // Union will merge two ranges to one and potentially introduce a range
- // not covered by the original two ranges. For example, [1, 5) and [8, 10)
- // will become [1, 10). In this case, we can not fold comparison between
- // constant 6 and a value of the above ranges. In practice, most values
- // have only one range, so it might not be worth handling this by
- // introducing additional complexity.
- CR = CR.unionWith(ConstantRange(Low->getValue(), High->getValue()));
- }
-
- return CR;
-}
-
/// SimplifyICmpInst - Given operands for an ICmpInst, see if we can
/// fold the result. If not, this returns null.
static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
@@ -2447,8 +2424,7 @@ static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
if (auto *I = dyn_cast<Instruction>(LHS))
if (auto *Ranges = I->getMetadata(LLVMContext::MD_range))
- LHS_CR =
- LHS_CR.intersectWith(GetConstantRangeFromMetadata(Ranges, Width));
+ LHS_CR = LHS_CR.intersectWith(getConstantRangeFromMetadata(*Ranges));
if (!LHS_CR.isFullSet()) {
if (RHS_CR.contains(LHS_CR))
@@ -2466,12 +2442,10 @@ static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
if (RHS_Instr->getMetadata(LLVMContext::MD_range) &&
LHS_Instr->getMetadata(LLVMContext::MD_range)) {
- uint32_t BitWidth = Q.DL.getTypeSizeInBits(RHS->getType());
-
- auto RHS_CR = GetConstantRangeFromMetadata(
- RHS_Instr->getMetadata(LLVMContext::MD_range), BitWidth);
- auto LHS_CR = GetConstantRangeFromMetadata(
- LHS_Instr->getMetadata(LLVMContext::MD_range), BitWidth);
+ auto RHS_CR = getConstantRangeFromMetadata(
+ *RHS_Instr->getMetadata(LLVMContext::MD_range));
+ auto LHS_CR = getConstantRangeFromMetadata(
+ *LHS_Instr->getMetadata(LLVMContext::MD_range));
auto Satisfied_CR = ConstantRange::makeSatisfyingICmpRegion(Pred, RHS_CR);
if (Satisfied_CR.contains(LHS_CR))
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 4f0ef2a9595..007fb65d69e 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -4133,26 +4133,9 @@ ScalarEvolution::GetMinTrailingZeros(const SCEV *S) {
/// GetRangeFromMetadata - Helper method to assign a range to V from
/// metadata present in the IR.
static Optional<ConstantRange> GetRangeFromMetadata(Value *V) {
- if (Instruction *I = dyn_cast<Instruction>(V)) {
- if (MDNode *MD = I->getMetadata(LLVMContext::MD_range)) {
- ConstantRange TotalRange(
- cast<IntegerType>(I->getType())->getBitWidth(), false);
-
- unsigned NumRanges = MD->getNumOperands() / 2;
- assert(NumRanges >= 1);
-
- for (unsigned i = 0; i < NumRanges; ++i) {
- ConstantInt *Lower =
- mdconst::extract<ConstantInt>(MD->getOperand(2 * i + 0));
- ConstantInt *Upper =
- mdconst::extract<ConstantInt>(MD->getOperand(2 * i + 1));
- ConstantRange Range(Lower->getValue(), Upper->getValue());
- TotalRange = TotalRange.unionWith(Range);
- }
-
- return TotalRange;
- }
- }
+ if (Instruction *I = dyn_cast<Instruction>(V))
+ if (MDNode *MD = I->getMetadata(LLVMContext::MD_range))
+ return getConstantRangeFromMetadata(*MD);
return None;
}
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index ea2321e832e..0fe87176f88 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -4041,3 +4041,25 @@ SelectPatternResult llvm::matchSelectPattern(Value *V,
return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
LHS, RHS);
}
+
+ConstantRange llvm::getConstantRangeFromMetadata(MDNode &Ranges) {
+ const unsigned NumRanges = Ranges.getNumOperands() / 2;
+ assert(NumRanges >= 1 && "Must have at least one range!");
+ assert(Ranges.getNumOperands() % 2 == 0 && "Must be a sequence of pairs");
+
+ auto *FirstLow = mdconst::extract<ConstantInt>(Ranges.getOperand(0));
+ auto *FirstHigh = mdconst::extract<ConstantInt>(Ranges.getOperand(1));
+
+ ConstantRange CR(FirstLow->getValue(), FirstHigh->getValue());
+
+ for (unsigned i = 1; i < NumRanges; ++i) {
+ auto *Low = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
+ auto *High = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
+
+ // Note: unionWith will potentially create a range that contains values not
+ // contained in any of the original N ranges.
+ CR = CR.unionWith(ConstantRange(Low->getValue(), High->getValue()));
+ }
+
+ return CR;
+}
OpenPOWER on IntegriCloud