Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===- DCE.cpp - Code to perform dead code elimination --------------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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 | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 19 | #define DEBUG_TYPE "dce" |
Chris Lattner | 022103b | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 20 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 2ed01d8 | 2002-05-07 18:10:55 +0000 | [diff] [blame] | 21 | #include "llvm/Transforms/Utils/Local.h" |
| 22 | #include "llvm/Instruction.h" |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 23 | #include "llvm/Pass.h" |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 24 | #include "llvm/Support/InstIterator.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 26 | #include <set> |
Chris Lattner | d745602 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 27 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 29 | STATISTIC(DIEEliminated, "Number of insts removed by DIE pass"); |
| 30 | STATISTIC(DCEEliminated, "Number of insts removed"); |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 32 | namespace { |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 33 | //===--------------------------------------------------------------------===// |
| 34 | // DeadInstElimination pass implementation |
| 35 | // |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 36 | struct DeadInstElimination : public BasicBlockPass { |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 37 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | 865f006 | 2009-02-18 05:09:16 +0000 | [diff] [blame] | 38 | DeadInstElimination() : BasicBlockPass(&ID) {} |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 39 | virtual bool runOnBasicBlock(BasicBlock &BB) { |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 40 | bool Changed = false; |
Chris Lattner | cb03f85 | 2008-11-27 22:46:09 +0000 | [diff] [blame] | 41 | for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); ) { |
| 42 | Instruction *Inst = DI++; |
| 43 | if (isInstructionTriviallyDead(Inst)) { |
| 44 | Inst->eraseFromParent(); |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 45 | Changed = true; |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 46 | ++DIEEliminated; |
Chris Lattner | cb03f85 | 2008-11-27 22:46:09 +0000 | [diff] [blame] | 47 | } |
| 48 | } |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 49 | return Changed; |
| 50 | } |
| 51 | |
| 52 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | cb2610e | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 53 | AU.setPreservesCFG(); |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 54 | } |
| 55 | }; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 58 | char DeadInstElimination::ID = 0; |
Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame^] | 59 | INITIALIZE_PASS(DeadInstElimination, "die", |
| 60 | "Dead Instruction Elimination", false, false); |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 61 | |
Devang Patel | 4d447f5 | 2007-01-25 23:23:25 +0000 | [diff] [blame] | 62 | Pass *llvm::createDeadInstEliminationPass() { |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 63 | return new DeadInstElimination(); |
Chris Lattner | c560f88 | 2002-01-23 05:48:24 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 66 | |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 67 | namespace { |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 68 | //===--------------------------------------------------------------------===// |
| 69 | // DeadCodeElimination pass implementation |
| 70 | // |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 71 | struct DCE : public FunctionPass { |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 72 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 73 | DCE() : FunctionPass(&ID) {} |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 75 | virtual bool runOnFunction(Function &F); |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 76 | |
| 77 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | cb2610e | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 78 | AU.setPreservesCFG(); |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 79 | } |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 80 | }; |
| 81 | } |
| 82 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 83 | char DCE::ID = 0; |
Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame^] | 84 | INITIALIZE_PASS(DCE, "dce", "Dead Code Elimination", false, false); |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 86 | bool DCE::runOnFunction(Function &F) { |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 87 | // Start out with all of the instructions in the worklist... |
Chris Lattner | 6ffe551 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 88 | std::vector<Instruction*> WorkList; |
Chris Lattner | 9281282 | 2005-05-08 18:45:26 +0000 | [diff] [blame] | 89 | for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) |
| 90 | WorkList.push_back(&*i); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 92 | // Loop over the worklist finding instructions that are dead. If they are |
| 93 | // dead make them drop all of their uses, making other instructions |
| 94 | // potentially dead, and work until the worklist is empty. |
| 95 | // |
Chris Lattner | 9281282 | 2005-05-08 18:45:26 +0000 | [diff] [blame] | 96 | bool MadeChange = false; |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 97 | while (!WorkList.empty()) { |
| 98 | Instruction *I = WorkList.back(); |
| 99 | WorkList.pop_back(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 9281282 | 2005-05-08 18:45:26 +0000 | [diff] [blame] | 101 | if (isInstructionTriviallyDead(I)) { // If the instruction is dead. |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 102 | // Loop over all of the values that the instruction uses, if there are |
| 103 | // instructions being used, add them to the worklist, because they might |
| 104 | // go dead after this one is removed. |
| 105 | // |
Chris Lattner | 4e5f036 | 2004-04-21 22:29:37 +0000 | [diff] [blame] | 106 | for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI) |
| 107 | if (Instruction *Used = dyn_cast<Instruction>(*OI)) |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 108 | WorkList.push_back(Used); |
| 109 | |
Chris Lattner | 9281282 | 2005-05-08 18:45:26 +0000 | [diff] [blame] | 110 | // Remove the instruction. |
| 111 | I->eraseFromParent(); |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 112 | |
Chris Lattner | 9281282 | 2005-05-08 18:45:26 +0000 | [diff] [blame] | 113 | // Remove the instruction from the worklist if it still exists in it. |
David Greene | 08d5fd9 | 2007-12-17 17:39:51 +0000 | [diff] [blame] | 114 | for (std::vector<Instruction*>::iterator WI = WorkList.begin(); |
Bill Wendling | 670ed09 | 2008-09-18 23:04:18 +0000 | [diff] [blame] | 115 | WI != WorkList.end(); ) { |
| 116 | if (*WI == I) |
David Greene | 08d5fd9 | 2007-12-17 17:39:51 +0000 | [diff] [blame] | 117 | WI = WorkList.erase(WI); |
Bill Wendling | 670ed09 | 2008-09-18 23:04:18 +0000 | [diff] [blame] | 118 | else |
| 119 | ++WI; |
| 120 | } |
Chris Lattner | 9281282 | 2005-05-08 18:45:26 +0000 | [diff] [blame] | 121 | |
| 122 | MadeChange = true; |
| 123 | ++DCEEliminated; |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 124 | } |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 125 | } |
Chris Lattner | 9281282 | 2005-05-08 18:45:26 +0000 | [diff] [blame] | 126 | return MadeChange; |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Brian Gaeke | 96d4bf7 | 2004-07-27 17:43:21 +0000 | [diff] [blame] | 129 | FunctionPass *llvm::createDeadCodeEliminationPass() { |
Chris Lattner | 92deeaf | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 130 | return new DCE(); |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 131 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 132 | |