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 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
223 bool DIDescriptor::isNameSpace() const {
224 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
227 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
228 bool DIDescriptor::isLexicalBlock() const {
229 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
232 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
233 bool DIDescriptor::isSubrange() const {
234 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
237 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
238 bool DIDescriptor::isEnumerator() const {
239 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
242 //===----------------------------------------------------------------------===//
243 // Simple Descriptor Constructors and other Methods
244 //===----------------------------------------------------------------------===//
246 DIType::DIType(MDNode *N) : DIDescriptor(N) {
248 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
253 unsigned DIArray::getNumElements() const {
256 return DbgNode->getNumOperands();
259 /// replaceAllUsesWith - Replace all uses of debug info referenced by
260 /// this descriptor. After this completes, the current debug info value
262 void DIDerivedType::replaceAllUsesWith(DIDescriptor &D) {
266 // Since we use a TrackingVH for the node, its easy for clients to manufacture
267 // legitimate situations where they want to replaceAllUsesWith() on something
268 // which, due to uniquing, has merged with the source. We shield clients from
269 // this detail by allowing a value to be replaced with replaceAllUsesWith()
271 if (getNode() != D.getNode()) {
272 MDNode *Node = DbgNode;
273 Node->replaceAllUsesWith(D.getNode());
278 /// Verify - Verify that a compile unit is well formed.
279 bool DICompileUnit::Verify() const {
282 StringRef N = getFilename();
285 // It is possible that directory and produce string is empty.
289 /// Verify - Verify that a type descriptor is well formed.
290 bool DIType::Verify() const {
293 if (!getContext().Verify())
296 DICompileUnit CU = getCompileUnit();
302 /// Verify - Verify that a composite type descriptor is well formed.
303 bool DICompositeType::Verify() const {
306 if (!getContext().Verify())
309 DICompileUnit CU = getCompileUnit();
315 /// Verify - Verify that a subprogram descriptor is well formed.
316 bool DISubprogram::Verify() const {
320 if (!getContext().Verify())
323 DICompileUnit CU = getCompileUnit();
327 DICompositeType Ty = getType();
333 /// Verify - Verify that a global variable descriptor is well formed.
334 bool DIGlobalVariable::Verify() const {
338 if (getDisplayName().empty())
341 if (!getContext().Verify())
344 DICompileUnit CU = getCompileUnit();
348 DIType Ty = getType();
358 /// Verify - Verify that a variable descriptor is well formed.
359 bool DIVariable::Verify() const {
363 if (!getContext().Verify())
366 DIType Ty = getType();
373 /// Verify - Verify that a location descriptor is well formed.
374 bool DILocation::Verify() const {
378 return DbgNode->getNumOperands() == 4;
381 /// getOriginalTypeSize - If this type is derived from a base type then
382 /// return base type size.
383 uint64_t DIDerivedType::getOriginalTypeSize() const {
384 unsigned Tag = getTag();
385 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
386 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
387 Tag == dwarf::DW_TAG_restrict_type) {
388 DIType BaseType = getTypeDerivedFrom();
389 // If this type is not derived from any type then take conservative
391 if (!BaseType.isValid())
392 return getSizeInBits();
393 if (BaseType.isDerivedType())
394 return DIDerivedType(BaseType.getNode()).getOriginalTypeSize();
396 return BaseType.getSizeInBits();
399 return getSizeInBits();
402 /// describes - Return true if this subprogram provides debugging
403 /// information for the function F.
404 bool DISubprogram::describes(const Function *F) {
405 assert(F && "Invalid function");
406 StringRef Name = getLinkageName();
409 if (F->getName() == Name)
414 StringRef DIScope::getFilename() const {
415 if (isLexicalBlock())
416 return DILexicalBlock(DbgNode).getFilename();
418 return DISubprogram(DbgNode).getFilename();
420 return DICompileUnit(DbgNode).getFilename();
422 return DINameSpace(DbgNode).getFilename();
423 assert(0 && "Invalid DIScope!");
427 StringRef DIScope::getDirectory() const {
428 if (isLexicalBlock())
429 return DILexicalBlock(DbgNode).getDirectory();
431 return DISubprogram(DbgNode).getDirectory();
433 return DICompileUnit(DbgNode).getDirectory();
435 return DINameSpace(DbgNode).getDirectory();
436 assert(0 && "Invalid DIScope!");
440 //===----------------------------------------------------------------------===//
441 // DIDescriptor: dump routines for all descriptors.
442 //===----------------------------------------------------------------------===//
445 /// dump - Print descriptor.
446 void DIDescriptor::dump() const {
447 dbgs() << "[" << dwarf::TagString(getTag()) << "] ";
448 dbgs().write_hex((intptr_t) &*DbgNode) << ']';
451 /// dump - Print compile unit.
452 void DICompileUnit::dump() const {
454 dbgs() << " [" << dwarf::LanguageString(getLanguage()) << "] ";
456 dbgs() << " [" << getDirectory() << "/" << getFilename() << " ]";
459 /// dump - Print type.
460 void DIType::dump() const {
461 if (!DbgNode) return;
463 StringRef Res = getName();
465 dbgs() << " [" << Res << "] ";
467 unsigned Tag = getTag();
468 dbgs() << " [" << dwarf::TagString(Tag) << "] ";
470 // TODO : Print context
471 getCompileUnit().dump();
473 << getLineNumber() << ", "
474 << getSizeInBits() << ", "
475 << getAlignInBits() << ", "
480 dbgs() << " [private] ";
481 else if (isProtected())
482 dbgs() << " [protected] ";
488 DIBasicType(DbgNode).dump();
489 else if (isDerivedType())
490 DIDerivedType(DbgNode).dump();
491 else if (isCompositeType())
492 DICompositeType(DbgNode).dump();
494 dbgs() << "Invalid DIType\n";
501 /// dump - Print basic type.
502 void DIBasicType::dump() const {
503 dbgs() << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
506 /// dump - Print derived type.
507 void DIDerivedType::dump() const {
508 dbgs() << "\n\t Derived From: "; getTypeDerivedFrom().dump();
511 /// dump - Print composite type.
512 void DICompositeType::dump() const {
513 DIArray A = getTypeArray();
514 dbgs() << " [" << A.getNumElements() << " elements]";
517 /// dump - Print global.
518 void DIGlobal::dump() const {
519 StringRef Res = getName();
521 dbgs() << " [" << Res << "] ";
523 unsigned Tag = getTag();
524 dbgs() << " [" << dwarf::TagString(Tag) << "] ";
526 // TODO : Print context
527 getCompileUnit().dump();
528 dbgs() << " [" << getLineNumber() << "] ";
531 dbgs() << " [local] ";
536 if (isGlobalVariable())
537 DIGlobalVariable(DbgNode).dump();
542 /// dump - Print subprogram.
543 void DISubprogram::dump() const {
544 StringRef Res = getName();
546 dbgs() << " [" << Res << "] ";
548 unsigned Tag = getTag();
549 dbgs() << " [" << dwarf::TagString(Tag) << "] ";
551 // TODO : Print context
552 getCompileUnit().dump();
553 dbgs() << " [" << getLineNumber() << "] ";
556 dbgs() << " [local] ";
564 /// dump - Print global variable.
565 void DIGlobalVariable::dump() const {
571 /// dump - Print variable.
572 void DIVariable::dump() const {
573 StringRef Res = getName();
575 dbgs() << " [" << Res << "] ";
577 getCompileUnit().dump();
578 dbgs() << " [" << getLineNumber() << "] ";
582 // FIXME: Dump complex addresses
585 //===----------------------------------------------------------------------===//
586 // DIFactory: Basic Helpers
587 //===----------------------------------------------------------------------===//
589 DIFactory::DIFactory(Module &m)
590 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
592 Constant *DIFactory::GetTagConstant(unsigned TAG) {
593 assert((TAG & LLVMDebugVersionMask) == 0 &&
594 "Tag too large for debug encoding!");
595 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
598 //===----------------------------------------------------------------------===//
599 // DIFactory: Primary Constructors
600 //===----------------------------------------------------------------------===//
602 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
603 /// This implicitly uniques the arrays created.
604 DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
605 SmallVector<Value*, 16> Elts;
608 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
610 for (unsigned i = 0; i != NumTys; ++i)
611 Elts.push_back(Tys[i].getNode());
613 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
616 /// GetOrCreateSubrange - Create a descriptor for a value range. This
617 /// implicitly uniques the values returned.
618 DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
620 GetTagConstant(dwarf::DW_TAG_subrange_type),
621 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
622 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
625 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
630 /// CreateCompileUnit - Create a new descriptor for the specified compile
631 /// unit. Note that this does not unique compile units within the module.
632 DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
639 unsigned RunTimeVer) {
641 GetTagConstant(dwarf::DW_TAG_compile_unit),
642 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
643 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
644 MDString::get(VMContext, Filename),
645 MDString::get(VMContext, Directory),
646 MDString::get(VMContext, Producer),
647 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
648 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
649 MDString::get(VMContext, Flags),
650 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
653 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
656 /// CreateEnumerator - Create a single enumerator value.
657 DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
659 GetTagConstant(dwarf::DW_TAG_enumerator),
660 MDString::get(VMContext, Name),
661 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
663 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
667 /// CreateBasicType - Create a basic type like int, float, etc.
668 DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
670 DICompileUnit CompileUnit,
673 uint64_t AlignInBits,
674 uint64_t OffsetInBits, unsigned Flags,
677 GetTagConstant(dwarf::DW_TAG_base_type),
679 MDString::get(VMContext, Name),
680 CompileUnit.getNode(),
681 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
682 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
683 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
684 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
685 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
686 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
688 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
692 /// CreateBasicType - Create a basic type like int, float, etc.
693 DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
695 DICompileUnit CompileUnit,
697 Constant *SizeInBits,
698 Constant *AlignInBits,
699 Constant *OffsetInBits, unsigned Flags,
702 GetTagConstant(dwarf::DW_TAG_base_type),
704 MDString::get(VMContext, Name),
705 CompileUnit.getNode(),
706 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
710 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
711 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
713 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
716 /// CreateArtificialType - Create a new DIType with "artificial" flag set.
717 DIType DIFactory::CreateArtificialType(DIType Ty) {
718 if (Ty.isArtificial())
721 SmallVector<Value *, 9> Elts;
722 MDNode *N = Ty.getNode();
723 assert (N && "Unexpected input DIType!");
724 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
725 if (Value *V = N->getOperand(i))
728 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
731 unsigned CurFlags = Ty.getFlags();
732 CurFlags = CurFlags | DIType::FlagArtificial;
734 // Flags are stored at this slot.
735 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
737 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
740 /// CreateDerivedType - Create a derived type like const qualified type,
741 /// pointer, typedef, etc.
742 DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
743 DIDescriptor Context,
745 DICompileUnit CompileUnit,
748 uint64_t AlignInBits,
749 uint64_t OffsetInBits,
751 DIType DerivedFrom) {
755 MDString::get(VMContext, Name),
756 CompileUnit.getNode(),
757 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
758 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
759 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
760 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
761 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
762 DerivedFrom.getNode(),
764 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
768 /// CreateDerivedType - Create a derived type like const qualified type,
769 /// pointer, typedef, etc.
770 DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
771 DIDescriptor Context,
773 DICompileUnit CompileUnit,
775 Constant *SizeInBits,
776 Constant *AlignInBits,
777 Constant *OffsetInBits,
779 DIType DerivedFrom) {
783 MDString::get(VMContext, Name),
784 CompileUnit.getNode(),
785 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
789 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
790 DerivedFrom.getNode(),
792 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
796 /// CreateCompositeType - Create a composite type like array, struct, etc.
797 DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
798 DIDescriptor Context,
800 DICompileUnit CompileUnit,
803 uint64_t AlignInBits,
804 uint64_t OffsetInBits,
808 unsigned RuntimeLang,
809 MDNode *ContainingType) {
814 MDString::get(VMContext, Name),
815 CompileUnit.getNode(),
816 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
817 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
818 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
819 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
820 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
821 DerivedFrom.getNode(),
823 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
826 return DICompositeType(MDNode::get(VMContext, &Elts[0], 13));
830 /// CreateCompositeType - Create a composite type like array, struct, etc.
831 DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
832 DIDescriptor Context,
834 DICompileUnit CompileUnit,
836 Constant *SizeInBits,
837 Constant *AlignInBits,
838 Constant *OffsetInBits,
842 unsigned RuntimeLang) {
847 MDString::get(VMContext, Name),
848 CompileUnit.getNode(),
849 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
853 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
854 DerivedFrom.getNode(),
856 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
858 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
862 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
863 /// See comments in DISubprogram for descriptions of these fields. This
864 /// method does not unique the generated descriptors.
865 DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
867 StringRef DisplayName,
868 StringRef LinkageName,
869 DICompileUnit CompileUnit,
870 unsigned LineNo, DIType Ty,
873 unsigned VK, unsigned VIndex,
874 DIType ContainingType,
878 GetTagConstant(dwarf::DW_TAG_subprogram),
879 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
881 MDString::get(VMContext, Name),
882 MDString::get(VMContext, DisplayName),
883 MDString::get(VMContext, LinkageName),
884 CompileUnit.getNode(),
885 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
887 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
888 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
889 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
890 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
891 ContainingType.getNode(),
892 ConstantInt::get(Type::getInt1Ty(VMContext), isArtificial)
894 return DISubprogram(MDNode::get(VMContext, &Elts[0], 15));
897 /// CreateSubprogramDefinition - Create new subprogram descriptor for the
898 /// given declaration.
899 DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration) {
900 if (SPDeclaration.isDefinition())
901 return DISubprogram(SPDeclaration.getNode());
903 MDNode *DeclNode = SPDeclaration.getNode();
905 GetTagConstant(dwarf::DW_TAG_subprogram),
906 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
907 DeclNode->getOperand(2), // Context
908 DeclNode->getOperand(3), // Name
909 DeclNode->getOperand(4), // DisplayName
910 DeclNode->getOperand(5), // LinkageName
911 DeclNode->getOperand(6), // CompileUnit
912 DeclNode->getOperand(7), // LineNo
913 DeclNode->getOperand(8), // Type
914 DeclNode->getOperand(9), // isLocalToUnit
915 ConstantInt::get(Type::getInt1Ty(VMContext), true),
916 DeclNode->getOperand(11), // Virtuality
917 DeclNode->getOperand(12), // VIndex
918 DeclNode->getOperand(13), // Containting Type
919 DeclNode->getOperand(14) // isArtificial
921 return DISubprogram(MDNode::get(VMContext, &Elts[0], 15));
924 /// CreateGlobalVariable - Create a new descriptor for the specified global.
926 DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
927 StringRef DisplayName,
928 StringRef LinkageName,
929 DICompileUnit CompileUnit,
930 unsigned LineNo, DIType Ty,bool isLocalToUnit,
931 bool isDefinition, llvm::GlobalVariable *Val) {
933 GetTagConstant(dwarf::DW_TAG_variable),
934 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
936 MDString::get(VMContext, Name),
937 MDString::get(VMContext, DisplayName),
938 MDString::get(VMContext, LinkageName),
939 CompileUnit.getNode(),
940 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
942 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
943 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
947 Value *const *Vs = &Elts[0];
948 MDNode *Node = MDNode::get(VMContext,Vs, 12);
950 // Create a named metadata so that we do not lose this mdnode.
951 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
952 NMD->addOperand(Node);
954 return DIGlobalVariable(Node);
958 /// CreateVariable - Create a new descriptor for the specified variable.
959 DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
961 DICompileUnit CompileUnit, unsigned LineNo,
966 MDString::get(VMContext, Name),
967 CompileUnit.getNode(),
968 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
971 return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
975 /// CreateComplexVariable - Create a new descriptor for the specified variable
976 /// which has a complex address expression for its address.
977 DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
978 const std::string &Name,
979 DICompileUnit CompileUnit,
982 SmallVector<Value *, 9> &addr) {
983 SmallVector<Value *, 9> Elts;
984 Elts.push_back(GetTagConstant(Tag));
985 Elts.push_back(Context.getNode());
986 Elts.push_back(MDString::get(VMContext, Name));
987 Elts.push_back(CompileUnit.getNode());
988 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
989 Elts.push_back(Ty.getNode());
990 Elts.insert(Elts.end(), addr.begin(), addr.end());
992 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
996 /// CreateBlock - This creates a descriptor for a lexical block with the
997 /// specified parent VMContext.
998 DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
999 unsigned LineNo, unsigned Col) {
1001 GetTagConstant(dwarf::DW_TAG_lexical_block),
1003 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1004 ConstantInt::get(Type::getInt32Ty(VMContext), Col)
1006 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 4));
1009 /// CreateNameSpace - This creates new descriptor for a namespace
1010 /// with the specified parent context.
1011 DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
1012 DICompileUnit CompileUnit,
1015 GetTagConstant(dwarf::DW_TAG_namespace),
1017 MDString::get(VMContext, Name),
1018 CompileUnit.getNode(),
1019 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1021 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1024 /// CreateLocation - Creates a debug info location.
1025 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1026 DIScope S, DILocation OrigLoc) {
1028 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1029 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1033 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1036 /// CreateLocation - Creates a debug info location.
1037 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1038 DIScope S, MDNode *OrigLoc) {
1040 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1041 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1045 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1048 //===----------------------------------------------------------------------===//
1049 // DIFactory: Routines for inserting code into a function
1050 //===----------------------------------------------------------------------===//
1052 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1053 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1054 Instruction *InsertBefore) {
1055 assert(Storage && "no storage passed to dbg.declare");
1056 assert(D.getNode() && "empty DIVariable passed to dbg.declare");
1058 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1060 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1062 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
1065 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1066 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1067 BasicBlock *InsertAtEnd) {
1068 assert(Storage && "no storage passed to dbg.declare");
1069 assert(D.getNode() && "empty DIVariable passed to dbg.declare");
1071 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1073 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1076 // If this block already has a terminator then insert this intrinsic
1077 // before the terminator.
1078 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1079 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1081 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
1083 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1084 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1086 Instruction *InsertBefore) {
1087 assert(V && "no value passed to dbg.value");
1088 assert(D.getNode() && "empty DIVariable passed to dbg.value");
1090 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1092 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1093 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1095 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1098 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1099 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1101 BasicBlock *InsertAtEnd) {
1102 assert(V && "no value passed to dbg.value");
1103 assert(D.getNode() && "empty DIVariable passed to dbg.value");
1105 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1107 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1108 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1110 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1113 //===----------------------------------------------------------------------===//
1114 // DebugInfoFinder implementations.
1115 //===----------------------------------------------------------------------===//
1117 /// processModule - Process entire module and collect debug info.
1118 void DebugInfoFinder::processModule(Module &M) {
1119 unsigned MDDbgKind = M.getMDKindID("dbg");
1121 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1122 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1123 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1125 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1126 processDeclare(DDI);
1127 else if (MDNode *L = BI->getMetadata(MDDbgKind))
1128 processLocation(DILocation(L));
1131 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
1135 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1136 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1137 if (addGlobalVariable(DIG)) {
1138 addCompileUnit(DIG.getCompileUnit());
1139 processType(DIG.getType());
1144 /// processLocation - Process DILocation.
1145 void DebugInfoFinder::processLocation(DILocation Loc) {
1146 if (!Loc.Verify()) return;
1147 DIDescriptor S(Loc.getScope().getNode());
1148 if (S.isCompileUnit())
1149 addCompileUnit(DICompileUnit(S.getNode()));
1150 else if (S.isSubprogram())
1151 processSubprogram(DISubprogram(S.getNode()));
1152 else if (S.isLexicalBlock())
1153 processLexicalBlock(DILexicalBlock(S.getNode()));
1154 processLocation(Loc.getOrigLocation());
1157 /// processType - Process DIType.
1158 void DebugInfoFinder::processType(DIType DT) {
1162 addCompileUnit(DT.getCompileUnit());
1163 if (DT.isCompositeType()) {
1164 DICompositeType DCT(DT.getNode());
1165 processType(DCT.getTypeDerivedFrom());
1166 DIArray DA = DCT.getTypeArray();
1167 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1168 DIDescriptor D = DA.getElement(i);
1170 processType(DIType(D.getNode()));
1171 else if (D.isSubprogram())
1172 processSubprogram(DISubprogram(D.getNode()));
1174 } else if (DT.isDerivedType()) {
1175 DIDerivedType DDT(DT.getNode());
1176 processType(DDT.getTypeDerivedFrom());
1180 /// processLexicalBlock
1181 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1182 DIScope Context = LB.getContext();
1183 if (Context.isLexicalBlock())
1184 return processLexicalBlock(DILexicalBlock(Context.getNode()));
1186 return processSubprogram(DISubprogram(Context.getNode()));
1189 /// processSubprogram - Process DISubprogram.
1190 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1191 if (!addSubprogram(SP))
1193 addCompileUnit(SP.getCompileUnit());
1194 processType(SP.getType());
1197 /// processDeclare - Process DbgDeclareInst.
1198 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
1199 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1203 if (!DV.isVariable())
1206 if (!NodesSeen.insert(DV.getNode()))
1209 addCompileUnit(DIVariable(N).getCompileUnit());
1210 processType(DIVariable(N).getType());
1213 /// addType - Add type into Tys.
1214 bool DebugInfoFinder::addType(DIType DT) {
1218 if (!NodesSeen.insert(DT.getNode()))
1221 TYs.push_back(DT.getNode());
1225 /// addCompileUnit - Add compile unit into CUs.
1226 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1230 if (!NodesSeen.insert(CU.getNode()))
1233 CUs.push_back(CU.getNode());
1237 /// addGlobalVariable - Add global variable into GVs.
1238 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1239 if (!DIDescriptor(DIG.getNode()).isGlobalVariable())
1242 if (!NodesSeen.insert(DIG.getNode()))
1245 GVs.push_back(DIG.getNode());
1249 // addSubprogram - Add subprgoram into SPs.
1250 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1251 if (!DIDescriptor(SP.getNode()).isSubprogram())
1254 if (!NodesSeen.insert(SP.getNode()))
1257 SPs.push_back(SP.getNode());
1261 /// Find the debug info descriptor corresponding to this global variable.
1262 static Value *findDbgGlobalDeclare(GlobalVariable *V) {
1263 const Module *M = V->getParent();
1264 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1268 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1269 DIDescriptor DIG(cast_or_null<MDNode>(NMD->getOperand(i)));
1270 if (!DIG.isGlobalVariable())
1272 if (DIGlobalVariable(DIG.getNode()).getGlobal() == V)
1273 return DIG.getNode();
1278 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1279 /// It looks through pointer casts too.
1280 static const DbgDeclareInst *findDbgDeclare(const Value *V) {
1281 V = V->stripPointerCasts();
1283 if (!isa<Instruction>(V) && !isa<Argument>(V))
1286 const Function *F = NULL;
1287 if (const Instruction *I = dyn_cast<Instruction>(V))
1288 F = I->getParent()->getParent();
1289 else if (const Argument *A = dyn_cast<Argument>(V))
1292 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1293 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1295 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1296 if (DDI->getAddress() == V)
1302 bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1303 std::string &Type, unsigned &LineNo,
1304 std::string &File, std::string &Dir) {
1308 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1309 Value *DIGV = findDbgGlobalDeclare(GV);
1310 if (!DIGV) return false;
1311 DIGlobalVariable Var(cast<MDNode>(DIGV));
1313 StringRef D = Var.getDisplayName();
1316 LineNo = Var.getLineNumber();
1317 Unit = Var.getCompileUnit();
1318 TypeD = Var.getType();
1320 const DbgDeclareInst *DDI = findDbgDeclare(V);
1321 if (!DDI) return false;
1322 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1324 StringRef D = Var.getName();
1327 LineNo = Var.getLineNumber();
1328 Unit = Var.getCompileUnit();
1329 TypeD = Var.getType();
1332 StringRef T = TypeD.getName();
1335 StringRef F = Unit.getFilename();
1338 StringRef D = Unit.getDirectory();
1344 /// ExtractDebugLocation - Extract debug location information
1345 /// from DILocation.
1346 DebugLoc llvm::ExtractDebugLocation(DILocation &Loc,
1347 DebugLocTracker &DebugLocInfo) {
1348 DenseMap<MDNode *, unsigned>::iterator II
1349 = DebugLocInfo.DebugIdMap.find(Loc.getNode());
1350 if (II != DebugLocInfo.DebugIdMap.end())
1351 return DebugLoc::get(II->second);
1353 // Add a new location entry.
1354 unsigned Id = DebugLocInfo.DebugLocations.size();
1355 DebugLocInfo.DebugLocations.push_back(Loc.getNode());
1356 DebugLocInfo.DebugIdMap[Loc.getNode()] = Id;
1358 return DebugLoc::get(Id);
1361 /// getDISubprogram - Find subprogram that is enclosing this scope.
1362 DISubprogram llvm::getDISubprogram(MDNode *Scope) {
1363 DIDescriptor D(Scope);
1364 if (D.isSubprogram())
1365 return DISubprogram(Scope);
1367 if (D.isLexicalBlock())
1368 return getDISubprogram(DILexicalBlock(Scope).getContext().getNode());
1370 return DISubprogram();
1373 /// getDICompositeType - Find underlying composite type.
1374 DICompositeType llvm::getDICompositeType(DIType T) {
1375 if (T.isCompositeType())
1376 return DICompositeType(T.getNode());
1378 if (T.isDerivedType())
1379 return getDICompositeType(DIDerivedType(T.getNode()).getTypeDerivedFrom());
1381 return DICompositeType();