1 //===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===//
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 implements the helper classes used to build and interpret debug
11 // information in LLVM IR form.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/IR/DebugInfo.h"
16 #include "LLVMContextImpl.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/StringSwitch.h"
21 #include "llvm/Analysis/ValueTracking.h"
22 #include "llvm/IR/Constants.h"
23 #include "llvm/IR/DIBuilder.h"
24 #include "llvm/IR/DerivedTypes.h"
25 #include "llvm/IR/Instructions.h"
26 #include "llvm/IR/IntrinsicInst.h"
27 #include "llvm/IR/Intrinsics.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/IR/ValueHandle.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/Dwarf.h"
32 #include "llvm/Support/raw_ostream.h"
34 using namespace llvm::dwarf;
36 //===----------------------------------------------------------------------===//
38 //===----------------------------------------------------------------------===//
40 unsigned DIDescriptor::getFlag(StringRef Flag) {
41 return StringSwitch<unsigned>(Flag)
42 #define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME)
43 #include "llvm/IR/DebugInfoFlags.def"
47 const char *DIDescriptor::getFlagString(unsigned Flag) {
51 #define HANDLE_DI_FLAG(ID, NAME) \
53 return "DIFlag" #NAME;
54 #include "llvm/IR/DebugInfoFlags.def"
58 unsigned DIDescriptor::splitFlags(unsigned Flags,
59 SmallVectorImpl<unsigned> &SplitFlags) {
60 // Accessibility flags need to be specially handled, since they're packed
62 if (unsigned A = Flags & FlagAccessibility) {
64 SplitFlags.push_back(FlagPrivate);
65 else if (A == FlagProtected)
66 SplitFlags.push_back(FlagProtected);
68 SplitFlags.push_back(FlagPublic);
72 #define HANDLE_DI_FLAG(ID, NAME) \
73 if (unsigned Bit = Flags & ID) { \
74 SplitFlags.push_back(Bit); \
77 #include "llvm/IR/DebugInfoFlags.def"
82 bool DIDescriptor::Verify() const {
84 (DIDerivedType(DbgNode).Verify() ||
85 DICompositeType(DbgNode).Verify() || DIBasicType(DbgNode).Verify() ||
86 DIVariable(DbgNode).Verify() || DISubprogram(DbgNode).Verify() ||
87 DIGlobalVariable(DbgNode).Verify() || DIFile(DbgNode).Verify() ||
88 DICompileUnit(DbgNode).Verify() || DINameSpace(DbgNode).Verify() ||
89 DILexicalBlock(DbgNode).Verify() ||
90 DILexicalBlockFile(DbgNode).Verify() ||
91 DISubrange(DbgNode).Verify() || DIEnumerator(DbgNode).Verify() ||
92 DIObjCProperty(DbgNode).Verify() ||
93 DITemplateTypeParameter(DbgNode).Verify() ||
94 DITemplateValueParameter(DbgNode).Verify() ||
95 DIImportedEntity(DbgNode).Verify() || DIExpression(DbgNode).Verify());
98 static Metadata *getField(const MDNode *DbgNode, unsigned Elt) {
99 if (!DbgNode || Elt >= DbgNode->getNumOperands())
101 return DbgNode->getOperand(Elt);
104 static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) {
105 return dyn_cast_or_null<MDNode>(getField(DbgNode, Elt));
108 static StringRef getStringField(const MDNode *DbgNode, unsigned Elt) {
109 if (MDString *MDS = dyn_cast_or_null<MDString>(getField(DbgNode, Elt)))
110 return MDS->getString();
114 StringRef DIDescriptor::getStringField(unsigned Elt) const {
115 return ::getStringField(DbgNode, Elt);
118 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
119 if (auto *C = getConstantField(Elt))
120 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
121 return CI->getZExtValue();
126 int64_t DIDescriptor::getInt64Field(unsigned Elt) const {
127 if (auto *C = getConstantField(Elt))
128 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
129 return CI->getZExtValue();
134 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
135 MDNode *Field = getNodeField(DbgNode, Elt);
136 return DIDescriptor(Field);
139 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
140 return dyn_cast_or_null<GlobalVariable>(getConstantField(Elt));
143 Constant *DIDescriptor::getConstantField(unsigned Elt) const {
147 if (Elt < DbgNode->getNumOperands())
149 dyn_cast_or_null<ConstantAsMetadata>(DbgNode->getOperand(Elt)))
150 return C->getValue();
154 Function *DIDescriptor::getFunctionField(unsigned Elt) const {
155 return dyn_cast_or_null<Function>(getConstantField(Elt));
158 void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) {
162 if (Elt < DbgNode->getNumOperands()) {
163 MDNode *Node = const_cast<MDNode *>(DbgNode);
164 Node->replaceOperandWith(Elt, F ? ConstantAsMetadata::get(F) : nullptr);
168 static unsigned DIVariableInlinedAtIndex = 4;
169 MDNode *DIVariable::getInlinedAt() const {
170 return getNodeField(DbgNode, DIVariableInlinedAtIndex);
173 /// \brief Return the size reported by the variable's type.
174 unsigned DIVariable::getSizeInBits(const DITypeIdentifierMap &Map) {
175 DIType Ty = getType().resolve(Map);
176 // Follow derived types until we reach a type that
177 // reports back a size.
178 while (Ty.isDerivedType() && !Ty.getSizeInBits()) {
179 DIDerivedType DT(&*Ty);
180 Ty = DT.getTypeDerivedFrom().resolve(Map);
182 assert(Ty.getSizeInBits() && "type with size 0");
183 return Ty.getSizeInBits();
186 uint64_t DIExpression::getElement(unsigned Idx) const {
187 unsigned I = Idx + 1;
188 assert(I < getNumHeaderFields() &&
189 "non-existing complex address element requested");
190 return getHeaderFieldAs<int64_t>(I);
193 bool DIExpression::isBitPiece() const {
194 unsigned N = getNumElements();
195 return N >=3 && getElement(N-3) == dwarf::DW_OP_bit_piece;
198 uint64_t DIExpression::getBitPieceOffset() const {
199 assert(isBitPiece() && "not a piece");
200 return getElement(getNumElements()-2);
203 uint64_t DIExpression::getBitPieceSize() const {
204 assert(isBitPiece() && "not a piece");
205 return getElement(getNumElements()-1);
208 DIExpression::iterator DIExpression::begin() const {
209 return DIExpression::iterator(*this);
212 DIExpression::iterator DIExpression::end() const {
213 return DIExpression::iterator();
216 DIExpression::Operand DIExpression::Operand::getNext() const {
221 //===----------------------------------------------------------------------===//
223 //===----------------------------------------------------------------------===//
225 bool DIDescriptor::isSubroutineType() const {
226 return DbgNode && getTag() == dwarf::DW_TAG_subroutine_type;
229 bool DIDescriptor::isBasicType() const {
233 case dwarf::DW_TAG_base_type:
234 case dwarf::DW_TAG_unspecified_type:
241 bool DIDescriptor::isDerivedType() const {
245 case dwarf::DW_TAG_typedef:
246 case dwarf::DW_TAG_pointer_type:
247 case dwarf::DW_TAG_ptr_to_member_type:
248 case dwarf::DW_TAG_reference_type:
249 case dwarf::DW_TAG_rvalue_reference_type:
250 case dwarf::DW_TAG_const_type:
251 case dwarf::DW_TAG_volatile_type:
252 case dwarf::DW_TAG_restrict_type:
253 case dwarf::DW_TAG_member:
254 case dwarf::DW_TAG_inheritance:
255 case dwarf::DW_TAG_friend:
258 // CompositeTypes are currently modelled as DerivedTypes.
259 return isCompositeType();
263 bool DIDescriptor::isCompositeType() const {
267 case dwarf::DW_TAG_array_type:
268 case dwarf::DW_TAG_structure_type:
269 case dwarf::DW_TAG_union_type:
270 case dwarf::DW_TAG_enumeration_type:
271 case dwarf::DW_TAG_subroutine_type:
272 case dwarf::DW_TAG_class_type:
279 bool DIDescriptor::isVariable() const {
283 case dwarf::DW_TAG_auto_variable:
284 case dwarf::DW_TAG_arg_variable:
291 bool DIDescriptor::isType() const {
292 return isBasicType() || isCompositeType() || isDerivedType();
295 bool DIDescriptor::isSubprogram() const {
296 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
299 bool DIDescriptor::isGlobalVariable() const {
300 return DbgNode && getTag() == dwarf::DW_TAG_variable;
303 bool DIDescriptor::isScope() const {
307 case dwarf::DW_TAG_compile_unit:
308 case dwarf::DW_TAG_lexical_block:
309 case dwarf::DW_TAG_subprogram:
310 case dwarf::DW_TAG_namespace:
311 case dwarf::DW_TAG_file_type:
319 bool DIDescriptor::isTemplateTypeParameter() const {
320 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
323 bool DIDescriptor::isTemplateValueParameter() const {
324 return DbgNode && (getTag() == dwarf::DW_TAG_template_value_parameter ||
325 getTag() == dwarf::DW_TAG_GNU_template_template_param ||
326 getTag() == dwarf::DW_TAG_GNU_template_parameter_pack);
329 bool DIDescriptor::isCompileUnit() const {
330 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
333 bool DIDescriptor::isFile() const {
334 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
337 bool DIDescriptor::isNameSpace() const {
338 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
341 bool DIDescriptor::isLexicalBlockFile() const {
342 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
343 DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 2;
346 bool DIDescriptor::isLexicalBlock() const {
347 // FIXME: There are always exactly 4 header fields in DILexicalBlock, but
348 // something relies on this returning true for DILexicalBlockFile.
349 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
350 DbgNode->getNumOperands() == 3 &&
351 (getNumHeaderFields() == 2 || getNumHeaderFields() == 4);
354 bool DIDescriptor::isSubrange() const {
355 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
358 bool DIDescriptor::isEnumerator() const {
359 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
362 bool DIDescriptor::isObjCProperty() const {
363 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
366 bool DIDescriptor::isImportedEntity() const {
367 return DbgNode && (getTag() == dwarf::DW_TAG_imported_module ||
368 getTag() == dwarf::DW_TAG_imported_declaration);
371 bool DIDescriptor::isExpression() const {
372 return DbgNode && (getTag() == dwarf::DW_TAG_expression);
375 //===----------------------------------------------------------------------===//
376 // Simple Descriptor Constructors and other Methods
377 //===----------------------------------------------------------------------===//
379 void DIDescriptor::replaceAllUsesWith(LLVMContext &, DIDescriptor D) {
380 assert(DbgNode && "Trying to replace an unverified type!");
381 assert(DbgNode->isTemporary() && "Expected temporary node");
382 TempMDNode Temp(get());
384 // Since we use a TrackingVH for the node, its easy for clients to manufacture
385 // legitimate situations where they want to replaceAllUsesWith() on something
386 // which, due to uniquing, has merged with the source. We shield clients from
387 // this detail by allowing a value to be replaced with replaceAllUsesWith()
389 if (Temp.get() == D.get()) {
390 DbgNode = MDNode::replaceWithUniqued(std::move(Temp));
394 Temp->replaceAllUsesWith(D.get());
398 void DIDescriptor::replaceAllUsesWith(MDNode *D) {
399 assert(DbgNode && "Trying to replace an unverified type!");
400 assert(DbgNode != D && "This replacement should always happen");
401 assert(DbgNode->isTemporary() && "Expected temporary node");
402 TempMDNode Node(get());
403 Node->replaceAllUsesWith(D);
406 bool DICompileUnit::Verify() const {
407 if (!isCompileUnit())
410 // Don't bother verifying the compilation directory or producer string
411 // as those could be empty.
412 if (getFilename().empty())
415 return DbgNode->getNumOperands() == 7 && getNumHeaderFields() == 8;
418 bool DIObjCProperty::Verify() const {
419 if (!isObjCProperty())
422 // Don't worry about the rest of the strings for now.
423 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 6;
426 /// \brief Check if a field at position Elt of a MDNode is a MDNode.
427 static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) {
428 Metadata *Fld = getField(DbgNode, Elt);
429 return !Fld || isa<MDNode>(Fld);
432 /// \brief Check if a field at position Elt of a MDNode is a MDString.
433 static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) {
434 Metadata *Fld = getField(DbgNode, Elt);
435 return !Fld || isa<MDString>(Fld);
438 /// \brief Check if a value can be a reference to a type.
439 static bool isTypeRef(const Metadata *MD) {
442 if (auto *S = dyn_cast<MDString>(MD))
443 return !S->getString().empty();
444 if (auto *N = dyn_cast<MDNode>(MD))
445 return DIType(N).isType();
449 /// \brief Check if referenced field might be a type.
450 static bool fieldIsTypeRef(const MDNode *DbgNode, unsigned Elt) {
451 return isTypeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt)));
454 /// \brief Check if a value can be a ScopeRef.
455 static bool isScopeRef(const Metadata *MD) {
458 if (auto *S = dyn_cast<MDString>(MD))
459 return !S->getString().empty();
460 if (auto *N = dyn_cast<MDNode>(MD))
461 return DIScope(N).isScope();
465 /// \brief Check if a field at position Elt of a MDNode can be a ScopeRef.
466 static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) {
467 return isScopeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt)));
471 /// \brief Check if a value can be a DescriptorRef.
472 static bool isDescriptorRef(const Metadata *MD) {
475 if (auto *S = dyn_cast<MDString>(MD))
476 return !S->getString().empty();
477 return isa<MDNode>(MD);
481 bool DIType::Verify() const {
484 // Make sure Context @ field 2 is MDNode.
485 if (!fieldIsScopeRef(DbgNode, 2))
488 // FIXME: Sink this into the various subclass verifies.
489 uint16_t Tag = getTag();
490 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
491 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
492 Tag != dwarf::DW_TAG_ptr_to_member_type &&
493 Tag != dwarf::DW_TAG_reference_type &&
494 Tag != dwarf::DW_TAG_rvalue_reference_type &&
495 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_array_type &&
496 Tag != dwarf::DW_TAG_enumeration_type &&
497 Tag != dwarf::DW_TAG_subroutine_type &&
498 Tag != dwarf::DW_TAG_inheritance && Tag != dwarf::DW_TAG_friend &&
499 getFilename().empty())
502 // DIType is abstract, it should be a BasicType, a DerivedType or
505 return DIBasicType(DbgNode).Verify();
506 else if (isCompositeType())
507 return DICompositeType(DbgNode).Verify();
508 else if (isDerivedType())
509 return DIDerivedType(DbgNode).Verify();
514 bool DIBasicType::Verify() const {
515 return isBasicType() && DbgNode->getNumOperands() == 3 &&
516 getNumHeaderFields() == 8;
519 bool DIDerivedType::Verify() const {
520 // Make sure DerivedFrom @ field 3 is TypeRef.
521 if (!fieldIsTypeRef(DbgNode, 3))
523 if (getTag() == dwarf::DW_TAG_ptr_to_member_type)
524 // Make sure ClassType @ field 4 is a TypeRef.
525 if (!fieldIsTypeRef(DbgNode, 4))
528 return isDerivedType() && DbgNode->getNumOperands() >= 4 &&
529 DbgNode->getNumOperands() <= 8 && getNumHeaderFields() >= 7 &&
530 getNumHeaderFields() <= 8;
533 bool DICompositeType::Verify() const {
534 if (!isCompositeType())
537 // Make sure DerivedFrom @ field 3 and ContainingType @ field 5 are TypeRef.
538 if (!fieldIsTypeRef(DbgNode, 3))
540 if (!fieldIsTypeRef(DbgNode, 5))
543 // Make sure the type identifier at field 7 is MDString, it can be null.
544 if (!fieldIsMDString(DbgNode, 7))
547 // A subroutine type can't be both & and &&.
548 if (isLValueReference() && isRValueReference())
551 return DbgNode->getNumOperands() == 8 && getNumHeaderFields() == 8;
554 bool DISubprogram::Verify() const {
558 // Make sure context @ field 2 is a ScopeRef and type @ field 3 is a MDNode.
559 if (!fieldIsScopeRef(DbgNode, 2))
561 if (!fieldIsMDNode(DbgNode, 3))
563 // Containing type @ field 4.
564 if (!fieldIsTypeRef(DbgNode, 4))
567 // A subprogram can't be both & and &&.
568 if (isLValueReference() && isRValueReference())
571 // If a DISubprogram has an llvm::Function*, then scope chains from all
572 // instructions within the function should lead to this DISubprogram.
573 if (auto *F = getFunction()) {
574 for (auto &BB : *F) {
576 DebugLoc DL = I.getDebugLoc();
580 MDNode *Scope = nullptr;
581 MDNode *IA = nullptr;
582 // walk the inlined-at scopes
583 while ((IA = DL.getInlinedAt()))
584 DL = DebugLoc::getFromDILocation(IA);
585 DL.getScopeAndInlinedAt(Scope, IA);
589 while (!DIDescriptor(Scope).isSubprogram()) {
590 DILexicalBlockFile D(Scope);
591 Scope = D.isLexicalBlockFile()
593 : DebugLoc::getFromDILexicalBlock(Scope).getScope();
597 if (!DISubprogram(Scope).describes(F))
602 return DbgNode->getNumOperands() == 9 && getNumHeaderFields() == 12;
605 bool DIGlobalVariable::Verify() const {
606 if (!isGlobalVariable())
609 if (getDisplayName().empty())
611 // Make sure context @ field 1 is an MDNode.
612 if (!fieldIsMDNode(DbgNode, 1))
614 // Make sure that type @ field 3 is a DITypeRef.
615 if (!fieldIsTypeRef(DbgNode, 3))
617 // Make sure StaticDataMemberDeclaration @ field 5 is MDNode.
618 if (!fieldIsMDNode(DbgNode, 5))
621 return DbgNode->getNumOperands() == 6 && getNumHeaderFields() == 7;
624 bool DIVariable::Verify() const {
628 // Make sure context @ field 1 is an MDNode.
629 if (!fieldIsMDNode(DbgNode, 1))
631 // Make sure that type @ field 3 is a DITypeRef.
632 if (!fieldIsTypeRef(DbgNode, 3))
635 // Check the number of header fields, which is common between complex and
637 if (getNumHeaderFields() != 4)
640 // Variable without an inline location.
641 if (DbgNode->getNumOperands() == 4)
644 // Variable with an inline location.
645 return getInlinedAt() != nullptr && DbgNode->getNumOperands() == 5;
648 bool DIExpression::Verify() const {
649 // Empty DIExpressions may be represented as a nullptr.
653 if (!(isExpression() && DbgNode->getNumOperands() == 1))
656 for (auto Op : *this)
658 case DW_OP_bit_piece:
659 // Must be the last element of the expression.
660 return std::distance(Op.getBase(), DIHeaderFieldIterator()) == 3;
662 if (std::distance(Op.getBase(), DIHeaderFieldIterator()) < 2)
668 // Other operators are not yet supported by the backend.
674 bool DILocation::Verify() const {
675 return DbgNode && isa<MDLocation>(DbgNode);
678 bool DINameSpace::Verify() const {
681 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 3;
684 MDNode *DIFile::getFileNode() const { return getNodeField(DbgNode, 1); }
686 bool DIFile::Verify() const {
687 return isFile() && DbgNode->getNumOperands() == 2;
690 bool DIEnumerator::Verify() const {
691 return isEnumerator() && DbgNode->getNumOperands() == 1 &&
692 getNumHeaderFields() == 3;
695 bool DISubrange::Verify() const {
696 return isSubrange() && DbgNode->getNumOperands() == 1 &&
697 getNumHeaderFields() == 3;
700 bool DILexicalBlock::Verify() const {
701 return isLexicalBlock() && DbgNode->getNumOperands() == 3 &&
702 getNumHeaderFields() == 4;
705 bool DILexicalBlockFile::Verify() const {
706 return isLexicalBlockFile() && DbgNode->getNumOperands() == 3 &&
707 getNumHeaderFields() == 2;
710 bool DITemplateTypeParameter::Verify() const {
711 return isTemplateTypeParameter() && DbgNode->getNumOperands() == 4 &&
712 getNumHeaderFields() == 4;
715 bool DITemplateValueParameter::Verify() const {
716 return isTemplateValueParameter() && DbgNode->getNumOperands() == 5 &&
717 getNumHeaderFields() == 4;
720 bool DIImportedEntity::Verify() const {
721 return isImportedEntity() && DbgNode->getNumOperands() == 3 &&
722 getNumHeaderFields() == 3;
725 MDNode *DIDerivedType::getObjCProperty() const {
726 return getNodeField(DbgNode, 4);
729 MDString *DICompositeType::getIdentifier() const {
730 return cast_or_null<MDString>(getField(DbgNode, 7));
734 static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) {
735 for (unsigned i = 0; i != LHS->getNumOperands(); ++i) {
736 // Skip the 'empty' list (that's a single i32 0, rather than truly empty).
737 if (i == 0 && mdconst::hasa<ConstantInt>(LHS->getOperand(i)))
739 const MDNode *E = cast<MDNode>(LHS->getOperand(i));
741 for (unsigned j = 0; !found && j != RHS->getNumOperands(); ++j)
742 found = (E == cast<MDNode>(RHS->getOperand(j)));
743 assert(found && "Losing a member during member list replacement");
748 void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) {
749 TrackingMDNodeRef N(*this);
752 // Check that the new list of members contains all the old members as well.
753 if (const MDNode *El = cast_or_null<MDNode>(N->getOperand(4)))
754 VerifySubsetOf(El, Elements);
756 N->replaceOperandWith(4, Elements);
759 N->replaceOperandWith(6, TParams);
763 DIScopeRef DIScope::getRef() const {
764 if (!isCompositeType())
765 return DIScopeRef(*this);
766 DICompositeType DTy(DbgNode);
767 if (!DTy.getIdentifier())
768 return DIScopeRef(*this);
769 return DIScopeRef(DTy.getIdentifier());
772 void DICompositeType::setContainingType(DICompositeType ContainingType) {
773 TrackingMDNodeRef N(*this);
774 N->replaceOperandWith(5, ContainingType.getRef());
778 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
779 assert(CurFn && "Invalid function");
780 if (!getContext().isSubprogram())
782 // This variable is not inlined function argument if its scope
783 // does not describe current function.
784 return !DISubprogram(getContext()).describes(CurFn);
787 bool DISubprogram::describes(const Function *F) {
788 assert(F && "Invalid function");
789 if (F == getFunction())
791 StringRef Name = getLinkageName();
794 if (F->getName() == Name)
799 MDNode *DISubprogram::getVariablesNodes() const {
800 return getNodeField(DbgNode, 8);
803 DIArray DISubprogram::getVariables() const {
804 return DIArray(getNodeField(DbgNode, 8));
807 Metadata *DITemplateValueParameter::getValue() const {
808 return DbgNode->getOperand(3);
811 DIScopeRef DIScope::getContext() const {
814 return DIType(DbgNode).getContext();
817 return DIScopeRef(DISubprogram(DbgNode).getContext());
819 if (isLexicalBlock())
820 return DIScopeRef(DILexicalBlock(DbgNode).getContext());
822 if (isLexicalBlockFile())
823 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext());
826 return DIScopeRef(DINameSpace(DbgNode).getContext());
828 assert((isFile() || isCompileUnit()) && "Unhandled type of scope.");
829 return DIScopeRef(nullptr);
832 StringRef DIScope::getName() const {
834 return DIType(DbgNode).getName();
836 return DISubprogram(DbgNode).getName();
838 return DINameSpace(DbgNode).getName();
839 assert((isLexicalBlock() || isLexicalBlockFile() || isFile() ||
841 "Unhandled type of scope.");
845 StringRef DIScope::getFilename() const {
848 return ::getStringField(getNodeField(DbgNode, 1), 0);
851 StringRef DIScope::getDirectory() const {
854 return ::getStringField(getNodeField(DbgNode, 1), 1);
857 DIArray DICompileUnit::getEnumTypes() const {
858 if (!DbgNode || DbgNode->getNumOperands() < 7)
861 return DIArray(getNodeField(DbgNode, 2));
864 DIArray DICompileUnit::getRetainedTypes() const {
865 if (!DbgNode || DbgNode->getNumOperands() < 7)
868 return DIArray(getNodeField(DbgNode, 3));
871 DIArray DICompileUnit::getSubprograms() const {
872 if (!DbgNode || DbgNode->getNumOperands() < 7)
875 return DIArray(getNodeField(DbgNode, 4));
878 DIArray DICompileUnit::getGlobalVariables() const {
879 if (!DbgNode || DbgNode->getNumOperands() < 7)
882 return DIArray(getNodeField(DbgNode, 5));
885 DIArray DICompileUnit::getImportedEntities() const {
886 if (!DbgNode || DbgNode->getNumOperands() < 7)
889 return DIArray(getNodeField(DbgNode, 6));
892 void DICompileUnit::replaceSubprograms(DIArray Subprograms) {
893 assert(Verify() && "Expected compile unit");
894 if (Subprograms == getSubprograms())
897 const_cast<MDNode *>(DbgNode)->replaceOperandWith(4, Subprograms);
900 void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {
901 assert(Verify() && "Expected compile unit");
902 if (GlobalVariables == getGlobalVariables())
905 const_cast<MDNode *>(DbgNode)->replaceOperandWith(5, GlobalVariables);
908 DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
909 DILexicalBlockFile NewScope) {
911 assert(NewScope && "Expected valid scope");
913 const auto *Old = cast<MDLocation>(DbgNode);
914 return DILocation(MDLocation::get(Ctx, Old->getLine(), Old->getColumn(),
915 NewScope, Old->getInlinedAt()));
918 unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
919 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
920 return ++Ctx.pImpl->DiscriminatorTable[Key];
923 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
924 LLVMContext &VMContext) {
925 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
927 return cleanseInlinedVariable(DV, VMContext);
929 // Insert inlined scope.
930 SmallVector<Metadata *, 8> Elts(DV->op_begin(),
931 DV->op_begin() + DIVariableInlinedAtIndex);
932 Elts.push_back(InlinedScope);
934 DIVariable Inlined(MDNode::get(VMContext, Elts));
935 assert(Inlined.Verify() && "Expected to create a DIVariable");
939 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
940 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
941 if (!DIVariable(DV).getInlinedAt())
942 return DIVariable(DV);
944 // Remove inlined scope.
945 SmallVector<Metadata *, 8> Elts(DV->op_begin(),
946 DV->op_begin() + DIVariableInlinedAtIndex);
948 DIVariable Cleansed(MDNode::get(VMContext, Elts));
949 assert(Cleansed.Verify() && "Expected to create a DIVariable");
953 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
954 DIDescriptor D(Scope);
955 if (D.isSubprogram())
956 return DISubprogram(Scope);
958 if (D.isLexicalBlockFile())
959 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
961 if (D.isLexicalBlock())
962 return getDISubprogram(DILexicalBlock(Scope).getContext());
964 return DISubprogram();
967 DISubprogram llvm::getDISubprogram(const Function *F) {
968 // We look for the first instr that has a debug annotation leading back to F.
969 for (auto &BB : *F) {
970 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
971 return !Inst.getDebugLoc().isUnknown();
973 if (Inst == BB.end())
975 DebugLoc DLoc = Inst->getDebugLoc();
976 const MDNode *Scope = DLoc.getScopeNode();
977 DISubprogram Subprogram = getDISubprogram(Scope);
978 return Subprogram.describes(F) ? Subprogram : DISubprogram();
981 return DISubprogram();
984 DICompositeType llvm::getDICompositeType(DIType T) {
985 if (T.isCompositeType())
986 return DICompositeType(T);
988 if (T.isDerivedType()) {
989 // This function is currently used by dragonegg and dragonegg does
990 // not generate identifier for types, so using an empty map to resolve
991 // DerivedFrom should be fine.
992 DITypeIdentifierMap EmptyMap;
993 return getDICompositeType(
994 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap));
997 return DICompositeType();
1001 llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
1002 DITypeIdentifierMap Map;
1003 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
1004 DICompileUnit CU(CU_Nodes->getOperand(CUi));
1005 DIArray Retain = CU.getRetainedTypes();
1006 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
1007 if (!Retain.getElement(Ti).isCompositeType())
1009 DICompositeType Ty(Retain.getElement(Ti));
1010 if (MDString *TypeId = Ty.getIdentifier()) {
1011 // Definition has priority over declaration.
1012 // Try to insert (TypeId, Ty) to Map.
1013 std::pair<DITypeIdentifierMap::iterator, bool> P =
1014 Map.insert(std::make_pair(TypeId, Ty));
1015 // If TypeId already exists in Map and this is a definition, replace
1016 // whatever we had (declaration or definition) with the definition.
1017 if (!P.second && !Ty.isForwardDecl())
1018 P.first->second = Ty;
1025 //===----------------------------------------------------------------------===//
1026 // DebugInfoFinder implementations.
1027 //===----------------------------------------------------------------------===//
1029 void DebugInfoFinder::reset() {
1036 TypeIdentifierMap.clear();
1037 TypeMapInitialized = false;
1040 void DebugInfoFinder::InitializeTypeMap(const Module &M) {
1041 if (!TypeMapInitialized)
1042 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
1043 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
1044 TypeMapInitialized = true;
1048 void DebugInfoFinder::processModule(const Module &M) {
1049 InitializeTypeMap(M);
1050 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
1051 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
1052 DICompileUnit CU(CU_Nodes->getOperand(i));
1054 DIArray GVs = CU.getGlobalVariables();
1055 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
1056 DIGlobalVariable DIG(GVs.getElement(i));
1057 if (addGlobalVariable(DIG)) {
1058 processScope(DIG.getContext());
1059 processType(DIG.getType().resolve(TypeIdentifierMap));
1062 DIArray SPs = CU.getSubprograms();
1063 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
1064 processSubprogram(DISubprogram(SPs.getElement(i)));
1065 DIArray EnumTypes = CU.getEnumTypes();
1066 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
1067 processType(DIType(EnumTypes.getElement(i)));
1068 DIArray RetainedTypes = CU.getRetainedTypes();
1069 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
1070 processType(DIType(RetainedTypes.getElement(i)));
1071 DIArray Imports = CU.getImportedEntities();
1072 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
1073 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i));
1074 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
1075 if (Entity.isType())
1076 processType(DIType(Entity));
1077 else if (Entity.isSubprogram())
1078 processSubprogram(DISubprogram(Entity));
1079 else if (Entity.isNameSpace())
1080 processScope(DINameSpace(Entity).getContext());
1086 void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
1089 InitializeTypeMap(M);
1090 processScope(Loc.getScope());
1091 processLocation(M, Loc.getOrigLocation());
1094 void DebugInfoFinder::processType(DIType DT) {
1097 processScope(DT.getContext().resolve(TypeIdentifierMap));
1098 if (DT.isCompositeType()) {
1099 DICompositeType DCT(DT);
1100 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
1101 if (DT.isSubroutineType()) {
1102 DITypeArray DTA = DISubroutineType(DT).getTypeArray();
1103 for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i)
1104 processType(DTA.getElement(i).resolve(TypeIdentifierMap));
1107 DIArray DA = DCT.getElements();
1108 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1109 DIDescriptor D = DA.getElement(i);
1111 processType(DIType(D));
1112 else if (D.isSubprogram())
1113 processSubprogram(DISubprogram(D));
1115 } else if (DT.isDerivedType()) {
1116 DIDerivedType DDT(DT);
1117 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
1121 void DebugInfoFinder::processScope(DIScope Scope) {
1122 if (Scope.isType()) {
1127 if (Scope.isCompileUnit()) {
1128 addCompileUnit(DICompileUnit(Scope));
1131 if (Scope.isSubprogram()) {
1132 processSubprogram(DISubprogram(Scope));
1135 if (!addScope(Scope))
1137 if (Scope.isLexicalBlock()) {
1138 DILexicalBlock LB(Scope);
1139 processScope(LB.getContext());
1140 } else if (Scope.isLexicalBlockFile()) {
1141 DILexicalBlockFile LBF = DILexicalBlockFile(Scope);
1142 processScope(LBF.getScope());
1143 } else if (Scope.isNameSpace()) {
1144 DINameSpace NS(Scope);
1145 processScope(NS.getContext());
1149 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1150 if (!addSubprogram(SP))
1152 processScope(SP.getContext().resolve(TypeIdentifierMap));
1153 processType(SP.getType());
1154 DIArray TParams = SP.getTemplateParams();
1155 for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
1156 DIDescriptor Element = TParams.getElement(I);
1157 if (Element.isTemplateTypeParameter()) {
1158 DITemplateTypeParameter TType(Element);
1159 processType(TType.getType().resolve(TypeIdentifierMap));
1160 } else if (Element.isTemplateValueParameter()) {
1161 DITemplateValueParameter TVal(Element);
1162 processType(TVal.getType().resolve(TypeIdentifierMap));
1167 void DebugInfoFinder::processDeclare(const Module &M,
1168 const DbgDeclareInst *DDI) {
1169 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1172 InitializeTypeMap(M);
1175 if (!DV.isVariable())
1178 if (!NodesSeen.insert(DV).second)
1180 processScope(DIVariable(N).getContext());
1181 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
1184 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
1185 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
1188 InitializeTypeMap(M);
1191 if (!DV.isVariable())
1194 if (!NodesSeen.insert(DV).second)
1196 processScope(DIVariable(N).getContext());
1197 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
1200 bool DebugInfoFinder::addType(DIType DT) {
1204 if (!NodesSeen.insert(DT).second)
1211 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1214 if (!NodesSeen.insert(CU).second)
1221 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1225 if (!NodesSeen.insert(DIG).second)
1232 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1236 if (!NodesSeen.insert(SP).second)
1243 bool DebugInfoFinder::addScope(DIScope Scope) {
1246 // FIXME: Ocaml binding generates a scope with no content, we treat it
1248 if (Scope->getNumOperands() == 0)
1250 if (!NodesSeen.insert(Scope).second)
1252 Scopes.push_back(Scope);
1256 //===----------------------------------------------------------------------===//
1257 // DIDescriptor: dump routines for all descriptors.
1258 //===----------------------------------------------------------------------===//
1260 void DIDescriptor::dump() const {
1265 void DIDescriptor::print(raw_ostream &OS) const {
1269 if (const char *Tag = dwarf::TagString(getTag()))
1270 OS << "[ " << Tag << " ]";
1272 if (this->isSubrange()) {
1273 DISubrange(DbgNode).printInternal(OS);
1274 } else if (this->isCompileUnit()) {
1275 DICompileUnit(DbgNode).printInternal(OS);
1276 } else if (this->isFile()) {
1277 DIFile(DbgNode).printInternal(OS);
1278 } else if (this->isEnumerator()) {
1279 DIEnumerator(DbgNode).printInternal(OS);
1280 } else if (this->isBasicType()) {
1281 DIType(DbgNode).printInternal(OS);
1282 } else if (this->isDerivedType()) {
1283 DIDerivedType(DbgNode).printInternal(OS);
1284 } else if (this->isCompositeType()) {
1285 DICompositeType(DbgNode).printInternal(OS);
1286 } else if (this->isSubprogram()) {
1287 DISubprogram(DbgNode).printInternal(OS);
1288 } else if (this->isGlobalVariable()) {
1289 DIGlobalVariable(DbgNode).printInternal(OS);
1290 } else if (this->isVariable()) {
1291 DIVariable(DbgNode).printInternal(OS);
1292 } else if (this->isObjCProperty()) {
1293 DIObjCProperty(DbgNode).printInternal(OS);
1294 } else if (this->isNameSpace()) {
1295 DINameSpace(DbgNode).printInternal(OS);
1296 } else if (this->isScope()) {
1297 DIScope(DbgNode).printInternal(OS);
1298 } else if (this->isExpression()) {
1299 DIExpression(DbgNode).printInternal(OS);
1303 void DISubrange::printInternal(raw_ostream &OS) const {
1304 int64_t Count = getCount();
1306 OS << " [" << getLo() << ", " << Count - 1 << ']';
1308 OS << " [unbounded]";
1311 void DIScope::printInternal(raw_ostream &OS) const {
1312 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1315 void DICompileUnit::printInternal(raw_ostream &OS) const {
1316 DIScope::printInternal(OS);
1318 unsigned Lang = getLanguage();
1319 if (const char *LangStr = dwarf::LanguageString(Lang))
1322 (OS << "lang 0x").write_hex(Lang);
1326 void DIEnumerator::printInternal(raw_ostream &OS) const {
1327 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1330 void DIType::printInternal(raw_ostream &OS) const {
1334 StringRef Res = getName();
1336 OS << " [" << Res << "]";
1338 // TODO: Print context?
1340 OS << " [line " << getLineNumber() << ", size " << getSizeInBits()
1341 << ", align " << getAlignInBits() << ", offset " << getOffsetInBits();
1343 if (const char *Enc =
1344 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1345 OS << ", enc " << Enc;
1350 else if (isProtected())
1351 OS << " [protected]";
1352 else if (isPublic())
1356 OS << " [artificial]";
1358 if (isForwardDecl())
1360 else if (getTag() == dwarf::DW_TAG_structure_type ||
1361 getTag() == dwarf::DW_TAG_union_type ||
1362 getTag() == dwarf::DW_TAG_enumeration_type ||
1363 getTag() == dwarf::DW_TAG_class_type)
1367 if (isStaticMember())
1370 if (isLValueReference())
1371 OS << " [reference]";
1373 if (isRValueReference())
1374 OS << " [rvalue reference]";
1377 void DIDerivedType::printInternal(raw_ostream &OS) const {
1378 DIType::printInternal(OS);
1379 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1382 void DICompositeType::printInternal(raw_ostream &OS) const {
1383 DIType::printInternal(OS);
1384 DIArray A = getElements();
1385 OS << " [" << A.getNumElements() << " elements]";
1388 void DINameSpace::printInternal(raw_ostream &OS) const {
1389 StringRef Name = getName();
1391 OS << " [" << Name << ']';
1393 OS << " [line " << getLineNumber() << ']';
1396 void DISubprogram::printInternal(raw_ostream &OS) const {
1397 // TODO : Print context
1398 OS << " [line " << getLineNumber() << ']';
1400 if (isLocalToUnit())
1406 if (getScopeLineNumber() != getLineNumber())
1407 OS << " [scope " << getScopeLineNumber() << "]";
1411 else if (isProtected())
1412 OS << " [protected]";
1413 else if (isPublic())
1416 if (isLValueReference())
1417 OS << " [reference]";
1419 if (isRValueReference())
1420 OS << " [rvalue reference]";
1422 StringRef Res = getName();
1424 OS << " [" << Res << ']';
1427 void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1428 StringRef Res = getName();
1430 OS << " [" << Res << ']';
1432 OS << " [line " << getLineNumber() << ']';
1434 // TODO : Print context
1436 if (isLocalToUnit())
1443 void DIVariable::printInternal(raw_ostream &OS) const {
1444 StringRef Res = getName();
1446 OS << " [" << Res << ']';
1448 OS << " [line " << getLineNumber() << ']';
1451 void DIExpression::printInternal(raw_ostream &OS) const {
1452 for (auto Op : *this) {
1453 OS << " [" << OperationEncodingString(Op);
1456 OS << " " << Op.getArg(1);
1459 case DW_OP_bit_piece: {
1460 OS << " offset=" << Op.getArg(1) << ", size=" << Op.getArg(2);
1467 llvm_unreachable("unhandled operation");
1473 void DIObjCProperty::printInternal(raw_ostream &OS) const {
1474 StringRef Name = getObjCPropertyName();
1476 OS << " [" << Name << ']';
1478 OS << " [line " << getLineNumber() << ", properties " << getUnsignedField(6)
1482 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1483 const LLVMContext &Ctx) {
1484 if (!DL.isUnknown()) { // Print source line info.
1485 DIScope Scope(DL.getScope(Ctx));
1486 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
1487 // Omit the directory, because it's likely to be long and uninteresting.
1488 CommentOS << Scope.getFilename();
1489 CommentOS << ':' << DL.getLine();
1490 if (DL.getCol() != 0)
1491 CommentOS << ':' << DL.getCol();
1492 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1493 if (!InlinedAtDL.isUnknown()) {
1494 CommentOS << " @[ ";
1495 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1501 void DIVariable::printExtendedName(raw_ostream &OS) const {
1502 const LLVMContext &Ctx = DbgNode->getContext();
1503 StringRef Res = getName();
1505 OS << Res << "," << getLineNumber();
1506 if (MDNode *InlinedAt = getInlinedAt()) {
1507 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1508 if (!InlinedAtDL.isUnknown()) {
1510 printDebugLoc(InlinedAtDL, OS, Ctx);
1516 template <> DIRef<DIDescriptor>::DIRef(const Metadata *V) : Val(V) {
1517 assert(isDescriptorRef(V) &&
1518 "DIDescriptorRef should be a MDString or MDNode");
1520 template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) {
1521 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
1523 template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) {
1524 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
1528 DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const {
1529 return DIDescriptorRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
1532 DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
1533 return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
1535 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
1536 return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
1539 bool llvm::StripDebugInfo(Module &M) {
1540 bool Changed = false;
1542 // Remove all of the calls to the debugger intrinsics, and remove them from
1544 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
1545 while (!Declare->use_empty()) {
1546 CallInst *CI = cast<CallInst>(Declare->user_back());
1547 CI->eraseFromParent();
1549 Declare->eraseFromParent();
1553 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
1554 while (!DbgVal->use_empty()) {
1555 CallInst *CI = cast<CallInst>(DbgVal->user_back());
1556 CI->eraseFromParent();
1558 DbgVal->eraseFromParent();
1562 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
1563 NME = M.named_metadata_end(); NMI != NME;) {
1564 NamedMDNode *NMD = NMI;
1566 if (NMD->getName().startswith("llvm.dbg.")) {
1567 NMD->eraseFromParent();
1572 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
1573 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
1575 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
1577 if (!BI->getDebugLoc().isUnknown()) {
1579 BI->setDebugLoc(DebugLoc());
1586 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
1587 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
1588 M.getModuleFlag("Debug Info Version")))
1589 return Val->getZExtValue();
1593 llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
1594 llvm::makeSubprogramMap(const Module &M) {
1595 DenseMap<const Function *, DISubprogram> R;
1597 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
1601 for (MDNode *N : CU_Nodes->operands()) {
1602 DICompileUnit CUNode(N);
1603 DIArray SPs = CUNode.getSubprograms();
1604 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
1605 DISubprogram SP(SPs.getElement(i));
1606 if (Function *F = SP.getFunction())
1607 R.insert(std::make_pair(F, SP));