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/DenseMapInfo.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/SetVector.h"
22#include "llvm/ADT/SmallPtrSet.h"
23#include "llvm/ADT/SmallSet.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/ADT/TinyPtrVector.h"
26#include "llvm/ADT/iterator_range.h"
27#include "llvm/IR/DataLayout.h"
28#include "llvm/IR/IntrinsicInst.h"
29#include "llvm/IR/PassManager.h"
30#include <optional>
31
32namespace llvm {
33
34class DbgDeclareInst;
35class DbgValueInst;
36class DbgVariableIntrinsic;
37class DbgVariableRecord;
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 DVRDeclares.
45TinyPtrVector<DbgVariableRecord *> findDVRDeclares(Value *V);
46
47/// Finds the llvm.dbg.value intrinsics describing a value.
48void findDbgValues(
49 SmallVectorImpl<DbgValueInst *> &DbgValues, Value *V,
50 SmallVectorImpl<DbgVariableRecord *> *DbgVariableRecords = nullptr);
51
52/// Finds the debug info intrinsics describing a value.
53void findDbgUsers(
54 SmallVectorImpl<DbgVariableIntrinsic *> &DbgInsts, Value *V,
55 SmallVectorImpl<DbgVariableRecord *> *DbgVariableRecords = nullptr);
56
57/// Find subprogram that is enclosing this scope.
58DISubprogram *getDISubprogram(const MDNode *Scope);
59
60/// Produce a DebugLoc to use for each dbg.declare that is promoted to a
61/// dbg.value.
62DebugLoc getDebugValueLoc(DbgVariableIntrinsic *DII);
63DebugLoc getDebugValueLoc(DbgVariableRecord *DVR);
64
65/// Strip debug info in the module if it exists.
66///
67/// To do this, we remove all calls to the debugger intrinsics and any named
68/// metadata for debugging. We also remove debug locations for instructions.
69/// Return true if module is modified.
70bool StripDebugInfo(Module &M);
71bool stripDebugInfo(Function &F);
72
73/// Downgrade the debug info in a module to contain only line table information.
74///
75/// In order to convert debug info to what -gline-tables-only would have
76/// created, this does the following:
77/// 1) Delete all debug intrinsics.
78/// 2) Delete all non-CU named metadata debug info nodes.
79/// 3) Create new DebugLocs for each instruction.
80/// 4) Create a new CU debug info, and similarly for every metadata node
81/// that's reachable from the CU debug info.
82/// All debug type metadata nodes are unreachable and garbage collected.
83bool stripNonLineTableDebugInfo(Module &M);
84
85/// Update the debug locations contained within the MD_loop metadata attached
86/// to the instruction \p I, if one exists. \p Updater is applied to Metadata
87/// operand in the MD_loop metadata: the returned value is included in the
88/// updated loop metadata node if it is non-null.
89void updateLoopMetadataDebugLocations(
90 Instruction &I, function_ref<Metadata *(Metadata *)> Updater);
91
92/// Return Debug Info Metadata Version by checking module flags.
93unsigned getDebugMetadataVersionFromModule(const Module &M);
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.
103class DebugInfoFinder {
104public:
105 /// Process entire module and collect debug info anchors.
106 void processModule(const Module &M);
107 /// Process a single instruction and collect debug info anchors.
108 void processInstruction(const Module &M, const Instruction &I);
109
110 /// Process a DILocalVariable.
111 void processVariable(const Module &M, const DILocalVariable *DVI);
112 /// Process debug info location.
113 void processLocation(const Module &M, const DILocation *Loc);
114 /// Process a DbgRecord (e.g, treat a DbgVariableRecord like a
115 /// DbgVariableIntrinsic).
116 void processDbgRecord(const Module &M, const DbgRecord &DR);
117
118 /// Process subprogram.
119 void processSubprogram(DISubprogram *SP);
120
121 /// Clear all lists.
122 void reset();
123
124private:
125 void processCompileUnit(DICompileUnit *CU);
126 void processScope(DIScope *Scope);
127 void processType(DIType *DT);
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:
135 using compile_unit_iterator =
136 SmallVectorImpl<DICompileUnit *>::const_iterator;
137 using subprogram_iterator = SmallVectorImpl<DISubprogram *>::const_iterator;
138 using global_variable_expression_iterator =
139 SmallVectorImpl<DIGlobalVariableExpression *>::const_iterator;
140 using type_iterator = SmallVectorImpl<DIType *>::const_iterator;
141 using scope_iterator = SmallVectorImpl<DIScope *>::const_iterator;
142
143 iterator_range<compile_unit_iterator> compile_units() const {
144 return make_range(x: CUs.begin(), y: CUs.end());
145 }
146
147 iterator_range<subprogram_iterator> subprograms() const {
148 return make_range(x: SPs.begin(), y: SPs.end());
149 }
150
151 iterator_range<global_variable_expression_iterator> global_variables() const {
152 return make_range(x: GVs.begin(), y: GVs.end());
153 }
154
155 iterator_range<type_iterator> types() const {
156 return make_range(x: TYs.begin(), y: TYs.end());
157 }
158
159 iterator_range<scope_iterator> scopes() const {
160 return make_range(x: Scopes.begin(), y: 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:
170 SmallVector<DICompileUnit *, 8> CUs;
171 SmallVector<DISubprogram *, 8> SPs;
172 SmallVector<DIGlobalVariableExpression *, 8> GVs;
173 SmallVector<DIType *, 8> TYs;
174 SmallVector<DIScope *, 8> Scopes;
175 SmallPtrSet<const MDNode *, 32> NodesSeen;
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.
184using AssignmentInstRange =
185 iterator_range<SmallVectorImpl<Instruction *>::iterator>;
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).
190AssignmentInstRange getAssignmentInsts(DIAssignID *ID);
191/// Return a range of instructions (typically just one) that perform the
192/// assignment that \p DAI encodes.
193/// Iterators invalidated by adding or removing DIAssignID metadata to/from any
194/// instruction (including by deleting or cloning instructions).
195inline AssignmentInstRange getAssignmentInsts(const DbgAssignIntrinsic *DAI) {
196 return getAssignmentInsts(ID: DAI->getAssignID());
197}
198
199inline AssignmentInstRange getAssignmentInsts(const DbgVariableRecord *DVR) {
200 assert(DVR->isDbgAssign() &&
201 "Can't get assignment instructions for non-assign DVR!");
202 return getAssignmentInsts(ID: DVR->getAssignID());
203}
204
205//
206// Utilities for enumerating llvm.dbg.assign intrinsic from an assignment ID.
207//
208/// High level: this is an iterator for llvm.dbg.assign intrinsics.
209/// Implementation details: this is a wrapper around Value's User iterator that
210/// dereferences to a DbgAssignIntrinsic ptr rather than a User ptr.
211class DbgAssignIt
212 : public iterator_adaptor_base<DbgAssignIt, Value::user_iterator,
213 typename std::iterator_traits<
214 Value::user_iterator>::iterator_category,
215 DbgAssignIntrinsic *, std::ptrdiff_t,
216 DbgAssignIntrinsic **,
217 DbgAssignIntrinsic *&> {
218public:
219 DbgAssignIt(Value::user_iterator It) : iterator_adaptor_base(It) {}
220 DbgAssignIntrinsic *operator*() const { return cast<DbgAssignIntrinsic>(Val: *I); }
221};
222/// A range of llvm.dbg.assign intrinsics.
223using AssignmentMarkerRange = iterator_range<DbgAssignIt>;
224/// Return a range of dbg.assign intrinsics which use \ID as an operand.
225/// Iterators invalidated by deleting an intrinsic contained in this range.
226AssignmentMarkerRange getAssignmentMarkers(DIAssignID *ID);
227/// Return a range of dbg.assign intrinsics for which \p Inst performs the
228/// assignment they encode.
229/// Iterators invalidated by deleting an intrinsic contained in this range.
230inline AssignmentMarkerRange getAssignmentMarkers(const Instruction *Inst) {
231 if (auto *ID = Inst->getMetadata(KindID: LLVMContext::MD_DIAssignID))
232 return getAssignmentMarkers(ID: cast<DIAssignID>(Val: ID));
233 else
234 return make_range(x: Value::user_iterator(), y: Value::user_iterator());
235}
236
237inline SmallVector<DbgVariableRecord *>
238getDVRAssignmentMarkers(const Instruction *Inst) {
239 if (auto *ID = Inst->getMetadata(KindID: LLVMContext::MD_DIAssignID))
240 return cast<DIAssignID>(Val: ID)->getAllDbgVariableRecordUsers();
241 return {};
242}
243
244/// Delete the llvm.dbg.assign intrinsics linked to \p Inst.
245void deleteAssignmentMarkers(const Instruction *Inst);
246
247/// Replace all uses (and attachments) of \p Old with \p New.
248void RAUW(DIAssignID *Old, DIAssignID *New);
249
250/// Remove all Assignment Tracking related intrinsics and metadata from \p F.
251void deleteAll(Function *F);
252
253/// Calculate the fragment of the variable in \p DAI covered
254/// from (Dest + SliceOffsetInBits) to
255/// to (Dest + SliceOffsetInBits + SliceSizeInBits)
256///
257/// Return false if it can't be calculated for any reason.
258/// Result is set to nullopt if the intersect equals the variable fragment (or
259/// variable size) in DAI.
260///
261/// Result contains a zero-sized fragment if there's no intersect.
262bool calculateFragmentIntersect(
263 const DataLayout &DL, const Value *Dest, uint64_t SliceOffsetInBits,
264 uint64_t SliceSizeInBits, const DbgAssignIntrinsic *DbgAssign,
265 std::optional<DIExpression::FragmentInfo> &Result);
266bool calculateFragmentIntersect(
267 const DataLayout &DL, const Value *Dest, uint64_t SliceOffsetInBits,
268 uint64_t SliceSizeInBits, const DbgVariableRecord *DVRAssign,
269 std::optional<DIExpression::FragmentInfo> &Result);
270
271/// Helper struct for trackAssignments, below. We don't use the similar
272/// DebugVariable class because trackAssignments doesn't (yet?) understand
273/// partial variables (fragment info) as input and want to make that clear and
274/// explicit using types. In addition, eventually we will want to understand
275/// expressions that modify the base address too, which a DebugVariable doesn't
276/// capture.
277struct VarRecord {
278 DILocalVariable *Var;
279 DILocation *DL;
280
281 VarRecord(DbgVariableIntrinsic *DVI)
282 : Var(DVI->getVariable()), DL(getDebugValueLoc(DII: DVI)) {}
283 VarRecord(DbgVariableRecord *DVR)
284 : Var(DVR->getVariable()), DL(getDebugValueLoc(DVR)) {}
285 VarRecord(DILocalVariable *Var, DILocation *DL) : Var(Var), DL(DL) {}
286 friend bool operator<(const VarRecord &LHS, const VarRecord &RHS) {
287 return std::tie(args: LHS.Var, args: LHS.DL) < std::tie(args: RHS.Var, args: RHS.DL);
288 }
289 friend bool operator==(const VarRecord &LHS, const VarRecord &RHS) {
290 return std::tie(args: LHS.Var, args: LHS.DL) == std::tie(args: RHS.Var, args: RHS.DL);
291 }
292};
293
294} // namespace at
295
296template <> struct DenseMapInfo<at::VarRecord> {
297 static inline at::VarRecord getEmptyKey() {
298 return at::VarRecord(DenseMapInfo<DILocalVariable *>::getEmptyKey(),
299 DenseMapInfo<DILocation *>::getEmptyKey());
300 }
301
302 static inline at::VarRecord getTombstoneKey() {
303 return at::VarRecord(DenseMapInfo<DILocalVariable *>::getTombstoneKey(),
304 DenseMapInfo<DILocation *>::getTombstoneKey());
305 }
306
307 static unsigned getHashValue(const at::VarRecord &Var) {
308 return hash_combine(args: Var.Var, args: Var.DL);
309 }
310
311 static bool isEqual(const at::VarRecord &A, const at::VarRecord &B) {
312 return A == B;
313 }
314};
315
316namespace at {
317/// Map of backing storage to a set of variables that are stored to it.
318/// TODO: Backing storage shouldn't be limited to allocas only. Some local
319/// variables have their storage allocated by the calling function (addresses
320/// passed in with sret & byval parameters).
321using StorageToVarsMap =
322 DenseMap<const AllocaInst *, SmallSetVector<VarRecord, 2>>;
323
324/// Track assignments to \p Vars between \p Start and \p End.
325
326void trackAssignments(Function::iterator Start, Function::iterator End,
327 const StorageToVarsMap &Vars, const DataLayout &DL,
328 bool DebugPrints = false);
329
330/// Describes properties of a store that has a static size and offset into a
331/// some base storage. Used by the getAssignmentInfo functions.
332struct AssignmentInfo {
333 AllocaInst const *Base; ///< Base storage.
334 uint64_t OffsetInBits; ///< Offset into Base.
335 uint64_t SizeInBits; ///< Number of bits stored.
336 bool StoreToWholeAlloca; ///< SizeInBits equals the size of the base storage.
337
338 AssignmentInfo(const DataLayout &DL, AllocaInst const *Base,
339 uint64_t OffsetInBits, uint64_t SizeInBits)
340 : Base(Base), OffsetInBits(OffsetInBits), SizeInBits(SizeInBits),
341 StoreToWholeAlloca(
342 OffsetInBits == 0 &&
343 SizeInBits == DL.getTypeSizeInBits(Ty: Base->getAllocatedType())) {}
344};
345
346std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL,
347 const MemIntrinsic *I);
348std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL,
349 const StoreInst *SI);
350std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL,
351 const AllocaInst *AI);
352
353} // end namespace at
354
355/// Convert @llvm.dbg.declare intrinsics into sets of @llvm.dbg.assign
356/// intrinsics by treating stores to the dbg.declare'd address as assignments
357/// to the variable. Not all kinds of variables are supported yet; those will
358/// be left with their dbg.declare intrinsics.
359/// The pass sets the debug-info-assignment-tracking module flag to true to
360/// indicate assignment tracking has been enabled.
361class AssignmentTrackingPass : public PassInfoMixin<AssignmentTrackingPass> {
362 /// Note: this method does not set the debug-info-assignment-tracking module
363 /// flag.
364 bool runOnFunction(Function &F);
365
366public:
367 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
368 PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
369};
370
371/// Return true if assignment tracking is enabled for module \p M.
372bool isAssignmentTrackingEnabled(const Module &M);
373
374} // end namespace llvm
375
376#endif // LLVM_IR_DEBUGINFO_H
377

source code of llvm/include/llvm/IR/DebugInfo.h