Skip to content

Commit 46c59a5

Browse files
committed
[SCEV][NFC] Refactor range computation for AddRec to pass around APInt
1 parent ff471dc commit 46c59a5

File tree

2 files changed

+41
-36
lines changed

2 files changed

+41
-36
lines changed

llvm/include/llvm/Analysis/ScalarEvolution.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1674,7 +1674,7 @@ class ScalarEvolution {
16741674
/// Determines the range for the affine SCEVAddRecExpr {\p Start,+,\p Step}.
16751675
/// Helper for \c getRange.
16761676
ConstantRange getRangeForAffineAR(const SCEV *Start, const SCEV *Step,
1677-
const SCEV *MaxBECount, unsigned BitWidth);
1677+
const APInt &MaxBECount);
16781678

16791679
/// Determines the range for the affine non-self-wrapping SCEVAddRecExpr {\p
16801680
/// Start,+,\p Step}<nw>.
@@ -1687,7 +1687,7 @@ class ScalarEvolution {
16871687
/// Step} by "factoring out" a ternary expression from the add recurrence.
16881688
/// Helper called by \c getRange.
16891689
ConstantRange getRangeViaFactoring(const SCEV *Start, const SCEV *Step,
1690-
const SCEV *MaxBECount, unsigned BitWidth);
1690+
const APInt &MaxBECount);
16911691

16921692
/// If the unknown expression U corresponds to a simple recurrence, return
16931693
/// a constant range which represents the entire recurrence. Note that

llvm/lib/Analysis/ScalarEvolution.cpp

