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/DerivedTypes.h"
23 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/IntrinsicInst.h"
25 #include "llvm/IR/Intrinsics.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/IR/ValueHandle.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/Dwarf.h"
30 #include "llvm/Support/raw_ostream.h"
32 using namespace llvm::dwarf;
34 //===----------------------------------------------------------------------===//
36 //===----------------------------------------------------------------------===//
38 bool DIDescriptor::Verify() const {
40 (DIDerivedType(DbgNode).Verify() ||
41 DICompositeType(DbgNode).Verify() || DIBasicType(DbgNode).Verify() ||
42 DIVariable(DbgNode).Verify() || DISubprogram(DbgNode).Verify() ||
43 DIGlobalVariable(DbgNode).Verify() || DIFile(DbgNode).Verify() ||
44 DICompileUnit(DbgNode).Verify() || DINameSpace(DbgNode).Verify() ||
45 DILexicalBlock(DbgNode).Verify() ||
46 DILexicalBlockFile(DbgNode).Verify() ||
47 DISubrange(DbgNode).Verify() || DIEnumerator(DbgNode).Verify() ||
48 DIObjCProperty(DbgNode).Verify() ||
49 DIUnspecifiedParameter(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 //===----------------------------------------------------------------------===//
156 //===----------------------------------------------------------------------===//
158 /// isBasicType - Return true if the specified tag is legal for
160 bool DIDescriptor::isBasicType() const {
164 case dwarf::DW_TAG_base_type:
165 case dwarf::DW_TAG_unspecified_type:
172 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
173 bool DIDescriptor::isDerivedType() const {
177 case dwarf::DW_TAG_typedef:
178 case dwarf::DW_TAG_pointer_type:
179 case dwarf::DW_TAG_ptr_to_member_type:
180 case dwarf::DW_TAG_reference_type:
181 case dwarf::DW_TAG_rvalue_reference_type:
182 case dwarf::DW_TAG_const_type:
183 case dwarf::DW_TAG_volatile_type:
184 case dwarf::DW_TAG_restrict_type:
185 case dwarf::DW_TAG_member:
186 case dwarf::DW_TAG_inheritance:
187 case dwarf::DW_TAG_friend:
190 // CompositeTypes are currently modelled as DerivedTypes.
191 return isCompositeType();
195 /// isCompositeType - Return true if the specified tag is legal for
197 bool DIDescriptor::isCompositeType() const {
201 case dwarf::DW_TAG_array_type:
202 case dwarf::DW_TAG_structure_type:
203 case dwarf::DW_TAG_union_type:
204 case dwarf::DW_TAG_enumeration_type:
205 case dwarf::DW_TAG_subroutine_type:
206 case dwarf::DW_TAG_class_type:
213 /// isVariable - Return true if the specified tag is legal for DIVariable.
214 bool DIDescriptor::isVariable() const {
218 case dwarf::DW_TAG_auto_variable:
219 case dwarf::DW_TAG_arg_variable:
226 /// isType - Return true if the specified tag is legal for DIType.
227 bool DIDescriptor::isType() const {
228 return isBasicType() || isCompositeType() || isDerivedType();
231 /// isSubprogram - Return true if the specified tag is legal for
233 bool DIDescriptor::isSubprogram() const {
234 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
237 /// isGlobalVariable - Return true if the specified tag is legal for
238 /// DIGlobalVariable.
239 bool DIDescriptor::isGlobalVariable() const {
240 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
241 getTag() == dwarf::DW_TAG_constant);
244 /// isUnspecifiedParmeter - Return true if the specified tag is
245 /// DW_TAG_unspecified_parameters.
246 bool DIDescriptor::isUnspecifiedParameter() const {
247 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
250 /// isScope - Return true if the specified tag is one of the scope
252 bool DIDescriptor::isScope() const {
256 case dwarf::DW_TAG_compile_unit:
257 case dwarf::DW_TAG_lexical_block:
258 case dwarf::DW_TAG_subprogram:
259 case dwarf::DW_TAG_namespace:
260 case dwarf::DW_TAG_file_type:
268 /// isTemplateTypeParameter - Return true if the specified tag is
269 /// DW_TAG_template_type_parameter.
270 bool DIDescriptor::isTemplateTypeParameter() const {
271 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
274 /// isTemplateValueParameter - Return true if the specified tag is
275 /// DW_TAG_template_value_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 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
283 bool DIDescriptor::isCompileUnit() const {
284 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
287 /// isFile - Return true if the specified tag is DW_TAG_file_type.
288 bool DIDescriptor::isFile() const {
289 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
292 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
293 bool DIDescriptor::isNameSpace() const {
294 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
297 /// isLexicalBlockFile - Return true if the specified descriptor is a
298 /// lexical block with an extra file.
299 bool DIDescriptor::isLexicalBlockFile() const {
300 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
301 (DbgNode->getNumOperands() == 3);
304 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
305 bool DIDescriptor::isLexicalBlock() const {
306 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
307 (DbgNode->getNumOperands() > 3);
310 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
311 bool DIDescriptor::isSubrange() const {
312 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
315 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
316 bool DIDescriptor::isEnumerator() const {
317 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
320 /// isObjCProperty - Return true if the specified tag is DW_TAG_APPLE_property.
321 bool DIDescriptor::isObjCProperty() const {
322 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
325 /// \brief Return true if the specified tag is DW_TAG_imported_module or
326 /// DW_TAG_imported_declaration.
327 bool DIDescriptor::isImportedEntity() const {
328 return DbgNode && (getTag() == dwarf::DW_TAG_imported_module ||
329 getTag() == dwarf::DW_TAG_imported_declaration);
332 //===----------------------------------------------------------------------===//
333 // Simple Descriptor Constructors and other Methods
334 //===----------------------------------------------------------------------===//
336 unsigned DIArray::getNumElements() const {
339 return DbgNode->getNumOperands();
342 /// replaceAllUsesWith - Replace all uses of the MDNode used by this
343 /// type with the one in the passed descriptor.
344 void DIType::replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D) {
346 assert(DbgNode && "Trying to replace an unverified type!");
348 // Since we use a TrackingVH for the node, its easy for clients to manufacture
349 // legitimate situations where they want to replaceAllUsesWith() on something
350 // which, due to uniquing, has merged with the source. We shield clients from
351 // this detail by allowing a value to be replaced with replaceAllUsesWith()
353 const MDNode *DN = D;
355 SmallVector<Value*, 10> Ops(DbgNode->getNumOperands());
356 for (size_t i = 0; i != Ops.size(); ++i)
357 Ops[i] = DbgNode->getOperand(i);
358 DN = MDNode::get(VMContext, Ops);
361 MDNode *Node = const_cast<MDNode *>(DbgNode);
362 const Value *V = cast_or_null<Value>(DN);
363 Node->replaceAllUsesWith(const_cast<Value *>(V));
364 MDNode::deleteTemporary(Node);
368 /// replaceAllUsesWith - Replace all uses of the MDNode used by this
369 /// type with the one in D.
370 void DIType::replaceAllUsesWith(MDNode *D) {
372 assert(DbgNode && "Trying to replace an unverified type!");
373 assert(DbgNode != D && "This replacement should always happen");
374 MDNode *Node = const_cast<MDNode *>(DbgNode);
375 const MDNode *DN = D;
376 const Value *V = cast_or_null<Value>(DN);
377 Node->replaceAllUsesWith(const_cast<Value *>(V));
378 MDNode::deleteTemporary(Node);
381 /// Verify - Verify that a compile unit is well formed.
382 bool DICompileUnit::Verify() const {
383 if (!isCompileUnit())
386 // Don't bother verifying the compilation directory or producer string
387 // as those could be empty.
388 if (getFilename().empty())
391 return DbgNode->getNumOperands() == 14;
394 /// Verify - Verify that an ObjC property is well formed.
395 bool DIObjCProperty::Verify() const {
396 if (!isObjCProperty())
399 // Don't worry about the rest of the strings for now.
400 return DbgNode->getNumOperands() == 8;
403 /// Check if a field at position Elt of a MDNode is a MDNode.
404 /// We currently allow an empty string and an integer.
405 /// But we don't allow a non-empty string in a MDNode field.
406 static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) {
407 // FIXME: This function should return true, if the field is null or the field
408 // is indeed a MDNode: return !Fld || isa<MDNode>(Fld).
409 Value *Fld = getField(DbgNode, Elt);
410 if (Fld && isa<MDString>(Fld) && !cast<MDString>(Fld)->getString().empty())
415 /// Check if a field at position Elt of a MDNode is a MDString.
416 static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) {
417 Value *Fld = getField(DbgNode, Elt);
418 return !Fld || isa<MDString>(Fld);
421 /// Check if a value can be a reference to a type.
422 static bool isTypeRef(const Value *Val) {
424 (isa<MDString>(Val) && !cast<MDString>(Val)->getString().empty()) ||
425 (isa<MDNode>(Val) && DIType(cast<MDNode>(Val)).isType());
428 /// Check if a field at position Elt of a MDNode can be a reference to a type.
429 static bool fieldIsTypeRef(const MDNode *DbgNode, unsigned Elt) {
430 Value *Fld = getField(DbgNode, Elt);
431 return isTypeRef(Fld);
434 /// Check if a value can be a ScopeRef.
435 static bool isScopeRef(const Value *Val) {
437 (isa<MDString>(Val) && !cast<MDString>(Val)->getString().empty()) ||
438 // Not checking for Val->isScope() here, because it would work
439 // only for lexical scopes and not all subclasses of DIScope.
443 /// Check if a field at position Elt of a MDNode can be a ScopeRef.
444 static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) {
445 Value *Fld = getField(DbgNode, Elt);
446 return isScopeRef(Fld);
449 /// Verify - Verify that a type descriptor is well formed.
450 bool DIType::Verify() const {
453 // Make sure Context @ field 2 is MDNode.
454 if (!fieldIsScopeRef(DbgNode, 2))
457 // FIXME: Sink this into the various subclass verifies.
458 uint16_t Tag = getTag();
459 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
460 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
461 Tag != dwarf::DW_TAG_ptr_to_member_type &&
462 Tag != dwarf::DW_TAG_reference_type &&
463 Tag != dwarf::DW_TAG_rvalue_reference_type &&
464 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_array_type &&
465 Tag != dwarf::DW_TAG_enumeration_type &&
466 Tag != dwarf::DW_TAG_subroutine_type &&
467 Tag != dwarf::DW_TAG_inheritance && Tag != dwarf::DW_TAG_friend &&
468 getFilename().empty())
470 // DIType is abstract, it should be a BasicType, a DerivedType or
473 return DIBasicType(DbgNode).Verify();
474 else if (isCompositeType())
475 return DICompositeType(DbgNode).Verify();
476 else if (isDerivedType())
477 return DIDerivedType(DbgNode).Verify();
482 /// Verify - Verify that a basic type descriptor is well formed.
483 bool DIBasicType::Verify() const {
484 return isBasicType() && DbgNode->getNumOperands() == 10;
487 /// Verify - Verify that a derived type descriptor is well formed.
488 bool DIDerivedType::Verify() const {
489 // Make sure DerivedFrom @ field 9 is TypeRef.
490 if (!fieldIsTypeRef(DbgNode, 9))
492 if (getTag() == dwarf::DW_TAG_ptr_to_member_type)
493 // Make sure ClassType @ field 10 is a TypeRef.
494 if (!fieldIsTypeRef(DbgNode, 10))
497 return isDerivedType() && DbgNode->getNumOperands() >= 10 &&
498 DbgNode->getNumOperands() <= 14;
501 /// Verify - Verify that a composite type descriptor is well formed.
502 bool DICompositeType::Verify() const {
503 if (!isCompositeType())
506 // Make sure DerivedFrom @ field 9 and ContainingType @ field 12 are TypeRef.
507 if (!fieldIsTypeRef(DbgNode, 9))
509 if (!fieldIsTypeRef(DbgNode, 12))
512 // Make sure the type identifier at field 14 is MDString, it can be null.
513 if (!fieldIsMDString(DbgNode, 14))
516 // A subroutine type can't be both & and &&.
517 if (isLValueReference() && isRValueReference())
520 return DbgNode->getNumOperands() == 15;
523 /// Verify - Verify that a subprogram descriptor is well formed.
524 bool DISubprogram::Verify() const {
528 // Make sure context @ field 2 is a ScopeRef and type @ field 7 is a MDNode.
529 if (!fieldIsScopeRef(DbgNode, 2))
531 if (!fieldIsMDNode(DbgNode, 7))
533 // Containing type @ field 12.
534 if (!fieldIsTypeRef(DbgNode, 12))
537 // A subprogram can't be both & and &&.
538 if (isLValueReference() && isRValueReference())
541 return DbgNode->getNumOperands() == 20;
544 /// Verify - Verify that a global variable descriptor is well formed.
545 bool DIGlobalVariable::Verify() const {
546 if (!isGlobalVariable())
549 if (getDisplayName().empty())
551 // Make sure context @ field 2 is an MDNode.
552 if (!fieldIsMDNode(DbgNode, 2))
554 // Make sure that type @ field 8 is a DITypeRef.
555 if (!fieldIsTypeRef(DbgNode, 8))
557 // Make sure StaticDataMemberDeclaration @ field 12 is MDNode.
558 if (!fieldIsMDNode(DbgNode, 12))
561 return DbgNode->getNumOperands() == 13;
564 /// Verify - Verify that a variable descriptor is well formed.
565 bool DIVariable::Verify() const {
569 // Make sure context @ field 1 is an MDNode.
570 if (!fieldIsMDNode(DbgNode, 1))
572 // Make sure that type @ field 5 is a DITypeRef.
573 if (!fieldIsTypeRef(DbgNode, 5))
576 // Variable without a complex expression.
577 if (DbgNode->getNumOperands() == 8)
580 // Make sure the complex expression is an MDNode.
581 return (DbgNode->getNumOperands() == 9 && fieldIsMDNode(DbgNode, 8));
584 /// Verify - Verify that a location descriptor is well formed.
585 bool DILocation::Verify() const {
589 return DbgNode->getNumOperands() == 4;
592 /// Verify - Verify that a namespace descriptor is well formed.
593 bool DINameSpace::Verify() const {
596 return DbgNode->getNumOperands() == 5;
599 /// \brief Retrieve the MDNode for the directory/file pair.
600 MDNode *DIFile::getFileNode() const { return getNodeField(DbgNode, 1); }
602 /// \brief Verify that the file descriptor is well formed.
603 bool DIFile::Verify() const {
604 return isFile() && DbgNode->getNumOperands() == 2;
607 /// \brief Verify that the enumerator descriptor is well formed.
608 bool DIEnumerator::Verify() const {
609 return isEnumerator() && DbgNode->getNumOperands() == 3;
612 /// \brief Verify that the subrange descriptor is well formed.
613 bool DISubrange::Verify() const {
614 return isSubrange() && DbgNode->getNumOperands() == 3;
617 /// \brief Verify that the lexical block descriptor is well formed.
618 bool DILexicalBlock::Verify() const {
619 return isLexicalBlock() && DbgNode->getNumOperands() == 7;
622 /// \brief Verify that the file-scoped lexical block descriptor is well formed.
623 bool DILexicalBlockFile::Verify() const {
624 return isLexicalBlockFile() && DbgNode->getNumOperands() == 3;
627 /// \brief Verify that an unspecified parameter descriptor is well formed.
628 bool DIUnspecifiedParameter::Verify() const {
629 return isUnspecifiedParameter() && DbgNode->getNumOperands() == 1;
632 /// \brief Verify that the template type parameter descriptor is well formed.
633 bool DITemplateTypeParameter::Verify() const {
634 return isTemplateTypeParameter() && DbgNode->getNumOperands() == 7;
637 /// \brief Verify that the template value parameter descriptor is well formed.
638 bool DITemplateValueParameter::Verify() const {
639 return isTemplateValueParameter() && DbgNode->getNumOperands() == 8;
642 /// \brief Verify that the imported module descriptor is well formed.
643 bool DIImportedEntity::Verify() const {
644 return isImportedEntity() &&
645 (DbgNode->getNumOperands() == 4 || DbgNode->getNumOperands() == 5);
648 /// getObjCProperty - Return property node, if this ivar is associated with one.
649 MDNode *DIDerivedType::getObjCProperty() const {
650 return getNodeField(DbgNode, 10);
653 MDString *DICompositeType::getIdentifier() const {
654 return cast_or_null<MDString>(getField(DbgNode, 14));
658 static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) {
659 for (unsigned i = 0; i != LHS->getNumOperands(); ++i) {
660 // Skip the 'empty' list (that's a single i32 0, rather than truly empty).
661 if (i == 0 && isa<ConstantInt>(LHS->getOperand(i)))
663 const MDNode *E = cast<MDNode>(LHS->getOperand(i));
665 for (unsigned j = 0; !found && j != RHS->getNumOperands(); ++j)
666 found = E == RHS->getOperand(j);
667 assert(found && "Losing a member during member list replacement");
672 /// \brief Set the array of member DITypes.
673 void DICompositeType::setTypeArray(DIArray Elements, DIArray TParams) {
674 assert((!TParams || DbgNode->getNumOperands() == 15) &&
675 "If you're setting the template parameters this should include a slot "
677 TrackingVH<MDNode> N(*this);
680 // Check that the new list of members contains all the old members as well.
681 if (const MDNode *El = cast_or_null<MDNode>(N->getOperand(10)))
682 VerifySubsetOf(El, Elements);
684 N->replaceOperandWith(10, Elements);
687 N->replaceOperandWith(13, TParams);
691 /// Generate a reference to this DIType. Uses the type identifier instead
692 /// of the actual MDNode if possible, to help type uniquing.
693 DIScopeRef DIScope::getRef() const {
694 if (!isCompositeType())
695 return DIScopeRef(*this);
696 DICompositeType DTy(DbgNode);
697 if (!DTy.getIdentifier())
698 return DIScopeRef(*this);
699 return DIScopeRef(DTy.getIdentifier());
702 /// \brief Set the containing type.
703 void DICompositeType::setContainingType(DICompositeType ContainingType) {
704 TrackingVH<MDNode> N(*this);
705 N->replaceOperandWith(12, ContainingType.getRef());
709 /// isInlinedFnArgument - Return true if this variable provides debugging
710 /// information for an inlined function arguments.
711 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
712 assert(CurFn && "Invalid function");
713 if (!getContext().isSubprogram())
715 // This variable is not inlined function argument if its scope
716 // does not describe current function.
717 return !DISubprogram(getContext()).describes(CurFn);
720 /// describes - Return true if this subprogram provides debugging
721 /// information for the function F.
722 bool DISubprogram::describes(const Function *F) {
723 assert(F && "Invalid function");
724 if (F == getFunction())
726 StringRef Name = getLinkageName();
729 if (F->getName() == Name)
734 unsigned DISubprogram::isOptimized() const {
735 assert(DbgNode && "Invalid subprogram descriptor!");
736 if (DbgNode->getNumOperands() == 15)
737 return getUnsignedField(14);
741 MDNode *DISubprogram::getVariablesNodes() const {
742 return getNodeField(DbgNode, 18);
745 DIArray DISubprogram::getVariables() const {
746 return DIArray(getNodeField(DbgNode, 18));
749 Value *DITemplateValueParameter::getValue() const {
750 return getField(DbgNode, 4);
753 // If the current node has a parent scope then return that,
754 // else return an empty scope.
755 DIScopeRef DIScope::getContext() const {
758 return DIType(DbgNode).getContext();
761 return DIScopeRef(DISubprogram(DbgNode).getContext());
763 if (isLexicalBlock())
764 return DIScopeRef(DILexicalBlock(DbgNode).getContext());
766 if (isLexicalBlockFile())
767 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext());
770 return DIScopeRef(DINameSpace(DbgNode).getContext());
772 assert((isFile() || isCompileUnit()) && "Unhandled type of scope.");
773 return DIScopeRef(nullptr);
776 // If the scope node has a name, return that, else return an empty string.
777 StringRef DIScope::getName() const {
779 return DIType(DbgNode).getName();
781 return DISubprogram(DbgNode).getName();
783 return DINameSpace(DbgNode).getName();
784 assert((isLexicalBlock() || isLexicalBlockFile() || isFile() ||
786 "Unhandled type of scope.");
790 StringRef DIScope::getFilename() const {
793 return ::getStringField(getNodeField(DbgNode, 1), 0);
796 StringRef DIScope::getDirectory() const {
799 return ::getStringField(getNodeField(DbgNode, 1), 1);
802 DIArray DICompileUnit::getEnumTypes() const {
803 if (!DbgNode || DbgNode->getNumOperands() < 13)
806 return DIArray(getNodeField(DbgNode, 7));
809 DIArray DICompileUnit::getRetainedTypes() const {
810 if (!DbgNode || DbgNode->getNumOperands() < 13)
813 return DIArray(getNodeField(DbgNode, 8));
816 DIArray DICompileUnit::getSubprograms() const {
817 if (!DbgNode || DbgNode->getNumOperands() < 13)
820 return DIArray(getNodeField(DbgNode, 9));
823 DIArray DICompileUnit::getGlobalVariables() const {
824 if (!DbgNode || DbgNode->getNumOperands() < 13)
827 return DIArray(getNodeField(DbgNode, 10));
830 DIArray DICompileUnit::getImportedEntities() const {
831 if (!DbgNode || DbgNode->getNumOperands() < 13)
834 return DIArray(getNodeField(DbgNode, 11));
837 /// copyWithNewScope - Return a copy of this location, replacing the
838 /// current scope with the given one.
839 DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
840 DILexicalBlock NewScope) {
841 SmallVector<Value *, 10> Elts;
843 for (unsigned I = 0; I < DbgNode->getNumOperands(); ++I) {
845 Elts.push_back(DbgNode->getOperand(I));
847 Elts.push_back(NewScope);
849 MDNode *NewDIL = MDNode::get(Ctx, Elts);
850 return DILocation(NewDIL);
853 /// computeNewDiscriminator - Generate a new discriminator value for this
854 /// file and line location.
855 unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
856 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
857 return ++Ctx.pImpl->DiscriminatorTable[Key];
860 /// fixupSubprogramName - Replace contains special characters used
861 /// in a typical Objective-C names with '.' in a given string.
862 static void fixupSubprogramName(DISubprogram Fn, SmallVectorImpl<char> &Out) {
864 Fn.getFunction() ? Fn.getFunction()->getName() : Fn.getName();
865 FName = Function::getRealLinkageName(FName);
867 StringRef Prefix("llvm.dbg.lv.");
868 Out.reserve(FName.size() + Prefix.size());
869 Out.append(Prefix.begin(), Prefix.end());
871 bool isObjCLike = false;
872 for (size_t i = 0, e = FName.size(); i < e; ++i) {
877 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
878 C == '+' || C == '(' || C == ')'))
885 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
886 /// suitable to hold function specific information.
887 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
888 SmallString<32> Name;
889 fixupSubprogramName(Fn, Name);
890 return M.getNamedMetadata(Name.str());
893 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
894 /// to hold function specific information.
895 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
896 SmallString<32> Name;
897 fixupSubprogramName(Fn, Name);
898 return M.getOrInsertNamedMetadata(Name.str());
901 /// createInlinedVariable - Create a new inlined variable based on current
903 /// @param DV Current Variable.
904 /// @param InlinedScope Location at current variable is inlined.
905 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
906 LLVMContext &VMContext) {
907 SmallVector<Value *, 16> Elts;
908 // Insert inlined scope as 7th element.
909 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
910 i == 7 ? Elts.push_back(InlinedScope) : Elts.push_back(DV->getOperand(i));
911 return DIVariable(MDNode::get(VMContext, Elts));
914 /// cleanseInlinedVariable - Remove inlined scope from the variable.
915 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
916 SmallVector<Value *, 16> Elts;
917 // Insert inlined scope as 7th element.
918 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
919 i == 7 ? Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)))
920 : Elts.push_back(DV->getOperand(i));
921 return DIVariable(MDNode::get(VMContext, Elts));
924 /// getDISubprogram - Find subprogram that is enclosing this scope.
925 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
926 DIDescriptor D(Scope);
927 if (D.isSubprogram())
928 return DISubprogram(Scope);
930 if (D.isLexicalBlockFile())
931 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
933 if (D.isLexicalBlock())
934 return getDISubprogram(DILexicalBlock(Scope).getContext());
936 return DISubprogram();
939 /// getDICompositeType - Find underlying composite type.
940 DICompositeType llvm::getDICompositeType(DIType T) {
941 if (T.isCompositeType())
942 return DICompositeType(T);
944 if (T.isDerivedType()) {
945 // This function is currently used by dragonegg and dragonegg does
946 // not generate identifier for types, so using an empty map to resolve
947 // DerivedFrom should be fine.
948 DITypeIdentifierMap EmptyMap;
949 return getDICompositeType(
950 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap));
953 return DICompositeType();
956 /// Update DITypeIdentifierMap by going through retained types of each CU.
958 llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
959 DITypeIdentifierMap Map;
960 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
961 DICompileUnit CU(CU_Nodes->getOperand(CUi));
962 DIArray Retain = CU.getRetainedTypes();
963 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
964 if (!Retain.getElement(Ti).isCompositeType())
966 DICompositeType Ty(Retain.getElement(Ti));
967 if (MDString *TypeId = Ty.getIdentifier()) {
968 // Definition has priority over declaration.
969 // Try to insert (TypeId, Ty) to Map.
970 std::pair<DITypeIdentifierMap::iterator, bool> P =
971 Map.insert(std::make_pair(TypeId, Ty));
972 // If TypeId already exists in Map and this is a definition, replace
973 // whatever we had (declaration or definition) with the definition.
974 if (!P.second && !Ty.isForwardDecl())
975 P.first->second = Ty;
982 //===----------------------------------------------------------------------===//
983 // DebugInfoFinder implementations.
984 //===----------------------------------------------------------------------===//
986 void DebugInfoFinder::reset() {
993 TypeIdentifierMap.clear();
994 TypeMapInitialized = false;
997 void DebugInfoFinder::InitializeTypeMap(const Module &M) {
998 if (!TypeMapInitialized)
999 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
1000 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
1001 TypeMapInitialized = true;
1005 /// processModule - Process entire module and collect debug info.
1006 void DebugInfoFinder::processModule(const Module &M) {
1007 InitializeTypeMap(M);
1008 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
1009 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
1010 DICompileUnit CU(CU_Nodes->getOperand(i));
1012 DIArray GVs = CU.getGlobalVariables();
1013 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
1014 DIGlobalVariable DIG(GVs.getElement(i));
1015 if (addGlobalVariable(DIG)) {
1016 processScope(DIG.getContext());
1017 processType(DIG.getType().resolve(TypeIdentifierMap));
1020 DIArray SPs = CU.getSubprograms();
1021 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
1022 processSubprogram(DISubprogram(SPs.getElement(i)));
1023 DIArray EnumTypes = CU.getEnumTypes();
1024 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
1025 processType(DIType(EnumTypes.getElement(i)));
1026 DIArray RetainedTypes = CU.getRetainedTypes();
1027 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
1028 processType(DIType(RetainedTypes.getElement(i)));
1029 DIArray Imports = CU.getImportedEntities();
1030 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
1031 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i));
1032 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
1033 if (Entity.isType())
1034 processType(DIType(Entity));
1035 else if (Entity.isSubprogram())
1036 processSubprogram(DISubprogram(Entity));
1037 else if (Entity.isNameSpace())
1038 processScope(DINameSpace(Entity).getContext());
1044 /// processLocation - Process DILocation.
1045 void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
1048 InitializeTypeMap(M);
1049 processScope(Loc.getScope());
1050 processLocation(M, Loc.getOrigLocation());
1053 /// processType - Process DIType.
1054 void DebugInfoFinder::processType(DIType DT) {
1057 processScope(DT.getContext().resolve(TypeIdentifierMap));
1058 if (DT.isCompositeType()) {
1059 DICompositeType DCT(DT);
1060 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
1061 DIArray DA = DCT.getTypeArray();
1062 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1063 DIDescriptor D = DA.getElement(i);
1065 processType(DIType(D));
1066 else if (D.isSubprogram())
1067 processSubprogram(DISubprogram(D));
1069 } else if (DT.isDerivedType()) {
1070 DIDerivedType DDT(DT);
1071 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
1075 void DebugInfoFinder::processScope(DIScope Scope) {
1076 if (Scope.isType()) {
1081 if (Scope.isCompileUnit()) {
1082 addCompileUnit(DICompileUnit(Scope));
1085 if (Scope.isSubprogram()) {
1086 processSubprogram(DISubprogram(Scope));
1089 if (!addScope(Scope))
1091 if (Scope.isLexicalBlock()) {
1092 DILexicalBlock LB(Scope);
1093 processScope(LB.getContext());
1094 } else if (Scope.isLexicalBlockFile()) {
1095 DILexicalBlockFile LBF = DILexicalBlockFile(Scope);
1096 processScope(LBF.getScope());
1097 } else if (Scope.isNameSpace()) {
1098 DINameSpace NS(Scope);
1099 processScope(NS.getContext());
1103 /// processSubprogram - Process DISubprogram.
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 /// processDeclare - Process DbgDeclareInst.
1125 void DebugInfoFinder::processDeclare(const Module &M,
1126 const DbgDeclareInst *DDI) {
1127 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1130 InitializeTypeMap(M);
1133 if (!DV.isVariable())
1136 if (!NodesSeen.insert(DV))
1138 processScope(DIVariable(N).getContext());
1139 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
1142 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
1143 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
1146 InitializeTypeMap(M);
1149 if (!DV.isVariable())
1152 if (!NodesSeen.insert(DV))
1154 processScope(DIVariable(N).getContext());
1155 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
1158 /// addType - Add type into Tys.
1159 bool DebugInfoFinder::addType(DIType DT) {
1163 if (!NodesSeen.insert(DT))
1170 /// addCompileUnit - Add compile unit into CUs.
1171 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1174 if (!NodesSeen.insert(CU))
1181 /// addGlobalVariable - Add global variable into GVs.
1182 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1186 if (!NodesSeen.insert(DIG))
1193 // addSubprogram - Add subprgoram into SPs.
1194 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1198 if (!NodesSeen.insert(SP))
1205 bool DebugInfoFinder::addScope(DIScope Scope) {
1208 // FIXME: Ocaml binding generates a scope with no content, we treat it
1210 if (Scope->getNumOperands() == 0)
1212 if (!NodesSeen.insert(Scope))
1214 Scopes.push_back(Scope);
1218 //===----------------------------------------------------------------------===//
1219 // DIDescriptor: dump routines for all descriptors.
1220 //===----------------------------------------------------------------------===//
1222 /// dump - Print descriptor to dbgs() with a newline.
1223 void DIDescriptor::dump() const {
1228 /// print - Print descriptor.
1229 void DIDescriptor::print(raw_ostream &OS) const {
1233 if (const char *Tag = dwarf::TagString(getTag()))
1234 OS << "[ " << Tag << " ]";
1236 if (this->isSubrange()) {
1237 DISubrange(DbgNode).printInternal(OS);
1238 } else if (this->isCompileUnit()) {
1239 DICompileUnit(DbgNode).printInternal(OS);
1240 } else if (this->isFile()) {
1241 DIFile(DbgNode).printInternal(OS);
1242 } else if (this->isEnumerator()) {
1243 DIEnumerator(DbgNode).printInternal(OS);
1244 } else if (this->isBasicType()) {
1245 DIType(DbgNode).printInternal(OS);
1246 } else if (this->isDerivedType()) {
1247 DIDerivedType(DbgNode).printInternal(OS);
1248 } else if (this->isCompositeType()) {
1249 DICompositeType(DbgNode).printInternal(OS);
1250 } else if (this->isSubprogram()) {
1251 DISubprogram(DbgNode).printInternal(OS);
1252 } else if (this->isGlobalVariable()) {
1253 DIGlobalVariable(DbgNode).printInternal(OS);
1254 } else if (this->isVariable()) {
1255 DIVariable(DbgNode).printInternal(OS);
1256 } else if (this->isObjCProperty()) {
1257 DIObjCProperty(DbgNode).printInternal(OS);
1258 } else if (this->isNameSpace()) {
1259 DINameSpace(DbgNode).printInternal(OS);
1260 } else if (this->isScope()) {
1261 DIScope(DbgNode).printInternal(OS);
1265 void DISubrange::printInternal(raw_ostream &OS) const {
1266 int64_t Count = getCount();
1268 OS << " [" << getLo() << ", " << Count - 1 << ']';
1270 OS << " [unbounded]";
1273 void DIScope::printInternal(raw_ostream &OS) const {
1274 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1277 void DICompileUnit::printInternal(raw_ostream &OS) const {
1278 DIScope::printInternal(OS);
1280 unsigned Lang = getLanguage();
1281 if (const char *LangStr = dwarf::LanguageString(Lang))
1284 (OS << "lang 0x").write_hex(Lang);
1288 void DIEnumerator::printInternal(raw_ostream &OS) const {
1289 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1292 void DIType::printInternal(raw_ostream &OS) const {
1296 StringRef Res = getName();
1298 OS << " [" << Res << "]";
1300 // TODO: Print context?
1302 OS << " [line " << getLineNumber() << ", size " << getSizeInBits()
1303 << ", align " << getAlignInBits() << ", offset " << getOffsetInBits();
1305 if (const char *Enc =
1306 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1307 OS << ", enc " << Enc;
1312 else if (isProtected())
1313 OS << " [protected]";
1316 OS << " [artificial]";
1318 if (isForwardDecl())
1320 else if (getTag() == dwarf::DW_TAG_structure_type ||
1321 getTag() == dwarf::DW_TAG_union_type ||
1322 getTag() == dwarf::DW_TAG_enumeration_type ||
1323 getTag() == dwarf::DW_TAG_class_type)
1327 if (isStaticMember())
1330 if (isLValueReference())
1331 OS << " [reference]";
1333 if (isRValueReference())
1334 OS << " [rvalue reference]";
1337 void DIDerivedType::printInternal(raw_ostream &OS) const {
1338 DIType::printInternal(OS);
1339 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1342 void DICompositeType::printInternal(raw_ostream &OS) const {
1343 DIType::printInternal(OS);
1344 DIArray A = getTypeArray();
1345 OS << " [" << A.getNumElements() << " elements]";
1348 void DINameSpace::printInternal(raw_ostream &OS) const {
1349 StringRef Name = getName();
1351 OS << " [" << Name << ']';
1353 OS << " [line " << getLineNumber() << ']';
1356 void DISubprogram::printInternal(raw_ostream &OS) const {
1357 // TODO : Print context
1358 OS << " [line " << getLineNumber() << ']';
1360 if (isLocalToUnit())
1366 if (getScopeLineNumber() != getLineNumber())
1367 OS << " [scope " << getScopeLineNumber() << "]";
1371 else if (isProtected())
1372 OS << " [protected]";
1374 if (isLValueReference())
1375 OS << " [reference]";
1377 if (isRValueReference())
1378 OS << " [rvalue reference]";
1380 StringRef Res = getName();
1382 OS << " [" << Res << ']';
1385 void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1386 StringRef Res = getName();
1388 OS << " [" << Res << ']';
1390 OS << " [line " << getLineNumber() << ']';
1392 // TODO : Print context
1394 if (isLocalToUnit())
1401 void DIVariable::printInternal(raw_ostream &OS) const {
1402 StringRef Res = getName();
1404 OS << " [" << Res << ']';
1406 OS << " [line " << getLineNumber() << ']';
1409 void DIObjCProperty::printInternal(raw_ostream &OS) const {
1410 StringRef Name = getObjCPropertyName();
1412 OS << " [" << Name << ']';
1414 OS << " [line " << getLineNumber() << ", properties " << getUnsignedField(6)
1418 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1419 const LLVMContext &Ctx) {
1420 if (!DL.isUnknown()) { // Print source line info.
1421 DIScope Scope(DL.getScope(Ctx));
1422 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
1423 // Omit the directory, because it's likely to be long and uninteresting.
1424 CommentOS << Scope.getFilename();
1425 CommentOS << ':' << DL.getLine();
1426 if (DL.getCol() != 0)
1427 CommentOS << ':' << DL.getCol();
1428 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1429 if (!InlinedAtDL.isUnknown()) {
1430 CommentOS << " @[ ";
1431 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1437 void DIVariable::printExtendedName(raw_ostream &OS) const {
1438 const LLVMContext &Ctx = DbgNode->getContext();
1439 StringRef Res = getName();
1441 OS << Res << "," << getLineNumber();
1442 if (MDNode *InlinedAt = getInlinedAt()) {
1443 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1444 if (!InlinedAtDL.isUnknown()) {
1446 printDebugLoc(InlinedAtDL, OS, Ctx);
1452 /// Specialize constructor to make sure it has the correct type.
1453 template <> DIRef<DIScope>::DIRef(const Value *V) : Val(V) {
1454 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
1456 template <> DIRef<DIType>::DIRef(const Value *V) : Val(V) {
1457 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
1460 /// Specialize getFieldAs to handle fields that are references to DIScopes.
1462 DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
1463 return DIScopeRef(getField(DbgNode, Elt));
1465 /// Specialize getFieldAs to handle fields that are references to DITypes.
1466 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
1467 return DITypeRef(getField(DbgNode, Elt));
1470 /// Strip debug info in the module if it exists.
1471 /// To do this, we remove all calls to the debugger intrinsics and any named
1472 /// metadata for debugging. We also remove debug locations for instructions.
1473 /// Return true if module is modified.
1474 bool llvm::StripDebugInfo(Module &M) {
1476 bool Changed = false;
1478 // Remove all of the calls to the debugger intrinsics, and remove them from
1480 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
1481 while (!Declare->use_empty()) {
1482 CallInst *CI = cast<CallInst>(Declare->user_back());
1483 CI->eraseFromParent();
1485 Declare->eraseFromParent();
1489 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
1490 while (!DbgVal->use_empty()) {
1491 CallInst *CI = cast<CallInst>(DbgVal->user_back());
1492 CI->eraseFromParent();
1494 DbgVal->eraseFromParent();
1498 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
1499 NME = M.named_metadata_end(); NMI != NME;) {
1500 NamedMDNode *NMD = NMI;
1502 if (NMD->getName().startswith("llvm.dbg.")) {
1503 NMD->eraseFromParent();
1508 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
1509 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
1511 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
1513 if (!BI->getDebugLoc().isUnknown()) {
1515 BI->setDebugLoc(DebugLoc());
1522 /// Return Debug Info Metadata Version by checking module flags.
1523 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
1524 Value *Val = M.getModuleFlag("Debug Info Version");
1527 return cast<ConstantInt>(Val)->getZExtValue();
1530 llvm::DenseMap<llvm::Function *, llvm::DISubprogram> llvm::makeSubprogramMap(Module &M) {
1531 DenseMap<Function *, DISubprogram> R;
1533 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
1537 for (MDNode *N : CU_Nodes->operands()) {
1538 DICompileUnit CUNode(N);
1539 DIArray SPs = CUNode.getSubprograms();
1540 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
1541 DISubprogram SP(SPs.getElement(i));
1542 if (Function *F = SP.getFunction())
1543 R.insert(std::make_pair(F, SP));