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;
58 /// \brief Maps from type identifier to the actual MDNode.
59 typedef DenseMap<const MDString *, MDNode *> DITypeIdentifierMap;
65 DIDescriptor(const MDNode *N = nullptr) : N(const_cast<MDNode *>(N)) {}
67 operator MDNode *() const { return N; }
68 MDNode *operator->() const { return N; }
69 MDNode &operator*() const { return *N; }
72 #define DECLARE_SIMPLIFY_DESCRIPTOR(DESC) \
74 template <> struct simplify_type<const DESC>; \
75 template <> struct simplify_type<DESC>;
76 DECLARE_SIMPLIFY_DESCRIPTOR(DIDescriptor)
77 DECLARE_SIMPLIFY_DESCRIPTOR(DISubrange)
78 DECLARE_SIMPLIFY_DESCRIPTOR(DIEnumerator)
79 DECLARE_SIMPLIFY_DESCRIPTOR(DIScope)
80 DECLARE_SIMPLIFY_DESCRIPTOR(DIType)
81 DECLARE_SIMPLIFY_DESCRIPTOR(DIBasicType)
82 DECLARE_SIMPLIFY_DESCRIPTOR(DIDerivedType)
83 DECLARE_SIMPLIFY_DESCRIPTOR(DICompositeType)
84 DECLARE_SIMPLIFY_DESCRIPTOR(DISubroutineType)
85 DECLARE_SIMPLIFY_DESCRIPTOR(DIFile)
86 DECLARE_SIMPLIFY_DESCRIPTOR(DICompileUnit)
87 DECLARE_SIMPLIFY_DESCRIPTOR(DISubprogram)
88 DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlock)
89 DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlockFile)
90 DECLARE_SIMPLIFY_DESCRIPTOR(DINameSpace)
91 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
92 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
93 DECLARE_SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
94 DECLARE_SIMPLIFY_DESCRIPTOR(DIVariable)
95 DECLARE_SIMPLIFY_DESCRIPTOR(DIExpression)
96 DECLARE_SIMPLIFY_DESCRIPTOR(DILocation)
97 DECLARE_SIMPLIFY_DESCRIPTOR(DIObjCProperty)
98 DECLARE_SIMPLIFY_DESCRIPTOR(DIImportedEntity)
99 #undef DECLARE_SIMPLIFY_DESCRIPTOR
101 typedef DebugNodeArray DIArray;
102 typedef MDTypeRefArray DITypeArray;
103 typedef DebugNodeRef DIDescriptorRef;
104 typedef MDScopeRef DIScopeRef;
105 typedef MDTypeRef DITypeRef;
111 DISubrange(const MDSubrange *N = nullptr) : N(const_cast<MDSubrange *>(N)) {}
113 operator MDSubrange *() const { return N; }
114 MDSubrange *operator->() const { return N; }
115 MDSubrange &operator*() const { return *N; }
122 DIEnumerator(const MDEnumerator *N = nullptr)
123 : N(const_cast<MDEnumerator *>(N)) {}
125 operator MDEnumerator *() const { return N; }
126 MDEnumerator *operator->() const { return N; }
127 MDEnumerator &operator*() const { return *N; }
134 DIScope(const MDScope *N = nullptr) : N(const_cast<MDScope *>(N)) {}
136 operator DIDescriptor() const { return N; }
137 operator MDScope *() const { return N; }
138 MDScope *operator->() const { return N; }
139 MDScope &operator*() const { return *N; }
146 DIType(const MDType *N = nullptr) : N(const_cast<MDType *>(N)) {}
148 operator DIDescriptor() const { return N; }
149 operator DIScope() const { return N; }
150 operator MDType *() const { return N; }
151 MDType *operator->() const { return N; }
152 MDType &operator*() const { return *N; }
159 DIBasicType(const MDBasicType *N = nullptr)
160 : N(const_cast<MDBasicType *>(N)) {}
162 operator DIDescriptor() const { return N; }
163 operator DIType() const { return N; }
164 operator MDBasicType *() const { return N; }
165 MDBasicType *operator->() const { return N; }
166 MDBasicType &operator*() const { return *N; }
169 class DIDerivedType {
170 MDDerivedTypeBase *N;
173 DIDerivedType(const MDDerivedTypeBase *N = nullptr)
174 : N(const_cast<MDDerivedTypeBase *>(N)) {}
176 operator DIDescriptor() const { return N; }
177 operator DIType() const { return N; }
178 operator MDDerivedTypeBase *() const { return N; }
179 MDDerivedTypeBase *operator->() const { return N; }
180 MDDerivedTypeBase &operator*() const { return *N; }
183 class DICompositeType {
184 MDCompositeTypeBase *N;
187 DICompositeType(const MDCompositeTypeBase *N = nullptr)
188 : N(const_cast<MDCompositeTypeBase *>(N)) {}
190 operator DIDescriptor() const { return N; }
191 operator DIType() const { return N; }
192 operator MDCompositeTypeBase *() const { return N; }
193 MDCompositeTypeBase *operator->() const { return N; }
194 MDCompositeTypeBase &operator*() const { return *N; }
197 class DISubroutineType {
201 DISubroutineType(const MDSubroutineType *N = nullptr)
202 : N(const_cast<MDSubroutineType *>(N)) {}
204 operator DIDescriptor() const { return N; }
205 operator DIType() const { return N; }
206 operator DICompositeType() const { return N; }
207 operator MDSubroutineType *() const { return N; }
208 MDSubroutineType *operator->() const { return N; }
209 MDSubroutineType &operator*() const { return *N; }
216 DIFile(const MDFile *N = nullptr) : N(const_cast<MDFile *>(N)) {}
218 operator DIDescriptor() const { return N; }
219 operator DIScope() const { return N; }
220 operator MDFile *() const { return N; }
221 MDFile *operator->() const { return N; }
222 MDFile &operator*() const { return *N; }
225 class DICompileUnit {
229 DICompileUnit(const MDCompileUnit *N = nullptr)
230 : N(const_cast<MDCompileUnit *>(N)) {}
232 operator DIDescriptor() const { return N; }
233 operator DIScope() const { return N; }
234 operator MDCompileUnit *() const { return N; }
235 MDCompileUnit *operator->() const { return N; }
236 MDCompileUnit &operator*() const { return *N; }
243 DISubprogram(const MDSubprogram *N = nullptr)
244 : N(const_cast<MDSubprogram *>(N)) {}
246 operator DIDescriptor() const { return N; }
247 operator DIScope() const { return N; }
248 operator MDSubprogram *() const { return N; }
249 MDSubprogram *operator->() const { return N; }
250 MDSubprogram &operator*() const { return *N; }
253 class DILexicalBlock {
254 MDLexicalBlockBase *N;
257 DILexicalBlock(const MDLexicalBlockBase *N = nullptr)
258 : N(const_cast<MDLexicalBlockBase *>(N)) {}
260 operator DIDescriptor() const { return N; }
261 operator MDLexicalBlockBase *() const { return N; }
262 MDLexicalBlockBase *operator->() const { return N; }
263 MDLexicalBlockBase &operator*() const { return *N; }
266 class DILexicalBlockFile {
267 MDLexicalBlockFile *N;
270 DILexicalBlockFile(const MDLexicalBlockFile *N = nullptr)
271 : N(const_cast<MDLexicalBlockFile *>(N)) {}
273 operator DIDescriptor() const { return N; }
274 operator MDLexicalBlockFile *() const { return N; }
275 MDLexicalBlockFile *operator->() const { return N; }
276 MDLexicalBlockFile &operator*() const { return *N; }
283 DINameSpace(const MDNamespace *N = nullptr)
284 : N(const_cast<MDNamespace *>(N)) {}
286 operator DIDescriptor() const { return N; }
287 operator DIScope() const { return N; }
288 operator MDNamespace *() const { return N; }
289 MDNamespace *operator->() const { return N; }
290 MDNamespace &operator*() const { return *N; }
293 class DITemplateTypeParameter {
294 MDTemplateTypeParameter *N;
297 DITemplateTypeParameter(const MDTemplateTypeParameter *N = nullptr)
298 : N(const_cast<MDTemplateTypeParameter *>(N)) {}
300 operator MDTemplateTypeParameter *() const { return N; }
301 MDTemplateTypeParameter *operator->() const { return N; }
302 MDTemplateTypeParameter &operator*() const { return *N; }
305 class DITemplateValueParameter {
306 MDTemplateValueParameter *N;
309 DITemplateValueParameter(const MDTemplateValueParameter *N = nullptr)
310 : N(const_cast<MDTemplateValueParameter *>(N)) {}
312 operator MDTemplateValueParameter *() const { return N; }
313 MDTemplateValueParameter *operator->() const { return N; }
314 MDTemplateValueParameter &operator*() const { return *N; }
317 class DIGlobalVariable {
321 DIGlobalVariable(const MDGlobalVariable *N = nullptr)
322 : N(const_cast<MDGlobalVariable *>(N)) {}
324 operator DIDescriptor() const { return N; }
325 operator MDGlobalVariable *() const { return N; }
326 MDGlobalVariable *operator->() const { return N; }
327 MDGlobalVariable &operator*() const { return *N; }
334 DIVariable(const MDLocalVariable *N = nullptr)
335 : N(const_cast<MDLocalVariable *>(N)) {}
337 operator MDLocalVariable *() const { return N; }
338 MDLocalVariable *operator->() const { return N; }
339 MDLocalVariable &operator*() const { return *N; }
346 DIExpression(const MDExpression *N = nullptr)
347 : N(const_cast<MDExpression *>(N)) {}
349 operator MDExpression *() const { return N; }
350 MDExpression *operator->() const { return N; }
351 MDExpression &operator*() const { return *N; }
358 DILocation(const MDLocation *N = nullptr) : N(const_cast<MDLocation *>(N)) {}
360 operator MDLocation *() const { return N; }
361 MDLocation *operator->() const { return N; }
362 MDLocation &operator*() const { return *N; }
365 class DIObjCProperty {
369 DIObjCProperty(const MDObjCProperty *N = nullptr)
370 : N(const_cast<MDObjCProperty *>(N)) {}
372 operator MDObjCProperty *() const { return N; }
373 MDObjCProperty *operator->() const { return N; }
374 MDObjCProperty &operator*() const { return *N; }
377 class DIImportedEntity {
381 DIImportedEntity(const MDImportedEntity *N = nullptr)
382 : N(const_cast<MDImportedEntity *>(N)) {}
384 operator DIDescriptor() const { return N; }
385 operator MDImportedEntity *() const { return N; }
386 MDImportedEntity *operator->() const { return N; }
387 MDImportedEntity &operator*() const { return *N; }
390 #define SIMPLIFY_DESCRIPTOR(DESC) \
391 template <> struct simplify_type<const DESC> { \
392 typedef Metadata *SimpleType; \
393 static SimpleType getSimplifiedValue(const DESC &DI) { return DI; } \
395 template <> struct simplify_type<DESC> : simplify_type<const DESC> {};
396 SIMPLIFY_DESCRIPTOR(DIDescriptor)
397 SIMPLIFY_DESCRIPTOR(DISubrange)
398 SIMPLIFY_DESCRIPTOR(DIEnumerator)
399 SIMPLIFY_DESCRIPTOR(DIScope)
400 SIMPLIFY_DESCRIPTOR(DIType)
401 SIMPLIFY_DESCRIPTOR(DIBasicType)
402 SIMPLIFY_DESCRIPTOR(DIDerivedType)
403 SIMPLIFY_DESCRIPTOR(DICompositeType)
404 SIMPLIFY_DESCRIPTOR(DISubroutineType)
405 SIMPLIFY_DESCRIPTOR(DIFile)
406 SIMPLIFY_DESCRIPTOR(DICompileUnit)
407 SIMPLIFY_DESCRIPTOR(DISubprogram)
408 SIMPLIFY_DESCRIPTOR(DILexicalBlock)
409 SIMPLIFY_DESCRIPTOR(DILexicalBlockFile)
410 SIMPLIFY_DESCRIPTOR(DINameSpace)
411 SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
412 SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
413 SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
414 SIMPLIFY_DESCRIPTOR(DIVariable)
415 SIMPLIFY_DESCRIPTOR(DIExpression)
416 SIMPLIFY_DESCRIPTOR(DILocation)
417 SIMPLIFY_DESCRIPTOR(DIObjCProperty)
418 SIMPLIFY_DESCRIPTOR(DIImportedEntity)
419 #undef SIMPLIFY_DESCRIPTOR
421 /// \brief Find subprogram that is enclosing this scope.
422 MDSubprogram *getDISubprogram(const MDNode *Scope);
424 /// \brief Find debug info for a given function.
425 /// \returns a valid DISubprogram, if found. Otherwise, it returns an empty
427 MDSubprogram *getDISubprogram(const Function *F);
429 /// \brief Find underlying composite type.
430 MDCompositeTypeBase *getDICompositeType(MDType *T);
432 /// \brief Generate map by visiting all retained types.
433 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
435 /// \brief Strip debug info in the module if it exists.
437 /// To do this, we remove all calls to the debugger intrinsics and any named
438 /// metadata for debugging. We also remove debug locations for instructions.
439 /// Return true if module is modified.
440 bool StripDebugInfo(Module &M);
441 bool stripDebugInfo(Function &F);
443 /// \brief Return Debug Info Metadata Version by checking module flags.
444 unsigned getDebugMetadataVersionFromModule(const Module &M);
446 /// \brief Utility to find all debug info in a module.
448 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
449 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
450 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
451 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
452 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
454 class DebugInfoFinder {
456 DebugInfoFinder() : TypeMapInitialized(false) {}
458 /// \brief Process entire module and collect debug info anchors.
459 void processModule(const Module &M);
461 /// \brief Process DbgDeclareInst.
462 void processDeclare(const Module &M, const DbgDeclareInst *DDI);
463 /// \brief Process DbgValueInst.
464 void processValue(const Module &M, const DbgValueInst *DVI);
465 /// \brief Process DILocation.
466 void processLocation(const Module &M, const MDLocation *Loc);
468 /// \brief Clear all lists.
472 void InitializeTypeMap(const Module &M);
474 void processType(MDType *DT);
475 void processSubprogram(MDSubprogram *SP);
476 void processScope(MDScope *Scope);
477 bool addCompileUnit(MDCompileUnit *CU);
478 bool addGlobalVariable(MDGlobalVariable *DIG);
479 bool addSubprogram(MDSubprogram *SP);
480 bool addType(MDType *DT);
481 bool addScope(MDScope *Scope);
484 typedef SmallVectorImpl<MDCompileUnit *>::const_iterator
485 compile_unit_iterator;
486 typedef SmallVectorImpl<MDSubprogram *>::const_iterator subprogram_iterator;
487 typedef SmallVectorImpl<MDGlobalVariable *>::const_iterator
488 global_variable_iterator;
489 typedef SmallVectorImpl<MDType *>::const_iterator type_iterator;
490 typedef SmallVectorImpl<MDScope *>::const_iterator scope_iterator;
492 iterator_range<compile_unit_iterator> compile_units() const {
493 return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
496 iterator_range<subprogram_iterator> subprograms() const {
497 return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
500 iterator_range<global_variable_iterator> global_variables() const {
501 return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
504 iterator_range<type_iterator> types() const {
505 return iterator_range<type_iterator>(TYs.begin(), TYs.end());
508 iterator_range<scope_iterator> scopes() const {
509 return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
512 unsigned compile_unit_count() const { return CUs.size(); }
513 unsigned global_variable_count() const { return GVs.size(); }
514 unsigned subprogram_count() const { return SPs.size(); }
515 unsigned type_count() const { return TYs.size(); }
516 unsigned scope_count() const { return Scopes.size(); }
519 SmallVector<MDCompileUnit *, 8> CUs;
520 SmallVector<MDSubprogram *, 8> SPs;
521 SmallVector<MDGlobalVariable *, 8> GVs;
522 SmallVector<MDType *, 8> TYs;
523 SmallVector<MDScope *, 8> Scopes;
524 SmallPtrSet<const MDNode *, 64> NodesSeen;
525 DITypeIdentifierMap TypeIdentifierMap;
527 /// \brief Specify if TypeIdentifierMap is initialized.
528 bool TypeMapInitialized;
531 DenseMap<const Function *, MDSubprogram *> makeSubprogramMap(const Module &M);
533 } // end namespace llvm