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 /// isTemplateTypeParameter - Return true if the specified tag is
225 /// DW_TAG_template_type_parameter.
226 bool DIDescriptor::isTemplateTypeParameter() const {
227 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
230 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
231 bool DIDescriptor::isCompileUnit() const {
232 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
235 /// isFile - Return true if the specified tag is DW_TAG_file_type.
236 bool DIDescriptor::isFile() const {
237 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
240 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
241 bool DIDescriptor::isNameSpace() const {
242 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
245 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
246 bool DIDescriptor::isLexicalBlock() const {
247 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
250 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
251 bool DIDescriptor::isSubrange() const {
252 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
255 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
256 bool DIDescriptor::isEnumerator() const {
257 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
260 //===----------------------------------------------------------------------===//
261 // Simple Descriptor Constructors and other Methods
262 //===----------------------------------------------------------------------===//
264 DIType::DIType(const MDNode *N) : DIScope(N) {
266 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
271 unsigned DIArray::getNumElements() const {
274 return DbgNode->getNumOperands();
277 /// replaceAllUsesWith - Replace all uses of debug info referenced by
279 void DIType::replaceAllUsesWith(DIDescriptor &D) {
283 // Since we use a TrackingVH for the node, its easy for clients to manufacture
284 // legitimate situations where they want to replaceAllUsesWith() on something
285 // which, due to uniquing, has merged with the source. We shield clients from
286 // this detail by allowing a value to be replaced with replaceAllUsesWith()
289 MDNode *Node = const_cast<MDNode*>(DbgNode);
290 const MDNode *DN = D;
291 const Value *V = cast_or_null<Value>(DN);
292 Node->replaceAllUsesWith(const_cast<Value*>(V));
293 MDNode::deleteTemporary(Node);
297 /// replaceAllUsesWith - Replace all uses of debug info referenced by
299 void DIType::replaceAllUsesWith(MDNode *D) {
303 // Since we use a TrackingVH for the node, its easy for clients to manufacture
304 // legitimate situations where they want to replaceAllUsesWith() on something
305 // which, due to uniquing, has merged with the source. We shield clients from
306 // this detail by allowing a value to be replaced with replaceAllUsesWith()
309 MDNode *Node = const_cast<MDNode*>(DbgNode);
310 const MDNode *DN = D;
311 const Value *V = cast_or_null<Value>(DN);
312 Node->replaceAllUsesWith(const_cast<Value*>(V));
313 MDNode::deleteTemporary(Node);
317 /// Verify - Verify that a compile unit is well formed.
318 bool DICompileUnit::Verify() const {
321 StringRef N = getFilename();
324 // It is possible that directory and produce string is empty.
328 /// Verify - Verify that a type descriptor is well formed.
329 bool DIType::Verify() const {
332 if (!getContext().Verify())
334 unsigned Tag = getTag();
335 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
336 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
337 Tag != dwarf::DW_TAG_reference_type && Tag != dwarf::DW_TAG_restrict_type
338 && Tag != dwarf::DW_TAG_vector_type && Tag != dwarf::DW_TAG_array_type
339 && Tag != dwarf::DW_TAG_enumeration_type
340 && getFilename().empty())
345 /// Verify - Verify that a basic type descriptor is well formed.
346 bool DIBasicType::Verify() const {
347 return isBasicType();
350 /// Verify - Verify that a derived type descriptor is well formed.
351 bool DIDerivedType::Verify() const {
352 return isDerivedType();
355 /// Verify - Verify that a composite type descriptor is well formed.
356 bool DICompositeType::Verify() const {
359 if (!getContext().Verify())
362 DICompileUnit CU = getCompileUnit();
368 /// Verify - Verify that a subprogram descriptor is well formed.
369 bool DISubprogram::Verify() const {
373 if (!getContext().Verify())
376 DICompileUnit CU = getCompileUnit();
380 DICompositeType Ty = getType();
386 /// Verify - Verify that a global variable descriptor is well formed.
387 bool DIGlobalVariable::Verify() const {
391 if (getDisplayName().empty())
394 if (!getContext().Verify())
397 DICompileUnit CU = getCompileUnit();
401 DIType Ty = getType();
405 if (!getGlobal() && !getConstant())
411 /// Verify - Verify that a variable descriptor is well formed.
412 bool DIVariable::Verify() const {
416 if (!getContext().Verify())
419 if (!getCompileUnit().Verify())
422 DIType Ty = getType();
429 /// Verify - Verify that a location descriptor is well formed.
430 bool DILocation::Verify() const {
434 return DbgNode->getNumOperands() == 4;
437 /// Verify - Verify that a namespace descriptor is well formed.
438 bool DINameSpace::Verify() const {
441 if (getName().empty())
443 if (!getCompileUnit().Verify())
448 /// getOriginalTypeSize - If this type is derived from a base type then
449 /// return base type size.
450 uint64_t DIDerivedType::getOriginalTypeSize() const {
451 unsigned Tag = getTag();
452 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
453 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
454 Tag == dwarf::DW_TAG_restrict_type) {
455 DIType BaseType = getTypeDerivedFrom();
456 // If this type is not derived from any type then take conservative
458 if (!BaseType.isValid())
459 return getSizeInBits();
460 if (BaseType.isDerivedType())
461 return DIDerivedType(BaseType).getOriginalTypeSize();
463 return BaseType.getSizeInBits();
466 return getSizeInBits();
469 /// isInlinedFnArgument - Return true if this variable provides debugging
470 /// information for an inlined function arguments.
471 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
472 assert(CurFn && "Invalid function");
473 if (!getContext().isSubprogram())
475 // This variable is not inlined function argument if its scope
476 // does not describe current function.
477 return !(DISubprogram(getContext()).describes(CurFn));
480 /// describes - Return true if this subprogram provides debugging
481 /// information for the function F.
482 bool DISubprogram::describes(const Function *F) {
483 assert(F && "Invalid function");
484 if (F == getFunction())
486 StringRef Name = getLinkageName();
489 if (F->getName() == Name)
494 unsigned DISubprogram::isOptimized() const {
495 assert (DbgNode && "Invalid subprogram descriptor!");
496 if (DbgNode->getNumOperands() == 16)
497 return getUnsignedField(15);
501 StringRef DIScope::getFilename() const {
504 if (isLexicalBlock())
505 return DILexicalBlock(DbgNode).getFilename();
507 return DISubprogram(DbgNode).getFilename();
509 return DICompileUnit(DbgNode).getFilename();
511 return DINameSpace(DbgNode).getFilename();
513 return DIType(DbgNode).getFilename();
515 return DIFile(DbgNode).getFilename();
516 assert(0 && "Invalid DIScope!");
520 StringRef DIScope::getDirectory() const {
523 if (isLexicalBlock())
524 return DILexicalBlock(DbgNode).getDirectory();
526 return DISubprogram(DbgNode).getDirectory();
528 return DICompileUnit(DbgNode).getDirectory();
530 return DINameSpace(DbgNode).getDirectory();
532 return DIType(DbgNode).getDirectory();
534 return DIFile(DbgNode).getDirectory();
535 assert(0 && "Invalid DIScope!");
539 //===----------------------------------------------------------------------===//
540 // DIDescriptor: dump routines for all descriptors.
541 //===----------------------------------------------------------------------===//
544 /// print - Print descriptor.
545 void DIDescriptor::print(raw_ostream &OS) const {
546 OS << "[" << dwarf::TagString(getTag()) << "] ";
547 OS.write_hex((intptr_t) &*DbgNode) << ']';
550 /// print - Print compile unit.
551 void DICompileUnit::print(raw_ostream &OS) const {
553 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
555 OS << " [" << getDirectory() << "/" << getFilename() << "]";
558 /// print - Print type.
559 void DIType::print(raw_ostream &OS) const {
560 if (!DbgNode) return;
562 StringRef Res = getName();
564 OS << " [" << Res << "] ";
566 unsigned Tag = getTag();
567 OS << " [" << dwarf::TagString(Tag) << "] ";
569 // TODO : Print context
570 getCompileUnit().print(OS);
572 << "line " << getLineNumber() << ", "
573 << getSizeInBits() << " bits, "
574 << getAlignInBits() << " bit alignment, "
575 << getOffsetInBits() << " bit offset"
580 else if (isProtected())
581 OS << " [protected] ";
587 DIBasicType(DbgNode).print(OS);
588 else if (isDerivedType())
589 DIDerivedType(DbgNode).print(OS);
590 else if (isCompositeType())
591 DICompositeType(DbgNode).print(OS);
593 OS << "Invalid DIType\n";
600 /// print - Print basic type.
601 void DIBasicType::print(raw_ostream &OS) const {
602 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
605 /// print - Print derived type.
606 void DIDerivedType::print(raw_ostream &OS) const {
607 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
610 /// print - Print composite type.
611 void DICompositeType::print(raw_ostream &OS) const {
612 DIArray A = getTypeArray();
613 OS << " [" << A.getNumElements() << " elements]";
616 /// print - Print subprogram.
617 void DISubprogram::print(raw_ostream &OS) const {
618 StringRef Res = getName();
620 OS << " [" << Res << "] ";
622 unsigned Tag = getTag();
623 OS << " [" << dwarf::TagString(Tag) << "] ";
625 // TODO : Print context
626 getCompileUnit().print(OS);
627 OS << " [" << getLineNumber() << "] ";
638 /// print - Print global variable.
639 void DIGlobalVariable::print(raw_ostream &OS) const {
641 StringRef Res = getName();
643 OS << " [" << Res << "] ";
645 unsigned Tag = getTag();
646 OS << " [" << dwarf::TagString(Tag) << "] ";
648 // TODO : Print context
649 getCompileUnit().print(OS);
650 OS << " [" << getLineNumber() << "] ";
658 if (isGlobalVariable())
659 DIGlobalVariable(DbgNode).print(OS);
663 /// print - Print variable.
664 void DIVariable::print(raw_ostream &OS) const {
665 StringRef Res = getName();
667 OS << " [" << Res << "] ";
669 getCompileUnit().print(OS);
670 OS << " [" << getLineNumber() << "] ";
674 // FIXME: Dump complex addresses
677 /// dump - Print descriptor to dbgs() with a newline.
678 void DIDescriptor::dump() const {
679 print(dbgs()); dbgs() << '\n';
682 /// dump - Print compile unit to dbgs() with a newline.
683 void DICompileUnit::dump() const {
684 print(dbgs()); dbgs() << '\n';
687 /// dump - Print type to dbgs() with a newline.
688 void DIType::dump() const {
689 print(dbgs()); dbgs() << '\n';
692 /// dump - Print basic type to dbgs() with a newline.
693 void DIBasicType::dump() const {
694 print(dbgs()); dbgs() << '\n';
697 /// dump - Print derived type to dbgs() with a newline.
698 void DIDerivedType::dump() const {
699 print(dbgs()); dbgs() << '\n';
702 /// dump - Print composite type to dbgs() with a newline.
703 void DICompositeType::dump() const {
704 print(dbgs()); dbgs() << '\n';
707 /// dump - Print subprogram to dbgs() with a newline.
708 void DISubprogram::dump() const {
709 print(dbgs()); dbgs() << '\n';
712 /// dump - Print global variable.
713 void DIGlobalVariable::dump() const {
714 print(dbgs()); dbgs() << '\n';
717 /// dump - Print variable.
718 void DIVariable::dump() const {
719 print(dbgs()); dbgs() << '\n';
722 //===----------------------------------------------------------------------===//
723 // DIFactory: Basic Helpers
724 //===----------------------------------------------------------------------===//
726 DIFactory::DIFactory(Module &m)
727 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
729 Constant *DIFactory::GetTagConstant(unsigned TAG) {
730 assert((TAG & LLVMDebugVersionMask) == 0 &&
731 "Tag too large for debug encoding!");
732 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
735 //===----------------------------------------------------------------------===//
736 // DIFactory: Primary Constructors
737 //===----------------------------------------------------------------------===//
739 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
740 /// This implicitly uniques the arrays created.
741 DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
743 Value *Null = llvm::Constant::getNullValue(Type::getInt32Ty(VMContext));
744 return DIArray(MDNode::get(VMContext, &Null, 1));
747 SmallVector<Value *, 16> Elts(Tys, Tys+NumTys);
748 return DIArray(MDNode::get(VMContext, Elts.data(), Elts.size()));
751 /// GetOrCreateSubrange - Create a descriptor for a value range. This
752 /// implicitly uniques the values returned.
753 DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
755 GetTagConstant(dwarf::DW_TAG_subrange_type),
756 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
757 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
760 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
763 /// CreateUnspecifiedParameter - Create unspeicified type descriptor
764 /// for the subroutine type.
765 DIDescriptor DIFactory::CreateUnspecifiedParameter() {
767 GetTagConstant(dwarf::DW_TAG_unspecified_parameters)
769 return DIDescriptor(MDNode::get(VMContext, &Elts[0], 1));
772 /// CreateCompileUnit - Create a new descriptor for the specified compile
773 /// unit. Note that this does not unique compile units within the module.
774 DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
781 unsigned RunTimeVer) {
783 GetTagConstant(dwarf::DW_TAG_compile_unit),
784 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
785 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
786 MDString::get(VMContext, Filename),
787 MDString::get(VMContext, Directory),
788 MDString::get(VMContext, Producer),
789 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
790 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
791 MDString::get(VMContext, Flags),
792 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
795 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
798 /// CreateFile - Create a new descriptor for the specified file.
799 DIFile DIFactory::CreateFile(StringRef Filename,
803 GetTagConstant(dwarf::DW_TAG_file_type),
804 MDString::get(VMContext, Filename),
805 MDString::get(VMContext, Directory),
809 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
812 /// CreateEnumerator - Create a single enumerator value.
813 DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
815 GetTagConstant(dwarf::DW_TAG_enumerator),
816 MDString::get(VMContext, Name),
817 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
819 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
823 /// CreateBasicType - Create a basic type like int, float, etc.
824 DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
829 uint64_t AlignInBits,
830 uint64_t OffsetInBits, unsigned Flags,
833 GetTagConstant(dwarf::DW_TAG_base_type),
835 MDString::get(VMContext, Name),
837 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
838 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
839 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
840 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
841 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
842 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
844 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
848 /// CreateBasicType - Create a basic type like int, float, etc.
849 DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
853 Constant *SizeInBits,
854 Constant *AlignInBits,
855 Constant *OffsetInBits, unsigned Flags,
858 GetTagConstant(dwarf::DW_TAG_base_type),
860 MDString::get(VMContext, Name),
862 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
866 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
867 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
869 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
872 /// CreateArtificialType - Create a new DIType with "artificial" flag set.
873 DIType DIFactory::CreateArtificialType(DIType Ty) {
874 if (Ty.isArtificial())
877 SmallVector<Value *, 9> Elts;
879 assert (N && "Unexpected input DIType!");
880 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
881 if (Value *V = N->getOperand(i))
884 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
887 unsigned CurFlags = Ty.getFlags();
888 CurFlags = CurFlags | DIType::FlagArtificial;
890 // Flags are stored at this slot.
891 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
893 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
896 /// CreateDerivedType - Create a derived type like const qualified type,
897 /// pointer, typedef, etc.
898 DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
899 DIDescriptor Context,
904 uint64_t AlignInBits,
905 uint64_t OffsetInBits,
907 DIType DerivedFrom) {
911 MDString::get(VMContext, Name),
913 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
914 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
915 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
916 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
917 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
920 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
924 /// CreateDerivedType - Create a derived type like const qualified type,
925 /// pointer, typedef, etc.
926 DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
927 DIDescriptor Context,
931 Constant *SizeInBits,
932 Constant *AlignInBits,
933 Constant *OffsetInBits,
935 DIType DerivedFrom) {
939 MDString::get(VMContext, Name),
941 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
945 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
948 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
952 /// CreateCompositeType - Create a composite type like array, struct, etc.
953 DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
954 DIDescriptor Context,
959 uint64_t AlignInBits,
960 uint64_t OffsetInBits,
964 unsigned RuntimeLang,
965 MDNode *ContainingType) {
970 MDString::get(VMContext, Name),
972 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
973 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
974 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
975 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
976 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
979 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
983 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
984 // Create a named metadata so that we do not lose this enum info.
985 if (Tag == dwarf::DW_TAG_enumeration_type) {
986 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
987 NMD->addOperand(Node);
989 return DICompositeType(Node);
992 /// CreateTemporaryType - Create a temporary forward-declared type.
993 DIType DIFactory::CreateTemporaryType() {
994 // Give the temporary MDNode a tag. It doesn't matter what tag we
995 // use here as long as DIType accepts it.
997 GetTagConstant(DW_TAG_base_type)
999 MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
1000 return DIType(Node);
1003 /// CreateTemporaryType - Create a temporary forward-declared type.
1004 DIType DIFactory::CreateTemporaryType(DIFile F) {
1005 // Give the temporary MDNode a tag. It doesn't matter what tag we
1006 // use here as long as DIType accepts it.
1008 GetTagConstant(DW_TAG_base_type),
1013 MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
1014 return DIType(Node);
1017 /// CreateCompositeType - Create a composite type like array, struct, etc.
1018 DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
1019 DIDescriptor Context,
1022 unsigned LineNumber,
1023 Constant *SizeInBits,
1024 Constant *AlignInBits,
1025 Constant *OffsetInBits,
1029 unsigned RuntimeLang,
1030 MDNode *ContainingType) {
1032 GetTagConstant(Tag),
1034 MDString::get(VMContext, Name),
1036 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
1040 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1043 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
1046 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
1047 // Create a named metadata so that we do not lose this enum info.
1048 if (Tag == dwarf::DW_TAG_enumeration_type) {
1049 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
1050 NMD->addOperand(Node);
1052 return DICompositeType(Node);
1056 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
1057 /// See comments in DISubprogram for descriptions of these fields. This
1058 /// method does not unique the generated descriptors.
1059 DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
1061 StringRef DisplayName,
1062 StringRef LinkageName,
1064 unsigned LineNo, DIType Ty,
1067 unsigned VK, unsigned VIndex,
1068 DIType ContainingType,
1074 GetTagConstant(dwarf::DW_TAG_subprogram),
1075 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1077 MDString::get(VMContext, Name),
1078 MDString::get(VMContext, DisplayName),
1079 MDString::get(VMContext, LinkageName),
1081 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1083 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1084 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1085 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
1086 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
1088 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1089 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1092 MDNode *Node = MDNode::get(VMContext, &Elts[0], 17);
1094 // Create a named metadata so that we do not lose this mdnode.
1095 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1096 NMD->addOperand(Node);
1097 return DISubprogram(Node);
1100 /// CreateSubprogramDefinition - Create new subprogram descriptor for the
1101 /// given declaration.
1102 DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration){
1103 if (SPDeclaration.isDefinition())
1104 return DISubprogram(SPDeclaration);
1106 MDNode *DeclNode = SPDeclaration;
1108 GetTagConstant(dwarf::DW_TAG_subprogram),
1109 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1110 DeclNode->getOperand(2), // Context
1111 DeclNode->getOperand(3), // Name
1112 DeclNode->getOperand(4), // DisplayName
1113 DeclNode->getOperand(5), // LinkageName
1114 DeclNode->getOperand(6), // CompileUnit
1115 DeclNode->getOperand(7), // LineNo
1116 DeclNode->getOperand(8), // Type
1117 DeclNode->getOperand(9), // isLocalToUnit
1118 ConstantInt::get(Type::getInt1Ty(VMContext), true),
1119 DeclNode->getOperand(11), // Virtuality
1120 DeclNode->getOperand(12), // VIndex
1121 DeclNode->getOperand(13), // Containting Type
1122 DeclNode->getOperand(14), // Flags
1123 DeclNode->getOperand(15), // isOptimized
1124 SPDeclaration.getFunction()
1126 MDNode *Node =MDNode::get(VMContext, &Elts[0], 16);
1128 // Create a named metadata so that we do not lose this mdnode.
1129 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1130 NMD->addOperand(Node);
1131 return DISubprogram(Node);
1134 /// CreateGlobalVariable - Create a new descriptor for the specified global.
1136 DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1137 StringRef DisplayName,
1138 StringRef LinkageName,
1140 unsigned LineNo, DIType Ty,bool isLocalToUnit,
1141 bool isDefinition, llvm::GlobalVariable *Val) {
1143 GetTagConstant(dwarf::DW_TAG_variable),
1144 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1146 MDString::get(VMContext, Name),
1147 MDString::get(VMContext, DisplayName),
1148 MDString::get(VMContext, LinkageName),
1150 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1152 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1153 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1157 Value *const *Vs = &Elts[0];
1158 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1160 // Create a named metadata so that we do not lose this mdnode.
1161 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1162 NMD->addOperand(Node);
1164 return DIGlobalVariable(Node);
1167 /// CreateGlobalVariable - Create a new descriptor for the specified constant.
1169 DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1170 StringRef DisplayName,
1171 StringRef LinkageName,
1173 unsigned LineNo, DIType Ty,bool isLocalToUnit,
1174 bool isDefinition, llvm::Constant *Val) {
1176 GetTagConstant(dwarf::DW_TAG_variable),
1177 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1179 MDString::get(VMContext, Name),
1180 MDString::get(VMContext, DisplayName),
1181 MDString::get(VMContext, LinkageName),
1183 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1185 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1186 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1190 Value *const *Vs = &Elts[0];
1191 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1193 // Create a named metadata so that we do not lose this mdnode.
1194 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1195 NMD->addOperand(Node);
1197 return DIGlobalVariable(Node);
1200 /// fixupObjcLikeName - Replace contains special characters used
1201 /// in a typical Objective-C names with '.' in a given string.
1202 static void fixupObjcLikeName(std::string &Str) {
1203 for (size_t i = 0, e = Str.size(); i < e; ++i) {
1205 if (C == '[' || C == ']' || C == ' ' || C == ':' || C == '+' ||
1206 C == '(' || C == ')')
1211 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
1212 /// to hold function specific information.
1213 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, StringRef FuncName) {
1214 SmallString<32> Out;
1215 if (FuncName.find('[') == StringRef::npos)
1216 return M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", FuncName)
1218 std::string Name = FuncName;
1219 fixupObjcLikeName(Name);
1220 return M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", Name)
1224 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
1225 /// suitable to hold function specific information.
1226 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, StringRef FuncName) {
1227 if (FuncName.find('[') == StringRef::npos)
1228 return M.getNamedMetadata(Twine("llvm.dbg.lv.", FuncName));
1229 std::string Name = FuncName;
1230 fixupObjcLikeName(Name);
1231 return M.getNamedMetadata(Twine("llvm.dbg.lv.", Name));
1234 /// CreateVariable - Create a new descriptor for the specified variable.
1235 DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
1239 DIType Ty, bool AlwaysPreserve,
1242 GetTagConstant(Tag),
1244 MDString::get(VMContext, Name),
1246 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1248 ConstantInt::get(Type::getInt32Ty(VMContext), Flags)
1250 MDNode *Node = MDNode::get(VMContext, &Elts[0], 7);
1251 if (AlwaysPreserve) {
1252 // The optimizer may remove local variable. If there is an interest
1253 // to preserve variable info in such situation then stash it in a
1255 DISubprogram Fn(getDISubprogram(Context));
1256 StringRef FName = "fn";
1257 if (Fn.getFunction())
1258 FName = Fn.getFunction()->getName();
1260 if (FName.startswith(StringRef(&One, 1)))
1261 FName = FName.substr(1);
1264 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, FName);
1265 FnLocals->addOperand(Node);
1267 return DIVariable(Node);
1271 /// CreateComplexVariable - Create a new descriptor for the specified variable
1272 /// which has a complex address expression for its address.
1273 DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
1274 StringRef Name, DIFile F,
1276 DIType Ty, Value *const *Addr,
1278 SmallVector<Value *, 15> Elts;
1279 Elts.push_back(GetTagConstant(Tag));
1280 Elts.push_back(Context);
1281 Elts.push_back(MDString::get(VMContext, Name));
1283 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
1285 Elts.append(Addr, Addr+NumAddr);
1287 return DIVariable(MDNode::get(VMContext, Elts.data(), Elts.size()));
1291 /// CreateBlock - This creates a descriptor for a lexical block with the
1292 /// specified parent VMContext.
1293 DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
1294 DIFile F, unsigned LineNo,
1296 // Defeat MDNode uniqing for lexical blocks.
1297 static unsigned int unique_id = 0;
1299 GetTagConstant(dwarf::DW_TAG_lexical_block),
1301 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1302 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1304 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1306 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 6));
1309 /// CreateNameSpace - This creates new descriptor for a namespace
1310 /// with the specified parent context.
1311 DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
1315 GetTagConstant(dwarf::DW_TAG_namespace),
1317 MDString::get(VMContext, Name),
1319 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1321 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1324 /// CreateLocation - Creates a debug info location.
1325 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1326 DIScope S, DILocation OrigLoc) {
1328 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1329 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1333 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1336 //===----------------------------------------------------------------------===//
1337 // DIFactory: Routines for inserting code into a function
1338 //===----------------------------------------------------------------------===//
1340 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1341 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1342 Instruction *InsertBefore) {
1343 assert(Storage && "no storage passed to dbg.declare");
1344 assert(D.Verify() && "empty DIVariable passed to dbg.declare");
1346 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1348 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1350 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
1353 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1354 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1355 BasicBlock *InsertAtEnd) {
1356 assert(Storage && "no storage passed to dbg.declare");
1357 assert(D.Verify() && "invalid DIVariable passed to dbg.declare");
1359 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1361 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1364 // If this block already has a terminator then insert this intrinsic
1365 // before the terminator.
1366 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1367 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1369 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
1371 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1372 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1374 Instruction *InsertBefore) {
1375 assert(V && "no value passed to dbg.value");
1376 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
1378 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1380 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1381 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1383 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1386 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1387 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1389 BasicBlock *InsertAtEnd) {
1390 assert(V && "no value passed to dbg.value");
1391 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
1393 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1395 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1396 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1398 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1401 // RecordType - Record DIType in a module such that it is not lost even if
1402 // it is not referenced through debug info anchors.
1403 void DIFactory::RecordType(DIType T) {
1404 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.ty");
1409 //===----------------------------------------------------------------------===//
1410 // DebugInfoFinder implementations.
1411 //===----------------------------------------------------------------------===//
1413 /// processModule - Process entire module and collect debug info.
1414 void DebugInfoFinder::processModule(Module &M) {
1415 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1416 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1417 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1419 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1420 processDeclare(DDI);
1422 DebugLoc Loc = BI->getDebugLoc();
1423 if (Loc.isUnknown())
1426 LLVMContext &Ctx = BI->getContext();
1427 DIDescriptor Scope(Loc.getScope(Ctx));
1429 if (Scope.isCompileUnit())
1430 addCompileUnit(DICompileUnit(Scope));
1431 else if (Scope.isSubprogram())
1432 processSubprogram(DISubprogram(Scope));
1433 else if (Scope.isLexicalBlock())
1434 processLexicalBlock(DILexicalBlock(Scope));
1436 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1437 processLocation(DILocation(IA));
1440 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
1441 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1442 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1443 if (addGlobalVariable(DIG)) {
1444 addCompileUnit(DIG.getCompileUnit());
1445 processType(DIG.getType());
1450 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
1451 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
1452 processSubprogram(DISubprogram(NMD->getOperand(i)));
1455 /// processLocation - Process DILocation.
1456 void DebugInfoFinder::processLocation(DILocation Loc) {
1457 if (!Loc.Verify()) return;
1458 DIDescriptor S(Loc.getScope());
1459 if (S.isCompileUnit())
1460 addCompileUnit(DICompileUnit(S));
1461 else if (S.isSubprogram())
1462 processSubprogram(DISubprogram(S));
1463 else if (S.isLexicalBlock())
1464 processLexicalBlock(DILexicalBlock(S));
1465 processLocation(Loc.getOrigLocation());
1468 /// processType - Process DIType.
1469 void DebugInfoFinder::processType(DIType DT) {
1473 addCompileUnit(DT.getCompileUnit());
1474 if (DT.isCompositeType()) {
1475 DICompositeType DCT(DT);
1476 processType(DCT.getTypeDerivedFrom());
1477 DIArray DA = DCT.getTypeArray();
1478 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1479 DIDescriptor D = DA.getElement(i);
1481 processType(DIType(D));
1482 else if (D.isSubprogram())
1483 processSubprogram(DISubprogram(D));
1485 } else if (DT.isDerivedType()) {
1486 DIDerivedType DDT(DT);
1487 processType(DDT.getTypeDerivedFrom());
1491 /// processLexicalBlock
1492 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1493 DIScope Context = LB.getContext();
1494 if (Context.isLexicalBlock())
1495 return processLexicalBlock(DILexicalBlock(Context));
1497 return processSubprogram(DISubprogram(Context));
1500 /// processSubprogram - Process DISubprogram.
1501 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1502 if (!addSubprogram(SP))
1504 addCompileUnit(SP.getCompileUnit());
1505 processType(SP.getType());
1508 /// processDeclare - Process DbgDeclareInst.
1509 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
1510 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1514 if (!DV.isVariable())
1517 if (!NodesSeen.insert(DV))
1520 addCompileUnit(DIVariable(N).getCompileUnit());
1521 processType(DIVariable(N).getType());
1524 /// addType - Add type into Tys.
1525 bool DebugInfoFinder::addType(DIType DT) {
1529 if (!NodesSeen.insert(DT))
1536 /// addCompileUnit - Add compile unit into CUs.
1537 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1541 if (!NodesSeen.insert(CU))
1548 /// addGlobalVariable - Add global variable into GVs.
1549 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1550 if (!DIDescriptor(DIG).isGlobalVariable())
1553 if (!NodesSeen.insert(DIG))
1560 // addSubprogram - Add subprgoram into SPs.
1561 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1562 if (!DIDescriptor(SP).isSubprogram())
1565 if (!NodesSeen.insert(SP))
1572 /// Find the debug info descriptor corresponding to this global variable.
1573 static Value *findDbgGlobalDeclare(GlobalVariable *V) {
1574 const Module *M = V->getParent();
1575 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1579 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1580 DIDescriptor DIG(cast<MDNode>(NMD->getOperand(i)));
1581 if (!DIG.isGlobalVariable())
1583 if (DIGlobalVariable(DIG).getGlobal() == V)
1589 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1590 /// It looks through pointer casts too.
1591 static const DbgDeclareInst *findDbgDeclare(const Value *V) {
1592 V = V->stripPointerCasts();
1594 if (!isa<Instruction>(V) && !isa<Argument>(V))
1597 const Function *F = NULL;
1598 if (const Instruction *I = dyn_cast<Instruction>(V))
1599 F = I->getParent()->getParent();
1600 else if (const Argument *A = dyn_cast<Argument>(V))
1603 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1604 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1606 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1607 if (DDI->getAddress() == V)
1613 bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1614 std::string &Type, unsigned &LineNo,
1615 std::string &File, std::string &Dir) {
1619 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1620 Value *DIGV = findDbgGlobalDeclare(GV);
1621 if (!DIGV) return false;
1622 DIGlobalVariable Var(cast<MDNode>(DIGV));
1624 StringRef D = Var.getDisplayName();
1627 LineNo = Var.getLineNumber();
1628 Unit = Var.getCompileUnit();
1629 TypeD = Var.getType();
1631 const DbgDeclareInst *DDI = findDbgDeclare(V);
1632 if (!DDI) return false;
1633 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1635 StringRef D = Var.getName();
1638 LineNo = Var.getLineNumber();
1639 Unit = Var.getCompileUnit();
1640 TypeD = Var.getType();
1643 StringRef T = TypeD.getName();
1646 StringRef F = Unit.getFilename();
1649 StringRef D = Unit.getDirectory();
1655 /// getDISubprogram - Find subprogram that is enclosing this scope.
1656 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
1657 DIDescriptor D(Scope);
1658 if (D.isSubprogram())
1659 return DISubprogram(Scope);
1661 if (D.isLexicalBlock())
1662 return getDISubprogram(DILexicalBlock(Scope).getContext());
1664 return DISubprogram();
1667 /// getDICompositeType - Find underlying composite type.
1668 DICompositeType llvm::getDICompositeType(DIType T) {
1669 if (T.isCompositeType())
1670 return DICompositeType(T);
1672 if (T.isDerivedType())
1673 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
1675 return DICompositeType();