+39-34
Original file line numberDiff line numberDiff line change
@@ -6699,29 +6699,31 @@ const ConstantRange &ScalarEvolution::getRangeRef(
66996699

67006700
// TODO: non-affine addrec
67016701
if (AddRec->isAffine()) {
6702-
const SCEV *MaxBECount =
6702+
const SCEV *MaxBEScev =
67036703
getConstantMaxBackedgeTakenCount(AddRec->getLoop());
6704-
if (!isa<SCEVCouldNotCompute>(MaxBECount) &&
6705-
getTypeSizeInBits(MaxBECount->getType()) <= BitWidth) {
6706-
auto RangeFromAffine = getRangeForAffineAR(
6707-
AddRec->getStart(), AddRec->getStepRecurrence(*this), MaxBECount,
6708-
BitWidth);
6709-
ConservativeResult =
6710-
ConservativeResult.intersectWith(RangeFromAffine, RangeType);
6704+
if (!isa<SCEVCouldNotCompute>(MaxBEScev)) {
6705+
APInt MaxBECount = cast<SCEVConstant>(MaxBEScev)->getAPInt();
6706+
if (MaxBECount.getBitWidth() < BitWidth)
6707+
MaxBECount = MaxBECount.zext(BitWidth);
6708+
if (MaxBECount.getBitWidth() == BitWidth) {
6709+
auto RangeFromAffine = getRangeForAffineAR(
6710+
AddRec->getStart(), AddRec->getStepRecurrence(*this), MaxBECount);
6711+
ConservativeResult =
6712+
ConservativeResult.intersectWith(RangeFromAffine, RangeType);
67116713

6712-
auto RangeFromFactoring = getRangeViaFactoring(
6713-
AddRec->getStart(), AddRec->getStepRecurrence(*this), MaxBECount,
6714-
BitWidth);
6715-
ConservativeResult =
6716-
ConservativeResult.intersectWith(RangeFromFactoring, RangeType);
6714+
auto RangeFromFactoring = getRangeViaFactoring(
6715+
AddRec->getStart(), AddRec->getStepRecurrence(*this), MaxBECount);
6716+
ConservativeResult =
6717+
ConservativeResult.intersectWith(RangeFromFactoring, RangeType);
6718+
}
67176719
}
67186720

67196721
// Now try symbolic BE count and more powerful methods.
67206722
if (UseExpensiveRangeSharpening) {
67216723
const SCEV *SymbolicMaxBECount =
67226724
getSymbolicMaxBackedgeTakenCount(AddRec->getLoop());
67236725
if (!isa<SCEVCouldNotCompute>(SymbolicMaxBECount) &&
6724-
getTypeSizeInBits(MaxBECount->getType()) <= BitWidth &&
6726+
getTypeSizeInBits(MaxBEScev->getType()) <= BitWidth &&
67256727
AddRec->hasNoSelfWrap()) {
67266728
auto RangeFromAffineNew = getRangeForAffineNoSelfWrappingAR(
67276729
AddRec, SymbolicMaxBECount, BitWidth, SignHint);
@@ -6885,7 +6887,10 @@ const ConstantRange &ScalarEvolution::getRangeRef(
68856887
static ConstantRange getRangeForAffineARHelper(APInt Step,
68866888
const ConstantRange &StartRange,
68876889
const APInt &MaxBECount,
6888-
unsigned BitWidth, bool Signed) {
6890+
bool Signed) {
6891+
unsigned BitWidth = Step.getBitWidth();
6892+
assert(BitWidth == StartRange.getBitWidth() &&
6893+
BitWidth == MaxBECount.getBitWidth() && "mismatched bit widths");
68896894
// If either Step or MaxBECount is 0, then the expression won't change, and we
68906895
// just need to return the initial range.
68916896
if (Step == 0 || MaxBECount == 0)
@@ -6944,32 +6949,28 @@ static ConstantRange getRangeForAffineARHelper(APInt Step,
69446949

69456950
ConstantRange ScalarEvolution::getRangeForAffineAR(const SCEV *Start,
69466951
const SCEV *Step,
6947-
const SCEV *MaxBECount,
6948-
unsigned BitWidth) {
6949-
assert(!isa<SCEVCouldNotCompute>(MaxBECount) &&
6950-
getTypeSizeInBits(MaxBECount->getType()) <= BitWidth &&
6951-
"Precondition!");
6952-
6953-
MaxBECount = getNoopOrZeroExtend(MaxBECount, Start->getType());
6954-
APInt MaxBECountValue = getUnsignedRangeMax(MaxBECount);
6952+
const APInt &MaxBECount) {
6953+
assert(getTypeSizeInBits(Start->getType()) ==
6954+
getTypeSizeInBits(Step->getType()) &&
6955+
getTypeSizeInBits(Start->getType()) == MaxBECount.getBitWidth() &&
6956+
"mismatched bit widths");
69556957

69566958
// First, consider step signed.
69576959
ConstantRange StartSRange = getSignedRange(Start);
69586960
ConstantRange StepSRange = getSignedRange(Step);
69596961

69606962
// If Step can be both positive and negative, we need to find ranges for the
69616963
// maximum absolute step values in both directions and union them.
6962-
ConstantRange SR =
6963-
getRangeForAffineARHelper(StepSRange.getSignedMin(), StartSRange,
6964-
MaxBECountValue, BitWidth, /* Signed = */ true);
6964+
ConstantRange SR = getRangeForAffineARHelper(
6965+
StepSRange.getSignedMin(), StartSRange, MaxBECount, /* Signed = */ true);
69656966
SR = SR.unionWith(getRangeForAffineARHelper(StepSRange.getSignedMax(),
6966-
StartSRange, MaxBECountValue,
6967-
BitWidth, /* Signed = */ true));
6967+
StartSRange, MaxBECount,
6968+
/* Signed = */ true));
69686969

69696970
// Next, consider step unsigned.
69706971
ConstantRange UR = getRangeForAffineARHelper(
6971-
getUnsignedRangeMax(Step), getUnsignedRange(Start),
6972-
MaxBECountValue, BitWidth, /* Signed = */ false);
6972+
getUnsignedRangeMax(Step), getUnsignedRange(Start), MaxBECount,
6973+
/* Signed = */ false);
69736974

69746975
// Finally, intersect signed and unsigned ranges.
69756976
return SR.intersectWith(UR, ConstantRange::Smallest);
@@ -7045,11 +7046,15 @@ ConstantRange ScalarEvolution::getRangeForAffineNoSelfWrappingAR(
70457046

70467047
ConstantRange ScalarEvolution::getRangeViaFactoring(const SCEV *Start,
70477048
const SCEV *Step,
7048-
const SCEV *MaxBECount,
7049-
unsigned BitWidth) {
7049+
const APInt &MaxBECount) {
70507050
// RangeOf({C?A:B,+,C?P:Q}) == RangeOf(C?{A,+,P}:{B,+,Q})
70517051
// == RangeOf({A,+,P}) union RangeOf({B,+,Q})
70527052

7053+
unsigned BitWidth = MaxBECount.getBitWidth();
7054+
assert(getTypeSizeInBits(Start->getType()) == BitWidth &&
7055+
getTypeSizeInBits(Step->getType()) == BitWidth &&
7056+
"mismatched bit widths");
7057+
70537058
struct SelectPattern {
70547059
Value *Condition = nullptr;
70557060
APInt TrueValue;
@@ -7151,9 +7156,9 @@ ConstantRange ScalarEvolution::getRangeViaFactoring(const SCEV *Start,
71517156
const SCEV *FalseStep = this->getConstant(StepPattern.FalseValue);
71527157

71537158
ConstantRange TrueRange =
7154-
this->getRangeForAffineAR(TrueStart, TrueStep, MaxBECount, BitWidth);
7159+
this->getRangeForAffineAR(TrueStart, TrueStep, MaxBECount);
71557160
ConstantRange FalseRange =
7156-
this->getRangeForAffineAR(FalseStart, FalseStep, MaxBECount, BitWidth);
7161+
this->getRangeForAffineAR(FalseStart, FalseStep, MaxBECount);
71577162

71587163
return TrueRange.unionWith(FalseRange);
71597164
}

0 commit comments

Comments
 (0)