LLVM 22.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
35class DbgDeclareInst;
36class DbgValueInst;
39class Instruction;
40class Module;
41
42/// Finds dbg.declare records declaring local variables as living in the
43/// memory that 'V' points to.
45/// As above, for DVRValues.
47
48/// Finds the debug info records describing a value.
49LLVM_ABI void
51 SmallVectorImpl<DbgVariableRecord *> &DbgVariableRecords);
52/// Finds the dbg.values describing a value.
53LLVM_ABI void
55 SmallVectorImpl<DbgVariableRecord *> &DbgVariableRecords);
56
57/// Find subprogram that is enclosing this scope.
59
60/// Produce a DebugLoc to use for each dbg.declare that is promoted to a
61/// dbg.value.
63
64/// Strip debug info in the module if it exists.
65///
66/// To do this, we remove all calls to the debugger intrinsics and any named
67/// metadata for debugging. We also remove debug locations for instructions.
68/// Return true if module is modified.
71
72/// Downgrade the debug info in a module to contain only line table information.
73///
74/// In order to convert debug info to what -gline-tables-only would have
75/// created, this does the following:
76/// 1) Delete all debug intrinsics.
77/// 2) Delete all non-CU named metadata debug info nodes.
78/// 3) Create new DebugLocs for each instruction.
79/// 4) Create a new CU debug info, and similarly for every metadata node
80/// that's reachable from the CU debug info.
81/// All debug type metadata nodes are unreachable and garbage collected.
83
84/// Update the debug locations contained within the MD_loop metadata attached
85/// to the instruction \p I, if one exists. \p Updater is applied to Metadata
86/// operand in the MD_loop metadata: the returned value is included in the
87/// updated loop metadata node if it is non-null.
88LLVM_ABI void
90 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 LLVM_ABI void processModule(const Module &M);
107 /// Process a single instruction and collect debug info anchors.
108 LLVM_ABI void processInstruction(const Module &M, const Instruction &I);
109
110 /// Process a DILocalVariable.
112 /// Process debug info location.
113 LLVM_ABI void processLocation(const Module &M, const DILocation *Loc);
114 /// Process a DbgRecord.
115 LLVM_ABI void processDbgRecord(const Module &M, const DbgRecord &DR);
116
117 /// Process subprogram.
119
120 /// Clear all lists.
121 LLVM_ABI void reset();
122
123private:
124 void processCompileUnit(DICompileUnit *CU);
125 void processScope(DIScope *Scope);
126 void processType(DIType *DT);
127 void processImportedEntity(DIImportedEntity *Import);
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
146
150
151 iterator_range<type_iterator> types() const { return TYs; }
152
153 iterator_range<scope_iterator> scopes() const { return Scopes; }
154
155 unsigned compile_unit_count() const { return CUs.size(); }
156 unsigned global_variable_count() const { return GVs.size(); }
157 unsigned subprogram_count() const { return SPs.size(); }
158 unsigned type_count() const { return TYs.size(); }
159 unsigned scope_count() const { return Scopes.size(); }
160
161private:
168};
169
170/// Assignment Tracking (at).
171namespace at {
172//
173// Utilities for enumerating storing instructions from an assignment ID.
174//
175/// A range of instructions.
178/// Return a range of instructions (typically just one) that have \p ID
179/// as an attachment.
180/// Iterators invalidated by adding or removing DIAssignID metadata to/from any
181/// instruction (including by deleting or cloning instructions).
183
185 assert(DVR->isDbgAssign() &&
186 "Can't get assignment instructions for non-assign DVR!");
187 return getAssignmentInsts(DVR->getAssignID());
188}
189
190/// Return a range of dbg_assign records for which \p Inst performs the
191/// assignment they encode.
194 if (auto *ID = Inst->getMetadata(LLVMContext::MD_DIAssignID))
195 return cast<DIAssignID>(ID)->getAllDbgVariableRecordUsers();
196 return {};
197}
198
199/// Delete the llvm.dbg.assign intrinsics linked to \p Inst.
201
202/// Replace all uses (and attachments) of \p Old with \p New.
203LLVM_ABI void RAUW(DIAssignID *Old, DIAssignID *New);
204
205/// Remove all Assignment Tracking related intrinsics and metadata from \p F.
207
208/// Calculate the fragment of the variable in \p DAI covered
209/// from (Dest + SliceOffsetInBits) to
210/// to (Dest + SliceOffsetInBits + SliceSizeInBits)
211///
212/// Return false if it can't be calculated for any reason.
213/// Result is set to nullopt if the intersect equals the variable fragment (or
214/// variable size) in DAI.
215///
216/// Result contains a zero-sized fragment if there's no intersect.
217LLVM_ABI bool
219 uint64_t SliceOffsetInBits, uint64_t SliceSizeInBits,
220 const DbgVariableRecord *DVRAssign,
221 std::optional<DIExpression::FragmentInfo> &Result);
222
223/// Replace DIAssignID uses and attachments with IDs from \p Map.
224/// If an ID is unmapped a new ID is generated and added to \p Map.
226 Instruction &I);
227
228/// Helper struct for trackAssignments, below. We don't use the similar
229/// DebugVariable class because trackAssignments doesn't (yet?) understand
230/// partial variables (fragment info) as input and want to make that clear and
231/// explicit using types. In addition, eventually we will want to understand
232/// expressions that modify the base address too, which a DebugVariable doesn't
233/// capture.
234struct VarRecord {
237
239 : Var(DVR->getVariable()), DL(getDebugValueLoc(DVR)) {}
241 friend bool operator<(const VarRecord &LHS, const VarRecord &RHS) {
242 return std::tie(LHS.Var, LHS.DL) < std::tie(RHS.Var, RHS.DL);
243 }
244 friend bool operator==(const VarRecord &LHS, const VarRecord &RHS) {
245 return std::tie(LHS.Var, LHS.DL) == std::tie(RHS.Var, RHS.DL);
246 }
247};
248
249} // namespace at
250
251template <> struct DenseMapInfo<at::VarRecord> {
256
261
262 static unsigned getHashValue(const at::VarRecord &Var) {
263 return hash_combine(Var.Var, Var.DL);
264 }
265
266 static bool isEqual(const at::VarRecord &A, const at::VarRecord &B) {
267 return A == B;
268 }
269};
270
271namespace at {
272/// Map of backing storage to a set of variables that are stored to it.
273/// TODO: Backing storage shouldn't be limited to allocas only. Some local
274/// variables have their storage allocated by the calling function (addresses
275/// passed in with sret & byval parameters).
278
279/// Track assignments to \p Vars between \p Start and \p End.
280
282 const StorageToVarsMap &Vars,
283 const DataLayout &DL, bool DebugPrints = false);
284
285/// Describes properties of a store that has a static size and offset into a
286/// some base storage. Used by the getAssignmentInfo functions.
288 AllocaInst const *Base; ///< Base storage.
289 uint64_t OffsetInBits; ///< Offset into Base.
290 uint64_t SizeInBits; ///< Number of bits stored.
291 bool StoreToWholeAlloca; ///< SizeInBits equals the size of the base storage.
292
297 OffsetInBits == 0 &&
298 SizeInBits == DL.getTypeSizeInBits(Base->getAllocatedType())) {}
299};
300
301LLVM_ABI std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL,
302 const MemIntrinsic *I);
303LLVM_ABI std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL,
304 const StoreInst *SI);
305LLVM_ABI std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL,
306 const AllocaInst *AI);
307
308} // end namespace at
309
310/// Convert @llvm.dbg.declare intrinsics into sets of @llvm.dbg.assign
311/// intrinsics by treating stores to the dbg.declare'd address as assignments
312/// to the variable. Not all kinds of variables are supported yet; those will
313/// be left with their dbg.declare intrinsics.
314/// The pass sets the debug-info-assignment-tracking module flag to true to
315/// indicate assignment tracking has been enabled.
316class AssignmentTrackingPass : public PassInfoMixin<AssignmentTrackingPass> {
317 /// Note: this method does not set the debug-info-assignment-tracking module
318 /// flag.
319 bool runOnFunction(Function &F);
320
321public:
324};
325
326/// Return true if assignment tracking is enabled for module \p M.
328
329} // end namespace llvm
330
331#endif // LLVM_IR_DEBUGINFO_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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:213
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:55
#define I(x, y, z)
Definition MD5.cpp:58
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
Convert @llvm.dbg.declare intrinsics into sets of @llvm.dbg.assign intrinsics by treating stores to t...
Definition DebugInfo.h:316
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
A pair of DIGlobalVariable and DIExpression.
An imported module (C++ using directive or similar).
Base class for scope-like contexts.
Subprogram description. Uses SubclassData1.
Base class for types.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
This represents the llvm.dbg.declare instruction.
Base class for non-instruction debug metadata records that have positions within IR.
This represents the llvm.dbg.value instruction.
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....
LLVM_ABI DIAssignID * getAssignID() const
Utility to find all debug info in a module.
Definition DebugInfo.h:103
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:157
LLVM_ABI void processModule(const Module &M)
Process entire module and collect debug info anchors.
unsigned type_count() const
Definition DebugInfo.h:158
LLVM_ABI void processVariable(DILocalVariable *DVI)
Process a DILocalVariable.
LLVM_ABI void processSubprogram(DISubprogram *SP)
Process subprogram.
SmallVectorImpl< DICompileUnit * >::const_iterator compile_unit_iterator
Definition DebugInfo.h:135
LLVM_ABI void processLocation(const Module &M, const DILocation *Loc)
Process debug info location.
LLVM_ABI void reset()
Clear all lists.
SmallVectorImpl< DIScope * >::const_iterator scope_iterator
Definition DebugInfo.h:141
unsigned global_variable_count() const
Definition DebugInfo.h:156
iterator_range< global_variable_expression_iterator > global_variables() const
Definition DebugInfo.h:147
iterator_range< subprogram_iterator > subprograms() const
Definition DebugInfo.h:145
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:151
iterator_range< scope_iterator > scopes() const
Definition DebugInfo.h:153
unsigned compile_unit_count() const
Definition DebugInfo.h:155
iterator_range< compile_unit_iterator > compile_units() const
Definition DebugInfo.h:143
LLVM_ABI void processDbgRecord(const Module &M, const DbgRecord &DR)
Process a DbgRecord.
unsigned scope_count() const
Definition DebugInfo.h:159
SmallVectorImpl< DIGlobalVariableExpression * >::const_iterator global_variable_expression_iterator
Definition DebugInfo.h:138
A debug info location.
Definition DebugLoc.h:124
BasicBlockListType::iterator iterator
Definition Function.h:69
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Metadata node.
Definition Metadata.h:1078
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:67
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.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
Assignment Tracking (at).
Definition DebugInfo.h:171
LLVM_ABI void deleteAll(Function *F)
Remove all Assignment Tracking related intrinsics and metadata from F.
LLVM_ABI AssignmentInstRange getAssignmentInsts(DIAssignID *ID)
Return a range of instructions (typically just one) that have ID as an attachment.
iterator_range< SmallVectorImpl< Instruction * >::iterator > AssignmentInstRange
A range of instructions.
Definition DebugInfo.h:176
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.
SmallVector< DbgVariableRecord * > getDVRAssignmentMarkers(const Instruction *Inst)
Return a range of dbg_assign records for which Inst performs the assignment they encode.
Definition DebugInfo.h:193
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:276
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:56
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:66
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.
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:49
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:592
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:266
static at::VarRecord getEmptyKey()
Definition DebugInfo.h:252
static unsigned getHashValue(const at::VarRecord &Var)
Definition DebugInfo.h:262
static at::VarRecord getTombstoneKey()
Definition DebugInfo.h:257
An information struct used to provide DenseMap with the various necessary components for a given valu...
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition PassManager.h:70
AssignmentInfo(const DataLayout &DL, AllocaInst const *Base, uint64_t OffsetInBits, uint64_t SizeInBits)
Definition DebugInfo.h:293
uint64_t OffsetInBits
Offset into Base.
Definition DebugInfo.h:289
uint64_t SizeInBits
Number of bits stored.
Definition DebugInfo.h:290
AllocaInst const * Base
Base storage.
Definition DebugInfo.h:288
bool StoreToWholeAlloca
SizeInBits equals the size of the base storage.
Definition DebugInfo.h:291
Helper struct for trackAssignments, below.
Definition DebugInfo.h:234
VarRecord(DbgVariableRecord *DVR)
Definition DebugInfo.h:238
friend bool operator==(const VarRecord &LHS, const VarRecord &RHS)
Definition DebugInfo.h:244
friend bool operator<(const VarRecord &LHS, const VarRecord &RHS)
Definition DebugInfo.h:241
DILocation * DL
Definition DebugInfo.h:236
VarRecord(DILocalVariable *Var, DILocation *DL)
Definition DebugInfo.h:240
DILocalVariable * Var
Definition DebugInfo.h:235