LLVM 24.0.0git
RemoveRedundantDebugValues.cpp
Go to the documentation of this file.
1//===- RemoveRedundantDebugValues.cpp - Remove Redundant Debug Value MIs --===//
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
10#include "llvm/ADT/DenseMap.h"
11#include "llvm/ADT/DenseSet.h"
13#include "llvm/ADT/Statistic.h"
19#include "llvm/IR/Function.h"
21#include "llvm/Pass.h"
22#include "llvm/PassRegistry.h"
23
24/// \file RemoveRedundantDebugValues.cpp
25///
26/// The RemoveRedundantDebugValues pass removes redundant DBG_VALUEs that
27/// appear in MIR after the register allocator.
28
29#define DEBUG_TYPE "removeredundantdebugvalues"
30
31using namespace llvm;
32
33STATISTIC(NumRemovedBackward, "Number of DBG_VALUEs removed (backward scan)");
34STATISTIC(NumRemovedForward, "Number of DBG_VALUEs removed (forward scan)");
35
36namespace {
37
38struct RemoveRedundantDebugValuesImpl {
39 bool reduceDbgValues(MachineFunction &MF);
40};
41
42class RemoveRedundantDebugValuesLegacy : public MachineFunctionPass {
43public:
44 static char ID;
45
46 RemoveRedundantDebugValuesLegacy();
47 /// Remove redundant debug value MIs for the given machine function.
48 bool runOnMachineFunction(MachineFunction &MF) override;
49
50 void getAnalysisUsage(AnalysisUsage &AU) const override {
51 AU.setPreservesCFG();
52 AU.addPreserved<MachineRegisterClassInfoWrapperPass>();
54 }
55};
56
57} // namespace
58
59//===----------------------------------------------------------------------===//
60// Implementation
61//===----------------------------------------------------------------------===//
62
63char RemoveRedundantDebugValuesLegacy::ID = 0;
64
65char &llvm::RemoveRedundantDebugValuesID = RemoveRedundantDebugValuesLegacy::ID;
66
67INITIALIZE_PASS(RemoveRedundantDebugValuesLegacy, DEBUG_TYPE,
68 "Remove Redundant DEBUG_VALUE analysis", false, false)
69
70/// Default construct and initialize the pass.
71RemoveRedundantDebugValuesLegacy::RemoveRedundantDebugValuesLegacy()
73
74// This analysis aims to remove redundant DBG_VALUEs by going forward
75// in the basic block by considering the first DBG_VALUE as a valid
76// until its first (location) operand is not clobbered/modified.
77// For example:
78// (1) DBG_VALUE $edi, !"var1", ...
79// (2) <block of code that does affect $edi>
80// (3) DBG_VALUE $edi, !"var1", ...
81// ...
82// in this case, we can remove (3).
83// TODO: Support DBG_VALUE_LIST and other debug instructions.
85 LLVM_DEBUG(dbgs() << "\n == Forward Scan == \n");
86
87 SmallVector<MachineInstr *, 8> DbgValsToBeRemoved;
89 VariableMap;
90 const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo();
91
92 for (auto &MI : MBB) {
93 if (MI.isDebugValue()) {
94 DebugVariable Var(MI.getDebugVariable(), std::nullopt,
95 MI.getDebugLoc()->getInlinedAt());
96 auto VMI = VariableMap.find(Var);
97 // Just stop tracking this variable, until we cover DBG_VALUE_LIST.
98 // 1 DBG_VALUE $rax, "x", DIExpression()
99 // ...
100 // 2 DBG_VALUE_LIST "x", DIExpression(...), $rax, $rbx
101 // ...
102 // 3 DBG_VALUE $rax, "x", DIExpression()
103 if (MI.isDebugValueList() && VMI != VariableMap.end()) {
104 VariableMap.erase(VMI);
105 continue;
106 }
107
108 MachineOperand &Loc = MI.getDebugOperand(0);
109 if (!Loc.isReg()) {
110 // If it's not a register, just stop tracking such variable.
111 if (VMI != VariableMap.end())
112 VariableMap.erase(VMI);
113 continue;
114 }
115
116 // We have found a new value for a variable.
117 if (VMI == VariableMap.end() ||
118 VMI->second.first->getReg() != Loc.getReg() ||
119 VMI->second.second != MI.getDebugExpression()) {
120 VariableMap[Var] = {&Loc, MI.getDebugExpression()};
121 continue;
122 }
123
124 // Found an identical DBG_VALUE, so it can be considered
125 // for later removal.
126 DbgValsToBeRemoved.push_back(&MI);
127 }
128
129 if (MI.isMetaInstruction())
130 continue;
131
132 // Stop tracking any location that is clobbered by this instruction.
133 VariableMap.remove_if([&](const auto &Var) {
134 return MI.modifiesRegister(Var.second.first->getReg(), TRI);
135 });
136 }
137
138 for (auto &Instr : DbgValsToBeRemoved) {
139 LLVM_DEBUG(dbgs() << "removing "; Instr->dump());
140 Instr->eraseFromParent();
141 ++NumRemovedForward;
142 }
143
144 return !DbgValsToBeRemoved.empty();
145}
146
147// This analysis aims to remove redundant DBG_VALUEs by going backward
148// in the basic block and removing all but the last DBG_VALUE for any
149// given variable in a set of consecutive DBG_VALUE instructions.
150// For example:
151// (1) DBG_VALUE $edi, !"var1", ...
152// (2) DBG_VALUE $esi, !"var2", ...
153// (3) DBG_VALUE $edi, !"var1", ...
154// ...
155// in this case, we can remove (1).
157 LLVM_DEBUG(dbgs() << "\n == Backward Scan == \n");
158 SmallVector<MachineInstr *, 8> DbgValsToBeRemoved;
160
161 for (MachineInstr &MI : llvm::reverse(MBB)) {
162 if (MI.isDebugValue()) {
163 DebugVariable Var(MI.getDebugVariable(), MI.getDebugExpression(),
164 MI.getDebugLoc()->getInlinedAt());
165 auto R = VariableSet.insert(Var);
166 // If it is a DBG_VALUE describing a constant as:
167 // DBG_VALUE 0, ...
168 // we just don't consider such instructions as candidates
169 // for redundant removal.
170 if (MI.isNonListDebugValue()) {
171 MachineOperand &Loc = MI.getDebugOperand(0);
172 if (!Loc.isReg()) {
173 // If we have already encountered this variable, just stop
174 // tracking it.
175 if (!R.second)
176 VariableSet.erase(Var);
177 continue;
178 }
179 }
180
181 // We have already encountered the value for this variable,
182 // so this one can be deleted.
183 if (!R.second)
184 DbgValsToBeRemoved.push_back(&MI);
185 continue;
186 }
187
188 // If we encountered a non-DBG_VALUE, try to find the next
189 // sequence with consecutive DBG_VALUE instructions.
190 VariableSet.clear();
191 }
192
193 for (auto &Instr : DbgValsToBeRemoved) {
194 LLVM_DEBUG(dbgs() << "removing "; Instr->dump());
195 Instr->eraseFromParent();
196 ++NumRemovedBackward;
197 }
198
199 return !DbgValsToBeRemoved.empty();
200}
201
202bool RemoveRedundantDebugValuesImpl::reduceDbgValues(MachineFunction &MF) {
203 LLVM_DEBUG(dbgs() << "\nDebug Value Reduction\n");
204
205 bool Changed = false;
206
207 for (auto &MBB : MF) {
210 }
211
212 return Changed;
213}
214
215bool RemoveRedundantDebugValuesLegacy::runOnMachineFunction(
216 MachineFunction &MF) {
217 // Skip functions without debugging information or functions from NoDebug
218 // compilation units.
219 if (!MF.getFunction().getSubprogram() ||
220 (MF.getFunction().getSubprogram()->getUnit()->getEmissionKind() ==
222 return false;
223
224 return RemoveRedundantDebugValuesImpl().reduceDbgValues(MF);
225}
226
227PreservedAnalyses
230 // Skip functions without debugging information or functions from NoDebug
231 // compilation units.
232 if (!MF.getFunction().getSubprogram() ||
233 (MF.getFunction().getSubprogram()->getUnit()->getEmissionKind() ==
235 return PreservedAnalyses::all();
236
237 if (!RemoveRedundantDebugValuesImpl().reduceDbgValues(MF))
238 return PreservedAnalyses::all();
239
241 PA.preserveSet<CFGAnalyses>();
242 return PA;
243}
MachineBasicBlock & MBB
This file defines the DenseMap class.
This file defines the DenseSet and SmallDenseSet classes.
#define DEBUG_TYPE
IRTranslator LLVM IR MI
Register const TargetRegisterInfo * TRI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static bool reduceDbgValsForwardScan(MachineBasicBlock &MBB)
static bool reduceDbgValsBackwardScan(MachineBasicBlock &MBB)
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:119
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:275
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
Identifies a unique instance of a variable.
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:223
bool erase(const KeyT &Val)
Definition DenseMap.h:377
bool remove_if(Predicate Pred)
Remove entries that match the given predicate.
Definition DenseMap.h:393
iterator end()
Definition DenseMap.h:141
DISubprogram * getSubprogram() const
Get the attached subprogram.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Implements a dense probed hash-table based set with some number of buckets stored inline.
Definition DenseSet.h:293
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:209
bool erase(const ValueT &V)
Definition DenseSet.h:97
Changed
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
auto reverse(ContainerTy &&C)
Definition STLExtras.h:407
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI char & RemoveRedundantDebugValuesID
RemoveRedundantDebugValues pass.