LLVM  10.0.0
DCE.cpp
Go to the documentation of this file.
1 //===- DCE.cpp - Code to perform dead code elimination --------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements dead inst elimination and dead code elimination.
10 //
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.
15 //
16 //===----------------------------------------------------------------------===//
17 
19 #include "llvm/ADT/SetVector.h"
20 #include "llvm/ADT/Statistic.h"
22 #include "llvm/IR/InstIterator.h"
23 #include "llvm/IR/Instruction.h"
24 #include "llvm/InitializePasses.h"
25 #include "llvm/Pass.h"
27 #include "llvm/Transforms/Scalar.h"
30 using namespace llvm;
31 
32 #define DEBUG_TYPE "dce"
33 
34 STATISTIC(DIEEliminated, "Number of insts removed by DIE pass");
35 STATISTIC(DCEEliminated, "Number of insts removed");
36 DEBUG_COUNTER(DCECounter, "dce-transform",
37  "Controls which instructions are eliminated");
38 
39 namespace {
40  //===--------------------------------------------------------------------===//
41  // DeadInstElimination pass implementation
42  //
43 struct DeadInstElimination : public FunctionPass {
44  static char ID; // Pass identification, replacement for typeid
45  DeadInstElimination() : FunctionPass(ID) {
47  }
48  bool runOnFunction(Function &F) override {
49  if (skipFunction(F))
50  return false;
51  auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
52  TargetLibraryInfo *TLI = TLIP ? &TLIP->getTLI(F) : nullptr;
53 
54  bool Changed = false;
55  for (auto &BB : F) {
56  for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); ) {
57  Instruction *Inst = &*DI++;
58  if (isInstructionTriviallyDead(Inst, TLI)) {
59  if (!DebugCounter::shouldExecute(DCECounter))
60  continue;
61  salvageDebugInfo(*Inst);
62  Inst->eraseFromParent();
63  Changed = true;
64  ++DIEEliminated;
65  }
66  }
67  }
68  return Changed;
69  }
70 
71  void getAnalysisUsage(AnalysisUsage &AU) const override {
72  AU.setPreservesCFG();
73  }
74 };
75 }
76 
78 INITIALIZE_PASS(DeadInstElimination, "die",
79  "Dead Instruction Elimination", false, false)
80 
82  return new DeadInstElimination();
83 }
84 
85 //===--------------------------------------------------------------------===//
86 // RedundantDbgInstElimination pass implementation
87 //
88 
89 namespace {
90 struct RedundantDbgInstElimination : public FunctionPass {
91  static char ID; // Pass identification, replacement for typeid
92  RedundantDbgInstElimination() : FunctionPass(ID) {
94  }
95  bool runOnFunction(Function &F) override {
96  if (skipFunction(F))
97  return false;
98  bool Changed = false;
99  for (auto &BB : F)
100  Changed |= RemoveRedundantDbgInstrs(&BB);
101  return Changed;
102  }
103 
104  void getAnalysisUsage(AnalysisUsage &AU) const override {
105  AU.setPreservesCFG();
106  }
107 };
108 }
109 
111 INITIALIZE_PASS(RedundantDbgInstElimination, "redundant-dbg-inst-elim",
112  "Redundant Dbg Instruction Elimination", false, false)
113 
115  return new RedundantDbgInstElimination();
116 }
117 
118 //===--------------------------------------------------------------------===//
119 // DeadCodeElimination pass implementation
120 //
121 
124  const TargetLibraryInfo *TLI) {
125  if (isInstructionTriviallyDead(I, TLI)) {
126  if (!DebugCounter::shouldExecute(DCECounter))
127  return false;
128 
129  salvageDebugInfo(*I);
130 
131  // Null out all of the instruction's operands to see if any operand becomes
132  // dead as we go.
133  for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
134  Value *OpV = I->getOperand(i);
135  I->setOperand(i, nullptr);
136 
137  if (!OpV->use_empty() || I == OpV)
138  continue;
139 
140  // If the operand is an instruction that became dead as we nulled out the
141  // operand, and if it is 'trivially' dead, delete it in a future loop
142  // iteration.
143  if (Instruction *OpI = dyn_cast<Instruction>(OpV))
144  if (isInstructionTriviallyDead(OpI, TLI))
145  WorkList.insert(OpI);
146  }
147 
148  I->eraseFromParent();
149  ++DCEEliminated;
150  return true;
151  }
152  return false;
153 }
154 
156  bool MadeChange = false;
158  // Iterate over the original function, only adding insts to the worklist
159  // if they actually need to be revisited. This avoids having to pre-init
160  // the worklist with the entire function's worth of instructions.
161  for (inst_iterator FI = inst_begin(F), FE = inst_end(F); FI != FE;) {
162  Instruction *I = &*FI;
163  ++FI;
164 
165  // We're visiting this instruction now, so make sure it's not in the
166  // worklist from an earlier visit.
167  if (!WorkList.count(I))
168  MadeChange |= DCEInstruction(I, WorkList, TLI);
169  }
170 
171  while (!WorkList.empty()) {
172  Instruction *I = WorkList.pop_back_val();
173  MadeChange |= DCEInstruction(I, WorkList, TLI);
174  }
175  return MadeChange;
176 }
177 
180  return PreservedAnalyses::all();
181 
183  PA.preserveSet<CFGAnalyses>();
184  return PA;
185 }
186 
187 namespace {
188 struct DCELegacyPass : public FunctionPass {
189  static char ID; // Pass identification, replacement for typeid
190  DCELegacyPass() : FunctionPass(ID) {
192  }
193 
194  bool runOnFunction(Function &F) override {
195  if (skipFunction(F))
196  return false;
197 
198  auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
199  TargetLibraryInfo *TLI = TLIP ? &TLIP->getTLI(F) : nullptr;
200 
201  return eliminateDeadCode(F, TLI);
202  }
203 
204  void getAnalysisUsage(AnalysisUsage &AU) const override {
205  AU.setPreservesCFG();
206  }
207 };
208 }
209 
210 char DCELegacyPass::ID = 0;
211 INITIALIZE_PASS(DCELegacyPass, "dce", "Dead Code Elimination", false, false)
212 
214  return new DCELegacyPass();
215 }
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:78
void initializeDCELegacyPassPass(PassRegistry &)
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:67
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Pass * createDeadInstEliminationPass()
bool salvageDebugInfo(Instruction &I)
Assuming the instruction I is going to be deleted, attempt to salvage debug users of I by writing the...
Definition: Local.cpp:1615
void initializeRedundantDbgInstEliminationPass(PassRegistry &)
STATISTIC(NumFunctions, "Total number of functions")
F(f)
INITIALIZE_PASS(DeadInstElimination, "die", "Dead Instruction Elimination", false, false) Pass *llvm
Definition: DCE.cpp:78
inst_iterator inst_begin(Function *F)
Definition: InstIterator.h:131
This file provides an implementation of debug counters.
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:141
FunctionPass * createDeadCodeEliminationPass()
Definition: DCE.cpp:213
Pass * createRedundantDbgInstEliminationPass()
Value * getOperand(unsigned i) const
Definition: User.h:169
size_type count(const key_type &key) const
Count the number of elements of a given key in the SetVector.
Definition: SetVector.h:210
static bool runOnFunction(Function &F, bool PostInlining)
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
static bool eliminateDeadCode(Function &F, TargetLibraryInfo *TLI)
Definition: DCE.cpp:155
Represent the analysis usage information of a pass.
constexpr double e
Definition: MathExtras.h:57
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:282
static bool shouldExecute(unsigned CounterName)
Definition: DebugCounter.h:73
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:160
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:297
Iterator for intrusive lists based on ilist_node.
unsigned getNumOperands() const
Definition: User.h:191
Provides information about what library functions are available for the current target.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:256
void setOperand(unsigned i, Value *Val)
Definition: User.h:174
Represents analyses that only rely on functions&#39; control flow.
Definition: PassManager.h:115
bool RemoveRedundantDbgInstrs(BasicBlock *BB)
Try to remove redundant dbg.value instructions from given basic block.
static bool DCEInstruction(Instruction *I, SmallSetVector< Instruction *, 16 > &WorkList, const TargetLibraryInfo *TLI)
Definition: DCE.cpp:122
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: DCE.cpp:178
void preserveSet()
Mark an analysis set as preserved.
Definition: PassManager.h:190
#define I(x, y, z)
Definition: MD5.cpp:58
bool empty() const
Determine if the SetVector is empty or not.
Definition: SetVector.h:72
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
Definition: PassManager.h:796
DEBUG_COUNTER(DCECounter, "dce-transform", "Controls which instructions are eliminated")
Analysis pass providing the TargetLibraryInfo.
bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction has no side ef...
Definition: Local.cpp:359
LLVM Value Representation.
Definition: Value.h:74
inst_iterator inst_end(Function *F)
Definition: InstIterator.h:132
A container for analyses that lazily runs them and caches their results.
bool use_empty() const
Definition: Value.h:343
void initializeDeadInstEliminationPass(PassRegistry &)