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;
61 /// \brief A thin wraper around MDNode to access encoded debug info.
63 /// This should not be stored in a container, because the underlying MDNode may
64 /// change in certain situations.
67 /// \brief Duplicated debug info flags.
69 /// \see DebugNode::DIFlags.
71 #define HANDLE_DI_FLAG(ID, NAME) Flag##NAME = DebugNode::Flag##NAME,
72 #include "llvm/IR/DebugInfoFlags.def"
73 FlagAccessibility = DebugNode::FlagAccessibility
77 const MDNode *DbgNode;
80 explicit DIDescriptor(const MDNode *N = nullptr) : DbgNode(N) {}
81 DIDescriptor(const DebugNode *N) : DbgNode(N) {}
83 MDNode *get() const { return const_cast<MDNode *>(DbgNode); }
84 operator MDNode *() const { return get(); }
85 MDNode *operator->() const { return get(); }
86 MDNode &operator*() const { return *get(); }
88 // An explicit operator bool so that we can do testing of DI values
90 // FIXME: This operator bool isn't actually protecting anything at the
91 // moment due to the conversion operator above making DIDescriptor nodes
92 // implicitly convertable to bool.
93 explicit operator bool() const { return DbgNode != nullptr; }
95 bool operator==(DIDescriptor Other) const { return DbgNode == Other.DbgNode; }
96 bool operator!=(DIDescriptor Other) const { return !operator==(Other); }
98 uint16_t getTag() const {
99 if (auto *N = dyn_cast_or_null<DebugNode>(get()))
104 void print(raw_ostream &OS) const;
108 #define DECLARE_SIMPLIFY_DESCRIPTOR(DESC) \
110 template <> struct simplify_type<const DESC>; \
111 template <> struct simplify_type<DESC>;
112 DECLARE_SIMPLIFY_DESCRIPTOR(DIDescriptor)
113 DECLARE_SIMPLIFY_DESCRIPTOR(DISubrange)
114 DECLARE_SIMPLIFY_DESCRIPTOR(DIEnumerator)
115 DECLARE_SIMPLIFY_DESCRIPTOR(DIScope)
116 DECLARE_SIMPLIFY_DESCRIPTOR(DIType)
117 DECLARE_SIMPLIFY_DESCRIPTOR(DIBasicType)
118 DECLARE_SIMPLIFY_DESCRIPTOR(DIDerivedType)
119 DECLARE_SIMPLIFY_DESCRIPTOR(DICompositeType)
120 DECLARE_SIMPLIFY_DESCRIPTOR(DISubroutineType)
121 DECLARE_SIMPLIFY_DESCRIPTOR(DIFile)
122 DECLARE_SIMPLIFY_DESCRIPTOR(DICompileUnit)
123 DECLARE_SIMPLIFY_DESCRIPTOR(DISubprogram)
124 DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlock)
125 DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlockFile)
126 DECLARE_SIMPLIFY_DESCRIPTOR(DINameSpace)
127 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
128 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
129 DECLARE_SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
130 DECLARE_SIMPLIFY_DESCRIPTOR(DIVariable)
131 DECLARE_SIMPLIFY_DESCRIPTOR(DIExpression)
132 DECLARE_SIMPLIFY_DESCRIPTOR(DILocation)
133 DECLARE_SIMPLIFY_DESCRIPTOR(DIObjCProperty)
134 DECLARE_SIMPLIFY_DESCRIPTOR(DIImportedEntity)
135 #undef DECLARE_SIMPLIFY_DESCRIPTOR
137 typedef DebugNodeArray DIArray;
138 typedef MDTypeRefArray DITypeArray;
140 /// \brief This is used to represent ranges, for array bounds.
141 class DISubrange : public DIDescriptor {
143 DISubrange() = default;
144 DISubrange(const MDSubrange *N) : DIDescriptor(N) {}
146 MDSubrange *get() const {
147 return cast_or_null<MDSubrange>(DIDescriptor::get());
149 operator MDSubrange *() const { return get(); }
150 MDSubrange *operator->() const { return get(); }
151 MDSubrange &operator*() const { return *get(); }
153 int64_t getLo() const { return get()->getLowerBound(); }
154 int64_t getCount() const { return get()->getCount(); }
157 /// \brief A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
159 /// FIXME: it seems strange that this doesn't have either a reference to the
160 /// type/precision or a file/line pair for location info.
161 class DIEnumerator : public DIDescriptor {
163 DIEnumerator() = default;
164 DIEnumerator(const MDEnumerator *N) : DIDescriptor(N) {}
166 MDEnumerator *get() const {
167 return cast_or_null<MDEnumerator>(DIDescriptor::get());
169 operator MDEnumerator *() const { return get(); }
170 MDEnumerator *operator->() const { return get(); }
171 MDEnumerator &operator*() const { return *get(); }
173 StringRef getName() const { return get()->getName(); }
174 int64_t getEnumValue() const { return get()->getValue(); }
177 template <typename T> class DIRef;
178 typedef DIRef<DIDescriptor> DIDescriptorRef;
179 typedef DIRef<DIScope> DIScopeRef;
180 typedef DIRef<DIType> DITypeRef;
182 /// \brief A base class for various scopes.
184 /// Although, implementation-wise, DIScope is the parent class of most
185 /// other DIxxx classes, including DIType and its descendants, most of
186 /// DIScope's descendants are not a substitutable subtype of
187 /// DIScope. The DIDescriptor::isScope() method only is true for
188 /// DIScopes that are scopes in the strict lexical scope sense
189 /// (DICompileUnit, DISubprogram, etc.), but not for, e.g., a DIType.
190 class DIScope : public DIDescriptor {
193 DIScope(const MDScope *N) : DIDescriptor(N) {}
195 MDScope *get() const { return cast_or_null<MDScope>(DIDescriptor::get()); }
196 operator MDScope *() const { return get(); }
197 MDScope *operator->() const { return get(); }
198 MDScope &operator*() const { return *get(); }
200 inline DIScopeRef getContext() const;
201 StringRef getName() const { return get()->getName(); }
202 StringRef getFilename() const { return get()->getFilename(); }
203 StringRef getDirectory() const { return get()->getDirectory(); }
205 /// \brief Generate a reference to this DIScope.
207 /// Uses the type identifier instead of the actual MDNode if possible, to
208 /// help type uniquing.
209 DIScopeRef getRef() const;
212 /// \brief Represents reference to a DIDescriptor.
214 /// Abstracts over direct and identifier-based metadata references.
215 template <typename T> class DIRef {
216 /// \brief Val can be either a MDNode or a MDString.
218 /// In the latter, MDString specifies the type identifier.
223 DIRef(const TypedDebugNodeRef<U> &Ref,
224 typename std::enable_if<std::is_convertible<U *, T>::value>::type * =
228 T resolve(const DITypeIdentifierMap &Map) const;
229 operator Metadata *() const { return const_cast<Metadata *>(Val); }
233 DIDescriptor DIRef<DIDescriptor>::resolve(const DITypeIdentifierMap &Map) const;
235 DIScope DIRef<DIScope>::resolve(const DITypeIdentifierMap &Map) const;
236 template <> DIType DIRef<DIType>::resolve(const DITypeIdentifierMap &Map) const;
238 DIScopeRef DIScope::getContext() const { return get()->getScope(); }
244 DIType(const MDType *N = nullptr) : N(const_cast<MDType *>(N)) {}
246 operator DIDescriptor() const { return N; }
247 operator DIScope() const { return N; }
248 operator MDType *() const { return N; }
249 MDType *operator->() const { return N; }
250 MDType &operator*() const { return *N; }
257 DIBasicType(const MDBasicType *N = nullptr)
258 : N(const_cast<MDBasicType *>(N)) {}
260 operator DIDescriptor() const { return N; }
261 operator DIType() const { return N; }
262 operator MDBasicType *() const { return N; }
263 MDBasicType *operator->() const { return N; }
264 MDBasicType &operator*() const { return *N; }
267 class DIDerivedType {
268 MDDerivedTypeBase *N;
271 DIDerivedType(const MDDerivedTypeBase *N = nullptr)
272 : N(const_cast<MDDerivedTypeBase *>(N)) {}
274 operator DIDescriptor() const { return N; }
275 operator DIType() const { return N; }
276 operator MDDerivedTypeBase *() const { return N; }
277 MDDerivedTypeBase *operator->() const { return N; }
278 MDDerivedTypeBase &operator*() const { return *N; }
281 class DICompositeType {
282 MDCompositeTypeBase *N;
285 DICompositeType(const MDCompositeTypeBase *N = nullptr)
286 : N(const_cast<MDCompositeTypeBase *>(N)) {}
288 operator DIDescriptor() const { return N; }
289 operator DIType() const { return N; }
290 operator MDCompositeTypeBase *() const { return N; }
291 MDCompositeTypeBase *operator->() const { return N; }
292 MDCompositeTypeBase &operator*() const { return *N; }
295 class DISubroutineType {
299 DISubroutineType(const MDSubroutineType *N = nullptr)
300 : N(const_cast<MDSubroutineType *>(N)) {}
302 operator DIDescriptor() const { return N; }
303 operator DIType() const { return N; }
304 operator DICompositeType() const { return N; }
305 operator MDSubroutineType *() const { return N; }
306 MDSubroutineType *operator->() const { return N; }
307 MDSubroutineType &operator*() const { return *N; }
314 DIFile(const MDFile *N = nullptr) : N(const_cast<MDFile *>(N)) {}
316 operator DIDescriptor() const { return N; }
317 operator DIScope() const { return N; }
318 operator MDFile *() const { return N; }
319 MDFile *operator->() const { return N; }
320 MDFile &operator*() const { return *N; }
323 class DICompileUnit {
327 DICompileUnit(const MDCompileUnit *N = nullptr)
328 : N(const_cast<MDCompileUnit *>(N)) {}
330 operator DIDescriptor() const { return N; }
331 operator DIScope() const { return N; }
332 operator MDCompileUnit *() const { return N; }
333 MDCompileUnit *operator->() const { return N; }
334 MDCompileUnit &operator*() const { return *N; }
341 DISubprogram(const MDSubprogram *N = nullptr)
342 : N(const_cast<MDSubprogram *>(N)) {}
344 operator DIDescriptor() const { return N; }
345 operator DIScope() const { return N; }
346 operator MDSubprogram *() const { return N; }
347 MDSubprogram *operator->() const { return N; }
348 MDSubprogram &operator*() const { return *N; }
351 class DILexicalBlock {
352 MDLexicalBlockBase *N;
355 DILexicalBlock(const MDLexicalBlockBase *N = nullptr)
356 : N(const_cast<MDLexicalBlockBase *>(N)) {}
358 operator DIDescriptor() const { return N; }
359 operator MDLexicalBlockBase *() const { return N; }
360 MDLexicalBlockBase *operator->() const { return N; }
361 MDLexicalBlockBase &operator*() const { return *N; }
364 class DILexicalBlockFile {
365 MDLexicalBlockFile *N;
368 DILexicalBlockFile(const MDLexicalBlockFile *N = nullptr)
369 : N(const_cast<MDLexicalBlockFile *>(N)) {}
371 operator DIDescriptor() const { return N; }
372 operator MDLexicalBlockFile *() const { return N; }
373 MDLexicalBlockFile *operator->() const { return N; }
374 MDLexicalBlockFile &operator*() const { return *N; }
381 DINameSpace(const MDNamespace *N = nullptr)
382 : N(const_cast<MDNamespace *>(N)) {}
384 operator DIDescriptor() const { return N; }
385 operator DIScope() const { return N; }
386 operator MDNamespace *() const { return N; }
387 MDNamespace *operator->() const { return N; }
388 MDNamespace &operator*() const { return *N; }
391 class DITemplateTypeParameter {
392 MDTemplateTypeParameter *N;
395 DITemplateTypeParameter(const MDTemplateTypeParameter *N = nullptr)
396 : N(const_cast<MDTemplateTypeParameter *>(N)) {}
398 operator MDTemplateTypeParameter *() const { return N; }
399 MDTemplateTypeParameter *operator->() const { return N; }
400 MDTemplateTypeParameter &operator*() const { return *N; }
403 class DITemplateValueParameter {
404 MDTemplateValueParameter *N;
407 DITemplateValueParameter(const MDTemplateValueParameter *N = nullptr)
408 : N(const_cast<MDTemplateValueParameter *>(N)) {}
410 operator MDTemplateValueParameter *() const { return N; }
411 MDTemplateValueParameter *operator->() const { return N; }
412 MDTemplateValueParameter &operator*() const { return *N; }
415 class DIGlobalVariable {
419 DIGlobalVariable(const MDGlobalVariable *N = nullptr)
420 : N(const_cast<MDGlobalVariable *>(N)) {}
422 operator DIDescriptor() const { return N; }
423 operator MDGlobalVariable *() const { return N; }
424 MDGlobalVariable *operator->() const { return N; }
425 MDGlobalVariable &operator*() const { return *N; }
432 DIVariable(const MDLocalVariable *N = nullptr)
433 : N(const_cast<MDLocalVariable *>(N)) {}
435 operator MDLocalVariable *() const { return N; }
436 MDLocalVariable *operator->() const { return N; }
437 MDLocalVariable &operator*() const { return *N; }
444 DIExpression(const MDExpression *N = nullptr)
445 : N(const_cast<MDExpression *>(N)) {}
447 operator MDExpression *() const { return N; }
448 MDExpression *operator->() const { return N; }
449 MDExpression &operator*() const { return *N; }
456 DILocation(const MDLocation *N = nullptr) : N(const_cast<MDLocation *>(N)) {}
458 operator MDLocation *() const { return N; }
459 MDLocation *operator->() const { return N; }
460 MDLocation &operator*() const { return *N; }
463 class DIObjCProperty {
467 DIObjCProperty(const MDObjCProperty *N = nullptr)
468 : N(const_cast<MDObjCProperty *>(N)) {}
470 operator MDObjCProperty *() const { return N; }
471 MDObjCProperty *operator->() const { return N; }
472 MDObjCProperty &operator*() const { return *N; }
475 class DIImportedEntity {
479 DIImportedEntity(const MDImportedEntity *N = nullptr)
480 : N(const_cast<MDImportedEntity *>(N)) {}
482 operator DIDescriptor() const { return N; }
483 operator MDImportedEntity *() const { return N; }
484 MDImportedEntity *operator->() const { return N; }
485 MDImportedEntity &operator*() const { return *N; }
488 #define SIMPLIFY_DESCRIPTOR(DESC) \
489 template <> struct simplify_type<const DESC> { \
490 typedef Metadata *SimpleType; \
491 static SimpleType getSimplifiedValue(const DESC &DI) { return DI; } \
493 template <> struct simplify_type<DESC> : simplify_type<const DESC> {};
494 SIMPLIFY_DESCRIPTOR(DIDescriptor)
495 SIMPLIFY_DESCRIPTOR(DISubrange)
496 SIMPLIFY_DESCRIPTOR(DIEnumerator)
497 SIMPLIFY_DESCRIPTOR(DIScope)
498 SIMPLIFY_DESCRIPTOR(DIType)
499 SIMPLIFY_DESCRIPTOR(DIBasicType)
500 SIMPLIFY_DESCRIPTOR(DIDerivedType)
501 SIMPLIFY_DESCRIPTOR(DICompositeType)
502 SIMPLIFY_DESCRIPTOR(DISubroutineType)
503 SIMPLIFY_DESCRIPTOR(DIFile)
504 SIMPLIFY_DESCRIPTOR(DICompileUnit)
505 SIMPLIFY_DESCRIPTOR(DISubprogram)
506 SIMPLIFY_DESCRIPTOR(DILexicalBlock)
507 SIMPLIFY_DESCRIPTOR(DILexicalBlockFile)
508 SIMPLIFY_DESCRIPTOR(DINameSpace)
509 SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
510 SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
511 SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
512 SIMPLIFY_DESCRIPTOR(DIVariable)
513 SIMPLIFY_DESCRIPTOR(DIExpression)
514 SIMPLIFY_DESCRIPTOR(DILocation)
515 SIMPLIFY_DESCRIPTOR(DIObjCProperty)
516 SIMPLIFY_DESCRIPTOR(DIImportedEntity)
517 #undef SIMPLIFY_DESCRIPTOR
519 /// \brief Find subprogram that is enclosing this scope.
520 DISubprogram getDISubprogram(const MDNode *Scope);
522 /// \brief Find debug info for a given function.
523 /// \returns a valid DISubprogram, if found. Otherwise, it returns an empty
525 DISubprogram getDISubprogram(const Function *F);
527 /// \brief Find underlying composite type.
528 DICompositeType getDICompositeType(DIType T);
530 /// \brief Generate map by visiting all retained types.
531 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
533 /// \brief Strip debug info in the module if it exists.
535 /// To do this, we remove all calls to the debugger intrinsics and any named
536 /// metadata for debugging. We also remove debug locations for instructions.
537 /// Return true if module is modified.
538 bool StripDebugInfo(Module &M);
539 bool stripDebugInfo(Function &F);
541 /// \brief Return Debug Info Metadata Version by checking module flags.
542 unsigned getDebugMetadataVersionFromModule(const Module &M);
544 /// \brief Utility to find all debug info in a module.
546 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
547 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
548 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
549 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
550 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
552 class DebugInfoFinder {
554 DebugInfoFinder() : TypeMapInitialized(false) {}
556 /// \brief Process entire module and collect debug info anchors.
557 void processModule(const Module &M);
559 /// \brief Process DbgDeclareInst.
560 void processDeclare(const Module &M, const DbgDeclareInst *DDI);
561 /// \brief Process DbgValueInst.
562 void processValue(const Module &M, const DbgValueInst *DVI);
563 /// \brief Process DILocation.
564 void processLocation(const Module &M, DILocation Loc);
566 /// \brief Clear all lists.
570 void InitializeTypeMap(const Module &M);
572 void processType(DIType DT);
573 void processSubprogram(DISubprogram SP);
574 void processScope(DIScope Scope);
575 bool addCompileUnit(DICompileUnit CU);
576 bool addGlobalVariable(DIGlobalVariable DIG);
577 bool addSubprogram(DISubprogram SP);
578 bool addType(DIType DT);
579 bool addScope(DIScope Scope);
582 typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
583 typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
584 typedef SmallVectorImpl<DIGlobalVariable>::const_iterator
585 global_variable_iterator;
586 typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
587 typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
589 iterator_range<compile_unit_iterator> compile_units() const {
590 return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
593 iterator_range<subprogram_iterator> subprograms() const {
594 return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
597 iterator_range<global_variable_iterator> global_variables() const {
598 return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
601 iterator_range<type_iterator> types() const {
602 return iterator_range<type_iterator>(TYs.begin(), TYs.end());
605 iterator_range<scope_iterator> scopes() const {
606 return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
609 unsigned compile_unit_count() const { return CUs.size(); }
610 unsigned global_variable_count() const { return GVs.size(); }
611 unsigned subprogram_count() const { return SPs.size(); }
612 unsigned type_count() const { return TYs.size(); }
613 unsigned scope_count() const { return Scopes.size(); }
616 SmallVector<DICompileUnit, 8> CUs;
617 SmallVector<DISubprogram, 8> SPs;
618 SmallVector<DIGlobalVariable, 8> GVs;
619 SmallVector<DIType, 8> TYs;
620 SmallVector<DIScope, 8> Scopes;
621 SmallPtrSet<MDNode *, 64> NodesSeen;
622 DITypeIdentifierMap TypeIdentifierMap;
624 /// \brief Specify if TypeIdentifierMap is initialized.
625 bool TypeMapInitialized;
628 DenseMap<const Function *, DISubprogram> makeSubprogramMap(const Module &M);
630 } // end namespace llvm