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/Analysis/ValueTracking.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/DIBuilder.h"
23 #include "llvm/IR/DerivedTypes.h"
24 #include "llvm/IR/Instructions.h"
25 #include "llvm/IR/IntrinsicInst.h"
26 #include "llvm/IR/Intrinsics.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/IR/ValueHandle.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/Dwarf.h"
31 #include "llvm/Support/raw_ostream.h"
33 using namespace llvm::dwarf;
35 //===----------------------------------------------------------------------===//
37 //===----------------------------------------------------------------------===//
39 bool DIDescriptor::Verify() const {
41 (DIDerivedType(DbgNode).Verify() ||
42 DICompositeType(DbgNode).Verify() || DIBasicType(DbgNode).Verify() ||
43 DIVariable(DbgNode).Verify() || DISubprogram(DbgNode).Verify() ||
44 DIGlobalVariable(DbgNode).Verify() || DIFile(DbgNode).Verify() ||
45 DICompileUnit(DbgNode).Verify() || DINameSpace(DbgNode).Verify() ||
46 DILexicalBlock(DbgNode).Verify() ||
47 DILexicalBlockFile(DbgNode).Verify() ||
48 DISubrange(DbgNode).Verify() || DIEnumerator(DbgNode).Verify() ||
49 DIObjCProperty(DbgNode).Verify() ||
50 DITemplateTypeParameter(DbgNode).Verify() ||
51 DITemplateValueParameter(DbgNode).Verify() ||
52 DIImportedEntity(DbgNode).Verify() || DIExpression(DbgNode).Verify());
55 static Metadata *getField(const MDNode *DbgNode, unsigned Elt) {
56 if (!DbgNode || Elt >= DbgNode->getNumOperands())
58 return DbgNode->getOperand(Elt);
61 static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) {
62 return dyn_cast_or_null<MDNode>(getField(DbgNode, Elt));
65 static StringRef getStringField(const MDNode *DbgNode, unsigned Elt) {
66 if (MDString *MDS = dyn_cast_or_null<MDString>(getField(DbgNode, Elt)))
67 return MDS->getString();
71 StringRef DIDescriptor::getStringField(unsigned Elt) const {
72 return ::getStringField(DbgNode, Elt);
75 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
76 if (auto *C = getConstantField(Elt))
77 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
78 return CI->getZExtValue();
83 int64_t DIDescriptor::getInt64Field(unsigned Elt) const {
84 if (auto *C = getConstantField(Elt))
85 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
86 return CI->getZExtValue();
91 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
92 MDNode *Field = getNodeField(DbgNode, Elt);
93 return DIDescriptor(Field);
96 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
97 return dyn_cast_or_null<GlobalVariable>(getConstantField(Elt));
100 Constant *DIDescriptor::getConstantField(unsigned Elt) const {
104 if (Elt < DbgNode->getNumOperands())
106 dyn_cast_or_null<ConstantAsMetadata>(DbgNode->getOperand(Elt)))
107 return C->getValue();
111 Function *DIDescriptor::getFunctionField(unsigned Elt) const {
112 return dyn_cast_or_null<Function>(getConstantField(Elt));
115 void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) {
119 if (Elt < DbgNode->getNumOperands()) {
120 MDNode *Node = const_cast<MDNode *>(DbgNode);
121 Node->replaceOperandWith(Elt, F ? ConstantAsMetadata::get(F) : nullptr);
125 static unsigned DIVariableInlinedAtIndex = 4;
126 MDNode *DIVariable::getInlinedAt() const {
127 return getNodeField(DbgNode, DIVariableInlinedAtIndex);
130 /// \brief Return the size reported by the variable's type.
131 unsigned DIVariable::getSizeInBits(const DITypeIdentifierMap &Map) {
132 DIType Ty = getType().resolve(Map);
133 // Follow derived types until we reach a type that
134 // reports back a size.
135 while (Ty.isDerivedType() && !Ty.getSizeInBits()) {
136 DIDerivedType DT(&*Ty);
137 Ty = DT.getTypeDerivedFrom().resolve(Map);
139 assert(Ty.getSizeInBits() && "type with size 0");
140 return Ty.getSizeInBits();
143 uint64_t DIExpression::getElement(unsigned Idx) const {
144 unsigned I = Idx + 1;
145 assert(I < getNumHeaderFields() &&
146 "non-existing complex address element requested");
147 return getHeaderFieldAs<int64_t>(I);
150 bool DIExpression::isVariablePiece() const {
151 unsigned N = getNumElements();
152 return N >=3 && getElement(N-3) == dwarf::DW_OP_piece;
155 uint64_t DIExpression::getPieceOffset() const {
156 assert(isVariablePiece() && "not a piece");
157 return getElement(getNumElements()-2);
160 uint64_t DIExpression::getPieceSize() const {
161 assert(isVariablePiece() && "not a piece");
162 return getElement(getNumElements()-1);
165 DIExpressionIterator DIExpression::begin() const {
166 return DIExpressionIterator(*this);
169 DIExpressionIterator DIExpression::end() const {
170 return DIExpressionIterator();
173 //===----------------------------------------------------------------------===//
175 //===----------------------------------------------------------------------===//
177 bool DIDescriptor::isSubroutineType() const {
178 return DbgNode && getTag() == dwarf::DW_TAG_subroutine_type;
181 bool DIDescriptor::isBasicType() const {
185 case dwarf::DW_TAG_base_type:
186 case dwarf::DW_TAG_unspecified_type:
193 bool DIDescriptor::isDerivedType() const {
197 case dwarf::DW_TAG_typedef:
198 case dwarf::DW_TAG_pointer_type:
199 case dwarf::DW_TAG_ptr_to_member_type:
200 case dwarf::DW_TAG_reference_type:
201 case dwarf::DW_TAG_rvalue_reference_type:
202 case dwarf::DW_TAG_const_type:
203 case dwarf::DW_TAG_volatile_type:
204 case dwarf::DW_TAG_restrict_type:
205 case dwarf::DW_TAG_member:
206 case dwarf::DW_TAG_inheritance:
207 case dwarf::DW_TAG_friend:
210 // CompositeTypes are currently modelled as DerivedTypes.
211 return isCompositeType();
215 bool DIDescriptor::isCompositeType() const {
219 case dwarf::DW_TAG_array_type:
220 case dwarf::DW_TAG_structure_type:
221 case dwarf::DW_TAG_union_type:
222 case dwarf::DW_TAG_enumeration_type:
223 case dwarf::DW_TAG_subroutine_type:
224 case dwarf::DW_TAG_class_type:
231 bool DIDescriptor::isVariable() const {
235 case dwarf::DW_TAG_auto_variable:
236 case dwarf::DW_TAG_arg_variable:
243 bool DIDescriptor::isType() const {
244 return isBasicType() || isCompositeType() || isDerivedType();
247 bool DIDescriptor::isSubprogram() const {
248 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
251 bool DIDescriptor::isGlobalVariable() const {
252 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
253 getTag() == dwarf::DW_TAG_constant);
256 bool DIDescriptor::isScope() const {
260 case dwarf::DW_TAG_compile_unit:
261 case dwarf::DW_TAG_lexical_block:
262 case dwarf::DW_TAG_subprogram:
263 case dwarf::DW_TAG_namespace:
264 case dwarf::DW_TAG_file_type:
272 bool DIDescriptor::isTemplateTypeParameter() const {
273 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
276 bool DIDescriptor::isTemplateValueParameter() const {
277 return DbgNode && (getTag() == dwarf::DW_TAG_template_value_parameter ||
278 getTag() == dwarf::DW_TAG_GNU_template_template_param ||
279 getTag() == dwarf::DW_TAG_GNU_template_parameter_pack);
282 bool DIDescriptor::isCompileUnit() const {
283 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
286 bool DIDescriptor::isFile() const {
287 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
290 bool DIDescriptor::isNameSpace() const {
291 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
294 bool DIDescriptor::isLexicalBlockFile() const {
295 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
296 DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 2;
299 bool DIDescriptor::isLexicalBlock() const {
300 // FIXME: There are always exactly 4 header fields in DILexicalBlock, but
301 // something relies on this returning true for DILexicalBlockFile.
302 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
303 DbgNode->getNumOperands() == 3 &&
304 (getNumHeaderFields() == 2 || getNumHeaderFields() == 4);
307 bool DIDescriptor::isSubrange() const {
308 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
311 bool DIDescriptor::isEnumerator() const {
312 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
315 bool DIDescriptor::isObjCProperty() const {
316 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
319 bool DIDescriptor::isImportedEntity() const {
320 return DbgNode && (getTag() == dwarf::DW_TAG_imported_module ||
321 getTag() == dwarf::DW_TAG_imported_declaration);
324 bool DIDescriptor::isExpression() const {
325 return DbgNode && (getTag() == dwarf::DW_TAG_expression);
328 //===----------------------------------------------------------------------===//
329 // Simple Descriptor Constructors and other Methods
330 //===----------------------------------------------------------------------===//
332 void DIDescriptor::replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D) {
334 assert(DbgNode && "Trying to replace an unverified type!");
336 // Since we use a TrackingVH for the node, its easy for clients to manufacture
337 // legitimate situations where they want to replaceAllUsesWith() on something
338 // which, due to uniquing, has merged with the source. We shield clients from
339 // this detail by allowing a value to be replaced with replaceAllUsesWith()
341 const MDNode *DN = D;
343 SmallVector<Metadata *, 10> Ops(DbgNode->getNumOperands());
344 for (size_t i = 0; i != Ops.size(); ++i)
345 Ops[i] = DbgNode->getOperand(i);
346 DN = MDNode::get(VMContext, Ops);
349 assert(DbgNode->isTemporary() && "Expected temporary node");
350 auto *Node = const_cast<MDNode *>(DbgNode);
351 Node->replaceAllUsesWith(const_cast<MDNode *>(DN));
352 MDNode::deleteTemporary(Node);
356 void DIDescriptor::replaceAllUsesWith(MDNode *D) {
357 assert(DbgNode && "Trying to replace an unverified type!");
358 assert(DbgNode != D && "This replacement should always happen");
359 assert(DbgNode->isTemporary() && "Expected temporary node");
360 auto *Node = const_cast<MDNode *>(DbgNode);
361 Node->replaceAllUsesWith(D);
362 MDNode::deleteTemporary(Node);
365 bool DICompileUnit::Verify() const {
366 if (!isCompileUnit())
369 // Don't bother verifying the compilation directory or producer string
370 // as those could be empty.
371 if (getFilename().empty())
374 return DbgNode->getNumOperands() == 7 && getNumHeaderFields() == 8;
377 bool DIObjCProperty::Verify() const {
378 if (!isObjCProperty())
381 // Don't worry about the rest of the strings for now.
382 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 6;
385 /// \brief Check if a field at position Elt of a MDNode is a MDNode.
387 /// We currently allow an empty string and an integer.
388 /// But we don't allow a non-empty string in a MDNode field.
389 static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) {
390 // FIXME: This function should return true, if the field is null or the field
391 // is indeed a MDNode: return !Fld || isa<MDNode>(Fld).
392 Metadata *Fld = getField(DbgNode, Elt);
393 if (Fld && isa<MDString>(Fld) && !cast<MDString>(Fld)->getString().empty())
398 /// \brief Check if a field at position Elt of a MDNode is a MDString.
399 static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) {
400 Metadata *Fld = getField(DbgNode, Elt);
401 return !Fld || isa<MDString>(Fld);
404 /// \brief Check if a value can be a reference to a type.
405 static bool isTypeRef(const Metadata *MD) {
408 if (auto *S = dyn_cast<MDString>(MD))
409 return !S->getString().empty();
410 if (auto *N = dyn_cast<MDNode>(MD))
411 return DIType(N).isType();
415 /// \brief Check if referenced field might be a type.
416 static bool fieldIsTypeRef(const MDNode *DbgNode, unsigned Elt) {
417 return isTypeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt)));
420 /// \brief Check if a value can be a ScopeRef.
421 static bool isScopeRef(const Metadata *MD) {
424 if (auto *S = dyn_cast<MDString>(MD))
425 return !S->getString().empty();
426 return isa<MDNode>(MD);
429 /// \brief Check if a field at position Elt of a MDNode can be a ScopeRef.
430 static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) {
431 return isScopeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt)));
434 bool DIType::Verify() const {
437 // Make sure Context @ field 2 is MDNode.
438 if (!fieldIsScopeRef(DbgNode, 2))
441 // FIXME: Sink this into the various subclass verifies.
442 uint16_t Tag = getTag();
443 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
444 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
445 Tag != dwarf::DW_TAG_ptr_to_member_type &&
446 Tag != dwarf::DW_TAG_reference_type &&
447 Tag != dwarf::DW_TAG_rvalue_reference_type &&
448 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_array_type &&
449 Tag != dwarf::DW_TAG_enumeration_type &&
450 Tag != dwarf::DW_TAG_subroutine_type &&
451 Tag != dwarf::DW_TAG_inheritance && Tag != dwarf::DW_TAG_friend &&
452 getFilename().empty())
455 // DIType is abstract, it should be a BasicType, a DerivedType or
458 return DIBasicType(DbgNode).Verify();
459 else if (isCompositeType())
460 return DICompositeType(DbgNode).Verify();
461 else if (isDerivedType())
462 return DIDerivedType(DbgNode).Verify();
467 bool DIBasicType::Verify() const {
468 return isBasicType() && DbgNode->getNumOperands() == 3 &&
469 getNumHeaderFields() == 8;
472 bool DIDerivedType::Verify() const {
473 // Make sure DerivedFrom @ field 3 is TypeRef.
474 if (!fieldIsTypeRef(DbgNode, 3))
476 if (getTag() == dwarf::DW_TAG_ptr_to_member_type)
477 // Make sure ClassType @ field 4 is a TypeRef.
478 if (!fieldIsTypeRef(DbgNode, 4))
481 return isDerivedType() && DbgNode->getNumOperands() >= 4 &&
482 DbgNode->getNumOperands() <= 8 && getNumHeaderFields() >= 7 &&
483 getNumHeaderFields() <= 8;
486 bool DICompositeType::Verify() const {
487 if (!isCompositeType())
490 // Make sure DerivedFrom @ field 3 and ContainingType @ field 5 are TypeRef.
491 if (!fieldIsTypeRef(DbgNode, 3))
493 if (!fieldIsTypeRef(DbgNode, 5))
496 // Make sure the type identifier at field 7 is MDString, it can be null.
497 if (!fieldIsMDString(DbgNode, 7))
500 // A subroutine type can't be both & and &&.
501 if (isLValueReference() && isRValueReference())
504 return DbgNode->getNumOperands() == 8 && getNumHeaderFields() == 8;
507 bool DISubprogram::Verify() const {
511 // Make sure context @ field 2 is a ScopeRef and type @ field 3 is a MDNode.
512 if (!fieldIsScopeRef(DbgNode, 2))
514 if (!fieldIsMDNode(DbgNode, 3))
516 // Containing type @ field 4.
517 if (!fieldIsTypeRef(DbgNode, 4))
520 // A subprogram can't be both & and &&.
521 if (isLValueReference() && isRValueReference())
524 // If a DISubprogram has an llvm::Function*, then scope chains from all
525 // instructions within the function should lead to this DISubprogram.
526 if (auto *F = getFunction()) {
527 for (auto &BB : *F) {
529 DebugLoc DL = I.getDebugLoc();
533 MDNode *Scope = nullptr;
534 MDNode *IA = nullptr;
535 // walk the inlined-at scopes
536 while ((IA = DL.getInlinedAt()))
537 DL = DebugLoc::getFromDILocation(IA);
538 DL.getScopeAndInlinedAt(Scope, IA);
542 while (!DIDescriptor(Scope).isSubprogram()) {
543 DILexicalBlockFile D(Scope);
544 Scope = D.isLexicalBlockFile()
546 : DebugLoc::getFromDILexicalBlock(Scope).getScope();
550 if (!DISubprogram(Scope).describes(F))
555 return DbgNode->getNumOperands() == 9 && getNumHeaderFields() == 12;
558 bool DIGlobalVariable::Verify() const {
559 if (!isGlobalVariable())
562 if (getDisplayName().empty())
564 // Make sure context @ field 1 is an MDNode.
565 if (!fieldIsMDNode(DbgNode, 1))
567 // Make sure that type @ field 3 is a DITypeRef.
568 if (!fieldIsTypeRef(DbgNode, 3))
570 // Make sure StaticDataMemberDeclaration @ field 5 is MDNode.
571 if (!fieldIsMDNode(DbgNode, 5))
574 return DbgNode->getNumOperands() == 6 && getNumHeaderFields() == 7;
577 bool DIVariable::Verify() const {
581 // Make sure context @ field 1 is an MDNode.
582 if (!fieldIsMDNode(DbgNode, 1))
584 // Make sure that type @ field 3 is a DITypeRef.
585 if (!fieldIsTypeRef(DbgNode, 3))
588 // Check the number of header fields, which is common between complex and
590 if (getNumHeaderFields() != 4)
593 // Variable without an inline location.
594 if (DbgNode->getNumOperands() == 4)
597 // Variable with an inline location.
598 return getInlinedAt() != nullptr && DbgNode->getNumOperands() == 5;
601 bool DIExpression::Verify() const {
602 // Empty DIExpressions may be represented as a nullptr.
606 if (!(isExpression() && DbgNode->getNumOperands() == 1))
609 for (auto E = end(), I = begin(); I != E; ++I)
612 // Must be the last element of the expression.
613 return std::distance(I.getBase(), DIHeaderFieldIterator()) == 3;
615 if (std::distance(I.getBase(), DIHeaderFieldIterator()) < 2)
621 // Other operators are not yet supported by the backend.
627 bool DILocation::Verify() const {
628 return DbgNode && isa<MDLocation>(DbgNode);
631 bool DINameSpace::Verify() const {
634 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 3;
637 MDNode *DIFile::getFileNode() const { return getNodeField(DbgNode, 1); }
639 bool DIFile::Verify() const {
640 return isFile() && DbgNode->getNumOperands() == 2;
643 bool DIEnumerator::Verify() const {
644 return isEnumerator() && DbgNode->getNumOperands() == 1 &&
645 getNumHeaderFields() == 3;
648 bool DISubrange::Verify() const {
649 return isSubrange() && DbgNode->getNumOperands() == 1 &&
650 getNumHeaderFields() == 3;
653 bool DILexicalBlock::Verify() const {
654 return isLexicalBlock() && DbgNode->getNumOperands() == 3 &&
655 getNumHeaderFields() == 4;
658 bool DILexicalBlockFile::Verify() const {
659 return isLexicalBlockFile() && DbgNode->getNumOperands() == 3 &&
660 getNumHeaderFields() == 2;
663 bool DITemplateTypeParameter::Verify() const {
664 return isTemplateTypeParameter() && DbgNode->getNumOperands() == 4 &&
665 getNumHeaderFields() == 4;
668 bool DITemplateValueParameter::Verify() const {
669 return isTemplateValueParameter() && DbgNode->getNumOperands() == 5 &&
670 getNumHeaderFields() == 4;
673 bool DIImportedEntity::Verify() const {
674 return isImportedEntity() && DbgNode->getNumOperands() == 3 &&
675 getNumHeaderFields() == 3;
678 MDNode *DIDerivedType::getObjCProperty() const {
679 return getNodeField(DbgNode, 4);
682 MDString *DICompositeType::getIdentifier() const {
683 return cast_or_null<MDString>(getField(DbgNode, 7));
687 static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) {
688 for (unsigned i = 0; i != LHS->getNumOperands(); ++i) {
689 // Skip the 'empty' list (that's a single i32 0, rather than truly empty).
690 if (i == 0 && mdconst::hasa<ConstantInt>(LHS->getOperand(i)))
692 const MDNode *E = cast<MDNode>(LHS->getOperand(i));
694 for (unsigned j = 0; !found && j != RHS->getNumOperands(); ++j)
695 found = (E == cast<MDNode>(RHS->getOperand(j)));
696 assert(found && "Losing a member during member list replacement");
701 void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) {
702 TrackingMDNodeRef N(*this);
705 // Check that the new list of members contains all the old members as well.
706 if (const MDNode *El = cast_or_null<MDNode>(N->getOperand(4)))
707 VerifySubsetOf(El, Elements);
709 N->replaceOperandWith(4, Elements);
712 N->replaceOperandWith(6, TParams);
716 DIScopeRef DIScope::getRef() const {
717 if (!isCompositeType())
718 return DIScopeRef(*this);
719 DICompositeType DTy(DbgNode);
720 if (!DTy.getIdentifier())
721 return DIScopeRef(*this);
722 return DIScopeRef(DTy.getIdentifier());
725 void DICompositeType::setContainingType(DICompositeType ContainingType) {
726 TrackingMDNodeRef N(*this);
727 N->replaceOperandWith(5, ContainingType.getRef());
731 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
732 assert(CurFn && "Invalid function");
733 if (!getContext().isSubprogram())
735 // This variable is not inlined function argument if its scope
736 // does not describe current function.
737 return !DISubprogram(getContext()).describes(CurFn);
740 bool DISubprogram::describes(const Function *F) {
741 assert(F && "Invalid function");
742 if (F == getFunction())
744 StringRef Name = getLinkageName();
747 if (F->getName() == Name)
752 MDNode *DISubprogram::getVariablesNodes() const {
753 return getNodeField(DbgNode, 8);
756 DIArray DISubprogram::getVariables() const {
757 return DIArray(getNodeField(DbgNode, 8));
760 Metadata *DITemplateValueParameter::getValue() const {
761 return DbgNode->getOperand(3);
764 DIScopeRef DIScope::getContext() const {
767 return DIType(DbgNode).getContext();
770 return DIScopeRef(DISubprogram(DbgNode).getContext());
772 if (isLexicalBlock())
773 return DIScopeRef(DILexicalBlock(DbgNode).getContext());
775 if (isLexicalBlockFile())
776 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext());
779 return DIScopeRef(DINameSpace(DbgNode).getContext());
781 assert((isFile() || isCompileUnit()) && "Unhandled type of scope.");
782 return DIScopeRef(nullptr);
785 StringRef DIScope::getName() const {
787 return DIType(DbgNode).getName();
789 return DISubprogram(DbgNode).getName();
791 return DINameSpace(DbgNode).getName();
792 assert((isLexicalBlock() || isLexicalBlockFile() || isFile() ||
794 "Unhandled type of scope.");
798 StringRef DIScope::getFilename() const {
801 return ::getStringField(getNodeField(DbgNode, 1), 0);
804 StringRef DIScope::getDirectory() const {
807 return ::getStringField(getNodeField(DbgNode, 1), 1);
810 DIArray DICompileUnit::getEnumTypes() const {
811 if (!DbgNode || DbgNode->getNumOperands() < 7)
814 return DIArray(getNodeField(DbgNode, 2));
817 DIArray DICompileUnit::getRetainedTypes() const {
818 if (!DbgNode || DbgNode->getNumOperands() < 7)
821 return DIArray(getNodeField(DbgNode, 3));
824 DIArray DICompileUnit::getSubprograms() const {
825 if (!DbgNode || DbgNode->getNumOperands() < 7)
828 return DIArray(getNodeField(DbgNode, 4));
831 DIArray DICompileUnit::getGlobalVariables() const {
832 if (!DbgNode || DbgNode->getNumOperands() < 7)
835 return DIArray(getNodeField(DbgNode, 5));
838 DIArray DICompileUnit::getImportedEntities() const {
839 if (!DbgNode || DbgNode->getNumOperands() < 7)
842 return DIArray(getNodeField(DbgNode, 6));
845 void DICompileUnit::replaceSubprograms(DIArray Subprograms) {
846 assert(Verify() && "Expected compile unit");
847 if (Subprograms == getSubprograms())
850 const_cast<MDNode *>(DbgNode)->replaceOperandWith(4, Subprograms);
853 void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {
854 assert(Verify() && "Expected compile unit");
855 if (GlobalVariables == getGlobalVariables())
858 const_cast<MDNode *>(DbgNode)->replaceOperandWith(5, GlobalVariables);
861 DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
862 DILexicalBlockFile NewScope) {
864 assert(NewScope && "Expected valid scope");
866 const auto *Old = cast<MDLocation>(DbgNode);
867 return DILocation(MDLocation::get(Ctx, Old->getLine(), Old->getColumn(),
868 NewScope, Old->getInlinedAt()));
871 unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
872 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
873 return ++Ctx.pImpl->DiscriminatorTable[Key];
876 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
877 LLVMContext &VMContext) {
878 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
880 return cleanseInlinedVariable(DV, VMContext);
882 // Insert inlined scope.
883 SmallVector<Metadata *, 8> Elts;
884 for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I)
885 Elts.push_back(DV->getOperand(I));
886 Elts.push_back(InlinedScope);
888 DIVariable Inlined(MDNode::get(VMContext, Elts));
889 assert(Inlined.Verify() && "Expected to create a DIVariable");
893 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
894 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
895 if (!DIVariable(DV).getInlinedAt())
896 return DIVariable(DV);
898 // Remove inlined scope.
899 SmallVector<Metadata *, 8> Elts;
900 for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I)
901 Elts.push_back(DV->getOperand(I));
903 DIVariable Cleansed(MDNode::get(VMContext, Elts));
904 assert(Cleansed.Verify() && "Expected to create a DIVariable");
908 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
909 DIDescriptor D(Scope);
910 if (D.isSubprogram())
911 return DISubprogram(Scope);
913 if (D.isLexicalBlockFile())
914 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
916 if (D.isLexicalBlock())
917 return getDISubprogram(DILexicalBlock(Scope).getContext());
919 return DISubprogram();
922 DISubprogram llvm::getDISubprogram(const Function *F) {
923 // We look for the first instr that has a debug annotation leading back to F.
924 for (auto &BB : *F) {
925 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
926 return !Inst.getDebugLoc().isUnknown();
928 if (Inst == BB.end())
930 DebugLoc DLoc = Inst->getDebugLoc();
931 const MDNode *Scope = DLoc.getScopeNode();
932 DISubprogram Subprogram = getDISubprogram(Scope);
933 return Subprogram.describes(F) ? Subprogram : DISubprogram();
936 return DISubprogram();
939 DICompositeType llvm::getDICompositeType(DIType T) {
940 if (T.isCompositeType())
941 return DICompositeType(T);
943 if (T.isDerivedType()) {
944 // This function is currently used by dragonegg and dragonegg does
945 // not generate identifier for types, so using an empty map to resolve
946 // DerivedFrom should be fine.
947 DITypeIdentifierMap EmptyMap;
948 return getDICompositeType(
949 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap));
952 return DICompositeType();
956 llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
957 DITypeIdentifierMap Map;
958 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
959 DICompileUnit CU(CU_Nodes->getOperand(CUi));
960 DIArray Retain = CU.getRetainedTypes();
961 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
962 if (!Retain.getElement(Ti).isCompositeType())
964 DICompositeType Ty(Retain.getElement(Ti));
965 if (MDString *TypeId = Ty.getIdentifier()) {
966 // Definition has priority over declaration.
967 // Try to insert (TypeId, Ty) to Map.
968 std::pair<DITypeIdentifierMap::iterator, bool> P =
969 Map.insert(std::make_pair(TypeId, Ty));
970 // If TypeId already exists in Map and this is a definition, replace
971 // whatever we had (declaration or definition) with the definition.
972 if (!P.second && !Ty.isForwardDecl())
973 P.first->second = Ty;
980 //===----------------------------------------------------------------------===//
981 // DebugInfoFinder implementations.
982 //===----------------------------------------------------------------------===//
984 void DebugInfoFinder::reset() {
991 TypeIdentifierMap.clear();
992 TypeMapInitialized = false;
995 void DebugInfoFinder::InitializeTypeMap(const Module &M) {
996 if (!TypeMapInitialized)
997 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
998 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
999 TypeMapInitialized = true;
1003 void DebugInfoFinder::processModule(const Module &M) {
1004 InitializeTypeMap(M);
1005 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
1006 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
1007 DICompileUnit CU(CU_Nodes->getOperand(i));
1009 DIArray GVs = CU.getGlobalVariables();
1010 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
1011 DIGlobalVariable DIG(GVs.getElement(i));
1012 if (addGlobalVariable(DIG)) {
1013 processScope(DIG.getContext());
1014 processType(DIG.getType().resolve(TypeIdentifierMap));
1017 DIArray SPs = CU.getSubprograms();
1018 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
1019 processSubprogram(DISubprogram(SPs.getElement(i)));
1020 DIArray EnumTypes = CU.getEnumTypes();
1021 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
1022 processType(DIType(EnumTypes.getElement(i)));
1023 DIArray RetainedTypes = CU.getRetainedTypes();
1024 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
1025 processType(DIType(RetainedTypes.getElement(i)));
1026 DIArray Imports = CU.getImportedEntities();
1027 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
1028 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i));
1029 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
1030 if (Entity.isType())
1031 processType(DIType(Entity));
1032 else if (Entity.isSubprogram())
1033 processSubprogram(DISubprogram(Entity));
1034 else if (Entity.isNameSpace())
1035 processScope(DINameSpace(Entity).getContext());
1041 void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
1044 InitializeTypeMap(M);
1045 processScope(Loc.getScope());
1046 processLocation(M, Loc.getOrigLocation());
1049 void DebugInfoFinder::processType(DIType DT) {
1052 processScope(DT.getContext().resolve(TypeIdentifierMap));
1053 if (DT.isCompositeType()) {
1054 DICompositeType DCT(DT);
1055 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
1056 if (DT.isSubroutineType()) {
1057 DITypeArray DTA = DISubroutineType(DT).getTypeArray();
1058 for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i)
1059 processType(DTA.getElement(i).resolve(TypeIdentifierMap));
1062 DIArray DA = DCT.getElements();
1063 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1064 DIDescriptor D = DA.getElement(i);
1066 processType(DIType(D));
1067 else if (D.isSubprogram())
1068 processSubprogram(DISubprogram(D));
1070 } else if (DT.isDerivedType()) {
1071 DIDerivedType DDT(DT);
1072 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
1076 void DebugInfoFinder::processScope(DIScope Scope) {
1077 if (Scope.isType()) {
1082 if (Scope.isCompileUnit()) {
1083 addCompileUnit(DICompileUnit(Scope));
1086 if (Scope.isSubprogram()) {
1087 processSubprogram(DISubprogram(Scope));
1090 if (!addScope(Scope))
1092 if (Scope.isLexicalBlock()) {
1093 DILexicalBlock LB(Scope);
1094 processScope(LB.getContext());
1095 } else if (Scope.isLexicalBlockFile()) {
1096 DILexicalBlockFile LBF = DILexicalBlockFile(Scope);
1097 processScope(LBF.getScope());
1098 } else if (Scope.isNameSpace()) {
1099 DINameSpace NS(Scope);
1100 processScope(NS.getContext());
1104 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1105 if (!addSubprogram(SP))
1107 processScope(SP.getContext().resolve(TypeIdentifierMap));
1108 processType(SP.getType());
1109 DIArray TParams = SP.getTemplateParams();
1110 for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
1111 DIDescriptor Element = TParams.getElement(I);
1112 if (Element.isTemplateTypeParameter()) {
1113 DITemplateTypeParameter TType(Element);
1114 processScope(TType.getContext().resolve(TypeIdentifierMap));
1115 processType(TType.getType().resolve(TypeIdentifierMap));
1116 } else if (Element.isTemplateValueParameter()) {
1117 DITemplateValueParameter TVal(Element);
1118 processScope(TVal.getContext().resolve(TypeIdentifierMap));
1119 processType(TVal.getType().resolve(TypeIdentifierMap));
1124 void DebugInfoFinder::processDeclare(const Module &M,
1125 const DbgDeclareInst *DDI) {
1126 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1129 InitializeTypeMap(M);
1132 if (!DV.isVariable())
1135 if (!NodesSeen.insert(DV).second)
1137 processScope(DIVariable(N).getContext());
1138 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
1141 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
1142 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
1145 InitializeTypeMap(M);
1148 if (!DV.isVariable())
1151 if (!NodesSeen.insert(DV).second)
1153 processScope(DIVariable(N).getContext());
1154 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
1157 bool DebugInfoFinder::addType(DIType DT) {
1161 if (!NodesSeen.insert(DT).second)
1168 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1171 if (!NodesSeen.insert(CU).second)
1178 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1182 if (!NodesSeen.insert(DIG).second)
1189 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1193 if (!NodesSeen.insert(SP).second)
1200 bool DebugInfoFinder::addScope(DIScope Scope) {
1203 // FIXME: Ocaml binding generates a scope with no content, we treat it
1205 if (Scope->getNumOperands() == 0)
1207 if (!NodesSeen.insert(Scope).second)
1209 Scopes.push_back(Scope);
1213 //===----------------------------------------------------------------------===//
1214 // DIDescriptor: dump routines for all descriptors.
1215 //===----------------------------------------------------------------------===//
1217 void DIDescriptor::dump() const {
1222 void DIDescriptor::print(raw_ostream &OS) const {
1226 if (const char *Tag = dwarf::TagString(getTag()))
1227 OS << "[ " << Tag << " ]";
1229 if (this->isSubrange()) {
1230 DISubrange(DbgNode).printInternal(OS);
1231 } else if (this->isCompileUnit()) {
1232 DICompileUnit(DbgNode).printInternal(OS);
1233 } else if (this->isFile()) {
1234 DIFile(DbgNode).printInternal(OS);
1235 } else if (this->isEnumerator()) {
1236 DIEnumerator(DbgNode).printInternal(OS);
1237 } else if (this->isBasicType()) {
1238 DIType(DbgNode).printInternal(OS);
1239 } else if (this->isDerivedType()) {
1240 DIDerivedType(DbgNode).printInternal(OS);
1241 } else if (this->isCompositeType()) {
1242 DICompositeType(DbgNode).printInternal(OS);
1243 } else if (this->isSubprogram()) {
1244 DISubprogram(DbgNode).printInternal(OS);
1245 } else if (this->isGlobalVariable()) {
1246 DIGlobalVariable(DbgNode).printInternal(OS);
1247 } else if (this->isVariable()) {
1248 DIVariable(DbgNode).printInternal(OS);
1249 } else if (this->isObjCProperty()) {
1250 DIObjCProperty(DbgNode).printInternal(OS);
1251 } else if (this->isNameSpace()) {
1252 DINameSpace(DbgNode).printInternal(OS);
1253 } else if (this->isScope()) {
1254 DIScope(DbgNode).printInternal(OS);
1255 } else if (this->isExpression()) {
1256 DIExpression(DbgNode).printInternal(OS);
1260 void DISubrange::printInternal(raw_ostream &OS) const {
1261 int64_t Count = getCount();
1263 OS << " [" << getLo() << ", " << Count - 1 << ']';
1265 OS << " [unbounded]";
1268 void DIScope::printInternal(raw_ostream &OS) const {
1269 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1272 void DICompileUnit::printInternal(raw_ostream &OS) const {
1273 DIScope::printInternal(OS);
1275 unsigned Lang = getLanguage();
1276 if (const char *LangStr = dwarf::LanguageString(Lang))
1279 (OS << "lang 0x").write_hex(Lang);
1283 void DIEnumerator::printInternal(raw_ostream &OS) const {
1284 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1287 void DIType::printInternal(raw_ostream &OS) const {
1291 StringRef Res = getName();
1293 OS << " [" << Res << "]";
1295 // TODO: Print context?
1297 OS << " [line " << getLineNumber() << ", size " << getSizeInBits()
1298 << ", align " << getAlignInBits() << ", offset " << getOffsetInBits();
1300 if (const char *Enc =
1301 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1302 OS << ", enc " << Enc;
1307 else if (isProtected())
1308 OS << " [protected]";
1309 else if (isPublic())
1313 OS << " [artificial]";
1315 if (isForwardDecl())
1317 else if (getTag() == dwarf::DW_TAG_structure_type ||
1318 getTag() == dwarf::DW_TAG_union_type ||
1319 getTag() == dwarf::DW_TAG_enumeration_type ||
1320 getTag() == dwarf::DW_TAG_class_type)
1324 if (isStaticMember())
1327 if (isLValueReference())
1328 OS << " [reference]";
1330 if (isRValueReference())
1331 OS << " [rvalue reference]";
1334 void DIDerivedType::printInternal(raw_ostream &OS) const {
1335 DIType::printInternal(OS);
1336 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1339 void DICompositeType::printInternal(raw_ostream &OS) const {
1340 DIType::printInternal(OS);
1341 DIArray A = getElements();
1342 OS << " [" << A.getNumElements() << " elements]";
1345 void DINameSpace::printInternal(raw_ostream &OS) const {
1346 StringRef Name = getName();
1348 OS << " [" << Name << ']';
1350 OS << " [line " << getLineNumber() << ']';
1353 void DISubprogram::printInternal(raw_ostream &OS) const {
1354 // TODO : Print context
1355 OS << " [line " << getLineNumber() << ']';
1357 if (isLocalToUnit())
1363 if (getScopeLineNumber() != getLineNumber())
1364 OS << " [scope " << getScopeLineNumber() << "]";
1368 else if (isProtected())
1369 OS << " [protected]";
1370 else if (isPublic())
1373 if (isLValueReference())
1374 OS << " [reference]";
1376 if (isRValueReference())
1377 OS << " [rvalue reference]";
1379 StringRef Res = getName();
1381 OS << " [" << Res << ']';
1384 void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1385 StringRef Res = getName();
1387 OS << " [" << Res << ']';
1389 OS << " [line " << getLineNumber() << ']';
1391 // TODO : Print context
1393 if (isLocalToUnit())
1400 void DIVariable::printInternal(raw_ostream &OS) const {
1401 StringRef Res = getName();
1403 OS << " [" << Res << ']';
1405 OS << " [line " << getLineNumber() << ']';
1408 void DIExpression::printInternal(raw_ostream &OS) const {
1409 for (unsigned I = 0; I < getNumElements(); ++I) {
1410 uint64_t OpCode = getElement(I);
1411 OS << " [" << OperationEncodingString(OpCode);
1414 OS << " " << getElement(++I);
1418 unsigned Offset = getElement(++I);
1419 unsigned Size = getElement(++I);
1420 OS << " offset=" << Offset << ", size=" << Size;
1427 // Else bail out early. This may be a line table entry.
1435 void DIObjCProperty::printInternal(raw_ostream &OS) const {
1436 StringRef Name = getObjCPropertyName();
1438 OS << " [" << Name << ']';
1440 OS << " [line " << getLineNumber() << ", properties " << getUnsignedField(6)
1444 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1445 const LLVMContext &Ctx) {
1446 if (!DL.isUnknown()) { // Print source line info.
1447 DIScope Scope(DL.getScope(Ctx));
1448 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
1449 // Omit the directory, because it's likely to be long and uninteresting.
1450 CommentOS << Scope.getFilename();
1451 CommentOS << ':' << DL.getLine();
1452 if (DL.getCol() != 0)
1453 CommentOS << ':' << DL.getCol();
1454 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1455 if (!InlinedAtDL.isUnknown()) {
1456 CommentOS << " @[ ";
1457 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1463 void DIVariable::printExtendedName(raw_ostream &OS) const {
1464 const LLVMContext &Ctx = DbgNode->getContext();
1465 StringRef Res = getName();
1467 OS << Res << "," << getLineNumber();
1468 if (MDNode *InlinedAt = getInlinedAt()) {
1469 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1470 if (!InlinedAtDL.isUnknown()) {
1472 printDebugLoc(InlinedAtDL, OS, Ctx);
1478 template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) {
1479 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
1481 template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) {
1482 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
1486 DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
1487 return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
1489 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
1490 return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
1493 bool llvm::StripDebugInfo(Module &M) {
1494 bool Changed = false;
1496 // Remove all of the calls to the debugger intrinsics, and remove them from
1498 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
1499 while (!Declare->use_empty()) {
1500 CallInst *CI = cast<CallInst>(Declare->user_back());
1501 CI->eraseFromParent();
1503 Declare->eraseFromParent();
1507 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
1508 while (!DbgVal->use_empty()) {
1509 CallInst *CI = cast<CallInst>(DbgVal->user_back());
1510 CI->eraseFromParent();
1512 DbgVal->eraseFromParent();
1516 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
1517 NME = M.named_metadata_end(); NMI != NME;) {
1518 NamedMDNode *NMD = NMI;
1520 if (NMD->getName().startswith("llvm.dbg.")) {
1521 NMD->eraseFromParent();
1526 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
1527 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
1529 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
1531 if (!BI->getDebugLoc().isUnknown()) {
1533 BI->setDebugLoc(DebugLoc());
1540 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
1541 if (auto *Val = mdconst::extract_or_null<ConstantInt>(
1542 M.getModuleFlag("Debug Info Version")))
1543 return Val->getZExtValue();
1547 llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
1548 llvm::makeSubprogramMap(const Module &M) {
1549 DenseMap<const Function *, DISubprogram> R;
1551 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
1555 for (MDNode *N : CU_Nodes->operands()) {
1556 DICompileUnit CUNode(N);
1557 DIArray SPs = CUNode.getSubprograms();
1558 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
1559 DISubprogram SP(SPs.getElement(i));
1560 if (Function *F = SP.getFunction())
1561 R.insert(std::make_pair(F, SP));