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