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());
55 static Value *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 {
79 if (Elt < DbgNode->getNumOperands())
81 dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
82 return CI->getZExtValue();
87 int64_t DIDescriptor::getInt64Field(unsigned Elt) const {
91 if (Elt < DbgNode->getNumOperands())
93 dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
94 return CI->getSExtValue();
99 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
100 MDNode *Field = getNodeField(DbgNode, Elt);
101 return DIDescriptor(Field);
104 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
108 if (Elt < DbgNode->getNumOperands())
109 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
113 Constant *DIDescriptor::getConstantField(unsigned Elt) const {
117 if (Elt < DbgNode->getNumOperands())
118 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
122 Function *DIDescriptor::getFunctionField(unsigned Elt) const {
126 if (Elt < DbgNode->getNumOperands())
127 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
131 void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) {
135 if (Elt < DbgNode->getNumOperands()) {
136 MDNode *Node = const_cast<MDNode *>(DbgNode);
137 Node->replaceOperandWith(Elt, F);
141 uint64_t DIVariable::getAddrElement(unsigned Idx) const {
142 DIDescriptor ComplexExpr = getDescriptorField(8);
143 if (Idx < ComplexExpr->getNumOperands())
144 if (auto *CI = dyn_cast_or_null<ConstantInt>(ComplexExpr->getOperand(Idx)))
145 return CI->getZExtValue();
147 assert(false && "non-existing complex address element requested");
151 /// getInlinedAt - If this variable is inlined then return inline location.
152 MDNode *DIVariable::getInlinedAt() const { return getNodeField(DbgNode, 7); }
154 bool DIVariable::isVariablePiece() const {
155 return hasComplexAddress() && getAddrElement(0) == DIBuilder::OpPiece;
158 uint64_t DIVariable::getPieceOffset() const {
159 assert(isVariablePiece());
160 return getAddrElement(1);
163 uint64_t DIVariable::getPieceSize() const {
164 assert(isVariablePiece());
165 return getAddrElement(2);
168 /// Return the size reported by the variable's type.
169 unsigned DIVariable::getSizeInBits(const DITypeIdentifierMap &Map) {
170 DIType Ty = getType().resolve(Map);
171 // Follow derived types until we reach a type that
172 // reports back a size.
173 while (Ty.isDerivedType() && !Ty.getSizeInBits()) {
174 DIDerivedType DT(&*Ty);
175 Ty = DT.getTypeDerivedFrom().resolve(Map);
177 assert(Ty.getSizeInBits() && "type with size 0");
178 return Ty.getSizeInBits();
184 //===----------------------------------------------------------------------===//
186 //===----------------------------------------------------------------------===//
188 bool DIDescriptor::isSubroutineType() const {
189 return isCompositeType() && getTag() == dwarf::DW_TAG_subroutine_type;
192 /// isBasicType - Return true if the specified tag is legal for
194 bool DIDescriptor::isBasicType() const {
198 case dwarf::DW_TAG_base_type:
199 case dwarf::DW_TAG_unspecified_type:
206 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
207 bool DIDescriptor::isDerivedType() const {
211 case dwarf::DW_TAG_typedef:
212 case dwarf::DW_TAG_pointer_type:
213 case dwarf::DW_TAG_ptr_to_member_type:
214 case dwarf::DW_TAG_reference_type:
215 case dwarf::DW_TAG_rvalue_reference_type:
216 case dwarf::DW_TAG_const_type:
217 case dwarf::DW_TAG_volatile_type:
218 case dwarf::DW_TAG_restrict_type:
219 case dwarf::DW_TAG_member:
220 case dwarf::DW_TAG_inheritance:
221 case dwarf::DW_TAG_friend:
224 // CompositeTypes are currently modelled as DerivedTypes.
225 return isCompositeType();
229 /// isCompositeType - Return true if the specified tag is legal for
231 bool DIDescriptor::isCompositeType() const {
235 case dwarf::DW_TAG_array_type:
236 case dwarf::DW_TAG_structure_type:
237 case dwarf::DW_TAG_union_type:
238 case dwarf::DW_TAG_enumeration_type:
239 case dwarf::DW_TAG_subroutine_type:
240 case dwarf::DW_TAG_class_type:
247 /// isVariable - Return true if the specified tag is legal for DIVariable.
248 bool DIDescriptor::isVariable() const {
252 case dwarf::DW_TAG_auto_variable:
253 case dwarf::DW_TAG_arg_variable:
260 /// isType - Return true if the specified tag is legal for DIType.
261 bool DIDescriptor::isType() const {
262 return isBasicType() || isCompositeType() || isDerivedType();
265 /// isSubprogram - Return true if the specified tag is legal for
267 bool DIDescriptor::isSubprogram() const {
268 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
271 /// isGlobalVariable - Return true if the specified tag is legal for
272 /// DIGlobalVariable.
273 bool DIDescriptor::isGlobalVariable() const {
274 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
275 getTag() == dwarf::DW_TAG_constant);
278 /// isScope - Return true if the specified tag is one of the scope
280 bool DIDescriptor::isScope() const {
284 case dwarf::DW_TAG_compile_unit:
285 case dwarf::DW_TAG_lexical_block:
286 case dwarf::DW_TAG_subprogram:
287 case dwarf::DW_TAG_namespace:
288 case dwarf::DW_TAG_file_type:
296 /// isTemplateTypeParameter - Return true if the specified tag is
297 /// DW_TAG_template_type_parameter.
298 bool DIDescriptor::isTemplateTypeParameter() const {
299 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
302 /// isTemplateValueParameter - Return true if the specified tag is
303 /// DW_TAG_template_value_parameter.
304 bool DIDescriptor::isTemplateValueParameter() const {
305 return DbgNode && (getTag() == dwarf::DW_TAG_template_value_parameter ||
306 getTag() == dwarf::DW_TAG_GNU_template_template_param ||
307 getTag() == dwarf::DW_TAG_GNU_template_parameter_pack);
310 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
311 bool DIDescriptor::isCompileUnit() const {
312 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
315 /// isFile - Return true if the specified tag is DW_TAG_file_type.
316 bool DIDescriptor::isFile() const {
317 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
320 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
321 bool DIDescriptor::isNameSpace() const {
322 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
325 /// isLexicalBlockFile - Return true if the specified descriptor is a
326 /// lexical block with an extra file.
327 bool DIDescriptor::isLexicalBlockFile() const {
328 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
329 (DbgNode->getNumOperands() == 3);
332 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
333 bool DIDescriptor::isLexicalBlock() const {
334 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
335 (DbgNode->getNumOperands() > 3);
338 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
339 bool DIDescriptor::isSubrange() const {
340 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
343 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
344 bool DIDescriptor::isEnumerator() const {
345 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
348 /// isObjCProperty - Return true if the specified tag is DW_TAG_APPLE_property.
349 bool DIDescriptor::isObjCProperty() const {
350 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
353 /// \brief Return true if the specified tag is DW_TAG_imported_module or
354 /// DW_TAG_imported_declaration.
355 bool DIDescriptor::isImportedEntity() const {
356 return DbgNode && (getTag() == dwarf::DW_TAG_imported_module ||
357 getTag() == dwarf::DW_TAG_imported_declaration);
360 //===----------------------------------------------------------------------===//
361 // Simple Descriptor Constructors and other Methods
362 //===----------------------------------------------------------------------===//
364 /// replaceAllUsesWith - Replace all uses of the MDNode used by this
365 /// type with the one in the passed descriptor.
366 void DIType::replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D) {
368 assert(DbgNode && "Trying to replace an unverified type!");
370 // Since we use a TrackingVH for the node, its easy for clients to manufacture
371 // legitimate situations where they want to replaceAllUsesWith() on something
372 // which, due to uniquing, has merged with the source. We shield clients from
373 // this detail by allowing a value to be replaced with replaceAllUsesWith()
375 const MDNode *DN = D;
377 SmallVector<Value*, 10> Ops(DbgNode->getNumOperands());
378 for (size_t i = 0; i != Ops.size(); ++i)
379 Ops[i] = DbgNode->getOperand(i);
380 DN = MDNode::get(VMContext, Ops);
383 MDNode *Node = const_cast<MDNode *>(DbgNode);
384 const Value *V = cast_or_null<Value>(DN);
385 Node->replaceAllUsesWith(const_cast<Value *>(V));
386 MDNode::deleteTemporary(Node);
390 /// replaceAllUsesWith - Replace all uses of the MDNode used by this
391 /// type with the one in D.
392 void DIType::replaceAllUsesWith(MDNode *D) {
394 assert(DbgNode && "Trying to replace an unverified type!");
395 assert(DbgNode != D && "This replacement should always happen");
396 MDNode *Node = const_cast<MDNode *>(DbgNode);
397 const MDNode *DN = D;
398 const Value *V = cast_or_null<Value>(DN);
399 Node->replaceAllUsesWith(const_cast<Value *>(V));
400 MDNode::deleteTemporary(Node);
403 /// Verify - Verify that a compile unit is well formed.
404 bool DICompileUnit::Verify() const {
405 if (!isCompileUnit())
408 // Don't bother verifying the compilation directory or producer string
409 // as those could be empty.
410 if (getFilename().empty())
413 return DbgNode->getNumOperands() == 14;
416 /// Verify - Verify that an ObjC property is well formed.
417 bool DIObjCProperty::Verify() const {
418 if (!isObjCProperty())
421 // Don't worry about the rest of the strings for now.
422 return DbgNode->getNumOperands() == 8;
425 /// Check if a field at position Elt of a MDNode is a MDNode.
426 /// We currently allow an empty string and an integer.
427 /// But we don't allow a non-empty string in a MDNode field.
428 static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) {
429 // FIXME: This function should return true, if the field is null or the field
430 // is indeed a MDNode: return !Fld || isa<MDNode>(Fld).
431 Value *Fld = getField(DbgNode, Elt);
432 if (Fld && isa<MDString>(Fld) && !cast<MDString>(Fld)->getString().empty())
437 /// Check if a field at position Elt of a MDNode is a MDString.
438 static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) {
439 Value *Fld = getField(DbgNode, Elt);
440 return !Fld || isa<MDString>(Fld);
443 /// Check if a value can be a reference to a type.
444 static bool isTypeRef(const Value *Val) {
446 (isa<MDString>(Val) && !cast<MDString>(Val)->getString().empty()) ||
447 (isa<MDNode>(Val) && DIType(cast<MDNode>(Val)).isType());
450 /// Check if a field at position Elt of a MDNode can be a reference to a type.
451 static bool fieldIsTypeRef(const MDNode *DbgNode, unsigned Elt) {
452 Value *Fld = getField(DbgNode, Elt);
453 return isTypeRef(Fld);
456 /// Check if a value can be a ScopeRef.
457 static bool isScopeRef(const Value *Val) {
459 (isa<MDString>(Val) && !cast<MDString>(Val)->getString().empty()) ||
460 // Not checking for Val->isScope() here, because it would work
461 // only for lexical scopes and not all subclasses of DIScope.
465 /// Check if a field at position Elt of a MDNode can be a ScopeRef.
466 static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) {
467 Value *Fld = getField(DbgNode, Elt);
468 return isScopeRef(Fld);
471 /// Verify - Verify that a type descriptor is well formed.
472 bool DIType::Verify() const {
475 // Make sure Context @ field 2 is MDNode.
476 if (!fieldIsScopeRef(DbgNode, 2))
479 // FIXME: Sink this into the various subclass verifies.
480 uint16_t Tag = getTag();
481 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
482 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
483 Tag != dwarf::DW_TAG_ptr_to_member_type &&
484 Tag != dwarf::DW_TAG_reference_type &&
485 Tag != dwarf::DW_TAG_rvalue_reference_type &&
486 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_array_type &&
487 Tag != dwarf::DW_TAG_enumeration_type &&
488 Tag != dwarf::DW_TAG_subroutine_type &&
489 Tag != dwarf::DW_TAG_inheritance && Tag != dwarf::DW_TAG_friend &&
490 getFilename().empty())
492 // DIType is abstract, it should be a BasicType, a DerivedType or
495 return DIBasicType(DbgNode).Verify();
496 else if (isCompositeType())
497 return DICompositeType(DbgNode).Verify();
498 else if (isDerivedType())
499 return DIDerivedType(DbgNode).Verify();
504 /// Verify - Verify that a basic type descriptor is well formed.
505 bool DIBasicType::Verify() const {
506 return isBasicType() && DbgNode->getNumOperands() == 10;
509 /// Verify - Verify that a derived type descriptor is well formed.
510 bool DIDerivedType::Verify() const {
511 // Make sure DerivedFrom @ field 9 is TypeRef.
512 if (!fieldIsTypeRef(DbgNode, 9))
514 if (getTag() == dwarf::DW_TAG_ptr_to_member_type)
515 // Make sure ClassType @ field 10 is a TypeRef.
516 if (!fieldIsTypeRef(DbgNode, 10))
519 return isDerivedType() && DbgNode->getNumOperands() >= 10 &&
520 DbgNode->getNumOperands() <= 14;
523 /// Verify - Verify that a composite type descriptor is well formed.
524 bool DICompositeType::Verify() const {
525 if (!isCompositeType())
528 // Make sure DerivedFrom @ field 9 and ContainingType @ field 12 are TypeRef.
529 if (!fieldIsTypeRef(DbgNode, 9))
531 if (!fieldIsTypeRef(DbgNode, 12))
534 // Make sure the type identifier at field 14 is MDString, it can be null.
535 if (!fieldIsMDString(DbgNode, 14))
538 // A subroutine type can't be both & and &&.
539 if (isLValueReference() && isRValueReference())
542 return DbgNode->getNumOperands() == 15;
545 /// Verify - Verify that a subprogram descriptor is well formed.
546 bool DISubprogram::Verify() const {
550 // Make sure context @ field 2 is a ScopeRef and type @ field 7 is a MDNode.
551 if (!fieldIsScopeRef(DbgNode, 2))
553 if (!fieldIsMDNode(DbgNode, 7))
555 // Containing type @ field 12.
556 if (!fieldIsTypeRef(DbgNode, 12))
559 // A subprogram can't be both & and &&.
560 if (isLValueReference() && isRValueReference())
563 if (auto *F = getFunction()) {
564 LLVMContext &Ctxt = F->getContext();
565 for (auto &BB : *F) {
567 DebugLoc DL = I.getDebugLoc();
571 MDNode *Scope = nullptr;
572 MDNode *IA = nullptr;
573 // walk the inlined-at scopes
574 while (DL.getScopeAndInlinedAt(Scope, IA, F->getContext()), IA)
575 DL = DebugLoc::getFromDILocation(IA);
576 DL.getScopeAndInlinedAt(Scope, IA, Ctxt);
578 while (!DIDescriptor(Scope).isSubprogram()) {
579 DILexicalBlockFile D(Scope);
580 Scope = D.isLexicalBlockFile()
582 : DebugLoc::getFromDILexicalBlock(Scope).getScope(Ctxt);
584 if (!DISubprogram(Scope).describes(F))
589 return DbgNode->getNumOperands() == 20;
592 /// Verify - Verify that a global variable descriptor is well formed.
593 bool DIGlobalVariable::Verify() const {
594 if (!isGlobalVariable())
597 if (getDisplayName().empty())
599 // Make sure context @ field 2 is an MDNode.
600 if (!fieldIsMDNode(DbgNode, 2))
602 // Make sure that type @ field 8 is a DITypeRef.
603 if (!fieldIsTypeRef(DbgNode, 8))
605 // Make sure StaticDataMemberDeclaration @ field 12 is MDNode.
606 if (!fieldIsMDNode(DbgNode, 12))
609 return DbgNode->getNumOperands() == 13;
612 /// Verify - Verify that a variable descriptor is well formed.
613 bool DIVariable::Verify() const {
617 // Make sure context @ field 1 is an MDNode.
618 if (!fieldIsMDNode(DbgNode, 1))
620 // Make sure that type @ field 5 is a DITypeRef.
621 if (!fieldIsTypeRef(DbgNode, 5))
624 // Variable without a complex expression.
625 if (DbgNode->getNumOperands() == 8)
628 // Make sure the complex expression is an MDNode.
629 return (DbgNode->getNumOperands() == 9 && fieldIsMDNode(DbgNode, 8));
632 /// Verify - Verify that a location descriptor is well formed.
633 bool DILocation::Verify() const {
637 return DbgNode->getNumOperands() == 4;
640 /// Verify - Verify that a namespace descriptor is well formed.
641 bool DINameSpace::Verify() const {
644 return DbgNode->getNumOperands() == 5;
647 /// \brief Retrieve the MDNode for the directory/file pair.
648 MDNode *DIFile::getFileNode() const { return getNodeField(DbgNode, 1); }
650 /// \brief Verify that the file descriptor is well formed.
651 bool DIFile::Verify() const {
652 return isFile() && DbgNode->getNumOperands() == 2;
655 /// \brief Verify that the enumerator descriptor is well formed.
656 bool DIEnumerator::Verify() const {
657 return isEnumerator() && DbgNode->getNumOperands() == 3;
660 /// \brief Verify that the subrange descriptor is well formed.
661 bool DISubrange::Verify() const {
662 return isSubrange() && DbgNode->getNumOperands() == 3;
665 /// \brief Verify that the lexical block descriptor is well formed.
666 bool DILexicalBlock::Verify() const {
667 return isLexicalBlock() && DbgNode->getNumOperands() == 7;
670 /// \brief Verify that the file-scoped lexical block descriptor is well formed.
671 bool DILexicalBlockFile::Verify() const {
672 return isLexicalBlockFile() && DbgNode->getNumOperands() == 3;
675 /// \brief Verify that the template type parameter descriptor is well formed.
676 bool DITemplateTypeParameter::Verify() const {
677 return isTemplateTypeParameter() && DbgNode->getNumOperands() == 7;
680 /// \brief Verify that the template value parameter descriptor is well formed.
681 bool DITemplateValueParameter::Verify() const {
682 return isTemplateValueParameter() && DbgNode->getNumOperands() == 8;
685 /// \brief Verify that the imported module descriptor is well formed.
686 bool DIImportedEntity::Verify() const {
687 return isImportedEntity() &&
688 (DbgNode->getNumOperands() == 4 || DbgNode->getNumOperands() == 5);
691 /// getObjCProperty - Return property node, if this ivar is associated with one.
692 MDNode *DIDerivedType::getObjCProperty() const {
693 return getNodeField(DbgNode, 10);
696 MDString *DICompositeType::getIdentifier() const {
697 return cast_or_null<MDString>(getField(DbgNode, 14));
701 static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) {
702 for (unsigned i = 0; i != LHS->getNumOperands(); ++i) {
703 // Skip the 'empty' list (that's a single i32 0, rather than truly empty).
704 if (i == 0 && isa<ConstantInt>(LHS->getOperand(i)))
706 const MDNode *E = cast<MDNode>(LHS->getOperand(i));
708 for (unsigned j = 0; !found && j != RHS->getNumOperands(); ++j)
709 found = E == RHS->getOperand(j);
710 assert(found && "Losing a member during member list replacement");
715 /// \brief Set the array of member DITypes.
716 void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) {
717 TrackingVH<MDNode> N(*this);
720 // Check that the new list of members contains all the old members as well.
721 if (const MDNode *El = cast_or_null<MDNode>(N->getOperand(10)))
722 VerifySubsetOf(El, Elements);
724 N->replaceOperandWith(10, Elements);
727 N->replaceOperandWith(13, TParams);
731 /// Generate a reference to this DIType. Uses the type identifier instead
732 /// of the actual MDNode if possible, to help type uniquing.
733 DIScopeRef DIScope::getRef() const {
734 if (!isCompositeType())
735 return DIScopeRef(*this);
736 DICompositeType DTy(DbgNode);
737 if (!DTy.getIdentifier())
738 return DIScopeRef(*this);
739 return DIScopeRef(DTy.getIdentifier());
742 /// \brief Set the containing type.
743 void DICompositeType::setContainingType(DICompositeType ContainingType) {
744 TrackingVH<MDNode> N(*this);
745 N->replaceOperandWith(12, ContainingType.getRef());
749 /// isInlinedFnArgument - Return true if this variable provides debugging
750 /// information for an inlined function arguments.
751 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
752 assert(CurFn && "Invalid function");
753 if (!getContext().isSubprogram())
755 // This variable is not inlined function argument if its scope
756 // does not describe current function.
757 return !DISubprogram(getContext()).describes(CurFn);
760 /// describes - Return true if this subprogram provides debugging
761 /// information for the function F.
762 bool DISubprogram::describes(const Function *F) {
763 assert(F && "Invalid function");
764 if (F == getFunction())
766 StringRef Name = getLinkageName();
769 if (F->getName() == Name)
774 unsigned DISubprogram::isOptimized() const {
775 assert(DbgNode && "Invalid subprogram descriptor!");
776 if (DbgNode->getNumOperands() == 15)
777 return getUnsignedField(14);
781 MDNode *DISubprogram::getVariablesNodes() const {
782 return getNodeField(DbgNode, 18);
785 DIArray DISubprogram::getVariables() const {
786 return DIArray(getNodeField(DbgNode, 18));
789 Value *DITemplateValueParameter::getValue() const {
790 return getField(DbgNode, 4);
793 // If the current node has a parent scope then return that,
794 // else return an empty scope.
795 DIScopeRef DIScope::getContext() const {
798 return DIType(DbgNode).getContext();
801 return DIScopeRef(DISubprogram(DbgNode).getContext());
803 if (isLexicalBlock())
804 return DIScopeRef(DILexicalBlock(DbgNode).getContext());
806 if (isLexicalBlockFile())
807 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext());
810 return DIScopeRef(DINameSpace(DbgNode).getContext());
812 assert((isFile() || isCompileUnit()) && "Unhandled type of scope.");
813 return DIScopeRef(nullptr);
816 // If the scope node has a name, return that, else return an empty string.
817 StringRef DIScope::getName() const {
819 return DIType(DbgNode).getName();
821 return DISubprogram(DbgNode).getName();
823 return DINameSpace(DbgNode).getName();
824 assert((isLexicalBlock() || isLexicalBlockFile() || isFile() ||
826 "Unhandled type of scope.");
830 StringRef DIScope::getFilename() const {
833 return ::getStringField(getNodeField(DbgNode, 1), 0);
836 StringRef DIScope::getDirectory() const {
839 return ::getStringField(getNodeField(DbgNode, 1), 1);
842 DIArray DICompileUnit::getEnumTypes() const {
843 if (!DbgNode || DbgNode->getNumOperands() < 13)
846 return DIArray(getNodeField(DbgNode, 7));
849 DIArray DICompileUnit::getRetainedTypes() const {
850 if (!DbgNode || DbgNode->getNumOperands() < 13)
853 return DIArray(getNodeField(DbgNode, 8));
856 DIArray DICompileUnit::getSubprograms() const {
857 if (!DbgNode || DbgNode->getNumOperands() < 13)
860 return DIArray(getNodeField(DbgNode, 9));
863 DIArray DICompileUnit::getGlobalVariables() const {
864 if (!DbgNode || DbgNode->getNumOperands() < 13)
867 return DIArray(getNodeField(DbgNode, 10));
870 DIArray DICompileUnit::getImportedEntities() const {
871 if (!DbgNode || DbgNode->getNumOperands() < 13)
874 return DIArray(getNodeField(DbgNode, 11));
877 /// copyWithNewScope - Return a copy of this location, replacing the
878 /// current scope with the given one.
879 DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
880 DILexicalBlock NewScope) {
881 SmallVector<Value *, 10> Elts;
883 for (unsigned I = 0; I < DbgNode->getNumOperands(); ++I) {
885 Elts.push_back(DbgNode->getOperand(I));
887 Elts.push_back(NewScope);
889 MDNode *NewDIL = MDNode::get(Ctx, Elts);
890 return DILocation(NewDIL);
893 /// computeNewDiscriminator - Generate a new discriminator value for this
894 /// file and line location.
895 unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
896 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
897 return ++Ctx.pImpl->DiscriminatorTable[Key];
900 /// fixupSubprogramName - Replace contains special characters used
901 /// in a typical Objective-C names with '.' in a given string.
902 static void fixupSubprogramName(DISubprogram Fn, SmallVectorImpl<char> &Out) {
904 Fn.getFunction() ? Fn.getFunction()->getName() : Fn.getName();
905 FName = Function::getRealLinkageName(FName);
907 StringRef Prefix("llvm.dbg.lv.");
908 Out.reserve(FName.size() + Prefix.size());
909 Out.append(Prefix.begin(), Prefix.end());
911 bool isObjCLike = false;
912 for (size_t i = 0, e = FName.size(); i < e; ++i) {
917 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
918 C == '+' || C == '(' || C == ')'))
925 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
926 /// suitable to hold function specific information.
927 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
928 SmallString<32> Name;
929 fixupSubprogramName(Fn, Name);
930 return M.getNamedMetadata(Name.str());
933 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
934 /// to hold function specific information.
935 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
936 SmallString<32> Name;
937 fixupSubprogramName(Fn, Name);
938 return M.getOrInsertNamedMetadata(Name.str());
941 /// createInlinedVariable - Create a new inlined variable based on current
943 /// @param DV Current Variable.
944 /// @param InlinedScope Location at current variable is inlined.
945 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
946 LLVMContext &VMContext) {
947 SmallVector<Value *, 16> Elts;
948 // Insert inlined scope as 7th element.
949 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
950 i == 7 ? Elts.push_back(InlinedScope) : Elts.push_back(DV->getOperand(i));
951 return DIVariable(MDNode::get(VMContext, Elts));
954 /// cleanseInlinedVariable - Remove inlined scope from the variable.
955 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
956 SmallVector<Value *, 16> Elts;
957 // Insert inlined scope as 7th element.
958 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
959 i == 7 ? Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)))
960 : Elts.push_back(DV->getOperand(i));
961 return DIVariable(MDNode::get(VMContext, Elts));
965 /// getEntireVariable - Remove OpPiece exprs from the variable.
966 DIVariable llvm::getEntireVariable(DIVariable DV) {
967 if (!DV.isVariablePiece())
970 SmallVector<Value *, 8> Elts;
971 for (unsigned i = 0; i < 8; ++i)
972 Elts.push_back(DV->getOperand(i));
974 return DIVariable(MDNode::get(DV->getContext(), Elts));
977 /// getDISubprogram - Find subprogram that is enclosing this scope.
978 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
979 DIDescriptor D(Scope);
980 if (D.isSubprogram())
981 return DISubprogram(Scope);
983 if (D.isLexicalBlockFile())
984 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
986 if (D.isLexicalBlock())
987 return getDISubprogram(DILexicalBlock(Scope).getContext());
989 return DISubprogram();
992 /// getDICompositeType - Find underlying composite type.
993 DICompositeType llvm::getDICompositeType(DIType T) {
994 if (T.isCompositeType())
995 return DICompositeType(T);
997 if (T.isDerivedType()) {
998 // This function is currently used by dragonegg and dragonegg does
999 // not generate identifier for types, so using an empty map to resolve
1000 // DerivedFrom should be fine.
1001 DITypeIdentifierMap EmptyMap;
1002 return getDICompositeType(
1003 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap));
1006 return DICompositeType();
1009 /// Update DITypeIdentifierMap by going through retained types of each CU.
1011 llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
1012 DITypeIdentifierMap Map;
1013 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
1014 DICompileUnit CU(CU_Nodes->getOperand(CUi));
1015 DIArray Retain = CU.getRetainedTypes();
1016 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
1017 if (!Retain.getElement(Ti).isCompositeType())
1019 DICompositeType Ty(Retain.getElement(Ti));
1020 if (MDString *TypeId = Ty.getIdentifier()) {
1021 // Definition has priority over declaration.
1022 // Try to insert (TypeId, Ty) to Map.
1023 std::pair<DITypeIdentifierMap::iterator, bool> P =
1024 Map.insert(std::make_pair(TypeId, Ty));
1025 // If TypeId already exists in Map and this is a definition, replace
1026 // whatever we had (declaration or definition) with the definition.
1027 if (!P.second && !Ty.isForwardDecl())
1028 P.first->second = Ty;
1035 //===----------------------------------------------------------------------===//
1036 // DebugInfoFinder implementations.
1037 //===----------------------------------------------------------------------===//
1039 void DebugInfoFinder::reset() {
1046 TypeIdentifierMap.clear();
1047 TypeMapInitialized = false;
1050 void DebugInfoFinder::InitializeTypeMap(const Module &M) {
1051 if (!TypeMapInitialized)
1052 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
1053 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
1054 TypeMapInitialized = true;
1058 /// processModule - Process entire module and collect debug info.
1059 void DebugInfoFinder::processModule(const Module &M) {
1060 InitializeTypeMap(M);
1061 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
1062 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
1063 DICompileUnit CU(CU_Nodes->getOperand(i));
1065 DIArray GVs = CU.getGlobalVariables();
1066 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
1067 DIGlobalVariable DIG(GVs.getElement(i));
1068 if (addGlobalVariable(DIG)) {
1069 processScope(DIG.getContext());
1070 processType(DIG.getType().resolve(TypeIdentifierMap));
1073 DIArray SPs = CU.getSubprograms();
1074 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
1075 processSubprogram(DISubprogram(SPs.getElement(i)));
1076 DIArray EnumTypes = CU.getEnumTypes();
1077 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
1078 processType(DIType(EnumTypes.getElement(i)));
1079 DIArray RetainedTypes = CU.getRetainedTypes();
1080 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
1081 processType(DIType(RetainedTypes.getElement(i)));
1082 DIArray Imports = CU.getImportedEntities();
1083 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
1084 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i));
1085 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
1086 if (Entity.isType())
1087 processType(DIType(Entity));
1088 else if (Entity.isSubprogram())
1089 processSubprogram(DISubprogram(Entity));
1090 else if (Entity.isNameSpace())
1091 processScope(DINameSpace(Entity).getContext());
1097 /// processLocation - Process DILocation.
1098 void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
1101 InitializeTypeMap(M);
1102 processScope(Loc.getScope());
1103 processLocation(M, Loc.getOrigLocation());
1106 /// processType - Process DIType.
1107 void DebugInfoFinder::processType(DIType DT) {
1110 processScope(DT.getContext().resolve(TypeIdentifierMap));
1111 if (DT.isCompositeType()) {
1112 DICompositeType DCT(DT);
1113 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
1114 if (DT.isSubroutineType()) {
1115 DITypeArray DTA = DISubroutineType(DT).getTypeArray();
1116 for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i)
1117 processType(DTA.getElement(i).resolve(TypeIdentifierMap));
1120 DIArray DA = DCT.getElements();
1121 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1122 DIDescriptor D = DA.getElement(i);
1124 processType(DIType(D));
1125 else if (D.isSubprogram())
1126 processSubprogram(DISubprogram(D));
1128 } else if (DT.isDerivedType()) {
1129 DIDerivedType DDT(DT);
1130 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
1134 void DebugInfoFinder::processScope(DIScope Scope) {
1135 if (Scope.isType()) {
1140 if (Scope.isCompileUnit()) {
1141 addCompileUnit(DICompileUnit(Scope));
1144 if (Scope.isSubprogram()) {
1145 processSubprogram(DISubprogram(Scope));
1148 if (!addScope(Scope))
1150 if (Scope.isLexicalBlock()) {
1151 DILexicalBlock LB(Scope);
1152 processScope(LB.getContext());
1153 } else if (Scope.isLexicalBlockFile()) {
1154 DILexicalBlockFile LBF = DILexicalBlockFile(Scope);
1155 processScope(LBF.getScope());
1156 } else if (Scope.isNameSpace()) {
1157 DINameSpace NS(Scope);
1158 processScope(NS.getContext());
1162 /// processSubprogram - Process DISubprogram.
1163 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1164 if (!addSubprogram(SP))
1166 processScope(SP.getContext().resolve(TypeIdentifierMap));
1167 processType(SP.getType());
1168 DIArray TParams = SP.getTemplateParams();
1169 for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
1170 DIDescriptor Element = TParams.getElement(I);
1171 if (Element.isTemplateTypeParameter()) {
1172 DITemplateTypeParameter TType(Element);
1173 processScope(TType.getContext().resolve(TypeIdentifierMap));
1174 processType(TType.getType().resolve(TypeIdentifierMap));
1175 } else if (Element.isTemplateValueParameter()) {
1176 DITemplateValueParameter TVal(Element);
1177 processScope(TVal.getContext().resolve(TypeIdentifierMap));
1178 processType(TVal.getType().resolve(TypeIdentifierMap));
1183 /// processDeclare - Process DbgDeclareInst.
1184 void DebugInfoFinder::processDeclare(const Module &M,
1185 const DbgDeclareInst *DDI) {
1186 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1189 InitializeTypeMap(M);
1192 if (!DV.isVariable())
1195 if (!NodesSeen.insert(DV))
1197 processScope(DIVariable(N).getContext());
1198 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
1201 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
1202 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
1205 InitializeTypeMap(M);
1208 if (!DV.isVariable())
1211 if (!NodesSeen.insert(DV))
1213 processScope(DIVariable(N).getContext());
1214 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
1217 /// addType - Add type into Tys.
1218 bool DebugInfoFinder::addType(DIType DT) {
1222 if (!NodesSeen.insert(DT))
1229 /// addCompileUnit - Add compile unit into CUs.
1230 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1233 if (!NodesSeen.insert(CU))
1240 /// addGlobalVariable - Add global variable into GVs.
1241 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1245 if (!NodesSeen.insert(DIG))
1252 // addSubprogram - Add subprgoram into SPs.
1253 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1257 if (!NodesSeen.insert(SP))
1264 bool DebugInfoFinder::addScope(DIScope Scope) {
1267 // FIXME: Ocaml binding generates a scope with no content, we treat it
1269 if (Scope->getNumOperands() == 0)
1271 if (!NodesSeen.insert(Scope))
1273 Scopes.push_back(Scope);
1277 //===----------------------------------------------------------------------===//
1278 // DIDescriptor: dump routines for all descriptors.
1279 //===----------------------------------------------------------------------===//
1281 /// dump - Print descriptor to dbgs() with a newline.
1282 void DIDescriptor::dump() const {
1287 /// print - Print descriptor.
1288 void DIDescriptor::print(raw_ostream &OS) const {
1292 if (const char *Tag = dwarf::TagString(getTag()))
1293 OS << "[ " << Tag << " ]";
1295 if (this->isSubrange()) {
1296 DISubrange(DbgNode).printInternal(OS);
1297 } else if (this->isCompileUnit()) {
1298 DICompileUnit(DbgNode).printInternal(OS);
1299 } else if (this->isFile()) {
1300 DIFile(DbgNode).printInternal(OS);
1301 } else if (this->isEnumerator()) {
1302 DIEnumerator(DbgNode).printInternal(OS);
1303 } else if (this->isBasicType()) {
1304 DIType(DbgNode).printInternal(OS);
1305 } else if (this->isDerivedType()) {
1306 DIDerivedType(DbgNode).printInternal(OS);
1307 } else if (this->isCompositeType()) {
1308 DICompositeType(DbgNode).printInternal(OS);
1309 } else if (this->isSubprogram()) {
1310 DISubprogram(DbgNode).printInternal(OS);
1311 } else if (this->isGlobalVariable()) {
1312 DIGlobalVariable(DbgNode).printInternal(OS);
1313 } else if (this->isVariable()) {
1314 DIVariable(DbgNode).printInternal(OS);
1315 } else if (this->isObjCProperty()) {
1316 DIObjCProperty(DbgNode).printInternal(OS);
1317 } else if (this->isNameSpace()) {
1318 DINameSpace(DbgNode).printInternal(OS);
1319 } else if (this->isScope()) {
1320 DIScope(DbgNode).printInternal(OS);
1324 void DISubrange::printInternal(raw_ostream &OS) const {
1325 int64_t Count = getCount();
1327 OS << " [" << getLo() << ", " << Count - 1 << ']';
1329 OS << " [unbounded]";
1332 void DIScope::printInternal(raw_ostream &OS) const {
1333 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1336 void DICompileUnit::printInternal(raw_ostream &OS) const {
1337 DIScope::printInternal(OS);
1339 unsigned Lang = getLanguage();
1340 if (const char *LangStr = dwarf::LanguageString(Lang))
1343 (OS << "lang 0x").write_hex(Lang);
1347 void DIEnumerator::printInternal(raw_ostream &OS) const {
1348 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1351 void DIType::printInternal(raw_ostream &OS) const {
1355 StringRef Res = getName();
1357 OS << " [" << Res << "]";
1359 // TODO: Print context?
1361 OS << " [line " << getLineNumber() << ", size " << getSizeInBits()
1362 << ", align " << getAlignInBits() << ", offset " << getOffsetInBits();
1364 if (const char *Enc =
1365 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1366 OS << ", enc " << Enc;
1371 else if (isProtected())
1372 OS << " [protected]";
1375 OS << " [artificial]";
1377 if (isForwardDecl())
1379 else if (getTag() == dwarf::DW_TAG_structure_type ||
1380 getTag() == dwarf::DW_TAG_union_type ||
1381 getTag() == dwarf::DW_TAG_enumeration_type ||
1382 getTag() == dwarf::DW_TAG_class_type)
1386 if (isStaticMember())
1389 if (isLValueReference())
1390 OS << " [reference]";
1392 if (isRValueReference())
1393 OS << " [rvalue reference]";
1396 void DIDerivedType::printInternal(raw_ostream &OS) const {
1397 DIType::printInternal(OS);
1398 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1401 void DICompositeType::printInternal(raw_ostream &OS) const {
1402 DIType::printInternal(OS);
1403 DIArray A = getElements();
1404 OS << " [" << A.getNumElements() << " elements]";
1407 void DINameSpace::printInternal(raw_ostream &OS) const {
1408 StringRef Name = getName();
1410 OS << " [" << Name << ']';
1412 OS << " [line " << getLineNumber() << ']';
1415 void DISubprogram::printInternal(raw_ostream &OS) const {
1416 // TODO : Print context
1417 OS << " [line " << getLineNumber() << ']';
1419 if (isLocalToUnit())
1425 if (getScopeLineNumber() != getLineNumber())
1426 OS << " [scope " << getScopeLineNumber() << "]";
1430 else if (isProtected())
1431 OS << " [protected]";
1433 if (isLValueReference())
1434 OS << " [reference]";
1436 if (isRValueReference())
1437 OS << " [rvalue reference]";
1439 StringRef Res = getName();
1441 OS << " [" << Res << ']';
1444 void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1445 StringRef Res = getName();
1447 OS << " [" << Res << ']';
1449 OS << " [line " << getLineNumber() << ']';
1451 // TODO : Print context
1453 if (isLocalToUnit())
1460 void DIVariable::printInternal(raw_ostream &OS) const {
1461 StringRef Res = getName();
1463 OS << " [" << Res << ']';
1465 OS << " [line " << getLineNumber() << ']';
1467 if (isVariablePiece())
1468 OS << " [piece, size " << getPieceSize()
1469 << ", offset " << getPieceOffset() << ']';
1472 void DIObjCProperty::printInternal(raw_ostream &OS) const {
1473 StringRef Name = getObjCPropertyName();
1475 OS << " [" << Name << ']';
1477 OS << " [line " << getLineNumber() << ", properties " << getUnsignedField(6)
1481 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1482 const LLVMContext &Ctx) {
1483 if (!DL.isUnknown()) { // Print source line info.
1484 DIScope Scope(DL.getScope(Ctx));
1485 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
1486 // Omit the directory, because it's likely to be long and uninteresting.
1487 CommentOS << Scope.getFilename();
1488 CommentOS << ':' << DL.getLine();
1489 if (DL.getCol() != 0)
1490 CommentOS << ':' << DL.getCol();
1491 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1492 if (!InlinedAtDL.isUnknown()) {
1493 CommentOS << " @[ ";
1494 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1500 void DIVariable::printExtendedName(raw_ostream &OS) const {
1501 const LLVMContext &Ctx = DbgNode->getContext();
1502 StringRef Res = getName();
1504 OS << Res << "," << getLineNumber();
1505 if (MDNode *InlinedAt = getInlinedAt()) {
1506 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1507 if (!InlinedAtDL.isUnknown()) {
1509 printDebugLoc(InlinedAtDL, OS, Ctx);
1515 /// Specialize constructor to make sure it has the correct type.
1516 template <> DIRef<DIScope>::DIRef(const Value *V) : Val(V) {
1517 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
1519 template <> DIRef<DIType>::DIRef(const Value *V) : Val(V) {
1520 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
1523 /// Specialize getFieldAs to handle fields that are references to DIScopes.
1525 DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
1526 return DIScopeRef(getField(DbgNode, Elt));
1528 /// Specialize getFieldAs to handle fields that are references to DITypes.
1529 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
1530 return DITypeRef(getField(DbgNode, Elt));
1533 /// Strip debug info in the module if it exists.
1534 /// To do this, we remove all calls to the debugger intrinsics and any named
1535 /// metadata for debugging. We also remove debug locations for instructions.
1536 /// Return true if module is modified.
1537 bool llvm::StripDebugInfo(Module &M) {
1539 bool Changed = false;
1541 // Remove all of the calls to the debugger intrinsics, and remove them from
1543 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
1544 while (!Declare->use_empty()) {
1545 CallInst *CI = cast<CallInst>(Declare->user_back());
1546 CI->eraseFromParent();
1548 Declare->eraseFromParent();
1552 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
1553 while (!DbgVal->use_empty()) {
1554 CallInst *CI = cast<CallInst>(DbgVal->user_back());
1555 CI->eraseFromParent();
1557 DbgVal->eraseFromParent();
1561 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
1562 NME = M.named_metadata_end(); NMI != NME;) {
1563 NamedMDNode *NMD = NMI;
1565 if (NMD->getName().startswith("llvm.dbg.")) {
1566 NMD->eraseFromParent();
1571 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
1572 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
1574 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
1576 if (!BI->getDebugLoc().isUnknown()) {
1578 BI->setDebugLoc(DebugLoc());
1585 /// Return Debug Info Metadata Version by checking module flags.
1586 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
1587 Value *Val = M.getModuleFlag("Debug Info Version");
1590 return cast<ConstantInt>(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));