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)
72 DIDescriptor::DIDescriptor(MDNode *N, unsigned RequiredTag) {
75 // If this is non-null, check to see if the Tag matches. If not, set to null.
76 if (N && getTag() != RequiredTag) {
82 DIDescriptor::getStringField(unsigned Elt) const {
86 if (Elt < DbgNode->getNumOperands())
87 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
88 return MDS->getString();
93 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
97 if (Elt < DbgNode->getNumOperands())
98 if (ConstantInt *CI = dyn_cast<ConstantInt>(DbgNode->getOperand(Elt)))
99 return CI->getZExtValue();
104 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
106 return DIDescriptor();
108 if (Elt < DbgNode->getNumOperands() && DbgNode->getOperand(Elt))
109 return DIDescriptor(dyn_cast<MDNode>(DbgNode->getOperand(Elt)));
111 return DIDescriptor();
114 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
118 if (Elt < DbgNode->getNumOperands())
119 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
123 unsigned DIVariable::getNumAddrElements() const {
124 return DbgNode->getNumOperands()-6;
128 //===----------------------------------------------------------------------===//
130 //===----------------------------------------------------------------------===//
132 /// isBasicType - Return true if the specified tag is legal for
134 bool DIDescriptor::isBasicType() const {
135 assert(!isNull() && "Invalid descriptor!");
136 return getTag() == dwarf::DW_TAG_base_type;
139 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
140 bool DIDescriptor::isDerivedType() const {
141 assert(!isNull() && "Invalid descriptor!");
143 case dwarf::DW_TAG_typedef:
144 case dwarf::DW_TAG_pointer_type:
145 case dwarf::DW_TAG_reference_type:
146 case dwarf::DW_TAG_const_type:
147 case dwarf::DW_TAG_volatile_type:
148 case dwarf::DW_TAG_restrict_type:
149 case dwarf::DW_TAG_member:
150 case dwarf::DW_TAG_inheritance:
153 // CompositeTypes are currently modelled as DerivedTypes.
154 return isCompositeType();
158 /// isCompositeType - Return true if the specified tag is legal for
160 bool DIDescriptor::isCompositeType() const {
161 assert(!isNull() && "Invalid descriptor!");
163 case dwarf::DW_TAG_array_type:
164 case dwarf::DW_TAG_structure_type:
165 case dwarf::DW_TAG_union_type:
166 case dwarf::DW_TAG_enumeration_type:
167 case dwarf::DW_TAG_vector_type:
168 case dwarf::DW_TAG_subroutine_type:
169 case dwarf::DW_TAG_class_type:
176 /// isVariable - Return true if the specified tag is legal for DIVariable.
177 bool DIDescriptor::isVariable() const {
178 assert(!isNull() && "Invalid descriptor!");
180 case dwarf::DW_TAG_auto_variable:
181 case dwarf::DW_TAG_arg_variable:
182 case dwarf::DW_TAG_return_variable:
189 /// isType - Return true if the specified tag is legal for DIType.
190 bool DIDescriptor::isType() const {
191 return isBasicType() || isCompositeType() || isDerivedType();
194 /// isSubprogram - Return true if the specified tag is legal for
196 bool DIDescriptor::isSubprogram() const {
197 assert(!isNull() && "Invalid descriptor!");
198 return getTag() == dwarf::DW_TAG_subprogram;
201 /// isGlobalVariable - Return true if the specified tag is legal for
202 /// DIGlobalVariable.
203 bool DIDescriptor::isGlobalVariable() const {
204 assert(!isNull() && "Invalid descriptor!");
205 return getTag() == dwarf::DW_TAG_variable;
208 /// isGlobal - Return true if the specified tag is legal for DIGlobal.
209 bool DIDescriptor::isGlobal() const {
210 return isGlobalVariable();
213 /// isScope - Return true if the specified tag is one of the scope
215 bool DIDescriptor::isScope() const {
216 assert(!isNull() && "Invalid descriptor!");
218 case dwarf::DW_TAG_compile_unit:
219 case dwarf::DW_TAG_lexical_block:
220 case dwarf::DW_TAG_subprogram:
221 case dwarf::DW_TAG_namespace:
229 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
230 bool DIDescriptor::isCompileUnit() const {
231 assert(!isNull() && "Invalid descriptor!");
232 return getTag() == dwarf::DW_TAG_compile_unit;
235 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
236 bool DIDescriptor::isNameSpace() const {
237 assert(!isNull() && "Invalid descriptor!");
238 return getTag() == dwarf::DW_TAG_namespace;
241 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
242 bool DIDescriptor::isLexicalBlock() const {
243 assert(!isNull() && "Invalid descriptor!");
244 return getTag() == dwarf::DW_TAG_lexical_block;
247 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
248 bool DIDescriptor::isSubrange() const {
249 assert(!isNull() && "Invalid descriptor!");
250 return getTag() == dwarf::DW_TAG_subrange_type;
253 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
254 bool DIDescriptor::isEnumerator() const {
255 assert(!isNull() && "Invalid descriptor!");
256 return getTag() == dwarf::DW_TAG_enumerator;
259 //===----------------------------------------------------------------------===//
260 // Simple Descriptor Constructors and other Methods
261 //===----------------------------------------------------------------------===//
263 DIType::DIType(MDNode *N) : DIDescriptor(N) {
265 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
270 unsigned DIArray::getNumElements() const {
271 assert(DbgNode && "Invalid DIArray");
272 return DbgNode->getNumOperands();
275 /// replaceAllUsesWith - Replace all uses of debug info referenced by
276 /// this descriptor. After this completes, the current debug info value
278 void DIDerivedType::replaceAllUsesWith(DIDescriptor &D) {
282 assert(!D.isNull() && "Can not replace with null");
284 // Since we use a TrackingVH for the node, its easy for clients to manufacture
285 // legitimate situations where they want to replaceAllUsesWith() on something
286 // which, due to uniquing, has merged with the source. We shield clients from
287 // this detail by allowing a value to be replaced with replaceAllUsesWith()
289 if (getNode() != D.getNode()) {
290 MDNode *Node = DbgNode;
291 Node->replaceAllUsesWith(D.getNode());
296 /// Verify - Verify that a compile unit is well formed.
297 bool DICompileUnit::Verify() const {
300 StringRef N = getFilename();
303 // It is possible that directory and produce string is empty.
307 /// Verify - Verify that a type descriptor is well formed.
308 bool DIType::Verify() const {
311 if (getContext().isNull())
314 DICompileUnit CU = getCompileUnit();
315 if (!CU.isNull() && !CU.Verify())
320 /// Verify - Verify that a composite type descriptor is well formed.
321 bool DICompositeType::Verify() const {
324 if (getContext().isNull())
327 DICompileUnit CU = getCompileUnit();
328 if (!CU.isNull() && !CU.Verify())
333 /// Verify - Verify that a subprogram descriptor is well formed.
334 bool DISubprogram::Verify() const {
338 if (getContext().isNull())
341 DICompileUnit CU = getCompileUnit();
345 DICompositeType Ty = getType();
346 if (!Ty.isNull() && !Ty.Verify())
351 /// Verify - Verify that a global variable descriptor is well formed.
352 bool DIGlobalVariable::Verify() const {
356 if (getDisplayName().empty())
359 if (getContext().isNull())
362 DICompileUnit CU = getCompileUnit();
363 if (!CU.isNull() && !CU.Verify())
366 DIType Ty = getType();
376 /// Verify - Verify that a variable descriptor is well formed.
377 bool DIVariable::Verify() const {
381 if (getContext().isNull())
384 DIType Ty = getType();
391 /// getOriginalTypeSize - If this type is derived from a base type then
392 /// return base type size.
393 uint64_t DIDerivedType::getOriginalTypeSize() const {
394 unsigned Tag = getTag();
395 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
396 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
397 Tag == dwarf::DW_TAG_restrict_type) {
398 DIType BaseType = getTypeDerivedFrom();
399 // If this type is not derived from any type then take conservative
401 if (BaseType.isNull())
402 return getSizeInBits();
403 if (BaseType.isDerivedType())
404 return DIDerivedType(BaseType.getNode()).getOriginalTypeSize();
406 return BaseType.getSizeInBits();
409 return getSizeInBits();
412 /// describes - Return true if this subprogram provides debugging
413 /// information for the function F.
414 bool DISubprogram::describes(const Function *F) {
415 assert(F && "Invalid function");
416 StringRef Name = getLinkageName();
419 if (F->getName() == Name)
424 StringRef DIScope::getFilename() const {
425 if (isLexicalBlock())
426 return DILexicalBlock(DbgNode).getFilename();
428 return DISubprogram(DbgNode).getFilename();
430 return DICompileUnit(DbgNode).getFilename();
432 return DINameSpace(DbgNode).getFilename();
433 assert(0 && "Invalid DIScope!");
437 StringRef DIScope::getDirectory() const {
438 if (isLexicalBlock())
439 return DILexicalBlock(DbgNode).getDirectory();
441 return DISubprogram(DbgNode).getDirectory();
443 return DICompileUnit(DbgNode).getDirectory();
445 return DINameSpace(DbgNode).getDirectory();
446 assert(0 && "Invalid DIScope!");
450 //===----------------------------------------------------------------------===//
451 // DIDescriptor: dump routines for all descriptors.
452 //===----------------------------------------------------------------------===//
455 /// dump - Print descriptor.
456 void DIDescriptor::dump() const {
457 dbgs() << "[" << dwarf::TagString(getTag()) << "] ";
458 dbgs().write_hex((intptr_t) &*DbgNode) << ']';
461 /// dump - Print compile unit.
462 void DICompileUnit::dump() const {
464 dbgs() << " [" << dwarf::LanguageString(getLanguage()) << "] ";
466 dbgs() << " [" << getDirectory() << "/" << getFilename() << " ]";
469 /// dump - Print type.
470 void DIType::dump() const {
471 if (isNull()) return;
473 StringRef Res = getName();
475 dbgs() << " [" << Res << "] ";
477 unsigned Tag = getTag();
478 dbgs() << " [" << dwarf::TagString(Tag) << "] ";
480 // TODO : Print context
481 getCompileUnit().dump();
483 << getLineNumber() << ", "
484 << getSizeInBits() << ", "
485 << getAlignInBits() << ", "
490 dbgs() << " [private] ";
491 else if (isProtected())
492 dbgs() << " [protected] ";
498 DIBasicType(DbgNode).dump();
499 else if (isDerivedType())
500 DIDerivedType(DbgNode).dump();
501 else if (isCompositeType())
502 DICompositeType(DbgNode).dump();
504 dbgs() << "Invalid DIType\n";
511 /// dump - Print basic type.
512 void DIBasicType::dump() const {
513 dbgs() << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
516 /// dump - Print derived type.
517 void DIDerivedType::dump() const {
518 dbgs() << "\n\t Derived From: "; getTypeDerivedFrom().dump();
521 /// dump - Print composite type.
522 void DICompositeType::dump() const {
523 DIArray A = getTypeArray();
526 dbgs() << " [" << A.getNumElements() << " elements]";
529 /// dump - Print global.
530 void DIGlobal::dump() const {
531 StringRef Res = getName();
533 dbgs() << " [" << Res << "] ";
535 unsigned Tag = getTag();
536 dbgs() << " [" << dwarf::TagString(Tag) << "] ";
538 // TODO : Print context
539 getCompileUnit().dump();
540 dbgs() << " [" << getLineNumber() << "] ";
543 dbgs() << " [local] ";
548 if (isGlobalVariable())
549 DIGlobalVariable(DbgNode).dump();
554 /// dump - Print subprogram.
555 void DISubprogram::dump() const {
556 StringRef Res = getName();
558 dbgs() << " [" << Res << "] ";
560 unsigned Tag = getTag();
561 dbgs() << " [" << dwarf::TagString(Tag) << "] ";
563 // TODO : Print context
564 getCompileUnit().dump();
565 dbgs() << " [" << getLineNumber() << "] ";
568 dbgs() << " [local] ";
576 /// dump - Print global variable.
577 void DIGlobalVariable::dump() const {
583 /// dump - Print variable.
584 void DIVariable::dump() const {
585 StringRef Res = getName();
587 dbgs() << " [" << Res << "] ";
589 getCompileUnit().dump();
590 dbgs() << " [" << getLineNumber() << "] ";
594 // FIXME: Dump complex addresses
597 //===----------------------------------------------------------------------===//
598 // DIFactory: Basic Helpers
599 //===----------------------------------------------------------------------===//
601 DIFactory::DIFactory(Module &m)
602 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
604 Constant *DIFactory::GetTagConstant(unsigned TAG) {
605 assert((TAG & LLVMDebugVersionMask) == 0 &&
606 "Tag too large for debug encoding!");
607 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
610 //===----------------------------------------------------------------------===//
611 // DIFactory: Primary Constructors
612 //===----------------------------------------------------------------------===//
614 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
615 /// This implicitly uniques the arrays created.
616 DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
617 SmallVector<Value*, 16> Elts;
620 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
622 for (unsigned i = 0; i != NumTys; ++i)
623 Elts.push_back(Tys[i].getNode());
625 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
628 /// GetOrCreateSubrange - Create a descriptor for a value range. This
629 /// implicitly uniques the values returned.
630 DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
632 GetTagConstant(dwarf::DW_TAG_subrange_type),
633 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
634 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
637 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
642 /// CreateCompileUnit - Create a new descriptor for the specified compile
643 /// unit. Note that this does not unique compile units within the module.
644 DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
651 unsigned RunTimeVer) {
653 GetTagConstant(dwarf::DW_TAG_compile_unit),
654 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
655 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
656 MDString::get(VMContext, Filename),
657 MDString::get(VMContext, Directory),
658 MDString::get(VMContext, Producer),
659 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
660 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
661 MDString::get(VMContext, Flags),
662 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
665 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
668 /// CreateEnumerator - Create a single enumerator value.
669 DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
671 GetTagConstant(dwarf::DW_TAG_enumerator),
672 MDString::get(VMContext, Name),
673 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
675 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
679 /// CreateBasicType - Create a basic type like int, float, etc.
680 DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
682 DICompileUnit CompileUnit,
685 uint64_t AlignInBits,
686 uint64_t OffsetInBits, unsigned Flags,
689 GetTagConstant(dwarf::DW_TAG_base_type),
691 MDString::get(VMContext, Name),
692 CompileUnit.getNode(),
693 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
694 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
695 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
696 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
697 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
698 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
700 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
704 /// CreateBasicType - Create a basic type like int, float, etc.
705 DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
707 DICompileUnit CompileUnit,
709 Constant *SizeInBits,
710 Constant *AlignInBits,
711 Constant *OffsetInBits, unsigned Flags,
714 GetTagConstant(dwarf::DW_TAG_base_type),
716 MDString::get(VMContext, Name),
717 CompileUnit.getNode(),
718 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
722 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
723 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
725 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
728 /// CreateArtificialType - Create a new DIType with "artificial" flag set.
729 DIType DIFactory::CreateArtificialType(DIType Ty) {
730 if (Ty.isArtificial())
733 SmallVector<Value *, 9> Elts;
734 MDNode *N = Ty.getNode();
735 assert (N && "Unexpected input DIType!");
736 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
737 if (Value *V = N->getOperand(i))
740 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
743 unsigned CurFlags = Ty.getFlags();
744 CurFlags = CurFlags | DIType::FlagArtificial;
746 // Flags are stored at this slot.
747 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
749 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
752 /// CreateDerivedType - Create a derived type like const qualified type,
753 /// pointer, typedef, etc.
754 DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
755 DIDescriptor Context,
757 DICompileUnit CompileUnit,
760 uint64_t AlignInBits,
761 uint64_t OffsetInBits,
763 DIType DerivedFrom) {
767 MDString::get(VMContext, Name),
768 CompileUnit.getNode(),
769 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
770 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
771 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
772 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
773 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
774 DerivedFrom.getNode(),
776 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
780 /// CreateDerivedType - Create a derived type like const qualified type,
781 /// pointer, typedef, etc.
782 DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
783 DIDescriptor Context,
785 DICompileUnit CompileUnit,
787 Constant *SizeInBits,
788 Constant *AlignInBits,
789 Constant *OffsetInBits,
791 DIType DerivedFrom) {
795 MDString::get(VMContext, Name),
796 CompileUnit.getNode(),
797 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
801 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
802 DerivedFrom.getNode(),
804 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
808 /// CreateCompositeType - Create a composite type like array, struct, etc.
809 DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
810 DIDescriptor Context,
812 DICompileUnit CompileUnit,
815 uint64_t AlignInBits,
816 uint64_t OffsetInBits,
820 unsigned RuntimeLang,
821 MDNode *ContainingType) {
826 MDString::get(VMContext, Name),
827 CompileUnit.getNode(),
828 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
829 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
830 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
831 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
832 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
833 DerivedFrom.getNode(),
835 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
838 return DICompositeType(MDNode::get(VMContext, &Elts[0], 13));
842 /// CreateCompositeType - Create a composite type like array, struct, etc.
843 DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
844 DIDescriptor Context,
846 DICompileUnit CompileUnit,
848 Constant *SizeInBits,
849 Constant *AlignInBits,
850 Constant *OffsetInBits,
854 unsigned RuntimeLang) {
859 MDString::get(VMContext, Name),
860 CompileUnit.getNode(),
861 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
865 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
866 DerivedFrom.getNode(),
868 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
870 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
874 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
875 /// See comments in DISubprogram for descriptions of these fields. This
876 /// method does not unique the generated descriptors.
877 DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
879 StringRef DisplayName,
880 StringRef LinkageName,
881 DICompileUnit CompileUnit,
882 unsigned LineNo, DIType Ty,
885 unsigned VK, unsigned VIndex,
886 DIType ContainingType,
890 GetTagConstant(dwarf::DW_TAG_subprogram),
891 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
893 MDString::get(VMContext, Name),
894 MDString::get(VMContext, DisplayName),
895 MDString::get(VMContext, LinkageName),
896 CompileUnit.getNode(),
897 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
899 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
900 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
901 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
902 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
903 ContainingType.getNode(),
904 ConstantInt::get(Type::getInt1Ty(VMContext), isArtificial)
906 return DISubprogram(MDNode::get(VMContext, &Elts[0], 15));
909 /// CreateSubprogramDefinition - Create new subprogram descriptor for the
910 /// given declaration.
911 DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration) {
912 if (SPDeclaration.isDefinition())
913 return DISubprogram(SPDeclaration.getNode());
915 MDNode *DeclNode = SPDeclaration.getNode();
917 GetTagConstant(dwarf::DW_TAG_subprogram),
918 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
919 DeclNode->getOperand(2), // Context
920 DeclNode->getOperand(3), // Name
921 DeclNode->getOperand(4), // DisplayName
922 DeclNode->getOperand(5), // LinkageName
923 DeclNode->getOperand(6), // CompileUnit
924 DeclNode->getOperand(7), // LineNo
925 DeclNode->getOperand(8), // Type
926 DeclNode->getOperand(9), // isLocalToUnit
927 ConstantInt::get(Type::getInt1Ty(VMContext), true),
928 DeclNode->getOperand(11), // Virtuality
929 DeclNode->getOperand(12), // VIndex
930 DeclNode->getOperand(13), // Containting Type
931 DeclNode->getOperand(14) // isArtificial
933 return DISubprogram(MDNode::get(VMContext, &Elts[0], 15));
936 /// CreateGlobalVariable - Create a new descriptor for the specified global.
938 DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
939 StringRef DisplayName,
940 StringRef LinkageName,
941 DICompileUnit CompileUnit,
942 unsigned LineNo, DIType Ty,bool isLocalToUnit,
943 bool isDefinition, llvm::GlobalVariable *Val) {
945 GetTagConstant(dwarf::DW_TAG_variable),
946 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
948 MDString::get(VMContext, Name),
949 MDString::get(VMContext, DisplayName),
950 MDString::get(VMContext, LinkageName),
951 CompileUnit.getNode(),
952 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
954 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
955 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
959 Value *const *Vs = &Elts[0];
960 MDNode *Node = MDNode::get(VMContext,Vs, 12);
962 // Create a named metadata so that we do not lose this mdnode.
963 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
964 NMD->addOperand(Node);
966 return DIGlobalVariable(Node);
970 /// CreateVariable - Create a new descriptor for the specified variable.
971 DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
973 DICompileUnit CompileUnit, unsigned LineNo,
978 MDString::get(VMContext, Name),
979 CompileUnit.getNode(),
980 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
983 return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
987 /// CreateComplexVariable - Create a new descriptor for the specified variable
988 /// which has a complex address expression for its address.
989 DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
990 const std::string &Name,
991 DICompileUnit CompileUnit,
994 SmallVector<Value *, 9> &addr) {
995 SmallVector<Value *, 9> Elts;
996 Elts.push_back(GetTagConstant(Tag));
997 Elts.push_back(Context.getNode());
998 Elts.push_back(MDString::get(VMContext, Name));
999 Elts.push_back(CompileUnit.getNode());
1000 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
1001 Elts.push_back(Ty.getNode());
1002 Elts.insert(Elts.end(), addr.begin(), addr.end());
1004 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
1008 /// CreateBlock - This creates a descriptor for a lexical block with the
1009 /// specified parent VMContext.
1010 DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
1011 unsigned LineNo, unsigned Col) {
1013 GetTagConstant(dwarf::DW_TAG_lexical_block),
1015 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1016 ConstantInt::get(Type::getInt32Ty(VMContext), Col)
1018 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 4));
1021 /// CreateNameSpace - This creates new descriptor for a namespace
1022 /// with the specified parent context.
1023 DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
1024 DICompileUnit CompileUnit,
1027 GetTagConstant(dwarf::DW_TAG_namespace),
1029 MDString::get(VMContext, Name),
1030 CompileUnit.getNode(),
1031 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1033 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1036 /// CreateLocation - Creates a debug info location.
1037 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1038 DIScope S, DILocation OrigLoc) {
1040 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1041 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1045 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1048 /// CreateLocation - Creates a debug info location.
1049 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1050 DIScope S, MDNode *OrigLoc) {
1052 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1053 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1057 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1060 //===----------------------------------------------------------------------===//
1061 // DIFactory: Routines for inserting code into a function
1062 //===----------------------------------------------------------------------===//
1064 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1065 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1066 Instruction *InsertBefore) {
1067 assert(Storage && "no storage passed to dbg.declare");
1068 assert(D.getNode() && "empty DIVariable passed to dbg.declare");
1070 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1072 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1074 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
1077 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1078 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1079 BasicBlock *InsertAtEnd) {
1080 assert(Storage && "no storage passed to dbg.declare");
1081 assert(D.getNode() && "empty DIVariable passed to dbg.declare");
1083 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1085 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1088 // If this block already has a terminator then insert this intrinsic
1089 // before the terminator.
1090 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1091 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1093 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
1095 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1096 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1098 Instruction *InsertBefore) {
1099 assert(V && "no value passed to dbg.value");
1100 assert(D.getNode() && "empty DIVariable passed to dbg.value");
1102 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1104 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1105 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1107 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1110 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1111 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1113 BasicBlock *InsertAtEnd) {
1114 assert(V && "no value passed to dbg.value");
1115 assert(D.getNode() && "empty DIVariable passed to dbg.value");
1117 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1119 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1120 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1122 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1125 //===----------------------------------------------------------------------===//
1126 // DebugInfoFinder implementations.
1127 //===----------------------------------------------------------------------===//
1129 /// processModule - Process entire module and collect debug info.
1130 void DebugInfoFinder::processModule(Module &M) {
1131 unsigned MDDbgKind = M.getMDKindID("dbg");
1133 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1134 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1135 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1137 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1138 processDeclare(DDI);
1139 else if (MDNode *L = BI->getMetadata(MDDbgKind))
1140 processLocation(DILocation(L));
1143 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
1147 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1148 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1149 if (addGlobalVariable(DIG)) {
1150 addCompileUnit(DIG.getCompileUnit());
1151 processType(DIG.getType());
1156 /// processLocation - Process DILocation.
1157 void DebugInfoFinder::processLocation(DILocation Loc) {
1158 if (Loc.isNull()) return;
1159 DIScope S(Loc.getScope().getNode());
1160 if (S.isNull()) return;
1161 if (S.isCompileUnit())
1162 addCompileUnit(DICompileUnit(S.getNode()));
1163 else if (S.isSubprogram())
1164 processSubprogram(DISubprogram(S.getNode()));
1165 else if (S.isLexicalBlock())
1166 processLexicalBlock(DILexicalBlock(S.getNode()));
1167 processLocation(Loc.getOrigLocation());
1170 /// processType - Process DIType.
1171 void DebugInfoFinder::processType(DIType DT) {
1175 addCompileUnit(DT.getCompileUnit());
1176 if (DT.isCompositeType()) {
1177 DICompositeType DCT(DT.getNode());
1178 processType(DCT.getTypeDerivedFrom());
1179 DIArray DA = DCT.getTypeArray();
1181 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1182 DIDescriptor D = DA.getElement(i);
1183 DIType TyE = DIType(D.getNode());
1187 processSubprogram(DISubprogram(D.getNode()));
1189 } else if (DT.isDerivedType()) {
1190 DIDerivedType DDT(DT.getNode());
1192 processType(DDT.getTypeDerivedFrom());
1196 /// processLexicalBlock
1197 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1200 DIScope Context = LB.getContext();
1201 if (Context.isLexicalBlock())
1202 return processLexicalBlock(DILexicalBlock(Context.getNode()));
1204 return processSubprogram(DISubprogram(Context.getNode()));
1207 /// processSubprogram - Process DISubprogram.
1208 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1211 if (!addSubprogram(SP))
1213 addCompileUnit(SP.getCompileUnit());
1214 processType(SP.getType());
1217 /// processDeclare - Process DbgDeclareInst.
1218 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
1219 DIVariable DV(cast<MDNode>(DDI->getVariable()));
1223 if (!NodesSeen.insert(DV.getNode()))
1226 addCompileUnit(DV.getCompileUnit());
1227 processType(DV.getType());
1230 /// addType - Add type into Tys.
1231 bool DebugInfoFinder::addType(DIType DT) {
1235 if (!NodesSeen.insert(DT.getNode()))
1238 TYs.push_back(DT.getNode());
1242 /// addCompileUnit - Add compile unit into CUs.
1243 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1247 if (!NodesSeen.insert(CU.getNode()))
1250 CUs.push_back(CU.getNode());
1254 /// addGlobalVariable - Add global variable into GVs.
1255 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1259 if (!NodesSeen.insert(DIG.getNode()))
1262 GVs.push_back(DIG.getNode());
1266 // addSubprogram - Add subprgoram into SPs.
1267 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1271 if (!NodesSeen.insert(SP.getNode()))
1274 SPs.push_back(SP.getNode());
1278 /// Find the debug info descriptor corresponding to this global variable.
1279 static Value *findDbgGlobalDeclare(GlobalVariable *V) {
1280 const Module *M = V->getParent();
1281 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1285 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1286 DIGlobalVariable DIG(cast_or_null<MDNode>(NMD->getOperand(i)));
1289 if (DIG.getGlobal() == V)
1290 return DIG.getNode();
1295 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1296 /// It looks through pointer casts too.
1297 static const DbgDeclareInst *findDbgDeclare(const Value *V) {
1298 V = V->stripPointerCasts();
1300 if (!isa<Instruction>(V) && !isa<Argument>(V))
1303 const Function *F = NULL;
1304 if (const Instruction *I = dyn_cast<Instruction>(V))
1305 F = I->getParent()->getParent();
1306 else if (const Argument *A = dyn_cast<Argument>(V))
1309 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1310 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1312 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1313 if (DDI->getAddress() == V)
1319 bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1320 std::string &Type, unsigned &LineNo,
1321 std::string &File, std::string &Dir) {
1325 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1326 Value *DIGV = findDbgGlobalDeclare(GV);
1327 if (!DIGV) return false;
1328 DIGlobalVariable Var(cast<MDNode>(DIGV));
1330 StringRef D = Var.getDisplayName();
1333 LineNo = Var.getLineNumber();
1334 Unit = Var.getCompileUnit();
1335 TypeD = Var.getType();
1337 const DbgDeclareInst *DDI = findDbgDeclare(V);
1338 if (!DDI) return false;
1339 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1341 StringRef D = Var.getName();
1344 LineNo = Var.getLineNumber();
1345 Unit = Var.getCompileUnit();
1346 TypeD = Var.getType();
1349 StringRef T = TypeD.getName();
1352 StringRef F = Unit.getFilename();
1355 StringRef D = Unit.getDirectory();
1361 /// ExtractDebugLocation - Extract debug location information
1362 /// from DILocation.
1363 DebugLoc llvm::ExtractDebugLocation(DILocation &Loc,
1364 DebugLocTracker &DebugLocInfo) {
1365 DenseMap<MDNode *, unsigned>::iterator II
1366 = DebugLocInfo.DebugIdMap.find(Loc.getNode());
1367 if (II != DebugLocInfo.DebugIdMap.end())
1368 return DebugLoc::get(II->second);
1370 // Add a new location entry.
1371 unsigned Id = DebugLocInfo.DebugLocations.size();
1372 DebugLocInfo.DebugLocations.push_back(Loc.getNode());
1373 DebugLocInfo.DebugIdMap[Loc.getNode()] = Id;
1375 return DebugLoc::get(Id);
1378 /// getDISubprogram - Find subprogram that is enclosing this scope.
1379 DISubprogram llvm::getDISubprogram(MDNode *Scope) {
1380 DIDescriptor D(Scope);
1382 return DISubprogram();
1384 if (D.isCompileUnit())
1385 return DISubprogram();
1387 if (D.isSubprogram())
1388 return DISubprogram(Scope);
1390 if (D.isLexicalBlock())
1391 return getDISubprogram(DILexicalBlock(Scope).getContext().getNode());
1393 return DISubprogram();
1396 /// getDICompositeType - Find underlying composite type.
1397 DICompositeType llvm::getDICompositeType(DIType T) {
1399 return DICompositeType();
1401 if (T.isCompositeType())
1402 return DICompositeType(T.getNode());
1404 if (T.isDerivedType())
1405 return getDICompositeType(DIDerivedType(T.getNode()).getTypeDerivedFrom());
1407 return DICompositeType();