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 DILexicalBlock F) : DbgNode(F.DbgNode) {
45 DIDescriptor::DIDescriptor(const DIVariable F) : DbgNode(F.DbgNode) {
48 DIDescriptor::DIDescriptor(const DIType F) : DbgNode(F.DbgNode) {
52 DIDescriptor::getStringField(unsigned Elt) const {
56 if (Elt < DbgNode->getNumOperands())
57 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
58 return MDS->getString();
63 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
67 if (Elt < DbgNode->getNumOperands())
68 if (ConstantInt *CI = dyn_cast<ConstantInt>(DbgNode->getOperand(Elt)))
69 return CI->getZExtValue();
74 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
76 return DIDescriptor();
78 if (Elt < DbgNode->getNumOperands())
80 DIDescriptor(dyn_cast_or_null<const MDNode>(DbgNode->getOperand(Elt)));
81 return DIDescriptor();
84 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
88 if (Elt < DbgNode->getNumOperands())
89 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
93 Constant *DIDescriptor::getConstantField(unsigned Elt) const {
97 if (Elt < DbgNode->getNumOperands())
98 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
102 Function *DIDescriptor::getFunctionField(unsigned Elt) const {
106 if (Elt < DbgNode->getNumOperands())
107 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
111 unsigned DIVariable::getNumAddrElements() const {
112 if (getVersion() <= llvm::LLVMDebugVersion8)
113 return DbgNode->getNumOperands()-6;
114 if (getVersion() == llvm::LLVMDebugVersion9)
115 return DbgNode->getNumOperands()-7;
116 return DbgNode->getNumOperands()-8;
119 /// getInlinedAt - If this variable is inlined then return inline location.
120 MDNode *DIVariable::getInlinedAt() const {
121 if (getVersion() <= llvm::LLVMDebugVersion9)
123 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(7));
126 //===----------------------------------------------------------------------===//
128 //===----------------------------------------------------------------------===//
130 /// isBasicType - Return true if the specified tag is legal for
132 bool DIDescriptor::isBasicType() const {
133 return DbgNode && getTag() == dwarf::DW_TAG_base_type;
136 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
137 bool DIDescriptor::isDerivedType() const {
138 if (!DbgNode) return false;
140 case dwarf::DW_TAG_typedef:
141 case dwarf::DW_TAG_pointer_type:
142 case dwarf::DW_TAG_reference_type:
143 case dwarf::DW_TAG_const_type:
144 case dwarf::DW_TAG_volatile_type:
145 case dwarf::DW_TAG_restrict_type:
146 case dwarf::DW_TAG_member:
147 case dwarf::DW_TAG_inheritance:
148 case dwarf::DW_TAG_friend:
151 // CompositeTypes are currently modelled as DerivedTypes.
152 return isCompositeType();
156 /// isCompositeType - Return true if the specified tag is legal for
158 bool DIDescriptor::isCompositeType() const {
159 if (!DbgNode) return false;
161 case dwarf::DW_TAG_array_type:
162 case dwarf::DW_TAG_structure_type:
163 case dwarf::DW_TAG_union_type:
164 case dwarf::DW_TAG_enumeration_type:
165 case dwarf::DW_TAG_vector_type:
166 case dwarf::DW_TAG_subroutine_type:
167 case dwarf::DW_TAG_class_type:
174 /// isVariable - Return true if the specified tag is legal for DIVariable.
175 bool DIDescriptor::isVariable() const {
176 if (!DbgNode) return false;
178 case dwarf::DW_TAG_auto_variable:
179 case dwarf::DW_TAG_arg_variable:
180 case dwarf::DW_TAG_return_variable:
187 /// isType - Return true if the specified tag is legal for DIType.
188 bool DIDescriptor::isType() const {
189 return isBasicType() || isCompositeType() || isDerivedType();
192 /// isSubprogram - Return true if the specified tag is legal for
194 bool DIDescriptor::isSubprogram() const {
195 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
198 /// isGlobalVariable - Return true if the specified tag is legal for
199 /// DIGlobalVariable.
200 bool DIDescriptor::isGlobalVariable() const {
201 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
202 getTag() == dwarf::DW_TAG_constant);
205 /// isGlobal - Return true if the specified tag is legal for DIGlobal.
206 bool DIDescriptor::isGlobal() const {
207 return isGlobalVariable();
210 /// isUnspecifiedParmeter - Return true if the specified tag is
211 /// DW_TAG_unspecified_parameters.
212 bool DIDescriptor::isUnspecifiedParameter() const {
213 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
216 /// isScope - Return true if the specified tag is one of the scope
218 bool DIDescriptor::isScope() const {
219 if (!DbgNode) return false;
221 case dwarf::DW_TAG_compile_unit:
222 case dwarf::DW_TAG_lexical_block:
223 case dwarf::DW_TAG_subprogram:
224 case dwarf::DW_TAG_namespace:
232 /// isTemplateTypeParameter - Return true if the specified tag is
233 /// DW_TAG_template_type_parameter.
234 bool DIDescriptor::isTemplateTypeParameter() const {
235 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
238 /// isTemplateValueParameter - Return true if the specified tag is
239 /// DW_TAG_template_value_parameter.
240 bool DIDescriptor::isTemplateValueParameter() const {
241 return DbgNode && getTag() == dwarf::DW_TAG_template_value_parameter;
244 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
245 bool DIDescriptor::isCompileUnit() const {
246 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
249 /// isFile - Return true if the specified tag is DW_TAG_file_type.
250 bool DIDescriptor::isFile() const {
251 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
254 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
255 bool DIDescriptor::isNameSpace() const {
256 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
259 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
260 bool DIDescriptor::isLexicalBlock() const {
261 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
264 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
265 bool DIDescriptor::isSubrange() const {
266 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
269 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
270 bool DIDescriptor::isEnumerator() const {
271 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
274 //===----------------------------------------------------------------------===//
275 // Simple Descriptor Constructors and other Methods
276 //===----------------------------------------------------------------------===//
278 DIType::DIType(const MDNode *N) : DIScope(N) {
280 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
285 unsigned DIArray::getNumElements() const {
288 return DbgNode->getNumOperands();
291 /// replaceAllUsesWith - Replace all uses of debug info referenced by
293 void DIType::replaceAllUsesWith(DIDescriptor &D) {
297 // Since we use a TrackingVH for the node, its easy for clients to manufacture
298 // legitimate situations where they want to replaceAllUsesWith() on something
299 // which, due to uniquing, has merged with the source. We shield clients from
300 // this detail by allowing a value to be replaced with replaceAllUsesWith()
303 MDNode *Node = const_cast<MDNode*>(DbgNode);
304 const MDNode *DN = D;
305 const Value *V = cast_or_null<Value>(DN);
306 Node->replaceAllUsesWith(const_cast<Value*>(V));
307 MDNode::deleteTemporary(Node);
311 /// replaceAllUsesWith - Replace all uses of debug info referenced by
313 void DIType::replaceAllUsesWith(MDNode *D) {
317 // Since we use a TrackingVH for the node, its easy for clients to manufacture
318 // legitimate situations where they want to replaceAllUsesWith() on something
319 // which, due to uniquing, has merged with the source. We shield clients from
320 // this detail by allowing a value to be replaced with replaceAllUsesWith()
323 MDNode *Node = const_cast<MDNode*>(DbgNode);
324 const MDNode *DN = D;
325 const Value *V = cast_or_null<Value>(DN);
326 Node->replaceAllUsesWith(const_cast<Value*>(V));
327 MDNode::deleteTemporary(Node);
331 /// Verify - Verify that a compile unit is well formed.
332 bool DICompileUnit::Verify() const {
335 StringRef N = getFilename();
338 // It is possible that directory and produce string is empty.
342 /// Verify - Verify that a type descriptor is well formed.
343 bool DIType::Verify() const {
346 if (!getContext().Verify())
348 unsigned Tag = getTag();
349 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
350 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
351 Tag != dwarf::DW_TAG_reference_type && Tag != dwarf::DW_TAG_restrict_type
352 && Tag != dwarf::DW_TAG_vector_type && Tag != dwarf::DW_TAG_array_type
353 && Tag != dwarf::DW_TAG_enumeration_type
354 && getFilename().empty())
359 /// Verify - Verify that a basic type descriptor is well formed.
360 bool DIBasicType::Verify() const {
361 return isBasicType();
364 /// Verify - Verify that a derived type descriptor is well formed.
365 bool DIDerivedType::Verify() const {
366 return isDerivedType();
369 /// Verify - Verify that a composite type descriptor is well formed.
370 bool DICompositeType::Verify() const {
373 if (!getContext().Verify())
376 DICompileUnit CU = getCompileUnit();
382 /// Verify - Verify that a subprogram descriptor is well formed.
383 bool DISubprogram::Verify() const {
387 if (!getContext().Verify())
390 DICompileUnit CU = getCompileUnit();
394 DICompositeType Ty = getType();
400 /// Verify - Verify that a global variable descriptor is well formed.
401 bool DIGlobalVariable::Verify() const {
405 if (getDisplayName().empty())
408 if (!getContext().Verify())
411 DICompileUnit CU = getCompileUnit();
415 DIType Ty = getType();
419 if (!getGlobal() && !getConstant())
425 /// Verify - Verify that a variable descriptor is well formed.
426 bool DIVariable::Verify() const {
430 if (!getContext().Verify())
433 if (!getCompileUnit().Verify())
436 DIType Ty = getType();
443 /// Verify - Verify that a location descriptor is well formed.
444 bool DILocation::Verify() const {
448 return DbgNode->getNumOperands() == 4;
451 /// Verify - Verify that a namespace descriptor is well formed.
452 bool DINameSpace::Verify() const {
455 if (getName().empty())
457 if (!getCompileUnit().Verify())
462 /// getOriginalTypeSize - If this type is derived from a base type then
463 /// return base type size.
464 uint64_t DIDerivedType::getOriginalTypeSize() const {
465 unsigned Tag = getTag();
466 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
467 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
468 Tag == dwarf::DW_TAG_restrict_type) {
469 DIType BaseType = getTypeDerivedFrom();
470 // If this type is not derived from any type then take conservative
472 if (!BaseType.isValid())
473 return getSizeInBits();
474 if (BaseType.isDerivedType())
475 return DIDerivedType(BaseType).getOriginalTypeSize();
477 return BaseType.getSizeInBits();
480 return getSizeInBits();
483 /// isInlinedFnArgument - Return true if this variable provides debugging
484 /// information for an inlined function arguments.
485 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
486 assert(CurFn && "Invalid function");
487 if (!getContext().isSubprogram())
489 // This variable is not inlined function argument if its scope
490 // does not describe current function.
491 return !(DISubprogram(getContext()).describes(CurFn));
494 /// describes - Return true if this subprogram provides debugging
495 /// information for the function F.
496 bool DISubprogram::describes(const Function *F) {
497 assert(F && "Invalid function");
498 if (F == getFunction())
500 StringRef Name = getLinkageName();
503 if (F->getName() == Name)
508 unsigned DISubprogram::isOptimized() const {
509 assert (DbgNode && "Invalid subprogram descriptor!");
510 if (DbgNode->getNumOperands() == 16)
511 return getUnsignedField(15);
515 StringRef DIScope::getFilename() const {
518 if (isLexicalBlock())
519 return DILexicalBlock(DbgNode).getFilename();
521 return DISubprogram(DbgNode).getFilename();
523 return DICompileUnit(DbgNode).getFilename();
525 return DINameSpace(DbgNode).getFilename();
527 return DIType(DbgNode).getFilename();
529 return DIFile(DbgNode).getFilename();
530 assert(0 && "Invalid DIScope!");
534 StringRef DIScope::getDirectory() const {
537 if (isLexicalBlock())
538 return DILexicalBlock(DbgNode).getDirectory();
540 return DISubprogram(DbgNode).getDirectory();
542 return DICompileUnit(DbgNode).getDirectory();
544 return DINameSpace(DbgNode).getDirectory();
546 return DIType(DbgNode).getDirectory();
548 return DIFile(DbgNode).getDirectory();
549 assert(0 && "Invalid DIScope!");
553 //===----------------------------------------------------------------------===//
554 // DIDescriptor: dump routines for all descriptors.
555 //===----------------------------------------------------------------------===//
558 /// print - Print descriptor.
559 void DIDescriptor::print(raw_ostream &OS) const {
560 OS << "[" << dwarf::TagString(getTag()) << "] ";
561 OS.write_hex((intptr_t) &*DbgNode) << ']';
564 /// print - Print compile unit.
565 void DICompileUnit::print(raw_ostream &OS) const {
567 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
569 OS << " [" << getDirectory() << "/" << getFilename() << "]";
572 /// print - Print type.
573 void DIType::print(raw_ostream &OS) const {
574 if (!DbgNode) return;
576 StringRef Res = getName();
578 OS << " [" << Res << "] ";
580 unsigned Tag = getTag();
581 OS << " [" << dwarf::TagString(Tag) << "] ";
583 // TODO : Print context
584 getCompileUnit().print(OS);
586 << "line " << getLineNumber() << ", "
587 << getSizeInBits() << " bits, "
588 << getAlignInBits() << " bit alignment, "
589 << getOffsetInBits() << " bit offset"
594 else if (isProtected())
595 OS << " [protected] ";
601 DIBasicType(DbgNode).print(OS);
602 else if (isDerivedType())
603 DIDerivedType(DbgNode).print(OS);
604 else if (isCompositeType())
605 DICompositeType(DbgNode).print(OS);
607 OS << "Invalid DIType\n";
614 /// print - Print basic type.
615 void DIBasicType::print(raw_ostream &OS) const {
616 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
619 /// print - Print derived type.
620 void DIDerivedType::print(raw_ostream &OS) const {
621 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
624 /// print - Print composite type.
625 void DICompositeType::print(raw_ostream &OS) const {
626 DIArray A = getTypeArray();
627 OS << " [" << A.getNumElements() << " elements]";
630 /// print - Print subprogram.
631 void DISubprogram::print(raw_ostream &OS) const {
632 StringRef Res = getName();
634 OS << " [" << Res << "] ";
636 unsigned Tag = getTag();
637 OS << " [" << dwarf::TagString(Tag) << "] ";
639 // TODO : Print context
640 getCompileUnit().print(OS);
641 OS << " [" << getLineNumber() << "] ";
652 /// print - Print global variable.
653 void DIGlobalVariable::print(raw_ostream &OS) const {
655 StringRef Res = getName();
657 OS << " [" << Res << "] ";
659 unsigned Tag = getTag();
660 OS << " [" << dwarf::TagString(Tag) << "] ";
662 // TODO : Print context
663 getCompileUnit().print(OS);
664 OS << " [" << getLineNumber() << "] ";
672 if (isGlobalVariable())
673 DIGlobalVariable(DbgNode).print(OS);
677 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
678 const LLVMContext &Ctx) {
679 if (!DL.isUnknown()) { // Print source line info.
680 DIScope Scope(DL.getScope(Ctx));
681 // Omit the directory, because it's likely to be long and uninteresting.
683 CommentOS << Scope.getFilename();
685 CommentOS << "<unknown>";
686 CommentOS << ':' << DL.getLine();
687 if (DL.getCol() != 0)
688 CommentOS << ':' << DL.getCol();
689 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
690 if (!InlinedAtDL.isUnknown()) {
692 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
698 void DIVariable::printExtendedName(raw_ostream &OS) const {
699 const LLVMContext &Ctx = DbgNode->getContext();
700 StringRef Res = getName();
702 OS << Res << "," << getLineNumber();
703 if (MDNode *InlinedAt = getInlinedAt()) {
704 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
705 if (!InlinedAtDL.isUnknown()) {
707 printDebugLoc(InlinedAtDL, OS, Ctx);
713 /// print - Print variable.
714 void DIVariable::print(raw_ostream &OS) const {
715 StringRef Res = getName();
717 OS << " [" << Res << "] ";
719 getCompileUnit().print(OS);
720 OS << " [" << getLineNumber() << "] ";
724 // FIXME: Dump complex addresses
727 /// dump - Print descriptor to dbgs() with a newline.
728 void DIDescriptor::dump() const {
729 print(dbgs()); dbgs() << '\n';
732 /// dump - Print compile unit to dbgs() with a newline.
733 void DICompileUnit::dump() const {
734 print(dbgs()); dbgs() << '\n';
737 /// dump - Print type to dbgs() with a newline.
738 void DIType::dump() const {
739 print(dbgs()); dbgs() << '\n';
742 /// dump - Print basic type to dbgs() with a newline.
743 void DIBasicType::dump() const {
744 print(dbgs()); dbgs() << '\n';
747 /// dump - Print derived type to dbgs() with a newline.
748 void DIDerivedType::dump() const {
749 print(dbgs()); dbgs() << '\n';
752 /// dump - Print composite type to dbgs() with a newline.
753 void DICompositeType::dump() const {
754 print(dbgs()); dbgs() << '\n';
757 /// dump - Print subprogram to dbgs() with a newline.
758 void DISubprogram::dump() const {
759 print(dbgs()); dbgs() << '\n';
762 /// dump - Print global variable.
763 void DIGlobalVariable::dump() const {
764 print(dbgs()); dbgs() << '\n';
767 /// dump - Print variable.
768 void DIVariable::dump() const {
769 print(dbgs()); dbgs() << '\n';
772 /// fixupObjcLikeName - Replace contains special characters used
773 /// in a typical Objective-C names with '.' in a given string.
774 static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
775 bool isObjCLike = false;
776 for (size_t i = 0, e = Str.size(); i < e; ++i) {
781 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
782 C == '+' || C == '(' || C == ')'))
789 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
790 /// suitable to hold function specific information.
791 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, StringRef FuncName) {
792 SmallString<32> Name = StringRef("llvm.dbg.lv.");
793 fixupObjcLikeName(FuncName, Name);
795 return M.getNamedMetadata(Name.str());
798 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
799 /// to hold function specific information.
800 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, StringRef FuncName) {
801 SmallString<32> Name = StringRef("llvm.dbg.lv.");
802 fixupObjcLikeName(FuncName, Name);
804 return M.getOrInsertNamedMetadata(Name.str());
807 /// createInlinedVariable - Create a new inlined variable based on current
809 /// @param DV Current Variable.
810 /// @param InlinedScope Location at current variable is inlined.
811 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
812 LLVMContext &VMContext) {
813 SmallVector<Value *, 16> Elts;
814 // Insert inlined scope as 7th element.
815 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
816 i == 7 ? Elts.push_back(InlinedScope) :
817 Elts.push_back(DV->getOperand(i));
818 return DIVariable(MDNode::get(VMContext, Elts));
821 /// cleanseInlinedVariable - Remove inlined scope from the variable.
822 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
823 SmallVector<Value *, 16> Elts;
824 // Insert inlined scope as 7th element.
825 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
827 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext))):
828 Elts.push_back(DV->getOperand(i));
829 return DIVariable(MDNode::get(VMContext, Elts));
832 //===----------------------------------------------------------------------===//
833 // DebugInfoFinder implementations.
834 //===----------------------------------------------------------------------===//
836 /// processModule - Process entire module and collect debug info.
837 void DebugInfoFinder::processModule(Module &M) {
838 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
839 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
840 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
842 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
845 DebugLoc Loc = BI->getDebugLoc();
849 LLVMContext &Ctx = BI->getContext();
850 DIDescriptor Scope(Loc.getScope(Ctx));
852 if (Scope.isCompileUnit())
853 addCompileUnit(DICompileUnit(Scope));
854 else if (Scope.isSubprogram())
855 processSubprogram(DISubprogram(Scope));
856 else if (Scope.isLexicalBlock())
857 processLexicalBlock(DILexicalBlock(Scope));
859 if (MDNode *IA = Loc.getInlinedAt(Ctx))
860 processLocation(DILocation(IA));
863 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
864 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
865 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
866 if (addGlobalVariable(DIG)) {
867 addCompileUnit(DIG.getCompileUnit());
868 processType(DIG.getType());
873 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
874 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
875 processSubprogram(DISubprogram(NMD->getOperand(i)));
878 /// processLocation - Process DILocation.
879 void DebugInfoFinder::processLocation(DILocation Loc) {
880 if (!Loc.Verify()) return;
881 DIDescriptor S(Loc.getScope());
882 if (S.isCompileUnit())
883 addCompileUnit(DICompileUnit(S));
884 else if (S.isSubprogram())
885 processSubprogram(DISubprogram(S));
886 else if (S.isLexicalBlock())
887 processLexicalBlock(DILexicalBlock(S));
888 processLocation(Loc.getOrigLocation());
891 /// processType - Process DIType.
892 void DebugInfoFinder::processType(DIType DT) {
896 addCompileUnit(DT.getCompileUnit());
897 if (DT.isCompositeType()) {
898 DICompositeType DCT(DT);
899 processType(DCT.getTypeDerivedFrom());
900 DIArray DA = DCT.getTypeArray();
901 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
902 DIDescriptor D = DA.getElement(i);
904 processType(DIType(D));
905 else if (D.isSubprogram())
906 processSubprogram(DISubprogram(D));
908 } else if (DT.isDerivedType()) {
909 DIDerivedType DDT(DT);
910 processType(DDT.getTypeDerivedFrom());
914 /// processLexicalBlock
915 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
916 DIScope Context = LB.getContext();
917 if (Context.isLexicalBlock())
918 return processLexicalBlock(DILexicalBlock(Context));
920 return processSubprogram(DISubprogram(Context));
923 /// processSubprogram - Process DISubprogram.
924 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
925 if (!addSubprogram(SP))
927 addCompileUnit(SP.getCompileUnit());
928 processType(SP.getType());
931 /// processDeclare - Process DbgDeclareInst.
932 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
933 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
937 if (!DV.isVariable())
940 if (!NodesSeen.insert(DV))
943 addCompileUnit(DIVariable(N).getCompileUnit());
944 processType(DIVariable(N).getType());
947 /// addType - Add type into Tys.
948 bool DebugInfoFinder::addType(DIType DT) {
952 if (!NodesSeen.insert(DT))
959 /// addCompileUnit - Add compile unit into CUs.
960 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
964 if (!NodesSeen.insert(CU))
971 /// addGlobalVariable - Add global variable into GVs.
972 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
973 if (!DIDescriptor(DIG).isGlobalVariable())
976 if (!NodesSeen.insert(DIG))
983 // addSubprogram - Add subprgoram into SPs.
984 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
985 if (!DIDescriptor(SP).isSubprogram())
988 if (!NodesSeen.insert(SP))
995 /// getDISubprogram - Find subprogram that is enclosing this scope.
996 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
997 DIDescriptor D(Scope);
998 if (D.isSubprogram())
999 return DISubprogram(Scope);
1001 if (D.isLexicalBlock())
1002 return getDISubprogram(DILexicalBlock(Scope).getContext());
1004 return DISubprogram();
1007 /// getDICompositeType - Find underlying composite type.
1008 DICompositeType llvm::getDICompositeType(DIType T) {
1009 if (T.isCompositeType())
1010 return DICompositeType(T);
1012 if (T.isDerivedType())
1013 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
1015 return DICompositeType();