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 && Tag != dwarf::DW_TAG_subroutine_type
371 && getFilename().empty())
376 /// Verify - Verify that a basic type descriptor is well formed.
377 bool DIBasicType::Verify() const {
378 return isBasicType();
381 /// Verify - Verify that a derived type descriptor is well formed.
382 bool DIDerivedType::Verify() const {
383 return isDerivedType();
386 /// Verify - Verify that a composite type descriptor is well formed.
387 bool DICompositeType::Verify() const {
390 if (getContext() && !getContext().Verify())
396 /// Verify - Verify that a subprogram descriptor is well formed.
397 bool DISubprogram::Verify() const {
401 if (getContext() && !getContext().Verify())
404 DICompositeType Ty = getType();
410 /// Verify - Verify that a global variable descriptor is well formed.
411 bool DIGlobalVariable::Verify() const {
415 if (getDisplayName().empty())
418 if (getContext() && !getContext().Verify())
421 DIType Ty = getType();
425 if (!getGlobal() && !getConstant())
431 /// Verify - Verify that a variable descriptor is well formed.
432 bool DIVariable::Verify() const {
436 if (getContext() && !getContext().Verify())
439 DIType Ty = getType();
446 /// Verify - Verify that a location descriptor is well formed.
447 bool DILocation::Verify() const {
451 return DbgNode->getNumOperands() == 4;
454 /// Verify - Verify that a namespace descriptor is well formed.
455 bool DINameSpace::Verify() const {
458 if (getName().empty())
463 /// getOriginalTypeSize - If this type is derived from a base type then
464 /// return base type size.
465 uint64_t DIDerivedType::getOriginalTypeSize() const {
466 unsigned Tag = getTag();
467 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
468 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
469 Tag == dwarf::DW_TAG_restrict_type) {
470 DIType BaseType = getTypeDerivedFrom();
471 // If this type is not derived from any type then take conservative
473 if (!BaseType.isValid())
474 return getSizeInBits();
475 if (BaseType.isDerivedType())
476 return DIDerivedType(BaseType).getOriginalTypeSize();
478 return BaseType.getSizeInBits();
481 return getSizeInBits();
484 /// isInlinedFnArgument - Return true if this variable provides debugging
485 /// information for an inlined function arguments.
486 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
487 assert(CurFn && "Invalid function");
488 if (!getContext().isSubprogram())
490 // This variable is not inlined function argument if its scope
491 // does not describe current function.
492 return !(DISubprogram(getContext()).describes(CurFn));
495 /// describes - Return true if this subprogram provides debugging
496 /// information for the function F.
497 bool DISubprogram::describes(const Function *F) {
498 assert(F && "Invalid function");
499 if (F == getFunction())
501 StringRef Name = getLinkageName();
504 if (F->getName() == Name)
509 unsigned DISubprogram::isOptimized() const {
510 assert (DbgNode && "Invalid subprogram descriptor!");
511 if (DbgNode->getNumOperands() == 16)
512 return getUnsignedField(15);
516 MDNode *DISubprogram::getVariablesNodes() const {
517 if (!DbgNode || DbgNode->getNumOperands() <= 19)
519 if (MDNode *Temp = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
520 return dyn_cast_or_null<MDNode>(Temp->getOperand(0));
524 DIArray DISubprogram::getVariables() const {
525 if (!DbgNode || DbgNode->getNumOperands() <= 19)
527 if (MDNode *T = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
528 if (MDNode *A = dyn_cast_or_null<MDNode>(T->getOperand(0)))
533 StringRef DIScope::getFilename() const {
536 if (isLexicalBlock())
537 return DILexicalBlock(DbgNode).getFilename();
539 return DISubprogram(DbgNode).getFilename();
541 return DICompileUnit(DbgNode).getFilename();
543 return DINameSpace(DbgNode).getFilename();
545 return DIType(DbgNode).getFilename();
547 return DIFile(DbgNode).getFilename();
548 assert(0 && "Invalid DIScope!");
552 StringRef DIScope::getDirectory() const {
555 if (isLexicalBlock())
556 return DILexicalBlock(DbgNode).getDirectory();
558 return DISubprogram(DbgNode).getDirectory();
560 return DICompileUnit(DbgNode).getDirectory();
562 return DINameSpace(DbgNode).getDirectory();
564 return DIType(DbgNode).getDirectory();
566 return DIFile(DbgNode).getDirectory();
567 assert(0 && "Invalid DIScope!");
571 DIArray DICompileUnit::getEnumTypes() const {
572 if (!DbgNode || DbgNode->getNumOperands() < 14)
575 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(10)))
576 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
581 DIArray DICompileUnit::getRetainedTypes() const {
582 if (!DbgNode || DbgNode->getNumOperands() < 14)
585 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(11)))
586 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
591 DIArray DICompileUnit::getSubprograms() const {
592 if (!DbgNode || DbgNode->getNumOperands() < 14)
595 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(12)))
596 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
602 DIArray DICompileUnit::getGlobalVariables() const {
603 if (!DbgNode || DbgNode->getNumOperands() < 14)
606 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(13)))
607 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0)))
612 //===----------------------------------------------------------------------===//
613 // DIDescriptor: dump routines for all descriptors.
614 //===----------------------------------------------------------------------===//
617 /// print - Print descriptor.
618 void DIDescriptor::print(raw_ostream &OS) const {
619 OS << "[" << dwarf::TagString(getTag()) << "] ";
620 OS.write_hex((intptr_t) &*DbgNode) << ']';
623 /// print - Print compile unit.
624 void DICompileUnit::print(raw_ostream &OS) const {
626 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
628 OS << " [" << getDirectory() << "/" << getFilename() << "]";
631 /// print - Print type.
632 void DIType::print(raw_ostream &OS) const {
633 if (!DbgNode) return;
635 StringRef Res = getName();
637 OS << " [" << Res << "] ";
639 unsigned Tag = getTag();
640 OS << " [" << dwarf::TagString(Tag) << "] ";
642 // TODO : Print context
644 << "line " << getLineNumber() << ", "
645 << getSizeInBits() << " bits, "
646 << getAlignInBits() << " bit alignment, "
647 << getOffsetInBits() << " bit offset"
652 else if (isProtected())
653 OS << " [protected] ";
659 DIBasicType(DbgNode).print(OS);
660 else if (isDerivedType())
661 DIDerivedType(DbgNode).print(OS);
662 else if (isCompositeType())
663 DICompositeType(DbgNode).print(OS);
665 OS << "Invalid DIType\n";
672 /// print - Print basic type.
673 void DIBasicType::print(raw_ostream &OS) const {
674 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
677 /// print - Print derived type.
678 void DIDerivedType::print(raw_ostream &OS) const {
679 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
682 /// print - Print composite type.
683 void DICompositeType::print(raw_ostream &OS) const {
684 DIArray A = getTypeArray();
685 OS << " [" << A.getNumElements() << " elements]";
688 /// print - Print subprogram.
689 void DISubprogram::print(raw_ostream &OS) const {
690 StringRef Res = getName();
692 OS << " [" << Res << "] ";
694 unsigned Tag = getTag();
695 OS << " [" << dwarf::TagString(Tag) << "] ";
697 // TODO : Print context
698 OS << " [" << getLineNumber() << "] ";
709 /// print - Print global variable.
710 void DIGlobalVariable::print(raw_ostream &OS) const {
712 StringRef Res = getName();
714 OS << " [" << Res << "] ";
716 unsigned Tag = getTag();
717 OS << " [" << dwarf::TagString(Tag) << "] ";
719 // TODO : Print context
720 OS << " [" << getLineNumber() << "] ";
728 if (isGlobalVariable())
729 DIGlobalVariable(DbgNode).print(OS);
733 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
734 const LLVMContext &Ctx) {
735 if (!DL.isUnknown()) { // Print source line info.
736 DIScope Scope(DL.getScope(Ctx));
737 // Omit the directory, because it's likely to be long and uninteresting.
739 CommentOS << Scope.getFilename();
741 CommentOS << "<unknown>";
742 CommentOS << ':' << DL.getLine();
743 if (DL.getCol() != 0)
744 CommentOS << ':' << DL.getCol();
745 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
746 if (!InlinedAtDL.isUnknown()) {
748 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
754 void DIVariable::printExtendedName(raw_ostream &OS) const {
755 const LLVMContext &Ctx = DbgNode->getContext();
756 StringRef Res = getName();
758 OS << Res << "," << getLineNumber();
759 if (MDNode *InlinedAt = getInlinedAt()) {
760 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
761 if (!InlinedAtDL.isUnknown()) {
763 printDebugLoc(InlinedAtDL, OS, Ctx);
769 /// print - Print variable.
770 void DIVariable::print(raw_ostream &OS) const {
771 StringRef Res = getName();
773 OS << " [" << Res << "] ";
775 OS << " [" << getLineNumber() << "] ";
779 // FIXME: Dump complex addresses
782 /// dump - Print descriptor to dbgs() with a newline.
783 void DIDescriptor::dump() const {
784 print(dbgs()); dbgs() << '\n';
787 /// dump - Print compile unit to dbgs() with a newline.
788 void DICompileUnit::dump() const {
789 print(dbgs()); dbgs() << '\n';
792 /// dump - Print type to dbgs() with a newline.
793 void DIType::dump() const {
794 print(dbgs()); dbgs() << '\n';
797 /// dump - Print basic type to dbgs() with a newline.
798 void DIBasicType::dump() const {
799 print(dbgs()); dbgs() << '\n';
802 /// dump - Print derived type to dbgs() with a newline.
803 void DIDerivedType::dump() const {
804 print(dbgs()); dbgs() << '\n';
807 /// dump - Print composite type to dbgs() with a newline.
808 void DICompositeType::dump() const {
809 print(dbgs()); dbgs() << '\n';
812 /// dump - Print subprogram to dbgs() with a newline.
813 void DISubprogram::dump() const {
814 print(dbgs()); dbgs() << '\n';
817 /// dump - Print global variable.
818 void DIGlobalVariable::dump() const {
819 print(dbgs()); dbgs() << '\n';
822 /// dump - Print variable.
823 void DIVariable::dump() const {
824 print(dbgs()); dbgs() << '\n';
827 /// fixupObjcLikeName - Replace contains special characters used
828 /// in a typical Objective-C names with '.' in a given string.
829 static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
830 bool isObjCLike = false;
831 for (size_t i = 0, e = Str.size(); i < e; ++i) {
836 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
837 C == '+' || C == '(' || C == ')'))
844 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
845 /// suitable to hold function specific information.
846 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
847 SmallString<32> Name = StringRef("llvm.dbg.lv.");
848 StringRef FName = "fn";
849 if (Fn.getFunction())
850 FName = Fn.getFunction()->getName();
852 FName = Fn.getName();
854 if (FName.startswith(StringRef(&One, 1)))
855 FName = FName.substr(1);
856 fixupObjcLikeName(FName, Name);
857 return M.getNamedMetadata(Name.str());
860 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
861 /// to hold function specific information.
862 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
863 SmallString<32> Name = StringRef("llvm.dbg.lv.");
864 StringRef FName = "fn";
865 if (Fn.getFunction())
866 FName = Fn.getFunction()->getName();
868 FName = Fn.getName();
870 if (FName.startswith(StringRef(&One, 1)))
871 FName = FName.substr(1);
872 fixupObjcLikeName(FName, Name);
874 return M.getOrInsertNamedMetadata(Name.str());
877 /// createInlinedVariable - Create a new inlined variable based on current
879 /// @param DV Current Variable.
880 /// @param InlinedScope Location at current variable is inlined.
881 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
882 LLVMContext &VMContext) {
883 SmallVector<Value *, 16> Elts;
884 // Insert inlined scope as 7th element.
885 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
886 i == 7 ? Elts.push_back(InlinedScope) :
887 Elts.push_back(DV->getOperand(i));
888 return DIVariable(MDNode::get(VMContext, Elts));
891 /// cleanseInlinedVariable - Remove inlined scope from the variable.
892 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
893 SmallVector<Value *, 16> Elts;
894 // Insert inlined scope as 7th element.
895 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
897 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext))):
898 Elts.push_back(DV->getOperand(i));
899 return DIVariable(MDNode::get(VMContext, Elts));
902 //===----------------------------------------------------------------------===//
903 // DebugInfoFinder implementations.
904 //===----------------------------------------------------------------------===//
906 /// processModule - Process entire module and collect debug info.
907 void DebugInfoFinder::processModule(Module &M) {
908 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu"))
909 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i)
910 addCompileUnit(DICompileUnit(CU_Nodes->getOperand(i)));
912 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
913 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
914 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
916 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
919 DebugLoc Loc = BI->getDebugLoc();
923 LLVMContext &Ctx = BI->getContext();
924 DIDescriptor Scope(Loc.getScope(Ctx));
926 if (Scope.isCompileUnit())
927 addCompileUnit(DICompileUnit(Scope));
928 else if (Scope.isSubprogram())
929 processSubprogram(DISubprogram(Scope));
930 else if (Scope.isLexicalBlock())
931 processLexicalBlock(DILexicalBlock(Scope));
933 if (MDNode *IA = Loc.getInlinedAt(Ctx))
934 processLocation(DILocation(IA));
937 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
938 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
939 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
940 if (addGlobalVariable(DIG)) {
941 if (DIG.getVersion() <= LLVMDebugVersion10)
942 addCompileUnit(DIG.getCompileUnit());
943 processType(DIG.getType());
948 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
949 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
950 processSubprogram(DISubprogram(NMD->getOperand(i)));
953 /// processLocation - Process DILocation.
954 void DebugInfoFinder::processLocation(DILocation Loc) {
955 if (!Loc.Verify()) return;
956 DIDescriptor S(Loc.getScope());
957 if (S.isCompileUnit())
958 addCompileUnit(DICompileUnit(S));
959 else if (S.isSubprogram())
960 processSubprogram(DISubprogram(S));
961 else if (S.isLexicalBlock())
962 processLexicalBlock(DILexicalBlock(S));
963 processLocation(Loc.getOrigLocation());
966 /// processType - Process DIType.
967 void DebugInfoFinder::processType(DIType DT) {
970 if (DT.getVersion() <= LLVMDebugVersion10)
971 addCompileUnit(DT.getCompileUnit());
972 if (DT.isCompositeType()) {
973 DICompositeType DCT(DT);
974 processType(DCT.getTypeDerivedFrom());
975 DIArray DA = DCT.getTypeArray();
976 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
977 DIDescriptor D = DA.getElement(i);
979 processType(DIType(D));
980 else if (D.isSubprogram())
981 processSubprogram(DISubprogram(D));
983 } else if (DT.isDerivedType()) {
984 DIDerivedType DDT(DT);
985 processType(DDT.getTypeDerivedFrom());
989 /// processLexicalBlock
990 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
991 DIScope Context = LB.getContext();
992 if (Context.isLexicalBlock())
993 return processLexicalBlock(DILexicalBlock(Context));
995 return processSubprogram(DISubprogram(Context));
998 /// processSubprogram - Process DISubprogram.
999 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1000 if (!addSubprogram(SP))
1002 if (SP.getVersion() <= LLVMDebugVersion10)
1003 addCompileUnit(SP.getCompileUnit());
1004 processType(SP.getType());
1007 /// processDeclare - Process DbgDeclareInst.
1008 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
1009 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1013 if (!DV.isVariable())
1016 if (!NodesSeen.insert(DV))
1018 if (DIVariable(N).getVersion() <= LLVMDebugVersion10)
1019 addCompileUnit(DIVariable(N).getCompileUnit());
1020 processType(DIVariable(N).getType());
1023 /// addType - Add type into Tys.
1024 bool DebugInfoFinder::addType(DIType DT) {
1028 if (!NodesSeen.insert(DT))
1035 /// addCompileUnit - Add compile unit into CUs.
1036 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1040 if (!NodesSeen.insert(CU))
1047 /// addGlobalVariable - Add global variable into GVs.
1048 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1049 if (!DIDescriptor(DIG).isGlobalVariable())
1052 if (!NodesSeen.insert(DIG))
1059 // addSubprogram - Add subprgoram into SPs.
1060 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1061 if (!DIDescriptor(SP).isSubprogram())
1064 if (!NodesSeen.insert(SP))
1071 /// getDISubprogram - Find subprogram that is enclosing this scope.
1072 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
1073 DIDescriptor D(Scope);
1074 if (D.isSubprogram())
1075 return DISubprogram(Scope);
1077 if (D.isLexicalBlock())
1078 return getDISubprogram(DILexicalBlock(Scope).getContext());
1080 return DISubprogram();
1083 /// getDICompositeType - Find underlying composite type.
1084 DICompositeType llvm::getDICompositeType(DIType T) {
1085 if (T.isCompositeType())
1086 return DICompositeType(T);
1088 if (T.isDerivedType())
1089 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
1091 return DICompositeType();
1094 /// isSubprogramContext - Return true if Context is either a subprogram
1095 /// or another context nested inside a subprogram.
1096 bool llvm::isSubprogramContext(const MDNode *Context) {
1099 DIDescriptor D(Context);
1100 if (D.isSubprogram())
1103 return isSubprogramContext(DIType(Context).getContext());