[llvm] e8b55cf - [SCEV] Apply loop guards when computing max BTC for arbitrary steps.
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 17 03:01:08 PST 2021
Author: Florian Hahn
Date: 2021-11-17T11:00:49Z
New Revision: e8b55cf7b70a695d158d3e172d1124e36d81a8fc
URL: https://github.com/llvm/llvm-project/commit/e8b55cf7b70a695d158d3e172d1124e36d81a8fc
DIFF: https://github.com/llvm/llvm-project/commit/e8b55cf7b70a695d158d3e172d1124e36d81a8fc.diff
LOG: [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
Added:
Modified:
llvm/lib/Analysis/ScalarEvolution.cpp
llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info-rewrite-expressions.ll
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index fbfaac8f3b536..95e269935ec5a 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -9669,9 +9669,12 @@ ScalarEvolution::howFarToZero(const SCEV *V, const Loop *L, bool ControlsExit,
// Solve the general equation.
const SCEV *E = SolveLinEquationWithOverflow(StepC->getAPInt(),
getNegativeSCEV(Start), *this);
- const SCEV *M = E == getCouldNotCompute()
- ? E
- : getConstant(getUnsignedRangeMax(E));
+
+ const SCEV *M = E;
+ if (E != getCouldNotCompute()) {
+ APInt MaxWithGuards = getUnsignedRangeMax(applyLoopGuards(E, L));
+ M = getConstant(APIntOps::umin(MaxWithGuards, getUnsignedRangeMax(E)));
+ }
return ExitLimit(E, M, false, Predicates);
}
diff --git a/llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info-rewrite-expressions.ll b/llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info-rewrite-expressions.ll
index de8ca288a9bd0..39af887d9ff1a 100644
--- a/llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info-rewrite-expressions.ll
+++ b/llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info-rewrite-expressions.ll
@@ -64,12 +64,13 @@ exit:
ret i32 0
}
-; Test case from PR52464.
-define i32 @rewrite_zext_icmp_ne(i32 %N) {
-; CHECK-LABEL: Determining loop execution counts for: @rewrite_zext_icmp_ne
-; 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)
-; CHECK-NEXT: Loop %loop: max backedge-taken count is 1073741823
-; 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)
+; Test case from PR52464. applyLoopGuards needs to apply information about %and
+; to %ext, which requires rewriting the zext.
+define i32 @rewrite_zext_with_info_from_icmp_ne(i32 %N) {
+; CHECK-LABEL: Determining loop execution counts for: @rewrite_zext_with_info_from_icmp_ne
+; CHECK-NEXT: Loop %loop: backedge-taken count is 0
+; CHECK-NEXT: Loop %loop: max backedge-taken count is 0
+; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is 0
; CHECK-NEXT: Predicates:
; CHECK-EMPTY:
; CHECK-NEXT: Loop %loop: Trip multiple is 1
@@ -97,7 +98,7 @@ exit:
ret i32 0
}
-; Similar to @rewrite_zext_icmp_ne, but the loop is not guarded by %and != 0,
+; Similar to @rewrite_zext_with_info_from_icmp_ne, but the loop is not guarded by %and != 0,
; hence the subsequent subtraction may yield a negative number.
define i32 @rewrite_zext_no_icmp_ne(i32 %N) {
; CHECK-LABEL: Determining loop execution counts for: @rewrite_zext_no_icmp_ne
More information about the llvm-commits
mailing list