LLVM 24.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"
31#include <optional>
32
33namespace llvm {
34
36class Instruction;
37class Module;
38
39/// Finds dbg.declare records declaring local variables as living in the
40/// memory that 'V' points to.
42/// As above, for DVRValues.
44/// As above, for DVRDeclareValues.
46
47/// Finds the debug info records describing a value.
48LLVM_ABI void
50 SmallVectorImpl<DbgVariableRecord *> &DbgVariableRecords);
51/// Finds the dbg.values describing a value.
52LLVM_ABI void
54 SmallVectorImpl<DbgVariableRecord *> &DbgVariableRecords);
55
56/// Find subprogram that is enclosing this scope.
58
59/// Produce a DebugLoc to use for each dbg.declare that is promoted to a
60/// dbg.value.
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.
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.
87LLVM_ABI void
89 function_ref<Metadata *(Metadata *)> Updater);
90
91/// Return Debug Info Metadata Version by checking module flags.
93
94/// Utility to find all debug info in a module.
95///
96/// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
97/// list debug info MDNodes used by an instruction, DebugInfoFinder uses
98/// processDeclare, processValue and processLocation to handle DbgDeclareInst,
99/// DbgValueInst and DbgLoc attached to instructions. processModule will go
100/// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
101/// used by the CUs.
103public:
104 /// Process entire module and collect debug info anchors.
105 LLVM_ABI void processModule(const Module &M);
106 /// Process a single instruction and collect debug info anchors.
107 LLVM_ABI void processInstruction(const Module &M, const Instruction &I);
108
109 /// Process a DILocalVariable.
111 /// Process debug info location.
112 LLVM_ABI void processLocation(const Module &M, const DILocation *Loc);
113 /// Process a DbgRecord.
114 LLVM_ABI void processDbgRecord(const Module &M, const DbgRecord &DR);
115
116 /// Process subprogram.
118
119 /// Clear all lists.
120 LLVM_ABI void reset();
121
122private:
123 void processCompileUnit(DICompileUnit *CU);
124 void processGlobalVariableExpression(DIGlobalVariableExpression *GVE);
125 void processScope(DIScope *Scope);
126 void processType(DIType *DT);
127 void processVariable(DIVariable *DV);
128 void processImportedEntity(const DIImportedEntity *Import);
129 void processMacroNode(DIMacroNode *Macro, DIMacroFile *CurrentMacroFile);
130 bool addCompileUnit(DICompileUnit *CU);
131 bool addGlobalVariable(DIGlobalVariableExpression *DIG);
132 bool addScope(DIScope *Scope);
133 bool addSubprogram(DISubprogram *SP);
134 bool addType(DIType *DT);
135 bool addMacro(DIMacro *Macro, DIMacroFile *MacroFile);
136
137public:
138 using DIMacroEntry = std::pair<DIMacro *, DIMacroFile *>;
147
149
151
155
156 iterator_range<type_iterator> types() const { return TYs; }
157
158 iterator_range<scope_iterator> scopes() const { return Scopes; }
159
160 iterator_range<macro_iterator> macros() const { return Macros; }
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 unsigned macro_count() const { return Macros.size(); }
168
169private:
177};
178
179/// Assignment Tracking (at).
180namespace at {
181//
182// Utilities for enumerating storing instructions from an assignment ID.
183//
184/// 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 ID->getInstructions();
192}
193
195 assert(DVR->isDbgAssign() &&
196 "Can't get assignment instructions for non-assign DVR!");
197 return getAssignmentInsts(DVR->getAssignID());
198}
199
200// Return the dbg_assign records linked to ID, most recently linked first. A
201// copy, as callers unlink or relink records while iterating.
205
206/// Return a range of dbg_assign records for which \p Inst performs the
207/// assignment they encode.
210 if (auto *ID = Inst->getMetadata(LLVMContext::MD_DIAssignID))
212 return {};
213}
214
215/// Delete the llvm.dbg.assign intrinsics linked to \p Inst.
217
218/// Replace all uses (and attachments) of \p Old with \p New.
219LLVM_ABI void RAUW(DIAssignID *Old, DIAssignID *New);
220
221/// Remove all Assignment Tracking related intrinsics and metadata from \p F.
223
224/// Calculate the fragment of the variable in \p DAI covered
225/// from (Dest + SliceOffsetInBits) to
226/// to (Dest + SliceOffsetInBits + SliceSizeInBits)
227///
228/// Return false if it can't be calculated for any reason.
229/// Result is set to nullopt if the intersect equals the variable fragment (or
230/// variable size) in DAI.
231///
232/// Result contains a zero-sized fragment if there's no intersect.
233LLVM_ABI bool
235 uint64_t SliceOffsetInBits, uint64_t SliceSizeInBits,
236 const DbgVariableRecord *DVRAssign,
237 std::optional<DIExpression::FragmentInfo> &Result);
238
239/// Replace DIAssignID uses and attachments with IDs from \p Map.
240/// If an ID is unmapped a new ID is generated and added to \p Map.
242 Instruction &I);
243
244/// Helper struct for trackAssignments, below. We don't use the similar
245/// DebugVariable class because trackAssignments doesn't (yet?) understand
246/// partial variables (fragment info) as input and want to make that clear and
247/// explicit using types. In addition, eventually we will want to understand
248/// expressions that modify the base address too, which a DebugVariable doesn't
249/// capture.
250struct VarRecord {
253
255 : Var(DVR->getVariable()), DL(getDebugValueLoc(DVR)) {}
257 friend bool operator<(const VarRecord &LHS, const VarRecord &RHS) {
258 return std::tie(LHS.Var, LHS.DL) < std::tie(RHS.Var, RHS.DL);
259 }
260 friend bool operator==(const VarRecord &LHS, const VarRecord &RHS) {
261 return std::tie(LHS.Var, LHS.DL) == std::tie(RHS.Var, RHS.DL);
262 }
263};
264
265} // namespace at
266
267template <> struct DenseMapInfo<at::VarRecord> {
268 static unsigned getHashValue(const at::VarRecord &Var) {
269 return hash_combine(Var.Var, Var.DL);
270 }
271
272 static bool isEqual(const at::VarRecord &A, const at::VarRecord &B) {
273 return A == B;
274 }
275};
276
277namespace at {
278/// Map of backing storage to a set of variables that are stored to it.
279/// TODO: Backing storage shouldn't be limited to allocas only. Some local
280/// variables have their storage allocated by the calling function (addresses
281/// passed in with sret & byval parameters).
284
285/// Track assignments to \p Vars between \p Start and \p End.
286
288 const StorageToVarsMap &Vars,
289 const DataLayout &DL, bool DebugPrints = false);
290
291/// Describes properties of a store that has a static size and offset into a
292/// some base storage. Used by the getAssignmentInfo functions.
294 AllocaInst const *Base; ///< Base storage.
295 uint64_t OffsetInBits; ///< Offset into Base.
296 uint64_t SizeInBits; ///< Number of bits stored.
297 bool StoreToWholeAlloca; ///< SizeInBits equals the size of the base storage.
298
303 OffsetInBits == 0 &&
304 SizeInBits == DL.getTypeSizeInBits(Base->getAllocatedType())) {}
305};
306
307LLVM_ABI std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL,
308 const MemIntrinsic *I);
309LLVM_ABI std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL,
310 const StoreInst *SI);
311LLVM_ABI std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL,
312 const AllocaInst *AI);
313
314} // end namespace at
315
316/// Convert @llvm.dbg.declare intrinsics into sets of @llvm.dbg.assign
317/// intrinsics by treating stores to the dbg.declare'd address as assignments
318/// to the variable. Not all kinds of variables are supported yet; those will
319/// be left with their dbg.declare intrinsics.
320/// The pass sets the debug-info-assignment-tracking module flag to true to
321/// indicate assignment tracking has been enabled.
323 : public OptionalPassInfoMixin<AssignmentTrackingPass> {
324 /// Note: this method does not set the debug-info-assignment-tracking module
325 /// flag.
326 bool runOnFunction(Function &F);
327
328public:
331};
332
333/// Return true if assignment tracking is enabled for module \p M.
335
336} // end namespace llvm
337
338#endif // LLVM_IR_DEBUGINFO_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_ABI
Definition Compiler.h:215
This file defines DenseMapInfo traits for DenseMap.
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
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
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Convert @llvm.dbg.declare intrinsics into sets of @llvm.dbg.assign intrinsics by treating stores to t...
Definition DebugInfo.h:323
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
A pair of DIGlobalVariable and DIExpression.
An imported module (C++ using directive or similar).
Macro Info DWARF-like metadata node.
Base class for scope-like contexts.
Subprogram description. Uses SubclassData1.
Base class for types.
Base class for variables.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Base class for non-instruction debug metadata records that have positions within IR.
Record of a variable value-assignment, aka a non instruction representation of the dbg....
LLVM_ABI DIAssignID * getAssignID() const
Utility to find all debug info in a module.
Definition DebugInfo.h:102
std::pair< DIMacro *, DIMacroFile * > DIMacroEntry
Definition DebugInfo.h:138
LLVM_ABI void processInstruction(const Module &M, const Instruction &I)
Process a single instruction and collect debug info anchors.
unsigned subprogram_count() const
Definition DebugInfo.h:164
LLVM_ABI void processModule(const Module &M)
Process entire module and collect debug info anchors.
unsigned type_count() const
Definition DebugInfo.h:165
LLVM_ABI void processSubprogram(DISubprogram *SP)
Process subprogram.
SmallVectorImpl< DICompileUnit * >::const_iterator compile_unit_iterator
Definition DebugInfo.h:139
unsigned macro_count() const
Definition DebugInfo.h:167
LLVM_ABI void processLocation(const Module &M, const DILocation *Loc)
Process debug info location.
LLVM_ABI void reset()
Clear all lists.
SmallVectorImpl< DIMacroEntry >::const_iterator macro_iterator
Definition DebugInfo.h:146
SmallVectorImpl< DIScope * >::const_iterator scope_iterator
Definition DebugInfo.h:145
iterator_range< macro_iterator > macros() const
Definition DebugInfo.h:160
unsigned global_variable_count() const
Definition DebugInfo.h:163
iterator_range< global_variable_expression_iterator > global_variables() const
Definition DebugInfo.h:152
iterator_range< subprogram_iterator > subprograms() const
Definition DebugInfo.h:150
SmallVectorImpl< DIType * >::const_iterator type_iterator
Definition DebugInfo.h:144
SmallVectorImpl< DISubprogram * >::const_iterator subprogram_iterator
Definition DebugInfo.h:141
LLVM_ABI void processVariable(const DILocalVariable *DVI)
Process a DILocalVariable.
iterator_range< type_iterator > types() const
Definition DebugInfo.h:156
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:148
LLVM_ABI void processDbgRecord(const Module &M, const DbgRecord &DR)
Process a DbgRecord.
unsigned scope_count() const
Definition DebugInfo.h:166
SmallVectorImpl< DIGlobalVariableExpression * >::const_iterator global_variable_expression_iterator
Definition DebugInfo.h:142
A debug info location.
Definition DebugLoc.h:126
BasicBlockListType::iterator iterator
Definition Function.h:70
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Metadata node.
Definition Metadata.h:1081
This is the common base class for memset/memcpy/memmove.
Root of the metadata hierarchy.
Definition Metadata.h:64
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
typename SuperClass::const_iterator const_iterator
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
TinyPtrVector - This class is specialized for cases where there are normally 0 or 1 element in a vect...
LLVM Value Representation.
Definition Value.h:75
An efficient, type-erasing, non-owning reference to a callable.
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.
Assignment Tracking (at).
Definition DebugInfo.h:180
LLVM_ABI void deleteAll(Function *F)
Remove all Assignment Tracking related intrinsics and metadata from F.
AssignmentInstRange getAssignmentInsts(DIAssignID *ID)
Return a range of instructions (typically just one) that have ID as an attachment.
Definition DebugInfo.h:190
SmallVector< DbgVariableRecord * > getAssignmentMarkers(DIAssignID *ID)
Definition DebugInfo.h:202
LLVM_ABI 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.
LLVM_ABI void remapAssignID(DenseMap< DIAssignID *, DIAssignID * > &Map, Instruction &I)
Replace DIAssignID uses and attachments with IDs from Map.
ArrayRef< Instruction * > AssignmentInstRange
A range of instructions.
Definition DebugInfo.h:185
SmallVector< DbgVariableRecord * > getDVRAssignmentMarkers(const Instruction *Inst)
Return a range of dbg_assign records for which Inst performs the assignment they encode.
Definition DebugInfo.h:209
LLVM_ABI void deleteAssignmentMarkers(const Instruction *Inst)
Delete the llvm.dbg.assign intrinsics linked to Inst.
LLVM_ABI std::optional< AssignmentInfo > getAssignmentInfo(const DataLayout &DL, const MemIntrinsic *I)
DenseMap< const AllocaInst *, SmallSetVector< VarRecord, 2 > > StorageToVarsMap
Map of backing storage to a set of variables that are stored to it.
Definition DebugInfo.h:282
LLVM_ABI void RAUW(DIAssignID *Old, DIAssignID *New)
Replace all uses (and attachments) of Old with New.
LLVM_ABI bool calculateFragmentIntersect(const DataLayout &DL, const Value *Dest, uint64_t SliceOffsetInBits, uint64_t SliceSizeInBits, const DbgVariableRecord *DVRAssign, std::optional< DIExpression::FragmentInfo > &Result)
Calculate the fragment of the variable in DAI covered from (Dest + SliceOffsetInBits) to to (Dest + S...
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void findDbgValues(Value *V, SmallVectorImpl< DbgVariableRecord * > &DbgVariableRecords)
Finds the dbg.values describing a value.
LLVM_ABI bool stripDebugInfo(Function &F)
@ Import
Import information from summary.
Definition IPO.h:39
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
LLVM_ABI bool stripNonLineTableDebugInfo(Module &M)
Downgrade the debug info in a module to contain only line table information.
LLVM_ABI TinyPtrVector< DbgVariableRecord * > findDVRValues(Value *V)
As above, for DVRValues.
Definition DebugInfo.cpp:82
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
LLVM_ABI unsigned getDebugMetadataVersionFromModule(const Module &M)
Return Debug Info Metadata Version by checking module flags.
LLVM_ABI bool StripDebugInfo(Module &M)
Strip debug info in the module if it exists.
LLVM_ABI bool isAssignmentTrackingEnabled(const Module &M)
Return true if assignment tracking is enabled for module M.
LLVM_ABI DebugLoc getDebugValueLoc(DbgVariableRecord *DVR)
Produce a DebugLoc to use for each dbg.declare that is promoted to a dbg.value.
LLVM_ABI TinyPtrVector< DbgVariableRecord * > findDVRDeclareValues(Value *V)
As above, for DVRDeclareValues.
Definition DebugInfo.cpp:65
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI TinyPtrVector< DbgVariableRecord * > findDVRDeclares(Value *V)
Finds dbg.declare records declaring local variables as living in the memory that 'V' points to.
Definition DebugInfo.cpp:48
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition Hashing.h:307
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:267
LLVM_ABI void updateLoopMetadataDebugLocations(Instruction &I, function_ref< Metadata *(Metadata *)> Updater)
Update the debug locations contained within the MD_loop metadata attached to the instruction I,...
LLVM_ABI void findDbgUsers(Value *V, SmallVectorImpl< DbgVariableRecord * > &DbgVariableRecords)
Finds the debug info records describing a value.
LLVM_ABI DISubprogram * getDISubprogram(const MDNode *Scope)
Find subprogram that is enclosing this scope.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
static bool isEqual(const at::VarRecord &A, const at::VarRecord &B)
Definition DebugInfo.h:272
static unsigned getHashValue(const at::VarRecord &Var)
Definition DebugInfo.h:268
An information struct used to provide DenseMap with the various necessary components for a given valu...
A CRTP mix-in for passes that can be skipped.
AssignmentInfo(const DataLayout &DL, AllocaInst const *Base, uint64_t OffsetInBits, uint64_t SizeInBits)
Definition DebugInfo.h:299
uint64_t OffsetInBits
Offset into Base.
Definition DebugInfo.h:295
uint64_t SizeInBits
Number of bits stored.
Definition DebugInfo.h:296
AllocaInst const * Base
Base storage.
Definition DebugInfo.h:294
bool StoreToWholeAlloca
SizeInBits equals the size of the base storage.
Definition DebugInfo.h:297
Helper struct for trackAssignments, below.
Definition DebugInfo.h:250
VarRecord(DbgVariableRecord *DVR)
Definition DebugInfo.h:254
friend bool operator==(const VarRecord &LHS, const VarRecord &RHS)
Definition DebugInfo.h:260
friend bool operator<(const VarRecord &LHS, const VarRecord &RHS)
Definition DebugInfo.h:257
DILocation * DL
Definition DebugInfo.h:252
VarRecord(DILocalVariable *Var, DILocation *DL)
Definition DebugInfo.h:256
DILocalVariable * Var
Definition DebugInfo.h:251