[PATCH] D47899: [SCEV] Implement (non-exact) getSmallConstantTripCountUpperBound

Krzysztof Parzyszek via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 7 13:31:54 PDT 2018


kparzysz updated this revision to Diff 150400.
kparzysz retitled this revision from "[SCEV] Estimate upper bound of trip count from predicated backedge count" to "[SCEV] Implement (non-exact) getSmallConstantTripCountUpperBound".
kparzysz edited the summary of this revision.
kparzysz added a comment.

Added a new function to ScalarEvolution to estimate the trip count upper bound.


Repository:
  rL LLVM

https://reviews.llvm.org/D47899

Files:
  include/llvm/Analysis/ScalarEvolution.h
  lib/Analysis/ScalarEvolution.cpp
  lib/Transforms/Vectorize/LoopVectorize.cpp


Index: lib/Transforms/Vectorize/LoopVectorize.cpp
===================================================================
--- lib/Transforms/Vectorize/LoopVectorize.cpp
+++ lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -5148,7 +5148,7 @@
     return 1;
 
   // Do not interleave loops with a relatively small trip count.
-  unsigned TC = PSE.getSE()->getSmallConstantTripCount(TheLoop);
+  unsigned TC = PSE.getSE()->getSmallConstantTripCountUpperBound(TheLoop);
   if (TC > 1 && TC < TinyTripCountInterleaveThreshold)
     return 1;
 
@@ -7346,7 +7346,7 @@
     }
   }
   if (!HasExpectedTC) {
-    ExpectedTC = SE->getSmallConstantMaxTripCount(L);
+    ExpectedTC = SE->getSmallConstantTripCountUpperBound(L);
     HasExpectedTC = (ExpectedTC > 0);
   }
 
Index: lib/Analysis/ScalarEvolution.cpp
===================================================================
--- lib/Analysis/ScalarEvolution.cpp
+++ lib/Analysis/ScalarEvolution.cpp
@@ -6390,6 +6390,27 @@
   return getConstantTripCount(MaxExitCount);
 }
 
+unsigned ScalarEvolution::getSmallConstantTripCountUpperBound(const Loop *L) {
+  if (const SCEVConstant *TakenCount =
+          dyn_cast<SCEVConstant>(getBackedgeTakenCount(L))) {
+    if (unsigned C = getConstantTripCount(TakenCount))
+      return C;
+  }
+
+  if (unsigned C = getSmallConstantMaxTripCount(L))
+    return C;
+
+  SCEVUnionPredicate Ps;
+  const SCEV *PredBEC = getPredicatedBackedgeTakenCount(L, Ps);
+  if (PredBEC != getCouldNotCompute()) {
+    APInt S = getUnsignedRange(PredBEC).getSetSize();
+    if (S.getActiveBits() < 32)
+      return S.getZExtValue();
+  }
+
+  return 0;
+}
+
 unsigned ScalarEvolution::getSmallConstantTripMultiple(const Loop *L) {
   if (BasicBlock *ExitingBB = L->getExitingBlock())
     return getSmallConstantTripMultiple(L, ExitingBB);
Index: include/llvm/Analysis/ScalarEvolution.h
===================================================================
--- include/llvm/Analysis/ScalarEvolution.h
+++ include/llvm/Analysis/ScalarEvolution.h
@@ -726,6 +726,11 @@
   unsigned getSmallConstantTripMultiple(const Loop *L,
                                         BasicBlock *ExitingBlock);
 
+  /// Returns the upper bound of the loop trip count as a normal unsigned
+  /// value. The upper bound may be larger than the actual trip count.
+  /// Returns 0 if the trip count is unknown or cannot be estimated.
+  unsigned getSmallConstantTripCountUpperBound(const Loop *L);
+
   /// Get the expression for the number of loop iterations for which this loop
   /// is guaranteed not to exit via ExitingBlock. Otherwise return
   /// SCEVCouldNotCompute.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D47899.150400.patch
Type: text/x-patch
Size: 2612 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180607/0e579a82/attachment.bin>


More information about the llvm-commits mailing list