diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp --- a/llvm/lib/Analysis/ScalarEvolution.cpp +++ b/llvm/lib/Analysis/ScalarEvolution.cpp @@ -10628,8 +10628,13 @@ const SCEV *Start = IV->getStart(); const SCEV *End = RHS; - if (!isLoopEntryGuardedByCond(L, Cond, getAddExpr(Start, Stride), RHS)) - End = IsSigned ? getSMinExpr(RHS, Start) : getUMinExpr(RHS, Start); + if (!isLoopEntryGuardedByCond(L, Cond, getAddExpr(Start, Stride), RHS)) { + if (isLoopEntryGuardedByCond( + L, IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE, Start, RHS)) + End = RHS; + else + End = IsSigned ? getSMinExpr(RHS, Start) : getUMinExpr(RHS, Start); + } const SCEV *BECount = computeBECount(getMinusSCEV(Start, End), Stride, false); diff --git a/llvm/test/Analysis/ScalarEvolution/pr46939-trip-count-count-down.ll b/llvm/test/Analysis/ScalarEvolution/pr46939-trip-count-count-down.ll --- a/llvm/test/Analysis/ScalarEvolution/pr46939-trip-count-count-down.ll +++ b/llvm/test/Analysis/ScalarEvolution/pr46939-trip-count-count-down.ll @@ -6,13 +6,13 @@ ; CHECK-LABEL: 'reverse_loop' ; CHECK-NEXT: Classifying expressions for: @reverse_loop ; CHECK-NEXT: %i.011 = phi i32 [ %n, %for.body.lr.ph ], [ %dec, %for.body ] -; CHECK-NEXT: --> {%n,+,-1}<%for.body> U: full-set S: full-set Exits: (0 smin %n) LoopDispositions: { %for.body: Computable } +; CHECK-NEXT: --> {%n,+,-1}<%for.body> U: full-set S: full-set Exits: 0 LoopDispositions: { %for.body: Computable } ; CHECK-NEXT: %dec = add nsw i32 %i.011, -1 -; CHECK-NEXT: --> {(-1 + %n),+,-1}<%for.body> U: full-set S: full-set Exits: (-1 + (0 smin %n)) LoopDispositions: { %for.body: Computable } +; CHECK-NEXT: --> {(-1 + %n),+,-1}<%for.body> U: full-set S: full-set Exits: -1 LoopDispositions: { %for.body: Computable } ; CHECK-NEXT: Determining loop execution counts for: @reverse_loop -; CHECK-NEXT: Loop %for.body: backedge-taken count is ((-1 * (0 smin %n)) + %n) +; CHECK-NEXT: Loop %for.body: backedge-taken count is %n ; CHECK-NEXT: Loop %for.body: max backedge-taken count is 2147483647 -; CHECK-NEXT: Loop %for.body: Predicated backedge-taken count is ((-1 * (0 smin %n)) + %n) +; CHECK-NEXT: Loop %for.body: Predicated backedge-taken count is %n ; CHECK-NEXT: Predicates: ; CHECK: Loop %for.body: Trip multiple is 1 ; diff --git a/llvm/test/Transforms/HardwareLoops/scalar-while.ll b/llvm/test/Transforms/HardwareLoops/scalar-while.ll --- a/llvm/test/Transforms/HardwareLoops/scalar-while.ll +++ b/llvm/test/Transforms/HardwareLoops/scalar-while.ll @@ -76,18 +76,14 @@ ; CHECK-GUARD: br i1 %cmp4, label %while.end, label %while.body.preheader ; CHECK-GUARD: while.body.preheader: ; CHECK-GUARD: [[ADD:%[^ ]+]] = add i32 %i, 1 -; CHECK-GUARD: [[SEL:%[^ ]+]] = icmp slt i32 %N, %i -; CHECK-GUARD: [[MIN:%[^ ]+]] = select i1 [[SEL]], i32 %N, i32 %i -; CHECK-GUARD: [[COUNT:%[^ ]+]] = sub i32 [[ADD]], [[MIN]] +; CHECK-GUARD: [[COUNT:%[^ ]+]] = sub i32 [[ADD]], %N ; CHECK-GUARD: call void @llvm.set.loop.iterations.i32(i32 [[COUNT]]) ; CHECK-GUARD: br label %while.body ; CHECK-LABEL: while_gte ; CHECK: while.body.preheader: ; CHECK: [[ADD:%[^ ]+]] = add i32 %i, 1 -; CHECK: [[SEL:%[^ ]+]] = icmp slt i32 %N, %i -; CHECK: [[MIN:%[^ ]+]] = select i1 [[SEL]], i32 %N, i32 %i -; CHECK: [[COUNT:%[^ ]+]] = sub i32 [[ADD]], [[MIN]] +; CHECK: [[COUNT:%[^ ]+]] = sub i32 [[ADD]], %N ; CHECK: call void @llvm.set.loop.iterations.i32(i32 [[COUNT]]) ; CHECK: br label %while.body