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 /// isUnsignedDIType - Return true if type encoding is unsigned.
332 bool DIType::isUnsignedDIType() {
333 DIDerivedType DTy(DbgNode);
335 return DTy.getTypeDerivedFrom().isUnsignedDIType();
337 DIBasicType BTy(DbgNode);
339 unsigned Encoding = BTy.getEncoding();
340 if (Encoding == dwarf::DW_ATE_unsigned ||
341 Encoding == dwarf::DW_ATE_unsigned_char)
347 /// Verify - Verify that a compile unit is well formed.
348 bool DICompileUnit::Verify() const {
351 StringRef N = getFilename();
354 // It is possible that directory and produce string is empty.
358 /// Verify - Verify that a type descriptor is well formed.
359 bool DIType::Verify() const {
362 if (getContext() && !getContext().Verify())
364 unsigned Tag = getTag();
365 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
366 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
367 Tag != dwarf::DW_TAG_reference_type && Tag != dwarf::DW_TAG_restrict_type
368 && Tag != dwarf::DW_TAG_vector_type && Tag != dwarf::DW_TAG_array_type
369 && Tag != dwarf::DW_TAG_enumeration_type
370 && getFilename().empty())
375 /// Verify - Verify that a basic type descriptor is well formed.
376 bool DIBasicType::Verify() const {
377 return isBasicType();
380 /// Verify - Verify that a derived type descriptor is well formed.
381 bool DIDerivedType::Verify() const {
382 return isDerivedType();
385 /// Verify - Verify that a composite type descriptor is well formed.
386 bool DICompositeType::Verify() const {
389 if (getContext() && !getContext().Verify())
395 /// Verify - Verify that a subprogram descriptor is well formed.
396 bool DISubprogram::Verify() const {
400 if (getContext() && !getContext().Verify())
403 DICompositeType Ty = getType();
409 /// Verify - Verify that a global variable descriptor is well formed.
410 bool DIGlobalVariable::Verify() const {
414 if (getDisplayName().empty())
417 if (getContext() && !getContext().Verify())
420 DIType Ty = getType();
424 if (!getGlobal() && !getConstant())
430 /// Verify - Verify that a variable descriptor is well formed.
431 bool DIVariable::Verify() const {
435 if (getContext() && !getContext().Verify())
438 DIType Ty = getType();
445 /// Verify - Verify that a location descriptor is well formed.
446 bool DILocation::Verify() const {
450 return DbgNode->getNumOperands() == 4;
453 /// Verify - Verify that a namespace descriptor is well formed.
454 bool DINameSpace::Verify() const {
457 if (getName().empty())
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 DIArray DICompileUnit::getEnumTypes() const {
554 if (!DbgNode || DbgNode->getNumOperands() < 14)
557 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(10)))
558 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
563 DIArray DICompileUnit::getRetainedTypes() const {
564 if (!DbgNode || DbgNode->getNumOperands() < 14)
567 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(11)))
568 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
573 DIArray DICompileUnit::getSubprograms() const {
574 if (!DbgNode || DbgNode->getNumOperands() < 14)
577 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(12)))
578 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
584 DIArray DICompileUnit::getGlobalVariables() const {
585 if (!DbgNode || DbgNode->getNumOperands() < 14)
588 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(13)))
589 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
594 //===----------------------------------------------------------------------===//
595 // DIDescriptor: dump routines for all descriptors.
596 //===----------------------------------------------------------------------===//
599 /// print - Print descriptor.
600 void DIDescriptor::print(raw_ostream &OS) const {
601 OS << "[" << dwarf::TagString(getTag()) << "] ";
602 OS.write_hex((intptr_t) &*DbgNode) << ']';
605 /// print - Print compile unit.
606 void DICompileUnit::print(raw_ostream &OS) const {
608 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
610 OS << " [" << getDirectory() << "/" << getFilename() << "]";
613 /// print - Print type.
614 void DIType::print(raw_ostream &OS) const {
615 if (!DbgNode) return;
617 StringRef Res = getName();
619 OS << " [" << Res << "] ";
621 unsigned Tag = getTag();
622 OS << " [" << dwarf::TagString(Tag) << "] ";
624 // TODO : Print context
626 << "line " << getLineNumber() << ", "
627 << getSizeInBits() << " bits, "
628 << getAlignInBits() << " bit alignment, "
629 << getOffsetInBits() << " bit offset"
634 else if (isProtected())
635 OS << " [protected] ";
641 DIBasicType(DbgNode).print(OS);
642 else if (isDerivedType())
643 DIDerivedType(DbgNode).print(OS);
644 else if (isCompositeType())
645 DICompositeType(DbgNode).print(OS);
647 OS << "Invalid DIType\n";
654 /// print - Print basic type.
655 void DIBasicType::print(raw_ostream &OS) const {
656 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
659 /// print - Print derived type.
660 void DIDerivedType::print(raw_ostream &OS) const {
661 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
664 /// print - Print composite type.
665 void DICompositeType::print(raw_ostream &OS) const {
666 DIArray A = getTypeArray();
667 OS << " [" << A.getNumElements() << " elements]";
670 /// print - Print subprogram.
671 void DISubprogram::print(raw_ostream &OS) const {
672 StringRef Res = getName();
674 OS << " [" << Res << "] ";
676 unsigned Tag = getTag();
677 OS << " [" << dwarf::TagString(Tag) << "] ";
679 // TODO : Print context
680 OS << " [" << getLineNumber() << "] ";
691 /// print - Print global variable.
692 void DIGlobalVariable::print(raw_ostream &OS) const {
694 StringRef Res = getName();
696 OS << " [" << Res << "] ";
698 unsigned Tag = getTag();
699 OS << " [" << dwarf::TagString(Tag) << "] ";
701 // TODO : Print context
702 OS << " [" << getLineNumber() << "] ";
710 if (isGlobalVariable())
711 DIGlobalVariable(DbgNode).print(OS);
715 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
716 const LLVMContext &Ctx) {
717 if (!DL.isUnknown()) { // Print source line info.
718 DIScope Scope(DL.getScope(Ctx));
719 // Omit the directory, because it's likely to be long and uninteresting.
721 CommentOS << Scope.getFilename();
723 CommentOS << "<unknown>";
724 CommentOS << ':' << DL.getLine();
725 if (DL.getCol() != 0)
726 CommentOS << ':' << DL.getCol();
727 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
728 if (!InlinedAtDL.isUnknown()) {
730 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
736 void DIVariable::printExtendedName(raw_ostream &OS) const {
737 const LLVMContext &Ctx = DbgNode->getContext();
738 StringRef Res = getName();
740 OS << Res << "," << getLineNumber();
741 if (MDNode *InlinedAt = getInlinedAt()) {
742 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
743 if (!InlinedAtDL.isUnknown()) {
745 printDebugLoc(InlinedAtDL, OS, Ctx);
751 /// print - Print variable.
752 void DIVariable::print(raw_ostream &OS) const {
753 StringRef Res = getName();
755 OS << " [" << Res << "] ";
757 OS << " [" << getLineNumber() << "] ";
761 // FIXME: Dump complex addresses
764 /// dump - Print descriptor to dbgs() with a newline.
765 void DIDescriptor::dump() const {
766 print(dbgs()); dbgs() << '\n';
769 /// dump - Print compile unit to dbgs() with a newline.
770 void DICompileUnit::dump() const {
771 print(dbgs()); dbgs() << '\n';
774 /// dump - Print type to dbgs() with a newline.
775 void DIType::dump() const {
776 print(dbgs()); dbgs() << '\n';
779 /// dump - Print basic type to dbgs() with a newline.
780 void DIBasicType::dump() const {
781 print(dbgs()); dbgs() << '\n';
784 /// dump - Print derived type to dbgs() with a newline.
785 void DIDerivedType::dump() const {
786 print(dbgs()); dbgs() << '\n';
789 /// dump - Print composite type to dbgs() with a newline.
790 void DICompositeType::dump() const {
791 print(dbgs()); dbgs() << '\n';
794 /// dump - Print subprogram to dbgs() with a newline.
795 void DISubprogram::dump() const {
796 print(dbgs()); dbgs() << '\n';
799 /// dump - Print global variable.
800 void DIGlobalVariable::dump() const {
801 print(dbgs()); dbgs() << '\n';
804 /// dump - Print variable.
805 void DIVariable::dump() const {
806 print(dbgs()); dbgs() << '\n';
809 /// fixupObjcLikeName - Replace contains special characters used
810 /// in a typical Objective-C names with '.' in a given string.
811 static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
812 bool isObjCLike = false;
813 for (size_t i = 0, e = Str.size(); i < e; ++i) {
818 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
819 C == '+' || C == '(' || C == ')'))
826 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
827 /// suitable to hold function specific information.
828 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, StringRef FuncName) {
829 SmallString<32> Name = StringRef("llvm.dbg.lv.");
830 fixupObjcLikeName(FuncName, Name);
832 return M.getNamedMetadata(Name.str());
835 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
836 /// to hold function specific information.
837 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, StringRef FuncName) {
838 SmallString<32> Name = StringRef("llvm.dbg.lv.");
839 fixupObjcLikeName(FuncName, Name);
841 return M.getOrInsertNamedMetadata(Name.str());
844 /// createInlinedVariable - Create a new inlined variable based on current
846 /// @param DV Current Variable.
847 /// @param InlinedScope Location at current variable is inlined.
848 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
849 LLVMContext &VMContext) {
850 SmallVector<Value *, 16> Elts;
851 // Insert inlined scope as 7th element.
852 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
853 i == 7 ? Elts.push_back(InlinedScope) :
854 Elts.push_back(DV->getOperand(i));
855 return DIVariable(MDNode::get(VMContext, Elts));
858 /// cleanseInlinedVariable - Remove inlined scope from the variable.
859 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
860 SmallVector<Value *, 16> Elts;
861 // Insert inlined scope as 7th element.
862 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
864 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext))):
865 Elts.push_back(DV->getOperand(i));
866 return DIVariable(MDNode::get(VMContext, Elts));
869 //===----------------------------------------------------------------------===//
870 // DebugInfoFinder implementations.
871 //===----------------------------------------------------------------------===//
873 /// processModule - Process entire module and collect debug info.
874 void DebugInfoFinder::processModule(Module &M) {
875 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
876 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
877 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
879 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
882 DebugLoc Loc = BI->getDebugLoc();
886 LLVMContext &Ctx = BI->getContext();
887 DIDescriptor Scope(Loc.getScope(Ctx));
889 if (Scope.isCompileUnit())
890 addCompileUnit(DICompileUnit(Scope));
891 else if (Scope.isSubprogram())
892 processSubprogram(DISubprogram(Scope));
893 else if (Scope.isLexicalBlock())
894 processLexicalBlock(DILexicalBlock(Scope));
896 if (MDNode *IA = Loc.getInlinedAt(Ctx))
897 processLocation(DILocation(IA));
900 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
901 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
902 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
903 if (addGlobalVariable(DIG)) {
904 addCompileUnit(DIG.getCompileUnit());
905 processType(DIG.getType());
910 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
911 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
912 processSubprogram(DISubprogram(NMD->getOperand(i)));
915 /// processLocation - Process DILocation.
916 void DebugInfoFinder::processLocation(DILocation Loc) {
917 if (!Loc.Verify()) return;
918 DIDescriptor S(Loc.getScope());
919 if (S.isCompileUnit())
920 addCompileUnit(DICompileUnit(S));
921 else if (S.isSubprogram())
922 processSubprogram(DISubprogram(S));
923 else if (S.isLexicalBlock())
924 processLexicalBlock(DILexicalBlock(S));
925 processLocation(Loc.getOrigLocation());
928 /// processType - Process DIType.
929 void DebugInfoFinder::processType(DIType DT) {
933 addCompileUnit(DT.getCompileUnit());
934 if (DT.isCompositeType()) {
935 DICompositeType DCT(DT);
936 processType(DCT.getTypeDerivedFrom());
937 DIArray DA = DCT.getTypeArray();
938 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
939 DIDescriptor D = DA.getElement(i);
941 processType(DIType(D));
942 else if (D.isSubprogram())
943 processSubprogram(DISubprogram(D));
945 } else if (DT.isDerivedType()) {
946 DIDerivedType DDT(DT);
947 processType(DDT.getTypeDerivedFrom());
951 /// processLexicalBlock
952 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
953 DIScope Context = LB.getContext();
954 if (Context.isLexicalBlock())
955 return processLexicalBlock(DILexicalBlock(Context));
957 return processSubprogram(DISubprogram(Context));
960 /// processSubprogram - Process DISubprogram.
961 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
962 if (!addSubprogram(SP))
964 addCompileUnit(SP.getCompileUnit());
965 processType(SP.getType());
968 /// processDeclare - Process DbgDeclareInst.
969 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
970 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
974 if (!DV.isVariable())
977 if (!NodesSeen.insert(DV))
980 addCompileUnit(DIVariable(N).getCompileUnit());
981 processType(DIVariable(N).getType());
984 /// addType - Add type into Tys.
985 bool DebugInfoFinder::addType(DIType DT) {
989 if (!NodesSeen.insert(DT))
996 /// addCompileUnit - Add compile unit into CUs.
997 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1001 if (!NodesSeen.insert(CU))
1008 /// addGlobalVariable - Add global variable into GVs.
1009 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1010 if (!DIDescriptor(DIG).isGlobalVariable())
1013 if (!NodesSeen.insert(DIG))
1020 // addSubprogram - Add subprgoram into SPs.
1021 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1022 if (!DIDescriptor(SP).isSubprogram())
1025 if (!NodesSeen.insert(SP))
1032 /// getDISubprogram - Find subprogram that is enclosing this scope.
1033 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
1034 DIDescriptor D(Scope);
1035 if (D.isSubprogram())
1036 return DISubprogram(Scope);
1038 if (D.isLexicalBlock())
1039 return getDISubprogram(DILexicalBlock(Scope).getContext());
1041 return DISubprogram();
1044 /// getDICompositeType - Find underlying composite type.
1045 DICompositeType llvm::getDICompositeType(DIType T) {
1046 if (T.isCompositeType())
1047 return DICompositeType(T);
1049 if (T.isDerivedType())
1050 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
1052 return DICompositeType();
1055 /// isSubprogramContext - Return true if Context is either a subprogram
1056 /// or another context nested inside a subprogram.
1057 bool llvm::isSubprogramContext(const MDNode *Context) {
1060 DIDescriptor D(Context);
1061 if (D.isSubprogram())
1064 return isSubprogramContext(DIType(Context).getContext());