@@ -13005,26 +13005,28 @@ ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
13005
13005
if (!isLoopInvariant(RHS, L)) {
13006
13006
const auto *RHSAddRec = dyn_cast<SCEVAddRecExpr>(RHS);
13007
13007
if (RHSAddRec != nullptr && RHSAddRec->getLoop() == L) {
13008
- /*
13009
- The structure of loop we are trying to calculate backedge-count of:
13010
- left = left_start
13011
- right = right_start
13012
- while(left < right){
13013
- // ... do something here ...
13014
- left += s1; // stride of left is s1>0
13015
- right -= s2; // stride of right is -s2 (s2 > 0)
13016
- }
13017
- // left and right are converging at the middle
13018
- // (maybe not exactly at center)
13008
+ // The structure of loop we are trying to calculate backedge count of:
13009
+ //
13010
+ // left = left_start
13011
+ // right = right_start
13012
+ //
13013
+ // while(left < right){
13014
+ // ... do something here ...
13015
+ // left += s1; // stride of left is s1>0
13016
+ // right -= s2; // stride of right is -s2 (s2 > 0)
13017
+ // }
13018
+ //
13019
+ // Here, left and right are converging somewhere in the middle.
13019
13020
13020
- */
13021
13021
const SCEV *RHSStart = RHSAddRec->getStart();
13022
13022
const SCEV *RHSStride = RHSAddRec->getStepRecurrence(*this);
13023
- // if Stride-RHSStride>0 and does not overflow, we can write
13024
- // backedge count as:
13025
- // RHSStart >= Start ? (RHSStart - Start)/(Stride - RHSStride) ? 0
13026
13023
13027
- // check if RHSStride<0 and Stride-RHSStride will not overflow
13024
+ // If Stride - RHSStride is positive and does not overflow, we can write
13025
+ // backedge count as ->
13026
+ // ceil((End - Start) /u (Stride - RHSStride))
13027
+ // Where, End = max(RHSStart, Start)
13028
+
13029
+ // Check if RHSStride < 0 and Stride - RHSStride will not overflow.
13028
13030
// FIXME: Can RHSStride be positive?
13029
13031
if (isKnownNegative(RHSStride) &&
13030
13032
willNotOverflow(Instruction::Sub, /*Signed=*/true, Stride,
@@ -13033,9 +13035,10 @@ ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
13033
13035
const SCEV *Denominator = getMinusSCEV(Stride, RHSStride);
13034
13036
if (isKnownPositive(Denominator)) {
13035
13037
End = IsSigned ? getSMaxExpr(RHSStart, Start)
13036
- : getUMaxExpr(RHSStart, Start); // max(RHSStart, Start)
13038
+ : getUMaxExpr(RHSStart, Start);
13037
13039
13038
- const SCEV *Delta = getMinusSCEV(End, Start); // End >= Start
13040
+ // We can do this because End >= Start, as End = max(RHSStart, Start)
13041
+ const SCEV *Delta = getMinusSCEV(End, Start);
13039
13042
13040
13043
BECount = getUDivCeilSCEV(Delta, Denominator);
13041
13044
BECountIfBackedgeTaken =
0 commit comments