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/DebugLoc.h"
28 #include "llvm/Support/raw_ostream.h"
30 using namespace llvm::dwarf;
32 //===----------------------------------------------------------------------===//
34 //===----------------------------------------------------------------------===//
36 /// ValidDebugInfo - Return true if V represents valid debug info value.
37 /// FIXME : Add DIDescriptor.isValid()
38 bool DIDescriptor::ValidDebugInfo(MDNode *N, unsigned OptLevel) {
44 // Check current version. Allow Version7 for now.
45 unsigned Version = DI.getVersion();
46 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion7)
49 switch (DI.getTag()) {
51 assert(DIVariable(N).Verify() && "Invalid DebugInfo value");
53 case DW_TAG_compile_unit:
54 assert(DICompileUnit(N).Verify() && "Invalid DebugInfo value");
56 case DW_TAG_subprogram:
57 assert(DISubprogram(N).Verify() && "Invalid DebugInfo value");
59 case DW_TAG_lexical_block:
60 // FIXME: This interfers with the quality of generated code during
62 if (OptLevel != CodeGenOpt::None)
73 DIDescriptor::getStringField(unsigned Elt) const {
77 if (Elt < DbgNode->getNumOperands())
78 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
79 return MDS->getString();
84 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
88 if (Elt < DbgNode->getNumOperands())
89 if (ConstantInt *CI = dyn_cast<ConstantInt>(DbgNode->getOperand(Elt)))
90 return CI->getZExtValue();
95 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
97 return DIDescriptor();
99 if (Elt < DbgNode->getNumOperands() && DbgNode->getOperand(Elt))
100 return DIDescriptor(dyn_cast<MDNode>(DbgNode->getOperand(Elt)));
102 return DIDescriptor();
105 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
109 if (Elt < DbgNode->getNumOperands())
110 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
114 unsigned DIVariable::getNumAddrElements() const {
115 return DbgNode->getNumOperands()-6;
119 //===----------------------------------------------------------------------===//
121 //===----------------------------------------------------------------------===//
123 /// isBasicType - Return true if the specified tag is legal for
125 bool DIDescriptor::isBasicType() const {
126 return DbgNode && getTag() == dwarf::DW_TAG_base_type;
129 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
130 bool DIDescriptor::isDerivedType() const {
131 if (!DbgNode) return false;
133 case dwarf::DW_TAG_typedef:
134 case dwarf::DW_TAG_pointer_type:
135 case dwarf::DW_TAG_reference_type:
136 case dwarf::DW_TAG_const_type:
137 case dwarf::DW_TAG_volatile_type:
138 case dwarf::DW_TAG_restrict_type:
139 case dwarf::DW_TAG_member:
140 case dwarf::DW_TAG_inheritance:
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;
196 /// isGlobal - Return true if the specified tag is legal for DIGlobal.
197 bool DIDescriptor::isGlobal() const {
198 return isGlobalVariable();
201 /// isScope - Return true if the specified tag is one of the scope
203 bool DIDescriptor::isScope() const {
204 if (!DbgNode) return false;
206 case dwarf::DW_TAG_compile_unit:
207 case dwarf::DW_TAG_lexical_block:
208 case dwarf::DW_TAG_subprogram:
209 case dwarf::DW_TAG_namespace:
217 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
218 bool DIDescriptor::isCompileUnit() const {
219 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
222 /// isFile - Return true if the specified tag is DW_TAG_file_type.
223 bool DIDescriptor::isFile() const {
224 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
227 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
228 bool DIDescriptor::isNameSpace() const {
229 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
232 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
233 bool DIDescriptor::isLexicalBlock() const {
234 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
237 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
238 bool DIDescriptor::isSubrange() const {
239 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
242 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
243 bool DIDescriptor::isEnumerator() const {
244 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
247 //===----------------------------------------------------------------------===//
248 // Simple Descriptor Constructors and other Methods
249 //===----------------------------------------------------------------------===//
251 DIType::DIType(MDNode *N) : DIScope(N) {
253 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
258 unsigned DIArray::getNumElements() const {
261 return DbgNode->getNumOperands();
264 /// replaceAllUsesWith - Replace all uses of debug info referenced by
265 /// this descriptor. After this completes, the current debug info value
267 void DIDerivedType::replaceAllUsesWith(DIDescriptor &D) {
271 // Since we use a TrackingVH for the node, its easy for clients to manufacture
272 // legitimate situations where they want to replaceAllUsesWith() on something
273 // which, due to uniquing, has merged with the source. We shield clients from
274 // this detail by allowing a value to be replaced with replaceAllUsesWith()
276 if (getNode() != D.getNode()) {
277 MDNode *Node = DbgNode;
278 Node->replaceAllUsesWith(D.getNode());
283 /// Verify - Verify that a compile unit is well formed.
284 bool DICompileUnit::Verify() const {
287 StringRef N = getFilename();
290 // It is possible that directory and produce string is empty.
294 /// Verify - Verify that a type descriptor is well formed.
295 bool DIType::Verify() const {
298 if (!getContext().Verify())
301 DICompileUnit CU = getCompileUnit();
307 /// Verify - Verify that a composite type descriptor is well formed.
308 bool DICompositeType::Verify() const {
311 if (!getContext().Verify())
314 DICompileUnit CU = getCompileUnit();
320 /// Verify - Verify that a subprogram descriptor is well formed.
321 bool DISubprogram::Verify() const {
325 if (!getContext().Verify())
328 DICompileUnit CU = getCompileUnit();
332 DICompositeType Ty = getType();
338 /// Verify - Verify that a global variable descriptor is well formed.
339 bool DIGlobalVariable::Verify() const {
343 if (getDisplayName().empty())
346 if (!getContext().Verify())
349 DICompileUnit CU = getCompileUnit();
353 DIType Ty = getType();
363 /// Verify - Verify that a variable descriptor is well formed.
364 bool DIVariable::Verify() const {
368 if (!getContext().Verify())
371 DIType Ty = getType();
378 /// Verify - Verify that a location descriptor is well formed.
379 bool DILocation::Verify() const {
383 return DbgNode->getNumOperands() == 4;
386 /// getOriginalTypeSize - If this type is derived from a base type then
387 /// return base type size.
388 uint64_t DIDerivedType::getOriginalTypeSize() const {
389 unsigned Tag = getTag();
390 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
391 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
392 Tag == dwarf::DW_TAG_restrict_type) {
393 DIType BaseType = getTypeDerivedFrom();
394 // If this type is not derived from any type then take conservative
396 if (!BaseType.isValid())
397 return getSizeInBits();
398 if (BaseType.isDerivedType())
399 return DIDerivedType(BaseType.getNode()).getOriginalTypeSize();
401 return BaseType.getSizeInBits();
404 return getSizeInBits();
407 /// describes - Return true if this subprogram provides debugging
408 /// information for the function F.
409 bool DISubprogram::describes(const Function *F) {
410 assert(F && "Invalid function");
411 StringRef Name = getLinkageName();
414 if (F->getName() == Name)
419 StringRef DIScope::getFilename() const {
422 if (isLexicalBlock())
423 return DILexicalBlock(DbgNode).getFilename();
425 return DISubprogram(DbgNode).getFilename();
427 return DICompileUnit(DbgNode).getFilename();
429 return DINameSpace(DbgNode).getFilename();
431 return DIType(DbgNode).getFilename();
433 return DIFile(DbgNode).getFilename();
434 assert(0 && "Invalid DIScope!");
438 StringRef DIScope::getDirectory() const {
441 if (isLexicalBlock())
442 return DILexicalBlock(DbgNode).getDirectory();
444 return DISubprogram(DbgNode).getDirectory();
446 return DICompileUnit(DbgNode).getDirectory();
448 return DINameSpace(DbgNode).getDirectory();
450 return DIType(DbgNode).getDirectory();
452 return DIFile(DbgNode).getDirectory();
453 assert(0 && "Invalid DIScope!");
457 //===----------------------------------------------------------------------===//
458 // DIDescriptor: dump routines for all descriptors.
459 //===----------------------------------------------------------------------===//
462 /// dump - Print descriptor.
463 void DIDescriptor::dump() const {
464 dbgs() << "[" << dwarf::TagString(getTag()) << "] ";
465 dbgs().write_hex((intptr_t) &*DbgNode) << ']';
468 /// dump - Print compile unit.
469 void DICompileUnit::dump() const {
471 dbgs() << " [" << dwarf::LanguageString(getLanguage()) << "] ";
473 dbgs() << " [" << getDirectory() << "/" << getFilename() << " ]";
476 /// dump - Print type.
477 void DIType::dump() const {
478 if (!DbgNode) return;
480 StringRef Res = getName();
482 dbgs() << " [" << Res << "] ";
484 unsigned Tag = getTag();
485 dbgs() << " [" << dwarf::TagString(Tag) << "] ";
487 // TODO : Print context
488 getCompileUnit().dump();
490 << getLineNumber() << ", "
491 << getSizeInBits() << ", "
492 << getAlignInBits() << ", "
497 dbgs() << " [private] ";
498 else if (isProtected())
499 dbgs() << " [protected] ";
505 DIBasicType(DbgNode).dump();
506 else if (isDerivedType())
507 DIDerivedType(DbgNode).dump();
508 else if (isCompositeType())
509 DICompositeType(DbgNode).dump();
511 dbgs() << "Invalid DIType\n";
518 /// dump - Print basic type.
519 void DIBasicType::dump() const {
520 dbgs() << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
523 /// dump - Print derived type.
524 void DIDerivedType::dump() const {
525 dbgs() << "\n\t Derived From: "; getTypeDerivedFrom().dump();
528 /// dump - Print composite type.
529 void DICompositeType::dump() const {
530 DIArray A = getTypeArray();
531 dbgs() << " [" << A.getNumElements() << " elements]";
534 /// dump - Print global.
535 void DIGlobal::dump() const {
536 StringRef Res = getName();
538 dbgs() << " [" << Res << "] ";
540 unsigned Tag = getTag();
541 dbgs() << " [" << dwarf::TagString(Tag) << "] ";
543 // TODO : Print context
544 getCompileUnit().dump();
545 dbgs() << " [" << getLineNumber() << "] ";
548 dbgs() << " [local] ";
553 if (isGlobalVariable())
554 DIGlobalVariable(DbgNode).dump();
559 /// dump - Print subprogram.
560 void DISubprogram::dump() const {
561 StringRef Res = getName();
563 dbgs() << " [" << Res << "] ";
565 unsigned Tag = getTag();
566 dbgs() << " [" << dwarf::TagString(Tag) << "] ";
568 // TODO : Print context
569 getCompileUnit().dump();
570 dbgs() << " [" << getLineNumber() << "] ";
573 dbgs() << " [local] ";
581 /// dump - Print global variable.
582 void DIGlobalVariable::dump() const {
588 /// dump - Print variable.
589 void DIVariable::dump() const {
590 StringRef Res = getName();
592 dbgs() << " [" << Res << "] ";
594 getCompileUnit().dump();
595 dbgs() << " [" << getLineNumber() << "] ";
599 // FIXME: Dump complex addresses
602 //===----------------------------------------------------------------------===//
603 // DIFactory: Basic Helpers
604 //===----------------------------------------------------------------------===//
606 DIFactory::DIFactory(Module &m)
607 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
609 Constant *DIFactory::GetTagConstant(unsigned TAG) {
610 assert((TAG & LLVMDebugVersionMask) == 0 &&
611 "Tag too large for debug encoding!");
612 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
615 //===----------------------------------------------------------------------===//
616 // DIFactory: Primary Constructors
617 //===----------------------------------------------------------------------===//
619 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
620 /// This implicitly uniques the arrays created.
621 DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
622 SmallVector<Value*, 16> Elts;
625 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
627 for (unsigned i = 0; i != NumTys; ++i)
628 Elts.push_back(Tys[i].getNode());
630 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
633 /// GetOrCreateSubrange - Create a descriptor for a value range. This
634 /// implicitly uniques the values returned.
635 DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
637 GetTagConstant(dwarf::DW_TAG_subrange_type),
638 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
639 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
642 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
647 /// CreateCompileUnit - Create a new descriptor for the specified compile
648 /// unit. Note that this does not unique compile units within the module.
649 DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
656 unsigned RunTimeVer) {
658 GetTagConstant(dwarf::DW_TAG_compile_unit),
659 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
660 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
661 MDString::get(VMContext, Filename),
662 MDString::get(VMContext, Directory),
663 MDString::get(VMContext, Producer),
664 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
665 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
666 MDString::get(VMContext, Flags),
667 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
670 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
673 /// CreateFile - Create a new descriptor for the specified file.
674 DIFile DIFactory::CreateFile(StringRef Filename,
678 GetTagConstant(dwarf::DW_TAG_file_type),
679 MDString::get(VMContext, Filename),
680 MDString::get(VMContext, Directory),
684 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
687 /// CreateEnumerator - Create a single enumerator value.
688 DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
690 GetTagConstant(dwarf::DW_TAG_enumerator),
691 MDString::get(VMContext, Name),
692 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
694 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
698 /// CreateBasicType - Create a basic type like int, float, etc.
699 DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
704 uint64_t AlignInBits,
705 uint64_t OffsetInBits, unsigned Flags,
708 GetTagConstant(dwarf::DW_TAG_base_type),
710 MDString::get(VMContext, Name),
712 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
713 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
714 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
715 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
716 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
717 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
719 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
723 /// CreateBasicType - Create a basic type like int, float, etc.
724 DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
728 Constant *SizeInBits,
729 Constant *AlignInBits,
730 Constant *OffsetInBits, unsigned Flags,
733 GetTagConstant(dwarf::DW_TAG_base_type),
735 MDString::get(VMContext, Name),
737 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
741 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
742 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
744 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
747 /// CreateArtificialType - Create a new DIType with "artificial" flag set.
748 DIType DIFactory::CreateArtificialType(DIType Ty) {
749 if (Ty.isArtificial())
752 SmallVector<Value *, 9> Elts;
753 MDNode *N = Ty.getNode();
754 assert (N && "Unexpected input DIType!");
755 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
756 if (Value *V = N->getOperand(i))
759 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
762 unsigned CurFlags = Ty.getFlags();
763 CurFlags = CurFlags | DIType::FlagArtificial;
765 // Flags are stored at this slot.
766 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
768 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
771 /// CreateDerivedType - Create a derived type like const qualified type,
772 /// pointer, typedef, etc.
773 DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
774 DIDescriptor Context,
779 uint64_t AlignInBits,
780 uint64_t OffsetInBits,
782 DIType DerivedFrom) {
786 MDString::get(VMContext, Name),
788 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
789 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
790 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
791 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
792 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
793 DerivedFrom.getNode(),
795 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
799 /// CreateDerivedType - Create a derived type like const qualified type,
800 /// pointer, typedef, etc.
801 DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
802 DIDescriptor Context,
806 Constant *SizeInBits,
807 Constant *AlignInBits,
808 Constant *OffsetInBits,
810 DIType DerivedFrom) {
814 MDString::get(VMContext, Name),
816 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
820 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
821 DerivedFrom.getNode(),
823 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
827 /// CreateCompositeType - Create a composite type like array, struct, etc.
828 DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
829 DIDescriptor Context,
834 uint64_t AlignInBits,
835 uint64_t OffsetInBits,
839 unsigned RuntimeLang,
840 MDNode *ContainingType) {
845 MDString::get(VMContext, Name),
847 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
848 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
849 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
850 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
851 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
852 DerivedFrom.getNode(),
854 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
857 return DICompositeType(MDNode::get(VMContext, &Elts[0], 13));
861 /// CreateCompositeType - Create a composite type like array, struct, etc.
862 DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
863 DIDescriptor Context,
867 Constant *SizeInBits,
868 Constant *AlignInBits,
869 Constant *OffsetInBits,
873 unsigned RuntimeLang) {
878 MDString::get(VMContext, Name),
880 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
884 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
885 DerivedFrom.getNode(),
887 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
889 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
893 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
894 /// See comments in DISubprogram for descriptions of these fields. This
895 /// method does not unique the generated descriptors.
896 DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
898 StringRef DisplayName,
899 StringRef LinkageName,
901 unsigned LineNo, DIType Ty,
904 unsigned VK, unsigned VIndex,
905 DIType ContainingType,
909 GetTagConstant(dwarf::DW_TAG_subprogram),
910 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
912 MDString::get(VMContext, Name),
913 MDString::get(VMContext, DisplayName),
914 MDString::get(VMContext, LinkageName),
916 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
918 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
919 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
920 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
921 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
922 ContainingType.getNode(),
923 ConstantInt::get(Type::getInt1Ty(VMContext), isArtificial)
925 return DISubprogram(MDNode::get(VMContext, &Elts[0], 15));
928 /// CreateSubprogramDefinition - Create new subprogram descriptor for the
929 /// given declaration.
930 DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration) {
931 if (SPDeclaration.isDefinition())
932 return DISubprogram(SPDeclaration.getNode());
934 MDNode *DeclNode = SPDeclaration.getNode();
936 GetTagConstant(dwarf::DW_TAG_subprogram),
937 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
938 DeclNode->getOperand(2), // Context
939 DeclNode->getOperand(3), // Name
940 DeclNode->getOperand(4), // DisplayName
941 DeclNode->getOperand(5), // LinkageName
942 DeclNode->getOperand(6), // CompileUnit
943 DeclNode->getOperand(7), // LineNo
944 DeclNode->getOperand(8), // Type
945 DeclNode->getOperand(9), // isLocalToUnit
946 ConstantInt::get(Type::getInt1Ty(VMContext), true),
947 DeclNode->getOperand(11), // Virtuality
948 DeclNode->getOperand(12), // VIndex
949 DeclNode->getOperand(13), // Containting Type
950 DeclNode->getOperand(14) // isArtificial
952 return DISubprogram(MDNode::get(VMContext, &Elts[0], 15));
955 /// CreateGlobalVariable - Create a new descriptor for the specified global.
957 DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
958 StringRef DisplayName,
959 StringRef LinkageName,
961 unsigned LineNo, DIType Ty,bool isLocalToUnit,
962 bool isDefinition, llvm::GlobalVariable *Val) {
964 GetTagConstant(dwarf::DW_TAG_variable),
965 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
967 MDString::get(VMContext, Name),
968 MDString::get(VMContext, DisplayName),
969 MDString::get(VMContext, LinkageName),
971 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
973 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
974 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
978 Value *const *Vs = &Elts[0];
979 MDNode *Node = MDNode::get(VMContext,Vs, 12);
981 // Create a named metadata so that we do not lose this mdnode.
982 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
983 NMD->addOperand(Node);
985 return DIGlobalVariable(Node);
989 /// CreateVariable - Create a new descriptor for the specified variable.
990 DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
998 MDString::get(VMContext, Name),
1000 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1003 return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
1007 /// CreateComplexVariable - Create a new descriptor for the specified variable
1008 /// which has a complex address expression for its address.
1009 DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
1010 const std::string &Name,
1014 SmallVector<Value *, 9> &addr) {
1015 SmallVector<Value *, 9> Elts;
1016 Elts.push_back(GetTagConstant(Tag));
1017 Elts.push_back(Context.getNode());
1018 Elts.push_back(MDString::get(VMContext, Name));
1019 Elts.push_back(F.getNode());
1020 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
1021 Elts.push_back(Ty.getNode());
1022 Elts.insert(Elts.end(), addr.begin(), addr.end());
1024 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
1028 /// CreateBlock - This creates a descriptor for a lexical block with the
1029 /// specified parent VMContext.
1030 DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
1031 unsigned LineNo, unsigned Col) {
1033 GetTagConstant(dwarf::DW_TAG_lexical_block),
1035 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1036 ConstantInt::get(Type::getInt32Ty(VMContext), Col)
1038 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 4));
1041 /// CreateNameSpace - This creates new descriptor for a namespace
1042 /// with the specified parent context.
1043 DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
1047 GetTagConstant(dwarf::DW_TAG_namespace),
1049 MDString::get(VMContext, Name),
1051 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1053 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1056 /// CreateLocation - Creates a debug info location.
1057 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1058 DIScope S, DILocation OrigLoc) {
1060 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1061 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1065 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1068 /// CreateLocation - Creates a debug info location.
1069 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1070 DIScope S, MDNode *OrigLoc) {
1072 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1073 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1077 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1080 //===----------------------------------------------------------------------===//
1081 // DIFactory: Routines for inserting code into a function
1082 //===----------------------------------------------------------------------===//
1084 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1085 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1086 Instruction *InsertBefore) {
1087 assert(Storage && "no storage passed to dbg.declare");
1088 assert(D.getNode() && "empty DIVariable passed to dbg.declare");
1090 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1092 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1094 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
1097 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1098 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1099 BasicBlock *InsertAtEnd) {
1100 assert(Storage && "no storage passed to dbg.declare");
1101 assert(D.getNode() && "empty DIVariable passed to dbg.declare");
1103 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1105 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1108 // If this block already has a terminator then insert this intrinsic
1109 // before the terminator.
1110 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1111 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1113 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
1115 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1116 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1118 Instruction *InsertBefore) {
1119 assert(V && "no value passed to dbg.value");
1120 assert(D.getNode() && "empty DIVariable passed to dbg.value");
1122 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1124 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1125 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1127 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1130 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1131 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1133 BasicBlock *InsertAtEnd) {
1134 assert(V && "no value passed to dbg.value");
1135 assert(D.getNode() && "empty DIVariable passed to dbg.value");
1137 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1139 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1140 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1142 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1145 //===----------------------------------------------------------------------===//
1146 // DebugInfoFinder implementations.
1147 //===----------------------------------------------------------------------===//
1149 /// processModule - Process entire module and collect debug info.
1150 void DebugInfoFinder::processModule(Module &M) {
1151 unsigned MDDbgKind = M.getMDKindID("dbg");
1153 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1154 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1155 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1157 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1158 processDeclare(DDI);
1159 else if (MDNode *L = BI->getMetadata(MDDbgKind))
1160 processLocation(DILocation(L));
1163 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
1167 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1168 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1169 if (addGlobalVariable(DIG)) {
1170 addCompileUnit(DIG.getCompileUnit());
1171 processType(DIG.getType());
1176 /// processLocation - Process DILocation.
1177 void DebugInfoFinder::processLocation(DILocation Loc) {
1178 if (!Loc.Verify()) return;
1179 DIDescriptor S(Loc.getScope().getNode());
1180 if (S.isCompileUnit())
1181 addCompileUnit(DICompileUnit(S.getNode()));
1182 else if (S.isSubprogram())
1183 processSubprogram(DISubprogram(S.getNode()));
1184 else if (S.isLexicalBlock())
1185 processLexicalBlock(DILexicalBlock(S.getNode()));
1186 processLocation(Loc.getOrigLocation());
1189 /// processType - Process DIType.
1190 void DebugInfoFinder::processType(DIType DT) {
1194 addCompileUnit(DT.getCompileUnit());
1195 if (DT.isCompositeType()) {
1196 DICompositeType DCT(DT.getNode());
1197 processType(DCT.getTypeDerivedFrom());
1198 DIArray DA = DCT.getTypeArray();
1199 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1200 DIDescriptor D = DA.getElement(i);
1202 processType(DIType(D.getNode()));
1203 else if (D.isSubprogram())
1204 processSubprogram(DISubprogram(D.getNode()));
1206 } else if (DT.isDerivedType()) {
1207 DIDerivedType DDT(DT.getNode());
1208 processType(DDT.getTypeDerivedFrom());
1212 /// processLexicalBlock
1213 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1214 DIScope Context = LB.getContext();
1215 if (Context.isLexicalBlock())
1216 return processLexicalBlock(DILexicalBlock(Context.getNode()));
1218 return processSubprogram(DISubprogram(Context.getNode()));
1221 /// processSubprogram - Process DISubprogram.
1222 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1223 if (!addSubprogram(SP))
1225 addCompileUnit(SP.getCompileUnit());
1226 processType(SP.getType());
1229 /// processDeclare - Process DbgDeclareInst.
1230 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
1231 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1235 if (!DV.isVariable())
1238 if (!NodesSeen.insert(DV.getNode()))
1241 addCompileUnit(DIVariable(N).getCompileUnit());
1242 processType(DIVariable(N).getType());
1245 /// addType - Add type into Tys.
1246 bool DebugInfoFinder::addType(DIType DT) {
1250 if (!NodesSeen.insert(DT.getNode()))
1253 TYs.push_back(DT.getNode());
1257 /// addCompileUnit - Add compile unit into CUs.
1258 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1262 if (!NodesSeen.insert(CU.getNode()))
1265 CUs.push_back(CU.getNode());
1269 /// addGlobalVariable - Add global variable into GVs.
1270 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1271 if (!DIDescriptor(DIG.getNode()).isGlobalVariable())
1274 if (!NodesSeen.insert(DIG.getNode()))
1277 GVs.push_back(DIG.getNode());
1281 // addSubprogram - Add subprgoram into SPs.
1282 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1283 if (!DIDescriptor(SP.getNode()).isSubprogram())
1286 if (!NodesSeen.insert(SP.getNode()))
1289 SPs.push_back(SP.getNode());
1293 /// Find the debug info descriptor corresponding to this global variable.
1294 static Value *findDbgGlobalDeclare(GlobalVariable *V) {
1295 const Module *M = V->getParent();
1296 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1300 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1301 DIDescriptor DIG(cast_or_null<MDNode>(NMD->getOperand(i)));
1302 if (!DIG.isGlobalVariable())
1304 if (DIGlobalVariable(DIG.getNode()).getGlobal() == V)
1305 return DIG.getNode();
1310 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1311 /// It looks through pointer casts too.
1312 static const DbgDeclareInst *findDbgDeclare(const Value *V) {
1313 V = V->stripPointerCasts();
1315 if (!isa<Instruction>(V) && !isa<Argument>(V))
1318 const Function *F = NULL;
1319 if (const Instruction *I = dyn_cast<Instruction>(V))
1320 F = I->getParent()->getParent();
1321 else if (const Argument *A = dyn_cast<Argument>(V))
1324 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1325 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1327 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1328 if (DDI->getAddress() == V)
1334 bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1335 std::string &Type, unsigned &LineNo,
1336 std::string &File, std::string &Dir) {
1340 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1341 Value *DIGV = findDbgGlobalDeclare(GV);
1342 if (!DIGV) return false;
1343 DIGlobalVariable Var(cast<MDNode>(DIGV));
1345 StringRef D = Var.getDisplayName();
1348 LineNo = Var.getLineNumber();
1349 Unit = Var.getCompileUnit();
1350 TypeD = Var.getType();
1352 const DbgDeclareInst *DDI = findDbgDeclare(V);
1353 if (!DDI) return false;
1354 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1356 StringRef D = Var.getName();
1359 LineNo = Var.getLineNumber();
1360 Unit = Var.getCompileUnit();
1361 TypeD = Var.getType();
1364 StringRef T = TypeD.getName();
1367 StringRef F = Unit.getFilename();
1370 StringRef D = Unit.getDirectory();
1376 /// ExtractDebugLocation - Extract debug location information
1377 /// from DILocation.
1378 DebugLoc llvm::ExtractDebugLocation(DILocation &Loc,
1379 DebugLocTracker &DebugLocInfo) {
1380 DenseMap<MDNode *, unsigned>::iterator II
1381 = DebugLocInfo.DebugIdMap.find(Loc.getNode());
1382 if (II != DebugLocInfo.DebugIdMap.end())
1383 return DebugLoc::get(II->second);
1385 // Add a new location entry.
1386 unsigned Id = DebugLocInfo.DebugLocations.size();
1387 DebugLocInfo.DebugLocations.push_back(Loc.getNode());
1388 DebugLocInfo.DebugIdMap[Loc.getNode()] = Id;
1390 return DebugLoc::get(Id);
1393 /// getDISubprogram - Find subprogram that is enclosing this scope.
1394 DISubprogram llvm::getDISubprogram(MDNode *Scope) {
1395 DIDescriptor D(Scope);
1396 if (D.isSubprogram())
1397 return DISubprogram(Scope);
1399 if (D.isLexicalBlock())
1400 return getDISubprogram(DILexicalBlock(Scope).getContext().getNode());
1402 return DISubprogram();
1405 /// getDICompositeType - Find underlying composite type.
1406 DICompositeType llvm::getDICompositeType(DIType T) {
1407 if (T.isCompositeType())
1408 return DICompositeType(T.getNode());
1410 if (T.isDerivedType())
1411 return getDICompositeType(DIDerivedType(T.getNode()).getTypeDerivedFrom());
1413 return DICompositeType();