1 //===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the helper classes used to build and interpret debug
11 // information in LLVM IR form.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Analysis/DebugInfo.h"
16 #include "llvm/Target/TargetMachine.h" // FIXME: LAYERING VIOLATION!
17 #include "llvm/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Intrinsics.h"
20 #include "llvm/IntrinsicInst.h"
21 #include "llvm/Instructions.h"
22 #include "llvm/Module.h"
23 #include "llvm/Analysis/ValueTracking.h"
24 #include "llvm/ADT/SmallPtrSet.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/Dwarf.h"
27 #include "llvm/Support/raw_ostream.h"
29 using namespace llvm::dwarf;
31 //===----------------------------------------------------------------------===//
33 //===----------------------------------------------------------------------===//
35 /// ValidDebugInfo - Return true if V represents valid debug info value.
36 /// FIXME : Add DIDescriptor.isValid()
37 bool DIDescriptor::ValidDebugInfo(MDNode *N, unsigned OptLevel) {
43 // Check current version. Allow Version7 for now.
44 unsigned Version = DI.getVersion();
45 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion7)
48 switch (DI.getTag()) {
50 assert(DIVariable(N).Verify() && "Invalid DebugInfo value");
52 case DW_TAG_compile_unit:
53 assert(DICompileUnit(N).Verify() && "Invalid DebugInfo value");
55 case DW_TAG_subprogram:
56 assert(DISubprogram(N).Verify() && "Invalid DebugInfo value");
58 case DW_TAG_lexical_block:
59 // FIXME: This interfers with the quality of generated code during
61 if (OptLevel != CodeGenOpt::None)
72 DIDescriptor::getStringField(unsigned Elt) const {
76 if (Elt < DbgNode->getNumOperands())
77 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
78 return MDS->getString();
83 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
87 if (Elt < DbgNode->getNumOperands())
88 if (ConstantInt *CI = dyn_cast<ConstantInt>(DbgNode->getOperand(Elt)))
89 return CI->getZExtValue();
94 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
96 return DIDescriptor();
98 if (Elt < DbgNode->getNumOperands())
99 return DIDescriptor(dyn_cast_or_null<MDNode>(DbgNode->getOperand(Elt)));
100 return DIDescriptor();
103 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
107 if (Elt < DbgNode->getNumOperands())
108 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
112 unsigned DIVariable::getNumAddrElements() const {
113 return DbgNode->getNumOperands()-6;
117 //===----------------------------------------------------------------------===//
119 //===----------------------------------------------------------------------===//
121 /// isBasicType - Return true if the specified tag is legal for
123 bool DIDescriptor::isBasicType() const {
124 return DbgNode && getTag() == dwarf::DW_TAG_base_type;
127 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
128 bool DIDescriptor::isDerivedType() const {
129 if (!DbgNode) return false;
131 case dwarf::DW_TAG_typedef:
132 case dwarf::DW_TAG_pointer_type:
133 case dwarf::DW_TAG_reference_type:
134 case dwarf::DW_TAG_const_type:
135 case dwarf::DW_TAG_volatile_type:
136 case dwarf::DW_TAG_restrict_type:
137 case dwarf::DW_TAG_member:
138 case dwarf::DW_TAG_inheritance:
141 // CompositeTypes are currently modelled as DerivedTypes.
142 return isCompositeType();
146 /// isCompositeType - Return true if the specified tag is legal for
148 bool DIDescriptor::isCompositeType() const {
149 if (!DbgNode) return false;
151 case dwarf::DW_TAG_array_type:
152 case dwarf::DW_TAG_structure_type:
153 case dwarf::DW_TAG_union_type:
154 case dwarf::DW_TAG_enumeration_type:
155 case dwarf::DW_TAG_vector_type:
156 case dwarf::DW_TAG_subroutine_type:
157 case dwarf::DW_TAG_class_type:
164 /// isVariable - Return true if the specified tag is legal for DIVariable.
165 bool DIDescriptor::isVariable() const {
166 if (!DbgNode) return false;
168 case dwarf::DW_TAG_auto_variable:
169 case dwarf::DW_TAG_arg_variable:
170 case dwarf::DW_TAG_return_variable:
177 /// isType - Return true if the specified tag is legal for DIType.
178 bool DIDescriptor::isType() const {
179 return isBasicType() || isCompositeType() || isDerivedType();
182 /// isSubprogram - Return true if the specified tag is legal for
184 bool DIDescriptor::isSubprogram() const {
185 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
188 /// isGlobalVariable - Return true if the specified tag is legal for
189 /// DIGlobalVariable.
190 bool DIDescriptor::isGlobalVariable() const {
191 return DbgNode && getTag() == dwarf::DW_TAG_variable;
194 /// isGlobal - Return true if the specified tag is legal for DIGlobal.
195 bool DIDescriptor::isGlobal() const {
196 return isGlobalVariable();
199 /// isScope - Return true if the specified tag is one of the scope
201 bool DIDescriptor::isScope() const {
202 if (!DbgNode) return false;
204 case dwarf::DW_TAG_compile_unit:
205 case dwarf::DW_TAG_lexical_block:
206 case dwarf::DW_TAG_subprogram:
207 case dwarf::DW_TAG_namespace:
215 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
216 bool DIDescriptor::isCompileUnit() const {
217 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
220 /// isFile - Return true if the specified tag is DW_TAG_file_type.
221 bool DIDescriptor::isFile() const {
222 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
225 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
226 bool DIDescriptor::isNameSpace() const {
227 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
230 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
231 bool DIDescriptor::isLexicalBlock() const {
232 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
235 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
236 bool DIDescriptor::isSubrange() const {
237 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
240 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
241 bool DIDescriptor::isEnumerator() const {
242 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
245 //===----------------------------------------------------------------------===//
246 // Simple Descriptor Constructors and other Methods
247 //===----------------------------------------------------------------------===//
249 DIType::DIType(MDNode *N) : DIScope(N) {
251 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
256 unsigned DIArray::getNumElements() const {
259 return DbgNode->getNumOperands();
262 /// replaceAllUsesWith - Replace all uses of debug info referenced by
263 /// this descriptor. After this completes, the current debug info value
265 void DIDerivedType::replaceAllUsesWith(DIDescriptor &D) {
269 // Since we use a TrackingVH for the node, its easy for clients to manufacture
270 // legitimate situations where they want to replaceAllUsesWith() on something
271 // which, due to uniquing, has merged with the source. We shield clients from
272 // this detail by allowing a value to be replaced with replaceAllUsesWith()
274 if (getNode() != D.getNode()) {
275 MDNode *Node = DbgNode;
276 Node->replaceAllUsesWith(D.getNode());
281 /// Verify - Verify that a compile unit is well formed.
282 bool DICompileUnit::Verify() const {
285 StringRef N = getFilename();
288 // It is possible that directory and produce string is empty.
292 /// Verify - Verify that a type descriptor is well formed.
293 bool DIType::Verify() const {
296 if (!getContext().Verify())
299 DICompileUnit CU = getCompileUnit();
305 /// Verify - Verify that a composite type descriptor is well formed.
306 bool DICompositeType::Verify() const {
309 if (!getContext().Verify())
312 DICompileUnit CU = getCompileUnit();
318 /// Verify - Verify that a subprogram descriptor is well formed.
319 bool DISubprogram::Verify() const {
323 if (!getContext().Verify())
326 DICompileUnit CU = getCompileUnit();
330 DICompositeType Ty = getType();
336 /// Verify - Verify that a global variable descriptor is well formed.
337 bool DIGlobalVariable::Verify() const {
341 if (getDisplayName().empty())
344 if (!getContext().Verify())
347 DICompileUnit CU = getCompileUnit();
351 DIType Ty = getType();
361 /// Verify - Verify that a variable descriptor is well formed.
362 bool DIVariable::Verify() const {
366 if (!getContext().Verify())
369 DIType Ty = getType();
376 /// Verify - Verify that a location descriptor is well formed.
377 bool DILocation::Verify() const {
381 return DbgNode->getNumOperands() == 4;
384 /// getOriginalTypeSize - If this type is derived from a base type then
385 /// return base type size.
386 uint64_t DIDerivedType::getOriginalTypeSize() const {
387 unsigned Tag = getTag();
388 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
389 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
390 Tag == dwarf::DW_TAG_restrict_type) {
391 DIType BaseType = getTypeDerivedFrom();
392 // If this type is not derived from any type then take conservative
394 if (!BaseType.isValid())
395 return getSizeInBits();
396 if (BaseType.isDerivedType())
397 return DIDerivedType(BaseType.getNode()).getOriginalTypeSize();
399 return BaseType.getSizeInBits();
402 return getSizeInBits();
405 /// describes - Return true if this subprogram provides debugging
406 /// information for the function F.
407 bool DISubprogram::describes(const Function *F) {
408 assert(F && "Invalid function");
409 StringRef Name = getLinkageName();
412 if (F->getName() == Name)
417 StringRef DIScope::getFilename() const {
420 if (isLexicalBlock())
421 return DILexicalBlock(DbgNode).getFilename();
423 return DISubprogram(DbgNode).getFilename();
425 return DICompileUnit(DbgNode).getFilename();
427 return DINameSpace(DbgNode).getFilename();
429 return DIType(DbgNode).getFilename();
431 return DIFile(DbgNode).getFilename();
432 assert(0 && "Invalid DIScope!");
436 StringRef DIScope::getDirectory() const {
439 if (isLexicalBlock())
440 return DILexicalBlock(DbgNode).getDirectory();
442 return DISubprogram(DbgNode).getDirectory();
444 return DICompileUnit(DbgNode).getDirectory();
446 return DINameSpace(DbgNode).getDirectory();
448 return DIType(DbgNode).getDirectory();
450 return DIFile(DbgNode).getDirectory();
451 assert(0 && "Invalid DIScope!");
455 //===----------------------------------------------------------------------===//
456 // DIDescriptor: dump routines for all descriptors.
457 //===----------------------------------------------------------------------===//
460 /// dump - Print descriptor.
461 void DIDescriptor::dump() const {
462 dbgs() << "[" << dwarf::TagString(getTag()) << "] ";
463 dbgs().write_hex((intptr_t) &*DbgNode) << ']';
466 /// dump - Print compile unit.
467 void DICompileUnit::dump() const {
469 dbgs() << " [" << dwarf::LanguageString(getLanguage()) << "] ";
471 dbgs() << " [" << getDirectory() << "/" << getFilename() << " ]";
474 /// dump - Print type.
475 void DIType::dump() const {
476 if (!DbgNode) return;
478 StringRef Res = getName();
480 dbgs() << " [" << Res << "] ";
482 unsigned Tag = getTag();
483 dbgs() << " [" << dwarf::TagString(Tag) << "] ";
485 // TODO : Print context
486 getCompileUnit().dump();
488 << getLineNumber() << ", "
489 << getSizeInBits() << ", "
490 << getAlignInBits() << ", "
495 dbgs() << " [private] ";
496 else if (isProtected())
497 dbgs() << " [protected] ";
503 DIBasicType(DbgNode).dump();
504 else if (isDerivedType())
505 DIDerivedType(DbgNode).dump();
506 else if (isCompositeType())
507 DICompositeType(DbgNode).dump();
509 dbgs() << "Invalid DIType\n";
516 /// dump - Print basic type.
517 void DIBasicType::dump() const {
518 dbgs() << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
521 /// dump - Print derived type.
522 void DIDerivedType::dump() const {
523 dbgs() << "\n\t Derived From: "; getTypeDerivedFrom().dump();
526 /// dump - Print composite type.
527 void DICompositeType::dump() const {
528 DIArray A = getTypeArray();
529 dbgs() << " [" << A.getNumElements() << " elements]";
532 /// dump - Print global.
533 void DIGlobal::dump() const {
534 StringRef Res = getName();
536 dbgs() << " [" << Res << "] ";
538 unsigned Tag = getTag();
539 dbgs() << " [" << dwarf::TagString(Tag) << "] ";
541 // TODO : Print context
542 getCompileUnit().dump();
543 dbgs() << " [" << getLineNumber() << "] ";
546 dbgs() << " [local] ";
551 if (isGlobalVariable())
552 DIGlobalVariable(DbgNode).dump();
557 /// dump - Print subprogram.
558 void DISubprogram::dump() const {
559 StringRef Res = getName();
561 dbgs() << " [" << Res << "] ";
563 unsigned Tag = getTag();
564 dbgs() << " [" << dwarf::TagString(Tag) << "] ";
566 // TODO : Print context
567 getCompileUnit().dump();
568 dbgs() << " [" << getLineNumber() << "] ";
571 dbgs() << " [local] ";
579 /// dump - Print global variable.
580 void DIGlobalVariable::dump() const {
586 /// dump - Print variable.
587 void DIVariable::dump() const {
588 StringRef Res = getName();
590 dbgs() << " [" << Res << "] ";
592 getCompileUnit().dump();
593 dbgs() << " [" << getLineNumber() << "] ";
597 // FIXME: Dump complex addresses
600 //===----------------------------------------------------------------------===//
601 // DIFactory: Basic Helpers
602 //===----------------------------------------------------------------------===//
604 DIFactory::DIFactory(Module &m)
605 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
607 Constant *DIFactory::GetTagConstant(unsigned TAG) {
608 assert((TAG & LLVMDebugVersionMask) == 0 &&
609 "Tag too large for debug encoding!");
610 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
613 //===----------------------------------------------------------------------===//
614 // DIFactory: Primary Constructors
615 //===----------------------------------------------------------------------===//
617 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
618 /// This implicitly uniques the arrays created.
619 DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
620 SmallVector<Value*, 16> Elts;
623 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
625 for (unsigned i = 0; i != NumTys; ++i)
626 Elts.push_back(Tys[i].getNode());
628 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
631 /// GetOrCreateSubrange - Create a descriptor for a value range. This
632 /// implicitly uniques the values returned.
633 DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
635 GetTagConstant(dwarf::DW_TAG_subrange_type),
636 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
637 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
640 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
645 /// CreateCompileUnit - Create a new descriptor for the specified compile
646 /// unit. Note that this does not unique compile units within the module.
647 DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
654 unsigned RunTimeVer) {
656 GetTagConstant(dwarf::DW_TAG_compile_unit),
657 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
658 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
659 MDString::get(VMContext, Filename),
660 MDString::get(VMContext, Directory),
661 MDString::get(VMContext, Producer),
662 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
663 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
664 MDString::get(VMContext, Flags),
665 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
668 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
671 /// CreateFile - Create a new descriptor for the specified file.
672 DIFile DIFactory::CreateFile(StringRef Filename,
676 GetTagConstant(dwarf::DW_TAG_file_type),
677 MDString::get(VMContext, Filename),
678 MDString::get(VMContext, Directory),
682 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
685 /// CreateEnumerator - Create a single enumerator value.
686 DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
688 GetTagConstant(dwarf::DW_TAG_enumerator),
689 MDString::get(VMContext, Name),
690 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
692 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
696 /// CreateBasicType - Create a basic type like int, float, etc.
697 DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
702 uint64_t AlignInBits,
703 uint64_t OffsetInBits, unsigned Flags,
706 GetTagConstant(dwarf::DW_TAG_base_type),
708 MDString::get(VMContext, Name),
710 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
711 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
712 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
713 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
714 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
715 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
717 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
721 /// CreateBasicType - Create a basic type like int, float, etc.
722 DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
726 Constant *SizeInBits,
727 Constant *AlignInBits,
728 Constant *OffsetInBits, unsigned Flags,
731 GetTagConstant(dwarf::DW_TAG_base_type),
733 MDString::get(VMContext, Name),
735 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
739 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
740 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
742 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
745 /// CreateArtificialType - Create a new DIType with "artificial" flag set.
746 DIType DIFactory::CreateArtificialType(DIType Ty) {
747 if (Ty.isArtificial())
750 SmallVector<Value *, 9> Elts;
751 MDNode *N = Ty.getNode();
752 assert (N && "Unexpected input DIType!");
753 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
754 if (Value *V = N->getOperand(i))
757 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
760 unsigned CurFlags = Ty.getFlags();
761 CurFlags = CurFlags | DIType::FlagArtificial;
763 // Flags are stored at this slot.
764 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
766 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
769 /// CreateDerivedType - Create a derived type like const qualified type,
770 /// pointer, typedef, etc.
771 DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
772 DIDescriptor Context,
777 uint64_t AlignInBits,
778 uint64_t OffsetInBits,
780 DIType DerivedFrom) {
784 MDString::get(VMContext, Name),
786 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
787 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
788 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
789 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
790 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
791 DerivedFrom.getNode(),
793 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
797 /// CreateDerivedType - Create a derived type like const qualified type,
798 /// pointer, typedef, etc.
799 DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
800 DIDescriptor Context,
804 Constant *SizeInBits,
805 Constant *AlignInBits,
806 Constant *OffsetInBits,
808 DIType DerivedFrom) {
812 MDString::get(VMContext, Name),
814 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
818 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
819 DerivedFrom.getNode(),
821 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
825 /// CreateCompositeType - Create a composite type like array, struct, etc.
826 DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
827 DIDescriptor Context,
832 uint64_t AlignInBits,
833 uint64_t OffsetInBits,
837 unsigned RuntimeLang,
838 MDNode *ContainingType) {
843 MDString::get(VMContext, Name),
845 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
846 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
847 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
848 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
849 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
850 DerivedFrom.getNode(),
852 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
855 return DICompositeType(MDNode::get(VMContext, &Elts[0], 13));
859 /// CreateCompositeType - Create a composite type like array, struct, etc.
860 DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
861 DIDescriptor Context,
865 Constant *SizeInBits,
866 Constant *AlignInBits,
867 Constant *OffsetInBits,
871 unsigned RuntimeLang) {
876 MDString::get(VMContext, Name),
878 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
882 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
883 DerivedFrom.getNode(),
885 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
887 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
891 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
892 /// See comments in DISubprogram for descriptions of these fields. This
893 /// method does not unique the generated descriptors.
894 DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
896 StringRef DisplayName,
897 StringRef LinkageName,
899 unsigned LineNo, DIType Ty,
902 unsigned VK, unsigned VIndex,
903 DIType ContainingType,
907 GetTagConstant(dwarf::DW_TAG_subprogram),
908 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
910 MDString::get(VMContext, Name),
911 MDString::get(VMContext, DisplayName),
912 MDString::get(VMContext, LinkageName),
914 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
916 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
917 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
918 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
919 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
920 ContainingType.getNode(),
921 ConstantInt::get(Type::getInt1Ty(VMContext), isArtificial)
923 return DISubprogram(MDNode::get(VMContext, &Elts[0], 15));
926 /// CreateSubprogramDefinition - Create new subprogram descriptor for the
927 /// given declaration.
928 DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration) {
929 if (SPDeclaration.isDefinition())
930 return DISubprogram(SPDeclaration.getNode());
932 MDNode *DeclNode = SPDeclaration.getNode();
934 GetTagConstant(dwarf::DW_TAG_subprogram),
935 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
936 DeclNode->getOperand(2), // Context
937 DeclNode->getOperand(3), // Name
938 DeclNode->getOperand(4), // DisplayName
939 DeclNode->getOperand(5), // LinkageName
940 DeclNode->getOperand(6), // CompileUnit
941 DeclNode->getOperand(7), // LineNo
942 DeclNode->getOperand(8), // Type
943 DeclNode->getOperand(9), // isLocalToUnit
944 ConstantInt::get(Type::getInt1Ty(VMContext), true),
945 DeclNode->getOperand(11), // Virtuality
946 DeclNode->getOperand(12), // VIndex
947 DeclNode->getOperand(13), // Containting Type
948 DeclNode->getOperand(14) // isArtificial
950 return DISubprogram(MDNode::get(VMContext, &Elts[0], 15));
953 /// CreateGlobalVariable - Create a new descriptor for the specified global.
955 DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
956 StringRef DisplayName,
957 StringRef LinkageName,
959 unsigned LineNo, DIType Ty,bool isLocalToUnit,
960 bool isDefinition, llvm::GlobalVariable *Val) {
962 GetTagConstant(dwarf::DW_TAG_variable),
963 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
965 MDString::get(VMContext, Name),
966 MDString::get(VMContext, DisplayName),
967 MDString::get(VMContext, LinkageName),
969 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
971 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
972 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
976 Value *const *Vs = &Elts[0];
977 MDNode *Node = MDNode::get(VMContext,Vs, 12);
979 // Create a named metadata so that we do not lose this mdnode.
980 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
981 NMD->addOperand(Node);
983 return DIGlobalVariable(Node);
987 /// CreateVariable - Create a new descriptor for the specified variable.
988 DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
996 MDString::get(VMContext, Name),
998 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1001 return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
1005 /// CreateComplexVariable - Create a new descriptor for the specified variable
1006 /// which has a complex address expression for its address.
1007 DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
1008 const std::string &Name,
1012 SmallVector<Value *, 9> &addr) {
1013 SmallVector<Value *, 9> Elts;
1014 Elts.push_back(GetTagConstant(Tag));
1015 Elts.push_back(Context.getNode());
1016 Elts.push_back(MDString::get(VMContext, Name));
1017 Elts.push_back(F.getNode());
1018 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
1019 Elts.push_back(Ty.getNode());
1020 Elts.insert(Elts.end(), addr.begin(), addr.end());
1022 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
1026 /// CreateBlock - This creates a descriptor for a lexical block with the
1027 /// specified parent VMContext.
1028 DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
1029 unsigned LineNo, unsigned Col) {
1031 GetTagConstant(dwarf::DW_TAG_lexical_block),
1033 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1034 ConstantInt::get(Type::getInt32Ty(VMContext), Col)
1036 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 4));
1039 /// CreateNameSpace - This creates new descriptor for a namespace
1040 /// with the specified parent context.
1041 DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
1045 GetTagConstant(dwarf::DW_TAG_namespace),
1047 MDString::get(VMContext, Name),
1049 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1051 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1054 /// CreateLocation - Creates a debug info location.
1055 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1056 DIScope S, DILocation OrigLoc) {
1058 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1059 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1063 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1066 /// CreateLocation - Creates a debug info location.
1067 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1068 DIScope S, MDNode *OrigLoc) {
1070 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1071 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1075 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1078 //===----------------------------------------------------------------------===//
1079 // DIFactory: Routines for inserting code into a function
1080 //===----------------------------------------------------------------------===//
1082 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1083 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1084 Instruction *InsertBefore) {
1085 assert(Storage && "no storage passed to dbg.declare");
1086 assert(D.getNode() && "empty DIVariable passed to dbg.declare");
1088 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1090 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1092 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
1095 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1096 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1097 BasicBlock *InsertAtEnd) {
1098 assert(Storage && "no storage passed to dbg.declare");
1099 assert(D.getNode() && "empty DIVariable passed to dbg.declare");
1101 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1103 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1106 // If this block already has a terminator then insert this intrinsic
1107 // before the terminator.
1108 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1109 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1111 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
1113 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1114 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1116 Instruction *InsertBefore) {
1117 assert(V && "no value passed to dbg.value");
1118 assert(D.getNode() && "empty DIVariable passed to dbg.value");
1120 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1122 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1123 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1125 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1128 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1129 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1131 BasicBlock *InsertAtEnd) {
1132 assert(V && "no value passed to dbg.value");
1133 assert(D.getNode() && "empty DIVariable passed to dbg.value");
1135 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1137 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1138 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1140 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1143 //===----------------------------------------------------------------------===//
1144 // DebugInfoFinder implementations.
1145 //===----------------------------------------------------------------------===//
1147 /// processModule - Process entire module and collect debug info.
1148 void DebugInfoFinder::processModule(Module &M) {
1149 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1150 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1151 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1153 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI)) {
1154 processDeclare(DDI);
1158 DebugLoc Loc = BI->getDebugLoc();
1159 if (Loc.isUnknown())
1162 LLVMContext &Ctx = BI->getContext();
1163 DIDescriptor Scope(Loc.getScope(Ctx));
1165 if (Scope.isCompileUnit())
1166 addCompileUnit(DICompileUnit(Scope.getNode()));
1167 else if (Scope.isSubprogram())
1168 processSubprogram(DISubprogram(Scope.getNode()));
1169 else if (Scope.isLexicalBlock())
1170 processLexicalBlock(DILexicalBlock(Scope.getNode()));
1172 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1173 processLocation(DILocation(IA));
1176 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
1180 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1181 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1182 if (addGlobalVariable(DIG)) {
1183 addCompileUnit(DIG.getCompileUnit());
1184 processType(DIG.getType());
1189 /// processLocation - Process DILocation.
1190 void DebugInfoFinder::processLocation(DILocation Loc) {
1191 if (!Loc.Verify()) return;
1192 DIDescriptor S(Loc.getScope().getNode());
1193 if (S.isCompileUnit())
1194 addCompileUnit(DICompileUnit(S.getNode()));
1195 else if (S.isSubprogram())
1196 processSubprogram(DISubprogram(S.getNode()));
1197 else if (S.isLexicalBlock())
1198 processLexicalBlock(DILexicalBlock(S.getNode()));
1199 processLocation(Loc.getOrigLocation());
1202 /// processType - Process DIType.
1203 void DebugInfoFinder::processType(DIType DT) {
1207 addCompileUnit(DT.getCompileUnit());
1208 if (DT.isCompositeType()) {
1209 DICompositeType DCT(DT.getNode());
1210 processType(DCT.getTypeDerivedFrom());
1211 DIArray DA = DCT.getTypeArray();
1212 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1213 DIDescriptor D = DA.getElement(i);
1215 processType(DIType(D.getNode()));
1216 else if (D.isSubprogram())
1217 processSubprogram(DISubprogram(D.getNode()));
1219 } else if (DT.isDerivedType()) {
1220 DIDerivedType DDT(DT.getNode());
1221 processType(DDT.getTypeDerivedFrom());
1225 /// processLexicalBlock
1226 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1227 DIScope Context = LB.getContext();
1228 if (Context.isLexicalBlock())
1229 return processLexicalBlock(DILexicalBlock(Context.getNode()));
1231 return processSubprogram(DISubprogram(Context.getNode()));
1234 /// processSubprogram - Process DISubprogram.
1235 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1236 if (!addSubprogram(SP))
1238 addCompileUnit(SP.getCompileUnit());
1239 processType(SP.getType());
1242 /// processDeclare - Process DbgDeclareInst.
1243 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
1244 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1248 if (!DV.isVariable())
1251 if (!NodesSeen.insert(DV.getNode()))
1254 addCompileUnit(DIVariable(N).getCompileUnit());
1255 processType(DIVariable(N).getType());
1258 /// addType - Add type into Tys.
1259 bool DebugInfoFinder::addType(DIType DT) {
1263 if (!NodesSeen.insert(DT.getNode()))
1266 TYs.push_back(DT.getNode());
1270 /// addCompileUnit - Add compile unit into CUs.
1271 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1275 if (!NodesSeen.insert(CU.getNode()))
1278 CUs.push_back(CU.getNode());
1282 /// addGlobalVariable - Add global variable into GVs.
1283 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1284 if (!DIDescriptor(DIG.getNode()).isGlobalVariable())
1287 if (!NodesSeen.insert(DIG.getNode()))
1290 GVs.push_back(DIG.getNode());
1294 // addSubprogram - Add subprgoram into SPs.
1295 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1296 if (!DIDescriptor(SP.getNode()).isSubprogram())
1299 if (!NodesSeen.insert(SP.getNode()))
1302 SPs.push_back(SP.getNode());
1306 /// Find the debug info descriptor corresponding to this global variable.
1307 static Value *findDbgGlobalDeclare(GlobalVariable *V) {
1308 const Module *M = V->getParent();
1309 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1313 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1314 DIDescriptor DIG(cast_or_null<MDNode>(NMD->getOperand(i)));
1315 if (!DIG.isGlobalVariable())
1317 if (DIGlobalVariable(DIG.getNode()).getGlobal() == V)
1318 return DIG.getNode();
1323 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1324 /// It looks through pointer casts too.
1325 static const DbgDeclareInst *findDbgDeclare(const Value *V) {
1326 V = V->stripPointerCasts();
1328 if (!isa<Instruction>(V) && !isa<Argument>(V))
1331 const Function *F = NULL;
1332 if (const Instruction *I = dyn_cast<Instruction>(V))
1333 F = I->getParent()->getParent();
1334 else if (const Argument *A = dyn_cast<Argument>(V))
1337 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1338 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1340 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1341 if (DDI->getAddress() == V)
1347 bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1348 std::string &Type, unsigned &LineNo,
1349 std::string &File, std::string &Dir) {
1353 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1354 Value *DIGV = findDbgGlobalDeclare(GV);
1355 if (!DIGV) return false;
1356 DIGlobalVariable Var(cast<MDNode>(DIGV));
1358 StringRef D = Var.getDisplayName();
1361 LineNo = Var.getLineNumber();
1362 Unit = Var.getCompileUnit();
1363 TypeD = Var.getType();
1365 const DbgDeclareInst *DDI = findDbgDeclare(V);
1366 if (!DDI) return false;
1367 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1369 StringRef D = Var.getName();
1372 LineNo = Var.getLineNumber();
1373 Unit = Var.getCompileUnit();
1374 TypeD = Var.getType();
1377 StringRef T = TypeD.getName();
1380 StringRef F = Unit.getFilename();
1383 StringRef D = Unit.getDirectory();
1389 /// getDISubprogram - Find subprogram that is enclosing this scope.
1390 DISubprogram llvm::getDISubprogram(MDNode *Scope) {
1391 DIDescriptor D(Scope);
1392 if (D.isSubprogram())
1393 return DISubprogram(Scope);
1395 if (D.isLexicalBlock())
1396 return getDISubprogram(DILexicalBlock(Scope).getContext().getNode());
1398 return DISubprogram();
1401 /// getDICompositeType - Find underlying composite type.
1402 DICompositeType llvm::getDICompositeType(DIType T) {
1403 if (T.isCompositeType())
1404 return DICompositeType(T.getNode());
1406 if (T.isDerivedType())
1407 return getDICompositeType(DIDerivedType(T.getNode()).getTypeDerivedFrom());
1409 return DICompositeType();