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) : DIScope(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 {
417 if (isLexicalBlock())
418 return DILexicalBlock(DbgNode).getFilename();
420 return DISubprogram(DbgNode).getFilename();
422 return DICompileUnit(DbgNode).getFilename();
424 return DINameSpace(DbgNode).getFilename();
426 return DIType(DbgNode).getFilename();
427 assert(0 && "Invalid DIScope!");
431 StringRef DIScope::getDirectory() const {
434 if (isLexicalBlock())
435 return DILexicalBlock(DbgNode).getDirectory();
437 return DISubprogram(DbgNode).getDirectory();
439 return DICompileUnit(DbgNode).getDirectory();
441 return DINameSpace(DbgNode).getDirectory();
443 return DIType(DbgNode).getDirectory();
444 assert(0 && "Invalid DIScope!");
448 //===----------------------------------------------------------------------===//
449 // DIDescriptor: dump routines for all descriptors.
450 //===----------------------------------------------------------------------===//
453 /// dump - Print descriptor.
454 void DIDescriptor::dump() const {
455 dbgs() << "[" << dwarf::TagString(getTag()) << "] ";
456 dbgs().write_hex((intptr_t) &*DbgNode) << ']';
459 /// dump - Print compile unit.
460 void DICompileUnit::dump() const {
462 dbgs() << " [" << dwarf::LanguageString(getLanguage()) << "] ";
464 dbgs() << " [" << getDirectory() << "/" << getFilename() << " ]";
467 /// dump - Print type.
468 void DIType::dump() const {
469 if (!DbgNode) return;
471 StringRef Res = getName();
473 dbgs() << " [" << Res << "] ";
475 unsigned Tag = getTag();
476 dbgs() << " [" << dwarf::TagString(Tag) << "] ";
478 // TODO : Print context
479 getCompileUnit().dump();
481 << getLineNumber() << ", "
482 << getSizeInBits() << ", "
483 << getAlignInBits() << ", "
488 dbgs() << " [private] ";
489 else if (isProtected())
490 dbgs() << " [protected] ";
496 DIBasicType(DbgNode).dump();
497 else if (isDerivedType())
498 DIDerivedType(DbgNode).dump();
499 else if (isCompositeType())
500 DICompositeType(DbgNode).dump();
502 dbgs() << "Invalid DIType\n";
509 /// dump - Print basic type.
510 void DIBasicType::dump() const {
511 dbgs() << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
514 /// dump - Print derived type.
515 void DIDerivedType::dump() const {
516 dbgs() << "\n\t Derived From: "; getTypeDerivedFrom().dump();
519 /// dump - Print composite type.
520 void DICompositeType::dump() const {
521 DIArray A = getTypeArray();
522 dbgs() << " [" << A.getNumElements() << " elements]";
525 /// dump - Print global.
526 void DIGlobal::dump() const {
527 StringRef Res = getName();
529 dbgs() << " [" << Res << "] ";
531 unsigned Tag = getTag();
532 dbgs() << " [" << dwarf::TagString(Tag) << "] ";
534 // TODO : Print context
535 getCompileUnit().dump();
536 dbgs() << " [" << getLineNumber() << "] ";
539 dbgs() << " [local] ";
544 if (isGlobalVariable())
545 DIGlobalVariable(DbgNode).dump();
550 /// dump - Print subprogram.
551 void DISubprogram::dump() const {
552 StringRef Res = getName();
554 dbgs() << " [" << Res << "] ";
556 unsigned Tag = getTag();
557 dbgs() << " [" << dwarf::TagString(Tag) << "] ";
559 // TODO : Print context
560 getCompileUnit().dump();
561 dbgs() << " [" << getLineNumber() << "] ";
564 dbgs() << " [local] ";
572 /// dump - Print global variable.
573 void DIGlobalVariable::dump() const {
579 /// dump - Print variable.
580 void DIVariable::dump() const {
581 StringRef Res = getName();
583 dbgs() << " [" << Res << "] ";
585 getCompileUnit().dump();
586 dbgs() << " [" << getLineNumber() << "] ";
590 // FIXME: Dump complex addresses
593 //===----------------------------------------------------------------------===//
594 // DIFactory: Basic Helpers
595 //===----------------------------------------------------------------------===//
597 DIFactory::DIFactory(Module &m)
598 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
600 Constant *DIFactory::GetTagConstant(unsigned TAG) {
601 assert((TAG & LLVMDebugVersionMask) == 0 &&
602 "Tag too large for debug encoding!");
603 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
606 //===----------------------------------------------------------------------===//
607 // DIFactory: Primary Constructors
608 //===----------------------------------------------------------------------===//
610 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
611 /// This implicitly uniques the arrays created.
612 DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
613 SmallVector<Value*, 16> Elts;
616 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
618 for (unsigned i = 0; i != NumTys; ++i)
619 Elts.push_back(Tys[i].getNode());
621 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
624 /// GetOrCreateSubrange - Create a descriptor for a value range. This
625 /// implicitly uniques the values returned.
626 DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
628 GetTagConstant(dwarf::DW_TAG_subrange_type),
629 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
630 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
633 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
638 /// CreateCompileUnit - Create a new descriptor for the specified compile
639 /// unit. Note that this does not unique compile units within the module.
640 DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
647 unsigned RunTimeVer) {
649 GetTagConstant(dwarf::DW_TAG_compile_unit),
650 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
651 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
652 MDString::get(VMContext, Filename),
653 MDString::get(VMContext, Directory),
654 MDString::get(VMContext, Producer),
655 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
656 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
657 MDString::get(VMContext, Flags),
658 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
661 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
664 /// CreateEnumerator - Create a single enumerator value.
665 DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
667 GetTagConstant(dwarf::DW_TAG_enumerator),
668 MDString::get(VMContext, Name),
669 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
671 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
675 /// CreateBasicType - Create a basic type like int, float, etc.
676 DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
678 DICompileUnit CompileUnit,
681 uint64_t AlignInBits,
682 uint64_t OffsetInBits, unsigned Flags,
685 GetTagConstant(dwarf::DW_TAG_base_type),
687 MDString::get(VMContext, Name),
688 CompileUnit.getNode(),
689 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
690 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
691 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
692 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
693 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
694 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
696 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
700 /// CreateBasicType - Create a basic type like int, float, etc.
701 DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
703 DICompileUnit CompileUnit,
705 Constant *SizeInBits,
706 Constant *AlignInBits,
707 Constant *OffsetInBits, unsigned Flags,
710 GetTagConstant(dwarf::DW_TAG_base_type),
712 MDString::get(VMContext, Name),
713 CompileUnit.getNode(),
714 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
718 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
719 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
721 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
724 /// CreateArtificialType - Create a new DIType with "artificial" flag set.
725 DIType DIFactory::CreateArtificialType(DIType Ty) {
726 if (Ty.isArtificial())
729 SmallVector<Value *, 9> Elts;
730 MDNode *N = Ty.getNode();
731 assert (N && "Unexpected input DIType!");
732 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
733 if (Value *V = N->getOperand(i))
736 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
739 unsigned CurFlags = Ty.getFlags();
740 CurFlags = CurFlags | DIType::FlagArtificial;
742 // Flags are stored at this slot.
743 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
745 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
748 /// CreateDerivedType - Create a derived type like const qualified type,
749 /// pointer, typedef, etc.
750 DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
751 DIDescriptor Context,
753 DICompileUnit CompileUnit,
756 uint64_t AlignInBits,
757 uint64_t OffsetInBits,
759 DIType DerivedFrom) {
763 MDString::get(VMContext, Name),
764 CompileUnit.getNode(),
765 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
766 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
767 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
768 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
769 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
770 DerivedFrom.getNode(),
772 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
776 /// CreateDerivedType - Create a derived type like const qualified type,
777 /// pointer, typedef, etc.
778 DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
779 DIDescriptor Context,
781 DICompileUnit CompileUnit,
783 Constant *SizeInBits,
784 Constant *AlignInBits,
785 Constant *OffsetInBits,
787 DIType DerivedFrom) {
791 MDString::get(VMContext, Name),
792 CompileUnit.getNode(),
793 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
797 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
798 DerivedFrom.getNode(),
800 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
804 /// CreateCompositeType - Create a composite type like array, struct, etc.
805 DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
806 DIDescriptor Context,
808 DICompileUnit CompileUnit,
811 uint64_t AlignInBits,
812 uint64_t OffsetInBits,
816 unsigned RuntimeLang,
817 MDNode *ContainingType) {
822 MDString::get(VMContext, Name),
823 CompileUnit.getNode(),
824 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
825 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
826 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
827 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
828 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
829 DerivedFrom.getNode(),
831 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
834 return DICompositeType(MDNode::get(VMContext, &Elts[0], 13));
838 /// CreateCompositeType - Create a composite type like array, struct, etc.
839 DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
840 DIDescriptor Context,
842 DICompileUnit CompileUnit,
844 Constant *SizeInBits,
845 Constant *AlignInBits,
846 Constant *OffsetInBits,
850 unsigned RuntimeLang) {
855 MDString::get(VMContext, Name),
856 CompileUnit.getNode(),
857 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
861 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
862 DerivedFrom.getNode(),
864 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
866 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
870 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
871 /// See comments in DISubprogram for descriptions of these fields. This
872 /// method does not unique the generated descriptors.
873 DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
875 StringRef DisplayName,
876 StringRef LinkageName,
877 DICompileUnit CompileUnit,
878 unsigned LineNo, DIType Ty,
881 unsigned VK, unsigned VIndex,
882 DIType ContainingType,
886 GetTagConstant(dwarf::DW_TAG_subprogram),
887 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
889 MDString::get(VMContext, Name),
890 MDString::get(VMContext, DisplayName),
891 MDString::get(VMContext, LinkageName),
892 CompileUnit.getNode(),
893 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
895 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
896 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
897 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
898 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
899 ContainingType.getNode(),
900 ConstantInt::get(Type::getInt1Ty(VMContext), isArtificial)
902 return DISubprogram(MDNode::get(VMContext, &Elts[0], 15));
905 /// CreateSubprogramDefinition - Create new subprogram descriptor for the
906 /// given declaration.
907 DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration) {
908 if (SPDeclaration.isDefinition())
909 return DISubprogram(SPDeclaration.getNode());
911 MDNode *DeclNode = SPDeclaration.getNode();
913 GetTagConstant(dwarf::DW_TAG_subprogram),
914 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
915 DeclNode->getOperand(2), // Context
916 DeclNode->getOperand(3), // Name
917 DeclNode->getOperand(4), // DisplayName
918 DeclNode->getOperand(5), // LinkageName
919 DeclNode->getOperand(6), // CompileUnit
920 DeclNode->getOperand(7), // LineNo
921 DeclNode->getOperand(8), // Type
922 DeclNode->getOperand(9), // isLocalToUnit
923 ConstantInt::get(Type::getInt1Ty(VMContext), true),
924 DeclNode->getOperand(11), // Virtuality
925 DeclNode->getOperand(12), // VIndex
926 DeclNode->getOperand(13), // Containting Type
927 DeclNode->getOperand(14) // isArtificial
929 return DISubprogram(MDNode::get(VMContext, &Elts[0], 15));
932 /// CreateGlobalVariable - Create a new descriptor for the specified global.
934 DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
935 StringRef DisplayName,
936 StringRef LinkageName,
937 DICompileUnit CompileUnit,
938 unsigned LineNo, DIType Ty,bool isLocalToUnit,
939 bool isDefinition, llvm::GlobalVariable *Val) {
941 GetTagConstant(dwarf::DW_TAG_variable),
942 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
944 MDString::get(VMContext, Name),
945 MDString::get(VMContext, DisplayName),
946 MDString::get(VMContext, LinkageName),
947 CompileUnit.getNode(),
948 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
950 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
951 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
955 Value *const *Vs = &Elts[0];
956 MDNode *Node = MDNode::get(VMContext,Vs, 12);
958 // Create a named metadata so that we do not lose this mdnode.
959 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
960 NMD->addOperand(Node);
962 return DIGlobalVariable(Node);
966 /// CreateVariable - Create a new descriptor for the specified variable.
967 DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
969 DICompileUnit CompileUnit, unsigned LineNo,
974 MDString::get(VMContext, Name),
975 CompileUnit.getNode(),
976 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
979 return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
983 /// CreateComplexVariable - Create a new descriptor for the specified variable
984 /// which has a complex address expression for its address.
985 DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
986 const std::string &Name,
987 DICompileUnit CompileUnit,
990 SmallVector<Value *, 9> &addr) {
991 SmallVector<Value *, 9> Elts;
992 Elts.push_back(GetTagConstant(Tag));
993 Elts.push_back(Context.getNode());
994 Elts.push_back(MDString::get(VMContext, Name));
995 Elts.push_back(CompileUnit.getNode());
996 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
997 Elts.push_back(Ty.getNode());
998 Elts.insert(Elts.end(), addr.begin(), addr.end());
1000 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
1004 /// CreateBlock - This creates a descriptor for a lexical block with the
1005 /// specified parent VMContext.
1006 DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
1007 unsigned LineNo, unsigned Col) {
1009 GetTagConstant(dwarf::DW_TAG_lexical_block),
1011 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1012 ConstantInt::get(Type::getInt32Ty(VMContext), Col)
1014 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 4));
1017 /// CreateNameSpace - This creates new descriptor for a namespace
1018 /// with the specified parent context.
1019 DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
1020 DICompileUnit CompileUnit,
1023 GetTagConstant(dwarf::DW_TAG_namespace),
1025 MDString::get(VMContext, Name),
1026 CompileUnit.getNode(),
1027 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1029 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1032 /// CreateLocation - Creates a debug info location.
1033 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1034 DIScope S, DILocation OrigLoc) {
1036 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1037 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1041 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1044 /// CreateLocation - Creates a debug info location.
1045 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1046 DIScope S, MDNode *OrigLoc) {
1048 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1049 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1053 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1056 //===----------------------------------------------------------------------===//
1057 // DIFactory: Routines for inserting code into a function
1058 //===----------------------------------------------------------------------===//
1060 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1061 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1062 Instruction *InsertBefore) {
1063 assert(Storage && "no storage passed to dbg.declare");
1064 assert(D.getNode() && "empty DIVariable passed to dbg.declare");
1066 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1068 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1070 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
1073 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1074 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1075 BasicBlock *InsertAtEnd) {
1076 assert(Storage && "no storage passed to dbg.declare");
1077 assert(D.getNode() && "empty DIVariable passed to dbg.declare");
1079 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1081 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1084 // If this block already has a terminator then insert this intrinsic
1085 // before the terminator.
1086 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1087 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1089 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
1091 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1092 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1094 Instruction *InsertBefore) {
1095 assert(V && "no value passed to dbg.value");
1096 assert(D.getNode() && "empty DIVariable passed to dbg.value");
1098 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1100 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1101 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1103 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1106 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1107 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1109 BasicBlock *InsertAtEnd) {
1110 assert(V && "no value passed to dbg.value");
1111 assert(D.getNode() && "empty DIVariable passed to dbg.value");
1113 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1115 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1116 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1118 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1121 //===----------------------------------------------------------------------===//
1122 // DebugInfoFinder implementations.
1123 //===----------------------------------------------------------------------===//
1125 /// processModule - Process entire module and collect debug info.
1126 void DebugInfoFinder::processModule(Module &M) {
1127 unsigned MDDbgKind = M.getMDKindID("dbg");
1129 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1130 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1131 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1133 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1134 processDeclare(DDI);
1135 else if (MDNode *L = BI->getMetadata(MDDbgKind))
1136 processLocation(DILocation(L));
1139 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
1143 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1144 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1145 if (addGlobalVariable(DIG)) {
1146 addCompileUnit(DIG.getCompileUnit());
1147 processType(DIG.getType());
1152 /// processLocation - Process DILocation.
1153 void DebugInfoFinder::processLocation(DILocation Loc) {
1154 if (!Loc.Verify()) return;
1155 DIDescriptor S(Loc.getScope().getNode());
1156 if (S.isCompileUnit())
1157 addCompileUnit(DICompileUnit(S.getNode()));
1158 else if (S.isSubprogram())
1159 processSubprogram(DISubprogram(S.getNode()));
1160 else if (S.isLexicalBlock())
1161 processLexicalBlock(DILexicalBlock(S.getNode()));
1162 processLocation(Loc.getOrigLocation());
1165 /// processType - Process DIType.
1166 void DebugInfoFinder::processType(DIType DT) {
1170 addCompileUnit(DT.getCompileUnit());
1171 if (DT.isCompositeType()) {
1172 DICompositeType DCT(DT.getNode());
1173 processType(DCT.getTypeDerivedFrom());
1174 DIArray DA = DCT.getTypeArray();
1175 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1176 DIDescriptor D = DA.getElement(i);
1178 processType(DIType(D.getNode()));
1179 else if (D.isSubprogram())
1180 processSubprogram(DISubprogram(D.getNode()));
1182 } else if (DT.isDerivedType()) {
1183 DIDerivedType DDT(DT.getNode());
1184 processType(DDT.getTypeDerivedFrom());
1188 /// processLexicalBlock
1189 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1190 DIScope Context = LB.getContext();
1191 if (Context.isLexicalBlock())
1192 return processLexicalBlock(DILexicalBlock(Context.getNode()));
1194 return processSubprogram(DISubprogram(Context.getNode()));
1197 /// processSubprogram - Process DISubprogram.
1198 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1199 if (!addSubprogram(SP))
1201 addCompileUnit(SP.getCompileUnit());
1202 processType(SP.getType());
1205 /// processDeclare - Process DbgDeclareInst.
1206 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
1207 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1211 if (!DV.isVariable())
1214 if (!NodesSeen.insert(DV.getNode()))
1217 addCompileUnit(DIVariable(N).getCompileUnit());
1218 processType(DIVariable(N).getType());
1221 /// addType - Add type into Tys.
1222 bool DebugInfoFinder::addType(DIType DT) {
1226 if (!NodesSeen.insert(DT.getNode()))
1229 TYs.push_back(DT.getNode());
1233 /// addCompileUnit - Add compile unit into CUs.
1234 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1238 if (!NodesSeen.insert(CU.getNode()))
1241 CUs.push_back(CU.getNode());
1245 /// addGlobalVariable - Add global variable into GVs.
1246 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1247 if (!DIDescriptor(DIG.getNode()).isGlobalVariable())
1250 if (!NodesSeen.insert(DIG.getNode()))
1253 GVs.push_back(DIG.getNode());
1257 // addSubprogram - Add subprgoram into SPs.
1258 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1259 if (!DIDescriptor(SP.getNode()).isSubprogram())
1262 if (!NodesSeen.insert(SP.getNode()))
1265 SPs.push_back(SP.getNode());
1269 /// Find the debug info descriptor corresponding to this global variable.
1270 static Value *findDbgGlobalDeclare(GlobalVariable *V) {
1271 const Module *M = V->getParent();
1272 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1276 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1277 DIDescriptor DIG(cast_or_null<MDNode>(NMD->getOperand(i)));
1278 if (!DIG.isGlobalVariable())
1280 if (DIGlobalVariable(DIG.getNode()).getGlobal() == V)
1281 return DIG.getNode();
1286 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1287 /// It looks through pointer casts too.
1288 static const DbgDeclareInst *findDbgDeclare(const Value *V) {
1289 V = V->stripPointerCasts();
1291 if (!isa<Instruction>(V) && !isa<Argument>(V))
1294 const Function *F = NULL;
1295 if (const Instruction *I = dyn_cast<Instruction>(V))
1296 F = I->getParent()->getParent();
1297 else if (const Argument *A = dyn_cast<Argument>(V))
1300 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1301 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1303 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1304 if (DDI->getAddress() == V)
1310 bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1311 std::string &Type, unsigned &LineNo,
1312 std::string &File, std::string &Dir) {
1316 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1317 Value *DIGV = findDbgGlobalDeclare(GV);
1318 if (!DIGV) return false;
1319 DIGlobalVariable Var(cast<MDNode>(DIGV));
1321 StringRef D = Var.getDisplayName();
1324 LineNo = Var.getLineNumber();
1325 Unit = Var.getCompileUnit();
1326 TypeD = Var.getType();
1328 const DbgDeclareInst *DDI = findDbgDeclare(V);
1329 if (!DDI) return false;
1330 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1332 StringRef D = Var.getName();
1335 LineNo = Var.getLineNumber();
1336 Unit = Var.getCompileUnit();
1337 TypeD = Var.getType();
1340 StringRef T = TypeD.getName();
1343 StringRef F = Unit.getFilename();
1346 StringRef D = Unit.getDirectory();
1352 /// ExtractDebugLocation - Extract debug location information
1353 /// from DILocation.
1354 DebugLoc llvm::ExtractDebugLocation(DILocation &Loc,
1355 DebugLocTracker &DebugLocInfo) {
1356 DenseMap<MDNode *, unsigned>::iterator II
1357 = DebugLocInfo.DebugIdMap.find(Loc.getNode());
1358 if (II != DebugLocInfo.DebugIdMap.end())
1359 return DebugLoc::get(II->second);
1361 // Add a new location entry.
1362 unsigned Id = DebugLocInfo.DebugLocations.size();
1363 DebugLocInfo.DebugLocations.push_back(Loc.getNode());
1364 DebugLocInfo.DebugIdMap[Loc.getNode()] = Id;
1366 return DebugLoc::get(Id);
1369 /// getDISubprogram - Find subprogram that is enclosing this scope.
1370 DISubprogram llvm::getDISubprogram(MDNode *Scope) {
1371 DIDescriptor D(Scope);
1372 if (D.isSubprogram())
1373 return DISubprogram(Scope);
1375 if (D.isLexicalBlock())
1376 return getDISubprogram(DILexicalBlock(Scope).getContext().getNode());
1378 return DISubprogram();
1381 /// getDICompositeType - Find underlying composite type.
1382 DICompositeType llvm::getDICompositeType(DIType T) {
1383 if (T.isCompositeType())
1384 return DICompositeType(T.getNode());
1386 if (T.isDerivedType())
1387 return getDICompositeType(DIDerivedType(T.getNode()).getTypeDerivedFrom());
1389 return DICompositeType();