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 /// isInlinedFnArgument - Return trule if this variable provides debugging
406 /// information for an inlined function arguments.
407 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
408 assert(CurFn && "Invalid function");
409 if (!getContext().isSubprogram())
411 // This variable is not inlined function argument if its scope
412 // does not describe current function.
413 return !(DISubprogram(getContext().getNode()).describes(CurFn));
416 /// describes - Return true if this subprogram provides debugging
417 /// information for the function F.
418 bool DISubprogram::describes(const Function *F) {
419 assert(F && "Invalid function");
420 StringRef Name = getLinkageName();
423 if (F->getName() == Name)
428 StringRef DIScope::getFilename() const {
431 if (isLexicalBlock())
432 return DILexicalBlock(DbgNode).getFilename();
434 return DISubprogram(DbgNode).getFilename();
436 return DICompileUnit(DbgNode).getFilename();
438 return DINameSpace(DbgNode).getFilename();
440 return DIType(DbgNode).getFilename();
442 return DIFile(DbgNode).getFilename();
443 assert(0 && "Invalid DIScope!");
447 StringRef DIScope::getDirectory() const {
450 if (isLexicalBlock())
451 return DILexicalBlock(DbgNode).getDirectory();
453 return DISubprogram(DbgNode).getDirectory();
455 return DICompileUnit(DbgNode).getDirectory();
457 return DINameSpace(DbgNode).getDirectory();
459 return DIType(DbgNode).getDirectory();
461 return DIFile(DbgNode).getDirectory();
462 assert(0 && "Invalid DIScope!");
466 //===----------------------------------------------------------------------===//
467 // DIDescriptor: dump routines for all descriptors.
468 //===----------------------------------------------------------------------===//
471 /// dump - Print descriptor.
472 void DIDescriptor::dump() const {
473 dbgs() << "[" << dwarf::TagString(getTag()) << "] ";
474 dbgs().write_hex((intptr_t) &*DbgNode) << ']';
477 /// dump - Print compile unit.
478 void DICompileUnit::dump() const {
480 dbgs() << " [" << dwarf::LanguageString(getLanguage()) << "] ";
482 dbgs() << " [" << getDirectory() << "/" << getFilename() << " ]";
485 /// dump - Print type.
486 void DIType::dump() const {
487 if (!DbgNode) return;
489 StringRef Res = getName();
491 dbgs() << " [" << Res << "] ";
493 unsigned Tag = getTag();
494 dbgs() << " [" << dwarf::TagString(Tag) << "] ";
496 // TODO : Print context
497 getCompileUnit().dump();
499 << getLineNumber() << ", "
500 << getSizeInBits() << ", "
501 << getAlignInBits() << ", "
506 dbgs() << " [private] ";
507 else if (isProtected())
508 dbgs() << " [protected] ";
514 DIBasicType(DbgNode).dump();
515 else if (isDerivedType())
516 DIDerivedType(DbgNode).dump();
517 else if (isCompositeType())
518 DICompositeType(DbgNode).dump();
520 dbgs() << "Invalid DIType\n";
527 /// dump - Print basic type.
528 void DIBasicType::dump() const {
529 dbgs() << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
532 /// dump - Print derived type.
533 void DIDerivedType::dump() const {
534 dbgs() << "\n\t Derived From: "; getTypeDerivedFrom().dump();
537 /// dump - Print composite type.
538 void DICompositeType::dump() const {
539 DIArray A = getTypeArray();
540 dbgs() << " [" << A.getNumElements() << " elements]";
543 /// dump - Print global.
544 void DIGlobal::dump() const {
545 StringRef Res = getName();
547 dbgs() << " [" << Res << "] ";
549 unsigned Tag = getTag();
550 dbgs() << " [" << dwarf::TagString(Tag) << "] ";
552 // TODO : Print context
553 getCompileUnit().dump();
554 dbgs() << " [" << getLineNumber() << "] ";
557 dbgs() << " [local] ";
562 if (isGlobalVariable())
563 DIGlobalVariable(DbgNode).dump();
568 /// dump - Print subprogram.
569 void DISubprogram::dump() const {
570 StringRef Res = getName();
572 dbgs() << " [" << Res << "] ";
574 unsigned Tag = getTag();
575 dbgs() << " [" << dwarf::TagString(Tag) << "] ";
577 // TODO : Print context
578 getCompileUnit().dump();
579 dbgs() << " [" << getLineNumber() << "] ";
582 dbgs() << " [local] ";
590 /// dump - Print global variable.
591 void DIGlobalVariable::dump() const {
597 /// dump - Print variable.
598 void DIVariable::dump() const {
599 StringRef Res = getName();
601 dbgs() << " [" << Res << "] ";
603 getCompileUnit().dump();
604 dbgs() << " [" << getLineNumber() << "] ";
608 // FIXME: Dump complex addresses
611 //===----------------------------------------------------------------------===//
612 // DIFactory: Basic Helpers
613 //===----------------------------------------------------------------------===//
615 DIFactory::DIFactory(Module &m)
616 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
618 Constant *DIFactory::GetTagConstant(unsigned TAG) {
619 assert((TAG & LLVMDebugVersionMask) == 0 &&
620 "Tag too large for debug encoding!");
621 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
624 //===----------------------------------------------------------------------===//
625 // DIFactory: Primary Constructors
626 //===----------------------------------------------------------------------===//
628 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
629 /// This implicitly uniques the arrays created.
630 DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
631 SmallVector<Value*, 16> Elts;
634 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
636 for (unsigned i = 0; i != NumTys; ++i)
637 Elts.push_back(Tys[i].getNode());
639 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
642 /// GetOrCreateSubrange - Create a descriptor for a value range. This
643 /// implicitly uniques the values returned.
644 DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
646 GetTagConstant(dwarf::DW_TAG_subrange_type),
647 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
648 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
651 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
656 /// CreateCompileUnit - Create a new descriptor for the specified compile
657 /// unit. Note that this does not unique compile units within the module.
658 DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
665 unsigned RunTimeVer) {
667 GetTagConstant(dwarf::DW_TAG_compile_unit),
668 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
669 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
670 MDString::get(VMContext, Filename),
671 MDString::get(VMContext, Directory),
672 MDString::get(VMContext, Producer),
673 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
674 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
675 MDString::get(VMContext, Flags),
676 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
679 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
682 /// CreateFile - Create a new descriptor for the specified file.
683 DIFile DIFactory::CreateFile(StringRef Filename,
687 GetTagConstant(dwarf::DW_TAG_file_type),
688 MDString::get(VMContext, Filename),
689 MDString::get(VMContext, Directory),
693 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
696 /// CreateEnumerator - Create a single enumerator value.
697 DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
699 GetTagConstant(dwarf::DW_TAG_enumerator),
700 MDString::get(VMContext, Name),
701 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
703 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
707 /// CreateBasicType - Create a basic type like int, float, etc.
708 DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
713 uint64_t AlignInBits,
714 uint64_t OffsetInBits, unsigned Flags,
717 GetTagConstant(dwarf::DW_TAG_base_type),
719 MDString::get(VMContext, Name),
721 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
722 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
723 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
724 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
725 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
726 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
728 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
732 /// CreateBasicType - Create a basic type like int, float, etc.
733 DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
737 Constant *SizeInBits,
738 Constant *AlignInBits,
739 Constant *OffsetInBits, unsigned Flags,
742 GetTagConstant(dwarf::DW_TAG_base_type),
744 MDString::get(VMContext, Name),
746 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
750 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
751 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
753 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
756 /// CreateArtificialType - Create a new DIType with "artificial" flag set.
757 DIType DIFactory::CreateArtificialType(DIType Ty) {
758 if (Ty.isArtificial())
761 SmallVector<Value *, 9> Elts;
762 MDNode *N = Ty.getNode();
763 assert (N && "Unexpected input DIType!");
764 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
765 if (Value *V = N->getOperand(i))
768 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
771 unsigned CurFlags = Ty.getFlags();
772 CurFlags = CurFlags | DIType::FlagArtificial;
774 // Flags are stored at this slot.
775 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
777 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
780 /// CreateDerivedType - Create a derived type like const qualified type,
781 /// pointer, typedef, etc.
782 DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
783 DIDescriptor Context,
788 uint64_t AlignInBits,
789 uint64_t OffsetInBits,
791 DIType DerivedFrom) {
795 MDString::get(VMContext, Name),
797 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
798 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
799 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
800 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
801 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
802 DerivedFrom.getNode(),
804 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
808 /// CreateDerivedType - Create a derived type like const qualified type,
809 /// pointer, typedef, etc.
810 DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
811 DIDescriptor Context,
815 Constant *SizeInBits,
816 Constant *AlignInBits,
817 Constant *OffsetInBits,
819 DIType DerivedFrom) {
823 MDString::get(VMContext, Name),
825 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
829 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
830 DerivedFrom.getNode(),
832 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
836 /// CreateCompositeType - Create a composite type like array, struct, etc.
837 DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
838 DIDescriptor Context,
843 uint64_t AlignInBits,
844 uint64_t OffsetInBits,
848 unsigned RuntimeLang,
849 MDNode *ContainingType) {
854 MDString::get(VMContext, Name),
856 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
857 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
858 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
859 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
860 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
861 DerivedFrom.getNode(),
863 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
866 return DICompositeType(MDNode::get(VMContext, &Elts[0], 13));
870 /// CreateCompositeType - Create a composite type like array, struct, etc.
871 DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
872 DIDescriptor Context,
876 Constant *SizeInBits,
877 Constant *AlignInBits,
878 Constant *OffsetInBits,
882 unsigned RuntimeLang) {
887 MDString::get(VMContext, Name),
889 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
893 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
894 DerivedFrom.getNode(),
896 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
898 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
902 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
903 /// See comments in DISubprogram for descriptions of these fields. This
904 /// method does not unique the generated descriptors.
905 DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
907 StringRef DisplayName,
908 StringRef LinkageName,
910 unsigned LineNo, DIType Ty,
913 unsigned VK, unsigned VIndex,
914 DIType ContainingType,
918 GetTagConstant(dwarf::DW_TAG_subprogram),
919 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
921 MDString::get(VMContext, Name),
922 MDString::get(VMContext, DisplayName),
923 MDString::get(VMContext, LinkageName),
925 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
927 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
928 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
929 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
930 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
931 ContainingType.getNode(),
932 ConstantInt::get(Type::getInt1Ty(VMContext), isArtificial)
934 return DISubprogram(MDNode::get(VMContext, &Elts[0], 15));
937 /// CreateSubprogramDefinition - Create new subprogram descriptor for the
938 /// given declaration.
939 DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration) {
940 if (SPDeclaration.isDefinition())
941 return DISubprogram(SPDeclaration.getNode());
943 MDNode *DeclNode = SPDeclaration.getNode();
945 GetTagConstant(dwarf::DW_TAG_subprogram),
946 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
947 DeclNode->getOperand(2), // Context
948 DeclNode->getOperand(3), // Name
949 DeclNode->getOperand(4), // DisplayName
950 DeclNode->getOperand(5), // LinkageName
951 DeclNode->getOperand(6), // CompileUnit
952 DeclNode->getOperand(7), // LineNo
953 DeclNode->getOperand(8), // Type
954 DeclNode->getOperand(9), // isLocalToUnit
955 ConstantInt::get(Type::getInt1Ty(VMContext), true),
956 DeclNode->getOperand(11), // Virtuality
957 DeclNode->getOperand(12), // VIndex
958 DeclNode->getOperand(13), // Containting Type
959 DeclNode->getOperand(14) // isArtificial
961 return DISubprogram(MDNode::get(VMContext, &Elts[0], 15));
964 /// CreateGlobalVariable - Create a new descriptor for the specified global.
966 DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
967 StringRef DisplayName,
968 StringRef LinkageName,
970 unsigned LineNo, DIType Ty,bool isLocalToUnit,
971 bool isDefinition, llvm::GlobalVariable *Val) {
973 GetTagConstant(dwarf::DW_TAG_variable),
974 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
976 MDString::get(VMContext, Name),
977 MDString::get(VMContext, DisplayName),
978 MDString::get(VMContext, LinkageName),
980 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
982 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
983 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
987 Value *const *Vs = &Elts[0];
988 MDNode *Node = MDNode::get(VMContext,Vs, 12);
990 // Create a named metadata so that we do not lose this mdnode.
991 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
992 NMD->addOperand(Node);
994 return DIGlobalVariable(Node);
998 /// CreateVariable - Create a new descriptor for the specified variable.
999 DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
1005 GetTagConstant(Tag),
1007 MDString::get(VMContext, Name),
1009 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1012 return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
1016 /// CreateComplexVariable - Create a new descriptor for the specified variable
1017 /// which has a complex address expression for its address.
1018 DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
1019 const std::string &Name,
1023 SmallVector<Value *, 9> &addr) {
1024 SmallVector<Value *, 9> Elts;
1025 Elts.push_back(GetTagConstant(Tag));
1026 Elts.push_back(Context.getNode());
1027 Elts.push_back(MDString::get(VMContext, Name));
1028 Elts.push_back(F.getNode());
1029 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
1030 Elts.push_back(Ty.getNode());
1031 Elts.insert(Elts.end(), addr.begin(), addr.end());
1033 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
1037 /// CreateBlock - This creates a descriptor for a lexical block with the
1038 /// specified parent VMContext.
1039 DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
1040 unsigned LineNo, unsigned Col) {
1042 GetTagConstant(dwarf::DW_TAG_lexical_block),
1044 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1045 ConstantInt::get(Type::getInt32Ty(VMContext), Col)
1047 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 4));
1050 /// CreateNameSpace - This creates new descriptor for a namespace
1051 /// with the specified parent context.
1052 DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
1056 GetTagConstant(dwarf::DW_TAG_namespace),
1058 MDString::get(VMContext, Name),
1060 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1062 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1065 /// CreateLocation - Creates a debug info location.
1066 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1067 DIScope S, DILocation OrigLoc) {
1069 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1070 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1074 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1077 /// CreateLocation - Creates a debug info location.
1078 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1079 DIScope S, MDNode *OrigLoc) {
1081 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1082 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1086 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1089 //===----------------------------------------------------------------------===//
1090 // DIFactory: Routines for inserting code into a function
1091 //===----------------------------------------------------------------------===//
1093 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1094 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1095 Instruction *InsertBefore) {
1096 assert(Storage && "no storage passed to dbg.declare");
1097 assert(D.getNode() && "empty DIVariable passed to dbg.declare");
1099 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1101 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1103 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
1106 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1107 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1108 BasicBlock *InsertAtEnd) {
1109 assert(Storage && "no storage passed to dbg.declare");
1110 assert(D.getNode() && "empty DIVariable passed to dbg.declare");
1112 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1114 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1117 // If this block already has a terminator then insert this intrinsic
1118 // before the terminator.
1119 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1120 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1122 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
1124 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1125 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1127 Instruction *InsertBefore) {
1128 assert(V && "no value passed to dbg.value");
1129 assert(D.getNode() && "empty DIVariable passed to dbg.value");
1131 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1133 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1134 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1136 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1139 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1140 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1142 BasicBlock *InsertAtEnd) {
1143 assert(V && "no value passed to dbg.value");
1144 assert(D.getNode() && "empty DIVariable passed to dbg.value");
1146 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1148 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1149 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1151 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1154 //===----------------------------------------------------------------------===//
1155 // DebugInfoFinder implementations.
1156 //===----------------------------------------------------------------------===//
1158 /// processModule - Process entire module and collect debug info.
1159 void DebugInfoFinder::processModule(Module &M) {
1160 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1161 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1162 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1164 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI)) {
1165 processDeclare(DDI);
1169 DebugLoc Loc = BI->getDebugLoc();
1170 if (Loc.isUnknown())
1173 LLVMContext &Ctx = BI->getContext();
1174 DIDescriptor Scope(Loc.getScope(Ctx));
1176 if (Scope.isCompileUnit())
1177 addCompileUnit(DICompileUnit(Scope.getNode()));
1178 else if (Scope.isSubprogram())
1179 processSubprogram(DISubprogram(Scope.getNode()));
1180 else if (Scope.isLexicalBlock())
1181 processLexicalBlock(DILexicalBlock(Scope.getNode()));
1183 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1184 processLocation(DILocation(IA));
1187 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
1191 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1192 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1193 if (addGlobalVariable(DIG)) {
1194 addCompileUnit(DIG.getCompileUnit());
1195 processType(DIG.getType());
1200 /// processLocation - Process DILocation.
1201 void DebugInfoFinder::processLocation(DILocation Loc) {
1202 if (!Loc.Verify()) return;
1203 DIDescriptor S(Loc.getScope().getNode());
1204 if (S.isCompileUnit())
1205 addCompileUnit(DICompileUnit(S.getNode()));
1206 else if (S.isSubprogram())
1207 processSubprogram(DISubprogram(S.getNode()));
1208 else if (S.isLexicalBlock())
1209 processLexicalBlock(DILexicalBlock(S.getNode()));
1210 processLocation(Loc.getOrigLocation());
1213 /// processType - Process DIType.
1214 void DebugInfoFinder::processType(DIType DT) {
1218 addCompileUnit(DT.getCompileUnit());
1219 if (DT.isCompositeType()) {
1220 DICompositeType DCT(DT.getNode());
1221 processType(DCT.getTypeDerivedFrom());
1222 DIArray DA = DCT.getTypeArray();
1223 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1224 DIDescriptor D = DA.getElement(i);
1226 processType(DIType(D.getNode()));
1227 else if (D.isSubprogram())
1228 processSubprogram(DISubprogram(D.getNode()));
1230 } else if (DT.isDerivedType()) {
1231 DIDerivedType DDT(DT.getNode());
1232 processType(DDT.getTypeDerivedFrom());
1236 /// processLexicalBlock
1237 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1238 DIScope Context = LB.getContext();
1239 if (Context.isLexicalBlock())
1240 return processLexicalBlock(DILexicalBlock(Context.getNode()));
1242 return processSubprogram(DISubprogram(Context.getNode()));
1245 /// processSubprogram - Process DISubprogram.
1246 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1247 if (!addSubprogram(SP))
1249 addCompileUnit(SP.getCompileUnit());
1250 processType(SP.getType());
1253 /// processDeclare - Process DbgDeclareInst.
1254 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
1255 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1259 if (!DV.isVariable())
1262 if (!NodesSeen.insert(DV.getNode()))
1265 addCompileUnit(DIVariable(N).getCompileUnit());
1266 processType(DIVariable(N).getType());
1269 /// addType - Add type into Tys.
1270 bool DebugInfoFinder::addType(DIType DT) {
1274 if (!NodesSeen.insert(DT.getNode()))
1277 TYs.push_back(DT.getNode());
1281 /// addCompileUnit - Add compile unit into CUs.
1282 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1286 if (!NodesSeen.insert(CU.getNode()))
1289 CUs.push_back(CU.getNode());
1293 /// addGlobalVariable - Add global variable into GVs.
1294 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1295 if (!DIDescriptor(DIG.getNode()).isGlobalVariable())
1298 if (!NodesSeen.insert(DIG.getNode()))
1301 GVs.push_back(DIG.getNode());
1305 // addSubprogram - Add subprgoram into SPs.
1306 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1307 if (!DIDescriptor(SP.getNode()).isSubprogram())
1310 if (!NodesSeen.insert(SP.getNode()))
1313 SPs.push_back(SP.getNode());
1317 /// Find the debug info descriptor corresponding to this global variable.
1318 static Value *findDbgGlobalDeclare(GlobalVariable *V) {
1319 const Module *M = V->getParent();
1320 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1324 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1325 DIDescriptor DIG(cast_or_null<MDNode>(NMD->getOperand(i)));
1326 if (!DIG.isGlobalVariable())
1328 if (DIGlobalVariable(DIG.getNode()).getGlobal() == V)
1329 return DIG.getNode();
1334 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1335 /// It looks through pointer casts too.
1336 static const DbgDeclareInst *findDbgDeclare(const Value *V) {
1337 V = V->stripPointerCasts();
1339 if (!isa<Instruction>(V) && !isa<Argument>(V))
1342 const Function *F = NULL;
1343 if (const Instruction *I = dyn_cast<Instruction>(V))
1344 F = I->getParent()->getParent();
1345 else if (const Argument *A = dyn_cast<Argument>(V))
1348 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1349 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1351 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1352 if (DDI->getAddress() == V)
1358 bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1359 std::string &Type, unsigned &LineNo,
1360 std::string &File, std::string &Dir) {
1364 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1365 Value *DIGV = findDbgGlobalDeclare(GV);
1366 if (!DIGV) return false;
1367 DIGlobalVariable Var(cast<MDNode>(DIGV));
1369 StringRef D = Var.getDisplayName();
1372 LineNo = Var.getLineNumber();
1373 Unit = Var.getCompileUnit();
1374 TypeD = Var.getType();
1376 const DbgDeclareInst *DDI = findDbgDeclare(V);
1377 if (!DDI) return false;
1378 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1380 StringRef D = Var.getName();
1383 LineNo = Var.getLineNumber();
1384 Unit = Var.getCompileUnit();
1385 TypeD = Var.getType();
1388 StringRef T = TypeD.getName();
1391 StringRef F = Unit.getFilename();
1394 StringRef D = Unit.getDirectory();
1400 /// getDISubprogram - Find subprogram that is enclosing this scope.
1401 DISubprogram llvm::getDISubprogram(MDNode *Scope) {
1402 DIDescriptor D(Scope);
1403 if (D.isSubprogram())
1404 return DISubprogram(Scope);
1406 if (D.isLexicalBlock())
1407 return getDISubprogram(DILexicalBlock(Scope).getContext().getNode());
1409 return DISubprogram();
1412 /// getDICompositeType - Find underlying composite type.
1413 DICompositeType llvm::getDICompositeType(DIType T) {
1414 if (T.isCompositeType())
1415 return DICompositeType(T.getNode());
1417 if (T.isDerivedType())
1418 return getDICompositeType(DIDerivedType(T.getNode()).getTypeDerivedFrom());
1420 return DICompositeType();