blob: 2310ba62e6bef7dc156204ae96e2c7dfa0a56063 [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===- DCE.cpp - Code to perform dead code elimination --------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner87e88062002-05-07 04:24:11 +000010// This file implements dead inst elimination and dead code elimination.
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
Chris Lattner87e88062002-05-07 04:24:11 +000012// 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 Lattner2f7c9632001-06-06 20:29:01 +000016//
17//===----------------------------------------------------------------------===//
18
Justin Bogner395c2122016-04-22 19:40:41 +000019#include "llvm/Transforms/Scalar/DCE.h"
Fiona Glaserb0c6d912015-09-30 17:49:49 +000020#include "llvm/ADT/SetVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/ADT/Statistic.h"
Chandler Carruth83948572014-03-04 10:30:26 +000022#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Instruction.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000024#include "llvm/Pass.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000025#include "llvm/Analysis/TargetLibraryInfo.h"
Justin Bogner395c2122016-04-22 19:40:41 +000026#include "llvm/Transforms/Scalar.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Transforms/Utils/Local.h"
Chris Lattner49525f82004-01-09 06:02:20 +000028using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000029
Chandler Carruth964daaa2014-04-22 02:55:47 +000030#define DEBUG_TYPE "dce"
31
Chris Lattner79a42ac2006-12-19 21:40:18 +000032STATISTIC(DIEEliminated, "Number of insts removed by DIE pass");
33STATISTIC(DCEEliminated, "Number of insts removed");
Chris Lattnerbf3a0992002-10-01 22:38:41 +000034
Chris Lattner79a42ac2006-12-19 21:40:18 +000035namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000036 //===--------------------------------------------------------------------===//
37 // DeadInstElimination pass implementation
38 //
Chris Lattner2dd09db2009-09-02 06:11:42 +000039 struct DeadInstElimination : public BasicBlockPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000040 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000041 DeadInstElimination() : BasicBlockPass(ID) {
42 initializeDeadInstEliminationPass(*PassRegistry::getPassRegistry());
43 }
Craig Topper3e4c6972014-03-05 09:10:37 +000044 bool runOnBasicBlock(BasicBlock &BB) override {
Andrew Kayloraa641a52016-04-22 22:06:11 +000045 if (skipBasicBlock(BB))
Paul Robinsonaf4e64d2014-02-06 00:07:05 +000046 return false;
Chandler Carruthb98f63d2015-01-15 10:41:28 +000047 auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
48 TargetLibraryInfo *TLI = TLIP ? &TLIP->getTLI() : nullptr;
Chris Lattner87e88062002-05-07 04:24:11 +000049 bool Changed = false;
Chris Lattnerc92fa422008-11-27 22:46:09 +000050 for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); ) {
Duncan P. N. Exon Smith3a9c9e32015-10-13 18:26:00 +000051 Instruction *Inst = &*DI++;
Benjamin Kramer8bcc9712012-08-29 15:32:21 +000052 if (isInstructionTriviallyDead(Inst, TLI)) {
Chris Lattnerc92fa422008-11-27 22:46:09 +000053 Inst->eraseFromParent();
Chris Lattner87e88062002-05-07 04:24:11 +000054 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +000055 ++DIEEliminated;
Chris Lattnerc92fa422008-11-27 22:46:09 +000056 }
57 }
Chris Lattner87e88062002-05-07 04:24:11 +000058 return Changed;
59 }
60
Craig Topper3e4c6972014-03-05 09:10:37 +000061 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner820d9712002-10-21 20:00:28 +000062 AU.setPreservesCFG();
Chris Lattner87e88062002-05-07 04:24:11 +000063 }
64 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000065}
Chris Lattner2f7c9632001-06-06 20:29:01 +000066
Dan Gohmand78c4002008-05-13 00:00:25 +000067char DeadInstElimination::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +000068INITIALIZE_PASS(DeadInstElimination, "die",
Owen Andersondf7a4f22010-10-07 22:25:06 +000069 "Dead Instruction Elimination", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000070
Devang Patel5292e652007-01-25 23:23:25 +000071Pass *llvm::createDeadInstEliminationPass() {
Chris Lattner04805fa2002-02-26 21:46:54 +000072 return new DeadInstElimination();
Chris Lattnerccbd4e42002-01-23 05:48:24 +000073}
74
Fiona Glaserb0c6d912015-09-30 17:49:49 +000075static bool DCEInstruction(Instruction *I,
76 SmallSetVector<Instruction *, 16> &WorkList,
77 const TargetLibraryInfo *TLI) {
78 if (isInstructionTriviallyDead(I, TLI)) {
79 // Null out all of the instruction's operands to see if any operand becomes
80 // dead as we go.
81 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
82 Value *OpV = I->getOperand(i);
83 I->setOperand(i, nullptr);
84
85 if (!OpV->use_empty() || I == OpV)
86 continue;
87
88 // If the operand is an instruction that became dead as we nulled out the
89 // operand, and if it is 'trivially' dead, delete it in a future loop
90 // iteration.
91 if (Instruction *OpI = dyn_cast<Instruction>(OpV))
92 if (isInstructionTriviallyDead(OpI, TLI))
93 WorkList.insert(OpI);
94 }
95
96 I->eraseFromParent();
97 ++DCEEliminated;
98 return true;
99 }
100 return false;
101}
102
Justin Bogner395c2122016-04-22 19:40:41 +0000103bool eliminateDeadCode(Function &F, TargetLibraryInfo *TLI) {
Chris Lattner49221182005-05-08 18:45:26 +0000104 bool MadeChange = false;
Fiona Glaserb0c6d912015-09-30 17:49:49 +0000105 SmallSetVector<Instruction *, 16> WorkList;
106 // Iterate over the original function, only adding insts to the worklist
107 // if they actually need to be revisited. This avoids having to pre-init
108 // the worklist with the entire function's worth of instructions.
109 for (inst_iterator FI = inst_begin(F), FE = inst_end(F); FI != FE;) {
110 Instruction *I = &*FI;
111 ++FI;
112
113 // We're visiting this instruction now, so make sure it's not in the
114 // worklist from an earlier visit.
115 if (!WorkList.count(I))
116 MadeChange |= DCEInstruction(I, WorkList, TLI);
117 }
118
Chris Lattner87e88062002-05-07 04:24:11 +0000119 while (!WorkList.empty()) {
Fiona Glaserb0c6d912015-09-30 17:49:49 +0000120 Instruction *I = WorkList.pop_back_val();
121 MadeChange |= DCEInstruction(I, WorkList, TLI);
Chris Lattner87e88062002-05-07 04:24:11 +0000122 }
Chris Lattner49221182005-05-08 18:45:26 +0000123 return MadeChange;
Chris Lattner04805fa2002-02-26 21:46:54 +0000124}
125
Justin Bogner395c2122016-04-22 19:40:41 +0000126PreservedAnalyses DCEPass::run(Function &F, AnalysisManager<Function> &AM) {
127 if (eliminateDeadCode(F, AM.getCachedResult<TargetLibraryAnalysis>(F)))
128 return PreservedAnalyses::none();
129 return PreservedAnalyses::all();
Chris Lattner04805fa2002-02-26 21:46:54 +0000130}
Brian Gaeke960707c2003-11-11 22:41:34 +0000131
Justin Bogner395c2122016-04-22 19:40:41 +0000132namespace {
133struct DCELegacyPass : public FunctionPass {
134 static char ID; // Pass identification, replacement for typeid
135 DCELegacyPass() : FunctionPass(ID) {
136 initializeDCELegacyPassPass(*PassRegistry::getPassRegistry());
137 }
138
139 bool runOnFunction(Function &F) override {
Andrew Kayloraa641a52016-04-22 22:06:11 +0000140 if (skipFunction(F))
Justin Bogner395c2122016-04-22 19:40:41 +0000141 return false;
142
143 auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
144 TargetLibraryInfo *TLI = TLIP ? &TLIP->getTLI() : nullptr;
145
146 return eliminateDeadCode(F, TLI);
147 }
148
149 void getAnalysisUsage(AnalysisUsage &AU) const override {
150 AU.setPreservesCFG();
151 }
152};
153}
154
155char DCELegacyPass::ID = 0;
156INITIALIZE_PASS(DCELegacyPass, "dce", "Dead Code Elimination", false, false)
157
158FunctionPass *llvm::createDeadCodeEliminationPass() {
159 return new DCELegacyPass();
160}