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 DPValue;
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 DPVDeclares.
45TinyPtrVector<DPValue *> findDPVDeclares(Value *V);
46
47/// Finds the llvm.dbg.value intrinsics describing a value.
48void findDbgValues(SmallVectorImpl<DbgValueInst *> &DbgValues,
49 Value *V, SmallVectorImpl<DPValue *> *DPValues = nullptr);
50
51/// Finds the debug info intrinsics describing a value.
52void findDbgUsers(SmallVectorImpl<DbgVariableIntrinsic *> &DbgInsts,
53 Value *V, SmallVectorImpl<DPValue *> *DPValues = nullptr);
54
55/// Find subprogram that is enclosing this scope.
56DISubprogram *getDISubprogram(const MDNode *Scope);
57
58/// Produce a DebugLoc to use for each dbg.declare that is promoted to a
59/// dbg.value.
60DebugLoc getDebugValueLoc(DbgVariableIntrinsic *DII);
61DebugLoc getDebugValueLoc(DPValue *DPV);
62
63/// Strip debug info in the module if it exists.
64///
65/// To do this, we remove all calls to the debugger intrinsics and any named
66/// metadata for debugging. We also remove debug locations for instructions.
67/// Return true if module is modified.
68bool StripDebugInfo(Module &M);
69bool stripDebugInfo(Function &F);
70
71/// Downgrade the debug info in a module to contain only line table information.
72///
73/// In order to convert debug info to what -gline-tables-only would have
74/// created, this does the following:
75/// 1) Delete all debug intrinsics.
76/// 2) Delete all non-CU named metadata debug info nodes.
77/// 3) Create new DebugLocs for each instruction.
78/// 4) Create a new CU debug info, and similarly for every metadata node
79/// that's reachable from the CU debug info.
80/// All debug type metadata nodes are unreachable and garbage collected.
82
83/// Update the debug locations contained within the MD_loop metadata attached
84/// to the instruction \p I, if one exists. \p Updater is applied to Metadata
85/// operand in the MD_loop metadata: the returned value is included in the
86/// updated loop metadata node if it is non-null.
88 Instruction &I, function_ref<Metadata *(Metadata *)> Updater);
89
90/// Return Debug Info Metadata Version by checking module flags.
92
93/// Utility to find all debug info in a module.
94///
95/// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
96/// list debug info MDNodes used by an instruction, DebugInfoFinder uses
97/// processDeclare, processValue and processLocation to handle DbgDeclareInst,
98/// DbgValueInst and DbgLoc attached to instructions. processModule will go
99/// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
100/// used by the CUs.
102public:
103 /// Process entire module and collect debug info anchors.
104 void processModule(const Module &M);
105 /// Process a single instruction and collect debug info anchors.
106 void processInstruction(const Module &M, const Instruction &I);
107
108 /// Process a DILocalVariable.
109 void processVariable(const Module &M, const DILocalVariable *DVI);
110 /// Process debug info location.
111 void processLocation(const Module &M, const DILocation *Loc);
112 // Process a DPValue, much like a DbgVariableIntrinsic.
113 void processDPValue(const Module &M, const DPValue &DPV);
114 /// Dispatch to DbgRecord subclasses handlers.
115 void processDbgRecord(const Module &M, const DbgRecord &DPE);
116
117 /// Process subprogram.
119
120 /// Clear all lists.
121 void reset();
122
123private:
124 void processCompileUnit(DICompileUnit *CU);
125 void processScope(DIScope *Scope);
126 void processType(DIType *DT);
127 bool addCompileUnit(DICompileUnit *CU);
128 bool addGlobalVariable(DIGlobalVariableExpression *DIG);
129 bool addScope(DIScope *Scope);
130 bool addSubprogram(DISubprogram *SP);
131 bool addType(DIType *DT);
132
133public:
141
143 return make_range(CUs.begin(), CUs.end());
144 }
145
147 return make_range(SPs.begin(), SPs.end());
148 }
149
151 return make_range(GVs.begin(), GVs.end());
152 }
153
155 return make_range(TYs.begin(), TYs.end());
156 }
157
159 return make_range(Scopes.begin(), Scopes.end());
160 }
161
162 unsigned compile_unit_count() const { return CUs.size(); }
163 unsigned global_variable_count() const { return GVs.size(); }
164 unsigned subprogram_count() const { return SPs.size(); }
165 unsigned type_count() const { return TYs.size(); }
166 unsigned scope_count() const { return Scopes.size(); }
167
168private:
175};
176
177/// Assignment Tracking (at).
178namespace at {
179//
180// Utilities for enumerating storing instructions from an assignment ID.
181//
182/// A range of instructions.
185/// Return a range of instructions (typically just one) that have \p ID
186/// as an attachment.
187/// Iterators invalidated by adding or removing DIAssignID metadata to/from any
188/// instruction (including by deleting or cloning instructions).
190/// Return a range of instructions (typically just one) that perform the
191/// assignment that \p DAI encodes.
192/// Iterators invalidated by adding or removing DIAssignID metadata to/from any
193/// instruction (including by deleting or cloning instructions).
195 return getAssignmentInsts(DAI->getAssignID());
196}
197
199 assert(DPV->isDbgAssign() &&
200 "Can't get assignment instructions for non-assign DPV!");
201 return getAssignmentInsts(DPV->getAssignID());
202}
203
204//
205// Utilities for enumerating llvm.dbg.assign intrinsic from an assignment ID.
206//
207/// High level: this is an iterator for llvm.dbg.assign intrinsics.
208/// Implementation details: this is a wrapper around Value's User iterator that
209/// dereferences to a DbgAssignIntrinsic ptr rather than a User ptr.
211 : public iterator_adaptor_base<DbgAssignIt, Value::user_iterator,
212 typename std::iterator_traits<
213 Value::user_iterator>::iterator_category,
214 DbgAssignIntrinsic *, std::ptrdiff_t,
215 DbgAssignIntrinsic **,
216 DbgAssignIntrinsic *&> {
217public:
219 DbgAssignIntrinsic *operator*() const { return cast<DbgAssignIntrinsic>(*I); }
220};
221/// A range of llvm.dbg.assign intrinsics.
223/// Return a range of dbg.assign intrinsics which use \ID as an operand.
224/// Iterators invalidated by deleting an intrinsic contained in this range.
226/// Return a range of dbg.assign intrinsics for which \p Inst performs the
227/// assignment they encode.
228/// Iterators invalidated by deleting an intrinsic contained in this range.
230 if (auto *ID = Inst->getMetadata(LLVMContext::MD_DIAssignID))
231 return getAssignmentMarkers(cast<DIAssignID>(ID));
232 else
234}
235
237 if (auto *ID = Inst->getMetadata(LLVMContext::MD_DIAssignID))
238 return cast<DIAssignID>(ID)->getAllDPValueUsers();
239 return {};
240}
241
242/// Delete the llvm.dbg.assign intrinsics linked to \p Inst.
243void deleteAssignmentMarkers(const Instruction *Inst);
244
245/// Replace all uses (and attachments) of \p Old with \p New.
246void RAUW(DIAssignID *Old, DIAssignID *New);
247
248/// Remove all Assignment Tracking related intrinsics and metadata from \p F.
249void deleteAll(Function *F);
250
251/// Calculate the fragment of the variable in \p DAI covered
252/// from (Dest + SliceOffsetInBits) to
253/// to (Dest + SliceOffsetInBits + SliceSizeInBits)
254///
255/// Return false if it can't be calculated for any reason.
256/// Result is set to nullopt if the intersect equals the variable fragment (or
257/// variable size) in DAI.
258///
259/// Result contains a zero-sized fragment if there's no intersect.
261 const DataLayout &DL, const Value *Dest, uint64_t SliceOffsetInBits,
262 uint64_t SliceSizeInBits, const DbgAssignIntrinsic *DbgAssign,
263 std::optional<DIExpression::FragmentInfo> &Result);
265 const DataLayout &DL, const Value *Dest, uint64_t SliceOffsetInBits,
266 uint64_t SliceSizeInBits, const DPValue *DPVAssign,
267 std::optional<DIExpression::FragmentInfo> &Result);
268
269/// Helper struct for trackAssignments, below. We don't use the similar
270/// DebugVariable class because trackAssignments doesn't (yet?) understand
271/// partial variables (fragment info) as input and want to make that clear and
272/// explicit using types. In addition, eventually we will want to understand
273/// expressions that modify the base address too, which a DebugVariable doesn't
274/// capture.
275struct VarRecord {
278
280 : Var(DVI->getVariable()), DL(getDebugValueLoc(DVI)) {}
282 : Var(DPV->getVariable()), DL(getDebugValueLoc(DPV)) {}
284 friend bool operator<(const VarRecord &LHS, const VarRecord &RHS) {
285 return std::tie(LHS.Var, LHS.DL) < std::tie(RHS.Var, RHS.DL);
286 }
287 friend bool operator==(const VarRecord &LHS, const VarRecord &RHS) {
288 return std::tie(LHS.Var, LHS.DL) == std::tie(RHS.Var, RHS.DL);
289 }
290};
291
292} // namespace at
293
294template <> struct DenseMapInfo<at::VarRecord> {
295 static inline at::VarRecord getEmptyKey() {
298 }
299
303 }
304
305 static unsigned getHashValue(const at::VarRecord &Var) {
306 return hash_combine(Var.Var, Var.DL);
307 }
308
309 static bool isEqual(const at::VarRecord &A, const at::VarRecord &B) {
310 return A == B;
311 }
312};
313
314namespace at {
315/// Map of backing storage to a set of variables that are stored to it.
316/// TODO: Backing storage shouldn't be limited to allocas only. Some local
317/// variables have their storage allocated by the calling function (addresses
318/// passed in with sret & byval parameters).
321
322/// Track assignments to \p Vars between \p Start and \p End.
323
325 const StorageToVarsMap &Vars, const DataLayout &DL,
326 bool DebugPrints = false);
327
328/// Describes properties of a store that has a static size and offset into a
329/// some base storage. Used by the getAssignmentInfo functions.
331 AllocaInst const *Base; ///< Base storage.
332 uint64_t OffsetInBits; ///< Offset into Base.
333 uint64_t SizeInBits; ///< Number of bits stored.
334 bool StoreToWholeAlloca; ///< SizeInBits equals the size of the base storage.
335
340 OffsetInBits == 0 &&
341 SizeInBits == DL.getTypeSizeInBits(Base->getAllocatedType())) {}
342};
343
344std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL,
345 const MemIntrinsic *I);
346std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL,
347 const StoreInst *SI);
348std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL,
349 const AllocaInst *AI);
350
351} // end namespace at
352
353/// Convert @llvm.dbg.declare intrinsics into sets of @llvm.dbg.assign
354/// intrinsics by treating stores to the dbg.declare'd address as assignments
355/// to the variable. Not all kinds of variables are supported yet; those will
356/// be left with their dbg.declare intrinsics.
357/// The pass sets the debug-info-assignment-tracking module flag to true to
358/// indicate assignment tracking has been enabled.
359class AssignmentTrackingPass : public PassInfoMixin<AssignmentTrackingPass> {
360 /// Note: this method does not set the debug-info-assignment-tracking module
361 /// flag.
362 bool runOnFunction(Function &F);
363
364public:
367};
368
369/// Return true if assignment tracking is enabled for module \p M.
371
372} // end namespace llvm
373
374#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:348
Convert @llvm.dbg.declare intrinsics into sets of @llvm.dbg.assign intrinsics by treating stores to t...
Definition: DebugInfo.h:359
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: DebugInfo.cpp:2319
Assignment ID.
A pair of DIGlobalVariable and DIExpression.
Debug location.
Base class for scope-like contexts.
Subprogram description.
Base class for types.
Record of a variable value-assignment, aka a non instruction representation of the dbg....
DIAssignID * getAssignID() const
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.
Utility to find all debug info in a module.
Definition: DebugInfo.h:101
void processInstruction(const Module &M, const Instruction &I)
Process a single instruction and collect debug info anchors.
Definition: DebugInfo.cpp:235
unsigned subprogram_count() const
Definition: DebugInfo.h:164
void processModule(const Module &M)
Process entire module and collect debug info anchors.
Definition: DebugInfo.cpp:191
SmallVectorImpl< DICompileUnit * >::const_iterator compile_unit_iterator
Definition: DebugInfo.h:135
void processVariable(const Module &M, const DILocalVariable *DVI)
Process a DILocalVariable.
Definition: DebugInfo.cpp:336
unsigned type_count() const
Definition: DebugInfo.h:165
void processSubprogram(DISubprogram *SP)
Process subprogram.
Definition: DebugInfo.cpp:313
void processLocation(const Module &M, const DILocation *Loc)
Process debug info location.
Definition: DebugInfo.cpp:247
SmallVectorImpl< DIGlobalVariableExpression * >::const_iterator global_variable_expression_iterator
Definition: DebugInfo.h:138
void reset()
Clear all lists.
Definition: DebugInfo.cpp:182
SmallVectorImpl< DIScope * >::const_iterator scope_iterator
Definition: DebugInfo.h:140
void processDbgRecord(const Module &M, const DbgRecord &DPE)
Dispatch to DbgRecord subclasses handlers.
Definition: DebugInfo.cpp:259
unsigned global_variable_count() const
Definition: DebugInfo.h:163
iterator_range< global_variable_expression_iterator > global_variables() const
Definition: DebugInfo.h:150
iterator_range< subprogram_iterator > subprograms() const
Definition: DebugInfo.h:146
SmallVectorImpl< DIType * >::const_iterator type_iterator
Definition: DebugInfo.h:139
SmallVectorImpl< DISubprogram * >::const_iterator subprogram_iterator
Definition: DebugInfo.h:136
iterator_range< type_iterator > types() const
Definition: DebugInfo.h:154
iterator_range< scope_iterator > scopes() const
Definition: DebugInfo.h:158
unsigned compile_unit_count() const
Definition: DebugInfo.h:162
iterator_range< compile_unit_iterator > compile_units() const
Definition: DebugInfo.h:142
void processDPValue(const Module &M, const DPValue &DPV)
Definition: DebugInfo.cpp:254
unsigned scope_count() const
Definition: DebugInfo.h:166
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:357
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:302
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:216
DbgAssignIt(Value::user_iterator It)
Definition: DebugInfo.h:218
DbgAssignIntrinsic * operator*() const
Definition: DebugInfo.h:219
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:1824
AssignmentInstRange getAssignmentInsts(DIAssignID *ID)
Return a range of instructions (typically just one) that have ID as an attachment.
Definition: DebugInfo.cpp:1773
AssignmentMarkerRange getAssignmentMarkers(DIAssignID *ID)
Return a range of dbg.assign intrinsics which use \ID as an operand.
Definition: DebugInfo.cpp:1785
SmallVector< DPValue * > getDPVAssignmentMarkers(const Instruction *Inst)
Definition: DebugInfo.h:236
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:2129
void deleteAssignmentMarkers(const Instruction *Inst)
Delete the llvm.dbg.assign intrinsics linked to Inst.
Definition: DebugInfo.cpp:1799
std::optional< AssignmentInfo > getAssignmentInfo(const DataLayout &DL, const MemIntrinsic *I)
Definition: DebugInfo.cpp:2047
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:2008
void RAUW(DIAssignID *Old, DIAssignID *New)
Replace all uses (and attachments) of Old with New.
Definition: DebugInfo.cpp:1811
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:551
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
TinyPtrVector< DPValue * > findDPVDeclares(Value *V)
As above, for DPVDeclares.
Definition: DebugInfo.cpp:66
bool stripNonLineTableDebugInfo(Module &M)
Downgrade the debug info in a module to contain only line table information.
Definition: DebugInfo.cpp:825
DebugLoc getDebugValueLoc(DbgVariableIntrinsic *DII)
Produce a DebugLoc to use for each dbg.declare that is promoted to a dbg.value.
Definition: DebugInfo.cpp:154
unsigned getDebugMetadataVersionFromModule(const Module &M)
Return Debug Info Metadata Version by checking module flags.
Definition: DebugInfo.cpp:924
bool StripDebugInfo(Module &M)
Strip debug info in the module if it exists.
Definition: DebugInfo.cpp:590
void findDbgUsers(SmallVectorImpl< DbgVariableIntrinsic * > &DbgInsts, Value *V, SmallVectorImpl< DPValue * > *DPValues=nullptr)
Finds the debug info intrinsics describing a value.
Definition: DebugInfo.cpp:142
bool isAssignmentTrackingEnabled(const Module &M)
Return true if assignment tracking is enabled for module M.
Definition: DebugInfo.cpp:2315
void findDbgValues(SmallVectorImpl< DbgValueInst * > &DbgValues, Value *V, SmallVectorImpl< DPValue * > *DPValues=nullptr)
Finds the llvm.dbg.value intrinsics describing a value.
Definition: DebugInfo.cpp:136
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:421
DISubprogram * getDISubprogram(const MDNode *Scope)
Find subprogram that is enclosing this scope.
Definition: DebugInfo.cpp:148
static bool isEqual(const at::VarRecord &A, const at::VarRecord &B)
Definition: DebugInfo.h:309
static at::VarRecord getEmptyKey()
Definition: DebugInfo.h:295
static unsigned getHashValue(const at::VarRecord &Var)
Definition: DebugInfo.h:305
static at::VarRecord getTombstoneKey()
Definition: DebugInfo.h:300
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:91
Describes properties of a store that has a static size and offset into a some base storage.
Definition: DebugInfo.h:330
AssignmentInfo(const DataLayout &DL, AllocaInst const *Base, uint64_t OffsetInBits, uint64_t SizeInBits)
Definition: DebugInfo.h:336
uint64_t OffsetInBits
Offset into Base.
Definition: DebugInfo.h:332
uint64_t SizeInBits
Number of bits stored.
Definition: DebugInfo.h:333
AllocaInst const * Base
Base storage.
Definition: DebugInfo.h:331
bool StoreToWholeAlloca
SizeInBits equals the size of the base storage.
Definition: DebugInfo.h:334
Helper struct for trackAssignments, below.
Definition: DebugInfo.h:275
VarRecord(DbgVariableIntrinsic *DVI)
Definition: DebugInfo.h:279
VarRecord(DPValue *DPV)
Definition: DebugInfo.h:281
friend bool operator==(const VarRecord &LHS, const VarRecord &RHS)
Definition: DebugInfo.h:287
friend bool operator<(const VarRecord &LHS, const VarRecord &RHS)
Definition: DebugInfo.h:284
DILocation * DL
Definition: DebugInfo.h:277
VarRecord(DILocalVariable *Var, DILocation *DL)
Definition: DebugInfo.h:283
DILocalVariable * Var
Definition: DebugInfo.h:276