Skip to content

Commit 2d49c3b

Browse files
mrdaybirdhiraditya
authored andcommitted
Add test and fix last commit
1 parent 83469ae commit 2d49c3b

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13004,20 +13004,19 @@ ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
1300413004
*BECountIfBackedgeTaken = nullptr;
1300513005
if (!isLoopInvariant(RHS, L)) {
1300613006
const auto *RHSAddRec = dyn_cast<SCEVAddRecExpr>(RHS);
13007-
bool RHSNoWrap = RHSAddRec->getNoWrapFlags();
13008-
if (RHSAddRec != nullptr && RHSAddRec->getLoop() == L && RHSNoWrap) {
13007+
if (PositiveStride && RHSAddRec != nullptr && RHSAddRec->getLoop() == L &&
13008+
RHSAddRec->getNoWrapFlags()) {
1300913009
// The structure of loop we are trying to calculate backedge count of:
1301013010
//
1301113011
// left = left_start
1301213012
// right = right_start
1301313013
//
1301413014
// while(left < right){
1301513015
// ... do something here ...
13016-
// left += s1; // stride of left is s1>0
13017-
// right -= s2; // stride of right is -s2 (s2 > 0)
13016+
// left += s1; // stride of left is s1 (s1 > 0)
13017+
// right += s2; // stride of right is s2 (s2 < 0)
1301813018
// }
1301913019
//
13020-
// Here, left and right are converging somewhere in the middle.
1302113020

1302213021
const SCEV *RHSStart = RHSAddRec->getStart();
1302313022
const SCEV *RHSStride = RHSAddRec->getStepRecurrence(*this);
@@ -13028,7 +13027,6 @@ ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
1302813027
// Where, End = max(RHSStart, Start)
1302913028

1303013029
// Check if RHSStride < 0 and Stride - RHSStride will not overflow.
13031-
// FIXME: Can RHSStride be positive?
1303213030
if (isKnownNegative(RHSStride) &&
1303313031
willNotOverflow(Instruction::Sub, /*Signed=*/true, Stride,
1303413032
RHSStride)) {

llvm/test/Analysis/ScalarEvolution/pr92560.ll

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,35 @@ while.end:
102102
ret void
103103
}
104104

105+
; abs(left_stride) != abs(right_stride)
106+
define dso_local void @simple2() local_unnamed_addr {
107+
; CHECK-LABEL: 'simple2'
108+
; CHECK-NEXT: Classifying expressions for: @simple2
109+
; CHECK-NEXT: %right.08 = phi i32 [ 50, %entry ], [ %add2, %while.body ]
110+
; CHECK-NEXT: --> {50,+,-5}<nsw><%while.body> U: [25,51) S: [25,51) Exits: 25 LoopDispositions: { %while.body: Computable }
111+
; CHECK-NEXT: %left.07 = phi i32 [ 0, %entry ], [ %add, %while.body ]
112+
; CHECK-NEXT: --> {0,+,4}<nuw><nsw><%while.body> U: [0,21) S: [0,21) Exits: 20 LoopDispositions: { %while.body: Computable }
113+
; CHECK-NEXT: %add = add nuw nsw i32 %left.07, 4
114+
; CHECK-NEXT: --> {4,+,4}<nuw><nsw><%while.body> U: [4,25) S: [4,25) Exits: 24 LoopDispositions: { %while.body: Computable }
115+
; CHECK-NEXT: %add2 = add nsw i32 %right.08, -5
116+
; CHECK-NEXT: --> {45,+,-5}<nsw><%while.body> U: [20,46) S: [20,46) Exits: 20 LoopDispositions: { %while.body: Computable }
117+
; CHECK-NEXT: Determining loop execution counts for: @simple2
118+
; CHECK-NEXT: Loop %while.body: backedge-taken count is i32 5
119+
; CHECK-NEXT: Loop %while.body: constant max backedge-taken count is i32 5
120+
; CHECK-NEXT: Loop %while.body: symbolic max backedge-taken count is i32 5
121+
; CHECK-NEXT: Loop %while.body: Trip multiple is 6
122+
;
123+
entry:
124+
br label %while.body
105125

126+
while.body:
127+
%right.08 = phi i32 [ 50, %entry ], [ %add2, %while.body ]
128+
%left.07 = phi i32 [ 0, %entry ], [ %add, %while.body ]
129+
%add = add nuw nsw i32 %left.07, 4
130+
%add2 = add nsw i32 %right.08, -5
131+
%cmp = icmp slt i32 %add, %add2
132+
br i1 %cmp, label %while.body, label %while.end
133+
134+
while.end:
135+
ret void
136+
}

0 commit comments

Comments
 (0)