From 5dacfa466d3a3b3c8deddb31969381600fb5c320 Mon Sep 17 00:00:00 2001 From: Sanjoy Das Date: Sat, 24 Oct 2015 05:37:35 +0000 Subject: [PATCH] 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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@251180 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Analysis/ValueTracking.h | 6 +++++ lib/Analysis/InstructionSimplify.cpp | 36 ++++----------------------- lib/Analysis/ScalarEvolution.cpp | 23 +++-------------- lib/Analysis/ValueTracking.cpp | 22 ++++++++++++++++ 4 files changed, 36 insertions(+), 51 deletions(-) diff --git a/include/llvm/Analysis/ValueTracking.h b/include/llvm/Analysis/ValueTracking.h index 90c8b1e7bc9..6216c4ddfd8 100644 --- a/include/llvm/Analysis/ValueTracking.h +++ b/include/llvm/Analysis/ValueTracking.h @@ -16,6 +16,7 @@ #define LLVM_ANALYSIS_VALUETRACKING_H #include "llvm/ADT/ArrayRef.h" +#include "llvm/IR/ConstantRange.h" #include "llvm/IR/Instruction.h" #include "llvm/Support/DataTypes.h" @@ -429,6 +430,11 @@ namespace llvm { SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp = nullptr); + /// Parse out a conservative ConstantRange from !range metadata. + /// + /// E.g. if RangeMD is !{i32 0, i32 10, i32 15, i32 20} then return [0, 20). + ConstantRange getConstantRangeFromMetadata(MDNode &RangeMD); + } // end namespace llvm #endif diff --git a/lib/Analysis/InstructionSimplify.cpp b/lib/Analysis/InstructionSimplify.cpp index ec50b8a05f4..13812b1175f 100644 --- a/lib/Analysis/InstructionSimplify.cpp +++ b/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(Ranges->getOperand(2 * i + 0)); - auto *High = - mdconst::extract(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(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/lib/Analysis/ScalarEvolution.cpp b/lib/Analysis/ScalarEvolution.cpp index 4f0ef2a9595..007fb65d69e 100644 --- a/lib/Analysis/ScalarEvolution.cpp +++ b/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 GetRangeFromMetadata(Value *V) { - if (Instruction *I = dyn_cast(V)) { - if (MDNode *MD = I->getMetadata(LLVMContext::MD_range)) { - ConstantRange TotalRange( - cast(I->getType())->getBitWidth(), false); - - unsigned NumRanges = MD->getNumOperands() / 2; - assert(NumRanges >= 1); - - for (unsigned i = 0; i < NumRanges; ++i) { - ConstantInt *Lower = - mdconst::extract(MD->getOperand(2 * i + 0)); - ConstantInt *Upper = - mdconst::extract(MD->getOperand(2 * i + 1)); - ConstantRange Range(Lower->getValue(), Upper->getValue()); - TotalRange = TotalRange.unionWith(Range); - } - - return TotalRange; - } - } + if (Instruction *I = dyn_cast(V)) + if (MDNode *MD = I->getMetadata(LLVMContext::MD_range)) + return getConstantRangeFromMetadata(*MD); return None; } diff --git a/lib/Analysis/ValueTracking.cpp b/lib/Analysis/ValueTracking.cpp index ea2321e832e..0fe87176f88 100644 --- a/lib/Analysis/ValueTracking.cpp +++ b/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(Ranges.getOperand(0)); + auto *FirstHigh = mdconst::extract(Ranges.getOperand(1)); + + ConstantRange CR(FirstLow->getValue(), FirstHigh->getValue()); + + for (unsigned i = 1; i < NumRanges; ++i) { + auto *Low = mdconst::extract(Ranges.getOperand(2 * i + 0)); + auto *High = mdconst::extract(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; +} -- 2.34.1