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 &VMContext, DIDescriptor D) {
381 assert(DbgNode && "Trying to replace an unverified type!");
383 // Since we use a TrackingVH for the node, its easy for clients to manufacture
384 // legitimate situations where they want to replaceAllUsesWith() on something
385 // which, due to uniquing, has merged with the source. We shield clients from
386 // this detail by allowing a value to be replaced with replaceAllUsesWith()
388 const MDNode *DN = D;
390 SmallVector<Metadata *, 10> Ops(DbgNode->op_begin(), DbgNode->op_end());
391 DN = MDNode::get(VMContext, Ops);
394 assert(DbgNode->isTemporary() && "Expected temporary node");
395 auto *Node = const_cast<MDNode *>(DbgNode);
396 Node->replaceAllUsesWith(const_cast<MDNode *>(DN));
397 MDNode::deleteTemporary(Node);
401 void DIDescriptor::replaceAllUsesWith(MDNode *D) {
402 assert(DbgNode && "Trying to replace an unverified type!");
403 assert(DbgNode != D && "This replacement should always happen");
404 assert(DbgNode->isTemporary() && "Expected temporary node");
405 auto *Node = const_cast<MDNode *>(DbgNode);
406 Node->replaceAllUsesWith(D);
407 MDNode::deleteTemporary(Node);
410 bool DICompileUnit::Verify() const {
411 if (!isCompileUnit())
414 // Don't bother verifying the compilation directory or producer string
415 // as those could be empty.
416 if (getFilename().empty())
419 return DbgNode->getNumOperands() == 7 && getNumHeaderFields() == 8;
422 bool DIObjCProperty::Verify() const {
423 if (!isObjCProperty())
426 // Don't worry about the rest of the strings for now.
427 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 6;
430 /// \brief Check if a field at position Elt of a MDNode is a MDNode.
431 static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) {
432 Metadata *Fld = getField(DbgNode, Elt);
433 return !Fld || isa<MDNode>(Fld);
436 /// \brief Check if a field at position Elt of a MDNode is a MDString.
437 static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) {
438 Metadata *Fld = getField(DbgNode, Elt);
439 return !Fld || isa<MDString>(Fld);
442 /// \brief Check if a value can be a reference to a type.
443 static bool isTypeRef(const Metadata *MD) {
446 if (auto *S = dyn_cast<MDString>(MD))
447 return !S->getString().empty();
448 if (auto *N = dyn_cast<MDNode>(MD))
449 return DIType(N).isType();
453 /// \brief Check if referenced field might be a type.
454 static bool fieldIsTypeRef(const MDNode *DbgNode, unsigned Elt) {
455 return isTypeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt)));
458 /// \brief Check if a value can be a ScopeRef.
459 static bool isScopeRef(const Metadata *MD) {
462 if (auto *S = dyn_cast<MDString>(MD))
463 return !S->getString().empty();
464 if (auto *N = dyn_cast<MDNode>(MD))
465 return DIScope(N).isScope();
469 /// \brief Check if a field at position Elt of a MDNode can be a ScopeRef.
470 static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) {
471 return isScopeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt)));
475 /// \brief Check if a value can be a DescriptorRef.
476 static bool isDescriptorRef(const Metadata *MD) {
479 if (auto *S = dyn_cast<MDString>(MD))
480 return !S->getString().empty();
481 return isa<MDNode>(MD);
485 bool DIType::Verify() const {
488 // Make sure Context @ field 2 is MDNode.
489 if (!fieldIsScopeRef(DbgNode, 2))
492 // FIXME: Sink this into the various subclass verifies.
493 uint16_t Tag = getTag();
494 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
495 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
496 Tag != dwarf::DW_TAG_ptr_to_member_type &&
497 Tag != dwarf::DW_TAG_reference_type &&
498 Tag != dwarf::DW_TAG_rvalue_reference_type &&
499 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_array_type &&
500 Tag != dwarf::DW_TAG_enumeration_type &&
501 Tag != dwarf::DW_TAG_subroutine_type &&
502 Tag != dwarf::DW_TAG_inheritance && Tag != dwarf::DW_TAG_friend &&
503 getFilename().empty())
506 // DIType is abstract, it should be a BasicType, a DerivedType or
509 return DIBasicType(DbgNode).Verify();
510 else if (isCompositeType())
511 return DICompositeType(DbgNode).Verify();
512 else if (isDerivedType())
513 return DIDerivedType(DbgNode).Verify();
518 bool DIBasicType::Verify() const {
519 return isBasicType() && DbgNode->getNumOperands() == 3 &&
520 getNumHeaderFields() == 8;
523 bool DIDerivedType::Verify() const {
524 // Make sure DerivedFrom @ field 3 is TypeRef.
525 if (!fieldIsTypeRef(DbgNode, 3))
527 if (getTag() == dwarf::DW_TAG_ptr_to_member_type)
528 // Make sure ClassType @ field 4 is a TypeRef.
529 if (!fieldIsTypeRef(DbgNode, 4))
532 return isDerivedType() && DbgNode->getNumOperands() >= 4 &&
533 DbgNode->getNumOperands() <= 8 && getNumHeaderFields() >= 7 &&
534 getNumHeaderFields() <= 8;
537 bool DICompositeType::Verify() const {
538 if (!isCompositeType())
541 // Make sure DerivedFrom @ field 3 and ContainingType @ field 5 are TypeRef.
542 if (!fieldIsTypeRef(DbgNode, 3))
544 if (!fieldIsTypeRef(DbgNode, 5))
547 // Make sure the type identifier at field 7 is MDString, it can be null.
548 if (!fieldIsMDString(DbgNode, 7))
551 // A subroutine type can't be both & and &&.
552 if (isLValueReference() && isRValueReference())
555 return DbgNode->getNumOperands() == 8 && getNumHeaderFields() == 8;
558 bool DISubprogram::Verify() const {
562 // Make sure context @ field 2 is a ScopeRef and type @ field 3 is a MDNode.
563 if (!fieldIsScopeRef(DbgNode, 2))
565 if (!fieldIsMDNode(DbgNode, 3))
567 // Containing type @ field 4.
568 if (!fieldIsTypeRef(DbgNode, 4))
571 // A subprogram can't be both & and &&.
572 if (isLValueReference() && isRValueReference())
575 // If a DISubprogram has an llvm::Function*, then scope chains from all
576 // instructions within the function should lead to this DISubprogram.
577 if (auto *F = getFunction()) {
578 for (auto &BB : *F) {
580 DebugLoc DL = I.getDebugLoc();
584 MDNode *Scope = nullptr;
585 MDNode *IA = nullptr;
586 // walk the inlined-at scopes
587 while ((IA = DL.getInlinedAt()))
588 DL = DebugLoc::getFromDILocation(IA);
589 DL.getScopeAndInlinedAt(Scope, IA);
593 while (!DIDescriptor(Scope).isSubprogram()) {
594 DILexicalBlockFile D(Scope);
595 Scope = D.isLexicalBlockFile()
597 : DebugLoc::getFromDILexicalBlock(Scope).getScope();
601 if (!DISubprogram(Scope).describes(F))
606 return DbgNode->getNumOperands() == 9 && getNumHeaderFields() == 12;
609 bool DIGlobalVariable::Verify() const {
610 if (!isGlobalVariable())
613 if (getDisplayName().empty())
615 // Make sure context @ field 1 is an MDNode.
616 if (!fieldIsMDNode(DbgNode, 1))
618 // Make sure that type @ field 3 is a DITypeRef.
619 if (!fieldIsTypeRef(DbgNode, 3))
621 // Make sure StaticDataMemberDeclaration @ field 5 is MDNode.
622 if (!fieldIsMDNode(DbgNode, 5))
625 return DbgNode->getNumOperands() == 6 && getNumHeaderFields() == 7;
628 bool DIVariable::Verify() const {
632 // Make sure context @ field 1 is an MDNode.
633 if (!fieldIsMDNode(DbgNode, 1))
635 // Make sure that type @ field 3 is a DITypeRef.
636 if (!fieldIsTypeRef(DbgNode, 3))
639 // Check the number of header fields, which is common between complex and
641 if (getNumHeaderFields() != 4)
644 // Variable without an inline location.
645 if (DbgNode->getNumOperands() == 4)
648 // Variable with an inline location.
649 return getInlinedAt() != nullptr && DbgNode->getNumOperands() == 5;
652 bool DIExpression::Verify() const {
653 // Empty DIExpressions may be represented as a nullptr.
657 if (!(isExpression() && DbgNode->getNumOperands() == 1))
660 for (auto Op : *this)
662 case DW_OP_bit_piece:
663 // Must be the last element of the expression.
664 return std::distance(Op.getBase(), DIHeaderFieldIterator()) == 3;
666 if (std::distance(Op.getBase(), DIHeaderFieldIterator()) < 2)
672 // Other operators are not yet supported by the backend.
678 bool DILocation::Verify() const {
679 return DbgNode && isa<MDLocation>(DbgNode);
682 bool DINameSpace::Verify() const {
685 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 3;
688 MDNode *DIFile::getFileNode() const { return getNodeField(DbgNode, 1); }
690 bool DIFile::Verify() const {
691 return isFile() && DbgNode->getNumOperands() == 2;
694 bool DIEnumerator::Verify() const {
695 return isEnumerator() && DbgNode->getNumOperands() == 1 &&
696 getNumHeaderFields() == 3;
699 bool DISubrange::Verify() const {
700 return isSubrange() && DbgNode->getNumOperands() == 1 &&
701 getNumHeaderFields() == 3;
704 bool DILexicalBlock::Verify() const {
705 return isLexicalBlock() && DbgNode->getNumOperands() == 3 &&
706 getNumHeaderFields() == 4;
709 bool DILexicalBlockFile::Verify() const {
710 return isLexicalBlockFile() && DbgNode->getNumOperands() == 3 &&
711 getNumHeaderFields() == 2;
714 bool DITemplateTypeParameter::Verify() const {
715 return isTemplateTypeParameter() && DbgNode->getNumOperands() == 4 &&
716 getNumHeaderFields() == 4;
719 bool DITemplateValueParameter::Verify() const {
720 return isTemplateValueParameter() && DbgNode->getNumOperands() == 5 &&
721 getNumHeaderFields() == 4;
724 bool DIImportedEntity::Verify() const {
725 return isImportedEntity() && DbgNode->getNumOperands() == 3 &&
726 getNumHeaderFields() == 3;
729 MDNode *DIDerivedType::getObjCProperty() const {
730 return getNodeField(DbgNode, 4);
733 MDString *DICompositeType::getIdentifier() const {
734 return cast_or_null<MDString>(getField(DbgNode, 7));
738 static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) {
739 for (unsigned i = 0; i != LHS->getNumOperands(); ++i) {
740 // Skip the 'empty' list (that's a single i32 0, rather than truly empty).
741 if (i == 0 && mdconst::hasa<ConstantInt>(LHS->getOperand(i)))
743 const MDNode *E = cast<MDNode>(LHS->getOperand(i));
745 for (unsigned j = 0; !found && j != RHS->getNumOperands(); ++j)
746 found = (E == cast<MDNode>(RHS->getOperand(j)));
747 assert(found && "Losing a member during member list replacement");
752 void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) {
753 TrackingMDNodeRef N(*this);
756 // Check that the new list of members contains all the old members as well.
757 if (const MDNode *El = cast_or_null<MDNode>(N->getOperand(4)))
758 VerifySubsetOf(El, Elements);
760 N->replaceOperandWith(4, Elements);
763 N->replaceOperandWith(6, TParams);
767 DIScopeRef DIScope::getRef() const {
768 if (!isCompositeType())
769 return DIScopeRef(*this);
770 DICompositeType DTy(DbgNode);
771 if (!DTy.getIdentifier())
772 return DIScopeRef(*this);
773 return DIScopeRef(DTy.getIdentifier());
776 void DICompositeType::setContainingType(DICompositeType ContainingType) {
777 TrackingMDNodeRef N(*this);
778 N->replaceOperandWith(5, ContainingType.getRef());
782 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
783 assert(CurFn && "Invalid function");
784 if (!getContext().isSubprogram())
786 // This variable is not inlined function argument if its scope
787 // does not describe current function.
788 return !DISubprogram(getContext()).describes(CurFn);
791 bool DISubprogram::describes(const Function *F) {
792 assert(F && "Invalid function");
793 if (F == getFunction())
795 StringRef Name = getLinkageName();
798 if (F->getName() == Name)
803 MDNode *DISubprogram::getVariablesNodes() const {
804 return getNodeField(DbgNode, 8);
807 DIArray DISubprogram::getVariables() const {
808 return DIArray(getNodeField(DbgNode, 8));
811 Metadata *DITemplateValueParameter::getValue() const {
812 return DbgNode->getOperand(3);
815 DIScopeRef DIScope::getContext() const {
818 return DIType(DbgNode).getContext();
821 return DIScopeRef(DISubprogram(DbgNode).getContext());
823 if (isLexicalBlock())
824 return DIScopeRef(DILexicalBlock(DbgNode).getContext());
826 if (isLexicalBlockFile())
827 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext());
830 return DIScopeRef(DINameSpace(DbgNode).getContext());
832 assert((isFile() || isCompileUnit()) && "Unhandled type of scope.");
833 return DIScopeRef(nullptr);
836 StringRef DIScope::getName() const {
838 return DIType(DbgNode).getName();
840 return DISubprogram(DbgNode).getName();
842 return DINameSpace(DbgNode).getName();
843 assert((isLexicalBlock() || isLexicalBlockFile() || isFile() ||
845 "Unhandled type of scope.");
849 StringRef DIScope::getFilename() const {
852 return ::getStringField(getNodeField(DbgNode, 1), 0);
855 StringRef DIScope::getDirectory() const {
858 return ::getStringField(getNodeField(DbgNode, 1), 1);
861 DIArray DICompileUnit::getEnumTypes() const {
862 if (!DbgNode || DbgNode->getNumOperands() < 7)
865 return DIArray(getNodeField(DbgNode, 2));
868 DIArray DICompileUnit::getRetainedTypes() const {
869 if (!DbgNode || DbgNode->getNumOperands() < 7)
872 return DIArray(getNodeField(DbgNode, 3));
875 DIArray DICompileUnit::getSubprograms() const {
876 if (!DbgNode || DbgNode->getNumOperands() < 7)
879 return DIArray(getNodeField(DbgNode, 4));
882 DIArray DICompileUnit::getGlobalVariables() const {
883 if (!DbgNode || DbgNode->getNumOperands() < 7)
886 return DIArray(getNodeField(DbgNode, 5));
889 DIArray DICompileUnit::getImportedEntities() const {
890 if (!DbgNode || DbgNode->getNumOperands() < 7)
893 return DIArray(getNodeField(DbgNode, 6));
896 void DICompileUnit::replaceSubprograms(DIArray Subprograms) {
897 assert(Verify() && "Expected compile unit");
898 if (Subprograms == getSubprograms())
901 const_cast<MDNode *>(DbgNode)->replaceOperandWith(4, Subprograms);
904 void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {
905 assert(Verify() && "Expected compile unit");
906 if (GlobalVariables == getGlobalVariables())
909 const_cast<MDNode *>(DbgNode)->replaceOperandWith(5, GlobalVariables);
912 DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
913 DILexicalBlockFile NewScope) {
915 assert(NewScope && "Expected valid scope");
917 const auto *Old = cast<MDLocation>(DbgNode);
918 return DILocation(MDLocation::get(Ctx, Old->getLine(), Old->getColumn(),
919 NewScope, Old->getInlinedAt()));
922 unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
923 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
924 return ++Ctx.pImpl->DiscriminatorTable[Key];
927 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
928 LLVMContext &VMContext) {
929 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
931 return cleanseInlinedVariable(DV, VMContext);
933 // Insert inlined scope.
934 SmallVector<Metadata *, 8> Elts(DV->op_begin(),
935 DV->op_begin() + DIVariableInlinedAtIndex);
936 Elts.push_back(InlinedScope);
938 DIVariable Inlined(MDNode::get(VMContext, Elts));
939 assert(Inlined.Verify() && "Expected to create a DIVariable");
943 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
944 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
945 if (!DIVariable(DV).getInlinedAt())
946 return DIVariable(DV);
948 // Remove inlined scope.
949 SmallVector<Metadata *, 8> Elts(DV->op_begin(),
950 DV->op_begin() + DIVariableInlinedAtIndex);
952 DIVariable Cleansed(MDNode::get(VMContext, Elts));
953 assert(Cleansed.Verify() && "Expected to create a DIVariable");
957 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
958 DIDescriptor D(Scope);
959 if (D.isSubprogram())
960 return DISubprogram(Scope);
962 if (D.isLexicalBlockFile())
963 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
965 if (D.isLexicalBlock())
966 return getDISubprogram(DILexicalBlock(Scope).getContext());
968 return DISubprogram();
971 DISubprogram llvm::getDISubprogram(const Function *F) {
972 // We look for the first instr that has a debug annotation leading back to F.
973 for (auto &BB : *F) {
974 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
975 return !Inst.getDebugLoc().isUnknown();
977 if (Inst == BB.end())
979 DebugLoc DLoc = Inst->getDebugLoc();
980 const MDNode *Scope = DLoc.getScopeNode();
981 DISubprogram Subprogram = getDISubprogram(Scope);
982 return Subprogram.describes(F) ? Subprogram : DISubprogram();
985 return DISubprogram();
988 DICompositeType llvm::getDICompositeType(DIType T) {
989 if (T.isCompositeType())
990 return DICompositeType(T);
992 if (T.isDerivedType()) {
993 // This function is currently used by dragonegg and dragonegg does
994 // not generate identifier for types, so using an empty map to resolve
995 // DerivedFrom should be fine.
996 DITypeIdentifierMap EmptyMap;
997 return getDICompositeType(
998 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap));
1001 return DICompositeType();
1005 llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
1006 DITypeIdentifierMap Map;
1007 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
1008 DICompileUnit CU(CU_Nodes->getOperand(CUi));
1009 DIArray Retain = CU.getRetainedTypes();
1010 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
1011 if (!Retain.getElement(Ti).isCompositeType())
1013 DICompositeType Ty(Retain.getElement(Ti));
1014 if (MDString *TypeId = Ty.getIdentifier()) {
1015 // Definition has priority over declaration.
1016 // Try to insert (TypeId, Ty) to Map.
1017 std::pair<DITypeIdentifierMap::iterator, bool> P =
1018 Map.insert(std::make_pair(TypeId, Ty));
1019 // If TypeId already exists in Map and this is a definition, replace
1020 // whatever we had (declaration or definition) with the definition.
1021 if (!P.second && !Ty.isForwardDecl())
1022 P.first->second = Ty;
1029 //===----------------------------------------------------------------------===//
1030 // DebugInfoFinder implementations.
1031 //===----------------------------------------------------------------------===//
1033 void DebugInfoFinder::reset() {
1040 TypeIdentifierMap.clear();
1041 TypeMapInitialized = false;
1044 void DebugInfoFinder::InitializeTypeMap(const Module &M) {
1045 if (!TypeMapInitialized)
1046 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
1047 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
1048 TypeMapInitialized = true;
1052 void DebugInfoFinder::processModule(const Module &M) {
1053 InitializeTypeMap(M);
1054 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
1055 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
1056 DICompileUnit CU(CU_Nodes->getOperand(i));
1058 DIArray GVs = CU.getGlobalVariables();
1059 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
1060 DIGlobalVariable DIG(GVs.getElement(i));
1061 if (addGlobalVariable(DIG)) {
1062 processScope(DIG.getContext());
1063 processType(DIG.getType().resolve(TypeIdentifierMap));
1066 DIArray SPs = CU.getSubprograms();
1067 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
1068 processSubprogram(DISubprogram(SPs.getElement(i)));
1069 DIArray EnumTypes = CU.getEnumTypes();
1070 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
1071 processType(DIType(EnumTypes.getElement(i)));
1072 DIArray RetainedTypes = CU.getRetainedTypes();
1073 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
1074 processType(DIType(RetainedTypes.getElement(i)));
1075 DIArray Imports = CU.getImportedEntities();
1076 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
1077 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i));
1078 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
1079 if (Entity.isType())
1080 processType(DIType(Entity));
1081 else if (Entity.isSubprogram())
1082 processSubprogram(DISubprogram(Entity));
1083 else if (Entity.isNameSpace())
1084 processScope(DINameSpace(Entity).getContext());
1090 void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
1093 InitializeTypeMap(M);
1094 processScope(Loc.getScope());
1095 processLocation(M, Loc.getOrigLocation());
1098 void DebugInfoFinder::processType(DIType DT) {
1101 processScope(DT.getContext().resolve(TypeIdentifierMap));
1102 if (DT.isCompositeType()) {
1103 DICompositeType DCT(DT);
1104 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
1105 if (DT.isSubroutineType()) {
1106 DITypeArray DTA = DISubroutineType(DT).getTypeArray();
1107 for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i)
1108 processType(DTA.getElement(i).resolve(TypeIdentifierMap));
1111 DIArray DA = DCT.getElements();
1112 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1113 DIDescriptor D = DA.getElement(i);
1115 processType(DIType(D));
1116 else if (D.isSubprogram())
1117 processSubprogram(DISubprogram(D));
1119 } else if (DT.isDerivedType()) {
1120 DIDerivedType DDT(DT);
1121 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
1125 void DebugInfoFinder::processScope(DIScope Scope) {
1126 if (Scope.isType()) {
1131 if (Scope.isCompileUnit()) {
1132 addCompileUnit(DICompileUnit(Scope));
1135 if (Scope.isSubprogram()) {
1136 processSubprogram(DISubprogram(Scope));
1139 if (!addScope(Scope))
1141 if (Scope.isLexicalBlock()) {
1142 DILexicalBlock LB(Scope);
1143 processScope(LB.getContext());
1144 } else if (Scope.isLexicalBlockFile()) {
1145 DILexicalBlockFile LBF = DILexicalBlockFile(Scope);
1146 processScope(LBF.getScope());
1147 } else if (Scope.isNameSpace()) {
1148 DINameSpace NS(Scope);
1149 processScope(NS.getContext());
1153 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1154 if (!addSubprogram(SP))
1156 processScope(SP.getContext().resolve(TypeIdentifierMap));
1157 processType(SP.getType());
1158 DIArray TParams = SP.getTemplateParams();
1159 for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
1160 DIDescriptor Element = TParams.getElement(I);
1161 if (Element.isTemplateTypeParameter()) {
1162 DITemplateTypeParameter TType(Element);
1163 processType(TType.getType().resolve(TypeIdentifierMap));
1164 } else if (Element.isTemplateValueParameter()) {
1165 DITemplateValueParameter TVal(Element);
1166 processType(TVal.getType().resolve(TypeIdentifierMap));
1171 void DebugInfoFinder::processDeclare(const Module &M,
1172 const DbgDeclareInst *DDI) {
1173 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1176 InitializeTypeMap(M);
1179 if (!DV.isVariable())
1182 if (!NodesSeen.insert(DV).second)
1184 processScope(DIVariable(N).getContext());
1185 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
1188 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
1189 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
1192 InitializeTypeMap(M);
1195 if (!DV.isVariable())
1198 if (!NodesSeen.insert(DV).second)
1200 processScope(DIVariable(N).getContext());
1201 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
1204 bool DebugInfoFinder::addType(DIType DT) {
1208 if (!NodesSeen.insert(DT).second)
1215 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1218 if (!NodesSeen.insert(CU).second)
1225 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1229 if (!NodesSeen.insert(DIG).second)
1236 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1240 if (!NodesSeen.insert(SP).second)
1247 bool DebugInfoFinder::addScope(DIScope Scope) {
1250 // FIXME: Ocaml binding generates a scope with no content, we treat it
1252 if (Scope->getNumOperands() == 0)
1254 if (!NodesSeen.insert(Scope).second)
1256 Scopes.push_back(Scope);
1260 //===----------------------------------------------------------------------===//
1261 // DIDescriptor: dump routines for all descriptors.
1262 //===----------------------------------------------------------------------===//
1264 void DIDescriptor::dump() const {
1269 void DIDescriptor::print(raw_ostream &OS) const {
1273 if (const char *Tag = dwarf::TagString(getTag()))
1274 OS << "[ " << Tag << " ]";
1276 if (this->isSubrange()) {
1277 DISubrange(DbgNode).printInternal(OS);
1278 } else if (this->isCompileUnit()) {
1279 DICompileUnit(DbgNode).printInternal(OS);
1280 } else if (this->isFile()) {
1281 DIFile(DbgNode).printInternal(OS);
1282 } else if (this->isEnumerator()) {
1283 DIEnumerator(DbgNode).printInternal(OS);
1284 } else if (this->isBasicType()) {
1285 DIType(DbgNode).printInternal(OS);
1286 } else if (this->isDerivedType()) {
1287 DIDerivedType(DbgNode).printInternal(OS);
1288 } else if (this->isCompositeType()) {
1289 DICompositeType(DbgNode).printInternal(OS);
1290 } else if (this->isSubprogram()) {
1291 DISubprogram(DbgNode).printInternal(OS);
1292 } else if (this->isGlobalVariable()) {
1293 DIGlobalVariable(DbgNode).printInternal(OS);
1294 } else if (this->isVariable()) {
1295 DIVariable(DbgNode).printInternal(OS);
1296 } else if (this->isObjCProperty()) {
1297 DIObjCProperty(DbgNode).printInternal(OS);
1298 } else if (this->isNameSpace()) {
1299 DINameSpace(DbgNode).printInternal(OS);
1300 } else if (this->isScope()) {
1301 DIScope(DbgNode).printInternal(OS);
1302 } else if (this->isExpression()) {
1303 DIExpression(DbgNode).printInternal(OS);
1307 void DISubrange::printInternal(raw_ostream &OS) const {
1308 int64_t Count = getCount();
1310 OS << " [" << getLo() << ", " << Count - 1 << ']';
1312 OS << " [unbounded]";
1315 void DIScope::printInternal(raw_ostream &OS) const {
1316 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1319 void DICompileUnit::printInternal(raw_ostream &OS) const {
1320 DIScope::printInternal(OS);
1322 unsigned Lang = getLanguage();
1323 if (const char *LangStr = dwarf::LanguageString(Lang))
1326 (OS << "lang 0x").write_hex(Lang);
1330 void DIEnumerator::printInternal(raw_ostream &OS) const {
1331 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1334 void DIType::printInternal(raw_ostream &OS) const {
1338 StringRef Res = getName();
1340 OS << " [" << Res << "]";
1342 // TODO: Print context?
1344 OS << " [line " << getLineNumber() << ", size " << getSizeInBits()
1345 << ", align " << getAlignInBits() << ", offset " << getOffsetInBits();
1347 if (const char *Enc =
1348 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1349 OS << ", enc " << Enc;
1354 else if (isProtected())
1355 OS << " [protected]";
1356 else if (isPublic())
1360 OS << " [artificial]";
1362 if (isForwardDecl())
1364 else if (getTag() == dwarf::DW_TAG_structure_type ||
1365 getTag() == dwarf::DW_TAG_union_type ||
1366 getTag() == dwarf::DW_TAG_enumeration_type ||
1367 getTag() == dwarf::DW_TAG_class_type)
1371 if (isStaticMember())
1374 if (isLValueReference())
1375 OS << " [reference]";
1377 if (isRValueReference())
1378 OS << " [rvalue reference]";
1381 void DIDerivedType::printInternal(raw_ostream &OS) const {
1382 DIType::printInternal(OS);
1383 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1386 void DICompositeType::printInternal(raw_ostream &OS) const {
1387 DIType::printInternal(OS);
1388 DIArray A = getElements();
1389 OS << " [" << A.getNumElements() << " elements]";
1392 void DINameSpace::printInternal(raw_ostream &OS) const {
1393 StringRef Name = getName();
1395 OS << " [" << Name << ']';
1397 OS << " [line " << getLineNumber() << ']';
1400 void DISubprogram::printInternal(raw_ostream &OS) const {
1401 // TODO : Print context
1402 OS << " [line " << getLineNumber() << ']';
1404 if (isLocalToUnit())
1410 if (getScopeLineNumber() != getLineNumber())
1411 OS << " [scope " << getScopeLineNumber() << "]";
1415 else if (isProtected())
1416 OS << " [protected]";
1417 else if (isPublic())
1420 if (isLValueReference())
1421 OS << " [reference]";
1423 if (isRValueReference())
1424 OS << " [rvalue reference]";
1426 StringRef Res = getName();
1428 OS << " [" << Res << ']';
1431 void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1432 StringRef Res = getName();
1434 OS << " [" << Res << ']';
1436 OS << " [line " << getLineNumber() << ']';
1438 // TODO : Print context
1440 if (isLocalToUnit())
1447 void DIVariable::printInternal(raw_ostream &OS) const {
1448 StringRef Res = getName();
1450 OS << " [" << Res << ']';
1452 OS << " [line " << getLineNumber() << ']';
1455 void DIExpression::printInternal(raw_ostream &OS) const {
1456 for (auto Op : *this) {
1457 OS << " [" << OperationEncodingString(Op);
1460 OS << " " << Op.getArg(1);
1463 case DW_OP_bit_piece: {
1464 OS << " offset=" << Op.getArg(1) << ", size=" << Op.getArg(2);
1471 llvm_unreachable("unhandled operation");
1477 void DIObjCProperty::printInternal(raw_ostream &OS) const {
1478 StringRef Name = getObjCPropertyName();
1480 OS << " [" << Name << ']';
1482 OS << " [line " << getLineNumber() << ", properties " << getUnsignedField(6)
1486 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1487 const LLVMContext &Ctx) {
1488 if (!DL.isUnknown()) { // Print source line info.
1489 DIScope Scope(DL.getScope(Ctx));
1490 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
1491 // Omit the directory, because it's likely to be long and uninteresting.
1492 CommentOS << Scope.getFilename();
1493 CommentOS << ':' << DL.getLine();
1494 if (DL.getCol() != 0)
1495 CommentOS << ':' << DL.getCol();
1496 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1497 if (!InlinedAtDL.isUnknown()) {
1498 CommentOS << " @[ ";
1499 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1505 void DIVariable::printExtendedName(raw_ostream &OS) const {
1506 const LLVMContext &Ctx = DbgNode->getContext();
1507 StringRef Res = getName();
1509 OS << Res << "," << getLineNumber();
1510 if (MDNode *InlinedAt = getInlinedAt()) {
1511 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1512 if (!InlinedAtDL.isUnknown()) {
1514 printDebugLoc(InlinedAtDL, OS, Ctx);
1520 template <> DIRef<DIDescriptor>::DIRef(const Metadata *V) : Val(V) {
1521 assert(isDescriptorRef(V) &&
1522 "DIDescriptorRef should be a MDString or MDNode");
1524 template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) {
1525 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
1527 template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) {
1528 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
1532 DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const {
1533 return DIDescriptorRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
1536 DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
1537 return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
1539 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
1540 return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
1543 bool llvm::StripDebugInfo(Module &M) {
1544 bool Changed = false;
1546 // Remove all of the calls to the debugger intrinsics, and remove them from
1548 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
1549 while (!Declare->use_empty()) {
1550 CallInst *CI = cast<CallInst>(Declare->user_back());
1551 CI->eraseFromParent();
1553 Declare->eraseFromParent();
1557 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
1558 while (!DbgVal->use_empty()) {
1559 CallInst *CI = cast<CallInst>(DbgVal->user_back());
1560 CI->eraseFromParent();
1562 DbgVal->eraseFromParent();
1566 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
1567 NME = M.named_metadata_end(); NMI != NME;) {
1568 NamedMDNode *NMD = NMI;
1570 if (NMD->getName().startswith("llvm.dbg.")) {
1571 NMD->eraseFromParent();
1576 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
1577 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
1579 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
1581 if (!BI->getDebugLoc().isUnknown()) {
1583 BI->setDebugLoc(DebugLoc());
1590 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
1591 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
1592 M.getModuleFlag("Debug Info Version")))
1593 return Val->getZExtValue();
1597 llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
1598 llvm::makeSubprogramMap(const Module &M) {
1599 DenseMap<const Function *, DISubprogram> R;
1601 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
1605 for (MDNode *N : CU_Nodes->operands()) {
1606 DICompileUnit CUNode(N);
1607 DIArray SPs = CUNode.getSubprograms();
1608 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
1609 DISubprogram SP(SPs.getElement(i));
1610 if (Function *F = SP.getFunction())
1611 R.insert(std::make_pair(F, SP));