Owen Anderson | 871d8eb | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 1 | //===- DCE.cpp - Code to perform dead code elimination --------------------===// |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 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. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Owen Anderson | 871d8eb | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 10 | // This file implements the Aggressive Dead Code Elimination pass. This pass |
| 11 | // optimistically assumes that all instructions are dead until proven otherwise, |
| 12 | // allowing it to eliminate dead computations that other DCE passes do not |
| 13 | // catch, particularly involving loop computations. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #define DEBUG_TYPE "adce" |
| 18 | #include "llvm/Transforms/Scalar.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 19 | #include "llvm/Instructions.h" |
Owen Anderson | 871d8eb | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 20 | #include "llvm/Pass.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Compiler.h" |
Owen Anderson | 871d8eb | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 22 | #include "llvm/Support/InstIterator.h" |
| 23 | #include "llvm/ADT/Statistic.h" |
| 24 | #include "llvm/ADT/SmallPtrSet.h" |
| 25 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Owen Anderson | 871d8eb | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 28 | STATISTIC(NumRemoved, "Number of instructions removed"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 29 | |
| 30 | namespace { |
Owen Anderson | 871d8eb | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 31 | struct VISIBILITY_HIDDEN ADCE : public FunctionPass { |
| 32 | static char ID; // Pass identification, replacement for typeid |
| 33 | ADCE() : FunctionPass((intptr_t)&ID) {} |
| 34 | |
| 35 | virtual bool runOnFunction(Function& F); |
| 36 | |
| 37 | virtual void getAnalysisUsage(AnalysisUsage& AU) const { |
| 38 | AU.setPreservesCFG(); |
| 39 | } |
| 40 | |
| 41 | }; |
| 42 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 43 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 44 | char ADCE::ID = 0; |
| 45 | static RegisterPass<ADCE> X("adce", "Aggressive Dead Code Elimination"); |
| 46 | |
Owen Anderson | 871d8eb | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 47 | bool ADCE::runOnFunction(Function& F) { |
| 48 | SmallPtrSet<Instruction*, 32> alive; |
| 49 | std::vector<Instruction*> worklist; |
Owen Anderson | 8cf41ad | 2008-05-16 04:34:51 +0000 | [diff] [blame] | 50 | |
Owen Anderson | 871d8eb | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 51 | // Collect the set of "root" instructions that are known live. |
| 52 | for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) |
| 53 | if (isa<TerminatorInst>(I.getInstructionIterator()) || |
| 54 | I->mayWriteToMemory()) { |
| 55 | alive.insert(I.getInstructionIterator()); |
| 56 | worklist.push_back(I.getInstructionIterator()); |
Owen Anderson | 8cf41ad | 2008-05-16 04:34:51 +0000 | [diff] [blame] | 57 | } |
Owen Anderson | 871d8eb | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 58 | |
| 59 | // Propagate liveness backwards to operands. |
| 60 | while (!worklist.empty()) { |
| 61 | Instruction* curr = worklist.back(); |
| 62 | worklist.pop_back(); |
Owen Anderson | 8cf41ad | 2008-05-16 04:34:51 +0000 | [diff] [blame] | 63 | |
Owen Anderson | 871d8eb | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 64 | for (Instruction::op_iterator OI = curr->op_begin(), OE = curr->op_end(); |
| 65 | OI != OE; ++OI) |
| 66 | if (Instruction* Inst = dyn_cast<Instruction>(OI)) |
| 67 | if (alive.insert(Inst)) |
| 68 | worklist.push_back(Inst); |
| 69 | } |
| 70 | |
| 71 | // The inverse of the live set is the dead set. These are those instructions |
| 72 | // which have no side effects and do not influence the control flow or return |
| 73 | // value of the function, and may therefore be deleted safely. |
| 74 | SmallPtrSet<Instruction*, 32> dead; |
| 75 | for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) |
| 76 | if (!alive.count(I.getInstructionIterator())) { |
| 77 | dead.insert(I.getInstructionIterator()); |
| 78 | I->dropAllReferences(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 79 | } |
Owen Anderson | 8cf41ad | 2008-05-16 04:34:51 +0000 | [diff] [blame] | 80 | |
Owen Anderson | 871d8eb | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 81 | for (SmallPtrSet<Instruction*, 32>::iterator I = dead.begin(), |
| 82 | E = dead.end(); I != E; ++I) { |
| 83 | NumRemoved++; |
| 84 | (*I)->eraseFromParent(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 85 | } |
Owen Anderson | 871d8eb | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 86 | |
| 87 | return !dead.empty(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 88 | } |
Owen Anderson | 871d8eb | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 89 | |
| 90 | FunctionPass *llvm::createAggressiveDCEPass() { |
| 91 | return new ADCE(); |
Duncan Sands | b758123 | 2008-05-29 14:38:23 +0000 | [diff] [blame^] | 92 | } |