blob: 4c964e6e888cabbe6e8c460966a3968b38dd67a9 [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 Carruth6bda14b2017-06-06 11:49:48 +000022#include "llvm/Analysis/TargetLibraryInfo.h"
David Blaikie31b98d22018-06-04 21:23:21 +000023#include "llvm/Transforms/Utils/Local.h"
Chandler Carruth83948572014-03-04 10:30:26 +000024#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/Instruction.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000026#include "llvm/Pass.h"
George Burgess IV7a8dc3d2018-09-13 20:29:50 +000027#include "llvm/Support/DebugCounter.h"
Justin Bogner395c2122016-04-22 19:40:41 +000028#include "llvm/Transforms/Scalar.h"
Chris Lattner49525f82004-01-09 06:02:20 +000029using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000030
Chandler Carruth964daaa2014-04-22 02:55:47 +000031#define DEBUG_TYPE "dce"
32
Chris Lattner79a42ac2006-12-19 21:40:18 +000033STATISTIC(DIEEliminated, "Number of insts removed by DIE pass");
34STATISTIC(DCEEliminated, "Number of insts removed");
George Burgess IV7a8dc3d2018-09-13 20:29:50 +000035DEBUG_COUNTER(DCECounter, "dce-transform",
36 "Controls which instructions are eliminated");
Chris Lattnerbf3a0992002-10-01 22:38:41 +000037
Chris Lattner79a42ac2006-12-19 21:40:18 +000038namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000039 //===--------------------------------------------------------------------===//
40 // DeadInstElimination pass implementation
41 //
Chris Lattner2dd09db2009-09-02 06:11:42 +000042 struct DeadInstElimination : public BasicBlockPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000043 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000044 DeadInstElimination() : BasicBlockPass(ID) {
45 initializeDeadInstEliminationPass(*PassRegistry::getPassRegistry());
46 }
Craig Topper3e4c6972014-03-05 09:10:37 +000047 bool runOnBasicBlock(BasicBlock &BB) override {
Andrew Kayloraa641a52016-04-22 22:06:11 +000048 if (skipBasicBlock(BB))
Paul Robinsonaf4e64d2014-02-06 00:07:05 +000049 return false;
Chandler Carruthb98f63d2015-01-15 10:41:28 +000050 auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
51 TargetLibraryInfo *TLI = TLIP ? &TLIP->getTLI() : nullptr;
Chris Lattner87e88062002-05-07 04:24:11 +000052 bool Changed = false;
Chris Lattnerc92fa422008-11-27 22:46:09 +000053 for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); ) {
Duncan P. N. Exon Smith3a9c9e32015-10-13 18:26:00 +000054 Instruction *Inst = &*DI++;
Benjamin Kramer8bcc9712012-08-29 15:32:21 +000055 if (isInstructionTriviallyDead(Inst, TLI)) {
George Burgess IV7a8dc3d2018-09-13 20:29:50 +000056 if (!DebugCounter::shouldExecute(DCECounter))
57 continue;
Vedant Kumar1df820e2018-02-15 22:26:18 +000058 salvageDebugInfo(*Inst);
Chris Lattnerc92fa422008-11-27 22:46:09 +000059 Inst->eraseFromParent();
Chris Lattner87e88062002-05-07 04:24:11 +000060 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +000061 ++DIEEliminated;
Chris Lattnerc92fa422008-11-27 22:46:09 +000062 }
63 }
Chris Lattner87e88062002-05-07 04:24:11 +000064 return Changed;
65 }
66
Craig Topper3e4c6972014-03-05 09:10:37 +000067 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner820d9712002-10-21 20:00:28 +000068 AU.setPreservesCFG();
Chris Lattner87e88062002-05-07 04:24:11 +000069 }
70 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000071}
Chris Lattner2f7c9632001-06-06 20:29:01 +000072
Dan Gohmand78c4002008-05-13 00:00:25 +000073char DeadInstElimination::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +000074INITIALIZE_PASS(DeadInstElimination, "die",
Owen Andersondf7a4f22010-10-07 22:25:06 +000075 "Dead Instruction Elimination", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000076
Devang Patel5292e652007-01-25 23:23:25 +000077Pass *llvm::createDeadInstEliminationPass() {
Chris Lattner04805fa2002-02-26 21:46:54 +000078 return new DeadInstElimination();
Chris Lattnerccbd4e42002-01-23 05:48:24 +000079}
80
Fiona Glaserb0c6d912015-09-30 17:49:49 +000081static bool DCEInstruction(Instruction *I,
82 SmallSetVector<Instruction *, 16> &WorkList,
83 const TargetLibraryInfo *TLI) {
84 if (isInstructionTriviallyDead(I, TLI)) {
George Burgess IV7a8dc3d2018-09-13 20:29:50 +000085 if (!DebugCounter::shouldExecute(DCECounter))
86 return false;
87
Vedant Kumar1df820e2018-02-15 22:26:18 +000088 salvageDebugInfo(*I);
89
Fiona Glaserb0c6d912015-09-30 17:49:49 +000090 // Null out all of the instruction's operands to see if any operand becomes
91 // dead as we go.
92 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
93 Value *OpV = I->getOperand(i);
94 I->setOperand(i, nullptr);
95
96 if (!OpV->use_empty() || I == OpV)
97 continue;
98
99 // If the operand is an instruction that became dead as we nulled out the
100 // operand, and if it is 'trivially' dead, delete it in a future loop
101 // iteration.
102 if (Instruction *OpI = dyn_cast<Instruction>(OpV))
103 if (isInstructionTriviallyDead(OpI, TLI))
104 WorkList.insert(OpI);
105 }
106
107 I->eraseFromParent();
108 ++DCEEliminated;
109 return true;
110 }
111 return false;
112}
113
Benjamin Kramera65b6102016-05-15 15:18:11 +0000114static bool eliminateDeadCode(Function &F, TargetLibraryInfo *TLI) {
Chris Lattner49221182005-05-08 18:45:26 +0000115 bool MadeChange = false;
Fiona Glaserb0c6d912015-09-30 17:49:49 +0000116 SmallSetVector<Instruction *, 16> WorkList;
117 // Iterate over the original function, only adding insts to the worklist
118 // if they actually need to be revisited. This avoids having to pre-init
119 // the worklist with the entire function's worth of instructions.
120 for (inst_iterator FI = inst_begin(F), FE = inst_end(F); FI != FE;) {
121 Instruction *I = &*FI;
122 ++FI;
123
124 // We're visiting this instruction now, so make sure it's not in the
125 // worklist from an earlier visit.
126 if (!WorkList.count(I))
127 MadeChange |= DCEInstruction(I, WorkList, TLI);
128 }
129
Chris Lattner87e88062002-05-07 04:24:11 +0000130 while (!WorkList.empty()) {
Fiona Glaserb0c6d912015-09-30 17:49:49 +0000131 Instruction *I = WorkList.pop_back_val();
132 MadeChange |= DCEInstruction(I, WorkList, TLI);
Chris Lattner87e88062002-05-07 04:24:11 +0000133 }
Chris Lattner49221182005-05-08 18:45:26 +0000134 return MadeChange;
Chris Lattner04805fa2002-02-26 21:46:54 +0000135}
136
Sean Silva36e0d012016-08-09 00:28:15 +0000137PreservedAnalyses DCEPass::run(Function &F, FunctionAnalysisManager &AM) {
Chandler Carruthca68a3e2017-01-15 06:32:49 +0000138 if (!eliminateDeadCode(F, AM.getCachedResult<TargetLibraryAnalysis>(F)))
139 return PreservedAnalyses::all();
140
141 PreservedAnalyses PA;
142 PA.preserveSet<CFGAnalyses>();
143 return PA;
Chris Lattner04805fa2002-02-26 21:46:54 +0000144}
Brian Gaeke960707c2003-11-11 22:41:34 +0000145
Justin Bogner395c2122016-04-22 19:40:41 +0000146namespace {
147struct DCELegacyPass : public FunctionPass {
148 static char ID; // Pass identification, replacement for typeid
149 DCELegacyPass() : FunctionPass(ID) {
150 initializeDCELegacyPassPass(*PassRegistry::getPassRegistry());
151 }
152
153 bool runOnFunction(Function &F) override {
Andrew Kayloraa641a52016-04-22 22:06:11 +0000154 if (skipFunction(F))
Justin Bogner395c2122016-04-22 19:40:41 +0000155 return false;
156
157 auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
158 TargetLibraryInfo *TLI = TLIP ? &TLIP->getTLI() : nullptr;
159
160 return eliminateDeadCode(F, TLI);
161 }
162
163 void getAnalysisUsage(AnalysisUsage &AU) const override {
164 AU.setPreservesCFG();
165 }
166};
167}
168
169char DCELegacyPass::ID = 0;
170INITIALIZE_PASS(DCELegacyPass, "dce", "Dead Code Elimination", false, false)
171
172FunctionPass *llvm::createDeadCodeEliminationPass() {
173 return new DCELegacyPass();
174}