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.
66 // Befriends DIRef so DIRef can befriend the protected member
67 // function: getFieldAs<DIRef>.
68 template <typename T> friend class DIRef;
71 /// \brief Duplicated debug info flags.
73 /// \see DebugNode::DIFlags.
75 #define HANDLE_DI_FLAG(ID, NAME) Flag##NAME = DebugNode::Flag##NAME,
76 #include "llvm/IR/DebugInfoFlags.def"
77 FlagAccessibility = DebugNode::FlagAccessibility
80 static unsigned getFlag(StringRef Flag);
81 static const char *getFlagString(unsigned Flag);
83 /// \brief Split up a flags bitfield.
85 /// Split \c Flags into \c SplitFlags, a vector of its components. Returns
86 /// any remaining (unrecognized) bits.
87 static unsigned splitFlags(unsigned Flags,
88 SmallVectorImpl<unsigned> &SplitFlags);
91 const MDNode *DbgNode;
93 DIDescriptor getDescriptorField(unsigned Elt) const;
94 template <typename DescTy> DescTy getFieldAs(unsigned Elt) const {
95 return DescTy(getDescriptorField(Elt));
99 explicit DIDescriptor(const MDNode *N = nullptr) : DbgNode(N) {}
101 MDNode *get() const { return const_cast<MDNode *>(DbgNode); }
102 operator MDNode *() const { return get(); }
103 MDNode *operator->() const { return get(); }
104 MDNode &operator*() const {
105 assert(get() && "Expected valid pointer");
109 // An explicit operator bool so that we can do testing of DI values
111 // FIXME: This operator bool isn't actually protecting anything at the
112 // moment due to the conversion operator above making DIDescriptor nodes
113 // implicitly convertable to bool.
114 explicit operator bool() const { return DbgNode != nullptr; }
116 bool operator==(DIDescriptor Other) const { return DbgNode == Other.DbgNode; }
117 bool operator!=(DIDescriptor Other) const { return !operator==(Other); }
119 uint16_t getTag() const {
120 if (auto *N = dyn_cast_or_null<DebugNode>(get()))
125 void print(raw_ostream &OS) const;
128 /// \brief Replace all uses of debug info referenced by this descriptor.
129 void replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D);
130 void replaceAllUsesWith(MDNode *D);
133 #define DECLARE_SIMPLIFY_DESCRIPTOR(DESC) \
135 template <> struct simplify_type<const DESC>; \
136 template <> struct simplify_type<DESC>;
137 DECLARE_SIMPLIFY_DESCRIPTOR(DIDescriptor)
138 DECLARE_SIMPLIFY_DESCRIPTOR(DISubrange)
139 DECLARE_SIMPLIFY_DESCRIPTOR(DIEnumerator)
140 DECLARE_SIMPLIFY_DESCRIPTOR(DIScope)
141 DECLARE_SIMPLIFY_DESCRIPTOR(DIType)
142 DECLARE_SIMPLIFY_DESCRIPTOR(DIBasicType)
143 DECLARE_SIMPLIFY_DESCRIPTOR(DIDerivedType)
144 DECLARE_SIMPLIFY_DESCRIPTOR(DICompositeType)
145 DECLARE_SIMPLIFY_DESCRIPTOR(DISubroutineType)
146 DECLARE_SIMPLIFY_DESCRIPTOR(DIFile)
147 DECLARE_SIMPLIFY_DESCRIPTOR(DICompileUnit)
148 DECLARE_SIMPLIFY_DESCRIPTOR(DISubprogram)
149 DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlock)
150 DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlockFile)
151 DECLARE_SIMPLIFY_DESCRIPTOR(DINameSpace)
152 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
153 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
154 DECLARE_SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
155 DECLARE_SIMPLIFY_DESCRIPTOR(DIVariable)
156 DECLARE_SIMPLIFY_DESCRIPTOR(DIExpression)
157 DECLARE_SIMPLIFY_DESCRIPTOR(DILocation)
158 DECLARE_SIMPLIFY_DESCRIPTOR(DIObjCProperty)
159 DECLARE_SIMPLIFY_DESCRIPTOR(DIImportedEntity)
160 #undef DECLARE_SIMPLIFY_DESCRIPTOR
162 /// \brief This is used to represent ranges, for array bounds.
163 class DISubrange : public DIDescriptor {
165 DISubrange() = default;
166 DISubrange(const MDSubrange *N) : DIDescriptor(N) {}
168 MDSubrange *get() const {
169 return cast_or_null<MDSubrange>(DIDescriptor::get());
171 operator MDSubrange *() const { return get(); }
172 MDSubrange *operator->() const { return get(); }
173 MDSubrange &operator*() const {
174 assert(get() && "Expected valid pointer");
178 int64_t getLo() const { return get()->getLo(); }
179 int64_t getCount() const { return get()->getCount(); }
182 /// \brief This descriptor holds an array of nodes with type T.
183 template <typename T> class DITypedArray : public DIDescriptor {
185 explicit DITypedArray(const MDNode *N = nullptr) : DIDescriptor(N) {}
186 operator MDTuple *() const {
187 return const_cast<MDTuple *>(cast_or_null<MDTuple>(DbgNode));
189 unsigned getNumElements() const {
190 return DbgNode ? DbgNode->getNumOperands() : 0;
192 T getElement(unsigned Idx) const { return getFieldAs<T>(Idx); }
195 typedef DITypedArray<DIDescriptor> DIArray;
197 /// \brief A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
199 /// FIXME: it seems strange that this doesn't have either a reference to the
200 /// type/precision or a file/line pair for location info.
201 class DIEnumerator : public DIDescriptor {
203 DIEnumerator() = default;
204 DIEnumerator(const MDEnumerator *N) : DIDescriptor(N) {}
206 MDEnumerator *get() const {
207 return cast_or_null<MDEnumerator>(DIDescriptor::get());
209 operator MDEnumerator *() const { return get(); }
210 MDEnumerator *operator->() const { return get(); }
211 MDEnumerator &operator*() const {
212 assert(get() && "Expected valid pointer");
216 StringRef getName() const { return get()->getName(); }
217 int64_t getEnumValue() const { return get()->getValue(); }
220 template <typename T> class DIRef;
221 typedef DIRef<DIDescriptor> DIDescriptorRef;
222 typedef DIRef<DIScope> DIScopeRef;
223 typedef DIRef<DIType> DITypeRef;
224 typedef DITypedArray<DITypeRef> DITypeArray;
226 /// \brief A base class for various scopes.
228 /// Although, implementation-wise, DIScope is the parent class of most
229 /// other DIxxx classes, including DIType and its descendants, most of
230 /// DIScope's descendants are not a substitutable subtype of
231 /// DIScope. The DIDescriptor::isScope() method only is true for
232 /// DIScopes that are scopes in the strict lexical scope sense
233 /// (DICompileUnit, DISubprogram, etc.), but not for, e.g., a DIType.
234 class DIScope : public DIDescriptor {
237 DIScope(const MDScope *N) : DIDescriptor(N) {}
239 MDScope *get() const { return cast_or_null<MDScope>(DIDescriptor::get()); }
240 operator MDScope *() const { return get(); }
241 MDScope *operator->() const { return get(); }
242 MDScope &operator*() const {
243 assert(get() && "Expected valid pointer");
247 /// \brief Get the parent scope.
249 /// Gets the parent scope for this scope node or returns a default
250 /// constructed scope.
251 DIScopeRef getContext() const;
252 /// \brief Get the scope name.
254 /// If the scope node has a name, return that, else return an empty string.
255 StringRef getName() const;
256 StringRef getFilename() const;
257 StringRef getDirectory() const;
259 /// \brief Generate a reference to this DIScope.
261 /// Uses the type identifier instead of the actual MDNode if possible, to
262 /// help type uniquing.
263 DIScopeRef getRef() const;
266 /// \brief Represents reference to a DIDescriptor.
268 /// Abstracts over direct and identifier-based metadata references.
269 template <typename T> class DIRef {
270 template <typename DescTy>
271 friend DescTy DIDescriptor::getFieldAs(unsigned Elt) const;
272 friend DIScopeRef DIScope::getContext() const;
273 friend DIScopeRef DIScope::getRef() const;
276 /// \brief Val can be either a MDNode or a MDString.
278 /// In the latter, MDString specifies the type identifier.
280 explicit DIRef(const Metadata *V);
284 DIRef(const TypedDebugNodeRef<U> &Ref,
285 typename std::enable_if<std::is_convertible<U *, T>::value>::type * =
289 T resolve(const DITypeIdentifierMap &Map) const;
290 operator Metadata *() const { return const_cast<Metadata *>(Val); }
292 static DIRef get(const Metadata *MD) { return DIRef(MD); }
296 DIDescriptor DIRef<DIDescriptor>::resolve(const DITypeIdentifierMap &Map) const;
298 DIScope DIRef<DIScope>::resolve(const DITypeIdentifierMap &Map) const;
299 template <> DIType DIRef<DIType>::resolve(const DITypeIdentifierMap &Map) const;
301 /// \brief Handle fields that are references to DIDescriptors.
303 DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const;
304 /// \brief Specialize DIRef constructor for DIDescriptorRef.
305 template <> DIRef<DIDescriptor>::DIRef(const Metadata *V);
307 /// \brief Handle fields that are references to DIScopes.
308 template <> DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const;
309 /// \brief Specialize DIRef constructor for DIScopeRef.
310 template <> DIRef<DIScope>::DIRef(const Metadata *V);
312 /// \brief Handle fields that are references to DITypes.
313 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const;
314 /// \brief Specialize DIRef constructor for DITypeRef.
315 template <> DIRef<DIType>::DIRef(const Metadata *V);
317 /// \brief This is a wrapper for a type.
319 /// FIXME: Types should be factored much better so that CV qualifiers and
320 /// others do not require a huge and empty descriptor full of zeros.
321 class DIType : public DIScope {
324 DIType(const MDType *N) : DIScope(N) {}
326 MDType *get() const { return cast_or_null<MDType>(DIDescriptor::get()); }
327 operator MDType *() const { return get(); }
328 MDType *operator->() const { return get(); }
329 MDType &operator*() const {
330 assert(get() && "Expected valid pointer");
334 DIScopeRef getContext() const { return DIScopeRef::get(get()->getScope()); }
335 StringRef getName() const { return get()->getName(); }
336 unsigned getLineNumber() const { return get()->getLine(); }
337 uint64_t getSizeInBits() const { return get()->getSizeInBits(); }
338 uint64_t getAlignInBits() const { return get()->getAlignInBits(); }
339 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
340 // carry this is just plain insane.
341 uint64_t getOffsetInBits() const { return get()->getOffsetInBits(); }
342 unsigned getFlags() const { return get()->getFlags(); }
343 bool isPrivate() const {
344 return (getFlags() & FlagAccessibility) == FlagPrivate;
346 bool isProtected() const {
347 return (getFlags() & FlagAccessibility) == FlagProtected;
349 bool isPublic() const {
350 return (getFlags() & FlagAccessibility) == FlagPublic;
352 bool isForwardDecl() const { return (getFlags() & FlagFwdDecl) != 0; }
353 bool isAppleBlockExtension() const {
354 return (getFlags() & FlagAppleBlock) != 0;
356 bool isBlockByrefStruct() const {
357 return (getFlags() & FlagBlockByrefStruct) != 0;
359 bool isVirtual() const { return (getFlags() & FlagVirtual) != 0; }
360 bool isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
361 bool isObjectPointer() const { return (getFlags() & FlagObjectPointer) != 0; }
362 bool isObjcClassComplete() const {
363 return (getFlags() & FlagObjcClassComplete) != 0;
365 bool isVector() const { return (getFlags() & FlagVector) != 0; }
366 bool isStaticMember() const { return (getFlags() & FlagStaticMember) != 0; }
367 bool isLValueReference() const {
368 return (getFlags() & FlagLValueReference) != 0;
370 bool isRValueReference() const {
371 return (getFlags() & FlagRValueReference) != 0;
373 bool isValid() const { return DbgNode && isa<MDType>(*this); }
376 /// \brief A basic type, like 'int' or 'float'.
377 class DIBasicType : public DIType {
379 DIBasicType() = default;
380 DIBasicType(const MDBasicType *N) : DIType(N) {}
382 MDBasicType *get() const {
383 return cast_or_null<MDBasicType>(DIDescriptor::get());
385 operator MDBasicType *() const { return get(); }
386 MDBasicType *operator->() const { return get(); }
387 MDBasicType &operator*() const {
388 assert(get() && "Expected valid pointer");
392 unsigned getEncoding() const { return get()->getEncoding(); }
395 /// \brief A simple derived type
397 /// Like a const qualified type, a typedef, a pointer or reference, et cetera.
398 /// Or, a data member of a class/struct/union.
399 class DIDerivedType : public DIType {
401 DIDerivedType() = default;
402 DIDerivedType(const MDDerivedTypeBase *N) : DIType(N) {}
404 MDDerivedTypeBase *get() const {
405 return cast_or_null<MDDerivedTypeBase>(DIDescriptor::get());
407 operator MDDerivedTypeBase *() const { return get(); }
408 MDDerivedTypeBase *operator->() const { return get(); }
409 MDDerivedTypeBase &operator*() const {
410 assert(get() && "Expected valid pointer");
414 DITypeRef getTypeDerivedFrom() const {
415 return DITypeRef::get(get()->getBaseType());
418 /// \brief Return property node, if this ivar is associated with one.
419 MDNode *getObjCProperty() const {
420 if (auto *N = dyn_cast<MDDerivedType>(get()))
421 return dyn_cast_or_null<MDNode>(N->getExtraData());
425 DITypeRef getClassType() const {
426 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
427 if (auto *N = dyn_cast<MDDerivedType>(get()))
428 return DITypeRef::get(N->getExtraData());
429 return DITypeRef::get(nullptr);
432 Constant *getConstant() const {
433 assert((getTag() == dwarf::DW_TAG_member) && isStaticMember());
434 if (auto *N = dyn_cast<MDDerivedType>(get()))
435 if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getExtraData()))
436 return C->getValue();
442 /// \brief Types that refer to multiple other types.
444 /// This descriptor holds a type that can refer to multiple other types, like a
445 /// function or struct.
447 /// DICompositeType is derived from DIDerivedType because some
448 /// composite types (such as enums) can be derived from basic types
449 // FIXME: Make this derive from DIType directly & just store the
450 // base type in a single DIType field.
451 class DICompositeType : public DIDerivedType {
452 friend class DIBuilder;
454 /// \brief Set the array of member DITypes.
455 void setArraysHelper(MDNode *Elements, MDNode *TParams);
458 DICompositeType() = default;
459 DICompositeType(const MDCompositeTypeBase *N) : DIDerivedType(N) {}
461 MDCompositeTypeBase *get() const {
462 return cast_or_null<MDCompositeTypeBase>(DIDescriptor::get());
464 operator MDCompositeTypeBase *() const { return get(); }
465 MDCompositeTypeBase *operator->() const { return get(); }
466 MDCompositeTypeBase &operator*() const {
467 assert(get() && "Expected valid pointer");
471 DIArray getElements() const {
472 assert(!isa<MDSubroutineType>(*this) && "no elements for DISubroutineType");
473 return DIArray(get()->getElements());
477 template <typename T>
478 void setArrays(DITypedArray<T> Elements, DIArray TParams = DIArray()) {
480 (!TParams || DbgNode->getNumOperands() == 8) &&
481 "If you're setting the template parameters this should include a slot "
483 setArraysHelper(Elements, TParams);
487 unsigned getRunTimeLang() const { return get()->getRuntimeLang(); }
488 DITypeRef getContainingType() const {
489 return DITypeRef::get(get()->getVTableHolder());
493 /// \brief Set the containing type.
494 void setContainingType(DICompositeType ContainingType);
497 DIArray getTemplateParams() const {
498 return DIArray(get()->getTemplateParams());
500 MDString *getIdentifier() const { return get()->getRawIdentifier(); }
503 class DISubroutineType : public DICompositeType {
505 DISubroutineType() = default;
506 DISubroutineType(const MDSubroutineType *N) : DICompositeType(N) {}
508 MDSubroutineType *get() const {
509 return cast_or_null<MDSubroutineType>(DIDescriptor::get());
511 operator MDSubroutineType *() const { return get(); }
512 MDSubroutineType *operator->() const { return get(); }
513 MDSubroutineType &operator*() const {
514 assert(get() && "Expected valid pointer");
518 DITypedArray<DITypeRef> getTypeArray() const {
519 return DITypedArray<DITypeRef>(get()->getTypeArray());
523 /// \brief This is a wrapper for a file.
524 class DIFile : public DIScope {
527 DIFile(const MDFile *N) : DIScope(N) {}
529 MDFile *get() const { return cast_or_null<MDFile>(DIDescriptor::get()); }
530 operator MDFile *() const { return get(); }
531 MDFile *operator->() const { return get(); }
532 MDFile &operator*() const {
533 assert(get() && "Expected valid pointer");
537 /// \brief Retrieve the MDNode for the directory/file pair.
538 MDNode *getFileNode() const { return get(); }
541 /// \brief A wrapper for a compile unit.
542 class DICompileUnit : public DIScope {
544 DICompileUnit() = default;
545 DICompileUnit(const MDCompileUnit *N) : DIScope(N) {}
547 MDCompileUnit *get() const {
548 return cast_or_null<MDCompileUnit>(DIDescriptor::get());
550 operator MDCompileUnit *() const { return get(); }
551 MDCompileUnit *operator->() const { return get(); }
552 MDCompileUnit &operator*() const {
553 assert(get() && "Expected valid pointer");
557 dwarf::SourceLanguage getLanguage() const {
558 return static_cast<dwarf::SourceLanguage>(get()->getSourceLanguage());
560 StringRef getProducer() const { return get()->getProducer(); }
561 bool isOptimized() const { return get()->isOptimized(); }
562 StringRef getFlags() const { return get()->getFlags(); }
563 unsigned getRunTimeVersion() const { return get()->getRuntimeVersion(); }
565 DIArray getEnumTypes() const { return DIArray(get()->getEnumTypes()); }
566 DIArray getRetainedTypes() const {
567 return DIArray(get()->getRetainedTypes());
569 DIArray getSubprograms() const { return DIArray(get()->getSubprograms()); }
570 DIArray getGlobalVariables() const {
571 return DIArray(get()->getGlobalVariables());
573 DIArray getImportedEntities() const {
574 return DIArray(get()->getImportedEntities());
577 void replaceSubprograms(DIArray Subprograms);
578 void replaceGlobalVariables(DIArray GlobalVariables);
580 StringRef getSplitDebugFilename() const {
581 return get()->getSplitDebugFilename();
583 unsigned getEmissionKind() const { return get()->getEmissionKind(); }
586 /// \brief This is a wrapper for a subprogram (e.g. a function).
587 class DISubprogram : public DIScope {
589 DISubprogram() = default;
590 DISubprogram(const MDSubprogram *N) : DIScope(N) {}
592 MDSubprogram *get() const {
593 return cast_or_null<MDSubprogram>(DIDescriptor::get());
595 operator MDSubprogram *() const { return get(); }
596 MDSubprogram *operator->() const { return get(); }
597 MDSubprogram &operator*() const {
598 assert(get() && "Expected valid pointer");
602 StringRef getName() const { return get()->getName(); }
603 StringRef getDisplayName() const { return get()->getDisplayName(); }
604 StringRef getLinkageName() const { return get()->getLinkageName(); }
605 unsigned getLineNumber() const { return get()->getLine(); }
607 /// \brief Check if this is local (like 'static' in C).
608 unsigned isLocalToUnit() const { return get()->isLocalToUnit(); }
609 unsigned isDefinition() const { return get()->isDefinition(); }
611 unsigned getVirtuality() const { return get()->getVirtuality(); }
612 unsigned getVirtualIndex() const { return get()->getVirtualIndex(); }
614 unsigned getFlags() const { return get()->getFlags(); }
616 unsigned isOptimized() const { return get()->isOptimized(); }
618 /// \brief Get the beginning of the scope of the function (not the name).
619 unsigned getScopeLineNumber() const { return get()->getScopeLine(); }
621 DIScopeRef getContext() const { return DIScopeRef::get(get()->getScope()); }
622 DISubroutineType getType() const {
623 return DISubroutineType(get()->getType());
626 DITypeRef getContainingType() const {
627 return DITypeRef::get(get()->getContainingType());
630 /// \brief Check if this provides debugging information for the function F.
631 bool describes(const Function *F);
633 Function *getFunction() const;
635 void replaceFunction(Function *F) {
637 N->replaceFunction(F);
639 DIArray getTemplateParams() const {
640 return DIArray(get()->getTemplateParams());
642 DISubprogram getFunctionDeclaration() const {
643 return DISubprogram(get()->getDeclaration());
645 MDNode *getVariablesNodes() const { return getVariables(); }
646 DIArray getVariables() const { return DIArray(get()->getVariables()); }
648 unsigned isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
649 /// \brief Check for the "private" access specifier.
650 bool isPrivate() const {
651 return (getFlags() & FlagAccessibility) == FlagPrivate;
653 /// \brief Check for the "protected" access specifier.
654 bool isProtected() const {
655 return (getFlags() & FlagAccessibility) == FlagProtected;
657 /// \brief Check for the "public" access specifier.
658 bool isPublic() const {
659 return (getFlags() & FlagAccessibility) == FlagPublic;
661 /// \brief Check for "explicit".
662 bool isExplicit() const { return (getFlags() & FlagExplicit) != 0; }
663 /// \brief Check if this is prototyped.
664 bool isPrototyped() const { return (getFlags() & FlagPrototyped) != 0; }
666 /// \brief Check if this is reference-qualified.
668 /// Return true if this subprogram is a C++11 reference-qualified non-static
669 /// member function (void foo() &).
670 unsigned isLValueReference() const {
671 return (getFlags() & FlagLValueReference) != 0;
674 /// \brief Check if this is rvalue-reference-qualified.
676 /// Return true if this subprogram is a C++11 rvalue-reference-qualified
677 /// non-static member function (void foo() &&).
678 unsigned isRValueReference() const {
679 return (getFlags() & FlagRValueReference) != 0;
683 /// \brief This is a wrapper for a lexical block.
684 class DILexicalBlock : public DIScope {
686 DILexicalBlock() = default;
687 DILexicalBlock(const MDLexicalBlockBase *N) : DIScope(N) {}
689 MDLexicalBlockBase *get() const {
690 return cast_or_null<MDLexicalBlockBase>(DIDescriptor::get());
692 operator MDLexicalBlockBase *() const { return get(); }
693 MDLexicalBlockBase *operator->() const { return get(); }
694 MDLexicalBlockBase &operator*() const {
695 assert(get() && "Expected valid pointer");
699 DIScope getContext() const { return DIScope(get()->getScope()); }
700 unsigned getLineNumber() const {
701 if (auto *N = dyn_cast<MDLexicalBlock>(get()))
705 unsigned getColumnNumber() const {
706 if (auto *N = dyn_cast<MDLexicalBlock>(get()))
707 return N->getColumn();
712 /// \brief This is a wrapper for a lexical block with a filename change.
713 class DILexicalBlockFile : public DIScope {
715 DILexicalBlockFile() = default;
716 DILexicalBlockFile(const MDLexicalBlockFile *N) : DIScope(N) {}
718 MDLexicalBlockFile *get() const {
719 return cast_or_null<MDLexicalBlockFile>(DIDescriptor::get());
721 operator MDLexicalBlockFile *() const { return get(); }
722 MDLexicalBlockFile *operator->() const { return get(); }
723 MDLexicalBlockFile &operator*() const {
724 assert(get() && "Expected valid pointer");
728 DIScope getContext() const { return get()->getScope(); }
729 unsigned getDiscriminator() const { return get()->getDiscriminator(); }
732 /// \brief A wrapper for a C++ style name space.
733 class DINameSpace : public DIScope {
735 DINameSpace() = default;
736 DINameSpace(const MDNamespace *N) : DIScope(N) {}
738 MDNamespace *get() const {
739 return cast_or_null<MDNamespace>(DIDescriptor::get());
741 operator MDNamespace *() const { return get(); }
742 MDNamespace *operator->() const { return get(); }
743 MDNamespace &operator*() const {
744 assert(get() && "Expected valid pointer");
748 StringRef getName() const { return get()->getName(); }
749 unsigned getLineNumber() const { return get()->getLine(); }
750 DIScope getContext() const { return DIScope(get()->getScope()); }
753 /// \brief This is a wrapper for template type parameter.
754 class DITemplateTypeParameter : public DIDescriptor {
756 DITemplateTypeParameter() = default;
757 DITemplateTypeParameter(const MDTemplateTypeParameter *N) : DIDescriptor(N) {}
759 MDTemplateTypeParameter *get() const {
760 return cast_or_null<MDTemplateTypeParameter>(DIDescriptor::get());
762 operator MDTemplateTypeParameter *() const { return get(); }
763 MDTemplateTypeParameter *operator->() const { return get(); }
764 MDTemplateTypeParameter &operator*() const {
765 assert(get() && "Expected valid pointer");
769 StringRef getName() const { return get()->getName(); }
771 DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
774 /// \brief This is a wrapper for template value parameter.
775 class DITemplateValueParameter : public DIDescriptor {
777 DITemplateValueParameter() = default;
778 DITemplateValueParameter(const MDTemplateValueParameter *N)
781 MDTemplateValueParameter *get() const {
782 return cast_or_null<MDTemplateValueParameter>(DIDescriptor::get());
784 operator MDTemplateValueParameter *() const { return get(); }
785 MDTemplateValueParameter *operator->() const { return get(); }
786 MDTemplateValueParameter &operator*() const {
787 assert(get() && "Expected valid pointer");
791 StringRef getName() const { return get()->getName(); }
792 DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
793 Metadata *getValue() const { return get()->getValue(); }
796 /// \brief This is a wrapper for a global variable.
797 class DIGlobalVariable : public DIDescriptor {
798 DIFile getFile() const { return DIFile(get()->getFile()); }
801 DIGlobalVariable() = default;
802 DIGlobalVariable(const MDGlobalVariable *N) : DIDescriptor(N) {}
804 MDGlobalVariable *get() const {
805 return cast_or_null<MDGlobalVariable>(DIDescriptor::get());
807 operator MDGlobalVariable *() const { return get(); }
808 MDGlobalVariable *operator->() const { return get(); }
809 MDGlobalVariable &operator*() const {
810 assert(get() && "Expected valid pointer");
814 StringRef getName() const { return get()->getName(); }
815 StringRef getDisplayName() const { return get()->getDisplayName(); }
816 StringRef getLinkageName() const { return get()->getLinkageName(); }
817 unsigned getLineNumber() const { return get()->getLine(); }
818 unsigned isLocalToUnit() const { return get()->isLocalToUnit(); }
819 unsigned isDefinition() const { return get()->isDefinition(); }
821 DIScope getContext() const { return DIScope(get()->getScope()); }
822 StringRef getFilename() const { return getFile().getFilename(); }
823 StringRef getDirectory() const { return getFile().getDirectory(); }
824 DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
826 GlobalVariable *getGlobal() const;
827 Constant *getConstant() const {
829 if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getVariable()))
830 return C->getValue();
833 DIDerivedType getStaticDataMemberDeclaration() const {
834 return DIDerivedType(get()->getStaticDataMemberDeclaration());
838 /// \brief This is a wrapper for a variable (e.g. parameter, local, global etc).
839 class DIVariable : public DIDescriptor {
840 unsigned getFlags() const { return get()->getFlags(); }
843 DIVariable() = default;
844 DIVariable(const MDLocalVariable *N) : DIDescriptor(N) {}
846 MDLocalVariable *get() const {
847 return cast_or_null<MDLocalVariable>(DIDescriptor::get());
849 operator MDLocalVariable *() const { return get(); }
850 MDLocalVariable *operator->() const { return get(); }
851 MDLocalVariable &operator*() const {
852 assert(get() && "Expected valid pointer");
856 StringRef getName() const { return get()->getName(); }
857 unsigned getLineNumber() const { return get()->getLine(); }
858 unsigned getArgNumber() const { return get()->getArg(); }
860 DIScope getContext() const { return DIScope(get()->getScope()); }
861 DIFile getFile() const { return DIFile(get()->getFile()); }
862 DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
864 /// \brief Return true if this variable is marked as "artificial".
865 bool isArtificial() const {
866 return (getFlags() & FlagArtificial) != 0;
869 bool isObjectPointer() const {
870 return (getFlags() & FlagObjectPointer) != 0;
873 /// \brief If this variable is inlined then return inline location.
874 MDNode *getInlinedAt() const { return DIDescriptor(get()->getInlinedAt()); }
876 /// \brief Check if this is a "__block" variable (Apple Blocks).
877 bool isBlockByrefVariable(const DITypeIdentifierMap &Map) const {
878 return (getType().resolve(Map)).isBlockByrefStruct();
881 /// \brief Check if this is an inlined function argument.
882 bool isInlinedFnArgument(const Function *CurFn);
884 /// \brief Return the size reported by the variable's type.
885 unsigned getSizeInBits(const DITypeIdentifierMap &Map);
887 void printExtendedName(raw_ostream &OS) const;
890 /// \brief A complex location expression in postfix notation.
892 /// This is (almost) a DWARF expression that modifies the location of a
893 /// variable or (or the location of a single piece of a variable).
895 /// FIXME: Instead of DW_OP_plus taking an argument, this should use DW_OP_const
896 /// and have DW_OP_plus consume the topmost elements on the stack.
897 class DIExpression : public DIDescriptor {
899 DIExpression() = default;
900 DIExpression(const MDExpression *N) : DIDescriptor(N) {}
902 MDExpression *get() const {
903 return cast_or_null<MDExpression>(DIDescriptor::get());
905 operator MDExpression *() const { return get(); }
906 MDExpression *operator->() const { return get(); }
907 MDExpression &operator*() const {
908 assert(get() && "Expected valid pointer");
912 /// \brief Return the number of elements in the complex expression.
913 unsigned getNumElements() const { return get()->getNumElements(); }
915 /// \brief return the Idx'th complex address element.
916 uint64_t getElement(unsigned I) const { return get()->getElement(I); }
918 /// \brief Return whether this is a piece of an aggregate variable.
919 bool isBitPiece() const;
920 /// \brief Return the offset of this piece in bits.
921 uint64_t getBitPieceOffset() const;
922 /// \brief Return the size of this piece in bits.
923 uint64_t getBitPieceSize() const;
926 /// \brief A lightweight wrapper around an element of a DIExpression.
928 friend class iterator;
929 MDExpression::element_iterator I;
931 Operand(MDExpression::element_iterator I) : I(I) {}
933 /// \brief Operands such as DW_OP_piece have explicit (non-stack) arguments.
934 /// Argument 0 is the operand itself.
935 uint64_t getArg(unsigned N) const {
936 MDExpression::element_iterator In = I;
940 operator uint64_t () const { return *I; }
941 /// \brief Returns underlying MDExpression::element_iterator.
942 const MDExpression::element_iterator &getBase() const { return I; }
943 /// \brief Returns the next operand.
944 iterator getNext() const;
947 /// \brief An iterator for DIExpression elements.
948 class iterator : public std::iterator<std::input_iterator_tag, StringRef,
949 unsigned, const Operand*, Operand> {
950 friend class Operand;
951 MDExpression::element_iterator I;
955 iterator(MDExpression::element_iterator I) : I(I) {}
956 const Operand &operator*() { return Tmp = Operand(I); }
957 const Operand *operator->() { return &(Tmp = Operand(I)); }
958 iterator &operator++() {
962 iterator operator++(int) {
967 bool operator==(const iterator &X) const { return I == X.I; }
968 bool operator!=(const iterator &X) const { return !(*this == X); }
973 case dwarf::DW_OP_bit_piece: std::advance(I, 3); break;
974 case dwarf::DW_OP_plus: std::advance(I, 2); break;
975 case dwarf::DW_OP_deref: std::advance(I, 1); break;
977 llvm_unreachable("unsupported operand");
982 iterator begin() const { return get()->elements_begin(); }
983 iterator end() const { return get()->elements_end(); }
986 /// \brief This object holds location information.
988 /// This object is not associated with any DWARF tag.
989 class DILocation : public DIDescriptor {
991 DILocation() = default;
992 DILocation(const MDLocation *N) : DIDescriptor(N) {}
994 MDLocation *get() const {
995 return cast_or_null<MDLocation>(DIDescriptor::get());
997 operator MDLocation *() const { return get(); }
998 MDLocation *operator->() const { return get(); }
999 MDLocation &operator*() const {
1000 assert(get() && "Expected valid pointer");
1004 unsigned getLineNumber() const { return get()->getLine(); }
1005 unsigned getColumnNumber() const { return get()->getColumn(); }
1006 DIScope getScope() const { return DIScope(get()->getScope()); }
1007 DILocation getOrigLocation() const {
1008 return DILocation(get()->getInlinedAt());
1010 StringRef getFilename() const { return getScope().getFilename(); }
1011 StringRef getDirectory() const { return getScope().getDirectory(); }
1012 bool atSameLineAs(const DILocation &Other) const {
1013 return (getLineNumber() == Other.getLineNumber() &&
1014 getFilename() == Other.getFilename());
1016 /// \brief Get the DWAF discriminator.
1018 /// DWARF discriminators are used to distinguish identical file locations for
1019 /// instructions that are on different basic blocks. If two instructions are
1020 /// inside the same lexical block and are in different basic blocks, we
1021 /// create a new lexical block with identical location as the original but
1022 /// with a different discriminator value
1023 /// (lib/Transforms/Util/AddDiscriminators.cpp for details).
1024 unsigned getDiscriminator() const {
1025 // Since discriminators are associated with lexical blocks, make
1026 // sure this location is a lexical block before retrieving its
1028 if (auto *F = dyn_cast<MDLexicalBlockFile>(get()->getScope()))
1029 return F->getDiscriminator();
1033 /// \brief Generate a new discriminator value for this location.
1034 unsigned computeNewDiscriminator(LLVMContext &Ctx);
1036 /// \brief Return a copy of this location with a different scope.
1037 DILocation copyWithNewScope(LLVMContext &Ctx, DILexicalBlockFile NewScope);
1040 class DIObjCProperty : public DIDescriptor {
1042 DIObjCProperty() = default;
1043 DIObjCProperty(const MDObjCProperty *N) : DIDescriptor(N) {}
1045 MDObjCProperty *get() const {
1046 return cast_or_null<MDObjCProperty>(DIDescriptor::get());
1048 operator MDObjCProperty *() const { return get(); }
1049 MDObjCProperty *operator->() const { return get(); }
1050 MDObjCProperty &operator*() const {
1051 assert(get() && "Expected valid pointer");
1055 StringRef getObjCPropertyName() const { return get()->getName(); }
1056 DIFile getFile() const { return DIFile(get()->getFile()); }
1057 unsigned getLineNumber() const { return get()->getLine(); }
1059 StringRef getObjCPropertyGetterName() const { return get()->getGetterName(); }
1060 StringRef getObjCPropertySetterName() const { return get()->getSetterName(); }
1061 unsigned getAttributes() const { return get()->getAttributes(); }
1062 bool isReadOnlyObjCProperty() const {
1063 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
1065 bool isReadWriteObjCProperty() const {
1066 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
1068 bool isAssignObjCProperty() const {
1069 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_assign) != 0;
1071 bool isRetainObjCProperty() const {
1072 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_retain) != 0;
1074 bool isCopyObjCProperty() const {
1075 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_copy) != 0;
1077 bool isNonAtomicObjCProperty() const {
1078 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
1081 /// \brief Get the type.
1083 /// \note Objective-C doesn't have an ODR, so there is no benefit in storing
1084 /// the type as a DITypeRef here.
1085 DIType getType() const { return DIType(get()->getType()); }
1088 /// \brief An imported module (C++ using directive or similar).
1089 class DIImportedEntity : public DIDescriptor {
1091 DIImportedEntity() = default;
1092 DIImportedEntity(const MDImportedEntity *N) : DIDescriptor(N) {}
1094 MDImportedEntity *get() const {
1095 return cast_or_null<MDImportedEntity>(DIDescriptor::get());
1097 operator MDImportedEntity *() const { return get(); }
1098 MDImportedEntity *operator->() const { return get(); }
1099 MDImportedEntity &operator*() const {
1100 assert(get() && "Expected valid pointer");
1104 DIScope getContext() const { return DIScope(get()->getScope()); }
1105 DIDescriptorRef getEntity() const {
1106 return DIDescriptorRef::get(get()->getEntity());
1108 unsigned getLineNumber() const { return get()->getLine(); }
1109 StringRef getName() const { return get()->getName(); }
1112 #define SIMPLIFY_DESCRIPTOR(DESC) \
1113 template <> struct simplify_type<const DESC> { \
1114 typedef Metadata *SimpleType; \
1115 static SimpleType getSimplifiedValue(const DESC &DI) { return DI; } \
1117 template <> struct simplify_type<DESC> : simplify_type<const DESC> {};
1118 SIMPLIFY_DESCRIPTOR(DIDescriptor)
1119 SIMPLIFY_DESCRIPTOR(DISubrange)
1120 SIMPLIFY_DESCRIPTOR(DIEnumerator)
1121 SIMPLIFY_DESCRIPTOR(DIScope)
1122 SIMPLIFY_DESCRIPTOR(DIType)
1123 SIMPLIFY_DESCRIPTOR(DIBasicType)
1124 SIMPLIFY_DESCRIPTOR(DIDerivedType)
1125 SIMPLIFY_DESCRIPTOR(DICompositeType)
1126 SIMPLIFY_DESCRIPTOR(DISubroutineType)
1127 SIMPLIFY_DESCRIPTOR(DIFile)
1128 SIMPLIFY_DESCRIPTOR(DICompileUnit)
1129 SIMPLIFY_DESCRIPTOR(DISubprogram)
1130 SIMPLIFY_DESCRIPTOR(DILexicalBlock)
1131 SIMPLIFY_DESCRIPTOR(DILexicalBlockFile)
1132 SIMPLIFY_DESCRIPTOR(DINameSpace)
1133 SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
1134 SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
1135 SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
1136 SIMPLIFY_DESCRIPTOR(DIVariable)
1137 SIMPLIFY_DESCRIPTOR(DIExpression)
1138 SIMPLIFY_DESCRIPTOR(DILocation)
1139 SIMPLIFY_DESCRIPTOR(DIObjCProperty)
1140 SIMPLIFY_DESCRIPTOR(DIImportedEntity)
1141 #undef SIMPLIFY_DESCRIPTOR
1143 /// \brief Find subprogram that is enclosing this scope.
1144 DISubprogram getDISubprogram(const MDNode *Scope);
1146 /// \brief Find debug info for a given function.
1147 /// \returns a valid DISubprogram, if found. Otherwise, it returns an empty
1149 DISubprogram getDISubprogram(const Function *F);
1151 /// \brief Find underlying composite type.
1152 DICompositeType getDICompositeType(DIType T);
1154 /// \brief Create a new inlined variable based on current variable.
1156 /// @param DV Current Variable.
1157 /// @param InlinedScope Location at current variable is inlined.
1158 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
1159 LLVMContext &VMContext);
1161 /// \brief Remove inlined scope from the variable.
1162 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
1164 /// \brief Generate map by visiting all retained types.
1165 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
1167 /// \brief Strip debug info in the module if it exists.
1169 /// To do this, we remove all calls to the debugger intrinsics and any named
1170 /// metadata for debugging. We also remove debug locations for instructions.
1171 /// Return true if module is modified.
1172 bool StripDebugInfo(Module &M);
1173 bool stripDebugInfo(Function &F);
1175 /// \brief Return Debug Info Metadata Version by checking module flags.
1176 unsigned getDebugMetadataVersionFromModule(const Module &M);
1178 /// \brief Utility to find all debug info in a module.
1180 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
1181 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
1182 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
1183 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
1184 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
1185 /// used by the CUs.
1186 class DebugInfoFinder {
1188 DebugInfoFinder() : TypeMapInitialized(false) {}
1190 /// \brief Process entire module and collect debug info anchors.
1191 void processModule(const Module &M);
1193 /// \brief Process DbgDeclareInst.
1194 void processDeclare(const Module &M, const DbgDeclareInst *DDI);
1195 /// \brief Process DbgValueInst.
1196 void processValue(const Module &M, const DbgValueInst *DVI);
1197 /// \brief Process DILocation.
1198 void processLocation(const Module &M, DILocation Loc);
1200 /// \brief Clear all lists.
1204 void InitializeTypeMap(const Module &M);
1206 void processType(DIType DT);
1207 void processSubprogram(DISubprogram SP);
1208 void processScope(DIScope Scope);
1209 bool addCompileUnit(DICompileUnit CU);
1210 bool addGlobalVariable(DIGlobalVariable DIG);
1211 bool addSubprogram(DISubprogram SP);
1212 bool addType(DIType DT);
1213 bool addScope(DIScope Scope);
1216 typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
1217 typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
1218 typedef SmallVectorImpl<DIGlobalVariable>::const_iterator
1219 global_variable_iterator;
1220 typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
1221 typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
1223 iterator_range<compile_unit_iterator> compile_units() const {
1224 return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
1227 iterator_range<subprogram_iterator> subprograms() const {
1228 return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
1231 iterator_range<global_variable_iterator> global_variables() const {
1232 return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
1235 iterator_range<type_iterator> types() const {
1236 return iterator_range<type_iterator>(TYs.begin(), TYs.end());
1239 iterator_range<scope_iterator> scopes() const {
1240 return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
1243 unsigned compile_unit_count() const { return CUs.size(); }
1244 unsigned global_variable_count() const { return GVs.size(); }
1245 unsigned subprogram_count() const { return SPs.size(); }
1246 unsigned type_count() const { return TYs.size(); }
1247 unsigned scope_count() const { return Scopes.size(); }
1250 SmallVector<DICompileUnit, 8> CUs;
1251 SmallVector<DISubprogram, 8> SPs;
1252 SmallVector<DIGlobalVariable, 8> GVs;
1253 SmallVector<DIType, 8> TYs;
1254 SmallVector<DIScope, 8> Scopes;
1255 SmallPtrSet<MDNode *, 64> NodesSeen;
1256 DITypeIdentifierMap TypeIdentifierMap;
1258 /// \brief Specify if TypeIdentifierMap is initialized.
1259 bool TypeMapInitialized;
1262 DenseMap<const Function *, DISubprogram> makeSubprogramMap(const Module &M);
1264 } // end namespace llvm