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