From ad5f211b48fde5621325c3f7d18fbdf0e680aa9d Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Fri, 16 May 2008 04:32:45 +0000 Subject: [PATCH] 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. llvm-svn: 51179 --- llvm/lib/Transforms/Scalar/LoopDeletion.cpp | 60 +++++++++++---------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/llvm/lib/Transforms/Scalar/LoopDeletion.cpp b/llvm/lib/Transforms/Scalar/LoopDeletion.cpp index c26a66c721ec..4073a7775d27 100644 --- a/llvm/lib/Transforms/Scalar/LoopDeletion.cpp +++ b/llvm/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(); AU.addRequired(); AU.addRequired(); AU.addRequiredID(LoopSimplifyID); AU.addRequiredID(LCSSAID); + AU.addPreserved(); AU.addPreserved(); AU.addPreserved(); 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& exitingBlocks) { @@ -77,10 +80,7 @@ bool LoopDeletion::SingleDominatingExit(Loop* L, return false; DominatorTree& DT = getAnalysis(); - 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 exitingBlocks; - L->getExitingBlocks(exitingBlocks); - - SmallVector 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 exitingBlocks; + L->getExitingBlocks(exitingBlocks); + + SmallVector 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(); + SCEVHandle S = SE.getIterationCount(L); + if (isa(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(); }