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 return DbgNode->getNumOperands()-7;
118 //===----------------------------------------------------------------------===//
120 //===----------------------------------------------------------------------===//
122 /// isBasicType - Return true if the specified tag is legal for
124 bool DIDescriptor::isBasicType() const {
125 return DbgNode && getTag() == dwarf::DW_TAG_base_type;
128 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
129 bool DIDescriptor::isDerivedType() const {
130 if (!DbgNode) return false;
132 case dwarf::DW_TAG_typedef:
133 case dwarf::DW_TAG_pointer_type:
134 case dwarf::DW_TAG_reference_type:
135 case dwarf::DW_TAG_const_type:
136 case dwarf::DW_TAG_volatile_type:
137 case dwarf::DW_TAG_restrict_type:
138 case dwarf::DW_TAG_member:
139 case dwarf::DW_TAG_inheritance:
140 case dwarf::DW_TAG_friend:
143 // CompositeTypes are currently modelled as DerivedTypes.
144 return isCompositeType();
148 /// isCompositeType - Return true if the specified tag is legal for
150 bool DIDescriptor::isCompositeType() const {
151 if (!DbgNode) return false;
153 case dwarf::DW_TAG_array_type:
154 case dwarf::DW_TAG_structure_type:
155 case dwarf::DW_TAG_union_type:
156 case dwarf::DW_TAG_enumeration_type:
157 case dwarf::DW_TAG_vector_type:
158 case dwarf::DW_TAG_subroutine_type:
159 case dwarf::DW_TAG_class_type:
166 /// isVariable - Return true if the specified tag is legal for DIVariable.
167 bool DIDescriptor::isVariable() const {
168 if (!DbgNode) return false;
170 case dwarf::DW_TAG_auto_variable:
171 case dwarf::DW_TAG_arg_variable:
172 case dwarf::DW_TAG_return_variable:
179 /// isType - Return true if the specified tag is legal for DIType.
180 bool DIDescriptor::isType() const {
181 return isBasicType() || isCompositeType() || isDerivedType();
184 /// isSubprogram - Return true if the specified tag is legal for
186 bool DIDescriptor::isSubprogram() const {
187 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
190 /// isGlobalVariable - Return true if the specified tag is legal for
191 /// DIGlobalVariable.
192 bool DIDescriptor::isGlobalVariable() const {
193 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
194 getTag() == dwarf::DW_TAG_constant);
197 /// isGlobal - Return true if the specified tag is legal for DIGlobal.
198 bool DIDescriptor::isGlobal() const {
199 return isGlobalVariable();
202 /// isUnspecifiedParmeter - Return true if the specified tab is
203 /// DW_TAG_unspecified_parameters.
204 bool DIDescriptor::isUnspecifiedParameter() const {
205 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
208 /// isScope - Return true if the specified tag is one of the scope
210 bool DIDescriptor::isScope() const {
211 if (!DbgNode) return false;
213 case dwarf::DW_TAG_compile_unit:
214 case dwarf::DW_TAG_lexical_block:
215 case dwarf::DW_TAG_subprogram:
216 case dwarf::DW_TAG_namespace:
224 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
225 bool DIDescriptor::isCompileUnit() const {
226 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
229 /// isFile - Return true if the specified tag is DW_TAG_file_type.
230 bool DIDescriptor::isFile() const {
231 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
234 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
235 bool DIDescriptor::isNameSpace() const {
236 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
239 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
240 bool DIDescriptor::isLexicalBlock() const {
241 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
244 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
245 bool DIDescriptor::isSubrange() const {
246 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
249 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
250 bool DIDescriptor::isEnumerator() const {
251 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
254 //===----------------------------------------------------------------------===//
255 // Simple Descriptor Constructors and other Methods
256 //===----------------------------------------------------------------------===//
258 DIType::DIType(const MDNode *N) : DIScope(N) {
260 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
265 unsigned DIArray::getNumElements() const {
268 return DbgNode->getNumOperands();
271 /// replaceAllUsesWith - Replace all uses of debug info referenced by
273 void DIType::replaceAllUsesWith(DIDescriptor &D) {
277 // Since we use a TrackingVH for the node, its easy for clients to manufacture
278 // legitimate situations where they want to replaceAllUsesWith() on something
279 // which, due to uniquing, has merged with the source. We shield clients from
280 // this detail by allowing a value to be replaced with replaceAllUsesWith()
283 MDNode *Node = const_cast<MDNode*>(DbgNode);
284 const MDNode *DN = D;
285 const Value *V = cast_or_null<Value>(DN);
286 Node->replaceAllUsesWith(const_cast<Value*>(V));
287 MDNode::deleteTemporary(Node);
291 /// replaceAllUsesWith - Replace all uses of debug info referenced by
293 void DIType::replaceAllUsesWith(MDNode *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 /// Verify - Verify that a compile unit is well formed.
312 bool DICompileUnit::Verify() const {
315 StringRef N = getFilename();
318 // It is possible that directory and produce string is empty.
322 /// Verify - Verify that a type descriptor is well formed.
323 bool DIType::Verify() const {
326 if (!getContext().Verify())
328 unsigned Tag = getTag();
329 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
330 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
331 Tag != dwarf::DW_TAG_reference_type && Tag != dwarf::DW_TAG_restrict_type
332 && Tag != dwarf::DW_TAG_vector_type && Tag != dwarf::DW_TAG_array_type
333 && Tag != dwarf::DW_TAG_enumeration_type
334 && getFilename().empty())
339 /// Verify - Verify that a basic type descriptor is well formed.
340 bool DIBasicType::Verify() const {
341 return isBasicType();
344 /// Verify - Verify that a derived type descriptor is well formed.
345 bool DIDerivedType::Verify() const {
346 return isDerivedType();
349 /// Verify - Verify that a composite type descriptor is well formed.
350 bool DICompositeType::Verify() const {
353 if (!getContext().Verify())
356 DICompileUnit CU = getCompileUnit();
362 /// Verify - Verify that a subprogram descriptor is well formed.
363 bool DISubprogram::Verify() const {
367 if (!getContext().Verify())
370 DICompileUnit CU = getCompileUnit();
374 DICompositeType Ty = getType();
380 /// Verify - Verify that a global variable descriptor is well formed.
381 bool DIGlobalVariable::Verify() const {
385 if (getDisplayName().empty())
388 if (!getContext().Verify())
391 DICompileUnit CU = getCompileUnit();
395 DIType Ty = getType();
399 if (!getGlobal() && !getConstant())
405 /// Verify - Verify that a variable descriptor is well formed.
406 bool DIVariable::Verify() const {
410 if (!getContext().Verify())
413 if (!getCompileUnit().Verify())
416 DIType Ty = getType();
423 /// Verify - Verify that a location descriptor is well formed.
424 bool DILocation::Verify() const {
428 return DbgNode->getNumOperands() == 4;
431 /// Verify - Verify that a namespace descriptor is well formed.
432 bool DINameSpace::Verify() const {
435 if (getName().empty())
437 if (!getCompileUnit().Verify())
442 /// getOriginalTypeSize - If this type is derived from a base type then
443 /// return base type size.
444 uint64_t DIDerivedType::getOriginalTypeSize() const {
445 unsigned Tag = getTag();
446 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
447 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
448 Tag == dwarf::DW_TAG_restrict_type) {
449 DIType BaseType = getTypeDerivedFrom();
450 // If this type is not derived from any type then take conservative
452 if (!BaseType.isValid())
453 return getSizeInBits();
454 if (BaseType.isDerivedType())
455 return DIDerivedType(BaseType).getOriginalTypeSize();
457 return BaseType.getSizeInBits();
460 return getSizeInBits();
463 /// isInlinedFnArgument - Return true if this variable provides debugging
464 /// information for an inlined function arguments.
465 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
466 assert(CurFn && "Invalid function");
467 if (!getContext().isSubprogram())
469 // This variable is not inlined function argument if its scope
470 // does not describe current function.
471 return !(DISubprogram(getContext()).describes(CurFn));
474 /// describes - Return true if this subprogram provides debugging
475 /// information for the function F.
476 bool DISubprogram::describes(const Function *F) {
477 assert(F && "Invalid function");
478 if (F == getFunction())
480 StringRef Name = getLinkageName();
483 if (F->getName() == Name)
488 unsigned DISubprogram::isOptimized() const {
489 assert (DbgNode && "Invalid subprogram descriptor!");
490 if (DbgNode->getNumOperands() == 16)
491 return getUnsignedField(15);
495 StringRef DIScope::getFilename() const {
498 if (isLexicalBlock())
499 return DILexicalBlock(DbgNode).getFilename();
501 return DISubprogram(DbgNode).getFilename();
503 return DICompileUnit(DbgNode).getFilename();
505 return DINameSpace(DbgNode).getFilename();
507 return DIType(DbgNode).getFilename();
509 return DIFile(DbgNode).getFilename();
510 assert(0 && "Invalid DIScope!");
514 StringRef DIScope::getDirectory() const {
517 if (isLexicalBlock())
518 return DILexicalBlock(DbgNode).getDirectory();
520 return DISubprogram(DbgNode).getDirectory();
522 return DICompileUnit(DbgNode).getDirectory();
524 return DINameSpace(DbgNode).getDirectory();
526 return DIType(DbgNode).getDirectory();
528 return DIFile(DbgNode).getDirectory();
529 assert(0 && "Invalid DIScope!");
533 //===----------------------------------------------------------------------===//
534 // DIDescriptor: dump routines for all descriptors.
535 //===----------------------------------------------------------------------===//
538 /// print - Print descriptor.
539 void DIDescriptor::print(raw_ostream &OS) const {
540 OS << "[" << dwarf::TagString(getTag()) << "] ";
541 OS.write_hex((intptr_t) &*DbgNode) << ']';
544 /// print - Print compile unit.
545 void DICompileUnit::print(raw_ostream &OS) const {
547 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
549 OS << " [" << getDirectory() << "/" << getFilename() << "]";
552 /// print - Print type.
553 void DIType::print(raw_ostream &OS) const {
554 if (!DbgNode) return;
556 StringRef Res = getName();
558 OS << " [" << Res << "] ";
560 unsigned Tag = getTag();
561 OS << " [" << dwarf::TagString(Tag) << "] ";
563 // TODO : Print context
564 getCompileUnit().print(OS);
566 << "line " << getLineNumber() << ", "
567 << getSizeInBits() << " bits, "
568 << getAlignInBits() << " bit alignment, "
569 << getOffsetInBits() << " bit offset"
574 else if (isProtected())
575 OS << " [protected] ";
581 DIBasicType(DbgNode).print(OS);
582 else if (isDerivedType())
583 DIDerivedType(DbgNode).print(OS);
584 else if (isCompositeType())
585 DICompositeType(DbgNode).print(OS);
587 OS << "Invalid DIType\n";
594 /// print - Print basic type.
595 void DIBasicType::print(raw_ostream &OS) const {
596 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
599 /// print - Print derived type.
600 void DIDerivedType::print(raw_ostream &OS) const {
601 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
604 /// print - Print composite type.
605 void DICompositeType::print(raw_ostream &OS) const {
606 DIArray A = getTypeArray();
607 OS << " [" << A.getNumElements() << " elements]";
610 /// print - Print subprogram.
611 void DISubprogram::print(raw_ostream &OS) const {
612 StringRef Res = getName();
614 OS << " [" << Res << "] ";
616 unsigned Tag = getTag();
617 OS << " [" << dwarf::TagString(Tag) << "] ";
619 // TODO : Print context
620 getCompileUnit().print(OS);
621 OS << " [" << getLineNumber() << "] ";
632 /// print - Print global variable.
633 void DIGlobalVariable::print(raw_ostream &OS) const {
635 StringRef Res = getName();
637 OS << " [" << Res << "] ";
639 unsigned Tag = getTag();
640 OS << " [" << dwarf::TagString(Tag) << "] ";
642 // TODO : Print context
643 getCompileUnit().print(OS);
644 OS << " [" << getLineNumber() << "] ";
652 if (isGlobalVariable())
653 DIGlobalVariable(DbgNode).print(OS);
657 /// print - Print variable.
658 void DIVariable::print(raw_ostream &OS) const {
659 StringRef Res = getName();
661 OS << " [" << Res << "] ";
663 getCompileUnit().print(OS);
664 OS << " [" << getLineNumber() << "] ";
668 // FIXME: Dump complex addresses
671 /// dump - Print descriptor to dbgs() with a newline.
672 void DIDescriptor::dump() const {
673 print(dbgs()); dbgs() << '\n';
676 /// dump - Print compile unit to dbgs() with a newline.
677 void DICompileUnit::dump() const {
678 print(dbgs()); dbgs() << '\n';
681 /// dump - Print type to dbgs() with a newline.
682 void DIType::dump() const {
683 print(dbgs()); dbgs() << '\n';
686 /// dump - Print basic type to dbgs() with a newline.
687 void DIBasicType::dump() const {
688 print(dbgs()); dbgs() << '\n';
691 /// dump - Print derived type to dbgs() with a newline.
692 void DIDerivedType::dump() const {
693 print(dbgs()); dbgs() << '\n';
696 /// dump - Print composite type to dbgs() with a newline.
697 void DICompositeType::dump() const {
698 print(dbgs()); dbgs() << '\n';
701 /// dump - Print subprogram to dbgs() with a newline.
702 void DISubprogram::dump() const {
703 print(dbgs()); dbgs() << '\n';
706 /// dump - Print global variable.
707 void DIGlobalVariable::dump() const {
708 print(dbgs()); dbgs() << '\n';
711 /// dump - Print variable.
712 void DIVariable::dump() const {
713 print(dbgs()); dbgs() << '\n';
716 //===----------------------------------------------------------------------===//
717 // DIFactory: Basic Helpers
718 //===----------------------------------------------------------------------===//
720 DIFactory::DIFactory(Module &m)
721 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
723 Constant *DIFactory::GetTagConstant(unsigned TAG) {
724 assert((TAG & LLVMDebugVersionMask) == 0 &&
725 "Tag too large for debug encoding!");
726 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
729 //===----------------------------------------------------------------------===//
730 // DIFactory: Primary Constructors
731 //===----------------------------------------------------------------------===//
733 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
734 /// This implicitly uniques the arrays created.
735 DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
737 Value *Null = llvm::Constant::getNullValue(Type::getInt32Ty(VMContext));
738 return DIArray(MDNode::get(VMContext, &Null, 1));
741 SmallVector<Value *, 16> Elts(Tys, Tys+NumTys);
742 return DIArray(MDNode::get(VMContext, Elts.data(), Elts.size()));
745 /// GetOrCreateSubrange - Create a descriptor for a value range. This
746 /// implicitly uniques the values returned.
747 DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
749 GetTagConstant(dwarf::DW_TAG_subrange_type),
750 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
751 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
754 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
757 /// CreateUnspecifiedParameter - Create unspeicified type descriptor
758 /// for the subroutine type.
759 DIDescriptor DIFactory::CreateUnspecifiedParameter() {
761 GetTagConstant(dwarf::DW_TAG_unspecified_parameters)
763 return DIDescriptor(MDNode::get(VMContext, &Elts[0], 1));
766 /// CreateCompileUnit - Create a new descriptor for the specified compile
767 /// unit. Note that this does not unique compile units within the module.
768 DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
775 unsigned RunTimeVer) {
777 GetTagConstant(dwarf::DW_TAG_compile_unit),
778 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
779 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
780 MDString::get(VMContext, Filename),
781 MDString::get(VMContext, Directory),
782 MDString::get(VMContext, Producer),
783 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
784 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
785 MDString::get(VMContext, Flags),
786 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
789 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
792 /// CreateFile - Create a new descriptor for the specified file.
793 DIFile DIFactory::CreateFile(StringRef Filename,
797 GetTagConstant(dwarf::DW_TAG_file_type),
798 MDString::get(VMContext, Filename),
799 MDString::get(VMContext, Directory),
803 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
806 /// CreateEnumerator - Create a single enumerator value.
807 DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
809 GetTagConstant(dwarf::DW_TAG_enumerator),
810 MDString::get(VMContext, Name),
811 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
813 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
817 /// CreateBasicType - Create a basic type like int, float, etc.
818 DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
823 uint64_t AlignInBits,
824 uint64_t OffsetInBits, unsigned Flags,
827 GetTagConstant(dwarf::DW_TAG_base_type),
829 MDString::get(VMContext, Name),
831 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
832 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
833 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
834 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
835 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
836 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
838 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
842 /// CreateBasicType - Create a basic type like int, float, etc.
843 DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
847 Constant *SizeInBits,
848 Constant *AlignInBits,
849 Constant *OffsetInBits, unsigned Flags,
852 GetTagConstant(dwarf::DW_TAG_base_type),
854 MDString::get(VMContext, Name),
856 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
860 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
861 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
863 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
866 /// CreateArtificialType - Create a new DIType with "artificial" flag set.
867 DIType DIFactory::CreateArtificialType(DIType Ty) {
868 if (Ty.isArtificial())
871 SmallVector<Value *, 9> Elts;
873 assert (N && "Unexpected input DIType!");
874 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
875 if (Value *V = N->getOperand(i))
878 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
881 unsigned CurFlags = Ty.getFlags();
882 CurFlags = CurFlags | DIType::FlagArtificial;
884 // Flags are stored at this slot.
885 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
887 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
890 /// CreateDerivedType - Create a derived type like const qualified type,
891 /// pointer, typedef, etc.
892 DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
893 DIDescriptor Context,
898 uint64_t AlignInBits,
899 uint64_t OffsetInBits,
901 DIType DerivedFrom) {
905 MDString::get(VMContext, Name),
907 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
908 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
909 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
910 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
911 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
914 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
918 /// CreateDerivedType - Create a derived type like const qualified type,
919 /// pointer, typedef, etc.
920 DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
921 DIDescriptor Context,
925 Constant *SizeInBits,
926 Constant *AlignInBits,
927 Constant *OffsetInBits,
929 DIType DerivedFrom) {
933 MDString::get(VMContext, Name),
935 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
939 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
942 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
946 /// CreateCompositeType - Create a composite type like array, struct, etc.
947 DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
948 DIDescriptor Context,
953 uint64_t AlignInBits,
954 uint64_t OffsetInBits,
958 unsigned RuntimeLang,
959 MDNode *ContainingType) {
964 MDString::get(VMContext, Name),
966 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
967 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
968 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
969 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
970 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
973 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
977 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
978 // Create a named metadata so that we do not lose this enum info.
979 if (Tag == dwarf::DW_TAG_enumeration_type) {
980 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
981 NMD->addOperand(Node);
983 return DICompositeType(Node);
986 /// CreateTemporaryType - Create a temporary forward-declared type.
987 DIType DIFactory::CreateTemporaryType() {
988 // Give the temporary MDNode a tag. It doesn't matter what tag we
989 // use here as long as DIType accepts it.
991 GetTagConstant(DW_TAG_base_type)
993 MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
997 /// CreateTemporaryType - Create a temporary forward-declared type.
998 DIType DIFactory::CreateTemporaryType(DIFile F) {
999 // Give the temporary MDNode a tag. It doesn't matter what tag we
1000 // use here as long as DIType accepts it.
1002 GetTagConstant(DW_TAG_base_type),
1007 MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
1008 return DIType(Node);
1011 /// CreateCompositeType - Create a composite type like array, struct, etc.
1012 DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
1013 DIDescriptor Context,
1016 unsigned LineNumber,
1017 Constant *SizeInBits,
1018 Constant *AlignInBits,
1019 Constant *OffsetInBits,
1023 unsigned RuntimeLang,
1024 MDNode *ContainingType) {
1026 GetTagConstant(Tag),
1028 MDString::get(VMContext, Name),
1030 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
1034 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1037 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
1040 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
1041 // Create a named metadata so that we do not lose this enum info.
1042 if (Tag == dwarf::DW_TAG_enumeration_type) {
1043 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
1044 NMD->addOperand(Node);
1046 return DICompositeType(Node);
1050 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
1051 /// See comments in DISubprogram for descriptions of these fields. This
1052 /// method does not unique the generated descriptors.
1053 DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
1055 StringRef DisplayName,
1056 StringRef LinkageName,
1058 unsigned LineNo, DIType Ty,
1061 unsigned VK, unsigned VIndex,
1062 DIType ContainingType,
1068 GetTagConstant(dwarf::DW_TAG_subprogram),
1069 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1071 MDString::get(VMContext, Name),
1072 MDString::get(VMContext, DisplayName),
1073 MDString::get(VMContext, LinkageName),
1075 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1077 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1078 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1079 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
1080 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
1082 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1083 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1086 MDNode *Node = MDNode::get(VMContext, &Elts[0], 17);
1088 // Create a named metadata so that we do not lose this mdnode.
1089 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1090 NMD->addOperand(Node);
1091 return DISubprogram(Node);
1094 /// CreateSubprogramDefinition - Create new subprogram descriptor for the
1095 /// given declaration.
1096 DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration){
1097 if (SPDeclaration.isDefinition())
1098 return DISubprogram(SPDeclaration);
1100 MDNode *DeclNode = SPDeclaration;
1102 GetTagConstant(dwarf::DW_TAG_subprogram),
1103 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1104 DeclNode->getOperand(2), // Context
1105 DeclNode->getOperand(3), // Name
1106 DeclNode->getOperand(4), // DisplayName
1107 DeclNode->getOperand(5), // LinkageName
1108 DeclNode->getOperand(6), // CompileUnit
1109 DeclNode->getOperand(7), // LineNo
1110 DeclNode->getOperand(8), // Type
1111 DeclNode->getOperand(9), // isLocalToUnit
1112 ConstantInt::get(Type::getInt1Ty(VMContext), true),
1113 DeclNode->getOperand(11), // Virtuality
1114 DeclNode->getOperand(12), // VIndex
1115 DeclNode->getOperand(13), // Containting Type
1116 DeclNode->getOperand(14), // Flags
1117 DeclNode->getOperand(15), // isOptimized
1118 SPDeclaration.getFunction()
1120 MDNode *Node =MDNode::get(VMContext, &Elts[0], 16);
1122 // Create a named metadata so that we do not lose this mdnode.
1123 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1124 NMD->addOperand(Node);
1125 return DISubprogram(Node);
1128 /// CreateGlobalVariable - Create a new descriptor for the specified global.
1130 DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1131 StringRef DisplayName,
1132 StringRef LinkageName,
1134 unsigned LineNo, DIType Ty,bool isLocalToUnit,
1135 bool isDefinition, llvm::GlobalVariable *Val) {
1137 GetTagConstant(dwarf::DW_TAG_variable),
1138 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1140 MDString::get(VMContext, Name),
1141 MDString::get(VMContext, DisplayName),
1142 MDString::get(VMContext, LinkageName),
1144 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1146 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1147 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1151 Value *const *Vs = &Elts[0];
1152 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1154 // Create a named metadata so that we do not lose this mdnode.
1155 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1156 NMD->addOperand(Node);
1158 return DIGlobalVariable(Node);
1161 /// CreateGlobalVariable - Create a new descriptor for the specified constant.
1163 DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1164 StringRef DisplayName,
1165 StringRef LinkageName,
1167 unsigned LineNo, DIType Ty,bool isLocalToUnit,
1168 bool isDefinition, llvm::Constant *Val) {
1170 GetTagConstant(dwarf::DW_TAG_variable),
1171 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1173 MDString::get(VMContext, Name),
1174 MDString::get(VMContext, DisplayName),
1175 MDString::get(VMContext, LinkageName),
1177 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1179 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1180 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1184 Value *const *Vs = &Elts[0];
1185 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1187 // Create a named metadata so that we do not lose this mdnode.
1188 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1189 NMD->addOperand(Node);
1191 return DIGlobalVariable(Node);
1194 /// fixupObjcLikeName - Replace contains special characters used
1195 /// in a typical Objective-C names with '.' in a given string.
1196 static void fixupObjcLikeName(std::string &Str) {
1197 for (size_t i = 0, e = Str.size(); i < e; ++i) {
1199 if (C == '[' || C == ']' || C == ' ' || C == ':' || C == '+' ||
1200 C == '(' || C == ')')
1205 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
1206 /// to hold function specific information.
1207 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, StringRef FuncName) {
1208 SmallString<32> Out;
1209 if (FuncName.find('[') == StringRef::npos)
1210 return M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", FuncName)
1212 std::string Name = FuncName;
1213 fixupObjcLikeName(Name);
1214 return M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", Name)
1218 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
1219 /// suitable to hold function specific information.
1220 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, StringRef FuncName) {
1221 if (FuncName.find('[') == StringRef::npos)
1222 return M.getNamedMetadata(Twine("llvm.dbg.lv.", FuncName));
1223 std::string Name = FuncName;
1224 fixupObjcLikeName(Name);
1225 return M.getNamedMetadata(Twine("llvm.dbg.lv.", Name));
1228 /// CreateVariable - Create a new descriptor for the specified variable.
1229 DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
1233 DIType Ty, bool AlwaysPreserve,
1236 GetTagConstant(Tag),
1238 MDString::get(VMContext, Name),
1240 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1242 ConstantInt::get(Type::getInt32Ty(VMContext), Flags)
1244 MDNode *Node = MDNode::get(VMContext, &Elts[0], 7);
1245 if (AlwaysPreserve) {
1246 // The optimizer may remove local variable. If there is an interest
1247 // to preserve variable info in such situation then stash it in a
1249 DISubprogram Fn(getDISubprogram(Context));
1250 StringRef FName = "fn";
1251 if (Fn.getFunction())
1252 FName = Fn.getFunction()->getName();
1254 if (FName.startswith(StringRef(&One, 1)))
1255 FName = FName.substr(1);
1258 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, FName);
1259 FnLocals->addOperand(Node);
1261 return DIVariable(Node);
1265 /// CreateComplexVariable - Create a new descriptor for the specified variable
1266 /// which has a complex address expression for its address.
1267 DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
1268 StringRef Name, DIFile F,
1270 DIType Ty, Value *const *Addr,
1272 SmallVector<Value *, 15> Elts;
1273 Elts.push_back(GetTagConstant(Tag));
1274 Elts.push_back(Context);
1275 Elts.push_back(MDString::get(VMContext, Name));
1277 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
1279 Elts.append(Addr, Addr+NumAddr);
1281 return DIVariable(MDNode::get(VMContext, Elts.data(), Elts.size()));
1285 /// CreateBlock - This creates a descriptor for a lexical block with the
1286 /// specified parent VMContext.
1287 DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
1288 DIFile F, unsigned LineNo,
1290 // Defeat MDNode uniqing for lexical blocks.
1291 static unsigned int unique_id = 0;
1293 GetTagConstant(dwarf::DW_TAG_lexical_block),
1295 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1296 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1298 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1300 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 6));
1303 /// CreateNameSpace - This creates new descriptor for a namespace
1304 /// with the specified parent context.
1305 DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
1309 GetTagConstant(dwarf::DW_TAG_namespace),
1311 MDString::get(VMContext, Name),
1313 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1315 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1318 /// CreateLocation - Creates a debug info location.
1319 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1320 DIScope S, DILocation OrigLoc) {
1322 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1323 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1327 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1330 //===----------------------------------------------------------------------===//
1331 // DIFactory: Routines for inserting code into a function
1332 //===----------------------------------------------------------------------===//
1334 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1335 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1336 Instruction *InsertBefore) {
1337 assert(Storage && "no storage passed to dbg.declare");
1338 assert(D.Verify() && "empty DIVariable passed to dbg.declare");
1340 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1342 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1344 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
1347 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1348 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1349 BasicBlock *InsertAtEnd) {
1350 assert(Storage && "no storage passed to dbg.declare");
1351 assert(D.Verify() && "invalid DIVariable passed to dbg.declare");
1353 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1355 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1358 // If this block already has a terminator then insert this intrinsic
1359 // before the terminator.
1360 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1361 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1363 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
1365 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1366 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1368 Instruction *InsertBefore) {
1369 assert(V && "no value passed to dbg.value");
1370 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
1372 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1374 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1375 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1377 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1380 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1381 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1383 BasicBlock *InsertAtEnd) {
1384 assert(V && "no value passed to dbg.value");
1385 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
1387 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1389 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1390 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1392 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1395 // RecordType - Record DIType in a module such that it is not lost even if
1396 // it is not referenced through debug info anchors.
1397 void DIFactory::RecordType(DIType T) {
1398 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.ty");
1403 //===----------------------------------------------------------------------===//
1404 // DebugInfoFinder implementations.
1405 //===----------------------------------------------------------------------===//
1407 /// processModule - Process entire module and collect debug info.
1408 void DebugInfoFinder::processModule(Module &M) {
1409 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1410 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1411 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1413 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1414 processDeclare(DDI);
1416 DebugLoc Loc = BI->getDebugLoc();
1417 if (Loc.isUnknown())
1420 LLVMContext &Ctx = BI->getContext();
1421 DIDescriptor Scope(Loc.getScope(Ctx));
1423 if (Scope.isCompileUnit())
1424 addCompileUnit(DICompileUnit(Scope));
1425 else if (Scope.isSubprogram())
1426 processSubprogram(DISubprogram(Scope));
1427 else if (Scope.isLexicalBlock())
1428 processLexicalBlock(DILexicalBlock(Scope));
1430 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1431 processLocation(DILocation(IA));
1434 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
1435 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1436 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1437 if (addGlobalVariable(DIG)) {
1438 addCompileUnit(DIG.getCompileUnit());
1439 processType(DIG.getType());
1444 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
1445 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
1446 processSubprogram(DISubprogram(NMD->getOperand(i)));
1449 /// processLocation - Process DILocation.
1450 void DebugInfoFinder::processLocation(DILocation Loc) {
1451 if (!Loc.Verify()) return;
1452 DIDescriptor S(Loc.getScope());
1453 if (S.isCompileUnit())
1454 addCompileUnit(DICompileUnit(S));
1455 else if (S.isSubprogram())
1456 processSubprogram(DISubprogram(S));
1457 else if (S.isLexicalBlock())
1458 processLexicalBlock(DILexicalBlock(S));
1459 processLocation(Loc.getOrigLocation());
1462 /// processType - Process DIType.
1463 void DebugInfoFinder::processType(DIType DT) {
1467 addCompileUnit(DT.getCompileUnit());
1468 if (DT.isCompositeType()) {
1469 DICompositeType DCT(DT);
1470 processType(DCT.getTypeDerivedFrom());
1471 DIArray DA = DCT.getTypeArray();
1472 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1473 DIDescriptor D = DA.getElement(i);
1475 processType(DIType(D));
1476 else if (D.isSubprogram())
1477 processSubprogram(DISubprogram(D));
1479 } else if (DT.isDerivedType()) {
1480 DIDerivedType DDT(DT);
1481 processType(DDT.getTypeDerivedFrom());
1485 /// processLexicalBlock
1486 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1487 DIScope Context = LB.getContext();
1488 if (Context.isLexicalBlock())
1489 return processLexicalBlock(DILexicalBlock(Context));
1491 return processSubprogram(DISubprogram(Context));
1494 /// processSubprogram - Process DISubprogram.
1495 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1496 if (!addSubprogram(SP))
1498 addCompileUnit(SP.getCompileUnit());
1499 processType(SP.getType());
1502 /// processDeclare - Process DbgDeclareInst.
1503 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
1504 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1508 if (!DV.isVariable())
1511 if (!NodesSeen.insert(DV))
1514 addCompileUnit(DIVariable(N).getCompileUnit());
1515 processType(DIVariable(N).getType());
1518 /// addType - Add type into Tys.
1519 bool DebugInfoFinder::addType(DIType DT) {
1523 if (!NodesSeen.insert(DT))
1530 /// addCompileUnit - Add compile unit into CUs.
1531 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1535 if (!NodesSeen.insert(CU))
1542 /// addGlobalVariable - Add global variable into GVs.
1543 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1544 if (!DIDescriptor(DIG).isGlobalVariable())
1547 if (!NodesSeen.insert(DIG))
1554 // addSubprogram - Add subprgoram into SPs.
1555 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1556 if (!DIDescriptor(SP).isSubprogram())
1559 if (!NodesSeen.insert(SP))
1566 /// Find the debug info descriptor corresponding to this global variable.
1567 static Value *findDbgGlobalDeclare(GlobalVariable *V) {
1568 const Module *M = V->getParent();
1569 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1573 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1574 DIDescriptor DIG(cast<MDNode>(NMD->getOperand(i)));
1575 if (!DIG.isGlobalVariable())
1577 if (DIGlobalVariable(DIG).getGlobal() == V)
1583 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1584 /// It looks through pointer casts too.
1585 static const DbgDeclareInst *findDbgDeclare(const Value *V) {
1586 V = V->stripPointerCasts();
1588 if (!isa<Instruction>(V) && !isa<Argument>(V))
1591 const Function *F = NULL;
1592 if (const Instruction *I = dyn_cast<Instruction>(V))
1593 F = I->getParent()->getParent();
1594 else if (const Argument *A = dyn_cast<Argument>(V))
1597 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1598 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1600 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1601 if (DDI->getAddress() == V)
1607 bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1608 std::string &Type, unsigned &LineNo,
1609 std::string &File, std::string &Dir) {
1613 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1614 Value *DIGV = findDbgGlobalDeclare(GV);
1615 if (!DIGV) return false;
1616 DIGlobalVariable Var(cast<MDNode>(DIGV));
1618 StringRef D = Var.getDisplayName();
1621 LineNo = Var.getLineNumber();
1622 Unit = Var.getCompileUnit();
1623 TypeD = Var.getType();
1625 const DbgDeclareInst *DDI = findDbgDeclare(V);
1626 if (!DDI) return false;
1627 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1629 StringRef D = Var.getName();
1632 LineNo = Var.getLineNumber();
1633 Unit = Var.getCompileUnit();
1634 TypeD = Var.getType();
1637 StringRef T = TypeD.getName();
1640 StringRef F = Unit.getFilename();
1643 StringRef D = Unit.getDirectory();
1649 /// getDISubprogram - Find subprogram that is enclosing this scope.
1650 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
1651 DIDescriptor D(Scope);
1652 if (D.isSubprogram())
1653 return DISubprogram(Scope);
1655 if (D.isLexicalBlock())
1656 return getDISubprogram(DILexicalBlock(Scope).getContext());
1658 return DISubprogram();
1661 /// getDICompositeType - Find underlying composite type.
1662 DICompositeType llvm::getDICompositeType(DIType T) {
1663 if (T.isCompositeType())
1664 return DICompositeType(T);
1666 if (T.isDerivedType())
1667 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
1669 return DICompositeType();