LLVM 20.0.0git
DebugHandlerBase.cpp
Go to the documentation of this file.
1//===-- llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp -------*- C++ -*--===//
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// Common functionality for different debug information format backends.
10// LLVM currently supports DWARF and CodeView.
11//
12//===----------------------------------------------------------------------===//
13
20#include "llvm/IR/DebugInfo.h"
21#include "llvm/IR/Module.h"
22#include "llvm/MC/MCStreamer.h"
24
25using namespace llvm;
26
27#define DEBUG_TYPE "dwarfdebug"
28
29/// If true, we drop variable location ranges which exist entirely outside the
30/// variable's lexical scope instruction ranges.
31static cl::opt<bool> TrimVarLocs("trim-var-locs", cl::Hidden, cl::init(true));
32
33std::optional<DbgVariableLocation>
36 DbgVariableLocation Location;
37 // Variables calculated from multiple locations can't be represented here.
38 if (Instruction.getNumDebugOperands() != 1)
39 return std::nullopt;
40 if (!Instruction.getDebugOperand(0).isReg())
41 return std::nullopt;
42 Location.Register = Instruction.getDebugOperand(0).getReg();
43 Location.FragmentInfo.reset();
44 // We only handle expressions generated by DIExpression::appendOffset,
45 // which doesn't require a full stack machine.
46 int64_t Offset = 0;
47 const DIExpression *DIExpr = Instruction.getDebugExpression();
48 auto Op = DIExpr->expr_op_begin();
49 // We can handle a DBG_VALUE_LIST iff it has exactly one location operand that
50 // appears exactly once at the start of the expression.
51 if (Instruction.isDebugValueList()) {
52 if (Instruction.getNumDebugOperands() == 1 &&
53 Op->getOp() == dwarf::DW_OP_LLVM_arg)
54 ++Op;
55 else
56 return std::nullopt;
57 }
58 while (Op != DIExpr->expr_op_end()) {
59 switch (Op->getOp()) {
60 case dwarf::DW_OP_constu: {
61 int Value = Op->getArg(0);
62 ++Op;
63 if (Op != DIExpr->expr_op_end()) {
64 switch (Op->getOp()) {
65 case dwarf::DW_OP_minus:
66 Offset -= Value;
67 break;
68 case dwarf::DW_OP_plus:
69 Offset += Value;
70 break;
71 default:
72 continue;
73 }
74 }
75 } break;
76 case dwarf::DW_OP_plus_uconst:
77 Offset += Op->getArg(0);
78 break;
80 Location.FragmentInfo = {Op->getArg(1), Op->getArg(0)};
81 break;
82 case dwarf::DW_OP_deref:
83 Location.LoadChain.push_back(Offset);
84 Offset = 0;
85 break;
86 default:
87 return std::nullopt;
88 }
89 ++Op;
90 }
91
92 // Do one final implicit DW_OP_deref if this was an indirect DBG_VALUE
93 // instruction.
94 // FIXME: Replace these with DIExpression.
95 if (Instruction.isIndirectDebugValue())
96 Location.LoadChain.push_back(Offset);
97
98 return Location;
99}
100
102
104
106 if (M->debug_compile_units().empty())
107 Asm = nullptr;
108}
109
110// Each LexicalScope has first instruction and last instruction to mark
111// beginning and end of a scope respectively. Create an inverse map that list
112// scopes starts (and ends) with an instruction. One instruction may start (or
113// end) multiple scopes. Ignore scopes that are not reachable.
117 while (!WorkList.empty()) {
118 LexicalScope *S = WorkList.pop_back_val();
119
120 const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
121 if (!Children.empty())
122 WorkList.append(Children.begin(), Children.end());
123
124 if (S->isAbstractScope())
125 continue;
126
127 for (const InsnRange &R : S->getRanges()) {
128 assert(R.first && "InsnRange does not have first instruction!");
129 assert(R.second && "InsnRange does not have second instruction!");
130 requestLabelBeforeInsn(R.first);
131 requestLabelAfterInsn(R.second);
132 }
133 }
134}
135
136// Return Label preceding the instruction.
138 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
139 assert(Label && "Didn't insert label before instruction");
140 return Label;
141}
142
143// Return Label immediately following the instruction.
145 return LabelsAfterInsn.lookup(MI);
146}
147
148/// If this type is derived from a base type then return base type size.
150 assert(Ty);
151 const DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty);
152 if (!DDTy)
153 return Ty->getSizeInBits();
154
155 unsigned Tag = DDTy->getTag();
156
157 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
158 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
159 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_atomic_type &&
160 Tag != dwarf::DW_TAG_immutable_type &&
161 Tag != dwarf::DW_TAG_template_alias)
162 return DDTy->getSizeInBits();
163
164 DIType *BaseType = DDTy->getBaseType();
165
166 if (!BaseType)
167 return 0;
168
169 // If this is a derived type, go ahead and get the base type, unless it's a
170 // reference then it's just the size of the field. Pointer types have no need
171 // of this since they're a different type of qualification on the type.
172 if (BaseType->getTag() == dwarf::DW_TAG_reference_type ||
173 BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type)
174 return Ty->getSizeInBits();
175
177}
178
180 if (isa<DIStringType>(Ty)) {
181 // Some transformations (e.g. instcombine) may decide to turn a Fortran
182 // character object into an integer, and later ones (e.g. SROA) may
183 // further inject a constant integer in a llvm.dbg.value call to track
184 // the object's value. Here we trust the transformations are doing the
185 // right thing, and treat the constant as unsigned to preserve that value
186 // (i.e. avoid sign extension).
187 return true;
188 }
189
190 if (auto *CTy = dyn_cast<DICompositeType>(Ty)) {
191 if (CTy->getTag() == dwarf::DW_TAG_enumeration_type) {
192 if (!(Ty = CTy->getBaseType()))
193 // FIXME: Enums without a fixed underlying type have unknown signedness
194 // here, leading to incorrectly emitted constants.
195 return false;
196 } else
197 // (Pieces of) aggregate types that get hacked apart by SROA may be
198 // represented by a constant. Encode them as unsigned bytes.
199 return true;
200 }
201
202 if (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
203 dwarf::Tag T = (dwarf::Tag)Ty->getTag();
204 // Encode pointer constants as unsigned bytes. This is used at least for
205 // null pointer constant emission.
206 // FIXME: reference and rvalue_reference /probably/ shouldn't be allowed
207 // here, but accept them for now due to a bug in SROA producing bogus
208 // dbg.values.
209 if (T == dwarf::DW_TAG_pointer_type ||
210 T == dwarf::DW_TAG_ptr_to_member_type ||
211 T == dwarf::DW_TAG_reference_type ||
212 T == dwarf::DW_TAG_rvalue_reference_type)
213 return true;
214 assert(T == dwarf::DW_TAG_typedef || T == dwarf::DW_TAG_const_type ||
215 T == dwarf::DW_TAG_volatile_type ||
216 T == dwarf::DW_TAG_restrict_type || T == dwarf::DW_TAG_atomic_type ||
217 T == dwarf::DW_TAG_immutable_type ||
218 T == dwarf::DW_TAG_template_alias);
219 assert(DTy->getBaseType() && "Expected valid base type");
220 return isUnsignedDIType(DTy->getBaseType());
221 }
222
223 auto *BTy = cast<DIBasicType>(Ty);
224 unsigned Encoding = BTy->getEncoding();
225 assert((Encoding == dwarf::DW_ATE_unsigned ||
226 Encoding == dwarf::DW_ATE_unsigned_char ||
227 Encoding == dwarf::DW_ATE_signed ||
228 Encoding == dwarf::DW_ATE_signed_char ||
229 Encoding == dwarf::DW_ATE_float || Encoding == dwarf::DW_ATE_UTF ||
230 Encoding == dwarf::DW_ATE_boolean ||
231 Encoding == dwarf::DW_ATE_complex_float ||
232 Encoding == dwarf::DW_ATE_signed_fixed ||
233 Encoding == dwarf::DW_ATE_unsigned_fixed ||
234 (Ty->getTag() == dwarf::DW_TAG_unspecified_type &&
235 Ty->getName() == "decltype(nullptr)")) &&
236 "Unsupported encoding");
237 return Encoding == dwarf::DW_ATE_unsigned ||
238 Encoding == dwarf::DW_ATE_unsigned_char ||
239 Encoding == dwarf::DW_ATE_UTF || Encoding == dwarf::DW_ATE_boolean ||
240 Encoding == llvm::dwarf::DW_ATE_unsigned_fixed ||
241 Ty->getTag() == dwarf::DW_TAG_unspecified_type;
242}
243
244static bool hasDebugInfo(const MachineFunction *MF) {
245 auto *SP = MF->getFunction().getSubprogram();
246 if (!SP)
247 return false;
248 assert(SP->getUnit());
249 auto EK = SP->getUnit()->getEmissionKind();
250 if (EK == DICompileUnit::NoDebug)
251 return false;
252 return true;
253}
254
256 PrevInstBB = nullptr;
257
258 if (!Asm || !hasDebugInfo(MF)) {
260 return;
261 }
262
263 // Grab the lexical scopes for the function, if we don't have any of those
264 // then we're not going to be able to do anything.
265 LScopes.initialize(*MF);
266 if (LScopes.empty()) {
268 return;
269 }
270
271 // Make sure that each lexical scope will have a begin/end label.
273
274 // Calculate history for local variables.
275 assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
276 assert(DbgLabels.empty() && "DbgLabels map wasn't cleaned!");
279 InstOrdering.initialize(*MF);
280 if (TrimVarLocs)
281 DbgValues.trimLocationRanges(*MF, LScopes, InstOrdering);
283
284 // Request labels for the full history.
285 for (const auto &I : DbgValues) {
286 const auto &Entries = I.second;
287 if (Entries.empty())
288 continue;
289
290 auto IsDescribedByReg = [](const MachineInstr *MI) {
291 return any_of(MI->debug_operands(),
292 [](auto &MO) { return MO.isReg() && MO.getReg(); });
293 };
294
295 // The first mention of a function argument gets the CurrentFnBegin label,
296 // so arguments are visible when breaking at function entry.
297 //
298 // We do not change the label for values that are described by registers,
299 // as that could place them above their defining instructions. We should
300 // ideally not change the labels for constant debug values either, since
301 // doing that violates the ranges that are calculated in the history map.
302 // However, we currently do not emit debug values for constant arguments
303 // directly at the start of the function, so this code is still useful.
304 const DILocalVariable *DIVar =
305 Entries.front().getInstr()->getDebugVariable();
306 if (DIVar->isParameter() &&
307 getDISubprogram(DIVar->getScope())->describes(&MF->getFunction())) {
308 if (!IsDescribedByReg(Entries.front().getInstr()))
309 LabelsBeforeInsn[Entries.front().getInstr()] = Asm->getFunctionBegin();
310 if (Entries.front().getInstr()->getDebugExpression()->isFragment()) {
311 // Mark all non-overlapping initial fragments.
312 for (const auto *I = Entries.begin(); I != Entries.end(); ++I) {
313 if (!I->isDbgValue())
314 continue;
315 const DIExpression *Fragment = I->getInstr()->getDebugExpression();
316 if (std::any_of(Entries.begin(), I,
317 [&](DbgValueHistoryMap::Entry Pred) {
318 return Pred.isDbgValue() &&
319 Fragment->fragmentsOverlap(
320 Pred.getInstr()->getDebugExpression());
321 }))
322 break;
323 // The code that generates location lists for DWARF assumes that the
324 // entries' start labels are monotonically increasing, and since we
325 // don't change the label for fragments that are described by
326 // registers, we must bail out when encountering such a fragment.
327 if (IsDescribedByReg(I->getInstr()))
328 break;
329 LabelsBeforeInsn[I->getInstr()] = Asm->getFunctionBegin();
330 }
331 }
332 }
333
334 for (const auto &Entry : Entries) {
335 if (Entry.isDbgValue())
336 requestLabelBeforeInsn(Entry.getInstr());
337 else
338 requestLabelAfterInsn(Entry.getInstr());
339 }
340 }
341
342 // Ensure there is a symbol before DBG_LABEL.
343 for (const auto &I : DbgLabels) {
344 const MachineInstr *MI = I.second;
346 }
347
351}
352
354 if (!Asm || !Asm->hasDebugInfo())
355 return;
356
357 assert(CurMI == nullptr);
358 CurMI = MI;
359
360 // Insert labels where requested.
362 LabelsBeforeInsn.find(MI);
363
364 // No label needed.
365 if (I == LabelsBeforeInsn.end())
366 return;
367
368 // Label already assigned.
369 if (I->second)
370 return;
371
372 if (!PrevLabel) {
374 Asm->OutStreamer->emitLabel(PrevLabel);
375 }
376 I->second = PrevLabel;
377}
378
380 if (!Asm || !Asm->hasDebugInfo())
381 return;
382
383 assert(CurMI != nullptr);
384 // Don't create a new label after DBG_VALUE and other instructions that don't
385 // generate code.
386 if (!CurMI->isMetaInstruction()) {
387 PrevLabel = nullptr;
389 }
390
393
394 // No label needed or label already assigned.
395 if (I == LabelsAfterInsn.end() || I->second) {
396 CurMI = nullptr;
397 return;
398 }
399
400 // We need a label after this instruction. With basic block sections, just
401 // use the end symbol of the section if this is the last instruction of the
402 // section. This reduces the need for an additional label and also helps
403 // merging ranges.
404 if (CurMI->getParent()->isEndSection() && CurMI->getNextNode() == nullptr) {
406 } else if (!PrevLabel) {
408 Asm->OutStreamer->emitLabel(PrevLabel);
409 }
410 I->second = PrevLabel;
411 CurMI = nullptr;
412}
413
415 if (Asm && hasDebugInfo(MF))
416 endFunctionImpl(MF);
419 LabelsBeforeInsn.clear();
420 LabelsAfterInsn.clear();
421 InstOrdering.clear();
422}
423
425 EpilogBeginBlock = nullptr;
426 if (!MBB.isEntryBlock())
428}
429
431 PrevLabel = nullptr;
432}
MachineBasicBlock & MBB
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static cl::opt< bool > TrimVarLocs("trim-var-locs", cl::Hidden, cl::init(true))
If true, we drop variable location ranges which exist entirely outside the variable's lexical scope i...
static bool hasDebugInfo(const MachineFunction *MF)
#define LLVM_DEBUG(X)
Definition: Debug.h:101
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition: MD5.cpp:58
Module.h This file contains the declarations for the Module class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:86
MCSymbol * getFunctionBegin() const
Definition: AsmPrinter.h:266
MachineFunction * MF
The current machine function.
Definition: AsmPrinter.h:104
bool hasDebugInfo() const
Returns true if valid debug info is present.
Definition: AsmPrinter.h:437
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:101
DWARF expression.
expr_op_iterator expr_op_begin() const
Visit the elements via ExprOperand wrappers.
expr_op_iterator expr_op_end() const
DILocalScope * getScope() const
Get the local scope for this variable.
dwarf::Tag getTag() const
Base class for types.
StringRef getName() const
uint64_t getSizeInBits() const
This class represents an Operation in the Expression.
Specifies a change in a variable's debug value history.
void trimLocationRanges(const MachineFunction &MF, LexicalScopes &LScopes, const InstructionOrdering &Ordering)
Drop location ranges which exist entirely outside each variable's scope.
LLVM_DUMP_METHOD void dump(StringRef FuncName) const
static bool isUnsignedDIType(const DIType *Ty)
Return true if type encoding is unsigned.
const MachineInstr * CurMI
If nonnull, stores the current machine instruction we're processing.
AsmPrinter * Asm
Target of debug info emission.
void endBasicBlockSection(const MachineBasicBlock &MBB)
virtual void endFunctionImpl(const MachineFunction *MF)=0
MCSymbol * getLabelBeforeInsn(const MachineInstr *MI)
Return Label preceding the instruction.
MachineModuleInfo * MMI
Collected machine module information.
void identifyScopeMarkers()
Indentify instructions that are marking the beginning of or ending of a scope.
void beginBasicBlockSection(const MachineBasicBlock &MBB)
virtual void skippedNonDebugFunction()
virtual void beginModule(Module *M)
DebugLoc PrevInstLoc
Previous instruction's location information.
void endFunction(const MachineFunction *MF)
MCSymbol * getLabelAfterInsn(const MachineInstr *MI)
Return Label immediately following the instruction.
DebugHandlerBase(AsmPrinter *A)
const MachineBasicBlock * PrevInstBB
virtual void beginFunctionImpl(const MachineFunction *MF)=0
void requestLabelAfterInsn(const MachineInstr *MI)
Ensure that a label will be emitted after MI.
DbgValueHistoryMap DbgValues
History of DBG_VALUE and clobber instructions for each user variable.
DbgLabelInstrMap DbgLabels
Mapping of inlined labels and DBG_LABEL machine instruction.
DenseMap< const MachineInstr *, MCSymbol * > LabelsBeforeInsn
Maps instruction with label emitted before instruction.
void beginFunction(const MachineFunction *MF)
DenseMap< const MachineInstr *, MCSymbol * > LabelsAfterInsn
Maps instruction with label emitted after instruction.
void requestLabelBeforeInsn(const MachineInstr *MI)
Ensure that a label will be emitted before MI.
virtual void beginInstruction(const MachineInstr *MI)
const MachineBasicBlock * EpilogBeginBlock
This block includes epilogue instructions.
static uint64_t getBaseTypeSize(const DIType *Ty)
If this type is derived from a base type then return base type size.
A debug info location.
Definition: DebugLoc.h:33
DISubprogram * getSubprogram() const
Get the attached subprogram.
Definition: Metadata.cpp:1837
void initialize(const MachineFunction &MF)
LexicalScope - This class is used to track scope information.
Definition: LexicalScopes.h:44
SmallVectorImpl< LexicalScope * > & getChildren()
Definition: LexicalScopes.h:65
SmallVectorImpl< InsnRange > & getRanges()
Definition: LexicalScopes.h:66
bool isAbstractScope() const
Definition: LexicalScopes.h:64
void initialize(const MachineFunction &)
initialize - Scan machine function and constuct lexical scope nest, resets the instance if necessary.
bool empty()
empty - Return true if there is any lexical scope information available.
LexicalScope * getCurrentFunctionScope() const
getCurrentFunctionScope - Return lexical scope for the current function.
MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
Definition: MCContext.cpp:346
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
MCSymbol * getEndSymbol() const
Returns the MCSymbol marking the end of this basic block.
bool isEntryBlock() const
Returns true if this is the entry block of the function.
bool isEndSection() const
Returns true if this block ends any section.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
Definition: MachineInstr.h:69
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:346
bool isMetaInstruction(QueryType Type=IgnoreBundle) const
Return true if this instruction doesn't produce any output in the form of executable instructions.
Definition: MachineInstr.h:936
const MCContext & getContext() const
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
bool empty() const
Definition: SmallVector.h:94
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:696
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
LLVM Value Representation.
Definition: Value.h:74
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition: ilist_node.h:353
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
@ DW_OP_LLVM_fragment
Only used in LLVM metadata.
Definition: Dwarf.h:142
@ DW_OP_LLVM_arg
Only used in LLVM metadata.
Definition: Dwarf.h:147
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
void calculateDbgEntityHistory(const MachineFunction *MF, const TargetRegisterInfo *TRI, DbgValueHistoryMap &DbgValues, DbgLabelInstrMap &DbgLabels)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1729
DWARFExpression::Operation Op
std::pair< const MachineInstr *, const MachineInstr * > InsnRange
InsnRange - This is used to track range of instructions with identical lexical scope.
Definition: LexicalScopes.h:39
DISubprogram * getDISubprogram(const MDNode *Scope)
Find subprogram that is enclosing this scope.
Definition: DebugInfo.cpp:152
Represents the location at which a variable is stored.
static std::optional< DbgVariableLocation > extractFromMachineInstruction(const MachineInstr &Instruction)
Extract a VariableLocation from a MachineInstr.