Skip to content

Commit

Permalink
[ConstantRange] Add getFull() + getEmpty() named constructors; NFC
Browse files Browse the repository at this point in the history
This adds ConstantRange::getFull(BitWidth) and
ConstantRange::getEmpty(BitWidth) named constructors as more readable
alternatives to the current ConstantRange(BitWidth, /* full */ false)
and similar. Additionally private getFull() and getEmpty() member
functions are added which return a full/empty range with the same bit
width -- these are commonly needed inside ConstantRange.cpp.

The IsFullSet argument in the ConstantRange(BitWidth, IsFullSet)
constructor is now mandatory for the few usages that still make use of it.

Differential Revision: https://reviews.llvm.org/D59716

llvm-svn: 356852
  • Loading branch information
nikic committed Mar 24, 2019
1 parent 54ce1b1 commit 977934f
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 85 deletions.
22 changes: 21 additions & 1 deletion llvm/include/llvm/IR/ConstantRange.h
Expand Up @@ -47,9 +47,19 @@ struct KnownBits;
class LLVM_NODISCARD ConstantRange {
APInt Lower, Upper;

/// Create empty constant range with same bitwidth.
ConstantRange getEmpty() const {
return ConstantRange(getBitWidth(), false);
}

/// Create full constant range with same bitwidth.
ConstantRange getFull() const {
return ConstantRange(getBitWidth(), true);
}

public:
/// Initialize a full (the default) or empty set for the specified bit width.
explicit ConstantRange(uint32_t BitWidth, bool isFullSet = true);
explicit ConstantRange(uint32_t BitWidth, bool isFullSet);

/// Initialize a range to hold the single specified value.
ConstantRange(APInt Value);
Expand All @@ -59,6 +69,16 @@ class LLVM_NODISCARD ConstantRange {
/// assert out if the two APInt's are not the same bit width.
ConstantRange(APInt Lower, APInt Upper);

/// Create empty constant range with the given bit width.
static ConstantRange getEmpty(uint32_t BitWidth) {
return ConstantRange(BitWidth, false);
}

/// Create full constant range with the given bit width.
static ConstantRange getFull(uint32_t BitWidth) {
return ConstantRange(BitWidth, true);
}

/// Initialize a range based on a known bits constraint. The IsSigned flag
/// indicates whether the constant range should not wrap in the signed or
/// unsigned domain.
Expand Down
10 changes: 5 additions & 5 deletions llvm/lib/Analysis/LazyValueInfo.cpp
Expand Up @@ -963,7 +963,7 @@ Optional<ConstantRange> LazyValueInfoImpl::getRangeForOperand(unsigned Op,

const unsigned OperandBitWidth =
DL.getTypeSizeInBits(I->getOperand(Op)->getType());
ConstantRange Range = ConstantRange(OperandBitWidth);
ConstantRange Range = ConstantRange::getFull(OperandBitWidth);
if (hasBlockValue(I->getOperand(Op), BB)) {
ValueLatticeElement Val = getBlockValue(I->getOperand(Op), BB);
intersectAssumeOrGuardBlockValueConstantRange(I->getOperand(Op), Val, I);
Expand Down Expand Up @@ -1576,14 +1576,14 @@ ConstantRange LazyValueInfo::getConstantRange(Value *V, BasicBlock *BB,
ValueLatticeElement Result =
getImpl(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI);
if (Result.isUndefined())
return ConstantRange(Width, /*isFullSet=*/false);
return ConstantRange::getEmpty(Width);
if (Result.isConstantRange())
return Result.getConstantRange();
// We represent ConstantInt constants as constant ranges but other kinds
// of integer constants, i.e. ConstantExpr will be tagged as constants
assert(!(Result.isConstant() && isa<ConstantInt>(Result.getConstant())) &&
"ConstantInt value must be represented as constantrange");
return ConstantRange(Width, /*isFullSet=*/true);
return ConstantRange::getFull(Width);
}

/// Determine whether the specified value is known to be a
Expand Down Expand Up @@ -1615,14 +1615,14 @@ ConstantRange LazyValueInfo::getConstantRangeOnEdge(Value *V,
getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);

if (Result.isUndefined())
return ConstantRange(Width, /*isFullSet=*/false);
return ConstantRange::getEmpty(Width);
if (Result.isConstantRange())
return Result.getConstantRange();
// We represent ConstantInt constants as constant ranges but other kinds
// of integer constants, i.e. ConstantExpr will be tagged as constants
assert(!(Result.isConstant() && isa<ConstantInt>(Result.getConstant())) &&
"ConstantInt value must be represented as constantrange");
return ConstantRange(Width, /*isFullSet=*/true);
return ConstantRange::getFull(Width);
}

static LazyValueInfo::Tristate
Expand Down
14 changes: 7 additions & 7 deletions llvm/lib/Analysis/ScalarEvolution.cpp
Expand Up @@ -5774,7 +5774,7 @@ static ConstantRange getRangeForAffineARHelper(APInt Step,
// FullRange), then we don't know anything about the final range either.
// Return FullRange.
if (StartRange.isFullSet())
return ConstantRange(BitWidth, /* isFullSet = */ true);
return ConstantRange::getFull(BitWidth);

// If Step is signed and negative, then we use its absolute value, but we also
// note that we're moving in the opposite direction.
Expand All @@ -5790,7 +5790,7 @@ static ConstantRange getRangeForAffineARHelper(APInt Step,
// Check if Offset is more than full span of BitWidth. If it is, the
// expression is guaranteed to overflow.
if (APInt::getMaxValue(StartRange.getBitWidth()).udiv(Step).ult(MaxBECount))
return ConstantRange(BitWidth, /* isFullSet = */ true);
return ConstantRange::getFull(BitWidth);

// Offset is by how much the expression can change. Checks above guarantee no
// overflow here.
Expand All @@ -5809,7 +5809,7 @@ static ConstantRange getRangeForAffineARHelper(APInt Step,
// range (due to wrap around). This means that the expression can take any
// value in this bitwidth, and we have to return full range.
if (StartRange.contains(MovedBoundary))
return ConstantRange(BitWidth, /* isFullSet = */ true);
return ConstantRange::getFull(BitWidth);

APInt NewLower =
Descending ? std::move(MovedBoundary) : std::move(StartLower);
Expand All @@ -5819,7 +5819,7 @@ static ConstantRange getRangeForAffineARHelper(APInt Step,

// If we end up with full range, return a proper full range.
if (NewLower == NewUpper)
return ConstantRange(BitWidth, /* isFullSet = */ true);
return ConstantRange::getFull(BitWidth);

// No overflow detected, return [StartLower, StartUpper + Offset + 1) range.
return ConstantRange(std::move(NewLower), std::move(NewUpper));
Expand Down Expand Up @@ -5939,17 +5939,17 @@ ConstantRange ScalarEvolution::getRangeViaFactoring(const SCEV *Start,

SelectPattern StartPattern(*this, BitWidth, Start);
if (!StartPattern.isRecognized())
return ConstantRange(BitWidth, /* isFullSet = */ true);
return ConstantRange::getFull(BitWidth);

SelectPattern StepPattern(*this, BitWidth, Step);
if (!StepPattern.isRecognized())
return ConstantRange(BitWidth, /* isFullSet = */ true);
return ConstantRange::getFull(BitWidth);

if (StartPattern.Condition != StepPattern.Condition) {
// We don't handle this case today; but we could, by considering four
// possibilities below instead of two. I'm not sure if there are cases where
// that will help over what getRange already does, though.
return ConstantRange(BitWidth, /* isFullSet = */ true);
return ConstantRange::getFull(BitWidth);
}

// NB! Calling ScalarEvolution::getConstant is fine, but we should not try to
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/ValueTracking.cpp
Expand Up @@ -5711,7 +5711,7 @@ ConstantRange llvm::computeConstantRange(const Value *V, bool UseInstrInfo) {
setLimitsForSelectPattern(*SI, Lower, Upper);

ConstantRange CR = Lower != Upper ? ConstantRange(Lower, Upper)
: ConstantRange(BitWidth, true);
: ConstantRange::getFull(BitWidth);

if (auto *I = dyn_cast<Instruction>(V))
if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range))
Expand Down

0 comments on commit 977934f

Please sign in to comment.