Owen Anderson | 871d8eb | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 1 | //===- DCE.cpp - Code to perform dead code elimination --------------------===// |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 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. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Owen Anderson | 871d8eb | 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. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #define DEBUG_TYPE "adce" |
| 18 | #include "llvm/Transforms/Scalar.h" |
Owen Anderson | 49ff8a4 | 2008-07-02 18:05:19 +0000 | [diff] [blame] | 19 | #include "llvm/BasicBlock.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 20 | #include "llvm/Instructions.h" |
Owen Anderson | 871d8eb | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 21 | #include "llvm/Pass.h" |
Owen Anderson | 49ff8a4 | 2008-07-02 18:05:19 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CFG.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Compiler.h" |
Owen Anderson | 871d8eb | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 24 | #include "llvm/Support/InstIterator.h" |
Owen Anderson | 49ff8a4 | 2008-07-02 18:05:19 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/DepthFirstIterator.h" |
Owen Anderson | 73b03a2 | 2008-07-02 18:41:09 +0000 | [diff] [blame^] | 26 | #include "llvm/ADT/SmallPtrSet.h" |
Owen Anderson | 40e5a55 | 2008-06-23 06:13:12 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallVector.h" |
Owen Anderson | 73b03a2 | 2008-07-02 18:41:09 +0000 | [diff] [blame^] | 28 | #include "llvm/ADT/Statistic.h" |
| 29 | |
Owen Anderson | 871d8eb | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 30 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
Owen Anderson | 871d8eb | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 33 | STATISTIC(NumRemoved, "Number of instructions removed"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 34 | |
| 35 | namespace { |
Owen Anderson | 871d8eb | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 36 | struct VISIBILITY_HIDDEN ADCE : public FunctionPass { |
| 37 | static char ID; // Pass identification, replacement for typeid |
| 38 | ADCE() : FunctionPass((intptr_t)&ID) {} |
| 39 | |
| 40 | virtual bool runOnFunction(Function& F); |
| 41 | |
| 42 | virtual void getAnalysisUsage(AnalysisUsage& AU) const { |
| 43 | AU.setPreservesCFG(); |
| 44 | } |
| 45 | |
| 46 | }; |
| 47 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 48 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 49 | char ADCE::ID = 0; |
| 50 | static RegisterPass<ADCE> X("adce", "Aggressive Dead Code Elimination"); |
| 51 | |
Owen Anderson | 871d8eb | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 52 | bool ADCE::runOnFunction(Function& F) { |
Owen Anderson | 73b03a2 | 2008-07-02 18:41:09 +0000 | [diff] [blame^] | 53 | SmallPtrSet<Instruction*, 128> alive; |
| 54 | SmallVector<Instruction*, 128> worklist; |
| 55 | |
| 56 | SmallPtrSet<BasicBlock*, 64> reachable; |
| 57 | SmallVector<BasicBlock*, 16> unreachable; |
Owen Anderson | 49ff8a4 | 2008-07-02 18:05:19 +0000 | [diff] [blame] | 58 | |
| 59 | // First, collect the set of reachable blocks ... |
Owen Anderson | 73b03a2 | 2008-07-02 18:41:09 +0000 | [diff] [blame^] | 60 | for (df_ext_iterator<BasicBlock*, SmallPtrSet<BasicBlock*, 64> > |
| 61 | DI = df_ext_begin(&F.getEntryBlock(), reachable), |
| 62 | DE = df_ext_end(&F.getEntryBlock(), reachable); DI != DE; ++DI) |
| 63 | ; // Deliberately empty, df_ext_iterator will fill in the set. |
Owen Anderson | 49ff8a4 | 2008-07-02 18:05:19 +0000 | [diff] [blame] | 64 | |
| 65 | // ... and then invert it into the list of unreachable ones. These |
| 66 | // blocks will be removed from the function. |
| 67 | for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) |
| 68 | if (!reachable.count(FI)) |
| 69 | unreachable.push_back(FI); |
| 70 | |
| 71 | // Prepare to remove blocks by removing the PHI node entries for those blocks |
| 72 | // in their successors, and remove them from reference counting. |
Owen Anderson | 73b03a2 | 2008-07-02 18:41:09 +0000 | [diff] [blame^] | 73 | for (SmallVector<BasicBlock*, 16>::iterator UI = unreachable.begin(), |
Owen Anderson | 49ff8a4 | 2008-07-02 18:05:19 +0000 | [diff] [blame] | 74 | UE = unreachable.end(); UI != UE; ++UI) { |
| 75 | BasicBlock* BB = *UI; |
| 76 | for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); |
| 77 | SI != SE; ++SI) { |
| 78 | BasicBlock* succ = *SI; |
| 79 | BasicBlock::iterator succ_inst = succ->begin(); |
| 80 | while (PHINode* P = dyn_cast<PHINode>(succ_inst)) { |
| 81 | P->removeIncomingValue(BB); |
| 82 | ++succ_inst; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | BB->dropAllReferences(); |
| 87 | } |
| 88 | |
| 89 | // Finally, erase the unreachable blocks. |
Owen Anderson | 73b03a2 | 2008-07-02 18:41:09 +0000 | [diff] [blame^] | 90 | for (SmallVector<BasicBlock*, 16>::iterator UI = unreachable.begin(), |
Owen Anderson | 49ff8a4 | 2008-07-02 18:05:19 +0000 | [diff] [blame] | 91 | UE = unreachable.end(); UI != UE; ++UI) |
| 92 | (*UI)->eraseFromParent(); |
Owen Anderson | 8cf41ad | 2008-05-16 04:34:51 +0000 | [diff] [blame] | 93 | |
Owen Anderson | 871d8eb | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 94 | // Collect the set of "root" instructions that are known live. |
| 95 | for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) |
| 96 | if (isa<TerminatorInst>(I.getInstructionIterator()) || |
| 97 | I->mayWriteToMemory()) { |
| 98 | alive.insert(I.getInstructionIterator()); |
| 99 | worklist.push_back(I.getInstructionIterator()); |
Owen Anderson | 8cf41ad | 2008-05-16 04:34:51 +0000 | [diff] [blame] | 100 | } |
Owen Anderson | 871d8eb | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 101 | |
| 102 | // Propagate liveness backwards to operands. |
| 103 | while (!worklist.empty()) { |
| 104 | Instruction* curr = worklist.back(); |
| 105 | worklist.pop_back(); |
Owen Anderson | 8cf41ad | 2008-05-16 04:34:51 +0000 | [diff] [blame] | 106 | |
Owen Anderson | 871d8eb | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 107 | for (Instruction::op_iterator OI = curr->op_begin(), OE = curr->op_end(); |
| 108 | OI != OE; ++OI) |
| 109 | if (Instruction* Inst = dyn_cast<Instruction>(OI)) |
| 110 | if (alive.insert(Inst)) |
| 111 | worklist.push_back(Inst); |
| 112 | } |
| 113 | |
| 114 | // The inverse of the live set is the dead set. These are those instructions |
| 115 | // which have no side effects and do not influence the control flow or return |
| 116 | // value of the function, and may therefore be deleted safely. |
Owen Anderson | 40e5a55 | 2008-06-23 06:13:12 +0000 | [diff] [blame] | 117 | // NOTE: We reuse the worklist vector here for memory efficiency. |
Owen Anderson | 871d8eb | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 118 | for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) |
| 119 | if (!alive.count(I.getInstructionIterator())) { |
Owen Anderson | 40e5a55 | 2008-06-23 06:13:12 +0000 | [diff] [blame] | 120 | worklist.push_back(I.getInstructionIterator()); |
Owen Anderson | 871d8eb | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 121 | I->dropAllReferences(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 122 | } |
Owen Anderson | 8cf41ad | 2008-05-16 04:34:51 +0000 | [diff] [blame] | 123 | |
Owen Anderson | 40e5a55 | 2008-06-23 06:13:12 +0000 | [diff] [blame] | 124 | for (SmallVector<Instruction*, 1024>::iterator I = worklist.begin(), |
| 125 | E = worklist.end(); I != E; ++I) { |
Owen Anderson | 871d8eb | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 126 | NumRemoved++; |
| 127 | (*I)->eraseFromParent(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 128 | } |
Owen Anderson | 871d8eb | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 129 | |
Owen Anderson | 40e5a55 | 2008-06-23 06:13:12 +0000 | [diff] [blame] | 130 | return !worklist.empty(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 131 | } |
Owen Anderson | 871d8eb | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 132 | |
| 133 | FunctionPass *llvm::createAggressiveDCEPass() { |
| 134 | return new ADCE(); |
Duncan Sands | b758123 | 2008-05-29 14:38:23 +0000 | [diff] [blame] | 135 | } |