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 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 static unsigned DIVariableInlinedAtIndex = 4;
142 MDNode *DIVariable::getInlinedAt() const {
143 return getNodeField(DbgNode, DIVariableInlinedAtIndex);
146 /// Return the size reported by the variable's type.
147 unsigned DIVariable::getSizeInBits(const DITypeIdentifierMap &Map) {
148 DIType Ty = getType().resolve(Map);
149 // Follow derived types until we reach a type that
150 // reports back a size.
151 while (Ty.isDerivedType() && !Ty.getSizeInBits()) {
152 DIDerivedType DT(&*Ty);
153 Ty = DT.getTypeDerivedFrom().resolve(Map);
155 assert(Ty.getSizeInBits() && "type with size 0");
156 return Ty.getSizeInBits();
159 uint64_t DIExpression::getElement(unsigned Idx) const {
160 unsigned I = Idx + 1;
161 assert(I < getNumHeaderFields() &&
162 "non-existing complex address element requested");
163 return getHeaderFieldAs<int64_t>(I);
166 bool DIExpression::isVariablePiece() const {
167 return getNumElements() && getElement(0) == dwarf::DW_OP_piece;
170 uint64_t DIExpression::getPieceOffset() const {
171 assert(isVariablePiece());
172 return getElement(1);
175 uint64_t DIExpression::getPieceSize() const {
176 assert(isVariablePiece());
177 return getElement(2);
180 //===----------------------------------------------------------------------===//
182 //===----------------------------------------------------------------------===//
184 bool DIDescriptor::isSubroutineType() const {
185 return isCompositeType() && getTag() == dwarf::DW_TAG_subroutine_type;
188 /// isBasicType - Return true if the specified tag is legal for
190 bool DIDescriptor::isBasicType() const {
194 case dwarf::DW_TAG_base_type:
195 case dwarf::DW_TAG_unspecified_type:
202 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
203 bool DIDescriptor::isDerivedType() const {
207 case dwarf::DW_TAG_typedef:
208 case dwarf::DW_TAG_pointer_type:
209 case dwarf::DW_TAG_ptr_to_member_type:
210 case dwarf::DW_TAG_reference_type:
211 case dwarf::DW_TAG_rvalue_reference_type:
212 case dwarf::DW_TAG_const_type:
213 case dwarf::DW_TAG_volatile_type:
214 case dwarf::DW_TAG_restrict_type:
215 case dwarf::DW_TAG_member:
216 case dwarf::DW_TAG_inheritance:
217 case dwarf::DW_TAG_friend:
220 // CompositeTypes are currently modelled as DerivedTypes.
221 return isCompositeType();
225 /// isCompositeType - Return true if the specified tag is legal for
227 bool DIDescriptor::isCompositeType() const {
231 case dwarf::DW_TAG_array_type:
232 case dwarf::DW_TAG_structure_type:
233 case dwarf::DW_TAG_union_type:
234 case dwarf::DW_TAG_enumeration_type:
235 case dwarf::DW_TAG_subroutine_type:
236 case dwarf::DW_TAG_class_type:
243 /// isVariable - Return true if the specified tag is legal for DIVariable.
244 bool DIDescriptor::isVariable() const {
248 case dwarf::DW_TAG_auto_variable:
249 case dwarf::DW_TAG_arg_variable:
256 /// isType - Return true if the specified tag is legal for DIType.
257 bool DIDescriptor::isType() const {
258 return isBasicType() || isCompositeType() || isDerivedType();
261 /// isSubprogram - Return true if the specified tag is legal for
263 bool DIDescriptor::isSubprogram() const {
264 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
267 /// isGlobalVariable - Return true if the specified tag is legal for
268 /// DIGlobalVariable.
269 bool DIDescriptor::isGlobalVariable() const {
270 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
271 getTag() == dwarf::DW_TAG_constant);
274 /// isScope - Return true if the specified tag is one of the scope
276 bool DIDescriptor::isScope() const {
280 case dwarf::DW_TAG_compile_unit:
281 case dwarf::DW_TAG_lexical_block:
282 case dwarf::DW_TAG_subprogram:
283 case dwarf::DW_TAG_namespace:
284 case dwarf::DW_TAG_file_type:
292 /// isTemplateTypeParameter - Return true if the specified tag is
293 /// DW_TAG_template_type_parameter.
294 bool DIDescriptor::isTemplateTypeParameter() const {
295 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
298 /// isTemplateValueParameter - Return true if the specified tag is
299 /// DW_TAG_template_value_parameter.
300 bool DIDescriptor::isTemplateValueParameter() const {
301 return DbgNode && (getTag() == dwarf::DW_TAG_template_value_parameter ||
302 getTag() == dwarf::DW_TAG_GNU_template_template_param ||
303 getTag() == dwarf::DW_TAG_GNU_template_parameter_pack);
306 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
307 bool DIDescriptor::isCompileUnit() const {
308 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
311 /// isFile - Return true if the specified tag is DW_TAG_file_type.
312 bool DIDescriptor::isFile() const {
313 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
316 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
317 bool DIDescriptor::isNameSpace() const {
318 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
321 /// isLexicalBlockFile - Return true if the specified descriptor is a
322 /// lexical block with an extra file.
323 bool DIDescriptor::isLexicalBlockFile() const {
324 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
325 DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 2;
328 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
329 bool DIDescriptor::isLexicalBlock() const {
330 // FIXME: There are always exactly 4 header fields in DILexicalBlock, but
331 // something relies on this returning true for DILexicalBlockFile.
332 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
333 DbgNode->getNumOperands() == 3 &&
334 (getNumHeaderFields() == 2 || getNumHeaderFields() == 4);
337 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
338 bool DIDescriptor::isSubrange() const {
339 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
342 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
343 bool DIDescriptor::isEnumerator() const {
344 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
347 /// isObjCProperty - Return true if the specified tag is DW_TAG_APPLE_property.
348 bool DIDescriptor::isObjCProperty() const {
349 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
352 /// \brief Return true if the specified tag is DW_TAG_imported_module or
353 /// DW_TAG_imported_declaration.
354 bool DIDescriptor::isImportedEntity() const {
355 return DbgNode && (getTag() == dwarf::DW_TAG_imported_module ||
356 getTag() == dwarf::DW_TAG_imported_declaration);
359 /// \brief Return true if the specified tag is DW_TAG_imported_module or
360 /// DW_TAG_imported_declaration.
361 bool DIDescriptor::isExpression() const {
362 return DbgNode && (getTag() == dwarf::DW_TAG_expression);
365 //===----------------------------------------------------------------------===//
366 // Simple Descriptor Constructors and other Methods
367 //===----------------------------------------------------------------------===//
369 /// replaceAllUsesWith - Replace all uses of the MDNode used by this
370 /// type with the one in the passed descriptor.
371 void DIDescriptor::replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D) {
373 assert(DbgNode && "Trying to replace an unverified type!");
375 // Since we use a TrackingVH for the node, its easy for clients to manufacture
376 // legitimate situations where they want to replaceAllUsesWith() on something
377 // which, due to uniquing, has merged with the source. We shield clients from
378 // this detail by allowing a value to be replaced with replaceAllUsesWith()
380 const MDNode *DN = D;
382 SmallVector<Value*, 10> Ops(DbgNode->getNumOperands());
383 for (size_t i = 0; i != Ops.size(); ++i)
384 Ops[i] = DbgNode->getOperand(i);
385 DN = MDNode::get(VMContext, Ops);
388 MDNode *Node = const_cast<MDNode *>(DbgNode);
389 const Value *V = cast_or_null<Value>(DN);
390 Node->replaceAllUsesWith(const_cast<Value *>(V));
391 MDNode::deleteTemporary(Node);
395 /// replaceAllUsesWith - Replace all uses of the MDNode used by this
396 /// type with the one in D.
397 void DIDescriptor::replaceAllUsesWith(MDNode *D) {
399 assert(DbgNode && "Trying to replace an unverified type!");
400 assert(DbgNode != D && "This replacement should always happen");
401 MDNode *Node = const_cast<MDNode *>(DbgNode);
402 const MDNode *DN = D;
403 const Value *V = cast_or_null<Value>(DN);
404 Node->replaceAllUsesWith(const_cast<Value *>(V));
405 MDNode::deleteTemporary(Node);
408 /// Verify - Verify that a compile unit is well formed.
409 bool DICompileUnit::Verify() const {
410 if (!isCompileUnit())
413 // Don't bother verifying the compilation directory or producer string
414 // as those could be empty.
415 if (getFilename().empty())
418 return DbgNode->getNumOperands() == 7 && getNumHeaderFields() == 8;
421 /// Verify - Verify that an ObjC property is well formed.
422 bool DIObjCProperty::Verify() const {
423 if (!isObjCProperty())
426 // Don't worry about the rest of the strings for now.
427 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 6;
430 /// Check if a field at position Elt of a MDNode is a MDNode.
431 /// We currently allow an empty string and an integer.
432 /// But we don't allow a non-empty string in a MDNode field.
433 static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) {
434 // FIXME: This function should return true, if the field is null or the field
435 // is indeed a MDNode: return !Fld || isa<MDNode>(Fld).
436 Value *Fld = getField(DbgNode, Elt);
437 if (Fld && isa<MDString>(Fld) && !cast<MDString>(Fld)->getString().empty())
442 /// Check if a field at position Elt of a MDNode is a MDString.
443 static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) {
444 Value *Fld = getField(DbgNode, Elt);
445 return !Fld || isa<MDString>(Fld);
448 /// Check if a value can be a reference to a type.
449 static bool isTypeRef(const Value *Val) {
451 (isa<MDString>(Val) && !cast<MDString>(Val)->getString().empty()) ||
452 (isa<MDNode>(Val) && DIType(cast<MDNode>(Val)).isType());
455 /// Check if a field at position Elt of a MDNode can be a reference to a type.
456 static bool fieldIsTypeRef(const MDNode *DbgNode, unsigned Elt) {
457 Value *Fld = getField(DbgNode, Elt);
458 return isTypeRef(Fld);
461 /// Check if a value can be a ScopeRef.
462 static bool isScopeRef(const Value *Val) {
464 (isa<MDString>(Val) && !cast<MDString>(Val)->getString().empty()) ||
465 // Not checking for Val->isScope() here, because it would work
466 // only for lexical scopes and not all subclasses of DIScope.
470 /// Check if a field at position Elt of a MDNode can be a ScopeRef.
471 static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) {
472 Value *Fld = getField(DbgNode, Elt);
473 return isScopeRef(Fld);
476 /// Verify - Verify that a type descriptor is well formed.
477 bool DIType::Verify() const {
480 // Make sure Context @ field 2 is MDNode.
481 if (!fieldIsScopeRef(DbgNode, 2))
484 // FIXME: Sink this into the various subclass verifies.
485 uint16_t Tag = getTag();
486 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
487 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
488 Tag != dwarf::DW_TAG_ptr_to_member_type &&
489 Tag != dwarf::DW_TAG_reference_type &&
490 Tag != dwarf::DW_TAG_rvalue_reference_type &&
491 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_array_type &&
492 Tag != dwarf::DW_TAG_enumeration_type &&
493 Tag != dwarf::DW_TAG_subroutine_type &&
494 Tag != dwarf::DW_TAG_inheritance && Tag != dwarf::DW_TAG_friend &&
495 getFilename().empty())
498 // DIType is abstract, it should be a BasicType, a DerivedType or
501 return DIBasicType(DbgNode).Verify();
502 else if (isCompositeType())
503 return DICompositeType(DbgNode).Verify();
504 else if (isDerivedType())
505 return DIDerivedType(DbgNode).Verify();
510 /// Verify - Verify that a basic type descriptor is well formed.
511 bool DIBasicType::Verify() const {
512 return isBasicType() && DbgNode->getNumOperands() == 3 &&
513 getNumHeaderFields() == 8;
516 /// Verify - Verify that a derived type descriptor is well formed.
517 bool DIDerivedType::Verify() const {
518 // Make sure DerivedFrom @ field 3 is TypeRef.
519 if (!fieldIsTypeRef(DbgNode, 3))
521 if (getTag() == dwarf::DW_TAG_ptr_to_member_type)
522 // Make sure ClassType @ field 4 is a TypeRef.
523 if (!fieldIsTypeRef(DbgNode, 4))
526 return isDerivedType() && DbgNode->getNumOperands() >= 4 &&
527 DbgNode->getNumOperands() <= 8 && getNumHeaderFields() >= 7 &&
528 getNumHeaderFields() <= 8;
531 /// Verify - Verify that a composite type descriptor is well formed.
532 bool DICompositeType::Verify() const {
533 if (!isCompositeType())
536 // Make sure DerivedFrom @ field 3 and ContainingType @ field 5 are TypeRef.
537 if (!fieldIsTypeRef(DbgNode, 3))
539 if (!fieldIsTypeRef(DbgNode, 5))
542 // Make sure the type identifier at field 7 is MDString, it can be null.
543 if (!fieldIsMDString(DbgNode, 7))
546 // A subroutine type can't be both & and &&.
547 if (isLValueReference() && isRValueReference())
550 return DbgNode->getNumOperands() == 8 && getNumHeaderFields() == 8;
553 /// Verify - Verify that a subprogram descriptor is well formed.
554 bool DISubprogram::Verify() const {
558 // Make sure context @ field 2 is a ScopeRef and type @ field 3 is a MDNode.
559 if (!fieldIsScopeRef(DbgNode, 2))
561 if (!fieldIsMDNode(DbgNode, 3))
563 // Containing type @ field 4.
564 if (!fieldIsTypeRef(DbgNode, 4))
567 // A subprogram can't be both & and &&.
568 if (isLValueReference() && isRValueReference())
571 return DbgNode->getNumOperands() == 9 && getNumHeaderFields() == 12;
574 /// Verify - Verify that a global variable descriptor is well formed.
575 bool DIGlobalVariable::Verify() const {
576 if (!isGlobalVariable())
579 if (getDisplayName().empty())
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))
587 // Make sure StaticDataMemberDeclaration @ field 5 is MDNode.
588 if (!fieldIsMDNode(DbgNode, 5))
591 return DbgNode->getNumOperands() == 6 && getNumHeaderFields() == 7;
594 /// Verify - Verify that a variable descriptor is well formed.
595 bool DIVariable::Verify() const {
599 // Make sure context @ field 1 is an MDNode.
600 if (!fieldIsMDNode(DbgNode, 1))
602 // Make sure that type @ field 3 is a DITypeRef.
603 if (!fieldIsTypeRef(DbgNode, 3))
606 // Check the number of header fields, which is common between complex and
608 if (getNumHeaderFields() != 4)
611 // Variable without an inline location.
612 if (DbgNode->getNumOperands() == 4)
615 // Variable with an inline location.
616 return getInlinedAt() != nullptr && DbgNode->getNumOperands() == 5;
619 /// Verify - Verify that a variable descriptor is well formed.
620 bool DIExpression::Verify() const {
621 // Empty DIExpressions may be represented as a nullptr.
625 return isExpression() && DbgNode->getNumOperands() == 1;
628 /// Verify - Verify that a location descriptor is well formed.
629 bool DILocation::Verify() const {
633 return DbgNode->getNumOperands() == 4;
636 /// Verify - Verify that a namespace descriptor is well formed.
637 bool DINameSpace::Verify() const {
640 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 3;
643 /// \brief Retrieve the MDNode for the directory/file pair.
644 MDNode *DIFile::getFileNode() const { return getNodeField(DbgNode, 1); }
646 /// \brief Verify that the file descriptor is well formed.
647 bool DIFile::Verify() const {
648 return isFile() && DbgNode->getNumOperands() == 2;
651 /// \brief Verify that the enumerator descriptor is well formed.
652 bool DIEnumerator::Verify() const {
653 return isEnumerator() && DbgNode->getNumOperands() == 1 &&
654 getNumHeaderFields() == 3;
657 /// \brief Verify that the subrange descriptor is well formed.
658 bool DISubrange::Verify() const {
659 return isSubrange() && DbgNode->getNumOperands() == 1 &&
660 getNumHeaderFields() == 3;
663 /// \brief Verify that the lexical block descriptor is well formed.
664 bool DILexicalBlock::Verify() const {
665 return isLexicalBlock() && DbgNode->getNumOperands() == 3 &&
666 getNumHeaderFields() == 4;
669 /// \brief Verify that the file-scoped lexical block descriptor is well formed.
670 bool DILexicalBlockFile::Verify() const {
671 return isLexicalBlockFile() && DbgNode->getNumOperands() == 3 &&
672 getNumHeaderFields() == 2;
675 /// \brief Verify that the template type parameter descriptor is well formed.
676 bool DITemplateTypeParameter::Verify() const {
677 return isTemplateTypeParameter() && DbgNode->getNumOperands() == 4 &&
678 getNumHeaderFields() == 4;
681 /// \brief Verify that the template value parameter descriptor is well formed.
682 bool DITemplateValueParameter::Verify() const {
683 return isTemplateValueParameter() && DbgNode->getNumOperands() == 5 &&
684 getNumHeaderFields() == 4;
687 /// \brief Verify that the imported module descriptor is well formed.
688 bool DIImportedEntity::Verify() const {
689 return isImportedEntity() && DbgNode->getNumOperands() == 3 &&
690 getNumHeaderFields() == 3;
693 /// getObjCProperty - Return property node, if this ivar is associated with one.
694 MDNode *DIDerivedType::getObjCProperty() const {
695 return getNodeField(DbgNode, 4);
698 MDString *DICompositeType::getIdentifier() const {
699 return cast_or_null<MDString>(getField(DbgNode, 7));
703 static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) {
704 for (unsigned i = 0; i != LHS->getNumOperands(); ++i) {
705 // Skip the 'empty' list (that's a single i32 0, rather than truly empty).
706 if (i == 0 && isa<ConstantInt>(LHS->getOperand(i)))
708 const MDNode *E = cast<MDNode>(LHS->getOperand(i));
710 for (unsigned j = 0; !found && j != RHS->getNumOperands(); ++j)
711 found = E == RHS->getOperand(j);
712 assert(found && "Losing a member during member list replacement");
717 /// \brief Set the array of member DITypes.
718 void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) {
719 TrackingVH<MDNode> N(*this);
722 // Check that the new list of members contains all the old members as well.
723 if (const MDNode *El = cast_or_null<MDNode>(N->getOperand(4)))
724 VerifySubsetOf(El, Elements);
726 N->replaceOperandWith(4, Elements);
729 N->replaceOperandWith(6, TParams);
733 /// Generate a reference to this DIType. Uses the type identifier instead
734 /// of the actual MDNode if possible, to help type uniquing.
735 DIScopeRef DIScope::getRef() const {
736 if (!isCompositeType())
737 return DIScopeRef(*this);
738 DICompositeType DTy(DbgNode);
739 if (!DTy.getIdentifier())
740 return DIScopeRef(*this);
741 return DIScopeRef(DTy.getIdentifier());
744 /// \brief Set the containing type.
745 void DICompositeType::setContainingType(DICompositeType ContainingType) {
746 TrackingVH<MDNode> N(*this);
747 N->replaceOperandWith(5, ContainingType.getRef());
751 /// isInlinedFnArgument - Return true if this variable provides debugging
752 /// information for an inlined function arguments.
753 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
754 assert(CurFn && "Invalid function");
755 if (!getContext().isSubprogram())
757 // This variable is not inlined function argument if its scope
758 // does not describe current function.
759 return !DISubprogram(getContext()).describes(CurFn);
762 /// describes - Return true if this subprogram provides debugging
763 /// information for the function F.
764 bool DISubprogram::describes(const Function *F) {
765 assert(F && "Invalid function");
766 if (F == getFunction())
768 StringRef Name = getLinkageName();
771 if (F->getName() == Name)
776 MDNode *DISubprogram::getVariablesNodes() const {
777 return getNodeField(DbgNode, 8);
780 DIArray DISubprogram::getVariables() const {
781 return DIArray(getNodeField(DbgNode, 8));
784 Value *DITemplateValueParameter::getValue() const {
785 return getField(DbgNode, 3);
788 // If the current node has a parent scope then return that,
789 // else return an empty scope.
790 DIScopeRef DIScope::getContext() const {
793 return DIType(DbgNode).getContext();
796 return DIScopeRef(DISubprogram(DbgNode).getContext());
798 if (isLexicalBlock())
799 return DIScopeRef(DILexicalBlock(DbgNode).getContext());
801 if (isLexicalBlockFile())
802 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext());
805 return DIScopeRef(DINameSpace(DbgNode).getContext());
807 assert((isFile() || isCompileUnit()) && "Unhandled type of scope.");
808 return DIScopeRef(nullptr);
811 // If the scope node has a name, return that, else return an empty string.
812 StringRef DIScope::getName() const {
814 return DIType(DbgNode).getName();
816 return DISubprogram(DbgNode).getName();
818 return DINameSpace(DbgNode).getName();
819 assert((isLexicalBlock() || isLexicalBlockFile() || isFile() ||
821 "Unhandled type of scope.");
825 StringRef DIScope::getFilename() const {
828 return ::getStringField(getNodeField(DbgNode, 1), 0);
831 StringRef DIScope::getDirectory() const {
834 return ::getStringField(getNodeField(DbgNode, 1), 1);
837 DIArray DICompileUnit::getEnumTypes() const {
838 if (!DbgNode || DbgNode->getNumOperands() < 7)
841 return DIArray(getNodeField(DbgNode, 2));
844 DIArray DICompileUnit::getRetainedTypes() const {
845 if (!DbgNode || DbgNode->getNumOperands() < 7)
848 return DIArray(getNodeField(DbgNode, 3));
851 DIArray DICompileUnit::getSubprograms() const {
852 if (!DbgNode || DbgNode->getNumOperands() < 7)
855 return DIArray(getNodeField(DbgNode, 4));
858 DIArray DICompileUnit::getGlobalVariables() const {
859 if (!DbgNode || DbgNode->getNumOperands() < 7)
862 return DIArray(getNodeField(DbgNode, 5));
865 DIArray DICompileUnit::getImportedEntities() const {
866 if (!DbgNode || DbgNode->getNumOperands() < 7)
869 return DIArray(getNodeField(DbgNode, 6));
872 void DICompileUnit::replaceSubprograms(DIArray Subprograms) {
873 assert(Verify() && "Expected compile unit");
874 if (Subprograms == getSubprograms())
877 const_cast<MDNode *>(DbgNode)->replaceOperandWith(4, Subprograms);
880 void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {
881 assert(Verify() && "Expected compile unit");
882 if (GlobalVariables == getGlobalVariables())
885 const_cast<MDNode *>(DbgNode)->replaceOperandWith(5, GlobalVariables);
888 /// copyWithNewScope - Return a copy of this location, replacing the
889 /// current scope with the given one.
890 DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
891 DILexicalBlockFile NewScope) {
892 SmallVector<Value *, 10> Elts;
894 for (unsigned I = 0; I < DbgNode->getNumOperands(); ++I) {
896 Elts.push_back(DbgNode->getOperand(I));
898 Elts.push_back(NewScope);
900 MDNode *NewDIL = MDNode::get(Ctx, Elts);
901 return DILocation(NewDIL);
904 /// computeNewDiscriminator - Generate a new discriminator value for this
905 /// file and line location.
906 unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
907 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
908 return ++Ctx.pImpl->DiscriminatorTable[Key];
911 /// fixupSubprogramName - Replace contains special characters used
912 /// in a typical Objective-C names with '.' in a given string.
913 static void fixupSubprogramName(DISubprogram Fn, SmallVectorImpl<char> &Out) {
915 Fn.getFunction() ? Fn.getFunction()->getName() : Fn.getName();
916 FName = Function::getRealLinkageName(FName);
918 StringRef Prefix("llvm.dbg.lv.");
919 Out.reserve(FName.size() + Prefix.size());
920 Out.append(Prefix.begin(), Prefix.end());
922 bool isObjCLike = false;
923 for (size_t i = 0, e = FName.size(); i < e; ++i) {
928 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
929 C == '+' || C == '(' || C == ')'))
936 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
937 /// suitable to hold function specific information.
938 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
939 SmallString<32> Name;
940 fixupSubprogramName(Fn, Name);
941 return M.getNamedMetadata(Name.str());
944 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
945 /// to hold function specific information.
946 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
947 SmallString<32> Name;
948 fixupSubprogramName(Fn, Name);
949 return M.getOrInsertNamedMetadata(Name.str());
952 /// createInlinedVariable - Create a new inlined variable based on current
954 /// @param DV Current Variable.
955 /// @param InlinedScope Location at current variable is inlined.
956 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
957 LLVMContext &VMContext) {
958 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
960 return cleanseInlinedVariable(DV, VMContext);
962 // Insert inlined scope.
963 SmallVector<Value *, 8> Elts;
964 for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I)
965 Elts.push_back(DV->getOperand(I));
966 Elts.push_back(InlinedScope);
968 DIVariable Inlined(MDNode::get(VMContext, Elts));
969 assert(Inlined.Verify() && "Expected to create a DIVariable");
973 /// cleanseInlinedVariable - Remove inlined scope from the variable.
974 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
975 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
976 if (!DIVariable(DV).getInlinedAt())
977 return DIVariable(DV);
979 // Remove inlined scope.
980 SmallVector<Value *, 8> Elts;
981 for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I)
982 Elts.push_back(DV->getOperand(I));
984 DIVariable Cleansed(MDNode::get(VMContext, Elts));
985 assert(Cleansed.Verify() && "Expected to create a DIVariable");
989 /// getDISubprogram - Find subprogram that is enclosing this scope.
990 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
991 DIDescriptor D(Scope);
992 if (D.isSubprogram())
993 return DISubprogram(Scope);
995 if (D.isLexicalBlockFile())
996 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
998 if (D.isLexicalBlock())
999 return getDISubprogram(DILexicalBlock(Scope).getContext());
1001 return DISubprogram();
1004 /// getDICompositeType - Find underlying composite type.
1005 DICompositeType llvm::getDICompositeType(DIType T) {
1006 if (T.isCompositeType())
1007 return DICompositeType(T);
1009 if (T.isDerivedType()) {
1010 // This function is currently used by dragonegg and dragonegg does
1011 // not generate identifier for types, so using an empty map to resolve
1012 // DerivedFrom should be fine.
1013 DITypeIdentifierMap EmptyMap;
1014 return getDICompositeType(
1015 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap));
1018 return DICompositeType();
1021 /// Update DITypeIdentifierMap by going through retained types of each CU.
1023 llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
1024 DITypeIdentifierMap Map;
1025 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
1026 DICompileUnit CU(CU_Nodes->getOperand(CUi));
1027 DIArray Retain = CU.getRetainedTypes();
1028 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
1029 if (!Retain.getElement(Ti).isCompositeType())
1031 DICompositeType Ty(Retain.getElement(Ti));
1032 if (MDString *TypeId = Ty.getIdentifier()) {
1033 // Definition has priority over declaration.
1034 // Try to insert (TypeId, Ty) to Map.
1035 std::pair<DITypeIdentifierMap::iterator, bool> P =
1036 Map.insert(std::make_pair(TypeId, Ty));
1037 // If TypeId already exists in Map and this is a definition, replace
1038 // whatever we had (declaration or definition) with the definition.
1039 if (!P.second && !Ty.isForwardDecl())
1040 P.first->second = Ty;
1047 //===----------------------------------------------------------------------===//
1048 // DebugInfoFinder implementations.
1049 //===----------------------------------------------------------------------===//
1051 void DebugInfoFinder::reset() {
1058 TypeIdentifierMap.clear();
1059 TypeMapInitialized = false;
1062 void DebugInfoFinder::InitializeTypeMap(const Module &M) {
1063 if (!TypeMapInitialized)
1064 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
1065 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
1066 TypeMapInitialized = true;
1070 /// processModule - Process entire module and collect debug info.
1071 void DebugInfoFinder::processModule(const Module &M) {
1072 InitializeTypeMap(M);
1073 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
1074 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
1075 DICompileUnit CU(CU_Nodes->getOperand(i));
1077 DIArray GVs = CU.getGlobalVariables();
1078 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
1079 DIGlobalVariable DIG(GVs.getElement(i));
1080 if (addGlobalVariable(DIG)) {
1081 processScope(DIG.getContext());
1082 processType(DIG.getType().resolve(TypeIdentifierMap));
1085 DIArray SPs = CU.getSubprograms();
1086 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
1087 processSubprogram(DISubprogram(SPs.getElement(i)));
1088 DIArray EnumTypes = CU.getEnumTypes();
1089 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
1090 processType(DIType(EnumTypes.getElement(i)));
1091 DIArray RetainedTypes = CU.getRetainedTypes();
1092 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
1093 processType(DIType(RetainedTypes.getElement(i)));
1094 DIArray Imports = CU.getImportedEntities();
1095 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
1096 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i));
1097 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
1098 if (Entity.isType())
1099 processType(DIType(Entity));
1100 else if (Entity.isSubprogram())
1101 processSubprogram(DISubprogram(Entity));
1102 else if (Entity.isNameSpace())
1103 processScope(DINameSpace(Entity).getContext());
1109 /// processLocation - Process DILocation.
1110 void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
1113 InitializeTypeMap(M);
1114 processScope(Loc.getScope());
1115 processLocation(M, Loc.getOrigLocation());
1118 /// processType - Process DIType.
1119 void DebugInfoFinder::processType(DIType DT) {
1122 processScope(DT.getContext().resolve(TypeIdentifierMap));
1123 if (DT.isCompositeType()) {
1124 DICompositeType DCT(DT);
1125 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
1126 if (DT.isSubroutineType()) {
1127 DITypeArray DTA = DISubroutineType(DT).getTypeArray();
1128 for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i)
1129 processType(DTA.getElement(i).resolve(TypeIdentifierMap));
1132 DIArray DA = DCT.getElements();
1133 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1134 DIDescriptor D = DA.getElement(i);
1136 processType(DIType(D));
1137 else if (D.isSubprogram())
1138 processSubprogram(DISubprogram(D));
1140 } else if (DT.isDerivedType()) {
1141 DIDerivedType DDT(DT);
1142 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
1146 void DebugInfoFinder::processScope(DIScope Scope) {
1147 if (Scope.isType()) {
1152 if (Scope.isCompileUnit()) {
1153 addCompileUnit(DICompileUnit(Scope));
1156 if (Scope.isSubprogram()) {
1157 processSubprogram(DISubprogram(Scope));
1160 if (!addScope(Scope))
1162 if (Scope.isLexicalBlock()) {
1163 DILexicalBlock LB(Scope);
1164 processScope(LB.getContext());
1165 } else if (Scope.isLexicalBlockFile()) {
1166 DILexicalBlockFile LBF = DILexicalBlockFile(Scope);
1167 processScope(LBF.getScope());
1168 } else if (Scope.isNameSpace()) {
1169 DINameSpace NS(Scope);
1170 processScope(NS.getContext());
1174 /// processSubprogram - Process DISubprogram.
1175 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1176 if (!addSubprogram(SP))
1178 processScope(SP.getContext().resolve(TypeIdentifierMap));
1179 processType(SP.getType());
1180 DIArray TParams = SP.getTemplateParams();
1181 for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
1182 DIDescriptor Element = TParams.getElement(I);
1183 if (Element.isTemplateTypeParameter()) {
1184 DITemplateTypeParameter TType(Element);
1185 processScope(TType.getContext().resolve(TypeIdentifierMap));
1186 processType(TType.getType().resolve(TypeIdentifierMap));
1187 } else if (Element.isTemplateValueParameter()) {
1188 DITemplateValueParameter TVal(Element);
1189 processScope(TVal.getContext().resolve(TypeIdentifierMap));
1190 processType(TVal.getType().resolve(TypeIdentifierMap));
1195 /// processDeclare - Process DbgDeclareInst.
1196 void DebugInfoFinder::processDeclare(const Module &M,
1197 const DbgDeclareInst *DDI) {
1198 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1201 InitializeTypeMap(M);
1204 if (!DV.isVariable())
1207 if (!NodesSeen.insert(DV))
1209 processScope(DIVariable(N).getContext());
1210 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
1213 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
1214 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
1217 InitializeTypeMap(M);
1220 if (!DV.isVariable())
1223 if (!NodesSeen.insert(DV))
1225 processScope(DIVariable(N).getContext());
1226 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
1229 /// addType - Add type into Tys.
1230 bool DebugInfoFinder::addType(DIType DT) {
1234 if (!NodesSeen.insert(DT))
1241 /// addCompileUnit - Add compile unit into CUs.
1242 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1245 if (!NodesSeen.insert(CU))
1252 /// addGlobalVariable - Add global variable into GVs.
1253 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1257 if (!NodesSeen.insert(DIG))
1264 // addSubprogram - Add subprgoram into SPs.
1265 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1269 if (!NodesSeen.insert(SP))
1276 bool DebugInfoFinder::addScope(DIScope Scope) {
1279 // FIXME: Ocaml binding generates a scope with no content, we treat it
1281 if (Scope->getNumOperands() == 0)
1283 if (!NodesSeen.insert(Scope))
1285 Scopes.push_back(Scope);
1289 //===----------------------------------------------------------------------===//
1290 // DIDescriptor: dump routines for all descriptors.
1291 //===----------------------------------------------------------------------===//
1293 /// dump - Print descriptor to dbgs() with a newline.
1294 void DIDescriptor::dump() const {
1299 /// print - Print descriptor.
1300 void DIDescriptor::print(raw_ostream &OS) const {
1304 if (const char *Tag = dwarf::TagString(getTag()))
1305 OS << "[ " << Tag << " ]";
1307 if (this->isSubrange()) {
1308 DISubrange(DbgNode).printInternal(OS);
1309 } else if (this->isCompileUnit()) {
1310 DICompileUnit(DbgNode).printInternal(OS);
1311 } else if (this->isFile()) {
1312 DIFile(DbgNode).printInternal(OS);
1313 } else if (this->isEnumerator()) {
1314 DIEnumerator(DbgNode).printInternal(OS);
1315 } else if (this->isBasicType()) {
1316 DIType(DbgNode).printInternal(OS);
1317 } else if (this->isDerivedType()) {
1318 DIDerivedType(DbgNode).printInternal(OS);
1319 } else if (this->isCompositeType()) {
1320 DICompositeType(DbgNode).printInternal(OS);
1321 } else if (this->isSubprogram()) {
1322 DISubprogram(DbgNode).printInternal(OS);
1323 } else if (this->isGlobalVariable()) {
1324 DIGlobalVariable(DbgNode).printInternal(OS);
1325 } else if (this->isVariable()) {
1326 DIVariable(DbgNode).printInternal(OS);
1327 } else if (this->isObjCProperty()) {
1328 DIObjCProperty(DbgNode).printInternal(OS);
1329 } else if (this->isNameSpace()) {
1330 DINameSpace(DbgNode).printInternal(OS);
1331 } else if (this->isScope()) {
1332 DIScope(DbgNode).printInternal(OS);
1333 } else if (this->isExpression()) {
1334 DIExpression(DbgNode).printInternal(OS);
1338 void DISubrange::printInternal(raw_ostream &OS) const {
1339 int64_t Count = getCount();
1341 OS << " [" << getLo() << ", " << Count - 1 << ']';
1343 OS << " [unbounded]";
1346 void DIScope::printInternal(raw_ostream &OS) const {
1347 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1350 void DICompileUnit::printInternal(raw_ostream &OS) const {
1351 DIScope::printInternal(OS);
1353 unsigned Lang = getLanguage();
1354 if (const char *LangStr = dwarf::LanguageString(Lang))
1357 (OS << "lang 0x").write_hex(Lang);
1361 void DIEnumerator::printInternal(raw_ostream &OS) const {
1362 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1365 void DIType::printInternal(raw_ostream &OS) const {
1369 StringRef Res = getName();
1371 OS << " [" << Res << "]";
1373 // TODO: Print context?
1375 OS << " [line " << getLineNumber() << ", size " << getSizeInBits()
1376 << ", align " << getAlignInBits() << ", offset " << getOffsetInBits();
1378 if (const char *Enc =
1379 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1380 OS << ", enc " << Enc;
1385 else if (isProtected())
1386 OS << " [protected]";
1387 else if (isPublic())
1391 OS << " [artificial]";
1393 if (isForwardDecl())
1395 else if (getTag() == dwarf::DW_TAG_structure_type ||
1396 getTag() == dwarf::DW_TAG_union_type ||
1397 getTag() == dwarf::DW_TAG_enumeration_type ||
1398 getTag() == dwarf::DW_TAG_class_type)
1402 if (isStaticMember())
1405 if (isLValueReference())
1406 OS << " [reference]";
1408 if (isRValueReference())
1409 OS << " [rvalue reference]";
1412 void DIDerivedType::printInternal(raw_ostream &OS) const {
1413 DIType::printInternal(OS);
1414 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1417 void DICompositeType::printInternal(raw_ostream &OS) const {
1418 DIType::printInternal(OS);
1419 DIArray A = getElements();
1420 OS << " [" << A.getNumElements() << " elements]";
1423 void DINameSpace::printInternal(raw_ostream &OS) const {
1424 StringRef Name = getName();
1426 OS << " [" << Name << ']';
1428 OS << " [line " << getLineNumber() << ']';
1431 void DISubprogram::printInternal(raw_ostream &OS) const {
1432 // TODO : Print context
1433 OS << " [line " << getLineNumber() << ']';
1435 if (isLocalToUnit())
1441 if (getScopeLineNumber() != getLineNumber())
1442 OS << " [scope " << getScopeLineNumber() << "]";
1446 else if (isProtected())
1447 OS << " [protected]";
1448 else if (isPublic())
1451 if (isLValueReference())
1452 OS << " [reference]";
1454 if (isRValueReference())
1455 OS << " [rvalue reference]";
1457 StringRef Res = getName();
1459 OS << " [" << Res << ']';
1462 void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1463 StringRef Res = getName();
1465 OS << " [" << Res << ']';
1467 OS << " [line " << getLineNumber() << ']';
1469 // TODO : Print context
1471 if (isLocalToUnit())
1478 void DIVariable::printInternal(raw_ostream &OS) const {
1479 StringRef Res = getName();
1481 OS << " [" << Res << ']';
1483 OS << " [line " << getLineNumber() << ']';
1486 void DIExpression::printInternal(raw_ostream &OS) const {
1487 for (unsigned I = 0; I < getNumElements(); ++I) {
1488 uint64_t OpCode = getElement(I);
1489 OS << " [" << OperationEncodingString(OpCode);
1492 OS << " " << getElement(++I);
1496 unsigned Offset = getElement(++I);
1497 unsigned Size = getElement(++I);
1498 OS << " offset=" << Offset << ", size=" << Size;
1502 // Else bail out early. This may be a line table entry.
1510 void DIObjCProperty::printInternal(raw_ostream &OS) const {
1511 StringRef Name = getObjCPropertyName();
1513 OS << " [" << Name << ']';
1515 OS << " [line " << getLineNumber() << ", properties " << getUnsignedField(6)
1519 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1520 const LLVMContext &Ctx) {
1521 if (!DL.isUnknown()) { // Print source line info.
1522 DIScope Scope(DL.getScope(Ctx));
1523 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
1524 // Omit the directory, because it's likely to be long and uninteresting.
1525 CommentOS << Scope.getFilename();
1526 CommentOS << ':' << DL.getLine();
1527 if (DL.getCol() != 0)
1528 CommentOS << ':' << DL.getCol();
1529 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1530 if (!InlinedAtDL.isUnknown()) {
1531 CommentOS << " @[ ";
1532 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1538 void DIVariable::printExtendedName(raw_ostream &OS) const {
1539 const LLVMContext &Ctx = DbgNode->getContext();
1540 StringRef Res = getName();
1542 OS << Res << "," << getLineNumber();
1543 if (MDNode *InlinedAt = getInlinedAt()) {
1544 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1545 if (!InlinedAtDL.isUnknown()) {
1547 printDebugLoc(InlinedAtDL, OS, Ctx);
1553 /// Specialize constructor to make sure it has the correct type.
1554 template <> DIRef<DIScope>::DIRef(const Value *V) : Val(V) {
1555 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
1557 template <> DIRef<DIType>::DIRef(const Value *V) : Val(V) {
1558 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
1561 /// Specialize getFieldAs to handle fields that are references to DIScopes.
1563 DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
1564 return DIScopeRef(getField(DbgNode, Elt));
1566 /// Specialize getFieldAs to handle fields that are references to DITypes.
1567 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
1568 return DITypeRef(getField(DbgNode, Elt));
1571 /// Strip debug info in the module if it exists.
1572 /// To do this, we remove all calls to the debugger intrinsics and any named
1573 /// metadata for debugging. We also remove debug locations for instructions.
1574 /// Return true if module is modified.
1575 bool llvm::StripDebugInfo(Module &M) {
1577 bool Changed = false;
1579 // Remove all of the calls to the debugger intrinsics, and remove them from
1581 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
1582 while (!Declare->use_empty()) {
1583 CallInst *CI = cast<CallInst>(Declare->user_back());
1584 CI->eraseFromParent();
1586 Declare->eraseFromParent();
1590 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
1591 while (!DbgVal->use_empty()) {
1592 CallInst *CI = cast<CallInst>(DbgVal->user_back());
1593 CI->eraseFromParent();
1595 DbgVal->eraseFromParent();
1599 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
1600 NME = M.named_metadata_end(); NMI != NME;) {
1601 NamedMDNode *NMD = NMI;
1603 if (NMD->getName().startswith("llvm.dbg.")) {
1604 NMD->eraseFromParent();
1609 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
1610 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
1612 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
1614 if (!BI->getDebugLoc().isUnknown()) {
1616 BI->setDebugLoc(DebugLoc());
1623 /// Return Debug Info Metadata Version by checking module flags.
1624 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
1625 Value *Val = M.getModuleFlag("Debug Info Version");
1628 return cast<ConstantInt>(Val)->getZExtValue();
1631 llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
1632 llvm::makeSubprogramMap(const Module &M) {
1633 DenseMap<const Function *, DISubprogram> R;
1635 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
1639 for (MDNode *N : CU_Nodes->operands()) {
1640 DICompileUnit CUNode(N);
1641 DIArray SPs = CUNode.getSubprograms();
1642 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
1643 DISubprogram SP(SPs.getElement(i));
1644 if (Function *F = SP.getFunction())
1645 R.insert(std::make_pair(F, SP));