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