llvm.org GIT mirror llvm / cd5029d
[SCEV] Unify getUnsignedRange and getSignedRange Summary: This removes some duplicated code, and also helps optimization: e.g. in the test case added, `%idx ULT 128` in `@x` is not currently optimized to `true` by `-indvars` but will be, after this change. The only functional change in ths commit is that for add recurrences, ScalarEvolution::getRange will be more aggressive -- computing the unsigned (resp. signed) range for a SCEVAddRecExpr will now look at the NSW (resp. NUW) bits and check for signed (resp. unsigned) overflow. This can be a strict improvement in some cases (such as the attached test case), and should be no worse in other cases. Reviewers: atrick, nlewycky Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D8142 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@231709 91177308-0d34-0410-b5e6-96231b3b80d8 Sanjoy Das 5 years ago
3 changed file(s) with 188 addition(s) and 229 deletion(s). Raw diff Collapse all Expand all
387387 /// computeBlockDisposition - Compute a BlockDisposition value.
388388 BlockDisposition computeBlockDisposition(const SCEV *S, const BasicBlock *BB);
389389
390 /// UnsignedRanges - Memoized results from getUnsignedRange
390 /// UnsignedRanges - Memoized results from getRange
391391 DenseMap UnsignedRanges;
392392
393 /// SignedRanges - Memoized results from getSignedRange
393 /// SignedRanges - Memoized results from getRange
394394 DenseMap SignedRanges;
395395
396 /// setUnsignedRange - Set the memoized unsigned range for the given SCEV.
397 const ConstantRange &setUnsignedRange(const SCEV *S,
398 const ConstantRange &CR) {
396 /// RangeSignHint - Used to parameterize getRange
397 enum RangeSignHint { HINT_RANGE_UNSIGNED, HINT_RANGE_SIGNED };
398
399 /// setRange - Set the memoized range for the given SCEV.
400 const ConstantRange &setRange(const SCEV *S, RangeSignHint Hint,
401 const ConstantRange &CR) {
402 DenseMap &Cache =
403 Hint == HINT_RANGE_UNSIGNED ? UnsignedRanges : SignedRanges;
404
399405 std::pair::iterator, bool> Pair =
400 UnsignedRanges.insert(std::make_pair(S, CR));
406 Cache.insert(std::make_pair(S, CR));
401407 if (!Pair.second)
402408 Pair.first->second = CR;
403409 return Pair.first->second;
404410 }
405411
406 /// setUnsignedRange - Set the memoized signed range for the given SCEV.
407 const ConstantRange &setSignedRange(const SCEV *S,
408 const ConstantRange &CR) {
409 std::pair::iterator, bool> Pair =
410 SignedRanges.insert(std::make_pair(S, CR));
411 if (!Pair.second)
412 Pair.first->second = CR;
413 return Pair.first->second;
414 }
412 /// getRange - Determine the range for a particular SCEV.
413 ConstantRange getRange(const SCEV *S, RangeSignHint Hint);
415414
416415 /// createSCEV - We know that there is no SCEV for the specified value.
417416 /// Analyze the expression.
842841
843842 /// getUnsignedRange - Determine the unsigned range for a particular SCEV.
844843 ///
845 ConstantRange getUnsignedRange(const SCEV *S);
844 ConstantRange getUnsignedRange(const SCEV *S) {
845 return getRange(S, HINT_RANGE_UNSIGNED);
846 }
846847
847848 /// getSignedRange - Determine the signed range for a particular SCEV.
848849 ///
849 ConstantRange getSignedRange(const SCEV *S);
850 ConstantRange getSignedRange(const SCEV *S) {
851 return getRange(S, HINT_RANGE_SIGNED);
852 }
850853
851854 /// isKnownNegative - Test if the given expression is known to be negative.
852855 ///
38673867 return None;
38683868 }
38693869
3870 /// getUnsignedRange - Determine the unsigned range for a particular SCEV.
3870 /// getRange - Determine the range for a particular SCEV. If SignHint is
3871 /// HINT_RANGE_UNSIGNED (resp. HINT_RANGE_SIGNED) then getRange prefers ranges
3872 /// with a "cleaner" unsigned (resp. signed) representation.
38713873 ///
38723874 ConstantRange
3873 ScalarEvolution::getUnsignedRange(const SCEV *S) {
3875 ScalarEvolution::getRange(const SCEV *S,
3876 ScalarEvolution::RangeSignHint SignHint) {
3877 DenseMap &Cache =
3878 SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED ? UnsignedRanges
3879 : SignedRanges;
3880
38743881 // See if we've computed this range already.
3875 DenseMap::iterator I = UnsignedRanges.find(S);
3876 if (I != UnsignedRanges.end())
3882 DenseMap::iterator I = Cache.find(S);
3883 if (I != Cache.end())
38773884 return I->second;
38783885
38793886 if (const SCEVConstant *C = dyn_cast(S))
3880 return setUnsignedRange(C, ConstantRange(C->getValue()->getValue()));
3887 return setRange(C, SignHint, ConstantRange(C->getValue()->getValue()));
38813888
38823889 unsigned BitWidth = getTypeSizeInBits(S->getType());
38833890 ConstantRange ConservativeResult(BitWidth, /*isFullSet=*/true);
38843891
3885 // If the value has known zeros, the maximum unsigned value will have those
3886 // known zeros as well.
3892 // If the value has known zeros, the maximum value will have those known zeros
3893 // as well.
38873894 uint32_t TZ = GetMinTrailingZeros(S);
3888 if (TZ != 0)
3889 ConservativeResult =
3890 ConstantRange(APInt::getMinValue(BitWidth),
3891 APInt::getMaxValue(BitWidth).lshr(TZ).shl(TZ) + 1);
3895 if (TZ != 0) {
3896 if (SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED)
3897 ConservativeResult =
3898 ConstantRange(APInt::getMinValue(BitWidth),
3899 APInt::getMaxValue(BitWidth).lshr(TZ).shl(TZ) + 1);
3900 else
3901 ConservativeResult = ConstantRange(
3902 APInt::getSignedMinValue(BitWidth),
3903 APInt::getSignedMaxValue(BitWidth).ashr(TZ).shl(TZ) + 1);
3904 }
38923905
38933906 if (const SCEVAddExpr *Add = dyn_cast(S)) {
3894 ConstantRange X = getUnsignedRange(Add->getOperand(0));
3907 ConstantRange X = getRange(Add->getOperand(0), SignHint);
38953908 for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i)
3896 X = X.add(getUnsignedRange(Add->getOperand(i)));
3897 return setUnsignedRange(Add, ConservativeResult.intersectWith(X));
3909 X = X.add(getRange(Add->getOperand(i), SignHint));
3910 return setRange(Add, SignHint, ConservativeResult.intersectWith(X));
38983911 }
38993912
39003913 if (const SCEVMulExpr *Mul = dyn_cast(S)) {
3901 ConstantRange X = getUnsignedRange(Mul->getOperand(0));
3914 ConstantRange X = getRange(Mul->getOperand(0), SignHint);
39023915 for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i)
3903 X = X.multiply(getUnsignedRange(Mul->getOperand(i)));
3904 return setUnsignedRange(Mul, ConservativeResult.intersectWith(X));
3916 X = X.multiply(getRange(Mul->getOperand(i), SignHint));
3917 return setRange(Mul, SignHint, ConservativeResult.intersectWith(X));
39053918 }
39063919
39073920 if (const SCEVSMaxExpr *SMax = dyn_cast(S)) {
3908 ConstantRange X = getUnsignedRange(SMax->getOperand(0));
3921 ConstantRange X = getRange(SMax->getOperand(0), SignHint);
39093922 for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i)
3910 X = X.smax(getUnsignedRange(SMax->getOperand(i)));
3911 return setUnsignedRange(SMax, ConservativeResult.intersectWith(X));
3923 X = X.smax(getRange(SMax->getOperand(i), SignHint));
3924 return setRange(SMax, SignHint, ConservativeResult.intersectWith(X));
39123925 }
39133926
39143927 if (const SCEVUMaxExpr *UMax = dyn_cast(S)) {
3915 ConstantRange X = getUnsignedRange(UMax->getOperand(0));
3928 ConstantRange X = getRange(UMax->getOperand(0), SignHint);
39163929 for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i)
3917 X = X.umax(getUnsignedRange(UMax->getOperand(i)));
3918 return setUnsignedRange(UMax, ConservativeResult.intersectWith(X));
3930 X = X.umax(getRange(UMax->getOperand(i), SignHint));
3931 return setRange(UMax, SignHint, ConservativeResult.intersectWith(X));
39193932 }
39203933
39213934 if (const SCEVUDivExpr *UDiv = dyn_cast(S)) {
3922 ConstantRange X = getUnsignedRange(UDiv->getLHS());
3923 ConstantRange Y = getUnsignedRange(UDiv->getRHS());
3924 return setUnsignedRange(UDiv, ConservativeResult.intersectWith(X.udiv(Y)));
3935 ConstantRange X = getRange(UDiv->getLHS(), SignHint);
3936 ConstantRange Y = getRange(UDiv->getRHS(), SignHint);
3937 return setRange(UDiv, SignHint,
3938 ConservativeResult.intersectWith(X.udiv(Y)));
39253939 }
39263940
39273941 if (const SCEVZeroExtendExpr *ZExt = dyn_cast(S)) {
3928 ConstantRange X = getUnsignedRange(ZExt->getOperand());
3929 return setUnsignedRange(ZExt,
3930 ConservativeResult.intersectWith(X.zeroExtend(BitWidth)));
3942 ConstantRange X = getRange(ZExt->getOperand(), SignHint);
3943 return setRange(ZExt, SignHint,
3944 ConservativeResult.intersectWith(X.zeroExtend(BitWidth)));
39313945 }
39323946
39333947 if (const SCEVSignExtendExpr *SExt = dyn_cast(S)) {
3934 ConstantRange X = getUnsignedRange(SExt->getOperand());
3935 return setUnsignedRange(SExt,
3936 ConservativeResult.intersectWith(X.signExtend(BitWidth)));
3948 ConstantRange X = getRange(SExt->getOperand(), SignHint);
3949 return setRange(SExt, SignHint,
3950 ConservativeResult.intersectWith(X.signExtend(BitWidth)));
39373951 }
39383952
39393953 if (const SCEVTruncateExpr *Trunc = dyn_cast(S)) {
3940 ConstantRange X = getUnsignedRange(Trunc->getOperand());
3941 return setUnsignedRange(Trunc,
3942 ConservativeResult.intersectWith(X.truncate(BitWidth)));
3954 ConstantRange X = getRange(Trunc->getOperand(), SignHint);
3955 return setRange(Trunc, SignHint,
3956 ConservativeResult.intersectWith(X.truncate(BitWidth)));
39433957 }
39443958
39453959 if (const SCEVAddRecExpr *AddRec = dyn_cast(S)) {
39523966 ConservativeResult.intersectWith(
39533967 ConstantRange(C->getValue()->getValue(), APInt(BitWidth, 0)));
39543968
3955 // TODO: non-affine addrec
3956 if (AddRec->isAffine()) {
3957 Type *Ty = AddRec->getType();
3958 const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop());
3959 if (!isa(MaxBECount) &&
3960 getTypeSizeInBits(MaxBECount->getType()) <= BitWidth) {
3961 MaxBECount = getNoopOrZeroExtend(MaxBECount, Ty);
3962
3963 const SCEV *Start = AddRec->getStart();
3964 const SCEV *Step = AddRec->getStepRecurrence(*this);
3965
3966 ConstantRange StartRange = getUnsignedRange(Start);
3967 ConstantRange StepRange = getSignedRange(Step);
3968 ConstantRange MaxBECountRange = getUnsignedRange(MaxBECount);
3969 ConstantRange EndRange =
3970 StartRange.add(MaxBECountRange.multiply(StepRange));
3971
3972 // Check for overflow. This must be done with ConstantRange arithmetic
3973 // because we could be called from within the ScalarEvolution overflow
3974 // checking code.
3975 ConstantRange ExtStartRange = StartRange.zextOrTrunc(BitWidth*2+1);
3976 ConstantRange ExtStepRange = StepRange.sextOrTrunc(BitWidth*2+1);
3977 ConstantRange ExtMaxBECountRange =
3978 MaxBECountRange.zextOrTrunc(BitWidth*2+1);
3979 ConstantRange ExtEndRange = EndRange.zextOrTrunc(BitWidth*2+1);
3980 if (ExtStartRange.add(ExtMaxBECountRange.multiply(ExtStepRange)) !=
3981 ExtEndRange)
3982 return setUnsignedRange(AddRec, ConservativeResult);
3983
3984 APInt Min = APIntOps::umin(StartRange.getUnsignedMin(),
3985 EndRange.getUnsignedMin());
3986 APInt Max = APIntOps::umax(StartRange.getUnsignedMax(),
3987 EndRange.getUnsignedMax());
3988 if (Min.isMinValue() && Max.isMaxValue())
3989 return setUnsignedRange(AddRec, ConservativeResult);
3990 return setUnsignedRange(AddRec,
3991 ConservativeResult.intersectWith(ConstantRange(Min, Max+1)));
3992 }
3993 }
3994
3995 return setUnsignedRange(AddRec, ConservativeResult);
3996 }
3997
3998 if (const SCEVUnknown *U = dyn_cast(S)) {
3999 // Check if the IR explicitly contains !range metadata.
4000 Optional MDRange = GetRangeFromMetadata(U->getValue());
4001 if (MDRange.hasValue())
4002 ConservativeResult = ConservativeResult.intersectWith(MDRange.getValue());
4003
4004 // For a SCEVUnknown, ask ValueTracking.
4005 APInt Zeros(BitWidth, 0), Ones(BitWidth, 0);
4006 computeKnownBits(U->getValue(), Zeros, Ones, DL, 0, AC, nullptr, DT);
4007 if (Ones == ~Zeros + 1)
4008 return setUnsignedRange(U, ConservativeResult);
4009 return setUnsignedRange(U,
4010 ConservativeResult.intersectWith(ConstantRange(Ones, ~Zeros + 1)));
4011 }
4012
4013 return setUnsignedRange(S, ConservativeResult);
4014 }
4015
4016 /// getSignedRange - Determine the signed range for a particular SCEV.
4017 ///
4018 ConstantRange
4019 ScalarEvolution::getSignedRange(const SCEV *S) {
4020 // See if we've computed this range already.
4021 DenseMap::iterator I = SignedRanges.find(S);
4022 if (I != SignedRanges.end())
4023 return I->second;
4024
4025 if (const SCEVConstant *C = dyn_cast(S))
4026 return setSignedRange(C, ConstantRange(C->getValue()->getValue()));
4027
4028 unsigned BitWidth = getTypeSizeInBits(S->getType());
4029 ConstantRange ConservativeResult(BitWidth, /*isFullSet=*/true);
4030
4031 // If the value has known zeros, the maximum signed value will have those
4032 // known zeros as well.
4033 uint32_t TZ = GetMinTrailingZeros(S);
4034 if (TZ != 0)
4035 ConservativeResult =
4036 ConstantRange(APInt::getSignedMinValue(BitWidth),
4037 APInt::getSignedMaxValue(BitWidth).ashr(TZ).shl(TZ) + 1);
4038
4039 if (const SCEVAddExpr *Add = dyn_cast(S)) {
4040 ConstantRange X = getSignedRange(Add->getOperand(0));
4041 for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i)
4042 X = X.add(getSignedRange(Add->getOperand(i)));
4043 return setSignedRange(Add, ConservativeResult.intersectWith(X));
4044 }
4045
4046 if (const SCEVMulExpr *Mul = dyn_cast(S)) {
4047 ConstantRange X = getSignedRange(Mul->getOperand(0));
4048 for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i)
4049 X = X.multiply(getSignedRange(Mul->getOperand(i)));
4050 return setSignedRange(Mul, ConservativeResult.intersectWith(X));
4051 }
4052
4053 if (const SCEVSMaxExpr *SMax = dyn_cast(S)) {
4054 ConstantRange X = getSignedRange(SMax->getOperand(0));
4055 for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i)
4056 X = X.smax(getSignedRange(SMax->getOperand(i)));
4057 return setSignedRange(SMax, ConservativeResult.intersectWith(X));
4058 }
4059
4060 if (const SCEVUMaxExpr *UMax = dyn_cast(S)) {
4061 ConstantRange X = getSignedRange(UMax->getOperand(0));
4062 for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i)
4063 X = X.umax(getSignedRange(UMax->getOperand(i)));
4064 return setSignedRange(UMax, ConservativeResult.intersectWith(X));
4065 }
4066
4067 if (const SCEVUDivExpr *UDiv = dyn_cast(S)) {
4068 ConstantRange X = getSignedRange(UDiv->getLHS());
4069 ConstantRange Y = getSignedRange(UDiv->getRHS());
4070 return setSignedRange(UDiv, ConservativeResult.intersectWith(X.udiv(Y)));
4071 }
4072
4073 if (const SCEVZeroExtendExpr *ZExt = dyn_cast(S)) {
4074 ConstantRange X = getSignedRange(ZExt->getOperand());
4075 return setSignedRange(ZExt,
4076 ConservativeResult.intersectWith(X.zeroExtend(BitWidth)));
4077 }
4078
4079 if (const SCEVSignExtendExpr *SExt = dyn_cast(S)) {
4080 ConstantRange X = getSignedRange(SExt->getOperand());
4081 return setSignedRange(SExt,
4082 ConservativeResult.intersectWith(X.signExtend(BitWidth)));
4083 }
4084
4085 if (const SCEVTruncateExpr *Trunc = dyn_cast(S)) {
4086 ConstantRange X = getSignedRange(Trunc->getOperand());
4087 return setSignedRange(Trunc,
4088 ConservativeResult.intersectWith(X.truncate(BitWidth)));
4089 }
4090
4091 if (const SCEVAddRecExpr *AddRec = dyn_cast(S)) {
40923969 // If there's no signed wrap, and all the operands have the same sign or
40933970 // zero, the value won't ever change sign.
40943971 if (AddRec->getNoWrapFlags(SCEV::FlagNSW)) {
41143991 const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop());
41153992 if (!isa(MaxBECount) &&
41163993 getTypeSizeInBits(MaxBECount->getType()) <= BitWidth) {
3994
3995 // Check for overflow. This must be done with ConstantRange arithmetic
3996 // because we could be called from within the ScalarEvolution overflow
3997 // checking code.
3998
41173999 MaxBECount = getNoopOrZeroExtend(MaxBECount, Ty);
4000 ConstantRange MaxBECountRange = getUnsignedRange(MaxBECount);
4001 ConstantRange ZExtMaxBECountRange =
4002 MaxBECountRange.zextOrTrunc(BitWidth * 2 + 1);
41184003
41194004 const SCEV *Start = AddRec->getStart();
41204005 const SCEV *Step = AddRec->getStepRecurrence(*this);
4121
4122 ConstantRange StartRange = getSignedRange(Start);
4123 ConstantRange StepRange = getSignedRange(Step);
4124 ConstantRange MaxBECountRange = getUnsignedRange(MaxBECount);
4125 ConstantRange EndRange =
4126 StartRange.add(MaxBECountRange.multiply(StepRange));
4127
4128 // Check for overflow. This must be done with ConstantRange arithmetic
4129 // because we could be called from within the ScalarEvolution overflow
4130 // checking code.
4131 ConstantRange ExtStartRange = StartRange.sextOrTrunc(BitWidth*2+1);
4132 ConstantRange ExtStepRange = StepRange.sextOrTrunc(BitWidth*2+1);
4133 ConstantRange ExtMaxBECountRange =
4134 MaxBECountRange.zextOrTrunc(BitWidth*2+1);
4135 ConstantRange ExtEndRange = EndRange.sextOrTrunc(BitWidth*2+1);
4136 if (ExtStartRange.add(ExtMaxBECountRange.multiply(ExtStepRange)) !=
4137 ExtEndRange)
4138 return setSignedRange(AddRec, ConservativeResult);
4139
4140 APInt Min = APIntOps::smin(StartRange.getSignedMin(),
4141 EndRange.getSignedMin());
4142 APInt Max = APIntOps::smax(StartRange.getSignedMax(),
4143 EndRange.getSignedMax());
4144 if (Min.isMinSignedValue() && Max.isMaxSignedValue())
4145 return setSignedRange(AddRec, ConservativeResult);
4146 return setSignedRange(AddRec,
4147 ConservativeResult.intersectWith(ConstantRange(Min, Max+1)));
4006 ConstantRange StepSRange = getSignedRange(Step);
4007 ConstantRange SExtStepSRange = StepSRange.sextOrTrunc(BitWidth * 2 + 1);
4008
4009 ConstantRange StartURange = getUnsignedRange(Start);
4010 ConstantRange EndURange =
4011 StartURange.add(MaxBECountRange.multiply(StepSRange));
4012
4013 // Check for unsigned overflow.
4014 ConstantRange ZExtStartURange =
4015 StartURange.zextOrTrunc(BitWidth * 2 + 1);
4016 ConstantRange ZExtEndURange = EndURange.zextOrTrunc(BitWidth * 2 + 1);
4017 if (ZExtStartURange.add(ZExtMaxBECountRange.multiply(SExtStepSRange)) ==
4018 ZExtEndURange) {
4019 APInt Min = APIntOps::umin(StartURange.getUnsignedMin(),
4020 EndURange.getUnsignedMin());
4021 APInt Max = APIntOps::umax(StartURange.getUnsignedMax(),
4022 EndURange.getUnsignedMax());
4023 bool IsFullRange = Min.isMinValue() && Max.isMaxValue();
4024 if (!IsFullRange)
4025 ConservativeResult =
4026 ConservativeResult.intersectWith(ConstantRange(Min, Max + 1));
4027 }
4028
4029 ConstantRange StartSRange = getSignedRange(Start);
4030 ConstantRange EndSRange =
4031 StartSRange.add(MaxBECountRange.multiply(StepSRange));
4032
4033 // Check for signed overflow. This must be done with ConstantRange
4034 // arithmetic because we could be called from within the ScalarEvolution
4035 // overflow checking code.
4036 ConstantRange SExtStartSRange =
4037 StartSRange.sextOrTrunc(BitWidth * 2 + 1);
4038 ConstantRange SExtEndSRange = EndSRange.sextOrTrunc(BitWidth * 2 + 1);
4039 if (SExtStartSRange.add(ZExtMaxBECountRange.multiply(SExtStepSRange)) ==
4040 SExtEndSRange) {
4041 APInt Min = APIntOps::smin(StartSRange.getSignedMin(),
4042 EndSRange.getSignedMin());
4043 APInt Max = APIntOps::smax(StartSRange.getSignedMax(),
4044 EndSRange.getSignedMax());
4045 bool IsFullRange = Min.isMinSignedValue() && Max.isMaxSignedValue();
4046 if (!IsFullRange)
4047 ConservativeResult =
4048 ConservativeResult.intersectWith(ConstantRange(Min, Max + 1));
4049 }
41484050 }
41494051 }
41504052
4151 return setSignedRange(AddRec, ConservativeResult);
4053 return setRange(AddRec, SignHint, ConservativeResult);
41524054 }
41534055
41544056 if (const SCEVUnknown *U = dyn_cast(S)) {
41574059 if (MDRange.hasValue())
41584060 ConservativeResult = ConservativeResult.intersectWith(MDRange.getValue());
41594061
4160 // For a SCEVUnknown, ask ValueTracking.
4161 if (!U->getValue()->getType()->isIntegerTy() && !DL)
4162 return setSignedRange(U, ConservativeResult);
4163 unsigned NS = ComputeNumSignBits(U->getValue(), DL, 0, AC, nullptr, DT);
4164 if (NS <= 1)
4165 return setSignedRange(U, ConservativeResult);
4166 return setSignedRange(U, ConservativeResult.intersectWith(
4167 ConstantRange(APInt::getSignedMinValue(BitWidth).ashr(NS - 1),
4168 APInt::getSignedMaxValue(BitWidth).ashr(NS - 1)+1)));
4169 }
4170
4171 return setSignedRange(S, ConservativeResult);
4062 // Split here to avoid paying the compile-time cost of calling both
4063 // computeKnownBits and ComputeNumSignBits. This restriction can be lifted
4064 // if needed.
4065
4066 if (SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED) {
4067 // For a SCEVUnknown, ask ValueTracking.
4068 APInt Zeros(BitWidth, 0), Ones(BitWidth, 0);
4069 computeKnownBits(U->getValue(), Zeros, Ones, DL, 0, AC, nullptr, DT);
4070 if (Ones != ~Zeros + 1)
4071 ConservativeResult =
4072 ConservativeResult.intersectWith(ConstantRange(Ones, ~Zeros + 1));
4073 } else {
4074 assert(SignHint == ScalarEvolution::HINT_RANGE_SIGNED &&
4075 "generalize as needed!");
4076 if (U->getValue()->getType()->isIntegerTy() || DL) {
4077 unsigned NS = ComputeNumSignBits(U->getValue(), DL, 0, AC, nullptr, DT);
4078 if (NS > 1)
4079 ConservativeResult = ConservativeResult.intersectWith(ConstantRange(
4080 APInt::getSignedMinValue(BitWidth).ashr(NS - 1),
4081 APInt::getSignedMaxValue(BitWidth).ashr(NS - 1) + 1));
4082 }
4083 }
4084
4085 return setRange(U, SignHint, ConservativeResult);
4086 }
4087
4088 return setRange(S, SignHint, ConservativeResult);
41724089 }
41734090
41744091 /// createSCEV - We know that there is no SCEV for the specified value.
0 ; RUN: opt -analyze -scalar-evolution < %s | FileCheck %s
1
2 define void @x(i1* %cond) {
3 ; CHECK-LABEL: Classifying expressions for: @x
4 entry:
5 br label %loop
6
7 loop:
8 %idx = phi i8 [ 0, %entry ], [ %idx.inc, %loop ]
9 ; CHECK: %idx = phi i8 [ 0, %entry ], [ %idx.inc, %loop ]
10 ; CHECK-NEXT: --> {0,+,1}<%loop> U: [0,-128) S: [0,-128)
11
12 %idx.inc = add nsw i8 %idx, 1
13
14 %c = load volatile i1, i1* %cond
15 br i1 %c, label %loop, label %exit
16
17 exit:
18 ret void
19 }
20
21 define void @y(i8* %addr) {
22 ; CHECK-LABEL: Classifying expressions for: @y
23 entry:
24 br label %loop
25
26 loop:
27 %idx = phi i8 [-5, %entry ], [ %idx.inc, %loop ]
28 ; CHECK: %idx = phi i8 [ -5, %entry ], [ %idx.inc, %loop ]
29 ; CHECK-NEXT: --> {-5,+,1}<%loop> U: [-5,6) S: [-5,6)
30
31 %idx.inc = add i8 %idx, 1
32
33 %continue = icmp slt i8 %idx.inc, 6
34 br i1 %continue, label %loop, label %exit
35
36 exit:
37 ret void
38 }