]> granicus.if.org Git - llvm/commitdiff
[SCEV] Make howFarToZero max backedge-taken count check for precondition.
authorEli Friedman <efriedma@codeaurora.org>
Wed, 11 Jan 2017 21:07:15 +0000 (21:07 +0000)
committerEli Friedman <efriedma@codeaurora.org>
Wed, 11 Jan 2017 21:07:15 +0000 (21:07 +0000)
Refines max backedge-taken count if a loop like
"for (int i = 0; i != n; ++i) { /* body */ }" is rotated.

Differential Revision: https://reviews.llvm.org/D28536

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@291704 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/ScalarEvolution.cpp
test/Analysis/ScalarEvolution/max-trip-count.ll

index bd7e0e832f68e42096e93ed674d680566a81f689..791042c61fb7af01aaf5766a6dc51b3be4923d8c 100644 (file)
@@ -7207,6 +7207,23 @@ ScalarEvolution::howFarToZero(const SCEV *V, const Loop *L, bool ControlsExit,
   //   N = Distance (as unsigned)
   if (StepC->getValue()->equalsInt(1) || StepC->getValue()->isAllOnesValue()) {
     APInt MaxBECount = getUnsignedRange(Distance).getUnsignedMax();
+
+    // When a loop like "for (int i = 0; i != n; ++i) { /* body */ }" is rotated,
+    // we end up with a loop whose backedge-taken count is n - 1.  Detect this
+    // case, and see if we can improve the bound.
+    //
+    // Explicitly handling this here is necessary because getUnsignedRange
+    // isn't context-sensitive; it doesn't know that we only care about the
+    // range inside the loop.
+    const SCEV *Zero = getZero(Distance->getType());
+    const SCEV *One = getOne(Distance->getType());
+    const SCEV *DistancePlusOne = getAddExpr(Distance, One);
+    if (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_NE, DistancePlusOne, Zero)) {
+      // If Distance + 1 doesn't overflow, we can compute the maximum distance
+      // as "unsigned_max(Distance + 1) - 1".
+      ConstantRange CR = getUnsignedRange(DistancePlusOne);
+      MaxBECount = APIntOps::umin(MaxBECount, CR.getUnsignedMax() - 1);
+    }
     return ExitLimit(Distance, getConstant(MaxBECount), false, Predicates);
   }
 
index bee0ac90f05e6e69234421fdbe118650fe65e11d..d87e7d033a1e17eaa20d52f2974fd4b56cf2b9ec 100644 (file)
@@ -242,9 +242,8 @@ bar.exit:
   ret i32 0
 }
 
-; TODO: Improve count based on guard.
 ; CHECK-LABEL: @ne_max_trip_count_3
-; CHECK: Loop %for.body: max backedge-taken count is -1
+; CHECK: Loop %for.body: max backedge-taken count is 6
 define i32 @ne_max_trip_count_3(i32 %n) {
 entry:
   %masked = and i32 %n, 7
@@ -267,9 +266,8 @@ exit:
   ret i32 0
 }
 
-; TODO: Improve count based on guard.
 ; CHECK-LABEL: @ne_max_trip_count_4
-; CHECK: Loop %for.body: max backedge-taken count is -1
+; CHECK: Loop %for.body: max backedge-taken count is -2
 define i32 @ne_max_trip_count_4(i32 %n) {
 entry:
   %guard = icmp eq i32 %n, 0