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 Version6 for now.
45 unsigned Version = DI.getVersion();
46 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6)
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,
701 DICompileUnit CompileUnit,
704 uint64_t AlignInBits,
705 uint64_t OffsetInBits, unsigned Flags,
708 GetTagConstant(dwarf::DW_TAG_base_type),
710 MDString::get(VMContext, Name),
711 CompileUnit.getNode(),
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,
726 DICompileUnit CompileUnit,
728 Constant *SizeInBits,
729 Constant *AlignInBits,
730 Constant *OffsetInBits, unsigned Flags,
733 GetTagConstant(dwarf::DW_TAG_base_type),
735 MDString::get(VMContext, Name),
736 CompileUnit.getNode(),
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,
776 DICompileUnit CompileUnit,
779 uint64_t AlignInBits,
780 uint64_t OffsetInBits,
782 DIType DerivedFrom) {
786 MDString::get(VMContext, Name),
787 CompileUnit.getNode(),
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,
804 DICompileUnit CompileUnit,
806 Constant *SizeInBits,
807 Constant *AlignInBits,
808 Constant *OffsetInBits,
810 DIType DerivedFrom) {
814 MDString::get(VMContext, Name),
815 CompileUnit.getNode(),
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,
831 DICompileUnit CompileUnit,
834 uint64_t AlignInBits,
835 uint64_t OffsetInBits,
839 unsigned RuntimeLang,
840 MDNode *ContainingType) {
845 MDString::get(VMContext, Name),
846 CompileUnit.getNode(),
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,
865 DICompileUnit CompileUnit,
867 Constant *SizeInBits,
868 Constant *AlignInBits,
869 Constant *OffsetInBits,
873 unsigned RuntimeLang) {
878 MDString::get(VMContext, Name),
879 CompileUnit.getNode(),
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,
900 DICompileUnit CompileUnit,
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),
915 CompileUnit.getNode(),
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,
960 DICompileUnit CompileUnit,
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),
970 CompileUnit.getNode(),
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,
992 DICompileUnit CompileUnit, unsigned LineNo,
997 MDString::get(VMContext, Name),
998 CompileUnit.getNode(),
999 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1002 return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
1006 /// CreateComplexVariable - Create a new descriptor for the specified variable
1007 /// which has a complex address expression for its address.
1008 DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
1009 const std::string &Name,
1010 DICompileUnit CompileUnit,
1013 SmallVector<Value *, 9> &addr) {
1014 SmallVector<Value *, 9> Elts;
1015 Elts.push_back(GetTagConstant(Tag));
1016 Elts.push_back(Context.getNode());
1017 Elts.push_back(MDString::get(VMContext, Name));
1018 Elts.push_back(CompileUnit.getNode());
1019 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
1020 Elts.push_back(Ty.getNode());
1021 Elts.insert(Elts.end(), addr.begin(), addr.end());
1023 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
1027 /// CreateBlock - This creates a descriptor for a lexical block with the
1028 /// specified parent VMContext.
1029 DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
1030 unsigned LineNo, unsigned Col) {
1032 GetTagConstant(dwarf::DW_TAG_lexical_block),
1034 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1035 ConstantInt::get(Type::getInt32Ty(VMContext), Col)
1037 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 4));
1040 /// CreateNameSpace - This creates new descriptor for a namespace
1041 /// with the specified parent context.
1042 DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
1043 DICompileUnit CompileUnit,
1046 GetTagConstant(dwarf::DW_TAG_namespace),
1048 MDString::get(VMContext, Name),
1049 CompileUnit.getNode(),
1050 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1052 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1055 /// CreateLocation - Creates a debug info location.
1056 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1057 DIScope S, DILocation OrigLoc) {
1059 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1060 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1064 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1067 /// CreateLocation - Creates a debug info location.
1068 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1069 DIScope S, MDNode *OrigLoc) {
1071 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1072 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1076 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1079 //===----------------------------------------------------------------------===//
1080 // DIFactory: Routines for inserting code into a function
1081 //===----------------------------------------------------------------------===//
1083 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1084 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1085 Instruction *InsertBefore) {
1086 assert(Storage && "no storage passed to dbg.declare");
1087 assert(D.getNode() && "empty DIVariable passed to dbg.declare");
1089 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1091 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1093 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
1096 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1097 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1098 BasicBlock *InsertAtEnd) {
1099 assert(Storage && "no storage passed to dbg.declare");
1100 assert(D.getNode() && "empty DIVariable passed to dbg.declare");
1102 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1104 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1107 // If this block already has a terminator then insert this intrinsic
1108 // before the terminator.
1109 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1110 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1112 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
1114 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1115 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1117 Instruction *InsertBefore) {
1118 assert(V && "no value passed to dbg.value");
1119 assert(D.getNode() && "empty DIVariable passed to dbg.value");
1121 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1123 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1124 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1126 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1129 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1130 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1132 BasicBlock *InsertAtEnd) {
1133 assert(V && "no value passed to dbg.value");
1134 assert(D.getNode() && "empty DIVariable passed to dbg.value");
1136 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1138 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1139 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1141 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1144 //===----------------------------------------------------------------------===//
1145 // DebugInfoFinder implementations.
1146 //===----------------------------------------------------------------------===//
1148 /// processModule - Process entire module and collect debug info.
1149 void DebugInfoFinder::processModule(Module &M) {
1150 unsigned MDDbgKind = M.getMDKindID("dbg");
1152 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1153 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1154 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1156 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1157 processDeclare(DDI);
1158 else if (MDNode *L = BI->getMetadata(MDDbgKind))
1159 processLocation(DILocation(L));
1162 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
1166 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1167 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1168 if (addGlobalVariable(DIG)) {
1169 addCompileUnit(DIG.getCompileUnit());
1170 processType(DIG.getType());
1175 /// processLocation - Process DILocation.
1176 void DebugInfoFinder::processLocation(DILocation Loc) {
1177 if (!Loc.Verify()) return;
1178 DIDescriptor S(Loc.getScope().getNode());
1179 if (S.isCompileUnit())
1180 addCompileUnit(DICompileUnit(S.getNode()));
1181 else if (S.isSubprogram())
1182 processSubprogram(DISubprogram(S.getNode()));
1183 else if (S.isLexicalBlock())
1184 processLexicalBlock(DILexicalBlock(S.getNode()));
1185 processLocation(Loc.getOrigLocation());
1188 /// processType - Process DIType.
1189 void DebugInfoFinder::processType(DIType DT) {
1193 addCompileUnit(DT.getCompileUnit());
1194 if (DT.isCompositeType()) {
1195 DICompositeType DCT(DT.getNode());
1196 processType(DCT.getTypeDerivedFrom());
1197 DIArray DA = DCT.getTypeArray();
1198 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1199 DIDescriptor D = DA.getElement(i);
1201 processType(DIType(D.getNode()));
1202 else if (D.isSubprogram())
1203 processSubprogram(DISubprogram(D.getNode()));
1205 } else if (DT.isDerivedType()) {
1206 DIDerivedType DDT(DT.getNode());
1207 processType(DDT.getTypeDerivedFrom());
1211 /// processLexicalBlock
1212 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1213 DIScope Context = LB.getContext();
1214 if (Context.isLexicalBlock())
1215 return processLexicalBlock(DILexicalBlock(Context.getNode()));
1217 return processSubprogram(DISubprogram(Context.getNode()));
1220 /// processSubprogram - Process DISubprogram.
1221 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1222 if (!addSubprogram(SP))
1224 addCompileUnit(SP.getCompileUnit());
1225 processType(SP.getType());
1228 /// processDeclare - Process DbgDeclareInst.
1229 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
1230 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1234 if (!DV.isVariable())
1237 if (!NodesSeen.insert(DV.getNode()))
1240 addCompileUnit(DIVariable(N).getCompileUnit());
1241 processType(DIVariable(N).getType());
1244 /// addType - Add type into Tys.
1245 bool DebugInfoFinder::addType(DIType DT) {
1249 if (!NodesSeen.insert(DT.getNode()))
1252 TYs.push_back(DT.getNode());
1256 /// addCompileUnit - Add compile unit into CUs.
1257 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1261 if (!NodesSeen.insert(CU.getNode()))
1264 CUs.push_back(CU.getNode());
1268 /// addGlobalVariable - Add global variable into GVs.
1269 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1270 if (!DIDescriptor(DIG.getNode()).isGlobalVariable())
1273 if (!NodesSeen.insert(DIG.getNode()))
1276 GVs.push_back(DIG.getNode());
1280 // addSubprogram - Add subprgoram into SPs.
1281 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1282 if (!DIDescriptor(SP.getNode()).isSubprogram())
1285 if (!NodesSeen.insert(SP.getNode()))
1288 SPs.push_back(SP.getNode());
1292 /// Find the debug info descriptor corresponding to this global variable.
1293 static Value *findDbgGlobalDeclare(GlobalVariable *V) {
1294 const Module *M = V->getParent();
1295 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1299 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1300 DIDescriptor DIG(cast_or_null<MDNode>(NMD->getOperand(i)));
1301 if (!DIG.isGlobalVariable())
1303 if (DIGlobalVariable(DIG.getNode()).getGlobal() == V)
1304 return DIG.getNode();
1309 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1310 /// It looks through pointer casts too.
1311 static const DbgDeclareInst *findDbgDeclare(const Value *V) {
1312 V = V->stripPointerCasts();
1314 if (!isa<Instruction>(V) && !isa<Argument>(V))
1317 const Function *F = NULL;
1318 if (const Instruction *I = dyn_cast<Instruction>(V))
1319 F = I->getParent()->getParent();
1320 else if (const Argument *A = dyn_cast<Argument>(V))
1323 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1324 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1326 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1327 if (DDI->getAddress() == V)
1333 bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1334 std::string &Type, unsigned &LineNo,
1335 std::string &File, std::string &Dir) {
1339 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1340 Value *DIGV = findDbgGlobalDeclare(GV);
1341 if (!DIGV) return false;
1342 DIGlobalVariable Var(cast<MDNode>(DIGV));
1344 StringRef D = Var.getDisplayName();
1347 LineNo = Var.getLineNumber();
1348 Unit = Var.getCompileUnit();
1349 TypeD = Var.getType();
1351 const DbgDeclareInst *DDI = findDbgDeclare(V);
1352 if (!DDI) return false;
1353 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1355 StringRef D = Var.getName();
1358 LineNo = Var.getLineNumber();
1359 Unit = Var.getCompileUnit();
1360 TypeD = Var.getType();
1363 StringRef T = TypeD.getName();
1366 StringRef F = Unit.getFilename();
1369 StringRef D = Unit.getDirectory();
1375 /// ExtractDebugLocation - Extract debug location information
1376 /// from DILocation.
1377 DebugLoc llvm::ExtractDebugLocation(DILocation &Loc,
1378 DebugLocTracker &DebugLocInfo) {
1379 DenseMap<MDNode *, unsigned>::iterator II
1380 = DebugLocInfo.DebugIdMap.find(Loc.getNode());
1381 if (II != DebugLocInfo.DebugIdMap.end())
1382 return DebugLoc::get(II->second);
1384 // Add a new location entry.
1385 unsigned Id = DebugLocInfo.DebugLocations.size();
1386 DebugLocInfo.DebugLocations.push_back(Loc.getNode());
1387 DebugLocInfo.DebugIdMap[Loc.getNode()] = Id;
1389 return DebugLoc::get(Id);
1392 /// getDISubprogram - Find subprogram that is enclosing this scope.
1393 DISubprogram llvm::getDISubprogram(MDNode *Scope) {
1394 DIDescriptor D(Scope);
1395 if (D.isSubprogram())
1396 return DISubprogram(Scope);
1398 if (D.isLexicalBlock())
1399 return getDISubprogram(DILexicalBlock(Scope).getContext().getNode());
1401 return DISubprogram();
1404 /// getDICompositeType - Find underlying composite type.
1405 DICompositeType llvm::getDICompositeType(DIType T) {
1406 if (T.isCompositeType())
1407 return DICompositeType(T.getNode());
1409 if (T.isDerivedType())
1410 return getDICompositeType(DIDerivedType(T.getNode()).getTypeDerivedFrom());
1412 return DICompositeType();