LLVM 19.0.0git
DebugInfo.h
Go to the documentation of this file.
1//===- DebugInfo.h - Debug Information Helpers ------------------*- 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// This file defines a bunch of datatypes that are useful for creating and
10// walking debug info in LLVM IR form. They essentially provide wrappers around
11// the information in the global variables that's needed when constructing the
12// DWARF information.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_IR_DEBUGINFO_H
17#define LLVM_IR_DEBUGINFO_H
18
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/SetVector.h"
23#include "llvm/ADT/SmallSet.h"
27#include "llvm/IR/DataLayout.h"
29#include "llvm/IR/PassManager.h"
30#include <optional>
31
32namespace llvm {
33
34class DbgDeclareInst;
35class DbgValueInst;
36class DbgVariableIntrinsic;
37class DbgVariableRecord;
38class Instruction;
39class Module;
40
41/// Finds dbg.declare intrinsics declaring local variables as living in the
42/// memory that 'V' points to.
43TinyPtrVector<DbgDeclareInst *> findDbgDeclares(Value *V);
44/// As above, for DVRDeclares.
45TinyPtrVector<DbgVariableRecord *> findDVRDeclares(Value *V);
46
47/// Finds the llvm.dbg.value intrinsics describing a value.
48void findDbgValues(
49 SmallVectorImpl<DbgValueInst *> &DbgValues, Value *V,
50 SmallVectorImpl<DbgVariableRecord *> *DbgVariableRecords = nullptr);
51
52/// Finds the debug info intrinsics describing a value.
53void findDbgUsers(
54 SmallVectorImpl<DbgVariableIntrinsic *> &DbgInsts, Value *V,
55 SmallVectorImpl<DbgVariableRecord *> *DbgVariableRecords = nullptr);
56
57/// Find subprogram that is enclosing this scope.
58DISubprogram *getDISubprogram(const MDNode *Scope);
59
60/// Produce a DebugLoc to use for each dbg.declare that is promoted to a
61/// dbg.value.
62DebugLoc getDebugValueLoc(DbgVariableIntrinsic *DII);
63DebugLoc getDebugValueLoc(DbgVariableRecord *DVR);
64
65/// Strip debug info in the module if it exists.
66///
67/// To do this, we remove all calls to the debugger intrinsics and any named
68/// metadata for debugging. We also remove debug locations for instructions.
69/// Return true if module is modified.
70bool StripDebugInfo(Module &M);
71bool stripDebugInfo(Function &F);
72
73/// Downgrade the debug info in a module to contain only line table information.
74///
75/// In order to convert debug info to what -gline-tables-only would have
76/// created, this does the following:
77/// 1) Delete all debug intrinsics.
78/// 2) Delete all non-CU named metadata debug info nodes.
79/// 3) Create new DebugLocs for each instruction.
80/// 4) Create a new CU debug info, and similarly for every metadata node
81/// that's reachable from the CU debug info.
82/// All debug type metadata nodes are unreachable and garbage collected.
84
85/// Update the debug locations contained within the MD_loop metadata attached
86/// to the instruction \p I, if one exists. \p Updater is applied to Metadata
87/// operand in the MD_loop metadata: the returned value is included in the
88/// updated loop metadata node if it is non-null.
90 Instruction &I, function_ref<Metadata *(Metadata *)> Updater);
91
92/// Return Debug Info Metadata Version by checking module flags.
94
95/// Utility to find all debug info in a module.
96///
97/// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
98/// list debug info MDNodes used by an instruction, DebugInfoFinder uses
99/// processDeclare, processValue and processLocation to handle DbgDeclareInst,
100/// DbgValueInst and DbgLoc attached to instructions. processModule will go
101/// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
102/// used by the CUs.
104public:
105 /// Process entire module and collect debug info anchors.
106 void processModule(const Module &M);
107 /// Process a single instruction and collect debug info anchors.
108 void processInstruction(const Module &M, const Instruction &I);
109
110 /// Process a DILocalVariable.
111 void processVariable(const Module &M, const DILocalVariable *DVI);
112 /// Process debug info location.
113 void processLocation(const Module &M, const DILocation *Loc);
114 /// Process a DbgRecord (e.g, treat a DbgVariableRecord like a
115 /// DbgVariableIntrinsic).
116 void processDbgRecord(const Module &M, const DbgRecord &DR);
117
118 /// Process subprogram.
120
121 /// Clear all lists.
122 void reset();
123
124private:
125 void processCompileUnit(DICompileUnit *CU);
126 void processScope(DIScope *Scope);
127 void processType(DIType *DT);
128 bool addCompileUnit(DICompileUnit *CU);
129 bool addGlobalVariable(DIGlobalVariableExpression *DIG);
130 bool addScope(DIScope *Scope);
131 bool addSubprogram(DISubprogram *SP);
132 bool addType(DIType *DT);
133
134public:
142
144 return make_range(CUs.begin(), CUs.end());
145 }
146
148 return make_range(SPs.begin(), SPs.end());
149 }
150
152 return make_range(GVs.begin(), GVs.end());
153 }
154
156 return make_range(TYs.begin(), TYs.end());
157 }
158
160 return make_range(Scopes.begin(), Scopes.end());
161 }
162
163 unsigned compile_unit_count() const { return CUs.size(); }
164 unsigned global_variable_count() const { return GVs.size(); }
165 unsigned subprogram_count() const { return SPs.size(); }
166 unsigned type_count() const { return TYs.size(); }
167 unsigned scope_count() const { return Scopes.size(); }
168
169private:
176};
177
178/// Assignment Tracking (at).
179namespace at {
180//
181// Utilities for enumerating storing instructions from an assignment ID.
182//
183/// A range of instructions.
186/// Return a range of instructions (typically just one) that have \p ID
187/// as an attachment.
188/// Iterators invalidated by adding or removing DIAssignID metadata to/from any
189/// instruction (including by deleting or cloning instructions).
191/// Return a range of instructions (typically just one) that perform the
192/// assignment that \p DAI encodes.
193/// Iterators invalidated by adding or removing DIAssignID metadata to/from any
194/// instruction (including by deleting or cloning instructions).
196 return getAssignmentInsts(DAI->getAssignID());
197}
198
200 assert(DVR->isDbgAssign() &&
201 "Can't get assignment instructions for non-assign DVR!");
202 return getAssignmentInsts(DVR->getAssignID());
203}
204
205//
206// Utilities for enumerating llvm.dbg.assign intrinsic from an assignment ID.
207//
208/// High level: this is an iterator for llvm.dbg.assign intrinsics.
209/// Implementation details: this is a wrapper around Value's User iterator that
210/// dereferences to a DbgAssignIntrinsic ptr rather than a User ptr.
212 : public iterator_adaptor_base<DbgAssignIt, Value::user_iterator,
213 typename std::iterator_traits<
214 Value::user_iterator>::iterator_category,
215 DbgAssignIntrinsic *, std::ptrdiff_t,
216 DbgAssignIntrinsic **,
217 DbgAssignIntrinsic *&> {
218public:
220 DbgAssignIntrinsic *operator*() const { return cast<DbgAssignIntrinsic>(*I); }
221};
222/// A range of llvm.dbg.assign intrinsics.
224/// Return a range of dbg.assign intrinsics which use \ID as an operand.
225/// Iterators invalidated by deleting an intrinsic contained in this range.
227/// Return a range of dbg.assign intrinsics for which \p Inst performs the
228/// assignment they encode.
229/// Iterators invalidated by deleting an intrinsic contained in this range.
231 if (auto *ID = Inst->getMetadata(LLVMContext::MD_DIAssignID))
232 return getAssignmentMarkers(cast<DIAssignID>(ID));
233 else
235}
236
239 if (auto *ID = Inst->getMetadata(LLVMContext::MD_DIAssignID))
240 return cast<DIAssignID>(ID)->getAllDbgVariableRecordUsers();
241 return {};
242}
243
244/// Delete the llvm.dbg.assign intrinsics linked to \p Inst.
245void deleteAssignmentMarkers(const Instruction *Inst);
246
247/// Replace all uses (and attachments) of \p Old with \p New.
248void RAUW(DIAssignID *Old, DIAssignID *New);
249
250/// Remove all Assignment Tracking related intrinsics and metadata from \p F.
251void deleteAll(Function *F);
252
253/// Calculate the fragment of the variable in \p DAI covered
254/// from (Dest + SliceOffsetInBits) to
255/// to (Dest + SliceOffsetInBits + SliceSizeInBits)
256///
257/// Return false if it can't be calculated for any reason.
258/// Result is set to nullopt if the intersect equals the variable fragment (or
259/// variable size) in DAI.
260///
261/// Result contains a zero-sized fragment if there's no intersect.
263 const DataLayout &DL, const Value *Dest, uint64_t SliceOffsetInBits,
264 uint64_t SliceSizeInBits, const DbgAssignIntrinsic *DbgAssign,
265 std::optional<DIExpression::FragmentInfo> &Result);
267 const DataLayout &DL, const Value *Dest, uint64_t SliceOffsetInBits,
268 uint64_t SliceSizeInBits, const DbgVariableRecord *DVRAssign,
269 std::optional<DIExpression::FragmentInfo> &Result);
270
271/// Helper struct for trackAssignments, below. We don't use the similar
272/// DebugVariable class because trackAssignments doesn't (yet?) understand
273/// partial variables (fragment info) as input and want to make that clear and
274/// explicit using types. In addition, eventually we will want to understand
275/// expressions that modify the base address too, which a DebugVariable doesn't
276/// capture.
277struct VarRecord {
280
282 : Var(DVI->getVariable()), DL(getDebugValueLoc(DVI)) {}
284 : Var(DVR->getVariable()), DL(getDebugValueLoc(DVR)) {}
286 friend bool operator<(const VarRecord &LHS, const VarRecord &RHS) {
287 return std::tie(LHS.Var, LHS.DL) < std::tie(RHS.Var, RHS.DL);
288 }
289 friend bool operator==(const VarRecord &LHS, const VarRecord &RHS) {
290 return std::tie(LHS.Var, LHS.DL) == std::tie(RHS.Var, RHS.DL);
291 }
292};
293
294} // namespace at
295
296template <> struct DenseMapInfo<at::VarRecord> {
297 static inline at::VarRecord getEmptyKey() {
300 }
301
305 }
306
307 static unsigned getHashValue(const at::VarRecord &Var) {
308 return hash_combine(Var.Var, Var.DL);
309 }
310
311 static bool isEqual(const at::VarRecord &A, const at::VarRecord &B) {
312 return A == B;
313 }
314};
315
316namespace at {
317/// Map of backing storage to a set of variables that are stored to it.
318/// TODO: Backing storage shouldn't be limited to allocas only. Some local
319/// variables have their storage allocated by the calling function (addresses
320/// passed in with sret & byval parameters).
323
324/// Track assignments to \p Vars between \p Start and \p End.
325
327 const StorageToVarsMap &Vars, const DataLayout &DL,
328 bool DebugPrints = false);
329
330/// Describes properties of a store that has a static size and offset into a
331/// some base storage. Used by the getAssignmentInfo functions.
333 AllocaInst const *Base; ///< Base storage.
334 uint64_t OffsetInBits; ///< Offset into Base.
335 uint64_t SizeInBits; ///< Number of bits stored.
336 bool StoreToWholeAlloca; ///< SizeInBits equals the size of the base storage.
337
342 OffsetInBits == 0 &&
343 SizeInBits == DL.getTypeSizeInBits(Base->getAllocatedType())) {}
344};
345
346std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL,
347 const MemIntrinsic *I);
348std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL,
349 const StoreInst *SI);
350std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL,
351 const AllocaInst *AI);
352
353} // end namespace at
354
355/// Convert @llvm.dbg.declare intrinsics into sets of @llvm.dbg.assign
356/// intrinsics by treating stores to the dbg.declare'd address as assignments
357/// to the variable. Not all kinds of variables are supported yet; those will
358/// be left with their dbg.declare intrinsics.
359/// The pass sets the debug-info-assignment-tracking module flag to true to
360/// indicate assignment tracking has been enabled.
361class AssignmentTrackingPass : public PassInfoMixin<AssignmentTrackingPass> {
362 /// Note: this method does not set the debug-info-assignment-tracking module
363 /// flag.
364 bool runOnFunction(Function &F);
365
366public:
369};
370
371/// Return true if assignment tracking is enabled for module \p M.
373
374} // end namespace llvm
375
376#endif // LLVM_IR_DEBUGINFO_H
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
This file defines DenseMapInfo traits for DenseMap.
bool End
Definition: ELF_riscv.cpp:480
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
Machine Check Debug Module
This header defines various interfaces for pass management in LLVM.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallPtrSet class.
This file defines the SmallSet class.
This file defines the SmallVector class.
Value * RHS
Value * LHS
an instruction to allocate memory on the stack
Definition: Instructions.h:59
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:321
Convert @llvm.dbg.declare intrinsics into sets of @llvm.dbg.assign intrinsics by treating stores to t...
Definition: DebugInfo.h:361
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: DebugInfo.cpp:2437
Assignment ID.
A pair of DIGlobalVariable and DIExpression.
Debug location.
Base class for scope-like contexts.
Subprogram description.
Base class for types.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
This represents the llvm.dbg.assign instruction.
DIAssignID * getAssignID() const
Base class for non-instruction debug metadata records that have positions within IR.
This is the common base class for debug info intrinsics for variables.
Record of a variable value-assignment, aka a non instruction representation of the dbg....
Utility to find all debug info in a module.
Definition: DebugInfo.h:103
void processInstruction(const Module &M, const Instruction &I)
Process a single instruction and collect debug info anchors.
Definition: DebugInfo.cpp:242
unsigned subprogram_count() const
Definition: DebugInfo.h:165
void processModule(const Module &M)
Process entire module and collect debug info anchors.
Definition: DebugInfo.cpp:198
SmallVectorImpl< DICompileUnit * >::const_iterator compile_unit_iterator
Definition: DebugInfo.h:136
void processVariable(const Module &M, const DILocalVariable *DVI)
Process a DILocalVariable.
Definition: DebugInfo.cpp:340
unsigned type_count() const
Definition: DebugInfo.h:166
void processSubprogram(DISubprogram *SP)
Process subprogram.
Definition: DebugInfo.cpp:317
void processLocation(const Module &M, const DILocation *Loc)
Process debug info location.
Definition: DebugInfo.cpp:254
SmallVectorImpl< DIGlobalVariableExpression * >::const_iterator global_variable_expression_iterator
Definition: DebugInfo.h:139
void reset()
Clear all lists.
Definition: DebugInfo.cpp:189
SmallVectorImpl< DIScope * >::const_iterator scope_iterator
Definition: DebugInfo.h:141
unsigned global_variable_count() const
Definition: DebugInfo.h:164
iterator_range< global_variable_expression_iterator > global_variables() const
Definition: DebugInfo.h:151
iterator_range< subprogram_iterator > subprograms() const
Definition: DebugInfo.h:147
SmallVectorImpl< DIType * >::const_iterator type_iterator
Definition: DebugInfo.h:140
SmallVectorImpl< DISubprogram * >::const_iterator subprogram_iterator
Definition: DebugInfo.h:137
iterator_range< type_iterator > types() const
Definition: DebugInfo.h:155
iterator_range< scope_iterator > scopes() const
Definition: DebugInfo.h:159
unsigned compile_unit_count() const
Definition: DebugInfo.h:163
iterator_range< compile_unit_iterator > compile_units() const
Definition: DebugInfo.h:143
void processDbgRecord(const Module &M, const DbgRecord &DR)
Process a DbgRecord (e.g, treat a DbgVariableRecord like a DbgVariableIntrinsic).
Definition: DebugInfo.cpp:261
unsigned scope_count() const
Definition: DebugInfo.h:167
BasicBlockListType::iterator iterator
Definition: Function.h:67
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:359
This is the common base class for memset/memcpy/memmove.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:109
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:427
typename SuperClass::const_iterator const_iterator
Definition: SmallVector.h:591
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
An instruction for storing to memory.
Definition: Instructions.h:317
LLVM Value Representation.
Definition: Value.h:74
user_iterator_impl< User > user_iterator
Definition: Value.h:390
High level: this is an iterator for llvm.dbg.assign intrinsics.
Definition: DebugInfo.h:217
DbgAssignIt(Value::user_iterator It)
Definition: DebugInfo.h:219
DbgAssignIntrinsic * operator*() const
Definition: DebugInfo.h:220
CRTP base class for adapting an iterator to a different type.
Definition: iterator.h:237
A range adaptor for a pair of iterators.
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
void deleteAll(Function *F)
Remove all Assignment Tracking related intrinsics and metadata from F.
Definition: DebugInfo.cpp:1937
AssignmentInstRange getAssignmentInsts(DIAssignID *ID)
Return a range of instructions (typically just one) that have ID as an attachment.
Definition: DebugInfo.cpp:1886
AssignmentMarkerRange getAssignmentMarkers(DIAssignID *ID)
Return a range of dbg.assign intrinsics which use \ID as an operand.
Definition: DebugInfo.cpp:1898
void trackAssignments(Function::iterator Start, Function::iterator End, const StorageToVarsMap &Vars, const DataLayout &DL, bool DebugPrints=false)
Track assignments to Vars between Start and End.
Definition: DebugInfo.cpp:2247
SmallVector< DbgVariableRecord * > getDVRAssignmentMarkers(const Instruction *Inst)
Definition: DebugInfo.h:238
void deleteAssignmentMarkers(const Instruction *Inst)
Delete the llvm.dbg.assign intrinsics linked to Inst.
Definition: DebugInfo.cpp:1912
std::optional< AssignmentInfo > getAssignmentInfo(const DataLayout &DL, const MemIntrinsic *I)
Definition: DebugInfo.cpp:2160
bool calculateFragmentIntersect(const DataLayout &DL, const Value *Dest, uint64_t SliceOffsetInBits, uint64_t SliceSizeInBits, const DbgAssignIntrinsic *DbgAssign, std::optional< DIExpression::FragmentInfo > &Result)
Calculate the fragment of the variable in DAI covered from (Dest + SliceOffsetInBits) to to (Dest + S...
Definition: DebugInfo.cpp:2121
void RAUW(DIAssignID *Old, DIAssignID *New)
Replace all uses (and attachments) of Old with New.
Definition: DebugInfo.cpp:1924
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
TinyPtrVector< DbgDeclareInst * > findDbgDeclares(Value *V)
Finds dbg.declare intrinsics declaring local variables as living in the memory that 'V' points to.
Definition: DebugInfo.cpp:47
bool stripDebugInfo(Function &F)
Definition: DebugInfo.cpp:555
void findDbgUsers(SmallVectorImpl< DbgVariableIntrinsic * > &DbgInsts, Value *V, SmallVectorImpl< DbgVariableRecord * > *DbgVariableRecords=nullptr)
Finds the debug info intrinsics describing a value.
Definition: DebugInfo.cpp:148
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void findDbgValues(SmallVectorImpl< DbgValueInst * > &DbgValues, Value *V, SmallVectorImpl< DbgVariableRecord * > *DbgVariableRecords=nullptr)
Finds the llvm.dbg.value intrinsics describing a value.
Definition: DebugInfo.cpp:141
bool stripNonLineTableDebugInfo(Module &M)
Downgrade the debug info in a module to contain only line table information.
Definition: DebugInfo.cpp:829
DebugLoc getDebugValueLoc(DbgVariableIntrinsic *DII)
Produce a DebugLoc to use for each dbg.declare that is promoted to a dbg.value.
Definition: DebugInfo.cpp:161
unsigned getDebugMetadataVersionFromModule(const Module &M)
Return Debug Info Metadata Version by checking module flags.
Definition: DebugInfo.cpp:928
bool StripDebugInfo(Module &M)
Strip debug info in the module if it exists.
Definition: DebugInfo.cpp:594
bool isAssignmentTrackingEnabled(const Module &M)
Return true if assignment tracking is enabled for module M.
Definition: DebugInfo.cpp:2433
TinyPtrVector< DbgVariableRecord * > findDVRDeclares(Value *V)
As above, for DVRDeclares.
Definition: DebugInfo.cpp:66
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition: Hashing.h:613
void updateLoopMetadataDebugLocations(Instruction &I, function_ref< Metadata *(Metadata *)> Updater)
Update the debug locations contained within the MD_loop metadata attached to the instruction I,...
Definition: DebugInfo.cpp:425
DISubprogram * getDISubprogram(const MDNode *Scope)
Find subprogram that is enclosing this scope.
Definition: DebugInfo.cpp:155
static bool isEqual(const at::VarRecord &A, const at::VarRecord &B)
Definition: DebugInfo.h:311
static at::VarRecord getEmptyKey()
Definition: DebugInfo.h:297
static unsigned getHashValue(const at::VarRecord &Var)
Definition: DebugInfo.h:307
static at::VarRecord getTombstoneKey()
Definition: DebugInfo.h:302
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition: DenseMapInfo.h:50
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:74
Describes properties of a store that has a static size and offset into a some base storage.
Definition: DebugInfo.h:332
AssignmentInfo(const DataLayout &DL, AllocaInst const *Base, uint64_t OffsetInBits, uint64_t SizeInBits)
Definition: DebugInfo.h:338
uint64_t OffsetInBits
Offset into Base.
Definition: DebugInfo.h:334
uint64_t SizeInBits
Number of bits stored.
Definition: DebugInfo.h:335
AllocaInst const * Base
Base storage.
Definition: DebugInfo.h:333
bool StoreToWholeAlloca
SizeInBits equals the size of the base storage.
Definition: DebugInfo.h:336
Helper struct for trackAssignments, below.
Definition: DebugInfo.h:277
VarRecord(DbgVariableRecord *DVR)
Definition: DebugInfo.h:283
VarRecord(DbgVariableIntrinsic *DVI)
Definition: DebugInfo.h:281
friend bool operator==(const VarRecord &LHS, const VarRecord &RHS)
Definition: DebugInfo.h:289
friend bool operator<(const VarRecord &LHS, const VarRecord &RHS)
Definition: DebugInfo.h:286
DILocation * DL
Definition: DebugInfo.h:279
VarRecord(DILocalVariable *Var, DILocation *DL)
Definition: DebugInfo.h:285
DILocalVariable * Var
Definition: DebugInfo.h:278