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<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 //===----------------------------------------------------------------------===//
293 // Simple Descriptor Constructors and other Methods
294 //===----------------------------------------------------------------------===//
296 DIType::DIType(const MDNode *N) : DIScope(N) {
298 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
303 unsigned DIArray::getNumElements() const {
306 return DbgNode->getNumOperands();
309 /// replaceAllUsesWith - Replace all uses of debug info referenced by
311 void DIType::replaceAllUsesWith(DIDescriptor &D) {
315 // Since we use a TrackingVH for the node, its easy for clients to manufacture
316 // legitimate situations where they want to replaceAllUsesWith() on something
317 // which, due to uniquing, has merged with the source. We shield clients from
318 // this detail by allowing a value to be replaced with replaceAllUsesWith()
321 MDNode *Node = const_cast<MDNode*>(DbgNode);
322 const MDNode *DN = D;
323 const Value *V = cast_or_null<Value>(DN);
324 Node->replaceAllUsesWith(const_cast<Value*>(V));
325 MDNode::deleteTemporary(Node);
329 /// replaceAllUsesWith - Replace all uses of debug info referenced by
331 void DIType::replaceAllUsesWith(MDNode *D) {
335 // Since we use a TrackingVH for the node, its easy for clients to manufacture
336 // legitimate situations where they want to replaceAllUsesWith() on something
337 // which, due to uniquing, has merged with the source. We shield clients from
338 // this detail by allowing a value to be replaced with replaceAllUsesWith()
341 MDNode *Node = const_cast<MDNode*>(DbgNode);
342 const MDNode *DN = D;
343 const Value *V = cast_or_null<Value>(DN);
344 Node->replaceAllUsesWith(const_cast<Value*>(V));
345 MDNode::deleteTemporary(Node);
349 /// isUnsignedDIType - Return true if type encoding is unsigned.
350 bool DIType::isUnsignedDIType() {
351 DIDerivedType DTy(DbgNode);
353 return DTy.getTypeDerivedFrom().isUnsignedDIType();
355 DIBasicType BTy(DbgNode);
357 unsigned Encoding = BTy.getEncoding();
358 if (Encoding == dwarf::DW_ATE_unsigned ||
359 Encoding == dwarf::DW_ATE_unsigned_char)
365 /// Verify - Verify that a compile unit is well formed.
366 bool DICompileUnit::Verify() const {
369 StringRef N = getFilename();
372 // It is possible that directory and produce string is empty.
376 /// Verify - Verify that a type descriptor is well formed.
377 bool DIType::Verify() const {
380 if (getContext() && !getContext().Verify())
382 unsigned Tag = getTag();
383 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
384 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
385 Tag != dwarf::DW_TAG_reference_type && Tag != dwarf::DW_TAG_restrict_type
386 && Tag != dwarf::DW_TAG_vector_type && Tag != dwarf::DW_TAG_array_type
387 && Tag != dwarf::DW_TAG_enumeration_type
388 && Tag != dwarf::DW_TAG_subroutine_type
389 && getFilename().empty())
394 /// Verify - Verify that a basic type descriptor is well formed.
395 bool DIBasicType::Verify() const {
396 return isBasicType();
399 /// Verify - Verify that a derived type descriptor is well formed.
400 bool DIDerivedType::Verify() const {
401 return isDerivedType();
404 /// Verify - Verify that a composite type descriptor is well formed.
405 bool DICompositeType::Verify() const {
408 if (getContext() && !getContext().Verify())
414 /// Verify - Verify that a subprogram descriptor is well formed.
415 bool DISubprogram::Verify() const {
419 if (getContext() && !getContext().Verify())
422 DICompositeType Ty = getType();
428 /// Verify - Verify that a global variable descriptor is well formed.
429 bool DIGlobalVariable::Verify() const {
433 if (getDisplayName().empty())
436 if (getContext() && !getContext().Verify())
439 DIType Ty = getType();
443 if (!getGlobal() && !getConstant())
449 /// Verify - Verify that a variable descriptor is well formed.
450 bool DIVariable::Verify() const {
454 if (getContext() && !getContext().Verify())
457 DIType Ty = getType();
464 /// Verify - Verify that a location descriptor is well formed.
465 bool DILocation::Verify() const {
469 return DbgNode->getNumOperands() == 4;
472 /// Verify - Verify that a namespace descriptor is well formed.
473 bool DINameSpace::Verify() const {
476 if (getName().empty())
481 /// getOriginalTypeSize - If this type is derived from a base type then
482 /// return base type size.
483 uint64_t DIDerivedType::getOriginalTypeSize() const {
484 unsigned Tag = getTag();
486 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
487 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
488 Tag == dwarf::DW_TAG_restrict_type) {
489 DIType BaseType = getTypeDerivedFrom();
490 // If this type is not derived from any type then take conservative
492 if (!BaseType.isValid())
493 return getSizeInBits();
494 // If this is a derived type, go ahead and get the base type, unless
495 // it's a reference then it's just the size of the field. Pointer types
496 // have no need of this since they're a different type of qualification
498 if (BaseType.getTag() == dwarf::DW_TAG_reference_type)
499 return getSizeInBits();
500 else if (BaseType.isDerivedType())
501 return DIDerivedType(BaseType).getOriginalTypeSize();
503 return BaseType.getSizeInBits();
506 return getSizeInBits();
509 /// isInlinedFnArgument - Return true if this variable provides debugging
510 /// information for an inlined function arguments.
511 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
512 assert(CurFn && "Invalid function");
513 if (!getContext().isSubprogram())
515 // This variable is not inlined function argument if its scope
516 // does not describe current function.
517 return !(DISubprogram(getContext()).describes(CurFn));
520 /// describes - Return true if this subprogram provides debugging
521 /// information for the function F.
522 bool DISubprogram::describes(const Function *F) {
523 assert(F && "Invalid function");
524 if (F == getFunction())
526 StringRef Name = getLinkageName();
529 if (F->getName() == Name)
534 unsigned DISubprogram::isOptimized() const {
535 assert (DbgNode && "Invalid subprogram descriptor!");
536 if (DbgNode->getNumOperands() == 16)
537 return getUnsignedField(15);
541 MDNode *DISubprogram::getVariablesNodes() const {
542 if (!DbgNode || DbgNode->getNumOperands() <= 19)
544 if (MDNode *Temp = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
545 return dyn_cast_or_null<MDNode>(Temp->getOperand(0));
549 DIArray DISubprogram::getVariables() const {
550 if (!DbgNode || DbgNode->getNumOperands() <= 19)
552 if (MDNode *T = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
553 if (MDNode *A = dyn_cast_or_null<MDNode>(T->getOperand(0)))
558 StringRef DIScope::getFilename() const {
561 if (isLexicalBlockFile())
562 return DILexicalBlockFile(DbgNode).getFilename();
563 if (isLexicalBlock())
564 return DILexicalBlock(DbgNode).getFilename();
566 return DISubprogram(DbgNode).getFilename();
568 return DICompileUnit(DbgNode).getFilename();
570 return DINameSpace(DbgNode).getFilename();
572 return DIType(DbgNode).getFilename();
574 return DIFile(DbgNode).getFilename();
575 assert(0 && "Invalid DIScope!");
579 StringRef DIScope::getDirectory() const {
582 if (isLexicalBlockFile())
583 return DILexicalBlockFile(DbgNode).getDirectory();
584 if (isLexicalBlock())
585 return DILexicalBlock(DbgNode).getDirectory();
587 return DISubprogram(DbgNode).getDirectory();
589 return DICompileUnit(DbgNode).getDirectory();
591 return DINameSpace(DbgNode).getDirectory();
593 return DIType(DbgNode).getDirectory();
595 return DIFile(DbgNode).getDirectory();
596 assert(0 && "Invalid DIScope!");
600 DIArray DICompileUnit::getEnumTypes() const {
601 if (!DbgNode || DbgNode->getNumOperands() < 14)
604 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(10)))
605 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
610 DIArray DICompileUnit::getRetainedTypes() const {
611 if (!DbgNode || DbgNode->getNumOperands() < 14)
614 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(11)))
615 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
620 DIArray DICompileUnit::getSubprograms() const {
621 if (!DbgNode || DbgNode->getNumOperands() < 14)
624 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(12)))
625 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
631 DIArray DICompileUnit::getGlobalVariables() const {
632 if (!DbgNode || DbgNode->getNumOperands() < 14)
635 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(13)))
636 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
641 //===----------------------------------------------------------------------===//
642 // DIDescriptor: vtable anchors for all descriptors.
643 //===----------------------------------------------------------------------===//
645 void DIScope::anchor() { }
647 void DICompileUnit::anchor() { }
649 void DIFile::anchor() { }
651 void DIType::anchor() { }
653 void DIBasicType::anchor() { }
655 void DIDerivedType::anchor() { }
657 void DICompositeType::anchor() { }
659 void DISubprogram::anchor() { }
661 void DILexicalBlock::anchor() { }
663 void DINameSpace::anchor() { }
665 void DILexicalBlockFile::anchor() { }
667 //===----------------------------------------------------------------------===//
668 // DIDescriptor: dump routines for all descriptors.
669 //===----------------------------------------------------------------------===//
672 /// print - Print descriptor.
673 void DIDescriptor::print(raw_ostream &OS) const {
674 OS << "[" << dwarf::TagString(getTag()) << "] ";
675 OS.write_hex((intptr_t) &*DbgNode) << ']';
678 /// print - Print compile unit.
679 void DICompileUnit::print(raw_ostream &OS) const {
681 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
683 OS << " [" << getDirectory() << "/" << getFilename() << "]";
686 /// print - Print type.
687 void DIType::print(raw_ostream &OS) const {
688 if (!DbgNode) return;
690 StringRef Res = getName();
692 OS << " [" << Res << "] ";
694 unsigned Tag = getTag();
695 OS << " [" << dwarf::TagString(Tag) << "] ";
697 // TODO : Print context
699 << "line " << getLineNumber() << ", "
700 << getSizeInBits() << " bits, "
701 << getAlignInBits() << " bit alignment, "
702 << getOffsetInBits() << " bit offset"
707 else if (isProtected())
708 OS << " [protected] ";
714 DIBasicType(DbgNode).print(OS);
715 else if (isDerivedType())
716 DIDerivedType(DbgNode).print(OS);
717 else if (isCompositeType())
718 DICompositeType(DbgNode).print(OS);
720 OS << "Invalid DIType\n";
727 /// print - Print basic type.
728 void DIBasicType::print(raw_ostream &OS) const {
729 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
732 /// print - Print derived type.
733 void DIDerivedType::print(raw_ostream &OS) const {
734 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
737 /// print - Print composite type.
738 void DICompositeType::print(raw_ostream &OS) const {
739 DIArray A = getTypeArray();
740 OS << " [" << A.getNumElements() << " elements]";
743 /// print - Print subprogram.
744 void DISubprogram::print(raw_ostream &OS) const {
745 StringRef Res = getName();
747 OS << " [" << Res << "] ";
749 unsigned Tag = getTag();
750 OS << " [" << dwarf::TagString(Tag) << "] ";
752 // TODO : Print context
753 OS << " [" << getLineNumber() << "] ";
764 /// print - Print global variable.
765 void DIGlobalVariable::print(raw_ostream &OS) const {
767 StringRef Res = getName();
769 OS << " [" << Res << "] ";
771 unsigned Tag = getTag();
772 OS << " [" << dwarf::TagString(Tag) << "] ";
774 // TODO : Print context
775 OS << " [" << getLineNumber() << "] ";
783 if (isGlobalVariable())
784 DIGlobalVariable(DbgNode).print(OS);
788 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
789 const LLVMContext &Ctx) {
790 if (!DL.isUnknown()) { // Print source line info.
791 DIScope Scope(DL.getScope(Ctx));
792 // Omit the directory, because it's likely to be long and uninteresting.
794 CommentOS << Scope.getFilename();
796 CommentOS << "<unknown>";
797 CommentOS << ':' << DL.getLine();
798 if (DL.getCol() != 0)
799 CommentOS << ':' << DL.getCol();
800 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
801 if (!InlinedAtDL.isUnknown()) {
803 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
809 void DIVariable::printExtendedName(raw_ostream &OS) const {
810 const LLVMContext &Ctx = DbgNode->getContext();
811 StringRef Res = getName();
813 OS << Res << "," << getLineNumber();
814 if (MDNode *InlinedAt = getInlinedAt()) {
815 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
816 if (!InlinedAtDL.isUnknown()) {
818 printDebugLoc(InlinedAtDL, OS, Ctx);
824 /// print - Print variable.
825 void DIVariable::print(raw_ostream &OS) const {
826 StringRef Res = getName();
828 OS << " [" << Res << "] ";
830 OS << " [" << getLineNumber() << "] ";
834 // FIXME: Dump complex addresses
837 /// dump - Print descriptor to dbgs() with a newline.
838 void DIDescriptor::dump() const {
839 print(dbgs()); dbgs() << '\n';
842 /// dump - Print compile unit to dbgs() with a newline.
843 void DICompileUnit::dump() const {
844 print(dbgs()); dbgs() << '\n';
847 /// dump - Print type to dbgs() with a newline.
848 void DIType::dump() const {
849 print(dbgs()); dbgs() << '\n';
852 /// dump - Print basic type to dbgs() with a newline.
853 void DIBasicType::dump() const {
854 print(dbgs()); dbgs() << '\n';
857 /// dump - Print derived type to dbgs() with a newline.
858 void DIDerivedType::dump() const {
859 print(dbgs()); dbgs() << '\n';
862 /// dump - Print composite type to dbgs() with a newline.
863 void DICompositeType::dump() const {
864 print(dbgs()); dbgs() << '\n';
867 /// dump - Print subprogram to dbgs() with a newline.
868 void DISubprogram::dump() const {
869 print(dbgs()); dbgs() << '\n';
872 /// dump - Print global variable.
873 void DIGlobalVariable::dump() const {
874 print(dbgs()); dbgs() << '\n';
877 /// dump - Print variable.
878 void DIVariable::dump() const {
879 print(dbgs()); dbgs() << '\n';
882 /// fixupObjcLikeName - Replace contains special characters used
883 /// in a typical Objective-C names with '.' in a given string.
884 static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
885 bool isObjCLike = false;
886 for (size_t i = 0, e = Str.size(); i < e; ++i) {
891 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
892 C == '+' || C == '(' || C == ')'))
899 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
900 /// suitable to hold function specific information.
901 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
902 SmallString<32> Name = StringRef("llvm.dbg.lv.");
903 StringRef FName = "fn";
904 if (Fn.getFunction())
905 FName = Fn.getFunction()->getName();
907 FName = Fn.getName();
909 if (FName.startswith(StringRef(&One, 1)))
910 FName = FName.substr(1);
911 fixupObjcLikeName(FName, Name);
912 return M.getNamedMetadata(Name.str());
915 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
916 /// to hold function specific information.
917 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
918 SmallString<32> Name = StringRef("llvm.dbg.lv.");
919 StringRef FName = "fn";
920 if (Fn.getFunction())
921 FName = Fn.getFunction()->getName();
923 FName = Fn.getName();
925 if (FName.startswith(StringRef(&One, 1)))
926 FName = FName.substr(1);
927 fixupObjcLikeName(FName, Name);
929 return M.getOrInsertNamedMetadata(Name.str());
932 /// createInlinedVariable - Create a new inlined variable based on current
934 /// @param DV Current Variable.
935 /// @param InlinedScope Location at current variable is inlined.
936 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
937 LLVMContext &VMContext) {
938 SmallVector<Value *, 16> Elts;
939 // Insert inlined scope as 7th element.
940 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
941 i == 7 ? Elts.push_back(InlinedScope) :
942 Elts.push_back(DV->getOperand(i));
943 return DIVariable(MDNode::get(VMContext, Elts));
946 /// cleanseInlinedVariable - Remove inlined scope from the variable.
947 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
948 SmallVector<Value *, 16> Elts;
949 // Insert inlined scope as 7th element.
950 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
952 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext))):
953 Elts.push_back(DV->getOperand(i));
954 return DIVariable(MDNode::get(VMContext, Elts));
957 //===----------------------------------------------------------------------===//
958 // DebugInfoFinder implementations.
959 //===----------------------------------------------------------------------===//
961 /// processModule - Process entire module and collect debug info.
962 void DebugInfoFinder::processModule(Module &M) {
963 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
964 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
965 DICompileUnit CU(CU_Nodes->getOperand(i));
967 if (CU.getVersion() > LLVMDebugVersion10) {
968 DIArray GVs = CU.getGlobalVariables();
969 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
970 DIGlobalVariable DIG(GVs.getElement(i));
971 if (addGlobalVariable(DIG))
972 processType(DIG.getType());
974 DIArray SPs = CU.getSubprograms();
975 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
976 processSubprogram(DISubprogram(SPs.getElement(i)));
977 DIArray EnumTypes = CU.getEnumTypes();
978 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
979 processType(DIType(EnumTypes.getElement(i)));
980 DIArray RetainedTypes = CU.getRetainedTypes();
981 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
982 processType(DIType(RetainedTypes.getElement(i)));
988 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
989 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
990 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
992 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
995 DebugLoc Loc = BI->getDebugLoc();
999 LLVMContext &Ctx = BI->getContext();
1000 DIDescriptor Scope(Loc.getScope(Ctx));
1002 if (Scope.isCompileUnit())
1003 addCompileUnit(DICompileUnit(Scope));
1004 else if (Scope.isSubprogram())
1005 processSubprogram(DISubprogram(Scope));
1006 else if (Scope.isLexicalBlockFile()) {
1007 DILexicalBlockFile DBF = DILexicalBlockFile(Scope);
1008 processLexicalBlock(DILexicalBlock(DBF.getScope()));
1010 else if (Scope.isLexicalBlock())
1011 processLexicalBlock(DILexicalBlock(Scope));
1013 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1014 processLocation(DILocation(IA));
1017 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
1018 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1019 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1020 if (addGlobalVariable(DIG)) {
1021 if (DIG.getVersion() <= LLVMDebugVersion10)
1022 addCompileUnit(DIG.getCompileUnit());
1023 processType(DIG.getType());
1028 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
1029 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
1030 processSubprogram(DISubprogram(NMD->getOperand(i)));
1033 /// processLocation - Process DILocation.
1034 void DebugInfoFinder::processLocation(DILocation Loc) {
1035 if (!Loc.Verify()) return;
1036 DIDescriptor S(Loc.getScope());
1037 if (S.isCompileUnit())
1038 addCompileUnit(DICompileUnit(S));
1039 else if (S.isSubprogram())
1040 processSubprogram(DISubprogram(S));
1041 else if (S.isLexicalBlock())
1042 processLexicalBlock(DILexicalBlock(S));
1043 else if (S.isLexicalBlockFile()) {
1044 DILexicalBlockFile DBF = DILexicalBlockFile(S);
1045 processLexicalBlock(DILexicalBlock(DBF.getScope()));
1047 processLocation(Loc.getOrigLocation());
1050 /// processType - Process DIType.
1051 void DebugInfoFinder::processType(DIType DT) {
1054 if (DT.getVersion() <= LLVMDebugVersion10)
1055 addCompileUnit(DT.getCompileUnit());
1056 if (DT.isCompositeType()) {
1057 DICompositeType DCT(DT);
1058 processType(DCT.getTypeDerivedFrom());
1059 DIArray DA = DCT.getTypeArray();
1060 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1061 DIDescriptor D = DA.getElement(i);
1063 processType(DIType(D));
1064 else if (D.isSubprogram())
1065 processSubprogram(DISubprogram(D));
1067 } else if (DT.isDerivedType()) {
1068 DIDerivedType DDT(DT);
1069 processType(DDT.getTypeDerivedFrom());
1073 /// processLexicalBlock
1074 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1075 DIScope Context = LB.getContext();
1076 if (Context.isLexicalBlock())
1077 return processLexicalBlock(DILexicalBlock(Context));
1078 else if (Context.isLexicalBlockFile()) {
1079 DILexicalBlockFile DBF = DILexicalBlockFile(Context);
1080 return processLexicalBlock(DILexicalBlock(DBF.getScope()));
1083 return processSubprogram(DISubprogram(Context));
1086 /// processSubprogram - Process DISubprogram.
1087 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1088 if (!addSubprogram(SP))
1090 if (SP.getVersion() <= LLVMDebugVersion10)
1091 addCompileUnit(SP.getCompileUnit());
1092 processType(SP.getType());
1095 /// processDeclare - Process DbgDeclareInst.
1096 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
1097 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1101 if (!DV.isVariable())
1104 if (!NodesSeen.insert(DV))
1106 if (DIVariable(N).getVersion() <= LLVMDebugVersion10)
1107 addCompileUnit(DIVariable(N).getCompileUnit());
1108 processType(DIVariable(N).getType());
1111 /// addType - Add type into Tys.
1112 bool DebugInfoFinder::addType(DIType DT) {
1116 if (!NodesSeen.insert(DT))
1123 /// addCompileUnit - Add compile unit into CUs.
1124 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1128 if (!NodesSeen.insert(CU))
1135 /// addGlobalVariable - Add global variable into GVs.
1136 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1137 if (!DIDescriptor(DIG).isGlobalVariable())
1140 if (!NodesSeen.insert(DIG))
1147 // addSubprogram - Add subprgoram into SPs.
1148 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1149 if (!DIDescriptor(SP).isSubprogram())
1152 if (!NodesSeen.insert(SP))
1159 /// getDISubprogram - Find subprogram that is enclosing this scope.
1160 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
1161 DIDescriptor D(Scope);
1162 if (D.isSubprogram())
1163 return DISubprogram(Scope);
1165 if (D.isLexicalBlockFile())
1166 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
1168 if (D.isLexicalBlock())
1169 return getDISubprogram(DILexicalBlock(Scope).getContext());
1171 return DISubprogram();
1174 /// getDICompositeType - Find underlying composite type.
1175 DICompositeType llvm::getDICompositeType(DIType T) {
1176 if (T.isCompositeType())
1177 return DICompositeType(T);
1179 if (T.isDerivedType())
1180 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
1182 return DICompositeType();
1185 /// isSubprogramContext - Return true if Context is either a subprogram
1186 /// or another context nested inside a subprogram.
1187 bool llvm::isSubprogramContext(const MDNode *Context) {
1190 DIDescriptor D(Context);
1191 if (D.isSubprogram())
1194 return isSubprogramContext(DIType(Context).getContext());