Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 1 | //===- DCE.cpp - Code to perform dead code elimination --------------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 9 | // |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 10 | // This file implements the Aggressive Dead Code Elimination pass. This pass |
| 11 | // optimistically assumes that all instructions are dead until proven otherwise, |
| 12 | // allowing it to eliminate dead computations that other DCE passes do not |
| 13 | // catch, particularly involving loop computations. |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 17 | #define DEBUG_TYPE "adce" |
Chris Lattner | 022103b | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 18 | #include "llvm/Transforms/Scalar.h" |
Owen Anderson | 77d76b7 | 2008-07-02 18:05:19 +0000 | [diff] [blame^] | 19 | #include "llvm/BasicBlock.h" |
Chris Lattner | ede6ac6 | 2004-04-10 06:53:09 +0000 | [diff] [blame] | 20 | #include "llvm/Instructions.h" |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 21 | #include "llvm/Pass.h" |
Owen Anderson | 77d76b7 | 2008-07-02 18:05:19 +0000 | [diff] [blame^] | 22 | #include "llvm/Support/CFG.h" |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Compiler.h" |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 24 | #include "llvm/Support/InstIterator.h" |
Owen Anderson | 2c04b0d | 2008-07-02 17:32:04 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/DenseSet.h" |
Owen Anderson | 77d76b7 | 2008-07-02 18:05:19 +0000 | [diff] [blame^] | 26 | #include "llvm/ADT/DepthFirstIterator.h" |
| 27 | #include "llvm/ADT/Statistic.h" |
Owen Anderson | ae18bd4 | 2008-06-23 06:13:12 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallVector.h" |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 29 | |
Chris Lattner | bd1a90e | 2003-12-19 09:08:34 +0000 | [diff] [blame] | 30 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 31 | |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 32 | STATISTIC(NumRemoved, "Number of instructions removed"); |
Chris Lattner | dfe81ab | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 34 | namespace { |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 35 | struct VISIBILITY_HIDDEN ADCE : public FunctionPass { |
| 36 | static char ID; // Pass identification, replacement for typeid |
| 37 | ADCE() : FunctionPass((intptr_t)&ID) {} |
| 38 | |
Owen Anderson | 2c04b0d | 2008-07-02 17:32:04 +0000 | [diff] [blame] | 39 | DenseSet<Instruction*> alive; |
Owen Anderson | ae18bd4 | 2008-06-23 06:13:12 +0000 | [diff] [blame] | 40 | SmallVector<Instruction*, 1024> worklist; |
| 41 | |
Owen Anderson | 77d76b7 | 2008-07-02 18:05:19 +0000 | [diff] [blame^] | 42 | DenseSet<BasicBlock*> reachable; |
| 43 | SmallVector<BasicBlock*, 1024> unreachable; |
| 44 | |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 45 | virtual bool runOnFunction(Function& F); |
| 46 | |
| 47 | virtual void getAnalysisUsage(AnalysisUsage& AU) const { |
| 48 | AU.setPreservesCFG(); |
| 49 | } |
| 50 | |
| 51 | }; |
| 52 | } |
Chris Lattner | dfe81ab | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 53 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 54 | char ADCE::ID = 0; |
| 55 | static RegisterPass<ADCE> X("adce", "Aggressive Dead Code Elimination"); |
| 56 | |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 57 | bool ADCE::runOnFunction(Function& F) { |
Owen Anderson | ae18bd4 | 2008-06-23 06:13:12 +0000 | [diff] [blame] | 58 | alive.clear(); |
| 59 | worklist.clear(); |
Owen Anderson | 77d76b7 | 2008-07-02 18:05:19 +0000 | [diff] [blame^] | 60 | reachable.clear(); |
| 61 | unreachable.clear(); |
| 62 | |
| 63 | // First, collect the set of reachable blocks ... |
| 64 | for (df_iterator<BasicBlock*> DI = df_begin(&F.getEntryBlock()), |
| 65 | DE = df_end(&F.getEntryBlock()); DI != DE; ++DI) |
| 66 | reachable.insert(*DI); |
| 67 | |
| 68 | // ... and then invert it into the list of unreachable ones. These |
| 69 | // blocks will be removed from the function. |
| 70 | for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) |
| 71 | if (!reachable.count(FI)) |
| 72 | unreachable.push_back(FI); |
| 73 | |
| 74 | // Prepare to remove blocks by removing the PHI node entries for those blocks |
| 75 | // in their successors, and remove them from reference counting. |
| 76 | for (SmallVector<BasicBlock*, 1024>::iterator UI = unreachable.begin(), |
| 77 | UE = unreachable.end(); UI != UE; ++UI) { |
| 78 | BasicBlock* BB = *UI; |
| 79 | for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); |
| 80 | SI != SE; ++SI) { |
| 81 | BasicBlock* succ = *SI; |
| 82 | BasicBlock::iterator succ_inst = succ->begin(); |
| 83 | while (PHINode* P = dyn_cast<PHINode>(succ_inst)) { |
| 84 | P->removeIncomingValue(BB); |
| 85 | ++succ_inst; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | BB->dropAllReferences(); |
| 90 | } |
| 91 | |
| 92 | // Finally, erase the unreachable blocks. |
| 93 | for (SmallVector<BasicBlock*, 1024>::iterator UI = unreachable.begin(), |
| 94 | UE = unreachable.end(); UI != UE; ++UI) |
| 95 | (*UI)->eraseFromParent(); |
Owen Anderson | 62849be | 2008-05-16 04:34:51 +0000 | [diff] [blame] | 96 | |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 97 | // Collect the set of "root" instructions that are known live. |
| 98 | for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) |
| 99 | if (isa<TerminatorInst>(I.getInstructionIterator()) || |
| 100 | I->mayWriteToMemory()) { |
| 101 | alive.insert(I.getInstructionIterator()); |
| 102 | worklist.push_back(I.getInstructionIterator()); |
Owen Anderson | 62849be | 2008-05-16 04:34:51 +0000 | [diff] [blame] | 103 | } |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 104 | |
| 105 | // Propagate liveness backwards to operands. |
| 106 | while (!worklist.empty()) { |
| 107 | Instruction* curr = worklist.back(); |
| 108 | worklist.pop_back(); |
Owen Anderson | 62849be | 2008-05-16 04:34:51 +0000 | [diff] [blame] | 109 | |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 110 | for (Instruction::op_iterator OI = curr->op_begin(), OE = curr->op_end(); |
| 111 | OI != OE; ++OI) |
| 112 | if (Instruction* Inst = dyn_cast<Instruction>(OI)) |
| 113 | if (alive.insert(Inst)) |
| 114 | worklist.push_back(Inst); |
| 115 | } |
| 116 | |
| 117 | // The inverse of the live set is the dead set. These are those instructions |
| 118 | // which have no side effects and do not influence the control flow or return |
| 119 | // value of the function, and may therefore be deleted safely. |
Owen Anderson | ae18bd4 | 2008-06-23 06:13:12 +0000 | [diff] [blame] | 120 | // NOTE: We reuse the worklist vector here for memory efficiency. |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 121 | for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) |
| 122 | if (!alive.count(I.getInstructionIterator())) { |
Owen Anderson | ae18bd4 | 2008-06-23 06:13:12 +0000 | [diff] [blame] | 123 | worklist.push_back(I.getInstructionIterator()); |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 124 | I->dropAllReferences(); |
Chris Lattner | b8259dd | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 125 | } |
Owen Anderson | 62849be | 2008-05-16 04:34:51 +0000 | [diff] [blame] | 126 | |
Owen Anderson | ae18bd4 | 2008-06-23 06:13:12 +0000 | [diff] [blame] | 127 | for (SmallVector<Instruction*, 1024>::iterator I = worklist.begin(), |
| 128 | E = worklist.end(); I != E; ++I) { |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 129 | NumRemoved++; |
| 130 | (*I)->eraseFromParent(); |
Chris Lattner | b8259dd | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 131 | } |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 132 | |
Owen Anderson | ae18bd4 | 2008-06-23 06:13:12 +0000 | [diff] [blame] | 133 | return !worklist.empty(); |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 134 | } |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 135 | |
| 136 | FunctionPass *llvm::createAggressiveDCEPass() { |
| 137 | return new ADCE(); |
Duncan Sands | a806a87 | 2008-05-29 14:38:23 +0000 | [diff] [blame] | 138 | } |