Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===- DCE.cpp - Code to perform dead code elimination --------------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 10 | // This file implements dead inst elimination and dead code elimination. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | // |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 12 | // Dead Inst Elimination performs a single pass over the function removing |
| 13 | // instructions that are obviously dead. Dead Code Elimination is similar, but |
| 14 | // it rechecks instructions that were used by removed instructions to see if |
| 15 | // they are newly dead. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Chris Lattner | 022103b | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 2ed01d8 | 2002-05-07 18:10:55 +0000 | [diff] [blame] | 20 | #include "llvm/Transforms/Utils/Local.h" |
| 21 | #include "llvm/Instruction.h" |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 22 | #include "llvm/Pass.h" |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 23 | #include "llvm/Support/InstIterator.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 25 | #include <set> |
Chris Lattner | d745602 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 26 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 27 | |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 28 | namespace { |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 29 | Statistic<> DIEEliminated("die", "Number of insts removed"); |
| 30 | Statistic<> DCEEliminated("dce", "Number of insts removed"); |
| 31 | |
| 32 | //===--------------------------------------------------------------------===// |
| 33 | // DeadInstElimination pass implementation |
| 34 | // |
| 35 | |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 36 | struct DeadInstElimination : public BasicBlockPass { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 37 | virtual bool runOnBasicBlock(BasicBlock &BB) { |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 38 | bool Changed = false; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 39 | for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); ) |
Chris Lattner | 16da494 | 2002-05-26 20:18:18 +0000 | [diff] [blame] | 40 | if (dceInstruction(DI)) { |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 41 | Changed = true; |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 42 | ++DIEEliminated; |
| 43 | } else |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 44 | ++DI; |
| 45 | return Changed; |
| 46 | } |
| 47 | |
| 48 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | cb2610e | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 49 | AU.setPreservesCFG(); |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 50 | } |
| 51 | }; |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 52 | |
Chris Lattner | a6275cc | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 53 | RegisterOpt<DeadInstElimination> X("die", "Dead Instruction Elimination"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Chris Lattner | 4b50156 | 2004-09-20 04:43:15 +0000 | [diff] [blame^] | 56 | FunctionPass *llvm::createDeadInstEliminationPass() { |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 57 | return new DeadInstElimination(); |
Chris Lattner | c560f88 | 2002-01-23 05:48:24 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 61 | //===----------------------------------------------------------------------===// |
| 62 | // DeadCodeElimination pass implementation |
Chris Lattner | f155e13 | 2001-06-07 16:59:26 +0000 | [diff] [blame] | 63 | // |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 64 | |
| 65 | namespace { |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 66 | struct DCE : public FunctionPass { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 67 | virtual bool runOnFunction(Function &F); |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 68 | |
| 69 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | cb2610e | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 70 | AU.setPreservesCFG(); |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 71 | } |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 72 | }; |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 73 | |
Chris Lattner | a6275cc | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 74 | RegisterOpt<DCE> Y("dce", "Dead Code Elimination"); |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 77 | bool DCE::runOnFunction(Function &F) { |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 78 | // Start out with all of the instructions in the worklist... |
Chris Lattner | 6ffe551 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 79 | std::vector<Instruction*> WorkList; |
| 80 | for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) { |
| 81 | WorkList.push_back(&*i); |
| 82 | } |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 83 | std::set<Instruction*> DeadInsts; |
| 84 | |
| 85 | // Loop over the worklist finding instructions that are dead. If they are |
| 86 | // dead make them drop all of their uses, making other instructions |
| 87 | // potentially dead, and work until the worklist is empty. |
| 88 | // |
| 89 | while (!WorkList.empty()) { |
| 90 | Instruction *I = WorkList.back(); |
| 91 | WorkList.pop_back(); |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 92 | |
Chris Lattner | 2ed01d8 | 2002-05-07 18:10:55 +0000 | [diff] [blame] | 93 | if (isInstructionTriviallyDead(I)) { // If the instruction is dead... |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 94 | // Loop over all of the values that the instruction uses, if there are |
| 95 | // instructions being used, add them to the worklist, because they might |
| 96 | // go dead after this one is removed. |
| 97 | // |
Chris Lattner | 4e5f036 | 2004-04-21 22:29:37 +0000 | [diff] [blame] | 98 | for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI) |
| 99 | if (Instruction *Used = dyn_cast<Instruction>(*OI)) |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 100 | WorkList.push_back(Used); |
| 101 | |
| 102 | // Tell the instruction to let go of all of the values it uses... |
| 103 | I->dropAllReferences(); |
| 104 | |
| 105 | // Keep track of this instruction, because we are going to delete it later |
| 106 | DeadInsts.insert(I); |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 107 | } |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | // If we found no dead instructions, we haven't changed the function... |
| 111 | if (DeadInsts.empty()) return false; |
| 112 | |
| 113 | // Otherwise, loop over the program, removing and deleting the instructions... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 114 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
| 115 | for (BasicBlock::iterator BI = I->begin(); BI != I->end(); ) |
| 116 | if (DeadInsts.count(BI)) { // Is this instruction dead? |
| 117 | BI = I->getInstList().erase(BI); // Yup, remove and delete inst |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 118 | ++DCEEliminated; |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 119 | } else { // This instruction is not dead |
| 120 | ++BI; // Continue on to the next one... |
| 121 | } |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 122 | |
| 123 | return true; |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Brian Gaeke | 96d4bf7 | 2004-07-27 17:43:21 +0000 | [diff] [blame] | 126 | FunctionPass *llvm::createDeadCodeEliminationPass() { |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 127 | return new DCE(); |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 128 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 129 | |