summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew Trick <atrick@apple.com>2014-05-23 19:47:13 +0000
committerAndrew Trick <atrick@apple.com>2014-05-23 19:47:13 +0000
commit839e30b2c07347b1538587693e7ed014c345b74b (patch)
tree9f30af90b707492ad5506d48b6b777ebcd973a4e
parentfe2413692f5f1247053ab1ec9cac0be48feb3572 (diff)
downloadbcm5719-llvm-839e30b2c07347b1538587693e7ed014c345b74b.tar.gz
bcm5719-llvm-839e30b2c07347b1538587693e7ed014c345b74b.zip
Fix and improve SCEV ComputeBackedgeTankCount.
This is a follow-up to r209358: PR19799: Indvars miscompile due to an incorrect max backedge taken count from SCEV. That fix was incomplete as pointed out by Arnold and Michael Z. The code was also too confusing. It needed a careful rewrite with more unit tests. This version will also happen to optimize more cases. <rdar://17005101> PR19799: Indvars miscompile... llvm-svn: 209545
-rw-r--r--llvm/lib/Analysis/ScalarEvolution.cpp65
-rw-r--r--llvm/test/Analysis/ScalarEvolution/max-trip-count.ll56
2 files changed, 102 insertions, 19 deletions
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index d27afb09cf3..0c864d840f1 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -4409,36 +4409,63 @@ ScalarEvolution::ComputeBackedgeTakenCount(const Loop *L) {
SmallVector<BasicBlock *, 8> ExitingBlocks;
L->getExitingBlocks(ExitingBlocks);
- // Examine all exits and pick the most conservative values.
- const SCEV *MaxBECount = getCouldNotCompute();
+ SmallVector<std::pair<BasicBlock *, const SCEV *>, 4> ExitCounts;
bool CouldComputeBECount = true;
BasicBlock *Latch = L->getLoopLatch(); // may be NULL.
- bool LatchMustExit = false;
- SmallVector<std::pair<BasicBlock *, const SCEV *>, 4> ExitCounts;
+ const SCEV *MustExitMaxBECount = nullptr;
+ const SCEV *MayExitMaxBECount = nullptr;
+
+ // Compute the ExitLimit for each loop exit. Use this to populate ExitCounts
+ // and compute maxBECount.
for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) {
- ExitLimit EL = ComputeExitLimit(L, ExitingBlocks[i]);
+ BasicBlock *ExitBB = ExitingBlocks[i];
+ ExitLimit EL = ComputeExitLimit(L, ExitBB);
+
+ // 1. For each exit that can be computed, add an entry to ExitCounts.
+ // CouldComputeBECount is true only if all exits can be computed.
if (EL.Exact == getCouldNotCompute())
// We couldn't compute an exact value for this exit, so
// we won't be able to compute an exact value for the loop.
CouldComputeBECount = false;
else
- ExitCounts.push_back(std::make_pair(ExitingBlocks[i], EL.Exact));
-
- if (MaxBECount == getCouldNotCompute())
- MaxBECount = EL.Max;
- else if (EL.Max != getCouldNotCompute()) {
- // We cannot take the "min" MaxBECount, because non-unit stride loops may
- // skip some loop tests. Taking the max over the exits is sufficiently
- // conservative. TODO: We could do better taking into consideration
- // non-latch exits that dominate the latch.
- if (EL.MustExit && ExitingBlocks[i] == Latch) {
- MaxBECount = EL.Max;
- LatchMustExit = true;
+ ExitCounts.push_back(std::make_pair(ExitBB, EL.Exact));
+
+ // 2. Derive the loop's MaxBECount from each exit's max number of
+ // non-exiting iterations. Partition the loop exits into two kinds:
+ // LoopMustExits and LoopMayExits.
+ //
+ // A LoopMustExit meets two requirements:
+ //
+ // (a) Its ExitLimit.MustExit flag must be set which indicates that the exit
+ // test condition cannot be skipped (the tested variable has unit stride or
+ // the test is less-than or greater-than, rather than a strict inequality).
+ //
+ // (b) It must dominate the loop latch, hence must be tested on every loop
+ // iteration.
+ //
+ // If any computable LoopMustExit is found, then MaxBECount is the minimum
+ // EL.Max of computable LoopMustExits. Otherwise, MaxBECount is
+ // conservatively the maximum EL.Max, where CouldNotCompute is considered
+ // greater than any computable EL.Max.
+ if (EL.MustExit && EL.Max != getCouldNotCompute() && Latch &&
+ DT->dominates(ExitBB, Latch)) {
+ if (!MustExitMaxBECount)
+ MustExitMaxBECount = EL.Max;
+ else {
+ MustExitMaxBECount =
+ getUMinFromMismatchedTypes(MustExitMaxBECount, EL.Max);
+ }
+ } else if (MayExitMaxBECount != getCouldNotCompute()) {
+ if (!MayExitMaxBECount || EL.Max == getCouldNotCompute())
+ MayExitMaxBECount = EL.Max;
+ else {
+ MayExitMaxBECount =
+ getUMaxFromMismatchedTypes(MayExitMaxBECount, EL.Max);
}
- else if (!LatchMustExit)
- MaxBECount = getUMaxFromMismatchedTypes(MaxBECount, EL.Max);
}
}
+ const SCEV *MaxBECount = MustExitMaxBECount ? MustExitMaxBECount :
+ (MayExitMaxBECount ? MayExitMaxBECount : getCouldNotCompute());
return BackedgeTakenInfo(ExitCounts, CouldComputeBECount, MaxBECount);
}
diff --git a/llvm/test/Analysis/ScalarEvolution/max-trip-count.ll b/llvm/test/Analysis/ScalarEvolution/max-trip-count.ll
index 43a54b4f303..d67d2fab2e3 100644
--- a/llvm/test/Analysis/ScalarEvolution/max-trip-count.ll
+++ b/llvm/test/Analysis/ScalarEvolution/max-trip-count.ll
@@ -124,3 +124,59 @@ for.cond.i: ; preds = %for.body.i
bar.exit: ; preds = %for.cond.i, %for.body.i
ret i32 0
}
+
+; Here we have a must-exit loop latch that is not computabe and a
+; may-exit early exit that can only have one non-exiting iteration
+; before the check is forever skipped.
+;
+; CHECK-LABEL: @cannot_compute_mustexit
+; CHECK: Loop %for.body.i: <multiple exits> Unpredictable backedge-taken count.
+; CHECK: Loop %for.body.i: Unpredictable max backedge-taken count.
+@b = common global i32 0, align 4
+
+define i32 @cannot_compute_mustexit() {
+entry:
+ store i32 -1, i32* @a, align 4
+ br label %for.body.i
+
+for.body.i: ; preds = %for.cond.i, %entry
+ %storemerge1.i = phi i32 [ -1, %entry ], [ %add.i.i, %for.cond.i ]
+ %tobool.i = icmp eq i32 %storemerge1.i, 0
+ %add.i.i = add nsw i32 %storemerge1.i, 2
+ br i1 %tobool.i, label %bar.exit, label %for.cond.i
+
+for.cond.i: ; preds = %for.body.i
+ store i32 %add.i.i, i32* @a, align 4
+ %ld = load volatile i32* @b
+ %cmp.i = icmp ne i32 %ld, 0
+ br i1 %cmp.i, label %for.body.i, label %bar.exit
+
+bar.exit: ; preds = %for.cond.i, %for.body.i
+ ret i32 0
+}
+
+; This loop has two must-exits, both of with dominate the latch. The
+; MaxBECount should be the minimum of them.
+;
+; CHECK-LABEL: @two_mustexit
+; CHECK: Loop %for.body.i: <multiple exits> Unpredictable backedge-taken count.
+; CHECK: Loop %for.body.i: max backedge-taken count is 1
+define i32 @two_mustexit() {
+entry:
+ store i32 -1, i32* @a, align 4
+ br label %for.body.i
+
+for.body.i: ; preds = %for.cond.i, %entry
+ %storemerge1.i = phi i32 [ -1, %entry ], [ %add.i.i, %for.cond.i ]
+ %tobool.i = icmp sgt i32 %storemerge1.i, 0
+ %add.i.i = add nsw i32 %storemerge1.i, 2
+ br i1 %tobool.i, label %bar.exit, label %for.cond.i
+
+for.cond.i: ; preds = %for.body.i
+ store i32 %add.i.i, i32* @a, align 4
+ %cmp.i = icmp slt i32 %storemerge1.i, 3
+ br i1 %cmp.i, label %for.body.i, label %bar.exit
+
+bar.exit: ; preds = %for.cond.i, %for.body.i
+ ret i32 0
+}
OpenPOWER on IntegriCloud