summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Analysis/ScalarEvolution.cpp
diff options
context:
space:
mode:
authorAndrew Trick <atrick@apple.com>2014-05-22 00:37:03 +0000
committerAndrew Trick <atrick@apple.com>2014-05-22 00:37:03 +0000
commite255359b57d6fe66c45ff1bda2c89a3efa02d5ba (patch)
treea793a4a1a514307bcfb5011135a8c8551a7d6765 /llvm/lib/Analysis/ScalarEvolution.cpp
parent328da5527eca8c5bfc349c8ad0b093f04bb79de9 (diff)
downloadbcm5719-llvm-e255359b57d6fe66c45ff1bda2c89a3efa02d5ba.tar.gz
bcm5719-llvm-e255359b57d6fe66c45ff1bda2c89a3efa02d5ba.zip
Fix a bug in SCEV's backedge taken count computation from my prior fix in Jan.
This has to do with the trip count computation for loops with multiple exits, which is quite subtle. Most passes just ask for a single trip count number, so we must be conservative assuming any exit could be taken. Normally, we rely on the "exact" trip count, which was correctly given as "unknown". However, SCEV also gives a "max" back-edge taken count. The loops max BE taken count is conservatively a maximum over the max of each exit's non-exiting iterations count. Note that some exit tests can be skipped so the max loop back-edge taken count can actually exceed the max non-exiting iterations for some exits. However, when we know the loop *latch* cannot be skipped, we can directly use its max taken count disregarding other exits. I previously took the minimum here without checking whether the other exit could be skipped. The correct, and simpler thing to do here is just to directly use the loop latch's max non-exiting iterations as the loops max back-edge count. In the problematic test case, the first loop exit had a max of zero non-exiting iterations, but could be skipped. The loop latch was known not to be skipped but had max of one non-exiting iteration. We incorrectly claimed the loop back-edge could be taken zero times, when it is actually taken one time. Fixes Loop %for.body.i: <multiple exits> Unpredictable backedge-taken count. Loop %for.body.i: max backedge-taken count is 1. llvm-svn: 209358
Diffstat (limited to 'llvm/lib/Analysis/ScalarEvolution.cpp')
-rw-r--r--llvm/lib/Analysis/ScalarEvolution.cpp14
1 files changed, 6 insertions, 8 deletions
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index cef9966f9e8..dad8e07dadb 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -4413,7 +4413,7 @@ ScalarEvolution::ComputeBackedgeTakenCount(const Loop *L) {
const SCEV *MaxBECount = getCouldNotCompute();
bool CouldComputeBECount = true;
BasicBlock *Latch = L->getLoopLatch(); // may be NULL.
- const SCEV *LatchMaxCount = nullptr;
+ bool LatchMustExit = false;
SmallVector<std::pair<BasicBlock *, const SCEV *>, 4> ExitCounts;
for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) {
ExitLimit EL = ComputeExitLimit(L, ExitingBlocks[i]);
@@ -4431,16 +4431,14 @@ ScalarEvolution::ComputeBackedgeTakenCount(const Loop *L) {
// 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)
- LatchMaxCount = EL.Max;
- else
+ if (EL.MustExit && ExitingBlocks[i] == Latch) {
+ MaxBECount = EL.Max;
+ LatchMustExit = true;
+ }
+ else if (!LatchMustExit)
MaxBECount = getUMaxFromMismatchedTypes(MaxBECount, EL.Max);
}
}
- // Be more precise in the easy case of a loop latch that must exit.
- if (LatchMaxCount) {
- MaxBECount = getUMinFromMismatchedTypes(MaxBECount, LatchMaxCount);
- }
return BackedgeTakenInfo(ExitCounts, CouldComputeBECount, MaxBECount);
}
OpenPOWER on IntegriCloud