@@ -6699,29 +6699,31 @@ const ConstantRange &ScalarEvolution::getRangeRef(
6699
6699
6700
6700
// TODO: non-affine addrec
6701
6701
if (AddRec->isAffine()) {
6702
- const SCEV *MaxBECount =
6702
+ const SCEV *MaxBEScev =
6703
6703
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);
6711
6713
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
+ }
6717
6719
}
6718
6720
6719
6721
// Now try symbolic BE count and more powerful methods.
6720
6722
if (UseExpensiveRangeSharpening) {
6721
6723
const SCEV *SymbolicMaxBECount =
6722
6724
getSymbolicMaxBackedgeTakenCount(AddRec->getLoop());
6723
6725
if (!isa<SCEVCouldNotCompute>(SymbolicMaxBECount) &&
6724
- getTypeSizeInBits(MaxBECount ->getType()) <= BitWidth &&
6726
+ getTypeSizeInBits(MaxBEScev ->getType()) <= BitWidth &&
6725
6727
AddRec->hasNoSelfWrap()) {
6726
6728
auto RangeFromAffineNew = getRangeForAffineNoSelfWrappingAR(
6727
6729
AddRec, SymbolicMaxBECount, BitWidth, SignHint);
@@ -6885,7 +6887,10 @@ const ConstantRange &ScalarEvolution::getRangeRef(
6885
6887
static ConstantRange getRangeForAffineARHelper(APInt Step,
6886
6888
const ConstantRange &StartRange,
6887
6889
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");
6889
6894
// If either Step or MaxBECount is 0, then the expression won't change, and we
6890
6895
// just need to return the initial range.
6891
6896
if (Step == 0 || MaxBECount == 0)
@@ -6944,32 +6949,28 @@ static ConstantRange getRangeForAffineARHelper(APInt Step,
6944
6949
6945
6950
ConstantRange ScalarEvolution::getRangeForAffineAR(const SCEV *Start,
6946
6951
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");
6955
6957
6956
6958
// First, consider step signed.
6957
6959
ConstantRange StartSRange = getSignedRange(Start);
6958
6960
ConstantRange StepSRange = getSignedRange(Step);
6959
6961
6960
6962
// If Step can be both positive and negative, we need to find ranges for the
6961
6963
// 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);
6965
6966
SR = SR.unionWith(getRangeForAffineARHelper(StepSRange.getSignedMax(),
6966
- StartSRange, MaxBECountValue ,
6967
- BitWidth, /* Signed = */ true));
6967
+ StartSRange, MaxBECount ,
6968
+ /* Signed = */ true));
6968
6969
6969
6970
// Next, consider step unsigned.
6970
6971
ConstantRange UR = getRangeForAffineARHelper(
6971
- getUnsignedRangeMax(Step), getUnsignedRange(Start),
6972
- MaxBECountValue, BitWidth, /* Signed = */ false);
6972
+ getUnsignedRangeMax(Step), getUnsignedRange(Start), MaxBECount,
6973
+ /* Signed = */ false);
6973
6974
6974
6975
// Finally, intersect signed and unsigned ranges.
6975
6976
return SR.intersectWith(UR, ConstantRange::Smallest);
@@ -7045,11 +7046,15 @@ ConstantRange ScalarEvolution::getRangeForAffineNoSelfWrappingAR(
7045
7046
7046
7047
ConstantRange ScalarEvolution::getRangeViaFactoring(const SCEV *Start,
7047
7048
const SCEV *Step,
7048
- const SCEV *MaxBECount,
7049
- unsigned BitWidth) {
7049
+ const APInt &MaxBECount) {
7050
7050
// RangeOf({C?A:B,+,C?P:Q}) == RangeOf(C?{A,+,P}:{B,+,Q})
7051
7051
// == RangeOf({A,+,P}) union RangeOf({B,+,Q})
7052
7052
7053
+ unsigned BitWidth = MaxBECount.getBitWidth();
7054
+ assert(getTypeSizeInBits(Start->getType()) == BitWidth &&
7055
+ getTypeSizeInBits(Step->getType()) == BitWidth &&
7056
+ "mismatched bit widths");
7057
+
7053
7058
struct SelectPattern {
7054
7059
Value *Condition = nullptr;
7055
7060
APInt TrueValue;
@@ -7151,9 +7156,9 @@ ConstantRange ScalarEvolution::getRangeViaFactoring(const SCEV *Start,
7151
7156
const SCEV *FalseStep = this->getConstant(StepPattern.FalseValue);
7152
7157
7153
7158
ConstantRange TrueRange =
7154
- this->getRangeForAffineAR(TrueStart, TrueStep, MaxBECount, BitWidth );
7159
+ this->getRangeForAffineAR(TrueStart, TrueStep, MaxBECount);
7155
7160
ConstantRange FalseRange =
7156
- this->getRangeForAffineAR(FalseStart, FalseStep, MaxBECount, BitWidth );
7161
+ this->getRangeForAffineAR(FalseStart, FalseStep, MaxBECount);
7157
7162
7158
7163
return TrueRange.unionWith(FalseRange);
7159
7164
}
0 commit comments