Owen Anderson | 038a874 | 2008-05-29 08:45:13 +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 | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 9 | // |
Owen Anderson | 038a874 | 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. |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 17 | #define DEBUG_TYPE "adce" |
Chris Lattner | 022103b | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 18 | #include "llvm/Transforms/Scalar.h" |
Owen Anderson | 77d76b7 | 2008-07-02 18:05:19 +0000 | [diff] [blame] | 19 | #include "llvm/BasicBlock.h" |
Chris Lattner | ede6ac6 | 2004-04-10 06:53:09 +0000 | [diff] [blame] | 20 | #include "llvm/Instructions.h" |
Dale Johannesen | 6129e24 | 2009-03-04 21:24:04 +0000 | [diff] [blame] | 21 | #include "llvm/IntrinsicInst.h" |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 22 | #include "llvm/Pass.h" |
Owen Anderson | 77d76b7 | 2008-07-02 18:05:19 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CFG.h" |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 24 | #include "llvm/Support/InstIterator.h" |
Owen Anderson | 77d76b7 | 2008-07-02 18:05:19 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/DepthFirstIterator.h" |
Owen Anderson | ea6462b | 2008-07-02 18:41:09 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallPtrSet.h" |
Owen Anderson | ae18bd4 | 2008-06-23 06:13:12 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallVector.h" |
Owen Anderson | ea6462b | 2008-07-02 18:41:09 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | bd1a90e | 2003-12-19 09:08:34 +0000 | [diff] [blame] | 29 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 30 | |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 31 | STATISTIC(NumRemoved, "Number of instructions removed"); |
Chris Lattner | dfe81ab | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 32 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 33 | namespace { |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 34 | struct ADCE : public FunctionPass { |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 35 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 36 | ADCE() : FunctionPass(&ID) {} |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 37 | |
| 38 | virtual bool runOnFunction(Function& F); |
| 39 | |
| 40 | virtual void getAnalysisUsage(AnalysisUsage& AU) const { |
| 41 | AU.setPreservesCFG(); |
| 42 | } |
| 43 | |
| 44 | }; |
| 45 | } |
Chris Lattner | dfe81ab | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 46 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 47 | char ADCE::ID = 0; |
| 48 | static RegisterPass<ADCE> X("adce", "Aggressive Dead Code Elimination"); |
| 49 | |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 50 | bool ADCE::runOnFunction(Function& F) { |
Owen Anderson | ea6462b | 2008-07-02 18:41:09 +0000 | [diff] [blame] | 51 | SmallPtrSet<Instruction*, 128> alive; |
| 52 | SmallVector<Instruction*, 128> worklist; |
| 53 | |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 54 | // Collect the set of "root" instructions that are known live. |
| 55 | for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) |
| 56 | if (isa<TerminatorInst>(I.getInstructionIterator()) || |
Dale Johannesen | 6129e24 | 2009-03-04 21:24:04 +0000 | [diff] [blame] | 57 | isa<DbgInfoIntrinsic>(I.getInstructionIterator()) || |
Duncan Sands | 7af1c78 | 2009-05-06 06:49:50 +0000 | [diff] [blame] | 58 | I->mayHaveSideEffects()) { |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 59 | alive.insert(I.getInstructionIterator()); |
| 60 | worklist.push_back(I.getInstructionIterator()); |
Owen Anderson | 62849be | 2008-05-16 04:34:51 +0000 | [diff] [blame] | 61 | } |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 62 | |
| 63 | // Propagate liveness backwards to operands. |
| 64 | while (!worklist.empty()) { |
Dan Gohman | 321a813 | 2010-01-05 16:27:25 +0000 | [diff] [blame] | 65 | Instruction* curr = worklist.pop_back_val(); |
Owen Anderson | 62849be | 2008-05-16 04:34:51 +0000 | [diff] [blame] | 66 | |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 67 | for (Instruction::op_iterator OI = curr->op_begin(), OE = curr->op_end(); |
| 68 | OI != OE; ++OI) |
| 69 | if (Instruction* Inst = dyn_cast<Instruction>(OI)) |
| 70 | if (alive.insert(Inst)) |
| 71 | worklist.push_back(Inst); |
| 72 | } |
| 73 | |
| 74 | // The inverse of the live set is the dead set. These are those instructions |
| 75 | // which have no side effects and do not influence the control flow or return |
| 76 | // value of the function, and may therefore be deleted safely. |
Owen Anderson | ae18bd4 | 2008-06-23 06:13:12 +0000 | [diff] [blame] | 77 | // NOTE: We reuse the worklist vector here for memory efficiency. |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 78 | for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) |
| 79 | if (!alive.count(I.getInstructionIterator())) { |
Owen Anderson | ae18bd4 | 2008-06-23 06:13:12 +0000 | [diff] [blame] | 80 | worklist.push_back(I.getInstructionIterator()); |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 81 | I->dropAllReferences(); |
Chris Lattner | b8259dd | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 82 | } |
Owen Anderson | 62849be | 2008-05-16 04:34:51 +0000 | [diff] [blame] | 83 | |
Owen Anderson | ae18bd4 | 2008-06-23 06:13:12 +0000 | [diff] [blame] | 84 | for (SmallVector<Instruction*, 1024>::iterator I = worklist.begin(), |
| 85 | E = worklist.end(); I != E; ++I) { |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 86 | NumRemoved++; |
| 87 | (*I)->eraseFromParent(); |
Chris Lattner | b8259dd | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 88 | } |
Devang Patel | 35275f4 | 2008-11-11 00:54:10 +0000 | [diff] [blame] | 89 | |
Devang Patel | b8f69c6 | 2008-11-19 18:59:41 +0000 | [diff] [blame] | 90 | return !worklist.empty(); |
Chris Lattner | 02e90d5 | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 91 | } |
Owen Anderson | 038a874 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 92 | |
| 93 | FunctionPass *llvm::createAggressiveDCEPass() { |
| 94 | return new ADCE(); |
Duncan Sands | a806a87 | 2008-05-29 14:38:23 +0000 | [diff] [blame] | 95 | } |