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(const 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<const 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(const 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()
275 MDNode *Node = const_cast<MDNode*>(DbgNode);
276 const MDNode *DN = D;
277 const Value *V = cast_or_null<Value>(DN);
278 Node->replaceAllUsesWith(const_cast<Value*>(V));
283 /// Verify - Verify that a compile unit is well formed.
284 bool DICompileUnit::Verify() const {
287 StringRef N = getFilename();
290 // It is possible that directory and produce string is empty.
294 /// Verify - Verify that a type descriptor is well formed.
295 bool DIType::Verify() const {
298 if (!getContext().Verify())
301 DICompileUnit CU = getCompileUnit();
307 /// Verify - Verify that a composite type descriptor is well formed.
308 bool DICompositeType::Verify() const {
311 if (!getContext().Verify())
314 DICompileUnit CU = getCompileUnit();
320 /// Verify - Verify that a subprogram descriptor is well formed.
321 bool DISubprogram::Verify() const {
325 if (!getContext().Verify())
328 DICompileUnit CU = getCompileUnit();
332 DICompositeType Ty = getType();
338 /// Verify - Verify that a global variable descriptor is well formed.
339 bool DIGlobalVariable::Verify() const {
343 if (getDisplayName().empty())
346 if (!getContext().Verify())
349 DICompileUnit CU = getCompileUnit();
353 DIType Ty = getType();
363 /// Verify - Verify that a variable descriptor is well formed.
364 bool DIVariable::Verify() const {
368 if (!getContext().Verify())
371 DIType Ty = getType();
378 /// Verify - Verify that a location descriptor is well formed.
379 bool DILocation::Verify() const {
383 return DbgNode->getNumOperands() == 4;
386 /// getOriginalTypeSize - If this type is derived from a base type then
387 /// return base type size.
388 uint64_t DIDerivedType::getOriginalTypeSize() const {
389 unsigned Tag = getTag();
390 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
391 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
392 Tag == dwarf::DW_TAG_restrict_type) {
393 DIType BaseType = getTypeDerivedFrom();
394 // If this type is not derived from any type then take conservative
396 if (!BaseType.isValid())
397 return getSizeInBits();
398 if (BaseType.isDerivedType())
399 return DIDerivedType(BaseType).getOriginalTypeSize();
401 return BaseType.getSizeInBits();
404 return getSizeInBits();
407 /// isInlinedFnArgument - Return trule if this variable provides debugging
408 /// information for an inlined function arguments.
409 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
410 assert(CurFn && "Invalid function");
411 if (!getContext().isSubprogram())
413 // This variable is not inlined function argument if its scope
414 // does not describe current function.
415 return !(DISubprogram(getContext()).describes(CurFn));
418 /// describes - Return true if this subprogram provides debugging
419 /// information for the function F.
420 bool DISubprogram::describes(const Function *F) {
421 assert(F && "Invalid function");
422 StringRef Name = getLinkageName();
425 if (F->getName() == Name)
430 unsigned DISubprogram::isOptimized() const {
431 assert (DbgNode && "Invalid subprogram descriptor!");
432 if (DbgNode->getNumOperands() == 16)
433 return getUnsignedField(15);
437 StringRef DIScope::getFilename() const {
440 if (isLexicalBlock())
441 return DILexicalBlock(DbgNode).getFilename();
443 return DISubprogram(DbgNode).getFilename();
445 return DICompileUnit(DbgNode).getFilename();
447 return DINameSpace(DbgNode).getFilename();
449 return DIType(DbgNode).getFilename();
451 return DIFile(DbgNode).getFilename();
452 assert(0 && "Invalid DIScope!");
456 StringRef DIScope::getDirectory() const {
459 if (isLexicalBlock())
460 return DILexicalBlock(DbgNode).getDirectory();
462 return DISubprogram(DbgNode).getDirectory();
464 return DICompileUnit(DbgNode).getDirectory();
466 return DINameSpace(DbgNode).getDirectory();
468 return DIType(DbgNode).getDirectory();
470 return DIFile(DbgNode).getDirectory();
471 assert(0 && "Invalid DIScope!");
475 //===----------------------------------------------------------------------===//
476 // DIDescriptor: dump routines for all descriptors.
477 //===----------------------------------------------------------------------===//
480 /// print - Print descriptor.
481 void DIDescriptor::print(raw_ostream &OS) const {
482 OS << "[" << dwarf::TagString(getTag()) << "] ";
483 OS.write_hex((intptr_t) &*DbgNode) << ']';
486 /// print - Print compile unit.
487 void DICompileUnit::print(raw_ostream &OS) const {
489 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
491 OS << " [" << getDirectory() << "/" << getFilename() << " ]";
494 /// print - Print type.
495 void DIType::print(raw_ostream &OS) const {
496 if (!DbgNode) return;
498 StringRef Res = getName();
500 OS << " [" << Res << "] ";
502 unsigned Tag = getTag();
503 OS << " [" << dwarf::TagString(Tag) << "] ";
505 // TODO : Print context
506 getCompileUnit().print(OS);
508 << "line " << getLineNumber() << ", "
509 << getSizeInBits() << " bits, "
510 << getAlignInBits() << " bit alignment, "
511 << getOffsetInBits() << " bit offset"
516 else if (isProtected())
517 OS << " [protected] ";
523 DIBasicType(DbgNode).print(OS);
524 else if (isDerivedType())
525 DIDerivedType(DbgNode).print(OS);
526 else if (isCompositeType())
527 DICompositeType(DbgNode).print(OS);
529 OS << "Invalid DIType\n";
536 /// print - Print basic type.
537 void DIBasicType::print(raw_ostream &OS) const {
538 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
541 /// print - Print derived type.
542 void DIDerivedType::print(raw_ostream &OS) const {
543 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
546 /// print - Print composite type.
547 void DICompositeType::print(raw_ostream &OS) const {
548 DIArray A = getTypeArray();
549 OS << " [" << A.getNumElements() << " elements]";
552 /// print - Print global.
553 void DIGlobal::print(raw_ostream &OS) const {
554 StringRef Res = getName();
556 OS << " [" << Res << "] ";
558 unsigned Tag = getTag();
559 OS << " [" << dwarf::TagString(Tag) << "] ";
561 // TODO : Print context
562 getCompileUnit().print(OS);
563 OS << " [" << getLineNumber() << "] ";
571 if (isGlobalVariable())
572 DIGlobalVariable(DbgNode).print(OS);
577 /// print - Print subprogram.
578 void DISubprogram::print(raw_ostream &OS) const {
579 StringRef Res = getName();
581 OS << " [" << Res << "] ";
583 unsigned Tag = getTag();
584 OS << " [" << dwarf::TagString(Tag) << "] ";
586 // TODO : Print context
587 getCompileUnit().print(OS);
588 OS << " [" << getLineNumber() << "] ";
599 /// print - Print global variable.
600 void DIGlobalVariable::print(raw_ostream &OS) const {
602 getGlobal()->print(OS);
606 /// print - Print variable.
607 void DIVariable::print(raw_ostream &OS) const {
608 StringRef Res = getName();
610 OS << " [" << Res << "] ";
612 getCompileUnit().print(OS);
613 OS << " [" << getLineNumber() << "] ";
617 // FIXME: Dump complex addresses
620 /// dump - Print descriptor to dbgs() with a newline.
621 void DIDescriptor::dump() const {
622 print(dbgs()); dbgs() << '\n';
625 /// dump - Print compile unit to dbgs() with a newline.
626 void DICompileUnit::dump() const {
627 print(dbgs()); dbgs() << '\n';
630 /// dump - Print type to dbgs() with a newline.
631 void DIType::dump() const {
632 print(dbgs()); dbgs() << '\n';
635 /// dump - Print basic type to dbgs() with a newline.
636 void DIBasicType::dump() const {
637 print(dbgs()); dbgs() << '\n';
640 /// dump - Print derived type to dbgs() with a newline.
641 void DIDerivedType::dump() const {
642 print(dbgs()); dbgs() << '\n';
645 /// dump - Print composite type to dbgs() with a newline.
646 void DICompositeType::dump() const {
647 print(dbgs()); dbgs() << '\n';
650 /// dump - Print global to dbgs() with a newline.
651 void DIGlobal::dump() const {
652 print(dbgs()); dbgs() << '\n';
655 /// dump - Print subprogram to dbgs() with a newline.
656 void DISubprogram::dump() const {
657 print(dbgs()); dbgs() << '\n';
660 /// dump - Print global variable.
661 void DIGlobalVariable::dump() const {
662 print(dbgs()); dbgs() << '\n';
665 /// dump - Print variable.
666 void DIVariable::dump() const {
667 print(dbgs()); dbgs() << '\n';
670 //===----------------------------------------------------------------------===//
671 // DIFactory: Basic Helpers
672 //===----------------------------------------------------------------------===//
674 DIFactory::DIFactory(Module &m)
675 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
677 Constant *DIFactory::GetTagConstant(unsigned TAG) {
678 assert((TAG & LLVMDebugVersionMask) == 0 &&
679 "Tag too large for debug encoding!");
680 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
683 //===----------------------------------------------------------------------===//
684 // DIFactory: Primary Constructors
685 //===----------------------------------------------------------------------===//
687 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
688 /// This implicitly uniques the arrays created.
689 DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
690 SmallVector<Value*, 16> Elts;
693 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
695 for (unsigned i = 0; i != NumTys; ++i)
696 Elts.push_back(Tys[i]);
698 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
701 /// GetOrCreateSubrange - Create a descriptor for a value range. This
702 /// implicitly uniques the values returned.
703 DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
705 GetTagConstant(dwarf::DW_TAG_subrange_type),
706 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
707 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
710 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
715 /// CreateCompileUnit - Create a new descriptor for the specified compile
716 /// unit. Note that this does not unique compile units within the module.
717 DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
724 unsigned RunTimeVer) {
726 GetTagConstant(dwarf::DW_TAG_compile_unit),
727 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
728 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
729 MDString::get(VMContext, Filename),
730 MDString::get(VMContext, Directory),
731 MDString::get(VMContext, Producer),
732 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
733 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
734 MDString::get(VMContext, Flags),
735 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
738 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
741 /// CreateFile - Create a new descriptor for the specified file.
742 DIFile DIFactory::CreateFile(StringRef Filename,
746 GetTagConstant(dwarf::DW_TAG_file_type),
747 MDString::get(VMContext, Filename),
748 MDString::get(VMContext, Directory),
752 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
755 /// CreateEnumerator - Create a single enumerator value.
756 DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
758 GetTagConstant(dwarf::DW_TAG_enumerator),
759 MDString::get(VMContext, Name),
760 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
762 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
766 /// CreateBasicType - Create a basic type like int, float, etc.
767 DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
772 uint64_t AlignInBits,
773 uint64_t OffsetInBits, unsigned Flags,
776 GetTagConstant(dwarf::DW_TAG_base_type),
778 MDString::get(VMContext, Name),
780 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
781 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
782 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
783 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
784 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
785 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
787 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
791 /// CreateBasicType - Create a basic type like int, float, etc.
792 DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
796 Constant *SizeInBits,
797 Constant *AlignInBits,
798 Constant *OffsetInBits, unsigned Flags,
801 GetTagConstant(dwarf::DW_TAG_base_type),
803 MDString::get(VMContext, Name),
805 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
809 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
810 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
812 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
815 /// CreateArtificialType - Create a new DIType with "artificial" flag set.
816 DIType DIFactory::CreateArtificialType(DIType Ty) {
817 if (Ty.isArtificial())
820 SmallVector<Value *, 9> Elts;
822 assert (N && "Unexpected input DIType!");
823 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
824 if (Value *V = N->getOperand(i))
827 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
830 unsigned CurFlags = Ty.getFlags();
831 CurFlags = CurFlags | DIType::FlagArtificial;
833 // Flags are stored at this slot.
834 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
836 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
839 /// CreateDerivedType - Create a derived type like const qualified type,
840 /// pointer, typedef, etc.
841 DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
842 DIDescriptor Context,
847 uint64_t AlignInBits,
848 uint64_t OffsetInBits,
850 DIType DerivedFrom) {
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),
863 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
867 /// CreateDerivedType - Create a derived type like const qualified type,
868 /// pointer, typedef, etc.
869 DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
870 DIDescriptor Context,
874 Constant *SizeInBits,
875 Constant *AlignInBits,
876 Constant *OffsetInBits,
878 DIType DerivedFrom) {
882 MDString::get(VMContext, Name),
884 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
888 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
891 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
895 /// CreateCompositeType - Create a composite type like array, struct, etc.
896 DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
897 DIDescriptor Context,
902 uint64_t AlignInBits,
903 uint64_t OffsetInBits,
907 unsigned RuntimeLang,
908 MDNode *ContainingType) {
913 MDString::get(VMContext, Name),
915 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
916 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
917 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
918 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
919 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
922 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
925 return DICompositeType(MDNode::get(VMContext, &Elts[0], 13));
929 /// CreateCompositeType - Create a composite type like array, struct, etc.
930 DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
931 DIDescriptor Context,
935 Constant *SizeInBits,
936 Constant *AlignInBits,
937 Constant *OffsetInBits,
941 unsigned RuntimeLang) {
946 MDString::get(VMContext, Name),
948 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
952 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
955 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
957 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
961 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
962 /// See comments in DISubprogram for descriptions of these fields. This
963 /// method does not unique the generated descriptors.
964 DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
966 StringRef DisplayName,
967 StringRef LinkageName,
969 unsigned LineNo, DIType Ty,
972 unsigned VK, unsigned VIndex,
973 DIType ContainingType,
978 GetTagConstant(dwarf::DW_TAG_subprogram),
979 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
981 MDString::get(VMContext, Name),
982 MDString::get(VMContext, DisplayName),
983 MDString::get(VMContext, LinkageName),
985 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
987 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
988 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
989 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
990 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
992 ConstantInt::get(Type::getInt1Ty(VMContext), isArtificial),
993 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized)
995 return DISubprogram(MDNode::get(VMContext, &Elts[0], 16));
998 /// CreateSubprogramDefinition - Create new subprogram descriptor for the
999 /// given declaration.
1000 DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration) {
1001 if (SPDeclaration.isDefinition())
1002 return DISubprogram(SPDeclaration);
1004 MDNode *DeclNode = SPDeclaration;
1006 GetTagConstant(dwarf::DW_TAG_subprogram),
1007 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1008 DeclNode->getOperand(2), // Context
1009 DeclNode->getOperand(3), // Name
1010 DeclNode->getOperand(4), // DisplayName
1011 DeclNode->getOperand(5), // LinkageName
1012 DeclNode->getOperand(6), // CompileUnit
1013 DeclNode->getOperand(7), // LineNo
1014 DeclNode->getOperand(8), // Type
1015 DeclNode->getOperand(9), // isLocalToUnit
1016 ConstantInt::get(Type::getInt1Ty(VMContext), true),
1017 DeclNode->getOperand(11), // Virtuality
1018 DeclNode->getOperand(12), // VIndex
1019 DeclNode->getOperand(13), // Containting Type
1020 DeclNode->getOperand(14), // isArtificial
1021 DeclNode->getOperand(15) // isOptimized
1023 return DISubprogram(MDNode::get(VMContext, &Elts[0], 16));
1026 /// CreateGlobalVariable - Create a new descriptor for the specified global.
1028 DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1029 StringRef DisplayName,
1030 StringRef LinkageName,
1032 unsigned LineNo, DIType Ty,bool isLocalToUnit,
1033 bool isDefinition, llvm::GlobalVariable *Val) {
1035 GetTagConstant(dwarf::DW_TAG_variable),
1036 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1038 MDString::get(VMContext, Name),
1039 MDString::get(VMContext, DisplayName),
1040 MDString::get(VMContext, LinkageName),
1042 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1044 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1045 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1049 Value *const *Vs = &Elts[0];
1050 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1052 // Create a named metadata so that we do not lose this mdnode.
1053 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1054 NMD->addOperand(Node);
1056 return DIGlobalVariable(Node);
1060 /// CreateVariable - Create a new descriptor for the specified variable.
1061 DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
1067 GetTagConstant(Tag),
1069 MDString::get(VMContext, Name),
1071 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1074 return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
1078 /// CreateComplexVariable - Create a new descriptor for the specified variable
1079 /// which has a complex address expression for its address.
1080 DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
1081 const std::string &Name,
1085 SmallVector<Value *, 9> &addr) {
1086 SmallVector<Value *, 9> Elts;
1087 Elts.push_back(GetTagConstant(Tag));
1088 Elts.push_back(Context);
1089 Elts.push_back(MDString::get(VMContext, Name));
1091 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
1093 Elts.insert(Elts.end(), addr.begin(), addr.end());
1095 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
1099 /// CreateBlock - This creates a descriptor for a lexical block with the
1100 /// specified parent VMContext.
1101 DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
1102 unsigned LineNo, unsigned Col) {
1104 GetTagConstant(dwarf::DW_TAG_lexical_block),
1106 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1107 ConstantInt::get(Type::getInt32Ty(VMContext), Col)
1109 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 4));
1112 /// CreateNameSpace - This creates new descriptor for a namespace
1113 /// with the specified parent context.
1114 DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
1118 GetTagConstant(dwarf::DW_TAG_namespace),
1120 MDString::get(VMContext, Name),
1122 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1124 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1127 /// CreateLocation - Creates a debug info location.
1128 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1129 DIScope S, DILocation OrigLoc) {
1131 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1132 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1136 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1139 /// CreateLocation - Creates a debug info location.
1140 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1141 DIScope S, MDNode *OrigLoc) {
1143 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1144 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1148 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1151 //===----------------------------------------------------------------------===//
1152 // DIFactory: Routines for inserting code into a function
1153 //===----------------------------------------------------------------------===//
1155 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1156 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1157 Instruction *InsertBefore) {
1158 assert(Storage && "no storage passed to dbg.declare");
1159 assert(D.Verify() && "empty DIVariable passed to dbg.declare");
1161 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1163 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1165 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
1168 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1169 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1170 BasicBlock *InsertAtEnd) {
1171 assert(Storage && "no storage passed to dbg.declare");
1172 assert(D.Verify() && "invalid DIVariable passed to dbg.declare");
1174 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1176 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1179 // If this block already has a terminator then insert this intrinsic
1180 // before the terminator.
1181 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1182 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1184 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
1186 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1187 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1189 Instruction *InsertBefore) {
1190 assert(V && "no value passed to dbg.value");
1191 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
1193 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1195 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1196 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1198 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1201 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1202 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1204 BasicBlock *InsertAtEnd) {
1205 assert(V && "no value passed to dbg.value");
1206 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
1208 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1210 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1211 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1213 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1216 //===----------------------------------------------------------------------===//
1217 // DebugInfoFinder implementations.
1218 //===----------------------------------------------------------------------===//
1220 /// processModule - Process entire module and collect debug info.
1221 void DebugInfoFinder::processModule(Module &M) {
1222 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1223 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1224 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1226 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1227 processDeclare(DDI);
1229 DebugLoc Loc = BI->getDebugLoc();
1230 if (Loc.isUnknown())
1233 LLVMContext &Ctx = BI->getContext();
1234 DIDescriptor Scope(Loc.getScope(Ctx));
1236 if (Scope.isCompileUnit())
1237 addCompileUnit(DICompileUnit(Scope));
1238 else if (Scope.isSubprogram())
1239 processSubprogram(DISubprogram(Scope));
1240 else if (Scope.isLexicalBlock())
1241 processLexicalBlock(DILexicalBlock(Scope));
1243 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1244 processLocation(DILocation(IA));
1247 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
1251 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1252 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1253 if (addGlobalVariable(DIG)) {
1254 addCompileUnit(DIG.getCompileUnit());
1255 processType(DIG.getType());
1260 /// processLocation - Process DILocation.
1261 void DebugInfoFinder::processLocation(DILocation Loc) {
1262 if (!Loc.Verify()) return;
1263 DIDescriptor S(Loc.getScope());
1264 if (S.isCompileUnit())
1265 addCompileUnit(DICompileUnit(S));
1266 else if (S.isSubprogram())
1267 processSubprogram(DISubprogram(S));
1268 else if (S.isLexicalBlock())
1269 processLexicalBlock(DILexicalBlock(S));
1270 processLocation(Loc.getOrigLocation());
1273 /// processType - Process DIType.
1274 void DebugInfoFinder::processType(DIType DT) {
1278 addCompileUnit(DT.getCompileUnit());
1279 if (DT.isCompositeType()) {
1280 DICompositeType DCT(DT);
1281 processType(DCT.getTypeDerivedFrom());
1282 DIArray DA = DCT.getTypeArray();
1283 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1284 DIDescriptor D = DA.getElement(i);
1286 processType(DIType(D));
1287 else if (D.isSubprogram())
1288 processSubprogram(DISubprogram(D));
1290 } else if (DT.isDerivedType()) {
1291 DIDerivedType DDT(DT);
1292 processType(DDT.getTypeDerivedFrom());
1296 /// processLexicalBlock
1297 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1298 DIScope Context = LB.getContext();
1299 if (Context.isLexicalBlock())
1300 return processLexicalBlock(DILexicalBlock(Context));
1302 return processSubprogram(DISubprogram(Context));
1305 /// processSubprogram - Process DISubprogram.
1306 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1307 if (!addSubprogram(SP))
1309 addCompileUnit(SP.getCompileUnit());
1310 processType(SP.getType());
1313 /// processDeclare - Process DbgDeclareInst.
1314 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
1315 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1319 if (!DV.isVariable())
1322 if (!NodesSeen.insert(DV))
1325 addCompileUnit(DIVariable(N).getCompileUnit());
1326 processType(DIVariable(N).getType());
1329 /// addType - Add type into Tys.
1330 bool DebugInfoFinder::addType(DIType DT) {
1334 if (!NodesSeen.insert(DT))
1341 /// addCompileUnit - Add compile unit into CUs.
1342 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1346 if (!NodesSeen.insert(CU))
1353 /// addGlobalVariable - Add global variable into GVs.
1354 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1355 if (!DIDescriptor(DIG).isGlobalVariable())
1358 if (!NodesSeen.insert(DIG))
1365 // addSubprogram - Add subprgoram into SPs.
1366 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1367 if (!DIDescriptor(SP).isSubprogram())
1370 if (!NodesSeen.insert(SP))
1377 /// Find the debug info descriptor corresponding to this global variable.
1378 static Value *findDbgGlobalDeclare(GlobalVariable *V) {
1379 const Module *M = V->getParent();
1380 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1384 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1385 DIDescriptor DIG(cast_or_null<MDNode>(NMD->getOperand(i)));
1386 if (!DIG.isGlobalVariable())
1388 if (DIGlobalVariable(DIG).getGlobal() == V)
1394 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1395 /// It looks through pointer casts too.
1396 static const DbgDeclareInst *findDbgDeclare(const Value *V) {
1397 V = V->stripPointerCasts();
1399 if (!isa<Instruction>(V) && !isa<Argument>(V))
1402 const Function *F = NULL;
1403 if (const Instruction *I = dyn_cast<Instruction>(V))
1404 F = I->getParent()->getParent();
1405 else if (const Argument *A = dyn_cast<Argument>(V))
1408 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1409 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1411 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1412 if (DDI->getAddress() == V)
1418 bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1419 std::string &Type, unsigned &LineNo,
1420 std::string &File, std::string &Dir) {
1424 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1425 Value *DIGV = findDbgGlobalDeclare(GV);
1426 if (!DIGV) return false;
1427 DIGlobalVariable Var(cast<MDNode>(DIGV));
1429 StringRef D = Var.getDisplayName();
1432 LineNo = Var.getLineNumber();
1433 Unit = Var.getCompileUnit();
1434 TypeD = Var.getType();
1436 const DbgDeclareInst *DDI = findDbgDeclare(V);
1437 if (!DDI) return false;
1438 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1440 StringRef D = Var.getName();
1443 LineNo = Var.getLineNumber();
1444 Unit = Var.getCompileUnit();
1445 TypeD = Var.getType();
1448 StringRef T = TypeD.getName();
1451 StringRef F = Unit.getFilename();
1454 StringRef D = Unit.getDirectory();
1460 /// getDISubprogram - Find subprogram that is enclosing this scope.
1461 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
1462 DIDescriptor D(Scope);
1463 if (D.isSubprogram())
1464 return DISubprogram(Scope);
1466 if (D.isLexicalBlock())
1467 return getDISubprogram(DILexicalBlock(Scope).getContext());
1469 return DISubprogram();
1472 /// getDICompositeType - Find underlying composite type.
1473 DICompositeType llvm::getDICompositeType(DIType T) {
1474 if (T.isCompositeType())
1475 return DICompositeType(T);
1477 if (T.isDerivedType())
1478 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
1480 return DICompositeType();