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
This commit is contained in:
Owen Anderson 2008-05-16 04:32:45 +00:00
parent 00c7d82391
commit ad5f211b48
1 changed files with 32 additions and 28 deletions

View File

@ -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();
}