Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===- DCE.cpp - Code to perform dead code elimination --------------------===// |
| 2 | // |
Chris Lattner | 87e8806 | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 3 | // This file implements dead inst elimination and dead code elimination. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 4 | // |
Chris Lattner | 87e8806 | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 5 | // Dead Inst Elimination performs a single pass over the function removing |
| 6 | // instructions that are obviously dead. Dead Code Elimination is similar, but |
| 7 | // it rechecks instructions that were used by removed instructions to see if |
| 8 | // they are newly dead. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
Chris Lattner | b4cfa7f | 2002-05-07 20:03:00 +0000 | [diff] [blame^] | 12 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 560da70 | 2002-05-07 18:10:55 +0000 | [diff] [blame] | 13 | #include "llvm/Transforms/Utils/Local.h" |
| 14 | #include "llvm/Instruction.h" |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 15 | #include "llvm/Pass.h" |
Chris Lattner | 87e8806 | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 16 | #include "llvm/Support/InstIterator.h" |
| 17 | #include <set> |
| 18 | |
Chris Lattner | 87e8806 | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 19 | //===----------------------------------------------------------------------===// |
| 20 | // DeadInstElimination pass implementation |
| 21 | // |
| 22 | |
| 23 | namespace { |
| 24 | struct DeadInstElimination : public BasicBlockPass { |
| 25 | const char *getPassName() const { return "Dead Instruction Elimination"; } |
| 26 | |
| 27 | virtual bool runOnBasicBlock(BasicBlock *BB) { |
| 28 | BasicBlock::InstListType &Vals = BB->getInstList(); |
| 29 | bool Changed = false; |
| 30 | for (BasicBlock::iterator DI = Vals.begin(); DI != Vals.end(); ) |
| 31 | if (dceInstruction(Vals, DI)) |
| 32 | Changed = true; |
| 33 | else |
| 34 | ++DI; |
| 35 | return Changed; |
| 36 | } |
| 37 | |
| 38 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 39 | AU.preservesCFG(); |
| 40 | } |
| 41 | }; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 44 | Pass *createDeadInstEliminationPass() { |
| 45 | return new DeadInstElimination(); |
Chris Lattner | ccbd4e4 | 2002-01-23 05:48:24 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Chris Lattner | 87e8806 | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 48 | |
| 49 | |
| 50 | //===----------------------------------------------------------------------===// |
| 51 | // DeadCodeElimination pass implementation |
Chris Lattner | d821c2a | 2001-06-07 16:59:26 +0000 | [diff] [blame] | 52 | // |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 53 | |
| 54 | namespace { |
Chris Lattner | 87e8806 | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 55 | struct DCE : public FunctionPass { |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 56 | const char *getPassName() const { return "Dead Code Elimination"; } |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 57 | |
Chris Lattner | 87e8806 | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 58 | virtual bool runOnFunction(Function *F); |
| 59 | |
| 60 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 61 | AU.preservesCFG(); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 62 | } |
Chris Lattner | 87e8806 | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 63 | }; |
| 64 | } |
| 65 | |
| 66 | bool DCE::runOnFunction(Function *F) { |
| 67 | // Start out with all of the instructions in the worklist... |
| 68 | std::vector<Instruction*> WorkList(inst_begin(F), inst_end(F)); |
| 69 | std::set<Instruction*> DeadInsts; |
| 70 | |
| 71 | // Loop over the worklist finding instructions that are dead. If they are |
| 72 | // dead make them drop all of their uses, making other instructions |
| 73 | // potentially dead, and work until the worklist is empty. |
| 74 | // |
| 75 | while (!WorkList.empty()) { |
| 76 | Instruction *I = WorkList.back(); |
| 77 | WorkList.pop_back(); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 78 | |
Chris Lattner | 560da70 | 2002-05-07 18:10:55 +0000 | [diff] [blame] | 79 | if (isInstructionTriviallyDead(I)) { // If the instruction is dead... |
Chris Lattner | 87e8806 | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 80 | // Loop over all of the values that the instruction uses, if there are |
| 81 | // instructions being used, add them to the worklist, because they might |
| 82 | // go dead after this one is removed. |
| 83 | // |
| 84 | for (User::use_iterator UI = I->use_begin(), UE = I->use_end(); |
| 85 | UI != UE; ++UI) |
| 86 | if (Instruction *Used = dyn_cast<Instruction>(*UI)) |
| 87 | WorkList.push_back(Used); |
| 88 | |
| 89 | // Tell the instruction to let go of all of the values it uses... |
| 90 | I->dropAllReferences(); |
| 91 | |
| 92 | // Keep track of this instruction, because we are going to delete it later |
| 93 | DeadInsts.insert(I); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 94 | } |
Chris Lattner | 87e8806 | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | // If we found no dead instructions, we haven't changed the function... |
| 98 | if (DeadInsts.empty()) return false; |
| 99 | |
| 100 | // Otherwise, loop over the program, removing and deleting the instructions... |
| 101 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) { |
| 102 | BasicBlock::InstListType &BBIL = (*I)->getInstList(); |
| 103 | for (BasicBlock::iterator BI = BBIL.begin(); BI != BBIL.end(); ) |
| 104 | if (DeadInsts.count(*BI)) { // Is this instruction dead? |
| 105 | delete BBIL.remove(BI); // Yup, remove and delete inst |
| 106 | } else { // This instruction is not dead |
| 107 | ++BI; // Continue on to the next one... |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return true; |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | Pass *createDeadCodeEliminationPass() { |
Chris Lattner | 87e8806 | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 115 | return new DCE(); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 116 | } |