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_reference_type && Tag != dwarf::DW_TAG_restrict_type
312 && getFilename().empty())
317 /// Verify - Verify that a basic type descriptor is well formed.
318 bool DIBasicType::Verify() const {
319 return isBasicType();
322 /// Verify - Verify that a derived type descriptor is well formed.
323 bool DIDerivedType::Verify() const {
324 return isDerivedType();
327 /// Verify - Verify that a composite type descriptor is well formed.
328 bool DICompositeType::Verify() const {
331 if (!getContext().Verify())
334 DICompileUnit CU = getCompileUnit();
340 /// Verify - Verify that a subprogram descriptor is well formed.
341 bool DISubprogram::Verify() const {
345 if (!getContext().Verify())
348 DICompileUnit CU = getCompileUnit();
352 DICompositeType Ty = getType();
358 /// Verify - Verify that a global variable descriptor is well formed.
359 bool DIGlobalVariable::Verify() const {
363 if (getDisplayName().empty())
366 if (!getContext().Verify())
369 DICompileUnit CU = getCompileUnit();
373 DIType Ty = getType();
377 if (!getGlobal() && !getConstant())
383 /// Verify - Verify that a variable descriptor is well formed.
384 bool DIVariable::Verify() const {
388 if (!getContext().Verify())
391 if (!getCompileUnit().Verify())
394 DIType Ty = getType();
401 /// Verify - Verify that a location descriptor is well formed.
402 bool DILocation::Verify() const {
406 return DbgNode->getNumOperands() == 4;
409 /// Verify - Verify that a namespace descriptor is well formed.
410 bool DINameSpace::Verify() const {
413 if (getName().empty())
415 if (!getCompileUnit().Verify())
420 /// getOriginalTypeSize - If this type is derived from a base type then
421 /// return base type size.
422 uint64_t DIDerivedType::getOriginalTypeSize() const {
423 unsigned Tag = getTag();
424 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
425 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
426 Tag == dwarf::DW_TAG_restrict_type) {
427 DIType BaseType = getTypeDerivedFrom();
428 // If this type is not derived from any type then take conservative
430 if (!BaseType.isValid())
431 return getSizeInBits();
432 if (BaseType.isDerivedType())
433 return DIDerivedType(BaseType).getOriginalTypeSize();
435 return BaseType.getSizeInBits();
438 return getSizeInBits();
441 /// isInlinedFnArgument - Return true if this variable provides debugging
442 /// information for an inlined function arguments.
443 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
444 assert(CurFn && "Invalid function");
445 if (!getContext().isSubprogram())
447 // This variable is not inlined function argument if its scope
448 // does not describe current function.
449 return !(DISubprogram(getContext()).describes(CurFn));
452 /// describes - Return true if this subprogram provides debugging
453 /// information for the function F.
454 bool DISubprogram::describes(const Function *F) {
455 assert(F && "Invalid function");
456 if (F == getFunction())
458 StringRef Name = getLinkageName();
461 if (F->getName() == Name)
466 unsigned DISubprogram::isOptimized() const {
467 assert (DbgNode && "Invalid subprogram descriptor!");
468 if (DbgNode->getNumOperands() == 16)
469 return getUnsignedField(15);
473 StringRef DIScope::getFilename() const {
476 if (isLexicalBlock())
477 return DILexicalBlock(DbgNode).getFilename();
479 return DISubprogram(DbgNode).getFilename();
481 return DICompileUnit(DbgNode).getFilename();
483 return DINameSpace(DbgNode).getFilename();
485 return DIType(DbgNode).getFilename();
487 return DIFile(DbgNode).getFilename();
488 assert(0 && "Invalid DIScope!");
492 StringRef DIScope::getDirectory() const {
495 if (isLexicalBlock())
496 return DILexicalBlock(DbgNode).getDirectory();
498 return DISubprogram(DbgNode).getDirectory();
500 return DICompileUnit(DbgNode).getDirectory();
502 return DINameSpace(DbgNode).getDirectory();
504 return DIType(DbgNode).getDirectory();
506 return DIFile(DbgNode).getDirectory();
507 assert(0 && "Invalid DIScope!");
511 //===----------------------------------------------------------------------===//
512 // DIDescriptor: dump routines for all descriptors.
513 //===----------------------------------------------------------------------===//
516 /// print - Print descriptor.
517 void DIDescriptor::print(raw_ostream &OS) const {
518 OS << "[" << dwarf::TagString(getTag()) << "] ";
519 OS.write_hex((intptr_t) &*DbgNode) << ']';
522 /// print - Print compile unit.
523 void DICompileUnit::print(raw_ostream &OS) const {
525 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
527 OS << " [" << getDirectory() << "/" << getFilename() << "]";
530 /// print - Print type.
531 void DIType::print(raw_ostream &OS) const {
532 if (!DbgNode) return;
534 StringRef Res = getName();
536 OS << " [" << Res << "] ";
538 unsigned Tag = getTag();
539 OS << " [" << dwarf::TagString(Tag) << "] ";
541 // TODO : Print context
542 getCompileUnit().print(OS);
544 << "line " << getLineNumber() << ", "
545 << getSizeInBits() << " bits, "
546 << getAlignInBits() << " bit alignment, "
547 << getOffsetInBits() << " bit offset"
552 else if (isProtected())
553 OS << " [protected] ";
559 DIBasicType(DbgNode).print(OS);
560 else if (isDerivedType())
561 DIDerivedType(DbgNode).print(OS);
562 else if (isCompositeType())
563 DICompositeType(DbgNode).print(OS);
565 OS << "Invalid DIType\n";
572 /// print - Print basic type.
573 void DIBasicType::print(raw_ostream &OS) const {
574 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
577 /// print - Print derived type.
578 void DIDerivedType::print(raw_ostream &OS) const {
579 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
582 /// print - Print composite type.
583 void DICompositeType::print(raw_ostream &OS) const {
584 DIArray A = getTypeArray();
585 OS << " [" << A.getNumElements() << " elements]";
588 /// print - Print subprogram.
589 void DISubprogram::print(raw_ostream &OS) const {
590 StringRef Res = getName();
592 OS << " [" << Res << "] ";
594 unsigned Tag = getTag();
595 OS << " [" << dwarf::TagString(Tag) << "] ";
597 // TODO : Print context
598 getCompileUnit().print(OS);
599 OS << " [" << getLineNumber() << "] ";
610 /// print - Print global variable.
611 void DIGlobalVariable::print(raw_ostream &OS) const {
613 StringRef Res = getName();
615 OS << " [" << Res << "] ";
617 unsigned Tag = getTag();
618 OS << " [" << dwarf::TagString(Tag) << "] ";
620 // TODO : Print context
621 getCompileUnit().print(OS);
622 OS << " [" << getLineNumber() << "] ";
630 if (isGlobalVariable())
631 DIGlobalVariable(DbgNode).print(OS);
635 /// print - Print variable.
636 void DIVariable::print(raw_ostream &OS) const {
637 StringRef Res = getName();
639 OS << " [" << Res << "] ";
641 getCompileUnit().print(OS);
642 OS << " [" << getLineNumber() << "] ";
646 // FIXME: Dump complex addresses
649 /// dump - Print descriptor to dbgs() with a newline.
650 void DIDescriptor::dump() const {
651 print(dbgs()); dbgs() << '\n';
654 /// dump - Print compile unit to dbgs() with a newline.
655 void DICompileUnit::dump() const {
656 print(dbgs()); dbgs() << '\n';
659 /// dump - Print type to dbgs() with a newline.
660 void DIType::dump() const {
661 print(dbgs()); dbgs() << '\n';
664 /// dump - Print basic type to dbgs() with a newline.
665 void DIBasicType::dump() const {
666 print(dbgs()); dbgs() << '\n';
669 /// dump - Print derived type to dbgs() with a newline.
670 void DIDerivedType::dump() const {
671 print(dbgs()); dbgs() << '\n';
674 /// dump - Print composite type to dbgs() with a newline.
675 void DICompositeType::dump() const {
676 print(dbgs()); dbgs() << '\n';
679 /// dump - Print subprogram to dbgs() with a newline.
680 void DISubprogram::dump() const {
681 print(dbgs()); dbgs() << '\n';
684 /// dump - Print global variable.
685 void DIGlobalVariable::dump() const {
686 print(dbgs()); dbgs() << '\n';
689 /// dump - Print variable.
690 void DIVariable::dump() const {
691 print(dbgs()); dbgs() << '\n';
694 //===----------------------------------------------------------------------===//
695 // DIFactory: Basic Helpers
696 //===----------------------------------------------------------------------===//
698 DIFactory::DIFactory(Module &m)
699 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
701 Constant *DIFactory::GetTagConstant(unsigned TAG) {
702 assert((TAG & LLVMDebugVersionMask) == 0 &&
703 "Tag too large for debug encoding!");
704 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
707 //===----------------------------------------------------------------------===//
708 // DIFactory: Primary Constructors
709 //===----------------------------------------------------------------------===//
711 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
712 /// This implicitly uniques the arrays created.
713 DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
715 Value *Null = llvm::Constant::getNullValue(Type::getInt32Ty(VMContext));
716 return DIArray(MDNode::get(VMContext, &Null, 1));
719 SmallVector<Value *, 16> Elts(Tys, Tys+NumTys);
720 return DIArray(MDNode::get(VMContext, Elts.data(), Elts.size()));
723 /// GetOrCreateSubrange - Create a descriptor for a value range. This
724 /// implicitly uniques the values returned.
725 DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
727 GetTagConstant(dwarf::DW_TAG_subrange_type),
728 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
729 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
732 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
735 /// CreateUnspecifiedParameter - Create unspeicified type descriptor
736 /// for the subroutine type.
737 DIDescriptor DIFactory::CreateUnspecifiedParameter() {
739 GetTagConstant(dwarf::DW_TAG_unspecified_parameters)
741 return DIDescriptor(MDNode::get(VMContext, &Elts[0], 1));
744 /// CreateCompileUnit - Create a new descriptor for the specified compile
745 /// unit. Note that this does not unique compile units within the module.
746 DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
753 unsigned RunTimeVer) {
755 GetTagConstant(dwarf::DW_TAG_compile_unit),
756 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
757 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
758 MDString::get(VMContext, Filename),
759 MDString::get(VMContext, Directory),
760 MDString::get(VMContext, Producer),
761 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
762 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
763 MDString::get(VMContext, Flags),
764 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
767 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
770 /// CreateFile - Create a new descriptor for the specified file.
771 DIFile DIFactory::CreateFile(StringRef Filename,
775 GetTagConstant(dwarf::DW_TAG_file_type),
776 MDString::get(VMContext, Filename),
777 MDString::get(VMContext, Directory),
781 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
784 /// CreateEnumerator - Create a single enumerator value.
785 DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
787 GetTagConstant(dwarf::DW_TAG_enumerator),
788 MDString::get(VMContext, Name),
789 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
791 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
795 /// CreateBasicType - Create a basic type like int, float, etc.
796 DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
801 uint64_t AlignInBits,
802 uint64_t OffsetInBits, unsigned Flags,
805 GetTagConstant(dwarf::DW_TAG_base_type),
807 MDString::get(VMContext, Name),
809 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
810 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
811 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
812 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
813 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
814 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
816 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
820 /// CreateBasicType - Create a basic type like int, float, etc.
821 DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
825 Constant *SizeInBits,
826 Constant *AlignInBits,
827 Constant *OffsetInBits, unsigned Flags,
830 GetTagConstant(dwarf::DW_TAG_base_type),
832 MDString::get(VMContext, Name),
834 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
838 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
839 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
841 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
844 /// CreateArtificialType - Create a new DIType with "artificial" flag set.
845 DIType DIFactory::CreateArtificialType(DIType Ty) {
846 if (Ty.isArtificial())
849 SmallVector<Value *, 9> Elts;
851 assert (N && "Unexpected input DIType!");
852 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
853 if (Value *V = N->getOperand(i))
856 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
859 unsigned CurFlags = Ty.getFlags();
860 CurFlags = CurFlags | DIType::FlagArtificial;
862 // Flags are stored at this slot.
863 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
865 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
868 /// CreateDerivedType - Create a derived type like const qualified type,
869 /// pointer, typedef, etc.
870 DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
871 DIDescriptor Context,
876 uint64_t AlignInBits,
877 uint64_t OffsetInBits,
879 DIType DerivedFrom) {
883 MDString::get(VMContext, Name),
885 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
886 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
887 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
888 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
889 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
892 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
896 /// CreateDerivedType - Create a derived type like const qualified type,
897 /// pointer, typedef, etc.
898 DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
899 DIDescriptor Context,
903 Constant *SizeInBits,
904 Constant *AlignInBits,
905 Constant *OffsetInBits,
907 DIType DerivedFrom) {
911 MDString::get(VMContext, Name),
913 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
917 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
920 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
924 /// CreateCompositeType - Create a composite type like array, struct, etc.
925 DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
926 DIDescriptor Context,
931 uint64_t AlignInBits,
932 uint64_t OffsetInBits,
936 unsigned RuntimeLang,
937 MDNode *ContainingType) {
942 MDString::get(VMContext, Name),
944 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
945 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
946 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
947 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
948 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
951 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
955 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
956 // Create a named metadata so that we do not lose this enum info.
957 if (Tag == dwarf::DW_TAG_enumeration_type) {
958 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
959 NMD->addOperand(Node);
961 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));
975 /// CreateTemporaryType - Create a temporary forward-declared type.
976 DIType DIFactory::CreateTemporaryType(DIFile F) {
977 // Give the temporary MDNode a tag. It doesn't matter what tag we
978 // use here as long as DIType accepts it.
980 GetTagConstant(DW_TAG_base_type),
985 MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
989 /// CreateCompositeType - Create a composite type like array, struct, etc.
990 DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
991 DIDescriptor Context,
995 Constant *SizeInBits,
996 Constant *AlignInBits,
997 Constant *OffsetInBits,
1001 unsigned RuntimeLang,
1002 MDNode *ContainingType) {
1004 GetTagConstant(Tag),
1006 MDString::get(VMContext, Name),
1008 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
1012 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1015 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
1018 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
1019 // Create a named metadata so that we do not lose this enum info.
1020 if (Tag == dwarf::DW_TAG_enumeration_type) {
1021 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
1022 NMD->addOperand(Node);
1024 return DICompositeType(Node);
1028 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
1029 /// See comments in DISubprogram for descriptions of these fields. This
1030 /// method does not unique the generated descriptors.
1031 DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
1033 StringRef DisplayName,
1034 StringRef LinkageName,
1036 unsigned LineNo, DIType Ty,
1039 unsigned VK, unsigned VIndex,
1040 DIType ContainingType,
1046 GetTagConstant(dwarf::DW_TAG_subprogram),
1047 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1049 MDString::get(VMContext, Name),
1050 MDString::get(VMContext, DisplayName),
1051 MDString::get(VMContext, LinkageName),
1053 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1055 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1056 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1057 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
1058 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
1060 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1061 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1064 MDNode *Node = MDNode::get(VMContext, &Elts[0], 17);
1066 // Create a named metadata so that we do not lose this mdnode.
1067 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1068 NMD->addOperand(Node);
1069 return DISubprogram(Node);
1072 /// CreateSubprogramDefinition - Create new subprogram descriptor for the
1073 /// given declaration.
1074 DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration){
1075 if (SPDeclaration.isDefinition())
1076 return DISubprogram(SPDeclaration);
1078 MDNode *DeclNode = SPDeclaration;
1080 GetTagConstant(dwarf::DW_TAG_subprogram),
1081 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1082 DeclNode->getOperand(2), // Context
1083 DeclNode->getOperand(3), // Name
1084 DeclNode->getOperand(4), // DisplayName
1085 DeclNode->getOperand(5), // LinkageName
1086 DeclNode->getOperand(6), // CompileUnit
1087 DeclNode->getOperand(7), // LineNo
1088 DeclNode->getOperand(8), // Type
1089 DeclNode->getOperand(9), // isLocalToUnit
1090 ConstantInt::get(Type::getInt1Ty(VMContext), true),
1091 DeclNode->getOperand(11), // Virtuality
1092 DeclNode->getOperand(12), // VIndex
1093 DeclNode->getOperand(13), // Containting Type
1094 DeclNode->getOperand(14), // Flags
1095 DeclNode->getOperand(15), // isOptimized
1096 SPDeclaration.getFunction()
1098 MDNode *Node =MDNode::get(VMContext, &Elts[0], 16);
1100 // Create a named metadata so that we do not lose this mdnode.
1101 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1102 NMD->addOperand(Node);
1103 return DISubprogram(Node);
1106 /// CreateGlobalVariable - Create a new descriptor for the specified global.
1108 DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1109 StringRef DisplayName,
1110 StringRef LinkageName,
1112 unsigned LineNo, DIType Ty,bool isLocalToUnit,
1113 bool isDefinition, llvm::GlobalVariable *Val) {
1115 GetTagConstant(dwarf::DW_TAG_variable),
1116 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1118 MDString::get(VMContext, Name),
1119 MDString::get(VMContext, DisplayName),
1120 MDString::get(VMContext, LinkageName),
1122 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1124 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1125 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1129 Value *const *Vs = &Elts[0];
1130 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1132 // Create a named metadata so that we do not lose this mdnode.
1133 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1134 NMD->addOperand(Node);
1136 return DIGlobalVariable(Node);
1139 /// CreateGlobalVariable - Create a new descriptor for the specified constant.
1141 DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1142 StringRef DisplayName,
1143 StringRef LinkageName,
1145 unsigned LineNo, DIType Ty,bool isLocalToUnit,
1146 bool isDefinition, llvm::Constant *Val) {
1148 GetTagConstant(dwarf::DW_TAG_variable),
1149 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1151 MDString::get(VMContext, Name),
1152 MDString::get(VMContext, DisplayName),
1153 MDString::get(VMContext, LinkageName),
1155 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1157 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1158 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1162 Value *const *Vs = &Elts[0];
1163 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1165 // Create a named metadata so that we do not lose this mdnode.
1166 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1167 NMD->addOperand(Node);
1169 return DIGlobalVariable(Node);
1172 /// fixupObjcLikeName - Replace contains special characters used
1173 /// in a typical Objective-C names with '.' in a given string.
1174 static void fixupObjcLikeName(std::string &Str) {
1175 for (size_t i = 0, e = Str.size(); i < e; ++i) {
1177 if (C == '[' || C == ']' || C == ' ' || C == ':' || C == '+' ||
1178 C == '(' || C == ')')
1183 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
1184 /// to hold function specific information.
1185 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, StringRef FuncName) {
1186 SmallString<32> Out;
1187 if (FuncName.find('[') == StringRef::npos)
1188 return M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", FuncName)
1190 std::string Name = FuncName;
1191 fixupObjcLikeName(Name);
1192 return M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", Name)
1196 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
1197 /// suitable to hold function specific information.
1198 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, StringRef FuncName) {
1199 if (FuncName.find('[') == StringRef::npos)
1200 return M.getNamedMetadata(Twine("llvm.dbg.lv.", FuncName));
1201 std::string Name = FuncName;
1202 fixupObjcLikeName(Name);
1203 return M.getNamedMetadata(Twine("llvm.dbg.lv.", Name));
1206 /// CreateVariable - Create a new descriptor for the specified variable.
1207 DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
1211 DIType Ty, bool AlwaysPreserve,
1214 GetTagConstant(Tag),
1216 MDString::get(VMContext, Name),
1218 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1220 ConstantInt::get(Type::getInt32Ty(VMContext), Flags)
1222 MDNode *Node = MDNode::get(VMContext, &Elts[0], 7);
1223 if (AlwaysPreserve) {
1224 // The optimizer may remove local variable. If there is an interest
1225 // to preserve variable info in such situation then stash it in a
1227 DISubprogram Fn(getDISubprogram(Context));
1228 StringRef FName = "fn";
1229 if (Fn.getFunction())
1230 FName = Fn.getFunction()->getName();
1232 if (FName.startswith(StringRef(&One, 1)))
1233 FName = FName.substr(1);
1236 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, FName);
1237 FnLocals->addOperand(Node);
1239 return DIVariable(Node);
1243 /// CreateComplexVariable - Create a new descriptor for the specified variable
1244 /// which has a complex address expression for its address.
1245 DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
1246 StringRef Name, DIFile F,
1248 DIType Ty, Value *const *Addr,
1250 SmallVector<Value *, 15> Elts;
1251 Elts.push_back(GetTagConstant(Tag));
1252 Elts.push_back(Context);
1253 Elts.push_back(MDString::get(VMContext, Name));
1255 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
1257 Elts.append(Addr, Addr+NumAddr);
1259 return DIVariable(MDNode::get(VMContext, Elts.data(), Elts.size()));
1263 /// CreateBlock - This creates a descriptor for a lexical block with the
1264 /// specified parent VMContext.
1265 DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
1266 DIFile F, unsigned LineNo,
1268 // Defeat MDNode uniqing for lexical blocks.
1269 static unsigned int unique_id = 0;
1271 GetTagConstant(dwarf::DW_TAG_lexical_block),
1273 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1274 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1276 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1278 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 6));
1281 /// CreateNameSpace - This creates new descriptor for a namespace
1282 /// with the specified parent context.
1283 DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
1287 GetTagConstant(dwarf::DW_TAG_namespace),
1289 MDString::get(VMContext, Name),
1291 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1293 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1296 /// CreateLocation - Creates a debug info location.
1297 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1298 DIScope S, DILocation OrigLoc) {
1300 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1301 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1305 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1308 //===----------------------------------------------------------------------===//
1309 // DIFactory: Routines for inserting code into a function
1310 //===----------------------------------------------------------------------===//
1312 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1313 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1314 Instruction *InsertBefore) {
1315 assert(Storage && "no storage passed to dbg.declare");
1316 assert(D.Verify() && "empty DIVariable passed to dbg.declare");
1318 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1320 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1322 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
1325 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1326 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1327 BasicBlock *InsertAtEnd) {
1328 assert(Storage && "no storage passed to dbg.declare");
1329 assert(D.Verify() && "invalid DIVariable passed to dbg.declare");
1331 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1333 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1336 // If this block already has a terminator then insert this intrinsic
1337 // before the terminator.
1338 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1339 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1341 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
1343 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1344 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1346 Instruction *InsertBefore) {
1347 assert(V && "no value passed to dbg.value");
1348 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
1350 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1352 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1353 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1355 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1358 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1359 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1361 BasicBlock *InsertAtEnd) {
1362 assert(V && "no value passed to dbg.value");
1363 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
1365 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1367 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1368 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1370 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1373 // RecordType - Record DIType in a module such that it is not lost even if
1374 // it is not referenced through debug info anchors.
1375 void DIFactory::RecordType(DIType T) {
1376 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.ty");
1381 //===----------------------------------------------------------------------===//
1382 // DebugInfoFinder implementations.
1383 //===----------------------------------------------------------------------===//
1385 /// processModule - Process entire module and collect debug info.
1386 void DebugInfoFinder::processModule(Module &M) {
1387 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1388 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1389 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1391 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1392 processDeclare(DDI);
1394 DebugLoc Loc = BI->getDebugLoc();
1395 if (Loc.isUnknown())
1398 LLVMContext &Ctx = BI->getContext();
1399 DIDescriptor Scope(Loc.getScope(Ctx));
1401 if (Scope.isCompileUnit())
1402 addCompileUnit(DICompileUnit(Scope));
1403 else if (Scope.isSubprogram())
1404 processSubprogram(DISubprogram(Scope));
1405 else if (Scope.isLexicalBlock())
1406 processLexicalBlock(DILexicalBlock(Scope));
1408 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1409 processLocation(DILocation(IA));
1412 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
1413 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1414 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1415 if (addGlobalVariable(DIG)) {
1416 addCompileUnit(DIG.getCompileUnit());
1417 processType(DIG.getType());
1422 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
1423 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
1424 processSubprogram(DISubprogram(NMD->getOperand(i)));
1427 /// processLocation - Process DILocation.
1428 void DebugInfoFinder::processLocation(DILocation Loc) {
1429 if (!Loc.Verify()) return;
1430 DIDescriptor S(Loc.getScope());
1431 if (S.isCompileUnit())
1432 addCompileUnit(DICompileUnit(S));
1433 else if (S.isSubprogram())
1434 processSubprogram(DISubprogram(S));
1435 else if (S.isLexicalBlock())
1436 processLexicalBlock(DILexicalBlock(S));
1437 processLocation(Loc.getOrigLocation());
1440 /// processType - Process DIType.
1441 void DebugInfoFinder::processType(DIType DT) {
1445 addCompileUnit(DT.getCompileUnit());
1446 if (DT.isCompositeType()) {
1447 DICompositeType DCT(DT);
1448 processType(DCT.getTypeDerivedFrom());
1449 DIArray DA = DCT.getTypeArray();
1450 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1451 DIDescriptor D = DA.getElement(i);
1453 processType(DIType(D));
1454 else if (D.isSubprogram())
1455 processSubprogram(DISubprogram(D));
1457 } else if (DT.isDerivedType()) {
1458 DIDerivedType DDT(DT);
1459 processType(DDT.getTypeDerivedFrom());
1463 /// processLexicalBlock
1464 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1465 DIScope Context = LB.getContext();
1466 if (Context.isLexicalBlock())
1467 return processLexicalBlock(DILexicalBlock(Context));
1469 return processSubprogram(DISubprogram(Context));
1472 /// processSubprogram - Process DISubprogram.
1473 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1474 if (!addSubprogram(SP))
1476 addCompileUnit(SP.getCompileUnit());
1477 processType(SP.getType());
1480 /// processDeclare - Process DbgDeclareInst.
1481 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
1482 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1486 if (!DV.isVariable())
1489 if (!NodesSeen.insert(DV))
1492 addCompileUnit(DIVariable(N).getCompileUnit());
1493 processType(DIVariable(N).getType());
1496 /// addType - Add type into Tys.
1497 bool DebugInfoFinder::addType(DIType DT) {
1501 if (!NodesSeen.insert(DT))
1508 /// addCompileUnit - Add compile unit into CUs.
1509 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1513 if (!NodesSeen.insert(CU))
1520 /// addGlobalVariable - Add global variable into GVs.
1521 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1522 if (!DIDescriptor(DIG).isGlobalVariable())
1525 if (!NodesSeen.insert(DIG))
1532 // addSubprogram - Add subprgoram into SPs.
1533 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1534 if (!DIDescriptor(SP).isSubprogram())
1537 if (!NodesSeen.insert(SP))
1544 /// Find the debug info descriptor corresponding to this global variable.
1545 static Value *findDbgGlobalDeclare(GlobalVariable *V) {
1546 const Module *M = V->getParent();
1547 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1551 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1552 DIDescriptor DIG(cast<MDNode>(NMD->getOperand(i)));
1553 if (!DIG.isGlobalVariable())
1555 if (DIGlobalVariable(DIG).getGlobal() == V)
1561 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1562 /// It looks through pointer casts too.
1563 static const DbgDeclareInst *findDbgDeclare(const Value *V) {
1564 V = V->stripPointerCasts();
1566 if (!isa<Instruction>(V) && !isa<Argument>(V))
1569 const Function *F = NULL;
1570 if (const Instruction *I = dyn_cast<Instruction>(V))
1571 F = I->getParent()->getParent();
1572 else if (const Argument *A = dyn_cast<Argument>(V))
1575 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1576 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1578 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1579 if (DDI->getAddress() == V)
1585 bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1586 std::string &Type, unsigned &LineNo,
1587 std::string &File, std::string &Dir) {
1591 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1592 Value *DIGV = findDbgGlobalDeclare(GV);
1593 if (!DIGV) return false;
1594 DIGlobalVariable Var(cast<MDNode>(DIGV));
1596 StringRef D = Var.getDisplayName();
1599 LineNo = Var.getLineNumber();
1600 Unit = Var.getCompileUnit();
1601 TypeD = Var.getType();
1603 const DbgDeclareInst *DDI = findDbgDeclare(V);
1604 if (!DDI) return false;
1605 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1607 StringRef D = Var.getName();
1610 LineNo = Var.getLineNumber();
1611 Unit = Var.getCompileUnit();
1612 TypeD = Var.getType();
1615 StringRef T = TypeD.getName();
1618 StringRef F = Unit.getFilename();
1621 StringRef D = Unit.getDirectory();
1627 /// getDISubprogram - Find subprogram that is enclosing this scope.
1628 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
1629 DIDescriptor D(Scope);
1630 if (D.isSubprogram())
1631 return DISubprogram(Scope);
1633 if (D.isLexicalBlock())
1634 return getDISubprogram(DILexicalBlock(Scope).getContext());
1636 return DISubprogram();
1639 /// getDICompositeType - Find underlying composite type.
1640 DICompositeType llvm::getDICompositeType(DIType T) {
1641 if (T.isCompositeType())
1642 return DICompositeType(T);
1644 if (T.isDerivedType())
1645 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
1647 return DICompositeType();