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;
37class DbgVariableIntrinsic;
38class DbgVariableRecord;
39class Instruction;
40class Module;
41
42/// Finds dbg.declare records declaring local variables as living in the
43/// memory that 'V' points to.
44LLVM_ABI TinyPtrVector<DbgVariableRecord *> findDVRDeclares(Value *V);
45/// As above, for DVRValues.
46LLVM_ABI TinyPtrVector<DbgVariableRecord *> findDVRValues(Value *V);
47
48/// Finds the debug info records describing a value.
49LLVM_ABI void
50findDbgUsers(Value *V,
51 SmallVectorImpl<DbgVariableRecord *> &DbgVariableRecords);
52/// Finds the dbg.values describing a value.
53LLVM_ABI void
54findDbgValues(Value *V,
55 SmallVectorImpl<DbgVariableRecord *> &DbgVariableRecords);
56
57/// Find subprogram that is enclosing this scope.
58LLVM_ABI DISubprogram *getDISubprogram(const MDNode *Scope);
59
60/// Produce a DebugLoc to use for each dbg.declare that is promoted to a
61/// dbg.value.
62LLVM_ABI DebugLoc getDebugValueLoc(DbgVariableRecord *DVR);
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.
70LLVM_ABI bool stripDebugInfo(Function &F);
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 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
193 assert(DVR->isDbgAssign() &&
194 "Can't get assignment instructions for non-assign DVR!");
195 return getAssignmentInsts(DVR->getAssignID());
196}
197
198/// Return a range of dbg_assign records for which \p Inst performs the
199/// assignment they encode.
202 if (auto *ID = Inst->getMetadata(LLVMContext::MD_DIAssignID))
203 return cast<DIAssignID>(ID)->getAllDbgVariableRecordUsers();
204 return {};
205}
206
207/// Delete the llvm.dbg.assign intrinsics linked to \p Inst.
209
210/// Replace all uses (and attachments) of \p Old with \p New.
211LLVM_ABI void RAUW(DIAssignID *Old, DIAssignID *New);
212
213/// Remove all Assignment Tracking related intrinsics and metadata from \p F.
215
216/// Calculate the fragment of the variable in \p DAI covered
217/// from (Dest + SliceOffsetInBits) to
218/// to (Dest + SliceOffsetInBits + SliceSizeInBits)
219///
220/// Return false if it can't be calculated for any reason.
221/// Result is set to nullopt if the intersect equals the variable fragment (or
222/// variable size) in DAI.
223///
224/// Result contains a zero-sized fragment if there's no intersect.
225LLVM_ABI bool
227 uint64_t SliceOffsetInBits, uint64_t SliceSizeInBits,
228 const DbgVariableRecord *DVRAssign,
229 std::optional<DIExpression::FragmentInfo> &Result);
230
231/// Replace DIAssignID uses and attachments with IDs from \p Map.
232/// If an ID is unmapped a new ID is generated and added to \p Map.
234 Instruction &I);
235
236/// Helper struct for trackAssignments, below. We don't use the similar
237/// DebugVariable class because trackAssignments doesn't (yet?) understand
238/// partial variables (fragment info) as input and want to make that clear and
239/// explicit using types. In addition, eventually we will want to understand
240/// expressions that modify the base address too, which a DebugVariable doesn't
241/// capture.
242struct VarRecord {
245
247 : Var(DVR->getVariable()), DL(getDebugValueLoc(DVR)) {}
249 friend bool operator<(const VarRecord &LHS, const VarRecord &RHS) {
250 return std::tie(LHS.Var, LHS.DL) < std::tie(RHS.Var, RHS.DL);
251 }
252 friend bool operator==(const VarRecord &LHS, const VarRecord &RHS) {
253 return std::tie(LHS.Var, LHS.DL) == std::tie(RHS.Var, RHS.DL);
254 }
255};
256
257} // namespace at
258
259template <> struct DenseMapInfo<at::VarRecord> {
260 static inline at::VarRecord getEmptyKey() {
263 }
264
268 }
269
270 static unsigned getHashValue(const at::VarRecord &Var) {
271 return hash_combine(Var.Var, Var.DL);
272 }
273
274 static bool isEqual(const at::VarRecord &A, const at::VarRecord &B) {
275 return A == B;
276 }
277};
278
279namespace at {
280/// Map of backing storage to a set of variables that are stored to it.
281/// TODO: Backing storage shouldn't be limited to allocas only. Some local
282/// variables have their storage allocated by the calling function (addresses
283/// passed in with sret & byval parameters).
286
287/// Track assignments to \p Vars between \p Start and \p End.
288
290 const StorageToVarsMap &Vars,
291 const DataLayout &DL, bool DebugPrints = false);
292
293/// Describes properties of a store that has a static size and offset into a
294/// some base storage. Used by the getAssignmentInfo functions.
296 AllocaInst const *Base; ///< Base storage.
297 uint64_t OffsetInBits; ///< Offset into Base.
298 uint64_t SizeInBits; ///< Number of bits stored.
299 bool StoreToWholeAlloca; ///< SizeInBits equals the size of the base storage.
300
305 OffsetInBits == 0 &&
306 SizeInBits == DL.getTypeSizeInBits(Base->getAllocatedType())) {}
307};
308
309LLVM_ABI std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL,
310 const MemIntrinsic *I);
311LLVM_ABI std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL,
312 const StoreInst *SI);
313LLVM_ABI std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL,
314 const AllocaInst *AI);
315
316} // end namespace at
317
318/// Convert @llvm.dbg.declare intrinsics into sets of @llvm.dbg.assign
319/// intrinsics by treating stores to the dbg.declare'd address as assignments
320/// to the variable. Not all kinds of variables are supported yet; those will
321/// be left with their dbg.declare intrinsics.
322/// The pass sets the debug-info-assignment-tracking module flag to true to
323/// indicate assignment tracking has been enabled.
324class AssignmentTrackingPass : public PassInfoMixin<AssignmentTrackingPass> {
325 /// Note: this method does not set the debug-info-assignment-tracking module
326 /// flag.
327 bool runOnFunction(Function &F);
328
329public:
332};
333
334/// Return true if assignment tracking is enabled for module \p M.
336
337} // end namespace llvm
338
339#endif // LLVM_IR_DEBUGINFO_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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")
#define LLVM_ABI
Definition: Compiler.h:213
dxil translate DXIL Translate Metadata
This file defines DenseMapInfo traits for DenseMap.
bool End
Definition: ELF_riscv.cpp:480
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
Machine Check Debug Module
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:64
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
Convert @llvm.dbg.declare intrinsics into sets of @llvm.dbg.assign intrinsics by treating stores to t...
Definition: DebugInfo.h:324
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: DebugInfo.cpp:2263
Assignment ID.
A pair of DIGlobalVariable and DIExpression.
An imported module (C++ using directive or similar).
Debug location.
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
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:103
LLVM_ABI void processInstruction(const Module &M, const Instruction &I)
Process a single instruction and collect debug info anchors.
Definition: DebugInfo.cpp:200
unsigned subprogram_count() const
Definition: DebugInfo.h:165
LLVM_ABI void processModule(const Module &M)
Process entire module and collect debug info anchors.
Definition: DebugInfo.cpp:165
SmallVectorImpl< DICompileUnit * >::const_iterator compile_unit_iterator
Definition: DebugInfo.h:136
unsigned type_count() const
Definition: DebugInfo.h:166
LLVM_ABI void processVariable(DILocalVariable *DVI)
Process a DILocalVariable.
Definition: DebugInfo.cpp:317
LLVM_ABI void processSubprogram(DISubprogram *SP)
Process subprogram.
Definition: DebugInfo.cpp:287
LLVM_ABI void processLocation(const Module &M, const DILocation *Loc)
Process debug info location.
Definition: DebugInfo.cpp:212
SmallVectorImpl< DIGlobalVariableExpression * >::const_iterator global_variable_expression_iterator
Definition: DebugInfo.h:139
LLVM_ABI void reset()
Clear all lists.
Definition: DebugInfo.cpp:156
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
LLVM_ABI void processDbgRecord(const Module &M, const DbgRecord &DR)
Process a DbgRecord.
Definition: DebugInfo.cpp:219
unsigned scope_count() const
Definition: DebugInfo.h:167
BasicBlockListType::iterator iterator
Definition: Function.h:69
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:428
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: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.
Definition: SmallPtrSet.h:541
typename SuperClass::const_iterator const_iterator
Definition: SmallVector.h:579
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
An instruction for storing to memory.
Definition: Instructions.h:296
LLVM Value Representation.
Definition: Value.h:75
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.
LLVM_ABI void deleteAll(Function *F)
Remove all Assignment Tracking related intrinsics and metadata from F.
Definition: DebugInfo.cpp:1917
LLVM_ABI AssignmentInstRange getAssignmentInsts(DIAssignID *ID)
Return a range of instructions (typically just one) that have ID as an attachment.
Definition: DebugInfo.cpp:1887
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.
Definition: DebugInfo.cpp:2080
LLVM_ABI void remapAssignID(DenseMap< DIAssignID *, DIAssignID * > &Map, Instruction &I)
Replace DIAssignID uses and attachments with IDs from Map.
Definition: DebugInfo.cpp:1964
SmallVector< DbgVariableRecord * > getDVRAssignmentMarkers(const Instruction *Inst)
Return a range of dbg_assign records for which Inst performs the assignment they encode.
Definition: DebugInfo.h:201
LLVM_ABI void deleteAssignmentMarkers(const Instruction *Inst)
Delete the llvm.dbg.assign intrinsics linked to Inst.
Definition: DebugInfo.cpp:1899
LLVM_ABI std::optional< AssignmentInfo > getAssignmentInfo(const DataLayout &DL, const MemIntrinsic *I)
Definition: DebugInfo.cpp:2007
LLVM_ABI void RAUW(DIAssignID *Old, DIAssignID *New)
Replace all uses (and attachments) of Old with New.
Definition: DebugInfo.cpp:1904
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...
Definition: DebugInfo.cpp:1930
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI void findDbgValues(Value *V, SmallVectorImpl< DbgVariableRecord * > &DbgVariableRecords)
Finds the dbg.values describing a value.
Definition: DebugInfo.cpp:124
LLVM_ABI bool stripDebugInfo(Function &F)
Definition: DebugInfo.cpp:531
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
@ Import
Import information from summary.
LLVM_ABI bool stripNonLineTableDebugInfo(Module &M)
Downgrade the debug info in a module to contain only line table information.
Definition: DebugInfo.cpp:804
LLVM_ABI TinyPtrVector< DbgVariableRecord * > findDVRValues(Value *V)
As above, for DVRValues.
Definition: DebugInfo.cpp:65
LLVM_ABI unsigned getDebugMetadataVersionFromModule(const Module &M)
Return Debug Info Metadata Version by checking module flags.
Definition: DebugInfo.cpp:890
LLVM_ABI bool StripDebugInfo(Module &M)
Strip debug info in the module if it exists.
Definition: DebugInfo.cpp:565
LLVM_ABI bool isAssignmentTrackingEnabled(const Module &M)
Return true if assignment tracking is enabled for module M.
Definition: DebugInfo.cpp:2259
LLVM_ABI DebugLoc getDebugValueLoc(DbgVariableRecord *DVR)
Produce a DebugLoc to use for each dbg.declare that is promoted to a dbg.value.
Definition: DebugInfo.cpp:140
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
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition: Hashing.h:595
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,...
Definition: DebugInfo.cpp:401
LLVM_ABI void findDbgUsers(Value *V, SmallVectorImpl< DbgVariableRecord * > &DbgVariableRecords)
Finds the debug info records describing a value.
Definition: DebugInfo.cpp:129
LLVM_ABI DISubprogram * getDISubprogram(const MDNode *Scope)
Find subprogram that is enclosing this scope.
Definition: DebugInfo.cpp:134
static bool isEqual(const at::VarRecord &A, const at::VarRecord &B)
Definition: DebugInfo.h:274
static at::VarRecord getEmptyKey()
Definition: DebugInfo.h:260
static unsigned getHashValue(const at::VarRecord &Var)
Definition: DebugInfo.h:270
static at::VarRecord getTombstoneKey()
Definition: DebugInfo.h:265
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition: DenseMapInfo.h:54
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:70
Describes properties of a store that has a static size and offset into a some base storage.
Definition: DebugInfo.h:295
AssignmentInfo(const DataLayout &DL, AllocaInst const *Base, uint64_t OffsetInBits, uint64_t SizeInBits)
Definition: DebugInfo.h:301
uint64_t OffsetInBits
Offset into Base.
Definition: DebugInfo.h:297
uint64_t SizeInBits
Number of bits stored.
Definition: DebugInfo.h:298
AllocaInst const * Base
Base storage.
Definition: DebugInfo.h:296
bool StoreToWholeAlloca
SizeInBits equals the size of the base storage.
Definition: DebugInfo.h:299
Helper struct for trackAssignments, below.
Definition: DebugInfo.h:242
VarRecord(DbgVariableRecord *DVR)
Definition: DebugInfo.h:246
friend bool operator==(const VarRecord &LHS, const VarRecord &RHS)
Definition: DebugInfo.h:252
friend bool operator<(const VarRecord &LHS, const VarRecord &RHS)
Definition: DebugInfo.h:249
DILocation * DL
Definition: DebugInfo.h:244
VarRecord(DILocalVariable *Var, DILocation *DL)
Definition: DebugInfo.h:248
DILocalVariable * Var
Definition: DebugInfo.h:243