1 //===- DebugInfo.h - Debug Information Helpers ------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a bunch of datatypes that are useful for creating and
11 // walking debug info in LLVM IR form. They essentially provide wrappers around
12 // the information in the global variables that's needed when constructing the
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_IR_DEBUGINFO_H
18 #define LLVM_IR_DEBUGINFO_H
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/iterator_range.h"
25 #include "llvm/IR/DebugInfoMetadata.h"
26 #include "llvm/Support/Casting.h"
27 #include "llvm/Support/Dwarf.h"
28 #include "llvm/Support/ErrorHandling.h"
52 class DILexicalBlockFile;
57 /// \brief Maps from type identifier to the actual MDNode.
58 typedef DenseMap<const MDString *, MDNode *> DITypeIdentifierMap;
60 #define DECLARE_SIMPLIFY_DESCRIPTOR(DESC) \
62 template <> struct simplify_type<const DESC>; \
63 template <> struct simplify_type<DESC>;
64 DECLARE_SIMPLIFY_DESCRIPTOR(DISubrange)
65 DECLARE_SIMPLIFY_DESCRIPTOR(DIEnumerator)
66 DECLARE_SIMPLIFY_DESCRIPTOR(DIType)
67 DECLARE_SIMPLIFY_DESCRIPTOR(DIBasicType)
68 DECLARE_SIMPLIFY_DESCRIPTOR(DIDerivedType)
69 DECLARE_SIMPLIFY_DESCRIPTOR(DICompositeType)
70 DECLARE_SIMPLIFY_DESCRIPTOR(DISubroutineType)
71 DECLARE_SIMPLIFY_DESCRIPTOR(DIFile)
72 DECLARE_SIMPLIFY_DESCRIPTOR(DICompileUnit)
73 DECLARE_SIMPLIFY_DESCRIPTOR(DISubprogram)
74 DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlock)
75 DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlockFile)
76 DECLARE_SIMPLIFY_DESCRIPTOR(DINameSpace)
77 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
78 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
79 DECLARE_SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
80 DECLARE_SIMPLIFY_DESCRIPTOR(DIVariable)
81 DECLARE_SIMPLIFY_DESCRIPTOR(DIExpression)
82 DECLARE_SIMPLIFY_DESCRIPTOR(DILocation)
83 DECLARE_SIMPLIFY_DESCRIPTOR(DIObjCProperty)
84 DECLARE_SIMPLIFY_DESCRIPTOR(DIImportedEntity)
85 #undef DECLARE_SIMPLIFY_DESCRIPTOR
87 typedef DebugNodeArray DIArray;
88 typedef MDTypeRefArray DITypeArray;
94 DISubrange(const MDSubrange *N = nullptr) : N(const_cast<MDSubrange *>(N)) {}
96 operator MDSubrange *() const { return N; }
97 MDSubrange *operator->() const { return N; }
98 MDSubrange &operator*() const { return *N; }
105 DIEnumerator(const MDEnumerator *N = nullptr)
106 : N(const_cast<MDEnumerator *>(N)) {}
108 operator MDEnumerator *() const { return N; }
109 MDEnumerator *operator->() const { return N; }
110 MDEnumerator &operator*() const { return *N; }
117 DIType(const MDType *N = nullptr) : N(const_cast<MDType *>(N)) {}
119 operator MDType *() const { return N; }
120 MDType *operator->() const { return N; }
121 MDType &operator*() const { return *N; }
128 DIBasicType(const MDBasicType *N = nullptr)
129 : N(const_cast<MDBasicType *>(N)) {}
131 operator DIType() const { return N; }
132 operator MDBasicType *() const { return N; }
133 MDBasicType *operator->() const { return N; }
134 MDBasicType &operator*() const { return *N; }
137 class DIDerivedType {
138 MDDerivedTypeBase *N;
141 DIDerivedType(const MDDerivedTypeBase *N = nullptr)
142 : N(const_cast<MDDerivedTypeBase *>(N)) {}
144 operator DIType() const { return N; }
145 operator MDDerivedTypeBase *() const { return N; }
146 MDDerivedTypeBase *operator->() const { return N; }
147 MDDerivedTypeBase &operator*() const { return *N; }
150 class DICompositeType {
151 MDCompositeTypeBase *N;
154 DICompositeType(const MDCompositeTypeBase *N = nullptr)
155 : N(const_cast<MDCompositeTypeBase *>(N)) {}
157 operator DIType() const { return N; }
158 operator MDCompositeTypeBase *() const { return N; }
159 MDCompositeTypeBase *operator->() const { return N; }
160 MDCompositeTypeBase &operator*() const { return *N; }
163 class DISubroutineType {
167 DISubroutineType(const MDSubroutineType *N = nullptr)
168 : N(const_cast<MDSubroutineType *>(N)) {}
170 operator DIType() const { return N; }
171 operator DICompositeType() const { return N; }
172 operator MDSubroutineType *() const { return N; }
173 MDSubroutineType *operator->() const { return N; }
174 MDSubroutineType &operator*() const { return *N; }
181 DIFile(const MDFile *N = nullptr) : N(const_cast<MDFile *>(N)) {}
183 operator MDFile *() const { return N; }
184 MDFile *operator->() const { return N; }
185 MDFile &operator*() const { return *N; }
188 class DICompileUnit {
192 DICompileUnit(const MDCompileUnit *N = nullptr)
193 : N(const_cast<MDCompileUnit *>(N)) {}
195 operator MDCompileUnit *() const { return N; }
196 MDCompileUnit *operator->() const { return N; }
197 MDCompileUnit &operator*() const { return *N; }
204 DISubprogram(const MDSubprogram *N = nullptr)
205 : N(const_cast<MDSubprogram *>(N)) {}
207 operator MDSubprogram *() const { return N; }
208 MDSubprogram *operator->() const { return N; }
209 MDSubprogram &operator*() const { return *N; }
212 class DILexicalBlock {
213 MDLexicalBlockBase *N;
216 DILexicalBlock(const MDLexicalBlockBase *N = nullptr)
217 : N(const_cast<MDLexicalBlockBase *>(N)) {}
219 operator MDLexicalBlockBase *() const { return N; }
220 MDLexicalBlockBase *operator->() const { return N; }
221 MDLexicalBlockBase &operator*() const { return *N; }
224 class DILexicalBlockFile {
225 MDLexicalBlockFile *N;
228 DILexicalBlockFile(const MDLexicalBlockFile *N = nullptr)
229 : N(const_cast<MDLexicalBlockFile *>(N)) {}
231 operator MDLexicalBlockFile *() const { return N; }
232 MDLexicalBlockFile *operator->() const { return N; }
233 MDLexicalBlockFile &operator*() const { return *N; }
240 DINameSpace(const MDNamespace *N = nullptr)
241 : N(const_cast<MDNamespace *>(N)) {}
243 operator MDNamespace *() const { return N; }
244 MDNamespace *operator->() const { return N; }
245 MDNamespace &operator*() const { return *N; }
248 class DITemplateTypeParameter {
249 MDTemplateTypeParameter *N;
252 DITemplateTypeParameter(const MDTemplateTypeParameter *N = nullptr)
253 : N(const_cast<MDTemplateTypeParameter *>(N)) {}
255 operator MDTemplateTypeParameter *() const { return N; }
256 MDTemplateTypeParameter *operator->() const { return N; }
257 MDTemplateTypeParameter &operator*() const { return *N; }
260 class DITemplateValueParameter {
261 MDTemplateValueParameter *N;
264 DITemplateValueParameter(const MDTemplateValueParameter *N = nullptr)
265 : N(const_cast<MDTemplateValueParameter *>(N)) {}
267 operator MDTemplateValueParameter *() const { return N; }
268 MDTemplateValueParameter *operator->() const { return N; }
269 MDTemplateValueParameter &operator*() const { return *N; }
272 class DIGlobalVariable {
276 DIGlobalVariable(const MDGlobalVariable *N = nullptr)
277 : N(const_cast<MDGlobalVariable *>(N)) {}
279 operator MDGlobalVariable *() const { return N; }
280 MDGlobalVariable *operator->() const { return N; }
281 MDGlobalVariable &operator*() const { return *N; }
288 DIVariable(const MDLocalVariable *N = nullptr)
289 : N(const_cast<MDLocalVariable *>(N)) {}
291 operator MDLocalVariable *() const { return N; }
292 MDLocalVariable *operator->() const { return N; }
293 MDLocalVariable &operator*() const { return *N; }
300 DIExpression(const MDExpression *N = nullptr)
301 : N(const_cast<MDExpression *>(N)) {}
303 operator MDExpression *() const { return N; }
304 MDExpression *operator->() const { return N; }
305 MDExpression &operator*() const { return *N; }
312 DILocation(const MDLocation *N = nullptr) : N(const_cast<MDLocation *>(N)) {}
314 operator MDLocation *() const { return N; }
315 MDLocation *operator->() const { return N; }
316 MDLocation &operator*() const { return *N; }
319 class DIObjCProperty {
323 DIObjCProperty(const MDObjCProperty *N = nullptr)
324 : N(const_cast<MDObjCProperty *>(N)) {}
326 operator MDObjCProperty *() const { return N; }
327 MDObjCProperty *operator->() const { return N; }
328 MDObjCProperty &operator*() const { return *N; }
331 class DIImportedEntity {
335 DIImportedEntity(const MDImportedEntity *N = nullptr)
336 : N(const_cast<MDImportedEntity *>(N)) {}
338 operator MDImportedEntity *() const { return N; }
339 MDImportedEntity *operator->() const { return N; }
340 MDImportedEntity &operator*() const { return *N; }
343 #define SIMPLIFY_DESCRIPTOR(DESC) \
344 template <> struct simplify_type<const DESC> { \
345 typedef Metadata *SimpleType; \
346 static SimpleType getSimplifiedValue(const DESC &DI) { return DI; } \
348 template <> struct simplify_type<DESC> : simplify_type<const DESC> {};
349 SIMPLIFY_DESCRIPTOR(DISubrange)
350 SIMPLIFY_DESCRIPTOR(DIEnumerator)
351 SIMPLIFY_DESCRIPTOR(DIType)
352 SIMPLIFY_DESCRIPTOR(DIBasicType)
353 SIMPLIFY_DESCRIPTOR(DIDerivedType)
354 SIMPLIFY_DESCRIPTOR(DICompositeType)
355 SIMPLIFY_DESCRIPTOR(DISubroutineType)
356 SIMPLIFY_DESCRIPTOR(DIFile)
357 SIMPLIFY_DESCRIPTOR(DICompileUnit)
358 SIMPLIFY_DESCRIPTOR(DISubprogram)
359 SIMPLIFY_DESCRIPTOR(DILexicalBlock)
360 SIMPLIFY_DESCRIPTOR(DILexicalBlockFile)
361 SIMPLIFY_DESCRIPTOR(DINameSpace)
362 SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
363 SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
364 SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
365 SIMPLIFY_DESCRIPTOR(DIVariable)
366 SIMPLIFY_DESCRIPTOR(DIExpression)
367 SIMPLIFY_DESCRIPTOR(DILocation)
368 SIMPLIFY_DESCRIPTOR(DIObjCProperty)
369 SIMPLIFY_DESCRIPTOR(DIImportedEntity)
370 #undef SIMPLIFY_DESCRIPTOR
372 /// \brief Find subprogram that is enclosing this scope.
373 MDSubprogram *getDISubprogram(const MDNode *Scope);
375 /// \brief Find debug info for a given function.
376 /// \returns a valid DISubprogram, if found. Otherwise, it returns an empty
378 MDSubprogram *getDISubprogram(const Function *F);
380 /// \brief Find underlying composite type.
381 MDCompositeTypeBase *getDICompositeType(MDType *T);
383 /// \brief Generate map by visiting all retained types.
384 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
386 /// \brief Strip debug info in the module if it exists.
388 /// To do this, we remove all calls to the debugger intrinsics and any named
389 /// metadata for debugging. We also remove debug locations for instructions.
390 /// Return true if module is modified.
391 bool StripDebugInfo(Module &M);
392 bool stripDebugInfo(Function &F);
394 /// \brief Return Debug Info Metadata Version by checking module flags.
395 unsigned getDebugMetadataVersionFromModule(const Module &M);
397 /// \brief Utility to find all debug info in a module.
399 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
400 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
401 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
402 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
403 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
405 class DebugInfoFinder {
407 DebugInfoFinder() : TypeMapInitialized(false) {}
409 /// \brief Process entire module and collect debug info anchors.
410 void processModule(const Module &M);
412 /// \brief Process DbgDeclareInst.
413 void processDeclare(const Module &M, const DbgDeclareInst *DDI);
414 /// \brief Process DbgValueInst.
415 void processValue(const Module &M, const DbgValueInst *DVI);
416 /// \brief Process DILocation.
417 void processLocation(const Module &M, const MDLocation *Loc);
419 /// \brief Clear all lists.
423 void InitializeTypeMap(const Module &M);
425 void processType(MDType *DT);
426 void processSubprogram(MDSubprogram *SP);
427 void processScope(MDScope *Scope);
428 bool addCompileUnit(MDCompileUnit *CU);
429 bool addGlobalVariable(MDGlobalVariable *DIG);
430 bool addSubprogram(MDSubprogram *SP);
431 bool addType(MDType *DT);
432 bool addScope(MDScope *Scope);
435 typedef SmallVectorImpl<MDCompileUnit *>::const_iterator
436 compile_unit_iterator;
437 typedef SmallVectorImpl<MDSubprogram *>::const_iterator subprogram_iterator;
438 typedef SmallVectorImpl<MDGlobalVariable *>::const_iterator
439 global_variable_iterator;
440 typedef SmallVectorImpl<MDType *>::const_iterator type_iterator;
441 typedef SmallVectorImpl<MDScope *>::const_iterator scope_iterator;
443 iterator_range<compile_unit_iterator> compile_units() const {
444 return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
447 iterator_range<subprogram_iterator> subprograms() const {
448 return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
451 iterator_range<global_variable_iterator> global_variables() const {
452 return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
455 iterator_range<type_iterator> types() const {
456 return iterator_range<type_iterator>(TYs.begin(), TYs.end());
459 iterator_range<scope_iterator> scopes() const {
460 return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
463 unsigned compile_unit_count() const { return CUs.size(); }
464 unsigned global_variable_count() const { return GVs.size(); }
465 unsigned subprogram_count() const { return SPs.size(); }
466 unsigned type_count() const { return TYs.size(); }
467 unsigned scope_count() const { return Scopes.size(); }
470 SmallVector<MDCompileUnit *, 8> CUs;
471 SmallVector<MDSubprogram *, 8> SPs;
472 SmallVector<MDGlobalVariable *, 8> GVs;
473 SmallVector<MDType *, 8> TYs;
474 SmallVector<MDScope *, 8> Scopes;
475 SmallPtrSet<const MDNode *, 64> NodesSeen;
476 DITypeIdentifierMap TypeIdentifierMap;
478 /// \brief Specify if TypeIdentifierMap is initialized.
479 bool TypeMapInitialized;
482 DenseMap<const Function *, MDSubprogram *> makeSubprogramMap(const Module &M);
484 } // end namespace llvm