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,
1031 DIType Ty, bool AlwaysPreserve) {
1033 GetTagConstant(Tag),
1035 MDString::get(VMContext, Name),
1037 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1040 MDNode *Node = MDNode::get(VMContext, &Elts[0], 6);
1041 if (AlwaysPreserve) {
1042 // The optimizer may remove local variable. If there is an interest
1043 // to preserve variable info in such situation then stash it in a
1045 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.lv");
1046 NMD->addOperand(Node);
1048 return DIVariable(Node);
1052 /// CreateComplexVariable - Create a new descriptor for the specified variable
1053 /// which has a complex address expression for its address.
1054 DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
1055 const std::string &Name,
1059 SmallVector<Value *, 9> &addr) {
1060 SmallVector<Value *, 9> Elts;
1061 Elts.push_back(GetTagConstant(Tag));
1062 Elts.push_back(Context);
1063 Elts.push_back(MDString::get(VMContext, Name));
1065 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
1067 Elts.insert(Elts.end(), addr.begin(), addr.end());
1069 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
1073 /// CreateBlock - This creates a descriptor for a lexical block with the
1074 /// specified parent VMContext.
1075 DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
1076 unsigned LineNo, unsigned Col) {
1078 GetTagConstant(dwarf::DW_TAG_lexical_block),
1080 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1081 ConstantInt::get(Type::getInt32Ty(VMContext), Col)
1083 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 4));
1086 /// CreateNameSpace - This creates new descriptor for a namespace
1087 /// with the specified parent context.
1088 DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
1092 GetTagConstant(dwarf::DW_TAG_namespace),
1094 MDString::get(VMContext, Name),
1096 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1098 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1101 /// CreateLocation - Creates a debug info location.
1102 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1103 DIScope S, DILocation OrigLoc) {
1105 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1106 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1110 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1113 /// CreateLocation - Creates a debug info location.
1114 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1115 DIScope S, MDNode *OrigLoc) {
1117 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1118 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1122 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1125 //===----------------------------------------------------------------------===//
1126 // DIFactory: Routines for inserting code into a function
1127 //===----------------------------------------------------------------------===//
1129 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1130 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1131 Instruction *InsertBefore) {
1132 assert(Storage && "no storage passed to dbg.declare");
1133 assert(D.Verify() && "empty DIVariable passed to dbg.declare");
1135 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1137 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1139 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
1142 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1143 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1144 BasicBlock *InsertAtEnd) {
1145 assert(Storage && "no storage passed to dbg.declare");
1146 assert(D.Verify() && "invalid DIVariable passed to dbg.declare");
1148 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1150 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1153 // If this block already has a terminator then insert this intrinsic
1154 // before the terminator.
1155 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1156 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1158 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
1160 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1161 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1163 Instruction *InsertBefore) {
1164 assert(V && "no value passed to dbg.value");
1165 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
1167 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1169 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1170 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1172 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1175 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1176 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1178 BasicBlock *InsertAtEnd) {
1179 assert(V && "no value passed to dbg.value");
1180 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
1182 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1184 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1185 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1187 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1190 //===----------------------------------------------------------------------===//
1191 // DebugInfoFinder implementations.
1192 //===----------------------------------------------------------------------===//
1194 /// processModule - Process entire module and collect debug info.
1195 void DebugInfoFinder::processModule(Module &M) {
1196 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1197 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1198 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1200 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1201 processDeclare(DDI);
1203 DebugLoc Loc = BI->getDebugLoc();
1204 if (Loc.isUnknown())
1207 LLVMContext &Ctx = BI->getContext();
1208 DIDescriptor Scope(Loc.getScope(Ctx));
1210 if (Scope.isCompileUnit())
1211 addCompileUnit(DICompileUnit(Scope));
1212 else if (Scope.isSubprogram())
1213 processSubprogram(DISubprogram(Scope));
1214 else if (Scope.isLexicalBlock())
1215 processLexicalBlock(DILexicalBlock(Scope));
1217 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1218 processLocation(DILocation(IA));
1221 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
1225 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1226 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1227 if (addGlobalVariable(DIG)) {
1228 addCompileUnit(DIG.getCompileUnit());
1229 processType(DIG.getType());
1234 /// processLocation - Process DILocation.
1235 void DebugInfoFinder::processLocation(DILocation Loc) {
1236 if (!Loc.Verify()) return;
1237 DIDescriptor S(Loc.getScope());
1238 if (S.isCompileUnit())
1239 addCompileUnit(DICompileUnit(S));
1240 else if (S.isSubprogram())
1241 processSubprogram(DISubprogram(S));
1242 else if (S.isLexicalBlock())
1243 processLexicalBlock(DILexicalBlock(S));
1244 processLocation(Loc.getOrigLocation());
1247 /// processType - Process DIType.
1248 void DebugInfoFinder::processType(DIType DT) {
1252 addCompileUnit(DT.getCompileUnit());
1253 if (DT.isCompositeType()) {
1254 DICompositeType DCT(DT);
1255 processType(DCT.getTypeDerivedFrom());
1256 DIArray DA = DCT.getTypeArray();
1257 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1258 DIDescriptor D = DA.getElement(i);
1260 processType(DIType(D));
1261 else if (D.isSubprogram())
1262 processSubprogram(DISubprogram(D));
1264 } else if (DT.isDerivedType()) {
1265 DIDerivedType DDT(DT);
1266 processType(DDT.getTypeDerivedFrom());
1270 /// processLexicalBlock
1271 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1272 DIScope Context = LB.getContext();
1273 if (Context.isLexicalBlock())
1274 return processLexicalBlock(DILexicalBlock(Context));
1276 return processSubprogram(DISubprogram(Context));
1279 /// processSubprogram - Process DISubprogram.
1280 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1281 if (!addSubprogram(SP))
1283 addCompileUnit(SP.getCompileUnit());
1284 processType(SP.getType());
1287 /// processDeclare - Process DbgDeclareInst.
1288 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
1289 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1293 if (!DV.isVariable())
1296 if (!NodesSeen.insert(DV))
1299 addCompileUnit(DIVariable(N).getCompileUnit());
1300 processType(DIVariable(N).getType());
1303 /// addType - Add type into Tys.
1304 bool DebugInfoFinder::addType(DIType DT) {
1308 if (!NodesSeen.insert(DT))
1315 /// addCompileUnit - Add compile unit into CUs.
1316 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1320 if (!NodesSeen.insert(CU))
1327 /// addGlobalVariable - Add global variable into GVs.
1328 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1329 if (!DIDescriptor(DIG).isGlobalVariable())
1332 if (!NodesSeen.insert(DIG))
1339 // addSubprogram - Add subprgoram into SPs.
1340 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1341 if (!DIDescriptor(SP).isSubprogram())
1344 if (!NodesSeen.insert(SP))
1351 /// Find the debug info descriptor corresponding to this global variable.
1352 static Value *findDbgGlobalDeclare(GlobalVariable *V) {
1353 const Module *M = V->getParent();
1354 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1358 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1359 DIDescriptor DIG(cast_or_null<MDNode>(NMD->getOperand(i)));
1360 if (!DIG.isGlobalVariable())
1362 if (DIGlobalVariable(DIG).getGlobal() == V)
1368 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1369 /// It looks through pointer casts too.
1370 static const DbgDeclareInst *findDbgDeclare(const Value *V) {
1371 V = V->stripPointerCasts();
1373 if (!isa<Instruction>(V) && !isa<Argument>(V))
1376 const Function *F = NULL;
1377 if (const Instruction *I = dyn_cast<Instruction>(V))
1378 F = I->getParent()->getParent();
1379 else if (const Argument *A = dyn_cast<Argument>(V))
1382 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1383 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1385 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1386 if (DDI->getAddress() == V)
1392 bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1393 std::string &Type, unsigned &LineNo,
1394 std::string &File, std::string &Dir) {
1398 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1399 Value *DIGV = findDbgGlobalDeclare(GV);
1400 if (!DIGV) return false;
1401 DIGlobalVariable Var(cast<MDNode>(DIGV));
1403 StringRef D = Var.getDisplayName();
1406 LineNo = Var.getLineNumber();
1407 Unit = Var.getCompileUnit();
1408 TypeD = Var.getType();
1410 const DbgDeclareInst *DDI = findDbgDeclare(V);
1411 if (!DDI) return false;
1412 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1414 StringRef D = Var.getName();
1417 LineNo = Var.getLineNumber();
1418 Unit = Var.getCompileUnit();
1419 TypeD = Var.getType();
1422 StringRef T = TypeD.getName();
1425 StringRef F = Unit.getFilename();
1428 StringRef D = Unit.getDirectory();
1434 /// getDISubprogram - Find subprogram that is enclosing this scope.
1435 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
1436 DIDescriptor D(Scope);
1437 if (D.isSubprogram())
1438 return DISubprogram(Scope);
1440 if (D.isLexicalBlock())
1441 return getDISubprogram(DILexicalBlock(Scope).getContext());
1443 return DISubprogram();
1446 /// getDICompositeType - Find underlying composite type.
1447 DICompositeType llvm::getDICompositeType(DIType T) {
1448 if (T.isCompositeType())
1449 return DICompositeType(T);
1451 if (T.isDerivedType())
1452 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
1454 return DICompositeType();