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/Target/TargetMachine.h" // FIXME: LAYERING VIOLATION!
17 #include "llvm/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Intrinsics.h"
20 #include "llvm/IntrinsicInst.h"
21 #include "llvm/Instructions.h"
22 #include "llvm/Module.h"
23 #include "llvm/Analysis/ValueTracking.h"
24 #include "llvm/ADT/SmallPtrSet.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/Dwarf.h"
27 #include "llvm/Support/raw_ostream.h"
29 using namespace llvm::dwarf;
31 //===----------------------------------------------------------------------===//
33 //===----------------------------------------------------------------------===//
36 DIDescriptor::getStringField(unsigned Elt) const {
40 if (Elt < DbgNode->getNumOperands())
41 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
42 return MDS->getString();
47 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
51 if (Elt < DbgNode->getNumOperands())
52 if (ConstantInt *CI = dyn_cast<ConstantInt>(DbgNode->getOperand(Elt)))
53 return CI->getZExtValue();
58 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
60 return DIDescriptor();
62 if (Elt < DbgNode->getNumOperands())
63 return DIDescriptor(dyn_cast_or_null<const MDNode>(DbgNode->getOperand(Elt)));
64 return DIDescriptor();
67 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
71 if (Elt < DbgNode->getNumOperands())
72 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
76 unsigned DIVariable::getNumAddrElements() const {
77 return DbgNode->getNumOperands()-6;
81 //===----------------------------------------------------------------------===//
83 //===----------------------------------------------------------------------===//
85 /// isBasicType - Return true if the specified tag is legal for
87 bool DIDescriptor::isBasicType() const {
88 return DbgNode && getTag() == dwarf::DW_TAG_base_type;
91 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
92 bool DIDescriptor::isDerivedType() const {
93 if (!DbgNode) return false;
95 case dwarf::DW_TAG_typedef:
96 case dwarf::DW_TAG_pointer_type:
97 case dwarf::DW_TAG_reference_type:
98 case dwarf::DW_TAG_const_type:
99 case dwarf::DW_TAG_volatile_type:
100 case dwarf::DW_TAG_restrict_type:
101 case dwarf::DW_TAG_member:
102 case dwarf::DW_TAG_inheritance:
105 // CompositeTypes are currently modelled as DerivedTypes.
106 return isCompositeType();
110 /// isCompositeType - Return true if the specified tag is legal for
112 bool DIDescriptor::isCompositeType() const {
113 if (!DbgNode) return false;
115 case dwarf::DW_TAG_array_type:
116 case dwarf::DW_TAG_structure_type:
117 case dwarf::DW_TAG_union_type:
118 case dwarf::DW_TAG_enumeration_type:
119 case dwarf::DW_TAG_vector_type:
120 case dwarf::DW_TAG_subroutine_type:
121 case dwarf::DW_TAG_class_type:
128 /// isVariable - Return true if the specified tag is legal for DIVariable.
129 bool DIDescriptor::isVariable() const {
130 if (!DbgNode) return false;
132 case dwarf::DW_TAG_auto_variable:
133 case dwarf::DW_TAG_arg_variable:
134 case dwarf::DW_TAG_return_variable:
141 /// isType - Return true if the specified tag is legal for DIType.
142 bool DIDescriptor::isType() const {
143 return isBasicType() || isCompositeType() || isDerivedType();
146 /// isSubprogram - Return true if the specified tag is legal for
148 bool DIDescriptor::isSubprogram() const {
149 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
152 /// isGlobalVariable - Return true if the specified tag is legal for
153 /// DIGlobalVariable.
154 bool DIDescriptor::isGlobalVariable() const {
155 return DbgNode && getTag() == dwarf::DW_TAG_variable;
158 /// isGlobal - Return true if the specified tag is legal for DIGlobal.
159 bool DIDescriptor::isGlobal() const {
160 return isGlobalVariable();
163 /// isScope - Return true if the specified tag is one of the scope
165 bool DIDescriptor::isScope() const {
166 if (!DbgNode) return false;
168 case dwarf::DW_TAG_compile_unit:
169 case dwarf::DW_TAG_lexical_block:
170 case dwarf::DW_TAG_subprogram:
171 case dwarf::DW_TAG_namespace:
179 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
180 bool DIDescriptor::isCompileUnit() const {
181 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
184 /// isFile - Return true if the specified tag is DW_TAG_file_type.
185 bool DIDescriptor::isFile() const {
186 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
189 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
190 bool DIDescriptor::isNameSpace() const {
191 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
194 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
195 bool DIDescriptor::isLexicalBlock() const {
196 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
199 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
200 bool DIDescriptor::isSubrange() const {
201 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
204 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
205 bool DIDescriptor::isEnumerator() const {
206 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
209 //===----------------------------------------------------------------------===//
210 // Simple Descriptor Constructors and other Methods
211 //===----------------------------------------------------------------------===//
213 DIType::DIType(const MDNode *N) : DIScope(N) {
215 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
220 unsigned DIArray::getNumElements() const {
223 return DbgNode->getNumOperands();
226 /// replaceAllUsesWith - Replace all uses of debug info referenced by
227 /// this descriptor. After this completes, the current debug info value
229 void DIDerivedType::replaceAllUsesWith(DIDescriptor &D) {
233 // Since we use a TrackingVH for the node, its easy for clients to manufacture
234 // legitimate situations where they want to replaceAllUsesWith() on something
235 // which, due to uniquing, has merged with the source. We shield clients from
236 // this detail by allowing a value to be replaced with replaceAllUsesWith()
239 MDNode *Node = const_cast<MDNode*>(DbgNode);
240 const MDNode *DN = D;
241 const Value *V = cast_or_null<Value>(DN);
242 Node->replaceAllUsesWith(const_cast<Value*>(V));
247 /// Verify - Verify that a compile unit is well formed.
248 bool DICompileUnit::Verify() const {
251 StringRef N = getFilename();
254 // It is possible that directory and produce string is empty.
258 /// Verify - Verify that a type descriptor is well formed.
259 bool DIType::Verify() const {
262 if (!getContext().Verify())
265 DICompileUnit CU = getCompileUnit();
271 /// Verify - Verify that a composite type descriptor is well formed.
272 bool DICompositeType::Verify() const {
275 if (!getContext().Verify())
278 DICompileUnit CU = getCompileUnit();
284 /// Verify - Verify that a subprogram descriptor is well formed.
285 bool DISubprogram::Verify() const {
289 if (!getContext().Verify())
292 DICompileUnit CU = getCompileUnit();
296 DICompositeType Ty = getType();
302 /// Verify - Verify that a global variable descriptor is well formed.
303 bool DIGlobalVariable::Verify() const {
307 if (getDisplayName().empty())
310 if (!getContext().Verify())
313 DICompileUnit CU = getCompileUnit();
317 DIType Ty = getType();
327 /// Verify - Verify that a variable descriptor is well formed.
328 bool DIVariable::Verify() const {
332 if (!getContext().Verify())
335 if (!getCompileUnit().Verify())
338 DIType Ty = getType();
345 /// Verify - Verify that a location descriptor is well formed.
346 bool DILocation::Verify() const {
350 return DbgNode->getNumOperands() == 4;
353 /// Verify - Verify that a namespace descriptor is well formed.
354 bool DINameSpace::Verify() const {
357 if (getName().empty())
359 if (!getCompileUnit().Verify())
364 /// getOriginalTypeSize - If this type is derived from a base type then
365 /// return base type size.
366 uint64_t DIDerivedType::getOriginalTypeSize() const {
367 unsigned Tag = getTag();
368 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
369 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
370 Tag == dwarf::DW_TAG_restrict_type) {
371 DIType BaseType = getTypeDerivedFrom();
372 // If this type is not derived from any type then take conservative
374 if (!BaseType.isValid())
375 return getSizeInBits();
376 if (BaseType.isDerivedType())
377 return DIDerivedType(BaseType).getOriginalTypeSize();
379 return BaseType.getSizeInBits();
382 return getSizeInBits();
385 /// isInlinedFnArgument - Return trule if this variable provides debugging
386 /// information for an inlined function arguments.
387 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
388 assert(CurFn && "Invalid function");
389 if (!getContext().isSubprogram())
391 // This variable is not inlined function argument if its scope
392 // does not describe current function.
393 return !(DISubprogram(getContext()).describes(CurFn));
396 /// describes - Return true if this subprogram provides debugging
397 /// information for the function F.
398 bool DISubprogram::describes(const Function *F) {
399 assert(F && "Invalid function");
400 StringRef Name = getLinkageName();
403 if (F->getName() == Name)
408 unsigned DISubprogram::isOptimized() const {
409 assert (DbgNode && "Invalid subprogram descriptor!");
410 if (DbgNode->getNumOperands() == 16)
411 return getUnsignedField(15);
415 StringRef DIScope::getFilename() const {
418 if (isLexicalBlock())
419 return DILexicalBlock(DbgNode).getFilename();
421 return DISubprogram(DbgNode).getFilename();
423 return DICompileUnit(DbgNode).getFilename();
425 return DINameSpace(DbgNode).getFilename();
427 return DIType(DbgNode).getFilename();
429 return DIFile(DbgNode).getFilename();
430 assert(0 && "Invalid DIScope!");
434 StringRef DIScope::getDirectory() const {
437 if (isLexicalBlock())
438 return DILexicalBlock(DbgNode).getDirectory();
440 return DISubprogram(DbgNode).getDirectory();
442 return DICompileUnit(DbgNode).getDirectory();
444 return DINameSpace(DbgNode).getDirectory();
446 return DIType(DbgNode).getDirectory();
448 return DIFile(DbgNode).getDirectory();
449 assert(0 && "Invalid DIScope!");
453 //===----------------------------------------------------------------------===//
454 // DIDescriptor: dump routines for all descriptors.
455 //===----------------------------------------------------------------------===//
458 /// print - Print descriptor.
459 void DIDescriptor::print(raw_ostream &OS) const {
460 OS << "[" << dwarf::TagString(getTag()) << "] ";
461 OS.write_hex((intptr_t) &*DbgNode) << ']';
464 /// print - Print compile unit.
465 void DICompileUnit::print(raw_ostream &OS) const {
467 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
469 OS << " [" << getDirectory() << "/" << getFilename() << "]";
472 /// print - Print type.
473 void DIType::print(raw_ostream &OS) const {
474 if (!DbgNode) return;
476 StringRef Res = getName();
478 OS << " [" << Res << "] ";
480 unsigned Tag = getTag();
481 OS << " [" << dwarf::TagString(Tag) << "] ";
483 // TODO : Print context
484 getCompileUnit().print(OS);
486 << "line " << getLineNumber() << ", "
487 << getSizeInBits() << " bits, "
488 << getAlignInBits() << " bit alignment, "
489 << getOffsetInBits() << " bit offset"
494 else if (isProtected())
495 OS << " [protected] ";
501 DIBasicType(DbgNode).print(OS);
502 else if (isDerivedType())
503 DIDerivedType(DbgNode).print(OS);
504 else if (isCompositeType())
505 DICompositeType(DbgNode).print(OS);
507 OS << "Invalid DIType\n";
514 /// print - Print basic type.
515 void DIBasicType::print(raw_ostream &OS) const {
516 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
519 /// print - Print derived type.
520 void DIDerivedType::print(raw_ostream &OS) const {
521 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
524 /// print - Print composite type.
525 void DICompositeType::print(raw_ostream &OS) const {
526 DIArray A = getTypeArray();
527 OS << " [" << A.getNumElements() << " elements]";
530 /// print - Print subprogram.
531 void DISubprogram::print(raw_ostream &OS) const {
532 StringRef Res = getName();
534 OS << " [" << Res << "] ";
536 unsigned Tag = getTag();
537 OS << " [" << dwarf::TagString(Tag) << "] ";
539 // TODO : Print context
540 getCompileUnit().print(OS);
541 OS << " [" << getLineNumber() << "] ";
552 /// print - Print global variable.
553 void DIGlobalVariable::print(raw_ostream &OS) const {
555 StringRef Res = getName();
557 OS << " [" << Res << "] ";
559 unsigned Tag = getTag();
560 OS << " [" << dwarf::TagString(Tag) << "] ";
562 // TODO : Print context
563 getCompileUnit().print(OS);
564 OS << " [" << getLineNumber() << "] ";
572 if (isGlobalVariable())
573 DIGlobalVariable(DbgNode).print(OS);
577 /// print - Print variable.
578 void DIVariable::print(raw_ostream &OS) const {
579 StringRef Res = getName();
581 OS << " [" << Res << "] ";
583 getCompileUnit().print(OS);
584 OS << " [" << getLineNumber() << "] ";
588 // FIXME: Dump complex addresses
591 /// dump - Print descriptor to dbgs() with a newline.
592 void DIDescriptor::dump() const {
593 print(dbgs()); dbgs() << '\n';
596 /// dump - Print compile unit to dbgs() with a newline.
597 void DICompileUnit::dump() const {
598 print(dbgs()); dbgs() << '\n';
601 /// dump - Print type to dbgs() with a newline.
602 void DIType::dump() const {
603 print(dbgs()); dbgs() << '\n';
606 /// dump - Print basic type to dbgs() with a newline.
607 void DIBasicType::dump() const {
608 print(dbgs()); dbgs() << '\n';
611 /// dump - Print derived type to dbgs() with a newline.
612 void DIDerivedType::dump() const {
613 print(dbgs()); dbgs() << '\n';
616 /// dump - Print composite type to dbgs() with a newline.
617 void DICompositeType::dump() const {
618 print(dbgs()); dbgs() << '\n';
621 /// dump - Print subprogram to dbgs() with a newline.
622 void DISubprogram::dump() const {
623 print(dbgs()); dbgs() << '\n';
626 /// dump - Print global variable.
627 void DIGlobalVariable::dump() const {
628 print(dbgs()); dbgs() << '\n';
631 /// dump - Print variable.
632 void DIVariable::dump() const {
633 print(dbgs()); dbgs() << '\n';
636 //===----------------------------------------------------------------------===//
637 // DIFactory: Basic Helpers
638 //===----------------------------------------------------------------------===//
640 DIFactory::DIFactory(Module &m)
641 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
643 Constant *DIFactory::GetTagConstant(unsigned TAG) {
644 assert((TAG & LLVMDebugVersionMask) == 0 &&
645 "Tag too large for debug encoding!");
646 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
649 //===----------------------------------------------------------------------===//
650 // DIFactory: Primary Constructors
651 //===----------------------------------------------------------------------===//
653 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
654 /// This implicitly uniques the arrays created.
655 DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
656 SmallVector<Value*, 16> Elts;
659 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
661 for (unsigned i = 0; i != NumTys; ++i)
662 Elts.push_back(Tys[i]);
664 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
667 /// GetOrCreateSubrange - Create a descriptor for a value range. This
668 /// implicitly uniques the values returned.
669 DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
671 GetTagConstant(dwarf::DW_TAG_subrange_type),
672 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
673 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
676 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
681 /// CreateCompileUnit - Create a new descriptor for the specified compile
682 /// unit. Note that this does not unique compile units within the module.
683 DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
690 unsigned RunTimeVer) {
692 GetTagConstant(dwarf::DW_TAG_compile_unit),
693 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
694 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
695 MDString::get(VMContext, Filename),
696 MDString::get(VMContext, Directory),
697 MDString::get(VMContext, Producer),
698 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
699 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
700 MDString::get(VMContext, Flags),
701 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
704 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
707 /// CreateFile - Create a new descriptor for the specified file.
708 DIFile DIFactory::CreateFile(StringRef Filename,
712 GetTagConstant(dwarf::DW_TAG_file_type),
713 MDString::get(VMContext, Filename),
714 MDString::get(VMContext, Directory),
718 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
721 /// CreateEnumerator - Create a single enumerator value.
722 DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
724 GetTagConstant(dwarf::DW_TAG_enumerator),
725 MDString::get(VMContext, Name),
726 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
728 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
732 /// CreateBasicType - Create a basic type like int, float, etc.
733 DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
738 uint64_t AlignInBits,
739 uint64_t OffsetInBits, unsigned Flags,
742 GetTagConstant(dwarf::DW_TAG_base_type),
744 MDString::get(VMContext, Name),
746 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
747 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
748 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
749 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
750 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
751 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
753 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
757 /// CreateBasicType - Create a basic type like int, float, etc.
758 DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
762 Constant *SizeInBits,
763 Constant *AlignInBits,
764 Constant *OffsetInBits, unsigned Flags,
767 GetTagConstant(dwarf::DW_TAG_base_type),
769 MDString::get(VMContext, Name),
771 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
775 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
776 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
778 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
781 /// CreateArtificialType - Create a new DIType with "artificial" flag set.
782 DIType DIFactory::CreateArtificialType(DIType Ty) {
783 if (Ty.isArtificial())
786 SmallVector<Value *, 9> Elts;
788 assert (N && "Unexpected input DIType!");
789 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
790 if (Value *V = N->getOperand(i))
793 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
796 unsigned CurFlags = Ty.getFlags();
797 CurFlags = CurFlags | DIType::FlagArtificial;
799 // Flags are stored at this slot.
800 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
802 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
805 /// CreateDerivedType - Create a derived type like const qualified type,
806 /// pointer, typedef, etc.
807 DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
808 DIDescriptor Context,
813 uint64_t AlignInBits,
814 uint64_t OffsetInBits,
816 DIType DerivedFrom) {
820 MDString::get(VMContext, Name),
822 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
823 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
824 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
825 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
826 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
829 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
833 /// CreateDerivedType - Create a derived type like const qualified type,
834 /// pointer, typedef, etc.
835 DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
836 DIDescriptor Context,
840 Constant *SizeInBits,
841 Constant *AlignInBits,
842 Constant *OffsetInBits,
844 DIType DerivedFrom) {
848 MDString::get(VMContext, Name),
850 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
854 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
857 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
861 /// CreateCompositeType - Create a composite type like array, struct, etc.
862 DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
863 DIDescriptor Context,
868 uint64_t AlignInBits,
869 uint64_t OffsetInBits,
873 unsigned RuntimeLang,
874 MDNode *ContainingType) {
879 MDString::get(VMContext, Name),
881 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
882 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
883 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
884 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
885 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
888 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
891 return DICompositeType(MDNode::get(VMContext, &Elts[0], 13));
895 /// CreateCompositeType - Create a composite type like array, struct, etc.
896 DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
897 DIDescriptor Context,
901 Constant *SizeInBits,
902 Constant *AlignInBits,
903 Constant *OffsetInBits,
907 unsigned RuntimeLang) {
912 MDString::get(VMContext, Name),
914 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
918 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
921 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
923 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
927 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
928 /// See comments in DISubprogram for descriptions of these fields. This
929 /// method does not unique the generated descriptors.
930 DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
932 StringRef DisplayName,
933 StringRef LinkageName,
935 unsigned LineNo, DIType Ty,
938 unsigned VK, unsigned VIndex,
939 DIType ContainingType,
944 GetTagConstant(dwarf::DW_TAG_subprogram),
945 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
947 MDString::get(VMContext, Name),
948 MDString::get(VMContext, DisplayName),
949 MDString::get(VMContext, LinkageName),
951 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
953 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
954 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
955 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
956 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
958 ConstantInt::get(Type::getInt1Ty(VMContext), isArtificial),
959 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized)
961 return DISubprogram(MDNode::get(VMContext, &Elts[0], 16));
964 /// CreateSubprogramDefinition - Create new subprogram descriptor for the
965 /// given declaration.
966 DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration) {
967 if (SPDeclaration.isDefinition())
968 return DISubprogram(SPDeclaration);
970 MDNode *DeclNode = SPDeclaration;
972 GetTagConstant(dwarf::DW_TAG_subprogram),
973 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
974 DeclNode->getOperand(2), // Context
975 DeclNode->getOperand(3), // Name
976 DeclNode->getOperand(4), // DisplayName
977 DeclNode->getOperand(5), // LinkageName
978 DeclNode->getOperand(6), // CompileUnit
979 DeclNode->getOperand(7), // LineNo
980 DeclNode->getOperand(8), // Type
981 DeclNode->getOperand(9), // isLocalToUnit
982 ConstantInt::get(Type::getInt1Ty(VMContext), true),
983 DeclNode->getOperand(11), // Virtuality
984 DeclNode->getOperand(12), // VIndex
985 DeclNode->getOperand(13), // Containting Type
986 DeclNode->getOperand(14), // isArtificial
987 DeclNode->getOperand(15) // isOptimized
989 return DISubprogram(MDNode::get(VMContext, &Elts[0], 16));
992 /// CreateGlobalVariable - Create a new descriptor for the specified global.
994 DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
995 StringRef DisplayName,
996 StringRef LinkageName,
998 unsigned LineNo, DIType Ty,bool isLocalToUnit,
999 bool isDefinition, llvm::GlobalVariable *Val) {
1001 GetTagConstant(dwarf::DW_TAG_variable),
1002 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1004 MDString::get(VMContext, Name),
1005 MDString::get(VMContext, DisplayName),
1006 MDString::get(VMContext, LinkageName),
1008 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1010 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1011 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1015 Value *const *Vs = &Elts[0];
1016 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1018 // Create a named metadata so that we do not lose this mdnode.
1019 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1020 NMD->addOperand(Node);
1022 return DIGlobalVariable(Node);
1026 /// CreateVariable - Create a new descriptor for the specified variable.
1027 DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
1033 GetTagConstant(Tag),
1035 MDString::get(VMContext, Name),
1037 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1040 return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
1044 /// CreateComplexVariable - Create a new descriptor for the specified variable
1045 /// which has a complex address expression for its address.
1046 DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
1047 const std::string &Name,
1051 SmallVector<Value *, 9> &addr) {
1052 SmallVector<Value *, 9> Elts;
1053 Elts.push_back(GetTagConstant(Tag));
1054 Elts.push_back(Context);
1055 Elts.push_back(MDString::get(VMContext, Name));
1057 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
1059 Elts.insert(Elts.end(), addr.begin(), addr.end());
1061 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
1065 /// CreateBlock - This creates a descriptor for a lexical block with the
1066 /// specified parent VMContext.
1067 DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
1068 unsigned LineNo, unsigned Col) {
1070 GetTagConstant(dwarf::DW_TAG_lexical_block),
1072 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1073 ConstantInt::get(Type::getInt32Ty(VMContext), Col)
1075 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 4));
1078 /// CreateNameSpace - This creates new descriptor for a namespace
1079 /// with the specified parent context.
1080 DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
1084 GetTagConstant(dwarf::DW_TAG_namespace),
1086 MDString::get(VMContext, Name),
1088 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1090 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1093 /// CreateLocation - Creates a debug info location.
1094 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1095 DIScope S, DILocation OrigLoc) {
1097 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1098 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1102 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1105 /// CreateLocation - Creates a debug info location.
1106 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1107 DIScope S, MDNode *OrigLoc) {
1109 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1110 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1114 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1117 //===----------------------------------------------------------------------===//
1118 // DIFactory: Routines for inserting code into a function
1119 //===----------------------------------------------------------------------===//
1121 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1122 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1123 Instruction *InsertBefore) {
1124 assert(Storage && "no storage passed to dbg.declare");
1125 assert(D.Verify() && "empty DIVariable passed to dbg.declare");
1127 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1129 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1131 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
1134 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1135 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1136 BasicBlock *InsertAtEnd) {
1137 assert(Storage && "no storage passed to dbg.declare");
1138 assert(D.Verify() && "invalid DIVariable passed to dbg.declare");
1140 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1142 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1145 // If this block already has a terminator then insert this intrinsic
1146 // before the terminator.
1147 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1148 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1150 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
1152 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1153 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1155 Instruction *InsertBefore) {
1156 assert(V && "no value passed to dbg.value");
1157 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
1159 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1161 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1162 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1164 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1167 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1168 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1170 BasicBlock *InsertAtEnd) {
1171 assert(V && "no value passed to dbg.value");
1172 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
1174 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1176 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1177 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1179 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1182 //===----------------------------------------------------------------------===//
1183 // DebugInfoFinder implementations.
1184 //===----------------------------------------------------------------------===//
1186 /// processModule - Process entire module and collect debug info.
1187 void DebugInfoFinder::processModule(Module &M) {
1188 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1189 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1190 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1192 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1193 processDeclare(DDI);
1195 DebugLoc Loc = BI->getDebugLoc();
1196 if (Loc.isUnknown())
1199 LLVMContext &Ctx = BI->getContext();
1200 DIDescriptor Scope(Loc.getScope(Ctx));
1202 if (Scope.isCompileUnit())
1203 addCompileUnit(DICompileUnit(Scope));
1204 else if (Scope.isSubprogram())
1205 processSubprogram(DISubprogram(Scope));
1206 else if (Scope.isLexicalBlock())
1207 processLexicalBlock(DILexicalBlock(Scope));
1209 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1210 processLocation(DILocation(IA));
1213 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
1217 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1218 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1219 if (addGlobalVariable(DIG)) {
1220 addCompileUnit(DIG.getCompileUnit());
1221 processType(DIG.getType());
1226 /// processLocation - Process DILocation.
1227 void DebugInfoFinder::processLocation(DILocation Loc) {
1228 if (!Loc.Verify()) return;
1229 DIDescriptor S(Loc.getScope());
1230 if (S.isCompileUnit())
1231 addCompileUnit(DICompileUnit(S));
1232 else if (S.isSubprogram())
1233 processSubprogram(DISubprogram(S));
1234 else if (S.isLexicalBlock())
1235 processLexicalBlock(DILexicalBlock(S));
1236 processLocation(Loc.getOrigLocation());
1239 /// processType - Process DIType.
1240 void DebugInfoFinder::processType(DIType DT) {
1244 addCompileUnit(DT.getCompileUnit());
1245 if (DT.isCompositeType()) {
1246 DICompositeType DCT(DT);
1247 processType(DCT.getTypeDerivedFrom());
1248 DIArray DA = DCT.getTypeArray();
1249 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1250 DIDescriptor D = DA.getElement(i);
1252 processType(DIType(D));
1253 else if (D.isSubprogram())
1254 processSubprogram(DISubprogram(D));
1256 } else if (DT.isDerivedType()) {
1257 DIDerivedType DDT(DT);
1258 processType(DDT.getTypeDerivedFrom());
1262 /// processLexicalBlock
1263 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1264 DIScope Context = LB.getContext();
1265 if (Context.isLexicalBlock())
1266 return processLexicalBlock(DILexicalBlock(Context));
1268 return processSubprogram(DISubprogram(Context));
1271 /// processSubprogram - Process DISubprogram.
1272 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1273 if (!addSubprogram(SP))
1275 addCompileUnit(SP.getCompileUnit());
1276 processType(SP.getType());
1279 /// processDeclare - Process DbgDeclareInst.
1280 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
1281 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1285 if (!DV.isVariable())
1288 if (!NodesSeen.insert(DV))
1291 addCompileUnit(DIVariable(N).getCompileUnit());
1292 processType(DIVariable(N).getType());
1295 /// addType - Add type into Tys.
1296 bool DebugInfoFinder::addType(DIType DT) {
1300 if (!NodesSeen.insert(DT))
1307 /// addCompileUnit - Add compile unit into CUs.
1308 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1312 if (!NodesSeen.insert(CU))
1319 /// addGlobalVariable - Add global variable into GVs.
1320 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1321 if (!DIDescriptor(DIG).isGlobalVariable())
1324 if (!NodesSeen.insert(DIG))
1331 // addSubprogram - Add subprgoram into SPs.
1332 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1333 if (!DIDescriptor(SP).isSubprogram())
1336 if (!NodesSeen.insert(SP))
1343 /// Find the debug info descriptor corresponding to this global variable.
1344 static Value *findDbgGlobalDeclare(GlobalVariable *V) {
1345 const Module *M = V->getParent();
1346 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1350 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1351 DIDescriptor DIG(cast_or_null<MDNode>(NMD->getOperand(i)));
1352 if (!DIG.isGlobalVariable())
1354 if (DIGlobalVariable(DIG).getGlobal() == V)
1360 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1361 /// It looks through pointer casts too.
1362 static const DbgDeclareInst *findDbgDeclare(const Value *V) {
1363 V = V->stripPointerCasts();
1365 if (!isa<Instruction>(V) && !isa<Argument>(V))
1368 const Function *F = NULL;
1369 if (const Instruction *I = dyn_cast<Instruction>(V))
1370 F = I->getParent()->getParent();
1371 else if (const Argument *A = dyn_cast<Argument>(V))
1374 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1375 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1377 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1378 if (DDI->getAddress() == V)
1384 bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1385 std::string &Type, unsigned &LineNo,
1386 std::string &File, std::string &Dir) {
1390 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1391 Value *DIGV = findDbgGlobalDeclare(GV);
1392 if (!DIGV) return false;
1393 DIGlobalVariable Var(cast<MDNode>(DIGV));
1395 StringRef D = Var.getDisplayName();
1398 LineNo = Var.getLineNumber();
1399 Unit = Var.getCompileUnit();
1400 TypeD = Var.getType();
1402 const DbgDeclareInst *DDI = findDbgDeclare(V);
1403 if (!DDI) return false;
1404 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1406 StringRef D = Var.getName();
1409 LineNo = Var.getLineNumber();
1410 Unit = Var.getCompileUnit();
1411 TypeD = Var.getType();
1414 StringRef T = TypeD.getName();
1417 StringRef F = Unit.getFilename();
1420 StringRef D = Unit.getDirectory();
1426 /// getDISubprogram - Find subprogram that is enclosing this scope.
1427 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
1428 DIDescriptor D(Scope);
1429 if (D.isSubprogram())
1430 return DISubprogram(Scope);
1432 if (D.isLexicalBlock())
1433 return getDISubprogram(DILexicalBlock(Scope).getContext());
1435 return DISubprogram();
1438 /// getDICompositeType - Find underlying composite type.
1439 DICompositeType llvm::getDICompositeType(DIType T) {
1440 if (T.isCompositeType())
1441 return DICompositeType(T);
1443 if (T.isDerivedType())
1444 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
1446 return DICompositeType();