Skip to content

Commit 90d8241

Browse files
authored
[SCEV] Use loop guards when checking that RHS >= Start (#75039)
Loop guards tend to provide better results when it comes to reasoning about ranges than isLoopEntryGuardedByCond(). See the test change for the motivating case. I have retained both the loop guard check and the implied cond based check for now, though the latter only seems to impact a single test and only via side effects (nowrap flag calculation) at that.
1 parent c87eb63 commit 90d8241

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12910,7 +12910,11 @@ ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
1291012910
if (!BECount) {
1291112911
auto canProveRHSGreaterThanEqualStart = [&]() {
1291212912
auto CondGE = IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
12913-
if (isLoopEntryGuardedByCond(L, CondGE, OrigRHS, OrigStart))
12913+
const SCEV *GuardedRHS = applyLoopGuards(OrigRHS, L);
12914+
const SCEV *GuardedStart = applyLoopGuards(OrigStart, L);
12915+
12916+
if (isLoopEntryGuardedByCond(L, CondGE, OrigRHS, OrigStart) ||
12917+
isKnownPredicate(CondGE, GuardedRHS, GuardedStart))
1291412918
return true;
1291512919

1291612920
// (RHS > Start - 1) implies RHS >= Start.

llvm/test/Analysis/ScalarEvolution/trip-count.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@ leave:
127127
define void @non_zero_from_loop_guard(i16 %n) {
128128
; CHECK-LABEL: 'non_zero_from_loop_guard'
129129
; CHECK-NEXT: Determining loop execution counts for: @non_zero_from_loop_guard
130-
; CHECK-NEXT: Loop %loop: backedge-taken count is (-1 + (1 umax (%n /u 2)))<nsw>
130+
; CHECK-NEXT: Loop %loop: backedge-taken count is (-1 + (%n /u 2))<nsw>
131131
; CHECK-NEXT: Loop %loop: constant max backedge-taken count is 32766
132-
; CHECK-NEXT: Loop %loop: symbolic max backedge-taken count is (-1 + (1 umax (%n /u 2)))<nsw>
133-
; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (-1 + (1 umax (%n /u 2)))<nsw>
132+
; CHECK-NEXT: Loop %loop: symbolic max backedge-taken count is (-1 + (%n /u 2))<nsw>
133+
; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (-1 + (%n /u 2))<nsw>
134134
; CHECK-NEXT: Predicates:
135135
; CHECK-NEXT: Loop %loop: Trip multiple is 1
136136
;

0 commit comments

Comments
 (0)