Skip to content

Commit a7e1378

Browse files
committedOct 24, 2015
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
1 parent bb5ffc5 commit a7e1378

File tree

4 files changed

+36
-51
lines changed

4 files changed

+36
-51
lines changed
 

‎llvm/include/llvm/Analysis/ValueTracking.h

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define LLVM_ANALYSIS_VALUETRACKING_H
1717

1818
#include "llvm/ADT/ArrayRef.h"
19+
#include "llvm/IR/ConstantRange.h"
1920
#include "llvm/IR/Instruction.h"
2021
#include "llvm/Support/DataTypes.h"
2122

@@ -429,6 +430,11 @@ namespace llvm {
429430
SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS,
430431
Instruction::CastOps *CastOp = nullptr);
431432

433+
/// Parse out a conservative ConstantRange from !range metadata.
434+
///
435+
/// E.g. if RangeMD is !{i32 0, i32 10, i32 15, i32 20} then return [0, 20).
436+
ConstantRange getConstantRangeFromMetadata(MDNode &RangeMD);
437+
432438
} // end namespace llvm
433439

434440
#endif

‎llvm/lib/Analysis/InstructionSimplify.cpp

+5-31
Original file line numberDiff line numberDiff line change
@@ -2176,29 +2176,6 @@ static bool implies(Value *A, Value *B) {
21762176
return false;
21772177
}
21782178

2179-
static ConstantRange GetConstantRangeFromMetadata(MDNode *Ranges, uint32_t BitWidth) {
2180-
const unsigned NumRanges = Ranges->getNumOperands() / 2;
2181-
assert(NumRanges >= 1);
2182-
2183-
ConstantRange CR(BitWidth, false);
2184-
for (unsigned i = 0; i < NumRanges; ++i) {
2185-
auto *Low =
2186-
mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
2187-
auto *High =
2188-
mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
2189-
2190-
// Union will merge two ranges to one and potentially introduce a range
2191-
// not covered by the original two ranges. For example, [1, 5) and [8, 10)
2192-
// will become [1, 10). In this case, we can not fold comparison between
2193-
// constant 6 and a value of the above ranges. In practice, most values
2194-
// have only one range, so it might not be worth handling this by
2195-
// introducing additional complexity.
2196-
CR = CR.unionWith(ConstantRange(Low->getValue(), High->getValue()));
2197-
}
2198-
2199-
return CR;
2200-
}
2201-
22022179
/// SimplifyICmpInst - Given operands for an ICmpInst, see if we can
22032180
/// fold the result. If not, this returns null.
22042181
static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
@@ -2447,8 +2424,7 @@ static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
24472424

24482425
if (auto *I = dyn_cast<Instruction>(LHS))
24492426
if (auto *Ranges = I->getMetadata(LLVMContext::MD_range))
2450-
LHS_CR =
2451-
LHS_CR.intersectWith(GetConstantRangeFromMetadata(Ranges, Width));
2427+
LHS_CR = LHS_CR.intersectWith(getConstantRangeFromMetadata(*Ranges));
24522428

24532429
if (!LHS_CR.isFullSet()) {
24542430
if (RHS_CR.contains(LHS_CR))
@@ -2466,12 +2442,10 @@ static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
24662442

24672443
if (RHS_Instr->getMetadata(LLVMContext::MD_range) &&
24682444
LHS_Instr->getMetadata(LLVMContext::MD_range)) {
2469-
uint32_t BitWidth = Q.DL.getTypeSizeInBits(RHS->getType());
2470-
2471-
auto RHS_CR = GetConstantRangeFromMetadata(
2472-
RHS_Instr->getMetadata(LLVMContext::MD_range), BitWidth);
2473-
auto LHS_CR = GetConstantRangeFromMetadata(
2474-
LHS_Instr->getMetadata(LLVMContext::MD_range), BitWidth);
2445+
auto RHS_CR = getConstantRangeFromMetadata(
2446+
*RHS_Instr->getMetadata(LLVMContext::MD_range));
2447+
auto LHS_CR = getConstantRangeFromMetadata(
2448+
*LHS_Instr->getMetadata(LLVMContext::MD_range));
24752449

24762450
auto Satisfied_CR = ConstantRange::makeSatisfyingICmpRegion(Pred, RHS_CR);
24772451
if (Satisfied_CR.contains(LHS_CR))

‎llvm/lib/Analysis/ScalarEvolution.cpp

+3-20
Original file line numberDiff line numberDiff line change
@@ -4133,26 +4133,9 @@ ScalarEvolution::GetMinTrailingZeros(const SCEV *S) {
41334133
/// GetRangeFromMetadata - Helper method to assign a range to V from
41344134
/// metadata present in the IR.
41354135
static Optional<ConstantRange> GetRangeFromMetadata(Value *V) {
4136-
if (Instruction *I = dyn_cast<Instruction>(V)) {
4137-
if (MDNode *MD = I->getMetadata(LLVMContext::MD_range)) {
4138-
ConstantRange TotalRange(
4139-
cast<IntegerType>(I->getType())->getBitWidth(), false);
4140-
4141-
unsigned NumRanges = MD->getNumOperands() / 2;
4142-
assert(NumRanges >= 1);
4143-
4144-
for (unsigned i = 0; i < NumRanges; ++i) {
4145-
ConstantInt *Lower =
4146-
mdconst::extract<ConstantInt>(MD->getOperand(2 * i + 0));
4147-
ConstantInt *Upper =
4148-
mdconst::extract<ConstantInt>(MD->getOperand(2 * i + 1));
4149-
ConstantRange Range(Lower->getValue(), Upper->getValue());
4150-
TotalRange = TotalRange.unionWith(Range);
4151-
}
4152-
4153-
return TotalRange;
4154-
}
4155-
}
4136+
if (Instruction *I = dyn_cast<Instruction>(V))
4137+
if (MDNode *MD = I->getMetadata(LLVMContext::MD_range))
4138+
return getConstantRangeFromMetadata(*MD);
41564139

41574140
return None;
41584141
}

‎llvm/lib/Analysis/ValueTracking.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -4041,3 +4041,25 @@ SelectPatternResult llvm::matchSelectPattern(Value *V,
40414041
return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
40424042
LHS, RHS);
40434043
}
4044+
4045+
ConstantRange llvm::getConstantRangeFromMetadata(MDNode &Ranges) {
4046+
const unsigned NumRanges = Ranges.getNumOperands() / 2;
4047+
assert(NumRanges >= 1 && "Must have at least one range!");
4048+
assert(Ranges.getNumOperands() % 2 == 0 && "Must be a sequence of pairs");
4049+
4050+
auto *FirstLow = mdconst::extract<ConstantInt>(Ranges.getOperand(0));
4051+
auto *FirstHigh = mdconst::extract<ConstantInt>(Ranges.getOperand(1));
4052+
4053+
ConstantRange CR(FirstLow->getValue(), FirstHigh->getValue());
4054+
4055+
for (unsigned i = 1; i < NumRanges; ++i) {
4056+
auto *Low = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
4057+
auto *High = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
4058+
4059+
// Note: unionWith will potentially create a range that contains values not
4060+
// contained in any of the original N ranges.
4061+
CR = CR.unionWith(ConstantRange(Low->getValue(), High->getValue()));
4062+
}
4063+
4064+
return CR;
4065+
}

0 commit comments

Comments
 (0)