[PATCH] D113578: [SCEV] Apply loop guards when computing max BTC for arbitrary steps.

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 10 09:33:49 PST 2021


fhahn created this revision.
fhahn added reviewers: reames, nikic, mkazantsev, efriedma.
Herald added a subscriber: hiraditya.
fhahn requested review of this revision.
Herald added a project: LLVM.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D113578

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


Index: llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info-rewrite-expressions.ll
===================================================================
--- llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info-rewrite-expressions.ll
+++ llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info-rewrite-expressions.ll
@@ -67,9 +67,9 @@
 ; 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)
+; 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
Index: llvm/lib/Analysis/ScalarEvolution.cpp
===================================================================
--- llvm/lib/Analysis/ScalarEvolution.cpp
+++ llvm/lib/Analysis/ScalarEvolution.cpp
@@ -9662,9 +9662,14 @@
   // 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));
+    assert(MaxWithGuards.ule(getUnsignedRangeMax(E)) &&
+           "applying loop guards unexpectedly pessimized max count");
+    M = getConstant(MaxWithGuards);
+  }
   return ExitLimit(E, M, false, Predicates);
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D113578.386208.patch
Type: text/x-patch
Size: 2121 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211110/2109f4d2/attachment.bin>


More information about the llvm-commits mailing list