Owen Anderson | 0ff7708 | 2008-04-29 00:38:34 +0000 | [diff] [blame^] | 1 | //===- DeadLoopElimination.cpp - Dead Loop Elimination Pass ---------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Dead Loop Elimination Pass. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "dead-loop" |
| 15 | |
| 16 | #include "llvm/Transforms/Scalar.h" |
| 17 | #include "llvm/Instruction.h" |
| 18 | #include "llvm/Analysis/LoopInfo.h" |
| 19 | #include "llvm/Analysis/LoopPass.h" |
| 20 | #include "llvm/ADT/Statistic.h" |
| 21 | #include "llvm/ADT/SmallVector.h" |
| 22 | |
| 23 | using namespace llvm; |
| 24 | |
| 25 | STATISTIC(NumDeleted, "Number of loops deleted"); |
| 26 | |
| 27 | namespace { |
| 28 | class VISIBILITY_HIDDEN DeadLoopElimination : public LoopPass { |
| 29 | public: |
| 30 | static char ID; // Pass ID, replacement for typeid |
| 31 | DeadLoopElimination() : LoopPass((intptr_t)&ID) { } |
| 32 | |
| 33 | // Possibly eliminate loop L if it is dead. |
| 34 | bool runOnLoop(Loop* L, LPPassManager& LPM); |
| 35 | |
| 36 | bool SingleDominatingExit(Loop* L); |
| 37 | bool IsLoopDead(Loop* L); |
| 38 | bool IsLoopInvariantInst(Instruction *I, Loop* L); |
| 39 | |
| 40 | virtual void getAnalysisUsage(AnalysisUsage& AU) const { |
| 41 | AU.addRequired<DominatorTree>(); |
| 42 | AU.addRequired<LoopInfo>(); |
| 43 | AU.addRequiredID(LoopSimplifyID); |
| 44 | AU.addRequiredID(LCSSAID); |
| 45 | |
| 46 | AU.addPreserved<DominatorTree>(); |
| 47 | AU.addPreserved<LoopInfo>(); |
| 48 | AU.addPreservedID(LoopSimplifyID); |
| 49 | AU.addPreservedID(LCSSAID); |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | char DeadLoopElimination::ID = 0; |
| 54 | RegisterPass<DeadLoopElimination> X ("dead-loop", "Eliminate dead loops"); |
| 55 | } |
| 56 | |
| 57 | LoopPass* llvm::createDeadLoopEliminationPass() { |
| 58 | return new DeadLoopElimination(); |
| 59 | } |
| 60 | |
| 61 | bool DeadLoopElimination::SingleDominatingExit(Loop* L) { |
| 62 | SmallVector<BasicBlock*, 4> exitingBlocks; |
| 63 | L->getExitingBlocks(exitingBlocks); |
| 64 | |
| 65 | if (exitingBlocks.size() != 1) |
| 66 | return 0; |
| 67 | |
| 68 | BasicBlock* latch = L->getLoopLatch(); |
| 69 | if (!latch) |
| 70 | return 0; |
| 71 | |
| 72 | DominatorTree& DT = getAnalysis<DominatorTree>(); |
| 73 | if (DT.dominates(exitingBlocks[0], latch)) |
| 74 | return exitingBlocks[0]; |
| 75 | else |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | bool DeadLoopElimination::IsLoopInvariantInst(Instruction *I, Loop* L) { |
| 80 | // PHI nodes are not loop invariant if defined in the loop. |
| 81 | if (isa<PHINode>(I) && L->contains(I->getParent())) |
| 82 | return false; |
| 83 | |
| 84 | // The instruction is loop invariant if all of its operands are loop-invariant |
| 85 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 86 | if (!L->isLoopInvariant(I->getOperand(i))) |
| 87 | return false; |
| 88 | |
| 89 | // If we got this far, the instruction is loop invariant! |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | bool DeadLoopElimination::IsLoopDead(Loop* L) { |
| 94 | SmallVector<BasicBlock*, 1> exitingBlocks; |
| 95 | L->getExitingBlocks(exitingBlocks); |
| 96 | BasicBlock* exitingBlock = exitingBlocks[0]; |
| 97 | |
| 98 | // Get the set of out-of-loop blocks that the exiting block branches to. |
| 99 | SmallVector<BasicBlock*, 8> exitBlocks; |
| 100 | L->getUniqueExitBlocks(exitBlocks); |
| 101 | if (exitBlocks.size() > 1) |
| 102 | return false; |
| 103 | BasicBlock* exitBlock = exitBlocks[0]; |
| 104 | |
| 105 | // Make sure that all PHI entries coming from the loop are loop invariant. |
| 106 | BasicBlock::iterator BI = exitBlock->begin(); |
| 107 | while (PHINode* P = dyn_cast<PHINode>(BI)) { |
| 108 | Value* incoming = P->getIncomingValueForBlock(exitingBlock); |
| 109 | if (Instruction* I = dyn_cast<Instruction>(incoming)) |
| 110 | if (!IsLoopInvariantInst(I, L)) |
| 111 | return false; |
| 112 | |
| 113 | BI++; |
| 114 | } |
| 115 | |
| 116 | // Make sure that no instructions in the block have potential side-effects. |
| 117 | for (Loop::block_iterator LI = L->block_begin(), LE = L->block_end(); |
| 118 | LI != LE; ++LI) { |
| 119 | for (BasicBlock::iterator BI = (*LI)->begin(), BE = (*LI)->end(); |
| 120 | BI != BE; ++BI) { |
| 121 | if (BI->mayWriteToMemory()) |
| 122 | return false; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | bool DeadLoopElimination::runOnLoop(Loop* L, LPPassManager& LPM) { |
| 130 | // Don't remove loops for which we can't solve the trip count. |
| 131 | // They could be infinite, in which case we'd be changing program behavior. |
| 132 | if (L->getTripCount()) |
| 133 | return false; |
| 134 | |
| 135 | // We can only remove the loop if there is a preheader that we can |
| 136 | // branch from after removing it. |
| 137 | BasicBlock* preheader = L->getLoopPreheader(); |
| 138 | if (!preheader) |
| 139 | return false; |
| 140 | |
| 141 | // We can't remove loops that contain subloops. If the subloops were dead, |
| 142 | // they would already have been removed in earlier executions of this pass. |
| 143 | if (L->begin() != L->end()) |
| 144 | return false; |
| 145 | |
| 146 | // Loops with multiple exits or exits that don't dominate the latch |
| 147 | // are too complicated to handle correctly. |
| 148 | if (!SingleDominatingExit(L)) |
| 149 | return false; |
| 150 | |
| 151 | // Finally, we have to check that the loop really is dead. |
| 152 | if (!IsLoopDead(L)) |
| 153 | return false; |
| 154 | |
| 155 | // Now that we know the removal is safe, change the branch from the preheader |
| 156 | // to go to the single exiting block. |
| 157 | SmallVector<BasicBlock*, 1> exitingBlocks; |
| 158 | L->getExitingBlocks(exitingBlocks); |
| 159 | BasicBlock* exitingBlock = exitingBlocks[0]; |
| 160 | |
| 161 | SmallVector<BasicBlock*, 1> exitBlocks; |
| 162 | L->getUniqueExitBlocks(exitBlocks); |
| 163 | BasicBlock* exitBlock = exitBlocks[0]; |
| 164 | |
| 165 | Function* F = L->getLoopLatch()->getParent(); |
| 166 | |
| 167 | for (Loop::block_iterator LI = L->block_begin(), LE = L->block_end(); |
| 168 | LI != LE; ++LI) |
| 169 | for (BasicBlock::iterator BI = (*LI)->begin(), BE = (*LI)->end(); |
| 170 | BI != BE; ) { |
| 171 | Instruction* I = BI++; |
| 172 | if (I->getNumUses() > 0 && IsLoopInvariantInst(I, L)) |
| 173 | I->moveBefore(preheader->getTerminator()); |
| 174 | } |
| 175 | |
| 176 | TerminatorInst* TI = preheader->getTerminator(); |
| 177 | if (BranchInst* BI = dyn_cast<BranchInst>(TI)) { |
| 178 | if (BI->isUnconditional()) |
| 179 | BI->setSuccessor(0, exitBlock); |
| 180 | else if (L->contains(BI->getSuccessor(0))) |
| 181 | BI->setSuccessor(0, exitBlock); |
| 182 | else |
| 183 | BI->setSuccessor(1, exitBlock); |
| 184 | } else { |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | BasicBlock::iterator BI = exitBlock->begin(); |
| 189 | while (PHINode* P = dyn_cast<PHINode>(BI)) { |
| 190 | unsigned i = P->getBasicBlockIndex(exitingBlock); |
| 191 | P->setIncomingBlock(i, preheader); |
| 192 | BI++; |
| 193 | } |
| 194 | |
| 195 | DominatorTree& DT = getAnalysis<DominatorTree>(); |
| 196 | for (Loop::block_iterator LI = L->block_begin(), LE = L->block_end(); |
| 197 | LI != LE; ++LI) { |
| 198 | SmallPtrSet<DomTreeNode*, 8> childNodes; |
| 199 | childNodes.insert(DT[*LI]->begin(), DT[*LI]->end()); |
| 200 | for (SmallPtrSet<DomTreeNode*, 8>::iterator DI = childNodes.begin(), |
| 201 | DE = childNodes.end(); DI != DE; ++DI) |
| 202 | DT.changeImmediateDominator(*DI, DT[preheader]); |
| 203 | |
| 204 | DT.eraseNode(*LI); |
| 205 | |
| 206 | for (BasicBlock::iterator BI = (*LI)->begin(), BE = (*LI)->end(); |
| 207 | BI != BE; ++BI) { |
| 208 | BI->dropAllReferences(); |
| 209 | } |
| 210 | |
| 211 | (*LI)->dropAllReferences(); |
| 212 | } |
| 213 | |
| 214 | unsigned bar = 0; |
| 215 | |
| 216 | for (Loop::block_iterator LI = L->block_begin(), LE = L->block_end(); |
| 217 | LI != LE; ++LI) { |
| 218 | for (BasicBlock::iterator BI = (*LI)->begin(), BE = (*LI)->end(); |
| 219 | BI != BE; ) { |
| 220 | Instruction* I = BI++; |
| 221 | I->eraseFromParent(); |
| 222 | } |
| 223 | |
| 224 | (*LI)->eraseFromParent(); |
| 225 | } |
| 226 | |
| 227 | LoopInfo& loopInfo = getAnalysis<LoopInfo>(); |
| 228 | SmallPtrSet<BasicBlock*, 8> blocks; |
| 229 | blocks.insert(L->block_begin(), L->block_end()); |
| 230 | for (SmallPtrSet<BasicBlock*,8>::iterator I = blocks.begin(), |
| 231 | E = blocks.end(); I != E; ++I) |
| 232 | loopInfo.removeBlock(*I); |
| 233 | |
| 234 | LPM.deleteLoopFromQueue(L); |
| 235 | |
| 236 | NumDeleted++; |
| 237 | |
| 238 | return true; |
| 239 | } |