Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
14 | pmbaty | 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" |
||
20 | #include "llvm/ADT/SmallPtrSet.h" |
||
21 | #include "llvm/ADT/SmallSet.h" |
||
22 | #include "llvm/ADT/SmallVector.h" |
||
23 | #include "llvm/ADT/TinyPtrVector.h" |
||
24 | #include "llvm/ADT/iterator_range.h" |
||
25 | #include "llvm/IR/DataLayout.h" |
||
26 | #include "llvm/IR/IntrinsicInst.h" |
||
27 | #include "llvm/IR/PassManager.h" |
||
28 | #include <optional> |
||
29 | |||
30 | namespace llvm { |
||
31 | |||
32 | class DbgDeclareInst; |
||
33 | class DbgValueInst; |
||
34 | class DbgVariableIntrinsic; |
||
35 | class Instruction; |
||
36 | class Module; |
||
37 | |||
38 | /// Finds all intrinsics declaring local variables as living in the memory that |
||
39 | /// 'V' points to. This may include a mix of dbg.declare and |
||
40 | /// dbg.addr intrinsics. |
||
41 | TinyPtrVector<DbgVariableIntrinsic *> FindDbgAddrUses(Value *V); |
||
42 | |||
43 | /// Like \c FindDbgAddrUses, but only returns dbg.declare intrinsics, not |
||
44 | /// dbg.addr. |
||
45 | TinyPtrVector<DbgDeclareInst *> FindDbgDeclareUses(Value *V); |
||
46 | |||
47 | /// Finds the llvm.dbg.value intrinsics describing a value. |
||
48 | void findDbgValues(SmallVectorImpl<DbgValueInst *> &DbgValues, Value *V); |
||
49 | |||
50 | /// Finds the debug info intrinsics describing a value. |
||
51 | void findDbgUsers(SmallVectorImpl<DbgVariableIntrinsic *> &DbgInsts, Value *V); |
||
52 | |||
53 | /// Find subprogram that is enclosing this scope. |
||
54 | DISubprogram *getDISubprogram(const MDNode *Scope); |
||
55 | |||
56 | /// Produce a DebugLoc to use for each dbg.declare that is promoted to a |
||
57 | /// dbg.value. |
||
58 | DebugLoc getDebugValueLoc(DbgVariableIntrinsic *DII); |
||
59 | |||
60 | /// Strip debug info in the module if it exists. |
||
61 | /// |
||
62 | /// To do this, we remove all calls to the debugger intrinsics and any named |
||
63 | /// metadata for debugging. We also remove debug locations for instructions. |
||
64 | /// Return true if module is modified. |
||
65 | bool StripDebugInfo(Module &M); |
||
66 | bool stripDebugInfo(Function &F); |
||
67 | |||
68 | /// Downgrade the debug info in a module to contain only line table information. |
||
69 | /// |
||
70 | /// In order to convert debug info to what -gline-tables-only would have |
||
71 | /// created, this does the following: |
||
72 | /// 1) Delete all debug intrinsics. |
||
73 | /// 2) Delete all non-CU named metadata debug info nodes. |
||
74 | /// 3) Create new DebugLocs for each instruction. |
||
75 | /// 4) Create a new CU debug info, and similarly for every metadata node |
||
76 | /// that's reachable from the CU debug info. |
||
77 | /// All debug type metadata nodes are unreachable and garbage collected. |
||
78 | bool stripNonLineTableDebugInfo(Module &M); |
||
79 | |||
80 | /// Update the debug locations contained within the MD_loop metadata attached |
||
81 | /// to the instruction \p I, if one exists. \p Updater is applied to Metadata |
||
82 | /// operand in the MD_loop metadata: the returned value is included in the |
||
83 | /// updated loop metadata node if it is non-null. |
||
84 | void updateLoopMetadataDebugLocations( |
||
85 | Instruction &I, function_ref<Metadata *(Metadata *)> Updater); |
||
86 | |||
87 | /// Return Debug Info Metadata Version by checking module flags. |
||
88 | unsigned getDebugMetadataVersionFromModule(const Module &M); |
||
89 | |||
90 | /// Utility to find all debug info in a module. |
||
91 | /// |
||
92 | /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To |
||
93 | /// list debug info MDNodes used by an instruction, DebugInfoFinder uses |
||
94 | /// processDeclare, processValue and processLocation to handle DbgDeclareInst, |
||
95 | /// DbgValueInst and DbgLoc attached to instructions. processModule will go |
||
96 | /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes |
||
97 | /// used by the CUs. |
||
98 | class DebugInfoFinder { |
||
99 | public: |
||
100 | /// Process entire module and collect debug info anchors. |
||
101 | void processModule(const Module &M); |
||
102 | /// Process a single instruction and collect debug info anchors. |
||
103 | void processInstruction(const Module &M, const Instruction &I); |
||
104 | |||
105 | /// Process DbgVariableIntrinsic. |
||
106 | void processVariable(const Module &M, const DbgVariableIntrinsic &DVI); |
||
107 | /// Process debug info location. |
||
108 | void processLocation(const Module &M, const DILocation *Loc); |
||
109 | |||
110 | /// Process subprogram. |
||
111 | void processSubprogram(DISubprogram *SP); |
||
112 | |||
113 | /// Clear all lists. |
||
114 | void reset(); |
||
115 | |||
116 | private: |
||
117 | void processCompileUnit(DICompileUnit *CU); |
||
118 | void processScope(DIScope *Scope); |
||
119 | void processType(DIType *DT); |
||
120 | bool addCompileUnit(DICompileUnit *CU); |
||
121 | bool addGlobalVariable(DIGlobalVariableExpression *DIG); |
||
122 | bool addScope(DIScope *Scope); |
||
123 | bool addSubprogram(DISubprogram *SP); |
||
124 | bool addType(DIType *DT); |
||
125 | |||
126 | public: |
||
127 | using compile_unit_iterator = |
||
128 | SmallVectorImpl<DICompileUnit *>::const_iterator; |
||
129 | using subprogram_iterator = SmallVectorImpl<DISubprogram *>::const_iterator; |
||
130 | using global_variable_expression_iterator = |
||
131 | SmallVectorImpl<DIGlobalVariableExpression *>::const_iterator; |
||
132 | using type_iterator = SmallVectorImpl<DIType *>::const_iterator; |
||
133 | using scope_iterator = SmallVectorImpl<DIScope *>::const_iterator; |
||
134 | |||
135 | iterator_range<compile_unit_iterator> compile_units() const { |
||
136 | return make_range(CUs.begin(), CUs.end()); |
||
137 | } |
||
138 | |||
139 | iterator_range<subprogram_iterator> subprograms() const { |
||
140 | return make_range(SPs.begin(), SPs.end()); |
||
141 | } |
||
142 | |||
143 | iterator_range<global_variable_expression_iterator> global_variables() const { |
||
144 | return make_range(GVs.begin(), GVs.end()); |
||
145 | } |
||
146 | |||
147 | iterator_range<type_iterator> types() const { |
||
148 | return make_range(TYs.begin(), TYs.end()); |
||
149 | } |
||
150 | |||
151 | iterator_range<scope_iterator> scopes() const { |
||
152 | return make_range(Scopes.begin(), Scopes.end()); |
||
153 | } |
||
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 | |||
161 | private: |
||
162 | SmallVector<DICompileUnit *, 8> CUs; |
||
163 | SmallVector<DISubprogram *, 8> SPs; |
||
164 | SmallVector<DIGlobalVariableExpression *, 8> GVs; |
||
165 | SmallVector<DIType *, 8> TYs; |
||
166 | SmallVector<DIScope *, 8> Scopes; |
||
167 | SmallPtrSet<const MDNode *, 32> NodesSeen; |
||
168 | }; |
||
169 | |||
170 | /// Assignment Tracking (at). |
||
171 | namespace at { |
||
172 | // |
||
173 | // Utilities for enumerating storing instructions from an assignment ID. |
||
174 | // |
||
175 | /// A range of instructions. |
||
176 | using AssignmentInstRange = |
||
177 | iterator_range<SmallVectorImpl<Instruction *>::iterator>; |
||
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). |
||
182 | AssignmentInstRange getAssignmentInsts(DIAssignID *ID); |
||
183 | /// Return a range of instructions (typically just one) that perform the |
||
184 | /// assignment that \p DAI encodes. |
||
185 | /// Iterators invalidated by adding or removing DIAssignID metadata to/from any |
||
186 | /// instruction (including by deleting or cloning instructions). |
||
187 | inline AssignmentInstRange getAssignmentInsts(const DbgAssignIntrinsic *DAI) { |
||
188 | return getAssignmentInsts(DAI->getAssignID()); |
||
189 | } |
||
190 | |||
191 | // |
||
192 | // Utilities for enumerating llvm.dbg.assign intrinsic from an assignment ID. |
||
193 | // |
||
194 | /// High level: this is an iterator for llvm.dbg.assign intrinsics. |
||
195 | /// Implementation details: this is a wrapper around Value's User iterator that |
||
196 | /// dereferences to a DbgAssignIntrinsic ptr rather than a User ptr. |
||
197 | class DbgAssignIt |
||
198 | : public iterator_adaptor_base<DbgAssignIt, Value::user_iterator, |
||
199 | typename std::iterator_traits< |
||
200 | Value::user_iterator>::iterator_category, |
||
201 | DbgAssignIntrinsic *, std::ptrdiff_t, |
||
202 | DbgAssignIntrinsic **, |
||
203 | DbgAssignIntrinsic *&> { |
||
204 | public: |
||
205 | DbgAssignIt(Value::user_iterator It) : iterator_adaptor_base(It) {} |
||
206 | DbgAssignIntrinsic *operator*() const { return cast<DbgAssignIntrinsic>(*I); } |
||
207 | }; |
||
208 | /// A range of llvm.dbg.assign intrinsics. |
||
209 | using AssignmentMarkerRange = iterator_range<DbgAssignIt>; |
||
210 | /// Return a range of dbg.assign intrinsics which use \ID as an operand. |
||
211 | /// Iterators invalidated by deleting an intrinsic contained in this range. |
||
212 | AssignmentMarkerRange getAssignmentMarkers(DIAssignID *ID); |
||
213 | /// Return a range of dbg.assign intrinsics for which \p Inst performs the |
||
214 | /// assignment they encode. |
||
215 | /// Iterators invalidated by deleting an intrinsic contained in this range. |
||
216 | inline AssignmentMarkerRange getAssignmentMarkers(const Instruction *Inst) { |
||
217 | if (auto *ID = Inst->getMetadata(LLVMContext::MD_DIAssignID)) |
||
218 | return getAssignmentMarkers(cast<DIAssignID>(ID)); |
||
219 | else |
||
220 | return make_range(Value::user_iterator(), Value::user_iterator()); |
||
221 | } |
||
222 | |||
223 | /// Delete the llvm.dbg.assign intrinsics linked to \p Inst. |
||
224 | void deleteAssignmentMarkers(const Instruction *Inst); |
||
225 | |||
226 | /// Replace all uses (and attachments) of \p Old with \p New. |
||
227 | void RAUW(DIAssignID *Old, DIAssignID *New); |
||
228 | |||
229 | /// Remove all Assignment Tracking related intrinsics and metadata from \p F. |
||
230 | void deleteAll(Function *F); |
||
231 | |||
232 | /// Helper struct for trackAssignments, below. We don't use the similar |
||
233 | /// DebugVariable class because trackAssignments doesn't (yet?) understand |
||
234 | /// partial variables (fragment info) as input and want to make that clear and |
||
235 | /// explicit using types. In addition, eventually we will want to understand |
||
236 | /// expressions that modify the base address too, which a DebugVariable doesn't |
||
237 | /// capture. |
||
238 | struct VarRecord { |
||
239 | DILocalVariable *Var; |
||
240 | DILocation *DL; |
||
241 | |||
242 | VarRecord(DbgVariableIntrinsic *DVI) |
||
243 | : Var(DVI->getVariable()), DL(getDebugValueLoc(DVI)) {} |
||
244 | VarRecord(DILocalVariable *Var, DILocation *DL) : Var(Var), DL(DL) {} |
||
245 | friend bool operator<(const VarRecord &LHS, const VarRecord &RHS) { |
||
246 | return std::tie(LHS.Var, LHS.DL) < std::tie(RHS.Var, RHS.DL); |
||
247 | } |
||
248 | friend bool operator==(const VarRecord &LHS, const VarRecord &RHS) { |
||
249 | return std::tie(LHS.Var, LHS.DL) == std::tie(RHS.Var, RHS.DL); |
||
250 | } |
||
251 | }; |
||
252 | |||
253 | /// Map of backing storage to a set of variables that are stored to it. |
||
254 | /// TODO: Backing storage shouldn't be limited to allocas only. Some local |
||
255 | /// variables have their storage allocated by the calling function (addresses |
||
256 | /// passed in with sret & byval parameters). |
||
257 | using StorageToVarsMap = DenseMap<const AllocaInst *, SmallSet<VarRecord, 2>>; |
||
258 | |||
259 | /// Track assignments to \p Vars between \p Start and \p End. |
||
260 | |||
261 | void trackAssignments(Function::iterator Start, Function::iterator End, |
||
262 | const StorageToVarsMap &Vars, const DataLayout &DL, |
||
263 | bool DebugPrints = false); |
||
264 | |||
265 | /// Describes properties of a store that has a static size and offset into a |
||
266 | /// some base storage. Used by the getAssignmentInfo functions. |
||
267 | struct AssignmentInfo { |
||
268 | AllocaInst const *Base; ///< Base storage. |
||
269 | uint64_t OffsetInBits; ///< Offset into Base. |
||
270 | uint64_t SizeInBits; ///< Number of bits stored. |
||
271 | bool StoreToWholeAlloca; ///< SizeInBits equals the size of the base storage. |
||
272 | |||
273 | AssignmentInfo(const DataLayout &DL, AllocaInst const *Base, |
||
274 | uint64_t OffsetInBits, uint64_t SizeInBits) |
||
275 | : Base(Base), OffsetInBits(OffsetInBits), SizeInBits(SizeInBits), |
||
276 | StoreToWholeAlloca( |
||
277 | OffsetInBits == 0 && |
||
278 | SizeInBits == DL.getTypeSizeInBits(Base->getAllocatedType())) {} |
||
279 | }; |
||
280 | |||
281 | std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL, |
||
282 | const MemIntrinsic *I); |
||
283 | std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL, |
||
284 | const StoreInst *SI); |
||
285 | std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL, |
||
286 | const AllocaInst *AI); |
||
287 | |||
288 | } // end namespace at |
||
289 | |||
290 | /// Convert @llvm.dbg.declare intrinsics into sets of @llvm.dbg.assign |
||
291 | /// intrinsics by treating stores to the dbg.declare'd address as assignments |
||
292 | /// to the variable. Not all kinds of variables are supported yet; those will |
||
293 | /// be left with their dbg.declare intrinsics. |
||
294 | /// The pass sets the debug-info-assignment-tracking module flag to true to |
||
295 | /// indicate assignment tracking has been enabled. |
||
296 | class AssignmentTrackingPass : public PassInfoMixin<AssignmentTrackingPass> { |
||
297 | /// Note: this method does not set the debug-info-assignment-tracking module |
||
298 | /// flag. |
||
299 | void runOnFunction(Function &F); |
||
300 | |||
301 | public: |
||
302 | PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); |
||
303 | PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); |
||
304 | }; |
||
305 | |||
306 | /// Return true if assignment tracking is enabled for module \p M. |
||
307 | bool isAssignmentTrackingEnabled(const Module &M); |
||
308 | } // end namespace llvm |
||
309 | |||
310 | #endif // LLVM_IR_DEBUGINFO_H |