[PATCH] D47899: [SCEV] Estimate upper bound of trip count from predicated backedge count

Krzysztof Parzyszek via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 7 12:04:27 PDT 2018


kparzysz created this revision.
kparzysz added a reviewer: sanjoy.

This loop came from a customer code:

  struct S {
    unsigned X;
    int Y;
  };
  
  int fred(int A, int B, struct S *P) {
    int C = A - 1;
    while ((B & 3) != 0) {
      P->X = C * 128;
      P->Y = 0;
      P++;
      B++;
    }
    return 0;
  }

Regardless of what `B` is, the loop will execute at most 4 times, however the upper bound returned from `getSmallConstantMaxTripCount` is `CouldNotCompute`.  This loop ends up getting interleaved by the loop vectorizer, which causes a needless code growth.

The maximum can actually be computed from the predicated backedge taken count, and patch uses that information to calculate another estimate of the upper bound.

As is, this patch will break a few lit tests.  Right now I just want to get initial feedback on whether this is the right approach.


Repository:
  rL LLVM

https://reviews.llvm.org/D47899

Files:
  lib/Analysis/ScalarEvolution.cpp


Index: lib/Analysis/ScalarEvolution.cpp
===================================================================
--- lib/Analysis/ScalarEvolution.cpp
+++ lib/Analysis/ScalarEvolution.cpp
@@ -6387,7 +6387,17 @@
 unsigned ScalarEvolution::getSmallConstantMaxTripCount(const Loop *L) {
   const auto *MaxExitCount =
       dyn_cast<SCEVConstant>(getMaxBackedgeTakenCount(L));
-  return getConstantTripCount(MaxExitCount);
+  if (unsigned C = getConstantTripCount(MaxExitCount))
+    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) {


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


More information about the llvm-commits mailing list