Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===- DCE.cpp - Code to perform dead code elimination --------------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 87e8806 | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 10 | // This file implements dead inst elimination and dead code elimination. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | // |
Chris Lattner | 87e8806 | 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 | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Chris Lattner | b4cfa7f | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/Scalar.h" |
Fiona Glaser | b0c6d91 | 2015-09-30 17:49:49 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SetVector.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 8394857 | 2014-03-04 10:30:26 +0000 | [diff] [blame] | 22 | #include "llvm/IR/InstIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Instruction.h" |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 24 | #include "llvm/Pass.h" |
Chandler Carruth | 62d4215 | 2015-01-15 02:16:27 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 49525f8 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 27 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 28 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 29 | #define DEBUG_TYPE "dce" |
| 30 | |
Chris Lattner | 79a42ac | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 31 | STATISTIC(DIEEliminated, "Number of insts removed by DIE pass"); |
| 32 | STATISTIC(DCEEliminated, "Number of insts removed"); |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 79a42ac | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 34 | namespace { |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 35 | //===--------------------------------------------------------------------===// |
| 36 | // DeadInstElimination pass implementation |
| 37 | // |
Chris Lattner | 2dd09db | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 38 | struct DeadInstElimination : public BasicBlockPass { |
Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 39 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 40 | DeadInstElimination() : BasicBlockPass(ID) { |
| 41 | initializeDeadInstEliminationPass(*PassRegistry::getPassRegistry()); |
| 42 | } |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 43 | bool runOnBasicBlock(BasicBlock &BB) override { |
Vedant Kumar | 6013f45 | 2016-04-22 06:51:37 +0000 | [diff] [blame^] | 44 | if (skipOptnoneFunction(BB)) |
Paul Robinson | af4e64d | 2014-02-06 00:07:05 +0000 | [diff] [blame] | 45 | return false; |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 46 | auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>(); |
| 47 | TargetLibraryInfo *TLI = TLIP ? &TLIP->getTLI() : nullptr; |
Chris Lattner | 87e8806 | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 48 | bool Changed = false; |
Chris Lattner | c92fa42 | 2008-11-27 22:46:09 +0000 | [diff] [blame] | 49 | for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); ) { |
Duncan P. N. Exon Smith | 3a9c9e3 | 2015-10-13 18:26:00 +0000 | [diff] [blame] | 50 | Instruction *Inst = &*DI++; |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 51 | if (isInstructionTriviallyDead(Inst, TLI)) { |
Chris Lattner | c92fa42 | 2008-11-27 22:46:09 +0000 | [diff] [blame] | 52 | Inst->eraseFromParent(); |
Chris Lattner | 87e8806 | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 53 | Changed = true; |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 54 | ++DIEEliminated; |
Chris Lattner | c92fa42 | 2008-11-27 22:46:09 +0000 | [diff] [blame] | 55 | } |
| 56 | } |
Chris Lattner | 87e8806 | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 57 | return Changed; |
| 58 | } |
| 59 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 60 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chris Lattner | 820d971 | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 61 | AU.setPreservesCFG(); |
Chris Lattner | 87e8806 | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 62 | } |
| 63 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 64 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 65 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 66 | char DeadInstElimination::ID = 0; |
Owen Anderson | a57b97e | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 67 | INITIALIZE_PASS(DeadInstElimination, "die", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 68 | "Dead Instruction Elimination", false, false) |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 69 | |
Devang Patel | 5292e65 | 2007-01-25 23:23:25 +0000 | [diff] [blame] | 70 | Pass *llvm::createDeadInstEliminationPass() { |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 71 | return new DeadInstElimination(); |
Chris Lattner | ccbd4e4 | 2002-01-23 05:48:24 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Chris Lattner | 87e8806 | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 75 | namespace { |
Chris Lattner | 79a42ac | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 76 | //===--------------------------------------------------------------------===// |
| 77 | // DeadCodeElimination pass implementation |
| 78 | // |
Chris Lattner | 87e8806 | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 79 | struct DCE : public FunctionPass { |
Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 80 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 81 | DCE() : FunctionPass(ID) { |
| 82 | initializeDCEPass(*PassRegistry::getPassRegistry()); |
| 83 | } |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 84 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 85 | bool runOnFunction(Function &F) override; |
Chris Lattner | 87e8806 | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 86 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 87 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chris Lattner | 820d971 | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 88 | AU.setPreservesCFG(); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 89 | } |
Chris Lattner | 87e8806 | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 90 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 91 | } |
Chris Lattner | 87e8806 | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 92 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 93 | char DCE::ID = 0; |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 94 | INITIALIZE_PASS(DCE, "dce", "Dead Code Elimination", false, false) |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 95 | |
Fiona Glaser | b0c6d91 | 2015-09-30 17:49:49 +0000 | [diff] [blame] | 96 | static bool DCEInstruction(Instruction *I, |
| 97 | SmallSetVector<Instruction *, 16> &WorkList, |
| 98 | const TargetLibraryInfo *TLI) { |
| 99 | if (isInstructionTriviallyDead(I, TLI)) { |
| 100 | // Null out all of the instruction's operands to see if any operand becomes |
| 101 | // dead as we go. |
| 102 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 103 | Value *OpV = I->getOperand(i); |
| 104 | I->setOperand(i, nullptr); |
| 105 | |
| 106 | if (!OpV->use_empty() || I == OpV) |
| 107 | continue; |
| 108 | |
| 109 | // If the operand is an instruction that became dead as we nulled out the |
| 110 | // operand, and if it is 'trivially' dead, delete it in a future loop |
| 111 | // iteration. |
| 112 | if (Instruction *OpI = dyn_cast<Instruction>(OpV)) |
| 113 | if (isInstructionTriviallyDead(OpI, TLI)) |
| 114 | WorkList.insert(OpI); |
| 115 | } |
| 116 | |
| 117 | I->eraseFromParent(); |
| 118 | ++DCEEliminated; |
| 119 | return true; |
| 120 | } |
| 121 | return false; |
| 122 | } |
| 123 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 124 | bool DCE::runOnFunction(Function &F) { |
Vedant Kumar | 6013f45 | 2016-04-22 06:51:37 +0000 | [diff] [blame^] | 125 | if (skipOptnoneFunction(F)) |
Paul Robinson | af4e64d | 2014-02-06 00:07:05 +0000 | [diff] [blame] | 126 | return false; |
| 127 | |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 128 | auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>(); |
| 129 | TargetLibraryInfo *TLI = TLIP ? &TLIP->getTLI() : nullptr; |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 130 | |
Chris Lattner | 4922118 | 2005-05-08 18:45:26 +0000 | [diff] [blame] | 131 | bool MadeChange = false; |
Fiona Glaser | b0c6d91 | 2015-09-30 17:49:49 +0000 | [diff] [blame] | 132 | SmallSetVector<Instruction *, 16> WorkList; |
| 133 | // Iterate over the original function, only adding insts to the worklist |
| 134 | // if they actually need to be revisited. This avoids having to pre-init |
| 135 | // the worklist with the entire function's worth of instructions. |
| 136 | for (inst_iterator FI = inst_begin(F), FE = inst_end(F); FI != FE;) { |
| 137 | Instruction *I = &*FI; |
| 138 | ++FI; |
| 139 | |
| 140 | // We're visiting this instruction now, so make sure it's not in the |
| 141 | // worklist from an earlier visit. |
| 142 | if (!WorkList.count(I)) |
| 143 | MadeChange |= DCEInstruction(I, WorkList, TLI); |
| 144 | } |
| 145 | |
Chris Lattner | 87e8806 | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 146 | while (!WorkList.empty()) { |
Fiona Glaser | b0c6d91 | 2015-09-30 17:49:49 +0000 | [diff] [blame] | 147 | Instruction *I = WorkList.pop_back_val(); |
| 148 | MadeChange |= DCEInstruction(I, WorkList, TLI); |
Chris Lattner | 87e8806 | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 149 | } |
Chris Lattner | 4922118 | 2005-05-08 18:45:26 +0000 | [diff] [blame] | 150 | return MadeChange; |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Brian Gaeke | 38b79e8 | 2004-07-27 17:43:21 +0000 | [diff] [blame] | 153 | FunctionPass *llvm::createDeadCodeEliminationPass() { |
Chris Lattner | 87e8806 | 2002-05-07 04:24:11 +0000 | [diff] [blame] | 154 | return new DCE(); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 155 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 156 | |