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/Analysis/DebugInfo.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Intrinsics.h"
19 #include "llvm/IntrinsicInst.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/Module.h"
22 #include "llvm/Analysis/ValueTracking.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/Dwarf.h"
28 #include "llvm/Support/raw_ostream.h"
30 using namespace llvm::dwarf;
32 //===----------------------------------------------------------------------===//
34 //===----------------------------------------------------------------------===//
36 DIDescriptor::DIDescriptor(const DIFile F) : DbgNode(F.DbgNode) {
39 DIDescriptor::DIDescriptor(const DISubprogram F) : DbgNode(F.DbgNode) {
42 DIDescriptor::DIDescriptor(const DILexicalBlockFile F) : DbgNode(F.DbgNode) {
45 DIDescriptor::DIDescriptor(const DILexicalBlock F) : DbgNode(F.DbgNode) {
48 DIDescriptor::DIDescriptor(const DIVariable F) : DbgNode(F.DbgNode) {
51 DIDescriptor::DIDescriptor(const DIType F) : DbgNode(F.DbgNode) {
55 DIDescriptor::getStringField(unsigned Elt) const {
59 if (Elt < DbgNode->getNumOperands())
60 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
61 return MDS->getString();
66 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
70 if (Elt < DbgNode->getNumOperands())
71 if (ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
72 return CI->getZExtValue();
77 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
79 return DIDescriptor();
81 if (Elt < DbgNode->getNumOperands())
83 DIDescriptor(dyn_cast_or_null<const MDNode>(DbgNode->getOperand(Elt)));
84 return DIDescriptor();
87 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
91 if (Elt < DbgNode->getNumOperands())
92 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
96 Constant *DIDescriptor::getConstantField(unsigned Elt) const {
100 if (Elt < DbgNode->getNumOperands())
101 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
105 Function *DIDescriptor::getFunctionField(unsigned Elt) const {
109 if (Elt < DbgNode->getNumOperands())
110 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
114 unsigned DIVariable::getNumAddrElements() const {
115 if (getVersion() <= llvm::LLVMDebugVersion8)
116 return DbgNode->getNumOperands()-6;
117 if (getVersion() == llvm::LLVMDebugVersion9)
118 return DbgNode->getNumOperands()-7;
119 return DbgNode->getNumOperands()-8;
122 /// getInlinedAt - If this variable is inlined then return inline location.
123 MDNode *DIVariable::getInlinedAt() const {
124 if (getVersion() <= llvm::LLVMDebugVersion9)
126 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(7));
129 //===----------------------------------------------------------------------===//
131 //===----------------------------------------------------------------------===//
133 /// isBasicType - Return true if the specified tag is legal for
135 bool DIDescriptor::isBasicType() const {
136 if (!DbgNode) return false;
138 case dwarf::DW_TAG_base_type:
139 case dwarf::DW_TAG_unspecified_type:
146 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
147 bool DIDescriptor::isDerivedType() const {
148 if (!DbgNode) return false;
150 case dwarf::DW_TAG_typedef:
151 case dwarf::DW_TAG_pointer_type:
152 case dwarf::DW_TAG_reference_type:
153 case dwarf::DW_TAG_const_type:
154 case dwarf::DW_TAG_volatile_type:
155 case dwarf::DW_TAG_restrict_type:
156 case dwarf::DW_TAG_member:
157 case dwarf::DW_TAG_inheritance:
158 case dwarf::DW_TAG_friend:
161 // CompositeTypes are currently modelled as DerivedTypes.
162 return isCompositeType();
166 /// isCompositeType - Return true if the specified tag is legal for
168 bool DIDescriptor::isCompositeType() const {
169 if (!DbgNode) return false;
171 case dwarf::DW_TAG_array_type:
172 case dwarf::DW_TAG_structure_type:
173 case dwarf::DW_TAG_union_type:
174 case dwarf::DW_TAG_enumeration_type:
175 case dwarf::DW_TAG_vector_type:
176 case dwarf::DW_TAG_subroutine_type:
177 case dwarf::DW_TAG_class_type:
184 /// isVariable - Return true if the specified tag is legal for DIVariable.
185 bool DIDescriptor::isVariable() const {
186 if (!DbgNode) return false;
188 case dwarf::DW_TAG_auto_variable:
189 case dwarf::DW_TAG_arg_variable:
190 case dwarf::DW_TAG_return_variable:
197 /// isType - Return true if the specified tag is legal for DIType.
198 bool DIDescriptor::isType() const {
199 return isBasicType() || isCompositeType() || isDerivedType();
202 /// isSubprogram - Return true if the specified tag is legal for
204 bool DIDescriptor::isSubprogram() const {
205 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
208 /// isGlobalVariable - Return true if the specified tag is legal for
209 /// DIGlobalVariable.
210 bool DIDescriptor::isGlobalVariable() const {
211 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
212 getTag() == dwarf::DW_TAG_constant);
215 /// isGlobal - Return true if the specified tag is legal for DIGlobal.
216 bool DIDescriptor::isGlobal() const {
217 return isGlobalVariable();
220 /// isUnspecifiedParmeter - Return true if the specified tag is
221 /// DW_TAG_unspecified_parameters.
222 bool DIDescriptor::isUnspecifiedParameter() const {
223 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
226 /// isScope - Return true if the specified tag is one of the scope
228 bool DIDescriptor::isScope() const {
229 if (!DbgNode) return false;
231 case dwarf::DW_TAG_compile_unit:
232 case dwarf::DW_TAG_lexical_block:
233 case dwarf::DW_TAG_subprogram:
234 case dwarf::DW_TAG_namespace:
242 /// isTemplateTypeParameter - Return true if the specified tag is
243 /// DW_TAG_template_type_parameter.
244 bool DIDescriptor::isTemplateTypeParameter() const {
245 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
248 /// isTemplateValueParameter - Return true if the specified tag is
249 /// DW_TAG_template_value_parameter.
250 bool DIDescriptor::isTemplateValueParameter() const {
251 return DbgNode && getTag() == dwarf::DW_TAG_template_value_parameter;
254 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
255 bool DIDescriptor::isCompileUnit() const {
256 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
259 /// isFile - Return true if the specified tag is DW_TAG_file_type.
260 bool DIDescriptor::isFile() const {
261 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
264 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
265 bool DIDescriptor::isNameSpace() const {
266 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
269 /// isLexicalBlockFile - Return true if the specified descriptor is a
270 /// lexical block with an extra file.
271 bool DIDescriptor::isLexicalBlockFile() const {
272 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
273 (DbgNode->getNumOperands() == 3);
276 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
277 bool DIDescriptor::isLexicalBlock() const {
278 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
279 (DbgNode->getNumOperands() > 3);
282 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
283 bool DIDescriptor::isSubrange() const {
284 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
287 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
288 bool DIDescriptor::isEnumerator() const {
289 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
292 /// isObjCProperty - Return true if the specified tag is DW_TAG
293 bool DIDescriptor::isObjCProperty() const {
294 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
296 //===----------------------------------------------------------------------===//
297 // Simple Descriptor Constructors and other Methods
298 //===----------------------------------------------------------------------===//
300 DIType::DIType(const MDNode *N) : DIScope(N) {
302 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
307 unsigned DIArray::getNumElements() const {
310 return DbgNode->getNumOperands();
313 /// replaceAllUsesWith - Replace all uses of debug info referenced by
315 void DIType::replaceAllUsesWith(DIDescriptor &D) {
319 // Since we use a TrackingVH for the node, its easy for clients to manufacture
320 // legitimate situations where they want to replaceAllUsesWith() on something
321 // which, due to uniquing, has merged with the source. We shield clients from
322 // this detail by allowing a value to be replaced with replaceAllUsesWith()
325 MDNode *Node = const_cast<MDNode*>(DbgNode);
326 const MDNode *DN = D;
327 const Value *V = cast_or_null<Value>(DN);
328 Node->replaceAllUsesWith(const_cast<Value*>(V));
329 MDNode::deleteTemporary(Node);
333 /// replaceAllUsesWith - Replace all uses of debug info referenced by
335 void DIType::replaceAllUsesWith(MDNode *D) {
339 // Since we use a TrackingVH for the node, its easy for clients to manufacture
340 // legitimate situations where they want to replaceAllUsesWith() on something
341 // which, due to uniquing, has merged with the source. We shield clients from
342 // this detail by allowing a value to be replaced with replaceAllUsesWith()
345 MDNode *Node = const_cast<MDNode*>(DbgNode);
346 const MDNode *DN = D;
347 const Value *V = cast_or_null<Value>(DN);
348 Node->replaceAllUsesWith(const_cast<Value*>(V));
349 MDNode::deleteTemporary(Node);
353 /// isUnsignedDIType - Return true if type encoding is unsigned.
354 bool DIType::isUnsignedDIType() {
355 DIDerivedType DTy(DbgNode);
357 return DTy.getTypeDerivedFrom().isUnsignedDIType();
359 DIBasicType BTy(DbgNode);
361 unsigned Encoding = BTy.getEncoding();
362 if (Encoding == dwarf::DW_ATE_unsigned ||
363 Encoding == dwarf::DW_ATE_unsigned_char)
369 /// Verify - Verify that a compile unit is well formed.
370 bool DICompileUnit::Verify() const {
373 StringRef N = getFilename();
376 // It is possible that directory and produce string is empty.
380 /// Verify - Verify that an ObjC property is well formed.
381 bool DIObjCProperty::Verify() const {
384 unsigned Tag = getTag();
385 if (Tag != dwarf::DW_TAG_APPLE_property) return false;
386 DIType Ty = getType();
387 if (!Ty.Verify()) return false;
389 // Don't worry about the rest of the strings for now.
393 /// Verify - Verify that a type descriptor is well formed.
394 bool DIType::Verify() const {
397 if (getContext() && !getContext().Verify())
399 unsigned Tag = getTag();
400 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
401 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
402 Tag != dwarf::DW_TAG_reference_type && Tag != dwarf::DW_TAG_restrict_type
403 && Tag != dwarf::DW_TAG_vector_type && Tag != dwarf::DW_TAG_array_type
404 && Tag != dwarf::DW_TAG_enumeration_type
405 && Tag != dwarf::DW_TAG_subroutine_type
406 && getFilename().empty())
411 /// Verify - Verify that a basic type descriptor is well formed.
412 bool DIBasicType::Verify() const {
413 return isBasicType();
416 /// Verify - Verify that a derived type descriptor is well formed.
417 bool DIDerivedType::Verify() const {
418 return isDerivedType();
421 /// Verify - Verify that a composite type descriptor is well formed.
422 bool DICompositeType::Verify() const {
425 if (getContext() && !getContext().Verify())
431 /// Verify - Verify that a subprogram descriptor is well formed.
432 bool DISubprogram::Verify() const {
436 if (getContext() && !getContext().Verify())
439 DICompositeType Ty = getType();
445 /// Verify - Verify that a global variable descriptor is well formed.
446 bool DIGlobalVariable::Verify() const {
450 if (getDisplayName().empty())
453 if (getContext() && !getContext().Verify())
456 DIType Ty = getType();
460 if (!getGlobal() && !getConstant())
466 /// Verify - Verify that a variable descriptor is well formed.
467 bool DIVariable::Verify() const {
471 if (getContext() && !getContext().Verify())
474 DIType Ty = getType();
481 /// Verify - Verify that a location descriptor is well formed.
482 bool DILocation::Verify() const {
486 return DbgNode->getNumOperands() == 4;
489 /// Verify - Verify that a namespace descriptor is well formed.
490 bool DINameSpace::Verify() const {
493 if (getName().empty())
498 /// getOriginalTypeSize - If this type is derived from a base type then
499 /// return base type size.
500 uint64_t DIDerivedType::getOriginalTypeSize() const {
501 unsigned Tag = getTag();
503 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
504 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
505 Tag == dwarf::DW_TAG_restrict_type) {
506 DIType BaseType = getTypeDerivedFrom();
507 // If this type is not derived from any type then take conservative
509 if (!BaseType.isValid())
510 return getSizeInBits();
511 // If this is a derived type, go ahead and get the base type, unless
512 // it's a reference then it's just the size of the field. Pointer types
513 // have no need of this since they're a different type of qualification
515 if (BaseType.getTag() == dwarf::DW_TAG_reference_type)
516 return getSizeInBits();
517 else if (BaseType.isDerivedType())
518 return DIDerivedType(BaseType).getOriginalTypeSize();
520 return BaseType.getSizeInBits();
523 return getSizeInBits();
526 /// getObjCProperty - Return property node, if this ivar is associated with one.
527 MDNode *DIDerivedType::getObjCProperty() const {
528 if (getVersion() <= LLVMDebugVersion11 || DbgNode->getNumOperands() <= 10)
530 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(10));
533 /// isInlinedFnArgument - Return true if this variable provides debugging
534 /// information for an inlined function arguments.
535 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
536 assert(CurFn && "Invalid function");
537 if (!getContext().isSubprogram())
539 // This variable is not inlined function argument if its scope
540 // does not describe current function.
541 return !(DISubprogram(getContext()).describes(CurFn));
544 /// describes - Return true if this subprogram provides debugging
545 /// information for the function F.
546 bool DISubprogram::describes(const Function *F) {
547 assert(F && "Invalid function");
548 if (F == getFunction())
550 StringRef Name = getLinkageName();
553 if (F->getName() == Name)
558 unsigned DISubprogram::isOptimized() const {
559 assert (DbgNode && "Invalid subprogram descriptor!");
560 if (DbgNode->getNumOperands() == 16)
561 return getUnsignedField(15);
565 MDNode *DISubprogram::getVariablesNodes() const {
566 if (!DbgNode || DbgNode->getNumOperands() <= 19)
568 if (MDNode *Temp = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
569 return dyn_cast_or_null<MDNode>(Temp->getOperand(0));
573 DIArray DISubprogram::getVariables() const {
574 if (!DbgNode || DbgNode->getNumOperands() <= 19)
576 if (MDNode *T = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
577 if (MDNode *A = dyn_cast_or_null<MDNode>(T->getOperand(0)))
582 StringRef DIScope::getFilename() const {
585 if (isLexicalBlockFile())
586 return DILexicalBlockFile(DbgNode).getFilename();
587 if (isLexicalBlock())
588 return DILexicalBlock(DbgNode).getFilename();
590 return DISubprogram(DbgNode).getFilename();
592 return DICompileUnit(DbgNode).getFilename();
594 return DINameSpace(DbgNode).getFilename();
596 return DIType(DbgNode).getFilename();
598 return DIFile(DbgNode).getFilename();
599 llvm_unreachable("Invalid DIScope!");
602 StringRef DIScope::getDirectory() const {
605 if (isLexicalBlockFile())
606 return DILexicalBlockFile(DbgNode).getDirectory();
607 if (isLexicalBlock())
608 return DILexicalBlock(DbgNode).getDirectory();
610 return DISubprogram(DbgNode).getDirectory();
612 return DICompileUnit(DbgNode).getDirectory();
614 return DINameSpace(DbgNode).getDirectory();
616 return DIType(DbgNode).getDirectory();
618 return DIFile(DbgNode).getDirectory();
619 llvm_unreachable("Invalid DIScope!");
622 DIArray DICompileUnit::getEnumTypes() const {
623 if (!DbgNode || DbgNode->getNumOperands() < 14)
626 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(10)))
627 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
632 DIArray DICompileUnit::getRetainedTypes() const {
633 if (!DbgNode || DbgNode->getNumOperands() < 14)
636 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(11)))
637 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
642 DIArray DICompileUnit::getSubprograms() const {
643 if (!DbgNode || DbgNode->getNumOperands() < 14)
646 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(12)))
647 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
653 DIArray DICompileUnit::getGlobalVariables() const {
654 if (!DbgNode || DbgNode->getNumOperands() < 14)
657 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(13)))
658 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
663 //===----------------------------------------------------------------------===//
664 // DIDescriptor: vtable anchors for all descriptors.
665 //===----------------------------------------------------------------------===//
667 void DIScope::anchor() { }
669 void DICompileUnit::anchor() { }
671 void DIFile::anchor() { }
673 void DIType::anchor() { }
675 void DIBasicType::anchor() { }
677 void DIDerivedType::anchor() { }
679 void DICompositeType::anchor() { }
681 void DISubprogram::anchor() { }
683 void DILexicalBlock::anchor() { }
685 void DINameSpace::anchor() { }
687 void DILexicalBlockFile::anchor() { }
689 //===----------------------------------------------------------------------===//
690 // DIDescriptor: dump routines for all descriptors.
691 //===----------------------------------------------------------------------===//
694 /// print - Print descriptor.
695 void DIDescriptor::print(raw_ostream &OS) const {
696 OS << "[" << dwarf::TagString(getTag()) << "] ";
697 OS.write_hex((intptr_t) &*DbgNode) << ']';
700 /// print - Print compile unit.
701 void DICompileUnit::print(raw_ostream &OS) const {
703 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
705 OS << " [" << getDirectory() << "/" << getFilename() << "]";
708 /// print - Print type.
709 void DIType::print(raw_ostream &OS) const {
710 if (!DbgNode) return;
712 StringRef Res = getName();
714 OS << " [" << Res << "] ";
716 unsigned Tag = getTag();
717 OS << " [" << dwarf::TagString(Tag) << "] ";
719 // TODO : Print context
721 << "line " << getLineNumber() << ", "
722 << getSizeInBits() << " bits, "
723 << getAlignInBits() << " bit alignment, "
724 << getOffsetInBits() << " bit offset"
729 else if (isProtected())
730 OS << " [protected] ";
736 DIBasicType(DbgNode).print(OS);
737 else if (isDerivedType()) {
738 DIDerivedType DTy = DIDerivedType(DbgNode);
740 DICompositeType CTy = getDICompositeType(DTy);
744 else if (isCompositeType())
745 DICompositeType(DbgNode).print(OS);
747 OS << "Invalid DIType\n";
754 /// print - Print basic type.
755 void DIBasicType::print(raw_ostream &OS) const {
756 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
759 /// print - Print derived type.
760 void DIDerivedType::print(raw_ostream &OS) const {
761 OS << "\n\t Derived From: ";
762 getTypeDerivedFrom().print(OS);
766 /// print - Print composite type.
767 void DICompositeType::print(raw_ostream &OS) const {
768 DIArray A = getTypeArray();
769 OS << " [" << A.getNumElements() << " elements]";
772 /// print - Print subprogram.
773 void DISubprogram::print(raw_ostream &OS) const {
774 StringRef Res = getName();
776 OS << " [" << Res << "] ";
778 unsigned Tag = getTag();
779 OS << " [" << dwarf::TagString(Tag) << "] ";
781 // TODO : Print context
782 OS << " [" << getLineNumber() << "] ";
793 /// print - Print global variable.
794 void DIGlobalVariable::print(raw_ostream &OS) const {
796 StringRef Res = getName();
798 OS << " [" << Res << "] ";
800 unsigned Tag = getTag();
801 OS << " [" << dwarf::TagString(Tag) << "] ";
803 // TODO : Print context
804 OS << " [" << getLineNumber() << "] ";
812 if (isGlobalVariable())
813 DIGlobalVariable(DbgNode).print(OS);
817 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
818 const LLVMContext &Ctx) {
819 if (!DL.isUnknown()) { // Print source line info.
820 DIScope Scope(DL.getScope(Ctx));
821 // Omit the directory, because it's likely to be long and uninteresting.
823 CommentOS << Scope.getFilename();
825 CommentOS << "<unknown>";
826 CommentOS << ':' << DL.getLine();
827 if (DL.getCol() != 0)
828 CommentOS << ':' << DL.getCol();
829 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
830 if (!InlinedAtDL.isUnknown()) {
832 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
838 void DIVariable::printExtendedName(raw_ostream &OS) const {
839 const LLVMContext &Ctx = DbgNode->getContext();
840 StringRef Res = getName();
842 OS << Res << "," << getLineNumber();
843 if (MDNode *InlinedAt = getInlinedAt()) {
844 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
845 if (!InlinedAtDL.isUnknown()) {
847 printDebugLoc(InlinedAtDL, OS, Ctx);
853 /// print - Print variable.
854 void DIVariable::print(raw_ostream &OS) const {
855 StringRef Res = getName();
857 OS << " [" << Res << "] ";
859 OS << " [" << getLineNumber() << "] ";
863 // FIXME: Dump complex addresses
866 /// dump - Print descriptor to dbgs() with a newline.
867 void DIDescriptor::dump() const {
868 print(dbgs()); dbgs() << '\n';
871 /// dump - Print compile unit to dbgs() with a newline.
872 void DICompileUnit::dump() const {
873 print(dbgs()); dbgs() << '\n';
876 /// dump - Print type to dbgs() with a newline.
877 void DIType::dump() const {
878 print(dbgs()); dbgs() << '\n';
881 /// dump - Print basic type to dbgs() with a newline.
882 void DIBasicType::dump() const {
883 print(dbgs()); dbgs() << '\n';
886 /// dump - Print derived type to dbgs() with a newline.
887 void DIDerivedType::dump() const {
888 print(dbgs()); dbgs() << '\n';
891 /// dump - Print composite type to dbgs() with a newline.
892 void DICompositeType::dump() const {
893 print(dbgs()); dbgs() << '\n';
896 /// dump - Print subprogram to dbgs() with a newline.
897 void DISubprogram::dump() const {
898 print(dbgs()); dbgs() << '\n';
901 /// dump - Print global variable.
902 void DIGlobalVariable::dump() const {
903 print(dbgs()); dbgs() << '\n';
906 /// dump - Print variable.
907 void DIVariable::dump() const {
908 print(dbgs()); dbgs() << '\n';
911 /// fixupObjcLikeName - Replace contains special characters used
912 /// in a typical Objective-C names with '.' in a given string.
913 static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
914 bool isObjCLike = false;
915 for (size_t i = 0, e = Str.size(); i < e; ++i) {
920 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
921 C == '+' || C == '(' || C == ')'))
928 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
929 /// suitable to hold function specific information.
930 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
931 SmallString<32> Name = StringRef("llvm.dbg.lv.");
932 StringRef FName = "fn";
933 if (Fn.getFunction())
934 FName = Fn.getFunction()->getName();
936 FName = Fn.getName();
938 if (FName.startswith(StringRef(&One, 1)))
939 FName = FName.substr(1);
940 fixupObjcLikeName(FName, 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 = StringRef("llvm.dbg.lv.");
948 StringRef FName = "fn";
949 if (Fn.getFunction())
950 FName = Fn.getFunction()->getName();
952 FName = Fn.getName();
954 if (FName.startswith(StringRef(&One, 1)))
955 FName = FName.substr(1);
956 fixupObjcLikeName(FName, Name);
958 return M.getOrInsertNamedMetadata(Name.str());
961 /// createInlinedVariable - Create a new inlined variable based on current
963 /// @param DV Current Variable.
964 /// @param InlinedScope Location at current variable is inlined.
965 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
966 LLVMContext &VMContext) {
967 SmallVector<Value *, 16> Elts;
968 // Insert inlined scope as 7th element.
969 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
970 i == 7 ? Elts.push_back(InlinedScope) :
971 Elts.push_back(DV->getOperand(i));
972 return DIVariable(MDNode::get(VMContext, Elts));
975 /// cleanseInlinedVariable - Remove inlined scope from the variable.
976 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
977 SmallVector<Value *, 16> Elts;
978 // Insert inlined scope as 7th element.
979 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
981 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext))):
982 Elts.push_back(DV->getOperand(i));
983 return DIVariable(MDNode::get(VMContext, Elts));
986 //===----------------------------------------------------------------------===//
987 // DebugInfoFinder implementations.
988 //===----------------------------------------------------------------------===//
990 /// processModule - Process entire module and collect debug info.
991 void DebugInfoFinder::processModule(Module &M) {
992 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
993 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
994 DICompileUnit CU(CU_Nodes->getOperand(i));
996 if (CU.getVersion() > LLVMDebugVersion10) {
997 DIArray GVs = CU.getGlobalVariables();
998 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
999 DIGlobalVariable DIG(GVs.getElement(i));
1000 if (addGlobalVariable(DIG))
1001 processType(DIG.getType());
1003 DIArray SPs = CU.getSubprograms();
1004 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
1005 processSubprogram(DISubprogram(SPs.getElement(i)));
1006 DIArray EnumTypes = CU.getEnumTypes();
1007 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
1008 processType(DIType(EnumTypes.getElement(i)));
1009 DIArray RetainedTypes = CU.getRetainedTypes();
1010 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
1011 processType(DIType(RetainedTypes.getElement(i)));
1017 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1018 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1019 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1021 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1022 processDeclare(DDI);
1024 DebugLoc Loc = BI->getDebugLoc();
1025 if (Loc.isUnknown())
1028 LLVMContext &Ctx = BI->getContext();
1029 DIDescriptor Scope(Loc.getScope(Ctx));
1031 if (Scope.isCompileUnit())
1032 addCompileUnit(DICompileUnit(Scope));
1033 else if (Scope.isSubprogram())
1034 processSubprogram(DISubprogram(Scope));
1035 else if (Scope.isLexicalBlockFile()) {
1036 DILexicalBlockFile DBF = DILexicalBlockFile(Scope);
1037 processLexicalBlock(DILexicalBlock(DBF.getScope()));
1039 else if (Scope.isLexicalBlock())
1040 processLexicalBlock(DILexicalBlock(Scope));
1042 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1043 processLocation(DILocation(IA));
1046 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
1047 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1048 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1049 if (addGlobalVariable(DIG)) {
1050 if (DIG.getVersion() <= LLVMDebugVersion10)
1051 addCompileUnit(DIG.getCompileUnit());
1052 processType(DIG.getType());
1057 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
1058 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
1059 processSubprogram(DISubprogram(NMD->getOperand(i)));
1062 /// processLocation - Process DILocation.
1063 void DebugInfoFinder::processLocation(DILocation Loc) {
1064 if (!Loc.Verify()) return;
1065 DIDescriptor S(Loc.getScope());
1066 if (S.isCompileUnit())
1067 addCompileUnit(DICompileUnit(S));
1068 else if (S.isSubprogram())
1069 processSubprogram(DISubprogram(S));
1070 else if (S.isLexicalBlock())
1071 processLexicalBlock(DILexicalBlock(S));
1072 else if (S.isLexicalBlockFile()) {
1073 DILexicalBlockFile DBF = DILexicalBlockFile(S);
1074 processLexicalBlock(DILexicalBlock(DBF.getScope()));
1076 processLocation(Loc.getOrigLocation());
1079 /// processType - Process DIType.
1080 void DebugInfoFinder::processType(DIType DT) {
1083 if (DT.getVersion() <= LLVMDebugVersion10)
1084 addCompileUnit(DT.getCompileUnit());
1085 if (DT.isCompositeType()) {
1086 DICompositeType DCT(DT);
1087 processType(DCT.getTypeDerivedFrom());
1088 DIArray DA = DCT.getTypeArray();
1089 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1090 DIDescriptor D = DA.getElement(i);
1092 processType(DIType(D));
1093 else if (D.isSubprogram())
1094 processSubprogram(DISubprogram(D));
1096 } else if (DT.isDerivedType()) {
1097 DIDerivedType DDT(DT);
1098 processType(DDT.getTypeDerivedFrom());
1102 /// processLexicalBlock
1103 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1104 DIScope Context = LB.getContext();
1105 if (Context.isLexicalBlock())
1106 return processLexicalBlock(DILexicalBlock(Context));
1107 else if (Context.isLexicalBlockFile()) {
1108 DILexicalBlockFile DBF = DILexicalBlockFile(Context);
1109 return processLexicalBlock(DILexicalBlock(DBF.getScope()));
1112 return processSubprogram(DISubprogram(Context));
1115 /// processSubprogram - Process DISubprogram.
1116 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1117 if (!addSubprogram(SP))
1119 if (SP.getVersion() <= LLVMDebugVersion10)
1120 addCompileUnit(SP.getCompileUnit());
1121 processType(SP.getType());
1124 /// processDeclare - Process DbgDeclareInst.
1125 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
1126 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1130 if (!DV.isVariable())
1133 if (!NodesSeen.insert(DV))
1135 if (DIVariable(N).getVersion() <= LLVMDebugVersion10)
1136 addCompileUnit(DIVariable(N).getCompileUnit());
1137 processType(DIVariable(N).getType());
1140 /// addType - Add type into Tys.
1141 bool DebugInfoFinder::addType(DIType DT) {
1145 if (!NodesSeen.insert(DT))
1152 /// addCompileUnit - Add compile unit into CUs.
1153 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1157 if (!NodesSeen.insert(CU))
1164 /// addGlobalVariable - Add global variable into GVs.
1165 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1166 if (!DIDescriptor(DIG).isGlobalVariable())
1169 if (!NodesSeen.insert(DIG))
1176 // addSubprogram - Add subprgoram into SPs.
1177 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1178 if (!DIDescriptor(SP).isSubprogram())
1181 if (!NodesSeen.insert(SP))
1188 /// getDISubprogram - Find subprogram that is enclosing this scope.
1189 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
1190 DIDescriptor D(Scope);
1191 if (D.isSubprogram())
1192 return DISubprogram(Scope);
1194 if (D.isLexicalBlockFile())
1195 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
1197 if (D.isLexicalBlock())
1198 return getDISubprogram(DILexicalBlock(Scope).getContext());
1200 return DISubprogram();
1203 /// getDICompositeType - Find underlying composite type.
1204 DICompositeType llvm::getDICompositeType(DIType T) {
1205 if (T.isCompositeType())
1206 return DICompositeType(T);
1208 if (T.isDerivedType())
1209 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
1211 return DICompositeType();
1214 /// isSubprogramContext - Return true if Context is either a subprogram
1215 /// or another context nested inside a subprogram.
1216 bool llvm::isSubprogramContext(const MDNode *Context) {
1219 DIDescriptor D(Context);
1220 if (D.isSubprogram())
1223 return isSubprogramContext(DIType(Context).getContext());