diff options
author | Owen Anderson <resistor@mac.com> | 2008-05-16 04:32:45 +0000 |
---|---|---|
committer | Owen Anderson <resistor@mac.com> | 2008-05-16 04:32:45 +0000 |
commit | c4a2cdb07c90e7c38157a528fa2c3d1759e14347 (patch) | |
tree | 457f0235575a178548bddf6809cb81896e389617 /lib/Transforms/Scalar/LoopDeletion.cpp | |
parent | 4c58400a0bc1dfe8b5d087048a5ee7a4e9e6e6d9 (diff) | |
download | external_llvm-c4a2cdb07c90e7c38157a528fa2c3d1759e14347.zip external_llvm-c4a2cdb07c90e7c38157a528fa2c3d1759e14347.tar.gz external_llvm-c4a2cdb07c90e7c38157a528fa2c3d1759e14347.tar.bz2 |
Clean ups for loop deletion based on Chris' feedback.
Also, use SCEV to determine the trip count of the loop, which is more powerful
and accurate that Loop::getTripCount.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@51179 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms/Scalar/LoopDeletion.cpp')
-rw-r--r-- | lib/Transforms/Scalar/LoopDeletion.cpp | 60 |
1 files changed, 32 insertions, 28 deletions
diff --git a/lib/Transforms/Scalar/LoopDeletion.cpp b/lib/Transforms/Scalar/LoopDeletion.cpp index c26a66c..4073a77 100644 --- a/lib/Transforms/Scalar/LoopDeletion.cpp +++ b/lib/Transforms/Scalar/LoopDeletion.cpp @@ -18,6 +18,7 @@ #include "llvm/Transforms/Scalar.h" #include "llvm/Analysis/LoopPass.h" +#include "llvm/Analysis/ScalarEvolution.h" #include "llvm/ADT/Statistic.h" #include "llvm/ADT/SmallVector.h" @@ -41,11 +42,13 @@ namespace { bool IsLoopInvariantInst(Instruction *I, Loop* L); virtual void getAnalysisUsage(AnalysisUsage& AU) const { + AU.addRequired<ScalarEvolution>(); AU.addRequired<DominatorTree>(); AU.addRequired<LoopInfo>(); AU.addRequiredID(LoopSimplifyID); AU.addRequiredID(LCSSAID); + AU.addPreserved<ScalarEvolution>(); AU.addPreserved<DominatorTree>(); AU.addPreserved<LoopInfo>(); AU.addPreservedID(LoopSimplifyID); @@ -62,10 +65,10 @@ LoopPass* llvm::createLoopDeletionPass() { } /// SingleDominatingExit - Checks that there is only a single blocks that -/// branches out of the loop, and that it also dominates the latch block. Loops +/// branches out of the loop, and that it also g the latch block. Loops /// with multiple or non-latch-dominating exiting blocks could be dead, but we'd /// have to do more extensive analysis to make sure, for instance, that the -/// control flow logic involves was or could be made loop-invariant. +/// control flow logic involved was or could be made loop-invariant. bool LoopDeletion::SingleDominatingExit(Loop* L, SmallVector<BasicBlock*, 4>& exitingBlocks) { @@ -77,10 +80,7 @@ bool LoopDeletion::SingleDominatingExit(Loop* L, return false; DominatorTree& DT = getAnalysis<DominatorTree>(); - if (DT.dominates(exitingBlocks[0], latch)) - return true; - else - return false; + return DT.dominates(exitingBlocks[0], latch); } /// IsLoopInvariantInst - Checks if an instruction is invariant with respect to @@ -98,7 +98,7 @@ bool LoopDeletion::IsLoopInvariantInst(Instruction *I, Loop* L) { for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) if (!L->isLoopInvariant(I->getOperand(i))) return false; - + // If we got this far, the instruction is loop invariant! return true; } @@ -153,19 +153,6 @@ bool LoopDeletion::IsLoopDead(Loop* L, /// NOTE: This entire process relies pretty heavily on LoopSimplify and LCSSA /// in order to make various safety checks work. bool LoopDeletion::runOnLoop(Loop* L, LPPassManager& LPM) { - SmallVector<BasicBlock*, 4> exitingBlocks; - L->getExitingBlocks(exitingBlocks); - - SmallVector<BasicBlock*, 4> exitBlocks; - L->getUniqueExitBlocks(exitBlocks); - - // We require that the loop only have a single exit block. Otherwise, we'd - // be in the situation of needing to be able to solve statically which exit - // block will be branced to, or trying to preserve the branching logic in - // a loop invariant manner. - if (exitBlocks.size() != 1) - return false; - // We can only remove the loop if there is a preheader that we can // branch from after removing it. BasicBlock* preheader = L->getLoopPreheader(); @@ -177,9 +164,17 @@ bool LoopDeletion::runOnLoop(Loop* L, LPPassManager& LPM) { if (L->begin() != L->end()) return false; - // Don't remove loops for which we can't solve the trip count. - // They could be infinite, in which case we'd be changing program behavior. - if (!L->getTripCount()) + SmallVector<BasicBlock*, 4> exitingBlocks; + L->getExitingBlocks(exitingBlocks); + + SmallVector<BasicBlock*, 4> exitBlocks; + L->getUniqueExitBlocks(exitBlocks); + + // We require that the loop only have a single exit block. Otherwise, we'd + // be in the situation of needing to be able to solve statically which exit + // block will be branched to, or trying to preserve the branching logic in + // a loop invariant manner. + if (exitBlocks.size() != 1) return false; // Loops with multiple exits or exits that don't dominate the latch @@ -191,6 +186,13 @@ bool LoopDeletion::runOnLoop(Loop* L, LPPassManager& LPM) { if (!IsLoopDead(L, exitingBlocks, exitBlocks)) return false; + // Don't remove loops for which we can't solve the trip count. + // They could be infinite, in which case we'd be changing program behavior. + ScalarEvolution& SE = getAnalysis<ScalarEvolution>(); + SCEVHandle S = SE.getIterationCount(L); + if (isa<SCEVCouldNotCompute>(S)) + return false; + // Now that we know the removal is safe, remove the loop by changing the // branch from the preheader to go to the single exit block. BasicBlock* exitBlock = exitBlocks[0]; @@ -239,13 +241,15 @@ bool LoopDeletion::runOnLoop(Loop* L, LPPassManager& LPM) { ChildNodes.clear(); DT.eraseNode(*LI); - // Drop all references between the instructions and the block so - // that we don't have reference counting problems later. + // Remove instructions that we're deleting from ScalarEvolution. for (BasicBlock::iterator BI = (*LI)->begin(), BE = (*LI)->end(); - BI != BE; ++BI) { - BI->dropAllReferences(); - } + BI != BE; ++BI) + SE.deleteValueFromRecords(BI); + + SE.deleteValueFromRecords(*LI); + // Remove the block from the reference counting scheme, so that we can + // delete it freely later. (*LI)->dropAllReferences(); } |