blob: 479e0ed7407430566d6b1f9e0c5285798666fb71 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Brukmanb1c93172005-04-21 23:48:37 +00006//
John Criswell482202a2003-10-20 19:43:21 +00007//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00008//
Chris Lattner87e88062002-05-07 04:24:11 +00009// This file implements dead inst elimination and dead code elimination.
Chris Lattner2f7c9632001-06-06 20:29:01 +000010//
Chris Lattner87e88062002-05-07 04:24:11 +000011// 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 Lattner2f7c9632001-06-06 20:29:01 +000015//
16//===----------------------------------------------------------------------===//
17
Justin Bogner395c2122016-04-22 19:40:41 +000018#include "llvm/Transforms/Scalar/DCE.h"
Fiona Glaserb0c6d912015-09-30 17:49:49 +000019#include "llvm/ADT/SetVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/ADT/Statistic.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000021#include "llvm/Analysis/TargetLibraryInfo.h"
David Blaikie31b98d22018-06-04 21:23:21 +000022#include "llvm/Transforms/Utils/Local.h"
Chandler Carruth83948572014-03-04 10:30:26 +000023#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Instruction.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000025#include "llvm/Pass.h"
George Burgess IV7a8dc3d2018-09-13 20:29:50 +000026#include "llvm/Support/DebugCounter.h"
Justin Bogner395c2122016-04-22 19:40:41 +000027#include "llvm/Transforms/Scalar.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");
George Burgess IV7a8dc3d2018-09-13 20:29:50 +000034DEBUG_COUNTER(DCECounter, "dce-transform",
35 "Controls which instructions are eliminated");
Chris Lattnerbf3a0992002-10-01 22:38:41 +000036
Chris Lattner79a42ac2006-12-19 21:40:18 +000037namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000038 //===--------------------------------------------------------------------===//
39 // DeadInstElimination pass implementation
40 //
Chris Lattner2dd09db2009-09-02 06:11:42 +000041 struct DeadInstElimination : public BasicBlockPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000042 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000043 DeadInstElimination() : BasicBlockPass(ID) {
44 initializeDeadInstEliminationPass(*PassRegistry::getPassRegistry());
45 }
Craig Topper3e4c6972014-03-05 09:10:37 +000046 bool runOnBasicBlock(BasicBlock &BB) override {
Andrew Kayloraa641a52016-04-22 22:06:11 +000047 if (skipBasicBlock(BB))
Paul Robinsonaf4e64d2014-02-06 00:07:05 +000048 return false;
Chandler Carruthb98f63d2015-01-15 10:41:28 +000049 auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
50 TargetLibraryInfo *TLI = TLIP ? &TLIP->getTLI() : nullptr;
Chris Lattner87e88062002-05-07 04:24:11 +000051 bool Changed = false;
Chris Lattnerc92fa422008-11-27 22:46:09 +000052 for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); ) {
Duncan P. N. Exon Smith3a9c9e32015-10-13 18:26:00 +000053 Instruction *Inst = &*DI++;
Benjamin Kramer8bcc9712012-08-29 15:32:21 +000054 if (isInstructionTriviallyDead(Inst, TLI)) {
George Burgess IV7a8dc3d2018-09-13 20:29:50 +000055 if (!DebugCounter::shouldExecute(DCECounter))
56 continue;
Vedant Kumar1df820e2018-02-15 22:26:18 +000057 salvageDebugInfo(*Inst);
Chris Lattnerc92fa422008-11-27 22:46:09 +000058 Inst->eraseFromParent();
Chris Lattner87e88062002-05-07 04:24:11 +000059 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +000060 ++DIEEliminated;
Chris Lattnerc92fa422008-11-27 22:46:09 +000061 }
62 }
Chris Lattner87e88062002-05-07 04:24:11 +000063 return Changed;
64 }
65
Craig Topper3e4c6972014-03-05 09:10:37 +000066 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner820d9712002-10-21 20:00:28 +000067 AU.setPreservesCFG();
Chris Lattner87e88062002-05-07 04:24:11 +000068 }
69 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000070}
Chris Lattner2f7c9632001-06-06 20:29:01 +000071
Dan Gohmand78c4002008-05-13 00:00:25 +000072char DeadInstElimination::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +000073INITIALIZE_PASS(DeadInstElimination, "die",
Owen Andersondf7a4f22010-10-07 22:25:06 +000074 "Dead Instruction Elimination", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000075
Devang Patel5292e652007-01-25 23:23:25 +000076Pass *llvm::createDeadInstEliminationPass() {
Chris Lattner04805fa2002-02-26 21:46:54 +000077 return new DeadInstElimination();
Chris Lattnerccbd4e42002-01-23 05:48:24 +000078}
79
Fiona Glaserb0c6d912015-09-30 17:49:49 +000080static bool DCEInstruction(Instruction *I,
81 SmallSetVector<Instruction *, 16> &WorkList,
82 const TargetLibraryInfo *TLI) {
83 if (isInstructionTriviallyDead(I, TLI)) {
George Burgess IV7a8dc3d2018-09-13 20:29:50 +000084 if (!DebugCounter::shouldExecute(DCECounter))
85 return false;
86
Vedant Kumar1df820e2018-02-15 22:26:18 +000087 salvageDebugInfo(*I);
88
Fiona Glaserb0c6d912015-09-30 17:49:49 +000089 // 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 Kramera65b6102016-05-15 15:18:11 +0000113static bool eliminateDeadCode(Function &F, TargetLibraryInfo *TLI) {
Chris Lattner49221182005-05-08 18:45:26 +0000114 bool MadeChange = false;
Fiona Glaserb0c6d912015-09-30 17:49:49 +0000115 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 Lattner87e88062002-05-07 04:24:11 +0000129 while (!WorkList.empty()) {
Fiona Glaserb0c6d912015-09-30 17:49:49 +0000130 Instruction *I = WorkList.pop_back_val();
131 MadeChange |= DCEInstruction(I, WorkList, TLI);
Chris Lattner87e88062002-05-07 04:24:11 +0000132 }
Chris Lattner49221182005-05-08 18:45:26 +0000133 return MadeChange;
Chris Lattner04805fa2002-02-26 21:46:54 +0000134}
135
Sean Silva36e0d012016-08-09 00:28:15 +0000136PreservedAnalyses DCEPass::run(Function &F, FunctionAnalysisManager &AM) {
Chandler Carruthca68a3e2017-01-15 06:32:49 +0000137 if (!eliminateDeadCode(F, AM.getCachedResult<TargetLibraryAnalysis>(F)))
138 return PreservedAnalyses::all();
139
140 PreservedAnalyses PA;
141 PA.preserveSet<CFGAnalyses>();
142 return PA;
Chris Lattner04805fa2002-02-26 21:46:54 +0000143}
Brian Gaeke960707c2003-11-11 22:41:34 +0000144
Justin Bogner395c2122016-04-22 19:40:41 +0000145namespace {
146struct 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 Kayloraa641a52016-04-22 22:06:11 +0000153 if (skipFunction(F))
Justin Bogner395c2122016-04-22 19:40:41 +0000154 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
168char DCELegacyPass::ID = 0;
169INITIALIZE_PASS(DCELegacyPass, "dce", "Dead Code Elimination", false, false)
170
171FunctionPass *llvm::createDeadCodeEliminationPass() {
172 return new DCELegacyPass();
173}