LLVM 23.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"
18#include "llvm/IR/Function.h"
20#include "llvm/Pass.h"
21#include "llvm/PassRegistry.h"
22
23/// \file RemoveRedundantDebugValues.cpp
24///
25/// The RemoveRedundantDebugValues pass removes redundant DBG_VALUEs that
26/// appear in MIR after the register allocator.
27
28#define DEBUG_TYPE "removeredundantdebugvalues"
29
30using namespace llvm;
31
32STATISTIC(NumRemovedBackward, "Number of DBG_VALUEs removed (backward scan)");
33STATISTIC(NumRemovedForward, "Number of DBG_VALUEs removed (forward scan)");
34
35namespace {
36
37struct RemoveRedundantDebugValuesImpl {
38 bool reduceDbgValues(MachineFunction &MF);
39};
40
41class RemoveRedundantDebugValuesLegacy : public MachineFunctionPass {
42public:
43 static char ID;
44
45 RemoveRedundantDebugValuesLegacy();
46 /// Remove redundant debug value MIs for the given machine function.
47 bool runOnMachineFunction(MachineFunction &MF) override;
48
49 void getAnalysisUsage(AnalysisUsage &AU) const override {
50 AU.setPreservesCFG();
52 }
53};
54
55} // namespace
56
57//===----------------------------------------------------------------------===//
58// Implementation
59//===----------------------------------------------------------------------===//
60
61char RemoveRedundantDebugValuesLegacy::ID = 0;
62
63char &llvm::RemoveRedundantDebugValuesID = RemoveRedundantDebugValuesLegacy::ID;
64
65INITIALIZE_PASS(RemoveRedundantDebugValuesLegacy, DEBUG_TYPE,
66 "Remove Redundant DEBUG_VALUE analysis", false, false)
67
68/// Default construct and initialize the pass.
69RemoveRedundantDebugValuesLegacy::RemoveRedundantDebugValuesLegacy()
71
72// This analysis aims to remove redundant DBG_VALUEs by going forward
73// in the basic block by considering the first DBG_VALUE as a valid
74// until its first (location) operand is not clobbered/modified.
75// For example:
76// (1) DBG_VALUE $edi, !"var1", ...
77// (2) <block of code that does affect $edi>
78// (3) DBG_VALUE $edi, !"var1", ...
79// ...
80// in this case, we can remove (3).
81// TODO: Support DBG_VALUE_LIST and other debug instructions.
83 LLVM_DEBUG(dbgs() << "\n == Forward Scan == \n");
84
85 SmallVector<MachineInstr *, 8> DbgValsToBeRemoved;
87 VariableMap;
88 const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo();
89
90 for (auto &MI : MBB) {
91 if (MI.isDebugValue()) {
92 DebugVariable Var(MI.getDebugVariable(), std::nullopt,
93 MI.getDebugLoc()->getInlinedAt());
94 auto VMI = VariableMap.find(Var);
95 // Just stop tracking this variable, until we cover DBG_VALUE_LIST.
96 // 1 DBG_VALUE $rax, "x", DIExpression()
97 // ...
98 // 2 DBG_VALUE_LIST "x", DIExpression(...), $rax, $rbx
99 // ...
100 // 3 DBG_VALUE $rax, "x", DIExpression()
101 if (MI.isDebugValueList() && VMI != VariableMap.end()) {
102 VariableMap.erase(VMI);
103 continue;
104 }
105
106 MachineOperand &Loc = MI.getDebugOperand(0);
107 if (!Loc.isReg()) {
108 // If it's not a register, just stop tracking such variable.
109 if (VMI != VariableMap.end())
110 VariableMap.erase(VMI);
111 continue;
112 }
113
114 // We have found a new value for a variable.
115 if (VMI == VariableMap.end() ||
116 VMI->second.first->getReg() != Loc.getReg() ||
117 VMI->second.second != MI.getDebugExpression()) {
118 VariableMap[Var] = {&Loc, MI.getDebugExpression()};
119 continue;
120 }
121
122 // Found an identical DBG_VALUE, so it can be considered
123 // for later removal.
124 DbgValsToBeRemoved.push_back(&MI);
125 }
126
127 if (MI.isMetaInstruction())
128 continue;
129
130 // Stop tracking any location that is clobbered by this instruction.
131 VariableMap.remove_if([&](const auto &Var) {
132 return MI.modifiesRegister(Var.second.first->getReg(), TRI);
133 });
134 }
135
136 for (auto &Instr : DbgValsToBeRemoved) {
137 LLVM_DEBUG(dbgs() << "removing "; Instr->dump());
138 Instr->eraseFromParent();
139 ++NumRemovedForward;
140 }
141
142 return !DbgValsToBeRemoved.empty();
143}
144
145// This analysis aims to remove redundant DBG_VALUEs by going backward
146// in the basic block and removing all but the last DBG_VALUE for any
147// given variable in a set of consecutive DBG_VALUE instructions.
148// For example:
149// (1) DBG_VALUE $edi, !"var1", ...
150// (2) DBG_VALUE $esi, !"var2", ...
151// (3) DBG_VALUE $edi, !"var1", ...
152// ...
153// in this case, we can remove (1).
155 LLVM_DEBUG(dbgs() << "\n == Backward Scan == \n");
156 SmallVector<MachineInstr *, 8> DbgValsToBeRemoved;
158
159 for (MachineInstr &MI : llvm::reverse(MBB)) {
160 if (MI.isDebugValue()) {
161 DebugVariable Var(MI.getDebugVariable(), MI.getDebugExpression(),
162 MI.getDebugLoc()->getInlinedAt());
163 auto R = VariableSet.insert(Var);
164 // If it is a DBG_VALUE describing a constant as:
165 // DBG_VALUE 0, ...
166 // we just don't consider such instructions as candidates
167 // for redundant removal.
168 if (MI.isNonListDebugValue()) {
169 MachineOperand &Loc = MI.getDebugOperand(0);
170 if (!Loc.isReg()) {
171 // If we have already encountered this variable, just stop
172 // tracking it.
173 if (!R.second)
174 VariableSet.erase(Var);
175 continue;
176 }
177 }
178
179 // We have already encountered the value for this variable,
180 // so this one can be deleted.
181 if (!R.second)
182 DbgValsToBeRemoved.push_back(&MI);
183 continue;
184 }
185
186 // If we encountered a non-DBG_VALUE, try to find the next
187 // sequence with consecutive DBG_VALUE instructions.
188 VariableSet.clear();
189 }
190
191 for (auto &Instr : DbgValsToBeRemoved) {
192 LLVM_DEBUG(dbgs() << "removing "; Instr->dump());
193 Instr->eraseFromParent();
194 ++NumRemovedBackward;
195 }
196
197 return !DbgValsToBeRemoved.empty();
198}
199
200bool RemoveRedundantDebugValuesImpl::reduceDbgValues(MachineFunction &MF) {
201 LLVM_DEBUG(dbgs() << "\nDebug Value Reduction\n");
202
203 bool Changed = false;
204
205 for (auto &MBB : MF) {
208 }
209
210 return Changed;
211}
212
213bool RemoveRedundantDebugValuesLegacy::runOnMachineFunction(
214 MachineFunction &MF) {
215 // Skip functions without debugging information or functions from NoDebug
216 // compilation units.
217 if (!MF.getFunction().getSubprogram() ||
218 (MF.getFunction().getSubprogram()->getUnit()->getEmissionKind() ==
220 return false;
221
222 return RemoveRedundantDebugValuesImpl().reduceDbgValues(MF);
223}
224
225PreservedAnalyses
228 // Skip functions without debugging information or functions from NoDebug
229 // compilation units.
230 if (!MF.getFunction().getSubprogram() ||
231 (MF.getFunction().getSubprogram()->getUnit()->getEmissionKind() ==
233 return PreservedAnalyses::all();
234
235 if (!RemoveRedundantDebugValuesImpl().reduceDbgValues(MF))
236 return PreservedAnalyses::all();
237
239 PA.preserveSet<CFGAnalyses>();
240 return PA;
241}
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
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:270
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:178
bool erase(const KeyT &Val)
Definition DenseMap.h:332
bool remove_if(Predicate Pred)
Remove entries that match the given predicate.
Definition DenseMap.h:348
iterator end()
Definition DenseMap.h:85
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:301
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:212
bool erase(const ValueT &V)
Definition DenseSet.h:100
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.