LLVM 18.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
19#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/SmallSet.h"
25#include "llvm/IR/DataLayout.h"
27#include "llvm/IR/PassManager.h"
28#include <optional>
29
30namespace llvm {
31
32class DbgDeclareInst;
33class DbgValueInst;
34class DbgVariableIntrinsic;
35class Instruction;
36class Module;
37
38/// Finds dbg.declare intrinsics declaring local variables as living in the
39/// memory that 'V' points to.
40TinyPtrVector<DbgDeclareInst *> FindDbgDeclareUses(Value *V);
41
42/// Finds the llvm.dbg.value intrinsics describing a value.
43void findDbgValues(SmallVectorImpl<DbgValueInst *> &DbgValues, Value *V);
44
45/// Finds the debug info intrinsics describing a value.
46void findDbgUsers(SmallVectorImpl<DbgVariableIntrinsic *> &DbgInsts, Value *V);
47
48/// Find subprogram that is enclosing this scope.
49DISubprogram *getDISubprogram(const MDNode *Scope);
50
51/// Produce a DebugLoc to use for each dbg.declare that is promoted to a
52/// dbg.value.
53DebugLoc getDebugValueLoc(DbgVariableIntrinsic *DII);
54
55/// Strip debug info in the module if it exists.
56///
57/// To do this, we remove all calls to the debugger intrinsics and any named
58/// metadata for debugging. We also remove debug locations for instructions.
59/// Return true if module is modified.
60bool StripDebugInfo(Module &M);
61bool stripDebugInfo(Function &F);
62
63/// Downgrade the debug info in a module to contain only line table information.
64///
65/// In order to convert debug info to what -gline-tables-only would have
66/// created, this does the following:
67/// 1) Delete all debug intrinsics.
68/// 2) Delete all non-CU named metadata debug info nodes.
69/// 3) Create new DebugLocs for each instruction.
70/// 4) Create a new CU debug info, and similarly for every metadata node
71/// that's reachable from the CU debug info.
72/// All debug type metadata nodes are unreachable and garbage collected.
74
75/// Update the debug locations contained within the MD_loop metadata attached
76/// to the instruction \p I, if one exists. \p Updater is applied to Metadata
77/// operand in the MD_loop metadata: the returned value is included in the
78/// updated loop metadata node if it is non-null.
80 Instruction &I, function_ref<Metadata *(Metadata *)> Updater);
81
82/// Return Debug Info Metadata Version by checking module flags.
84
85/// Utility to find all debug info in a module.
86///
87/// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
88/// list debug info MDNodes used by an instruction, DebugInfoFinder uses
89/// processDeclare, processValue and processLocation to handle DbgDeclareInst,
90/// DbgValueInst and DbgLoc attached to instructions. processModule will go
91/// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
92/// used by the CUs.
94public:
95 /// Process entire module and collect debug info anchors.
96 void processModule(const Module &M);
97 /// Process a single instruction and collect debug info anchors.
98 void processInstruction(const Module &M, const Instruction &I);
99
100 /// Process DbgVariableIntrinsic.
101 void processVariable(const Module &M, const DbgVariableIntrinsic &DVI);
102 /// Process debug info location.
103 void processLocation(const Module &M, const DILocation *Loc);
104
105 /// Process subprogram.
107
108 /// Clear all lists.
109 void reset();
110
111private:
112 void processCompileUnit(DICompileUnit *CU);
113 void processScope(DIScope *Scope);
114 void processType(DIType *DT);
115 bool addCompileUnit(DICompileUnit *CU);
116 bool addGlobalVariable(DIGlobalVariableExpression *DIG);
117 bool addScope(DIScope *Scope);
118 bool addSubprogram(DISubprogram *SP);
119 bool addType(DIType *DT);
120
121public:
129
131 return make_range(CUs.begin(), CUs.end());
132 }
133
135 return make_range(SPs.begin(), SPs.end());
136 }
137
139 return make_range(GVs.begin(), GVs.end());
140 }
141
143 return make_range(TYs.begin(), TYs.end());
144 }
145
147 return make_range(Scopes.begin(), Scopes.end());
148 }
149
150 unsigned compile_unit_count() const { return CUs.size(); }
151 unsigned global_variable_count() const { return GVs.size(); }
152 unsigned subprogram_count() const { return SPs.size(); }
153 unsigned type_count() const { return TYs.size(); }
154 unsigned scope_count() const { return Scopes.size(); }
155
156private:
163};
164
165/// Assignment Tracking (at).
166namespace at {
167//
168// Utilities for enumerating storing instructions from an assignment ID.
169//
170/// A range of instructions.
173/// Return a range of instructions (typically just one) that have \p ID
174/// as an attachment.
175/// Iterators invalidated by adding or removing DIAssignID metadata to/from any
176/// instruction (including by deleting or cloning instructions).
178/// Return a range of instructions (typically just one) that perform the
179/// assignment that \p DAI encodes.
180/// Iterators invalidated by adding or removing DIAssignID metadata to/from any
181/// instruction (including by deleting or cloning instructions).
183 return getAssignmentInsts(DAI->getAssignID());
184}
185
186//
187// Utilities for enumerating llvm.dbg.assign intrinsic from an assignment ID.
188//
189/// High level: this is an iterator for llvm.dbg.assign intrinsics.
190/// Implementation details: this is a wrapper around Value's User iterator that
191/// dereferences to a DbgAssignIntrinsic ptr rather than a User ptr.
193 : public iterator_adaptor_base<DbgAssignIt, Value::user_iterator,
194 typename std::iterator_traits<
195 Value::user_iterator>::iterator_category,
196 DbgAssignIntrinsic *, std::ptrdiff_t,
197 DbgAssignIntrinsic **,
198 DbgAssignIntrinsic *&> {
199public:
201 DbgAssignIntrinsic *operator*() const { return cast<DbgAssignIntrinsic>(*I); }
202};
203/// A range of llvm.dbg.assign intrinsics.
205/// Return a range of dbg.assign intrinsics which use \ID as an operand.
206/// Iterators invalidated by deleting an intrinsic contained in this range.
208/// Return a range of dbg.assign intrinsics for which \p Inst performs the
209/// assignment they encode.
210/// Iterators invalidated by deleting an intrinsic contained in this range.
212 if (auto *ID = Inst->getMetadata(LLVMContext::MD_DIAssignID))
213 return getAssignmentMarkers(cast<DIAssignID>(ID));
214 else
216}
217
218/// Delete the llvm.dbg.assign intrinsics linked to \p Inst.
219void deleteAssignmentMarkers(const Instruction *Inst);
220
221/// Replace all uses (and attachments) of \p Old with \p New.
222void RAUW(DIAssignID *Old, DIAssignID *New);
223
224/// Remove all Assignment Tracking related intrinsics and metadata from \p F.
225void deleteAll(Function *F);
226
227/// Calculate the fragment of the variable in \p DAI covered
228/// from (Dest + SliceOffsetInBits) to
229/// to (Dest + SliceOffsetInBits + SliceSizeInBits)
230///
231/// Return false if it can't be calculated for any reason.
232/// Result is set to nullopt if the intersect equals the variable fragment (or
233/// variable size) in DAI.
234///
235/// Result contains a zero-sized fragment if there's no intersect.
237 const DataLayout &DL, const Value *Dest, uint64_t SliceOffsetInBits,
238 uint64_t SliceSizeInBits, const DbgAssignIntrinsic *DAI,
239 std::optional<DIExpression::FragmentInfo> &Result);
240
241/// Helper struct for trackAssignments, below. We don't use the similar
242/// DebugVariable class because trackAssignments doesn't (yet?) understand
243/// partial variables (fragment info) as input and want to make that clear and
244/// explicit using types. In addition, eventually we will want to understand
245/// expressions that modify the base address too, which a DebugVariable doesn't
246/// capture.
247struct VarRecord {
250
252 : Var(DVI->getVariable()), DL(getDebugValueLoc(DVI)) {}
254 friend bool operator<(const VarRecord &LHS, const VarRecord &RHS) {
255 return std::tie(LHS.Var, LHS.DL) < std::tie(RHS.Var, RHS.DL);
256 }
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};
261
262/// Map of backing storage to a set of variables that are stored to it.
263/// TODO: Backing storage shouldn't be limited to allocas only. Some local
264/// variables have their storage allocated by the calling function (addresses
265/// passed in with sret & byval parameters).
267
268/// Track assignments to \p Vars between \p Start and \p End.
269
271 const StorageToVarsMap &Vars, const DataLayout &DL,
272 bool DebugPrints = false);
273
274/// Describes properties of a store that has a static size and offset into a
275/// some base storage. Used by the getAssignmentInfo functions.
277 AllocaInst const *Base; ///< Base storage.
278 uint64_t OffsetInBits; ///< Offset into Base.
279 uint64_t SizeInBits; ///< Number of bits stored.
280 bool StoreToWholeAlloca; ///< SizeInBits equals the size of the base storage.
281
286 OffsetInBits == 0 &&
287 SizeInBits == DL.getTypeSizeInBits(Base->getAllocatedType())) {}
288};
289
290std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL,
291 const MemIntrinsic *I);
292std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL,
293 const StoreInst *SI);
294std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL,
295 const AllocaInst *AI);
296
297} // end namespace at
298
299/// Convert @llvm.dbg.declare intrinsics into sets of @llvm.dbg.assign
300/// intrinsics by treating stores to the dbg.declare'd address as assignments
301/// to the variable. Not all kinds of variables are supported yet; those will
302/// be left with their dbg.declare intrinsics.
303/// The pass sets the debug-info-assignment-tracking module flag to true to
304/// indicate assignment tracking has been enabled.
305class AssignmentTrackingPass : public PassInfoMixin<AssignmentTrackingPass> {
306 /// Note: this method does not set the debug-info-assignment-tracking module
307 /// flag.
308 bool runOnFunction(Function &F);
309
310public:
313};
314
315/// Return true if assignment tracking is enabled for module \p M.
317} // end namespace llvm
318
319#endif // LLVM_IR_DEBUGINFO_H
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
bool End
Definition: ELF_riscv.cpp:469
#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.
This file contains some templates that are useful if you are working with the STL at all.
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:58
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:620
Convert @llvm.dbg.declare intrinsics into sets of @llvm.dbg.assign intrinsics by treating stores to t...
Definition: DebugInfo.h:305
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: DebugInfo.cpp:2199
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
This is the common base class for debug info intrinsics for variables.
Utility to find all debug info in a module.
Definition: DebugInfo.h:93
void processVariable(const Module &M, const DbgVariableIntrinsic &DVI)
Process DbgVariableIntrinsic.
Definition: DebugInfo.cpp:272
void processInstruction(const Module &M, const Instruction &I)
Process a single instruction and collect debug info anchors.
Definition: DebugInfo.cpp:183
unsigned subprogram_count() const
Definition: DebugInfo.h:152
void processModule(const Module &M)
Process entire module and collect debug info anchors.
Definition: DebugInfo.cpp:139
SmallVectorImpl< DICompileUnit * >::const_iterator compile_unit_iterator
Definition: DebugInfo.h:123
unsigned type_count() const
Definition: DebugInfo.h:153
void processSubprogram(DISubprogram *SP)
Process subprogram.
Definition: DebugInfo.cpp:249
void processLocation(const Module &M, const DILocation *Loc)
Process debug info location.
Definition: DebugInfo.cpp:192
SmallVectorImpl< DIGlobalVariableExpression * >::const_iterator global_variable_expression_iterator
Definition: DebugInfo.h:126
void reset()
Clear all lists.
Definition: DebugInfo.cpp:130
SmallVectorImpl< DIScope * >::const_iterator scope_iterator
Definition: DebugInfo.h:128
unsigned global_variable_count() const
Definition: DebugInfo.h:151
iterator_range< global_variable_expression_iterator > global_variables() const
Definition: DebugInfo.h:138
iterator_range< subprogram_iterator > subprograms() const
Definition: DebugInfo.h:134
SmallVectorImpl< DIType * >::const_iterator type_iterator
Definition: DebugInfo.h:127
SmallVectorImpl< DISubprogram * >::const_iterator subprogram_iterator
Definition: DebugInfo.h:124
iterator_range< type_iterator > types() const
Definition: DebugInfo.h:142
iterator_range< scope_iterator > scopes() const
Definition: DebugInfo.h:146
unsigned compile_unit_count() const
Definition: DebugInfo.h:150
iterator_range< compile_unit_iterator > compile_units() const
Definition: DebugInfo.h:130
unsigned scope_count() const
Definition: DebugInfo.h:154
BasicBlockListType::iterator iterator
Definition: Function.h:65
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:302
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: PassManager.h:152
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:451
typename SuperClass::const_iterator const_iterator
Definition: SmallVector.h:582
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
An instruction for storing to memory.
Definition: Instructions.h:301
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:198
DbgAssignIt(Value::user_iterator It)
Definition: DebugInfo.h:200
DbgAssignIntrinsic * operator*() const
Definition: DebugInfo.h:201
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:1769
AssignmentInstRange getAssignmentInsts(DIAssignID *ID)
Return a range of instructions (typically just one) that have ID as an attachment.
Definition: DebugInfo.cpp:1716
AssignmentMarkerRange getAssignmentMarkers(DIAssignID *ID)
Return a range of dbg.assign intrinsics which use \ID as an operand.
Definition: DebugInfo.cpp:1728
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:2014
void deleteAssignmentMarkers(const Instruction *Inst)
Delete the llvm.dbg.assign intrinsics linked to Inst.
Definition: DebugInfo.cpp:1742
std::optional< AssignmentInfo > getAssignmentInfo(const DataLayout &DL, const MemIntrinsic *I)
Definition: DebugInfo.cpp:1941
bool calculateFragmentIntersect(const DataLayout &DL, const Value *Dest, uint64_t SliceOffsetInBits, uint64_t SliceSizeInBits, const DbgAssignIntrinsic *DAI, std::optional< DIExpression::FragmentInfo > &Result)
Calculate the fragment of the variable in DAI covered from (Dest + SliceOffsetInBits) to to (Dest + S...
Definition: DebugInfo.cpp:1783
void RAUW(DIAssignID *Old, DIAssignID *New)
Replace all uses (and attachments) of Old with New.
Definition: DebugInfo.cpp:1751
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
TinyPtrVector< DbgDeclareInst * > FindDbgDeclareUses(Value *V)
Finds dbg.declare intrinsics declaring local variables as living in the memory that 'V' points to.
Definition: DebugInfo.cpp:46
bool stripDebugInfo(Function &F)
Definition: DebugInfo.cpp:495
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void findDbgUsers(SmallVectorImpl< DbgVariableIntrinsic * > &DbgInsts, Value *V)
Finds the debug info intrinsics describing a value.
Definition: DebugInfo.cpp:103
bool stripNonLineTableDebugInfo(Module &M)
Downgrade the debug info in a module to contain only line table information.
Definition: DebugInfo.cpp:768
DebugLoc getDebugValueLoc(DbgVariableIntrinsic *DII)
Produce a DebugLoc to use for each dbg.declare that is promoted to a dbg.value.
Definition: DebugInfo.cpp:114
void findDbgValues(SmallVectorImpl< DbgValueInst * > &DbgValues, Value *V)
Finds the llvm.dbg.value intrinsics describing a value.
Definition: DebugInfo.cpp:99
unsigned getDebugMetadataVersionFromModule(const Module &M)
Return Debug Info Metadata Version by checking module flags.
Definition: DebugInfo.cpp:864
bool StripDebugInfo(Module &M)
Strip debug info in the module if it exists.
Definition: DebugInfo.cpp:533
bool isAssignmentTrackingEnabled(const Module &M)
Return true if assignment tracking is enabled for module M.
Definition: DebugInfo.cpp:2195
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:365
DISubprogram * getDISubprogram(const MDNode *Scope)
Find subprogram that is enclosing this scope.
Definition: DebugInfo.cpp:108
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:371
Describes properties of a store that has a static size and offset into a some base storage.
Definition: DebugInfo.h:276
AssignmentInfo(const DataLayout &DL, AllocaInst const *Base, uint64_t OffsetInBits, uint64_t SizeInBits)
Definition: DebugInfo.h:282
uint64_t OffsetInBits
Offset into Base.
Definition: DebugInfo.h:278
uint64_t SizeInBits
Number of bits stored.
Definition: DebugInfo.h:279
AllocaInst const * Base
Base storage.
Definition: DebugInfo.h:277
bool StoreToWholeAlloca
SizeInBits equals the size of the base storage.
Definition: DebugInfo.h:280
Helper struct for trackAssignments, below.
Definition: DebugInfo.h:247
VarRecord(DbgVariableIntrinsic *DVI)
Definition: DebugInfo.h:251
friend bool operator==(const VarRecord &LHS, const VarRecord &RHS)
Definition: DebugInfo.h:257
friend bool operator<(const VarRecord &LHS, const VarRecord &RHS)
Definition: DebugInfo.h:254
DILocation * DL
Definition: DebugInfo.h:249
VarRecord(DILocalVariable *Var, DILocation *DL)
Definition: DebugInfo.h:253
DILocalVariable * Var
Definition: DebugInfo.h:248