Skip to content

Commit e8b55cf

Browse files
committed
[SCEV] Apply loop guards when computing max BTC for arbitrary steps.
Similar other cases in the current function (e.g. when the step is 1 or -1), applying loop guards can lead to tighter upper bounds for the backedge-taken counts. Fixes PR52464. Reviewed By: reames, nikic Differential Revision: https://reviews.llvm.org/D113578
1 parent 9574da8 commit e8b55cf

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9669,9 +9669,12 @@ ScalarEvolution::howFarToZero(const SCEV *V, const Loop *L, bool ControlsExit,
96699669
// Solve the general equation.
96709670
const SCEV *E = SolveLinEquationWithOverflow(StepC->getAPInt(),
96719671
getNegativeSCEV(Start), *this);
9672-
const SCEV *M = E == getCouldNotCompute()
9673-
? E
9674-
: getConstant(getUnsignedRangeMax(E));
9672+
9673+
const SCEV *M = E;
9674+
if (E != getCouldNotCompute()) {
9675+
APInt MaxWithGuards = getUnsignedRangeMax(applyLoopGuards(E, L));
9676+
M = getConstant(APIntOps::umin(MaxWithGuards, getUnsignedRangeMax(E)));
9677+
}
96759678
return ExitLimit(E, M, false, Predicates);
96769679
}
96779680

llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info-rewrite-expressions.ll

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,13 @@ exit:
6464
ret i32 0
6565
}
6666

67-
; Test case from PR52464.
68-
define i32 @rewrite_zext_icmp_ne(i32 %N) {
69-
; CHECK-LABEL: Determining loop execution counts for: @rewrite_zext_icmp_ne
70-
; CHECK-NEXT: Loop %loop: backedge-taken count is ((-4 + (4 * ((4 + (zext i32 (-1 + (zext i2 (trunc i32 %N to i2) to i32))<nsw> to i64))<nuw><nsw> /u 4))<nuw><nsw>)<nsw> /u 4)
71-
; CHECK-NEXT: Loop %loop: max backedge-taken count is 1073741823
72-
; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is ((-4 + (4 * ((4 + (zext i32 (-1 + (zext i2 (trunc i32 %N to i2) to i32))<nsw> to i64))<nuw><nsw> /u 4))<nuw><nsw>)<nsw> /u 4)
67+
; Test case from PR52464. applyLoopGuards needs to apply information about %and
68+
; to %ext, which requires rewriting the zext.
69+
define i32 @rewrite_zext_with_info_from_icmp_ne(i32 %N) {
70+
; CHECK-LABEL: Determining loop execution counts for: @rewrite_zext_with_info_from_icmp_ne
71+
; CHECK-NEXT: Loop %loop: backedge-taken count is 0
72+
; CHECK-NEXT: Loop %loop: max backedge-taken count is 0
73+
; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is 0
7374
; CHECK-NEXT: Predicates:
7475
; CHECK-EMPTY:
7576
; CHECK-NEXT: Loop %loop: Trip multiple is 1
@@ -97,7 +98,7 @@ exit:
9798
ret i32 0
9899
}
99100

100-
; Similar to @rewrite_zext_icmp_ne, but the loop is not guarded by %and != 0,
101+
; Similar to @rewrite_zext_with_info_from_icmp_ne, but the loop is not guarded by %and != 0,
101102
; hence the subsequent subtraction may yield a negative number.
102103
define i32 @rewrite_zext_no_icmp_ne(i32 %N) {
103104
; CHECK-LABEL: Determining loop execution counts for: @rewrite_zext_no_icmp_ne

0 commit comments

Comments
 (0)