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 /// Verify - Verify that a compile unit is well formed.
292 bool DICompileUnit::Verify() const {
295 StringRef N = getFilename();
298 // It is possible that directory and produce string is empty.
302 /// Verify - Verify that a type descriptor is well formed.
303 bool DIType::Verify() const {
306 if (!getContext().Verify())
308 unsigned Tag = getTag();
309 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
310 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
311 Tag != dwarf::DW_TAG_restrict_type && getFilename().empty())
316 /// Verify - Verify that a basic type descriptor is well formed.
317 bool DIBasicType::Verify() const {
318 return isBasicType();
321 /// Verify - Verify that a derived type descriptor is well formed.
322 bool DIDerivedType::Verify() const {
323 return isDerivedType();
326 /// Verify - Verify that a composite type descriptor is well formed.
327 bool DICompositeType::Verify() const {
330 if (!getContext().Verify())
333 DICompileUnit CU = getCompileUnit();
339 /// Verify - Verify that a subprogram descriptor is well formed.
340 bool DISubprogram::Verify() const {
344 if (!getContext().Verify())
347 DICompileUnit CU = getCompileUnit();
351 DICompositeType Ty = getType();
357 /// Verify - Verify that a global variable descriptor is well formed.
358 bool DIGlobalVariable::Verify() const {
362 if (getDisplayName().empty())
365 if (!getContext().Verify())
368 DICompileUnit CU = getCompileUnit();
372 DIType Ty = getType();
376 if (!getGlobal() && !getConstant())
382 /// Verify - Verify that a variable descriptor is well formed.
383 bool DIVariable::Verify() const {
387 if (!getContext().Verify())
390 if (!getCompileUnit().Verify())
393 DIType Ty = getType();
400 /// Verify - Verify that a location descriptor is well formed.
401 bool DILocation::Verify() const {
405 return DbgNode->getNumOperands() == 4;
408 /// Verify - Verify that a namespace descriptor is well formed.
409 bool DINameSpace::Verify() const {
412 if (getName().empty())
414 if (!getCompileUnit().Verify())
419 /// getOriginalTypeSize - If this type is derived from a base type then
420 /// return base type size.
421 uint64_t DIDerivedType::getOriginalTypeSize() const {
422 unsigned Tag = getTag();
423 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
424 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
425 Tag == dwarf::DW_TAG_restrict_type) {
426 DIType BaseType = getTypeDerivedFrom();
427 // If this type is not derived from any type then take conservative
429 if (!BaseType.isValid())
430 return getSizeInBits();
431 if (BaseType.isDerivedType())
432 return DIDerivedType(BaseType).getOriginalTypeSize();
434 return BaseType.getSizeInBits();
437 return getSizeInBits();
440 /// isInlinedFnArgument - Return true if this variable provides debugging
441 /// information for an inlined function arguments.
442 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
443 assert(CurFn && "Invalid function");
444 if (!getContext().isSubprogram())
446 // This variable is not inlined function argument if its scope
447 // does not describe current function.
448 return !(DISubprogram(getContext()).describes(CurFn));
451 /// describes - Return true if this subprogram provides debugging
452 /// information for the function F.
453 bool DISubprogram::describes(const Function *F) {
454 assert(F && "Invalid function");
455 if (F == getFunction())
457 StringRef Name = getLinkageName();
460 if (F->getName() == Name)
465 unsigned DISubprogram::isOptimized() const {
466 assert (DbgNode && "Invalid subprogram descriptor!");
467 if (DbgNode->getNumOperands() == 16)
468 return getUnsignedField(15);
472 StringRef DIScope::getFilename() const {
475 if (isLexicalBlock())
476 return DILexicalBlock(DbgNode).getFilename();
478 return DISubprogram(DbgNode).getFilename();
480 return DICompileUnit(DbgNode).getFilename();
482 return DINameSpace(DbgNode).getFilename();
484 return DIType(DbgNode).getFilename();
486 return DIFile(DbgNode).getFilename();
487 assert(0 && "Invalid DIScope!");
491 StringRef DIScope::getDirectory() const {
494 if (isLexicalBlock())
495 return DILexicalBlock(DbgNode).getDirectory();
497 return DISubprogram(DbgNode).getDirectory();
499 return DICompileUnit(DbgNode).getDirectory();
501 return DINameSpace(DbgNode).getDirectory();
503 return DIType(DbgNode).getDirectory();
505 return DIFile(DbgNode).getDirectory();
506 assert(0 && "Invalid DIScope!");
510 //===----------------------------------------------------------------------===//
511 // DIDescriptor: dump routines for all descriptors.
512 //===----------------------------------------------------------------------===//
515 /// print - Print descriptor.
516 void DIDescriptor::print(raw_ostream &OS) const {
517 OS << "[" << dwarf::TagString(getTag()) << "] ";
518 OS.write_hex((intptr_t) &*DbgNode) << ']';
521 /// print - Print compile unit.
522 void DICompileUnit::print(raw_ostream &OS) const {
524 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
526 OS << " [" << getDirectory() << "/" << getFilename() << "]";
529 /// print - Print type.
530 void DIType::print(raw_ostream &OS) const {
531 if (!DbgNode) return;
533 StringRef Res = getName();
535 OS << " [" << Res << "] ";
537 unsigned Tag = getTag();
538 OS << " [" << dwarf::TagString(Tag) << "] ";
540 // TODO : Print context
541 getCompileUnit().print(OS);
543 << "line " << getLineNumber() << ", "
544 << getSizeInBits() << " bits, "
545 << getAlignInBits() << " bit alignment, "
546 << getOffsetInBits() << " bit offset"
551 else if (isProtected())
552 OS << " [protected] ";
558 DIBasicType(DbgNode).print(OS);
559 else if (isDerivedType())
560 DIDerivedType(DbgNode).print(OS);
561 else if (isCompositeType())
562 DICompositeType(DbgNode).print(OS);
564 OS << "Invalid DIType\n";
571 /// print - Print basic type.
572 void DIBasicType::print(raw_ostream &OS) const {
573 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
576 /// print - Print derived type.
577 void DIDerivedType::print(raw_ostream &OS) const {
578 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
581 /// print - Print composite type.
582 void DICompositeType::print(raw_ostream &OS) const {
583 DIArray A = getTypeArray();
584 OS << " [" << A.getNumElements() << " elements]";
587 /// print - Print subprogram.
588 void DISubprogram::print(raw_ostream &OS) const {
589 StringRef Res = getName();
591 OS << " [" << Res << "] ";
593 unsigned Tag = getTag();
594 OS << " [" << dwarf::TagString(Tag) << "] ";
596 // TODO : Print context
597 getCompileUnit().print(OS);
598 OS << " [" << getLineNumber() << "] ";
609 /// print - Print global variable.
610 void DIGlobalVariable::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() << "] ";
629 if (isGlobalVariable())
630 DIGlobalVariable(DbgNode).print(OS);
634 /// print - Print variable.
635 void DIVariable::print(raw_ostream &OS) const {
636 StringRef Res = getName();
638 OS << " [" << Res << "] ";
640 getCompileUnit().print(OS);
641 OS << " [" << getLineNumber() << "] ";
645 // FIXME: Dump complex addresses
648 /// dump - Print descriptor to dbgs() with a newline.
649 void DIDescriptor::dump() const {
650 print(dbgs()); dbgs() << '\n';
653 /// dump - Print compile unit to dbgs() with a newline.
654 void DICompileUnit::dump() const {
655 print(dbgs()); dbgs() << '\n';
658 /// dump - Print type to dbgs() with a newline.
659 void DIType::dump() const {
660 print(dbgs()); dbgs() << '\n';
663 /// dump - Print basic type to dbgs() with a newline.
664 void DIBasicType::dump() const {
665 print(dbgs()); dbgs() << '\n';
668 /// dump - Print derived type to dbgs() with a newline.
669 void DIDerivedType::dump() const {
670 print(dbgs()); dbgs() << '\n';
673 /// dump - Print composite type to dbgs() with a newline.
674 void DICompositeType::dump() const {
675 print(dbgs()); dbgs() << '\n';
678 /// dump - Print subprogram to dbgs() with a newline.
679 void DISubprogram::dump() const {
680 print(dbgs()); dbgs() << '\n';
683 /// dump - Print global variable.
684 void DIGlobalVariable::dump() const {
685 print(dbgs()); dbgs() << '\n';
688 /// dump - Print variable.
689 void DIVariable::dump() const {
690 print(dbgs()); dbgs() << '\n';
693 //===----------------------------------------------------------------------===//
694 // DIFactory: Basic Helpers
695 //===----------------------------------------------------------------------===//
697 DIFactory::DIFactory(Module &m)
698 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
700 Constant *DIFactory::GetTagConstant(unsigned TAG) {
701 assert((TAG & LLVMDebugVersionMask) == 0 &&
702 "Tag too large for debug encoding!");
703 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
706 //===----------------------------------------------------------------------===//
707 // DIFactory: Primary Constructors
708 //===----------------------------------------------------------------------===//
710 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
711 /// This implicitly uniques the arrays created.
712 DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
714 Value *Null = llvm::Constant::getNullValue(Type::getInt32Ty(VMContext));
715 return DIArray(MDNode::get(VMContext, &Null, 1));
718 SmallVector<Value *, 16> Elts(Tys, Tys+NumTys);
719 return DIArray(MDNode::get(VMContext, Elts.data(), Elts.size()));
722 /// GetOrCreateSubrange - Create a descriptor for a value range. This
723 /// implicitly uniques the values returned.
724 DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
726 GetTagConstant(dwarf::DW_TAG_subrange_type),
727 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
728 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
731 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
734 /// CreateUnspecifiedParameter - Create unspeicified type descriptor
735 /// for the subroutine type.
736 DIDescriptor DIFactory::CreateUnspecifiedParameter() {
738 GetTagConstant(dwarf::DW_TAG_unspecified_parameters)
740 return DIDescriptor(MDNode::get(VMContext, &Elts[0], 1));
743 /// CreateCompileUnit - Create a new descriptor for the specified compile
744 /// unit. Note that this does not unique compile units within the module.
745 DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
752 unsigned RunTimeVer) {
754 GetTagConstant(dwarf::DW_TAG_compile_unit),
755 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
756 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
757 MDString::get(VMContext, Filename),
758 MDString::get(VMContext, Directory),
759 MDString::get(VMContext, Producer),
760 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
761 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
762 MDString::get(VMContext, Flags),
763 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
766 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
769 /// CreateFile - Create a new descriptor for the specified file.
770 DIFile DIFactory::CreateFile(StringRef Filename,
774 GetTagConstant(dwarf::DW_TAG_file_type),
775 MDString::get(VMContext, Filename),
776 MDString::get(VMContext, Directory),
780 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
783 /// CreateEnumerator - Create a single enumerator value.
784 DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
786 GetTagConstant(dwarf::DW_TAG_enumerator),
787 MDString::get(VMContext, Name),
788 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
790 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
794 /// CreateBasicType - Create a basic type like int, float, etc.
795 DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
800 uint64_t AlignInBits,
801 uint64_t OffsetInBits, unsigned Flags,
804 GetTagConstant(dwarf::DW_TAG_base_type),
806 MDString::get(VMContext, Name),
808 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
809 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
810 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
811 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
812 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
813 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
815 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
819 /// CreateBasicType - Create a basic type like int, float, etc.
820 DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
824 Constant *SizeInBits,
825 Constant *AlignInBits,
826 Constant *OffsetInBits, unsigned Flags,
829 GetTagConstant(dwarf::DW_TAG_base_type),
831 MDString::get(VMContext, Name),
833 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
837 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
838 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
840 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
843 /// CreateArtificialType - Create a new DIType with "artificial" flag set.
844 DIType DIFactory::CreateArtificialType(DIType Ty) {
845 if (Ty.isArtificial())
848 SmallVector<Value *, 9> Elts;
850 assert (N && "Unexpected input DIType!");
851 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
852 if (Value *V = N->getOperand(i))
855 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
858 unsigned CurFlags = Ty.getFlags();
859 CurFlags = CurFlags | DIType::FlagArtificial;
861 // Flags are stored at this slot.
862 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
864 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
867 /// CreateDerivedType - Create a derived type like const qualified type,
868 /// pointer, typedef, etc.
869 DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
870 DIDescriptor Context,
875 uint64_t AlignInBits,
876 uint64_t OffsetInBits,
878 DIType DerivedFrom) {
882 MDString::get(VMContext, Name),
884 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
885 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
886 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
887 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
888 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
891 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
895 /// CreateDerivedType - Create a derived type like const qualified type,
896 /// pointer, typedef, etc.
897 DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
898 DIDescriptor Context,
902 Constant *SizeInBits,
903 Constant *AlignInBits,
904 Constant *OffsetInBits,
906 DIType DerivedFrom) {
910 MDString::get(VMContext, Name),
912 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
916 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
919 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
923 /// CreateCompositeType - Create a composite type like array, struct, etc.
924 DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
925 DIDescriptor Context,
930 uint64_t AlignInBits,
931 uint64_t OffsetInBits,
935 unsigned RuntimeLang,
936 MDNode *ContainingType) {
941 MDString::get(VMContext, Name),
943 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
944 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
945 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
946 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
947 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
950 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
954 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
955 // Create a named metadata so that we do not lose this enum info.
956 if (Tag == dwarf::DW_TAG_enumeration_type) {
957 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
958 NMD->addOperand(Node);
960 return DICompositeType(Node);
964 /// CreateTemporaryType - Create a temporary forward-declared type.
965 DIType DIFactory::CreateTemporaryType() {
966 // Give the temporary MDNode a tag. It doesn't matter what tag we
967 // use here as long as DIType accepts it.
969 GetTagConstant(DW_TAG_base_type)
971 MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
976 /// CreateCompositeType - Create a composite type like array, struct, etc.
977 DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
978 DIDescriptor Context,
982 Constant *SizeInBits,
983 Constant *AlignInBits,
984 Constant *OffsetInBits,
988 unsigned RuntimeLang,
989 MDNode *ContainingType) {
993 MDString::get(VMContext, Name),
995 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
999 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1002 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
1005 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
1006 // Create a named metadata so that we do not lose this enum info.
1007 if (Tag == dwarf::DW_TAG_enumeration_type) {
1008 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
1009 NMD->addOperand(Node);
1011 return DICompositeType(Node);
1015 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
1016 /// See comments in DISubprogram for descriptions of these fields. This
1017 /// method does not unique the generated descriptors.
1018 DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
1020 StringRef DisplayName,
1021 StringRef LinkageName,
1023 unsigned LineNo, DIType Ty,
1026 unsigned VK, unsigned VIndex,
1027 DIType ContainingType,
1033 GetTagConstant(dwarf::DW_TAG_subprogram),
1034 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1036 MDString::get(VMContext, Name),
1037 MDString::get(VMContext, DisplayName),
1038 MDString::get(VMContext, LinkageName),
1040 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1042 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1043 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1044 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
1045 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
1047 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1048 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1051 MDNode *Node = MDNode::get(VMContext, &Elts[0], 17);
1053 // Create a named metadata so that we do not lose this mdnode.
1054 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1055 NMD->addOperand(Node);
1056 return DISubprogram(Node);
1059 /// CreateSubprogramDefinition - Create new subprogram descriptor for the
1060 /// given declaration.
1061 DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration){
1062 if (SPDeclaration.isDefinition())
1063 return DISubprogram(SPDeclaration);
1065 MDNode *DeclNode = SPDeclaration;
1067 GetTagConstant(dwarf::DW_TAG_subprogram),
1068 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1069 DeclNode->getOperand(2), // Context
1070 DeclNode->getOperand(3), // Name
1071 DeclNode->getOperand(4), // DisplayName
1072 DeclNode->getOperand(5), // LinkageName
1073 DeclNode->getOperand(6), // CompileUnit
1074 DeclNode->getOperand(7), // LineNo
1075 DeclNode->getOperand(8), // Type
1076 DeclNode->getOperand(9), // isLocalToUnit
1077 ConstantInt::get(Type::getInt1Ty(VMContext), true),
1078 DeclNode->getOperand(11), // Virtuality
1079 DeclNode->getOperand(12), // VIndex
1080 DeclNode->getOperand(13), // Containting Type
1081 DeclNode->getOperand(14), // Flags
1082 DeclNode->getOperand(15), // isOptimized
1083 SPDeclaration.getFunction()
1085 MDNode *Node =MDNode::get(VMContext, &Elts[0], 16);
1087 // Create a named metadata so that we do not lose this mdnode.
1088 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1089 NMD->addOperand(Node);
1090 return DISubprogram(Node);
1093 /// CreateGlobalVariable - Create a new descriptor for the specified global.
1095 DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1096 StringRef DisplayName,
1097 StringRef LinkageName,
1099 unsigned LineNo, DIType Ty,bool isLocalToUnit,
1100 bool isDefinition, llvm::GlobalVariable *Val) {
1102 GetTagConstant(dwarf::DW_TAG_variable),
1103 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1105 MDString::get(VMContext, Name),
1106 MDString::get(VMContext, DisplayName),
1107 MDString::get(VMContext, LinkageName),
1109 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1111 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1112 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1116 Value *const *Vs = &Elts[0];
1117 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1119 // Create a named metadata so that we do not lose this mdnode.
1120 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1121 NMD->addOperand(Node);
1123 return DIGlobalVariable(Node);
1126 /// CreateGlobalVariable - Create a new descriptor for the specified constant.
1128 DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1129 StringRef DisplayName,
1130 StringRef LinkageName,
1132 unsigned LineNo, DIType Ty,bool isLocalToUnit,
1133 bool isDefinition, llvm::Constant *Val) {
1135 GetTagConstant(dwarf::DW_TAG_variable),
1136 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1138 MDString::get(VMContext, Name),
1139 MDString::get(VMContext, DisplayName),
1140 MDString::get(VMContext, LinkageName),
1142 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1144 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1145 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1149 Value *const *Vs = &Elts[0];
1150 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1152 // Create a named metadata so that we do not lose this mdnode.
1153 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1154 NMD->addOperand(Node);
1156 return DIGlobalVariable(Node);
1159 /// fixupObjcLikeName - Replace contains special characters used
1160 /// in a typical Objective-C names with '.' in a given string.
1161 static void fixupObjcLikeName(std::string &Str) {
1162 for (size_t i = 0, e = Str.size(); i < e; ++i) {
1164 if (C == '[' || C == ']' || C == ' ' || C == ':' || C == '+')
1169 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
1170 /// to hold function specific information.
1171 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, StringRef FuncName) {
1172 SmallString<32> Out;
1173 if (FuncName.find('[') == StringRef::npos)
1174 return M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", FuncName)
1176 std::string Name = FuncName;
1177 fixupObjcLikeName(Name);
1178 return M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", Name)
1182 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
1183 /// suitable to hold function specific information.
1184 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, StringRef FuncName) {
1185 if (FuncName.find('[') == StringRef::npos)
1186 return M.getNamedMetadata(Twine("llvm.dbg.lv.", FuncName));
1187 std::string Name = FuncName;
1188 fixupObjcLikeName(Name);
1189 return M.getNamedMetadata(Twine("llvm.dbg.lv.", Name));
1192 /// CreateVariable - Create a new descriptor for the specified variable.
1193 DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
1197 DIType Ty, bool AlwaysPreserve,
1200 GetTagConstant(Tag),
1202 MDString::get(VMContext, Name),
1204 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1206 ConstantInt::get(Type::getInt32Ty(VMContext), Flags)
1208 MDNode *Node = MDNode::get(VMContext, &Elts[0], 7);
1209 if (AlwaysPreserve) {
1210 // The optimizer may remove local variable. If there is an interest
1211 // to preserve variable info in such situation then stash it in a
1213 DISubprogram Fn(getDISubprogram(Context));
1214 StringRef FName = "fn";
1215 if (Fn.getFunction())
1216 FName = Fn.getFunction()->getName();
1218 if (FName.startswith(StringRef(&One, 1)))
1219 FName = FName.substr(1);
1222 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, FName);
1223 FnLocals->addOperand(Node);
1225 return DIVariable(Node);
1229 /// CreateComplexVariable - Create a new descriptor for the specified variable
1230 /// which has a complex address expression for its address.
1231 DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
1232 StringRef Name, DIFile F,
1234 DIType Ty, Value *const *Addr,
1236 SmallVector<Value *, 15> Elts;
1237 Elts.push_back(GetTagConstant(Tag));
1238 Elts.push_back(Context);
1239 Elts.push_back(MDString::get(VMContext, Name));
1241 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
1243 Elts.append(Addr, Addr+NumAddr);
1245 return DIVariable(MDNode::get(VMContext, Elts.data(), Elts.size()));
1249 /// CreateBlock - This creates a descriptor for a lexical block with the
1250 /// specified parent VMContext.
1251 DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
1252 DIFile F, unsigned LineNo,
1254 // Defeat MDNode uniqing for lexical blocks.
1255 static unsigned int unique_id = 0;
1257 GetTagConstant(dwarf::DW_TAG_lexical_block),
1259 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1260 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1262 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1264 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 6));
1267 /// CreateNameSpace - This creates new descriptor for a namespace
1268 /// with the specified parent context.
1269 DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
1273 GetTagConstant(dwarf::DW_TAG_namespace),
1275 MDString::get(VMContext, Name),
1277 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1279 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1282 /// CreateLocation - Creates a debug info location.
1283 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1284 DIScope S, DILocation OrigLoc) {
1286 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1287 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1291 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1294 //===----------------------------------------------------------------------===//
1295 // DIFactory: Routines for inserting code into a function
1296 //===----------------------------------------------------------------------===//
1298 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1299 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1300 Instruction *InsertBefore) {
1301 assert(Storage && "no storage passed to dbg.declare");
1302 assert(D.Verify() && "empty DIVariable passed to dbg.declare");
1304 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1306 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1308 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
1311 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1312 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1313 BasicBlock *InsertAtEnd) {
1314 assert(Storage && "no storage passed to dbg.declare");
1315 assert(D.Verify() && "invalid DIVariable passed to dbg.declare");
1317 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1319 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1322 // If this block already has a terminator then insert this intrinsic
1323 // before the terminator.
1324 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1325 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1327 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
1329 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1330 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1332 Instruction *InsertBefore) {
1333 assert(V && "no value passed to dbg.value");
1334 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
1336 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1338 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1339 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1341 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1344 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1345 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1347 BasicBlock *InsertAtEnd) {
1348 assert(V && "no value passed to dbg.value");
1349 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
1351 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1353 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1354 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1356 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1359 // RecordType - Record DIType in a module such that it is not lost even if
1360 // it is not referenced through debug info anchors.
1361 void DIFactory::RecordType(DIType T) {
1362 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.ty");
1367 //===----------------------------------------------------------------------===//
1368 // DebugInfoFinder implementations.
1369 //===----------------------------------------------------------------------===//
1371 /// processModule - Process entire module and collect debug info.
1372 void DebugInfoFinder::processModule(Module &M) {
1373 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1374 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1375 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1377 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1378 processDeclare(DDI);
1380 DebugLoc Loc = BI->getDebugLoc();
1381 if (Loc.isUnknown())
1384 LLVMContext &Ctx = BI->getContext();
1385 DIDescriptor Scope(Loc.getScope(Ctx));
1387 if (Scope.isCompileUnit())
1388 addCompileUnit(DICompileUnit(Scope));
1389 else if (Scope.isSubprogram())
1390 processSubprogram(DISubprogram(Scope));
1391 else if (Scope.isLexicalBlock())
1392 processLexicalBlock(DILexicalBlock(Scope));
1394 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1395 processLocation(DILocation(IA));
1398 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
1399 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1400 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1401 if (addGlobalVariable(DIG)) {
1402 addCompileUnit(DIG.getCompileUnit());
1403 processType(DIG.getType());
1408 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
1409 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
1410 processSubprogram(DISubprogram(NMD->getOperand(i)));
1413 /// processLocation - Process DILocation.
1414 void DebugInfoFinder::processLocation(DILocation Loc) {
1415 if (!Loc.Verify()) return;
1416 DIDescriptor S(Loc.getScope());
1417 if (S.isCompileUnit())
1418 addCompileUnit(DICompileUnit(S));
1419 else if (S.isSubprogram())
1420 processSubprogram(DISubprogram(S));
1421 else if (S.isLexicalBlock())
1422 processLexicalBlock(DILexicalBlock(S));
1423 processLocation(Loc.getOrigLocation());
1426 /// processType - Process DIType.
1427 void DebugInfoFinder::processType(DIType DT) {
1431 addCompileUnit(DT.getCompileUnit());
1432 if (DT.isCompositeType()) {
1433 DICompositeType DCT(DT);
1434 processType(DCT.getTypeDerivedFrom());
1435 DIArray DA = DCT.getTypeArray();
1436 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1437 DIDescriptor D = DA.getElement(i);
1439 processType(DIType(D));
1440 else if (D.isSubprogram())
1441 processSubprogram(DISubprogram(D));
1443 } else if (DT.isDerivedType()) {
1444 DIDerivedType DDT(DT);
1445 processType(DDT.getTypeDerivedFrom());
1449 /// processLexicalBlock
1450 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1451 DIScope Context = LB.getContext();
1452 if (Context.isLexicalBlock())
1453 return processLexicalBlock(DILexicalBlock(Context));
1455 return processSubprogram(DISubprogram(Context));
1458 /// processSubprogram - Process DISubprogram.
1459 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1460 if (!addSubprogram(SP))
1462 addCompileUnit(SP.getCompileUnit());
1463 processType(SP.getType());
1466 /// processDeclare - Process DbgDeclareInst.
1467 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
1468 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1472 if (!DV.isVariable())
1475 if (!NodesSeen.insert(DV))
1478 addCompileUnit(DIVariable(N).getCompileUnit());
1479 processType(DIVariable(N).getType());
1482 /// addType - Add type into Tys.
1483 bool DebugInfoFinder::addType(DIType DT) {
1487 if (!NodesSeen.insert(DT))
1494 /// addCompileUnit - Add compile unit into CUs.
1495 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1499 if (!NodesSeen.insert(CU))
1506 /// addGlobalVariable - Add global variable into GVs.
1507 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1508 if (!DIDescriptor(DIG).isGlobalVariable())
1511 if (!NodesSeen.insert(DIG))
1518 // addSubprogram - Add subprgoram into SPs.
1519 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1520 if (!DIDescriptor(SP).isSubprogram())
1523 if (!NodesSeen.insert(SP))
1530 /// Find the debug info descriptor corresponding to this global variable.
1531 static Value *findDbgGlobalDeclare(GlobalVariable *V) {
1532 const Module *M = V->getParent();
1533 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1537 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1538 DIDescriptor DIG(cast<MDNode>(NMD->getOperand(i)));
1539 if (!DIG.isGlobalVariable())
1541 if (DIGlobalVariable(DIG).getGlobal() == V)
1547 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1548 /// It looks through pointer casts too.
1549 static const DbgDeclareInst *findDbgDeclare(const Value *V) {
1550 V = V->stripPointerCasts();
1552 if (!isa<Instruction>(V) && !isa<Argument>(V))
1555 const Function *F = NULL;
1556 if (const Instruction *I = dyn_cast<Instruction>(V))
1557 F = I->getParent()->getParent();
1558 else if (const Argument *A = dyn_cast<Argument>(V))
1561 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1562 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1564 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1565 if (DDI->getAddress() == V)
1571 bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1572 std::string &Type, unsigned &LineNo,
1573 std::string &File, std::string &Dir) {
1577 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1578 Value *DIGV = findDbgGlobalDeclare(GV);
1579 if (!DIGV) return false;
1580 DIGlobalVariable Var(cast<MDNode>(DIGV));
1582 StringRef D = Var.getDisplayName();
1585 LineNo = Var.getLineNumber();
1586 Unit = Var.getCompileUnit();
1587 TypeD = Var.getType();
1589 const DbgDeclareInst *DDI = findDbgDeclare(V);
1590 if (!DDI) return false;
1591 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1593 StringRef D = Var.getName();
1596 LineNo = Var.getLineNumber();
1597 Unit = Var.getCompileUnit();
1598 TypeD = Var.getType();
1601 StringRef T = TypeD.getName();
1604 StringRef F = Unit.getFilename();
1607 StringRef D = Unit.getDirectory();
1613 /// getDISubprogram - Find subprogram that is enclosing this scope.
1614 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
1615 DIDescriptor D(Scope);
1616 if (D.isSubprogram())
1617 return DISubprogram(Scope);
1619 if (D.isLexicalBlock())
1620 return getDISubprogram(DILexicalBlock(Scope).getContext());
1622 return DISubprogram();
1625 /// getDICompositeType - Find underlying composite type.
1626 DICompositeType llvm::getDICompositeType(DIType T) {
1627 if (T.isCompositeType())
1628 return DICompositeType(T);
1630 if (T.isDerivedType())
1631 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
1633 return DICompositeType();