aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOwen Anderson <resistor@mac.com>2008-05-16 04:32:45 +0000
committerOwen Anderson <resistor@mac.com>2008-05-16 04:32:45 +0000
commit0db198d7587951f47e9cc93db1fd5e6175ceb695 (patch)
tree457f0235575a178548bddf6809cb81896e389617
parentd870b9a4e35b9c22c491eaa4ec15351ce50f1a5f (diff)
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
-rw-r--r--lib/Transforms/Scalar/LoopDeletion.cpp60
1 files changed, 32 insertions, 28 deletions
diff --git a/lib/Transforms/Scalar/LoopDeletion.cpp b/lib/Transforms/Scalar/LoopDeletion.cpp
index c26a66c721..4073a7775d 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();
}