summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Transforms
diff options
context:
space:
mode:
authorPhilip Reames <listmail@philipreames.com>2019-06-22 17:54:25 +0000
committerPhilip Reames <listmail@philipreames.com>2019-06-22 17:54:25 +0000
commit8deb84c8ef858e89dc418f133fb13ce5cb4ee329 (patch)
treef6066234f3ff928ed1aed9ad8f2e2b2d3d17307b /llvm/lib/Transforms
parenta5b83bc9e3b8e8945b55068c762bd6c73621a4b0 (diff)
downloadbcm5719-llvm-8deb84c8ef858e89dc418f133fb13ce5cb4ee329.tar.gz
bcm5719-llvm-8deb84c8ef858e89dc418f133fb13ce5cb4ee329.zip
Exploit a zero LoopExit count to eliminate loop exits
This turned out to be surprisingly effective. I was originally doing this just for completeness sake, but it seems like there are a lot of cases where SCEV's exit count reasoning is stronger than it's isKnownPredicate reasoning. Once this is in, I'm thinking about trying to build on the same infrastructure to eliminate provably untaken checks. There may be something generally interesting here. Differential Revision: https://reviews.llvm.org/D63618 llvm-svn: 364135
Diffstat (limited to 'llvm/lib/Transforms')
-rw-r--r--llvm/lib/Transforms/Scalar/IndVarSimplify.cpp16
1 files changed, 14 insertions, 2 deletions
diff --git a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
index 69dd973c114..57f246373f0 100644
--- a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
+++ b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
@@ -2724,9 +2724,21 @@ bool IndVarSimplify::run(Loop *L) {
if (isa<SCEVCouldNotCompute>(ExitCount))
continue;
- // Better to fold to true (TODO: do so!)
- if (ExitCount->isZero())
+ // If we know we'd exit on the first iteration, rewrite the exit to
+ // reflect this. This does not imply the loop must exit through this
+ // exit; there may be an earlier one taken on the first iteration.
+ // TODO: Given we know the backedge can't be taken, we should go ahead
+ // and break it. Or at least, kill all the header phis and simplify.
+ if (ExitCount->isZero()) {
+ auto *BI = cast<BranchInst>(ExitingBB->getTerminator());
+ bool ExitIfTrue = !L->contains(*succ_begin(ExitingBB));
+ auto *NewCond = ExitIfTrue ?
+ ConstantInt::getTrue(BI->getCondition()->getType()) :
+ ConstantInt::getFalse(BI->getCondition()->getType());
+ BI->setCondition(NewCond);
+ Changed = true;
continue;
+ }
PHINode *IndVar = FindLoopCounter(L, ExitingBB, ExitCount, SE, DT);
if (!IndVar)
OpenPOWER on IntegriCloud