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/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Intrinsics.h"
19 #include "llvm/IntrinsicInst.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/Module.h"
22 #include "llvm/Analysis/ValueTracking.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 #include "llvm/ADT/SmallString.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 DIDescriptor::DIDescriptor(const DIFile F) : DbgNode(F.DbgNode) {
38 DIDescriptor::DIDescriptor(const DISubprogram F) : DbgNode(F.DbgNode) {
41 DIDescriptor::DIDescriptor(const DILexicalBlock F) : DbgNode(F.DbgNode) {
44 DIDescriptor::DIDescriptor(const DIVariable F) : DbgNode(F.DbgNode) {
47 DIDescriptor::DIDescriptor(const DIType F) : DbgNode(F.DbgNode) {
51 DIDescriptor::getStringField(unsigned Elt) const {
55 if (Elt < DbgNode->getNumOperands())
56 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
57 return MDS->getString();
62 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
66 if (Elt < DbgNode->getNumOperands())
67 if (ConstantInt *CI = dyn_cast<ConstantInt>(DbgNode->getOperand(Elt)))
68 return CI->getZExtValue();
73 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
75 return DIDescriptor();
77 if (Elt < DbgNode->getNumOperands())
79 DIDescriptor(dyn_cast_or_null<const MDNode>(DbgNode->getOperand(Elt)));
80 return DIDescriptor();
83 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
87 if (Elt < DbgNode->getNumOperands())
88 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
92 Function *DIDescriptor::getFunctionField(unsigned Elt) const {
96 if (Elt < DbgNode->getNumOperands())
97 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
101 unsigned DIVariable::getNumAddrElements() const {
102 return DbgNode->getNumOperands()-6;
106 //===----------------------------------------------------------------------===//
108 //===----------------------------------------------------------------------===//
110 /// isBasicType - Return true if the specified tag is legal for
112 bool DIDescriptor::isBasicType() const {
113 return DbgNode && getTag() == dwarf::DW_TAG_base_type;
116 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
117 bool DIDescriptor::isDerivedType() const {
118 if (!DbgNode) return false;
120 case dwarf::DW_TAG_typedef:
121 case dwarf::DW_TAG_pointer_type:
122 case dwarf::DW_TAG_reference_type:
123 case dwarf::DW_TAG_const_type:
124 case dwarf::DW_TAG_volatile_type:
125 case dwarf::DW_TAG_restrict_type:
126 case dwarf::DW_TAG_member:
127 case dwarf::DW_TAG_inheritance:
130 // CompositeTypes are currently modelled as DerivedTypes.
131 return isCompositeType();
135 /// isCompositeType - Return true if the specified tag is legal for
137 bool DIDescriptor::isCompositeType() const {
138 if (!DbgNode) return false;
140 case dwarf::DW_TAG_array_type:
141 case dwarf::DW_TAG_structure_type:
142 case dwarf::DW_TAG_union_type:
143 case dwarf::DW_TAG_enumeration_type:
144 case dwarf::DW_TAG_vector_type:
145 case dwarf::DW_TAG_subroutine_type:
146 case dwarf::DW_TAG_class_type:
153 /// isVariable - Return true if the specified tag is legal for DIVariable.
154 bool DIDescriptor::isVariable() const {
155 if (!DbgNode) return false;
157 case dwarf::DW_TAG_auto_variable:
158 case dwarf::DW_TAG_arg_variable:
159 case dwarf::DW_TAG_return_variable:
166 /// isType - Return true if the specified tag is legal for DIType.
167 bool DIDescriptor::isType() const {
168 return isBasicType() || isCompositeType() || isDerivedType();
171 /// isSubprogram - Return true if the specified tag is legal for
173 bool DIDescriptor::isSubprogram() const {
174 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
177 /// isGlobalVariable - Return true if the specified tag is legal for
178 /// DIGlobalVariable.
179 bool DIDescriptor::isGlobalVariable() const {
180 return DbgNode && getTag() == dwarf::DW_TAG_variable;
183 /// isGlobal - Return true if the specified tag is legal for DIGlobal.
184 bool DIDescriptor::isGlobal() const {
185 return isGlobalVariable();
188 /// isScope - Return true if the specified tag is one of the scope
190 bool DIDescriptor::isScope() const {
191 if (!DbgNode) return false;
193 case dwarf::DW_TAG_compile_unit:
194 case dwarf::DW_TAG_lexical_block:
195 case dwarf::DW_TAG_subprogram:
196 case dwarf::DW_TAG_namespace:
204 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
205 bool DIDescriptor::isCompileUnit() const {
206 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
209 /// isFile - Return true if the specified tag is DW_TAG_file_type.
210 bool DIDescriptor::isFile() const {
211 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
214 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
215 bool DIDescriptor::isNameSpace() const {
216 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
219 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
220 bool DIDescriptor::isLexicalBlock() const {
221 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
224 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
225 bool DIDescriptor::isSubrange() const {
226 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
229 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
230 bool DIDescriptor::isEnumerator() const {
231 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
234 //===----------------------------------------------------------------------===//
235 // Simple Descriptor Constructors and other Methods
236 //===----------------------------------------------------------------------===//
238 DIType::DIType(const MDNode *N) : DIScope(N) {
240 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
245 unsigned DIArray::getNumElements() const {
248 return DbgNode->getNumOperands();
251 /// replaceAllUsesWith - Replace all uses of debug info referenced by
253 void DIDerivedType::replaceAllUsesWith(DIDescriptor &D) {
257 // Since we use a TrackingVH for the node, its easy for clients to manufacture
258 // legitimate situations where they want to replaceAllUsesWith() on something
259 // which, due to uniquing, has merged with the source. We shield clients from
260 // this detail by allowing a value to be replaced with replaceAllUsesWith()
263 MDNode *Node = const_cast<MDNode*>(DbgNode);
264 const MDNode *DN = D;
265 const Value *V = cast_or_null<Value>(DN);
266 Node->replaceAllUsesWith(const_cast<Value*>(V));
270 /// Verify - Verify that a compile unit is well formed.
271 bool DICompileUnit::Verify() const {
274 StringRef N = getFilename();
277 // It is possible that directory and produce string is empty.
281 /// Verify - Verify that a type descriptor is well formed.
282 bool DIType::Verify() const {
285 if (!getContext().Verify())
288 DICompileUnit CU = getCompileUnit();
294 /// Verify - Verify that a composite type descriptor is well formed.
295 bool DICompositeType::Verify() const {
298 if (!getContext().Verify())
301 DICompileUnit CU = getCompileUnit();
307 /// Verify - Verify that a subprogram descriptor is well formed.
308 bool DISubprogram::Verify() const {
312 if (!getContext().Verify())
315 DICompileUnit CU = getCompileUnit();
319 DICompositeType Ty = getType();
325 /// Verify - Verify that a global variable descriptor is well formed.
326 bool DIGlobalVariable::Verify() const {
330 if (getDisplayName().empty())
333 if (!getContext().Verify())
336 DICompileUnit CU = getCompileUnit();
340 DIType Ty = getType();
350 /// Verify - Verify that a variable descriptor is well formed.
351 bool DIVariable::Verify() const {
355 if (!getContext().Verify())
358 if (!getCompileUnit().Verify())
361 DIType Ty = getType();
368 /// Verify - Verify that a location descriptor is well formed.
369 bool DILocation::Verify() const {
373 return DbgNode->getNumOperands() == 4;
376 /// Verify - Verify that a namespace descriptor is well formed.
377 bool DINameSpace::Verify() const {
380 if (getName().empty())
382 if (!getCompileUnit().Verify())
387 /// getOriginalTypeSize - If this type is derived from a base type then
388 /// return base type size.
389 uint64_t DIDerivedType::getOriginalTypeSize() const {
390 unsigned Tag = getTag();
391 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
392 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
393 Tag == dwarf::DW_TAG_restrict_type) {
394 DIType BaseType = getTypeDerivedFrom();
395 // If this type is not derived from any type then take conservative
397 if (!BaseType.isValid())
398 return getSizeInBits();
399 if (BaseType.isDerivedType())
400 return DIDerivedType(BaseType).getOriginalTypeSize();
402 return BaseType.getSizeInBits();
405 return getSizeInBits();
408 /// isInlinedFnArgument - Return true if this variable provides debugging
409 /// information for an inlined function arguments.
410 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
411 assert(CurFn && "Invalid function");
412 if (!getContext().isSubprogram())
414 // This variable is not inlined function argument if its scope
415 // does not describe current function.
416 return !(DISubprogram(getContext()).describes(CurFn));
419 /// describes - Return true if this subprogram provides debugging
420 /// information for the function F.
421 bool DISubprogram::describes(const Function *F) {
422 assert(F && "Invalid function");
423 if (F == getFunction())
425 StringRef Name = getLinkageName();
428 if (F->getName() == Name)
433 unsigned DISubprogram::isOptimized() const {
434 assert (DbgNode && "Invalid subprogram descriptor!");
435 if (DbgNode->getNumOperands() == 16)
436 return getUnsignedField(15);
440 StringRef DIScope::getFilename() const {
443 if (isLexicalBlock())
444 return DILexicalBlock(DbgNode).getFilename();
446 return DISubprogram(DbgNode).getFilename();
448 return DICompileUnit(DbgNode).getFilename();
450 return DINameSpace(DbgNode).getFilename();
452 return DIType(DbgNode).getFilename();
454 return DIFile(DbgNode).getFilename();
455 assert(0 && "Invalid DIScope!");
459 StringRef DIScope::getDirectory() const {
462 if (isLexicalBlock())
463 return DILexicalBlock(DbgNode).getDirectory();
465 return DISubprogram(DbgNode).getDirectory();
467 return DICompileUnit(DbgNode).getDirectory();
469 return DINameSpace(DbgNode).getDirectory();
471 return DIType(DbgNode).getDirectory();
473 return DIFile(DbgNode).getDirectory();
474 assert(0 && "Invalid DIScope!");
478 //===----------------------------------------------------------------------===//
479 // DIDescriptor: dump routines for all descriptors.
480 //===----------------------------------------------------------------------===//
483 /// print - Print descriptor.
484 void DIDescriptor::print(raw_ostream &OS) const {
485 OS << "[" << dwarf::TagString(getTag()) << "] ";
486 OS.write_hex((intptr_t) &*DbgNode) << ']';
489 /// print - Print compile unit.
490 void DICompileUnit::print(raw_ostream &OS) const {
492 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
494 OS << " [" << getDirectory() << "/" << getFilename() << "]";
497 /// print - Print type.
498 void DIType::print(raw_ostream &OS) const {
499 if (!DbgNode) return;
501 StringRef Res = getName();
503 OS << " [" << Res << "] ";
505 unsigned Tag = getTag();
506 OS << " [" << dwarf::TagString(Tag) << "] ";
508 // TODO : Print context
509 getCompileUnit().print(OS);
511 << "line " << getLineNumber() << ", "
512 << getSizeInBits() << " bits, "
513 << getAlignInBits() << " bit alignment, "
514 << getOffsetInBits() << " bit offset"
519 else if (isProtected())
520 OS << " [protected] ";
526 DIBasicType(DbgNode).print(OS);
527 else if (isDerivedType())
528 DIDerivedType(DbgNode).print(OS);
529 else if (isCompositeType())
530 DICompositeType(DbgNode).print(OS);
532 OS << "Invalid DIType\n";
539 /// print - Print basic type.
540 void DIBasicType::print(raw_ostream &OS) const {
541 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
544 /// print - Print derived type.
545 void DIDerivedType::print(raw_ostream &OS) const {
546 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
549 /// print - Print composite type.
550 void DICompositeType::print(raw_ostream &OS) const {
551 DIArray A = getTypeArray();
552 OS << " [" << A.getNumElements() << " elements]";
555 /// print - Print subprogram.
556 void DISubprogram::print(raw_ostream &OS) const {
557 StringRef Res = getName();
559 OS << " [" << Res << "] ";
561 unsigned Tag = getTag();
562 OS << " [" << dwarf::TagString(Tag) << "] ";
564 // TODO : Print context
565 getCompileUnit().print(OS);
566 OS << " [" << getLineNumber() << "] ";
577 /// print - Print global variable.
578 void DIGlobalVariable::print(raw_ostream &OS) const {
580 StringRef Res = getName();
582 OS << " [" << Res << "] ";
584 unsigned Tag = getTag();
585 OS << " [" << dwarf::TagString(Tag) << "] ";
587 // TODO : Print context
588 getCompileUnit().print(OS);
589 OS << " [" << getLineNumber() << "] ";
597 if (isGlobalVariable())
598 DIGlobalVariable(DbgNode).print(OS);
602 /// print - Print variable.
603 void DIVariable::print(raw_ostream &OS) const {
604 StringRef Res = getName();
606 OS << " [" << Res << "] ";
608 getCompileUnit().print(OS);
609 OS << " [" << getLineNumber() << "] ";
613 // FIXME: Dump complex addresses
616 /// dump - Print descriptor to dbgs() with a newline.
617 void DIDescriptor::dump() const {
618 print(dbgs()); dbgs() << '\n';
621 /// dump - Print compile unit to dbgs() with a newline.
622 void DICompileUnit::dump() const {
623 print(dbgs()); dbgs() << '\n';
626 /// dump - Print type to dbgs() with a newline.
627 void DIType::dump() const {
628 print(dbgs()); dbgs() << '\n';
631 /// dump - Print basic type to dbgs() with a newline.
632 void DIBasicType::dump() const {
633 print(dbgs()); dbgs() << '\n';
636 /// dump - Print derived type to dbgs() with a newline.
637 void DIDerivedType::dump() const {
638 print(dbgs()); dbgs() << '\n';
641 /// dump - Print composite type to dbgs() with a newline.
642 void DICompositeType::dump() const {
643 print(dbgs()); dbgs() << '\n';
646 /// dump - Print subprogram to dbgs() with a newline.
647 void DISubprogram::dump() const {
648 print(dbgs()); dbgs() << '\n';
651 /// dump - Print global variable.
652 void DIGlobalVariable::dump() const {
653 print(dbgs()); dbgs() << '\n';
656 /// dump - Print variable.
657 void DIVariable::dump() const {
658 print(dbgs()); dbgs() << '\n';
661 //===----------------------------------------------------------------------===//
662 // DIFactory: Basic Helpers
663 //===----------------------------------------------------------------------===//
665 DIFactory::DIFactory(Module &m)
666 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
668 Constant *DIFactory::GetTagConstant(unsigned TAG) {
669 assert((TAG & LLVMDebugVersionMask) == 0 &&
670 "Tag too large for debug encoding!");
671 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
674 //===----------------------------------------------------------------------===//
675 // DIFactory: Primary Constructors
676 //===----------------------------------------------------------------------===//
678 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
679 /// This implicitly uniques the arrays created.
680 DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
681 SmallVector<Value*, 16> Elts;
684 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
686 for (unsigned i = 0; i != NumTys; ++i)
687 Elts.push_back(Tys[i]);
689 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
692 /// GetOrCreateSubrange - Create a descriptor for a value range. This
693 /// implicitly uniques the values returned.
694 DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
696 GetTagConstant(dwarf::DW_TAG_subrange_type),
697 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
698 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
701 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
706 /// CreateCompileUnit - Create a new descriptor for the specified compile
707 /// unit. Note that this does not unique compile units within the module.
708 DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
715 unsigned RunTimeVer) {
717 GetTagConstant(dwarf::DW_TAG_compile_unit),
718 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
719 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
720 MDString::get(VMContext, Filename),
721 MDString::get(VMContext, Directory),
722 MDString::get(VMContext, Producer),
723 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
724 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
725 MDString::get(VMContext, Flags),
726 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
729 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
732 /// CreateFile - Create a new descriptor for the specified file.
733 DIFile DIFactory::CreateFile(StringRef Filename,
737 GetTagConstant(dwarf::DW_TAG_file_type),
738 MDString::get(VMContext, Filename),
739 MDString::get(VMContext, Directory),
743 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
746 /// CreateEnumerator - Create a single enumerator value.
747 DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
749 GetTagConstant(dwarf::DW_TAG_enumerator),
750 MDString::get(VMContext, Name),
751 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
753 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
757 /// CreateBasicType - Create a basic type like int, float, etc.
758 DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
763 uint64_t AlignInBits,
764 uint64_t OffsetInBits, unsigned Flags,
767 GetTagConstant(dwarf::DW_TAG_base_type),
769 MDString::get(VMContext, Name),
771 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
772 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
773 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
774 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
775 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
776 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
778 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
782 /// CreateBasicType - Create a basic type like int, float, etc.
783 DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
787 Constant *SizeInBits,
788 Constant *AlignInBits,
789 Constant *OffsetInBits, unsigned Flags,
792 GetTagConstant(dwarf::DW_TAG_base_type),
794 MDString::get(VMContext, Name),
796 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
800 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
801 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
803 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
806 /// CreateArtificialType - Create a new DIType with "artificial" flag set.
807 DIType DIFactory::CreateArtificialType(DIType Ty) {
808 if (Ty.isArtificial())
811 SmallVector<Value *, 9> Elts;
813 assert (N && "Unexpected input DIType!");
814 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
815 if (Value *V = N->getOperand(i))
818 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
821 unsigned CurFlags = Ty.getFlags();
822 CurFlags = CurFlags | DIType::FlagArtificial;
824 // Flags are stored at this slot.
825 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
827 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
830 /// CreateDerivedType - Create a derived type like const qualified type,
831 /// pointer, typedef, etc.
832 DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
833 DIDescriptor Context,
838 uint64_t AlignInBits,
839 uint64_t OffsetInBits,
841 DIType DerivedFrom) {
845 MDString::get(VMContext, Name),
847 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
848 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
849 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
850 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
851 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
854 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
858 /// CreateDerivedType - Create a derived type like const qualified type,
859 /// pointer, typedef, etc.
860 DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
861 DIDescriptor Context,
865 Constant *SizeInBits,
866 Constant *AlignInBits,
867 Constant *OffsetInBits,
869 DIType DerivedFrom) {
873 MDString::get(VMContext, Name),
875 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
879 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
882 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
886 /// CreateCompositeType - Create a composite type like array, struct, etc.
887 DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
888 DIDescriptor Context,
893 uint64_t AlignInBits,
894 uint64_t OffsetInBits,
898 unsigned RuntimeLang,
899 MDNode *ContainingType) {
904 MDString::get(VMContext, Name),
906 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
907 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
908 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
909 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
910 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
913 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
916 return DICompositeType(MDNode::get(VMContext, &Elts[0], 13));
920 /// CreateCompositeType - Create a composite type like array, struct, etc.
921 DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
922 DIDescriptor Context,
926 Constant *SizeInBits,
927 Constant *AlignInBits,
928 Constant *OffsetInBits,
932 unsigned RuntimeLang) {
937 MDString::get(VMContext, Name),
939 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
943 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
946 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
948 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
952 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
953 /// See comments in DISubprogram for descriptions of these fields. This
954 /// method does not unique the generated descriptors.
955 DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
957 StringRef DisplayName,
958 StringRef LinkageName,
960 unsigned LineNo, DIType Ty,
963 unsigned VK, unsigned VIndex,
964 DIType ContainingType,
970 GetTagConstant(dwarf::DW_TAG_subprogram),
971 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
973 MDString::get(VMContext, Name),
974 MDString::get(VMContext, DisplayName),
975 MDString::get(VMContext, LinkageName),
977 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
979 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
980 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
981 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
982 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
984 ConstantInt::get(Type::getInt1Ty(VMContext), isArtificial),
985 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
988 MDNode *Node = MDNode::get(VMContext, &Elts[0], 17);
990 // Create a named metadata so that we do not lose this mdnode.
991 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
992 NMD->addOperand(Node);
993 return DISubprogram(Node);
996 /// CreateSubprogramDefinition - Create new subprogram descriptor for the
997 /// given declaration.
998 DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration){
999 if (SPDeclaration.isDefinition())
1000 return DISubprogram(SPDeclaration);
1002 MDNode *DeclNode = SPDeclaration;
1004 GetTagConstant(dwarf::DW_TAG_subprogram),
1005 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1006 DeclNode->getOperand(2), // Context
1007 DeclNode->getOperand(3), // Name
1008 DeclNode->getOperand(4), // DisplayName
1009 DeclNode->getOperand(5), // LinkageName
1010 DeclNode->getOperand(6), // CompileUnit
1011 DeclNode->getOperand(7), // LineNo
1012 DeclNode->getOperand(8), // Type
1013 DeclNode->getOperand(9), // isLocalToUnit
1014 ConstantInt::get(Type::getInt1Ty(VMContext), true),
1015 DeclNode->getOperand(11), // Virtuality
1016 DeclNode->getOperand(12), // VIndex
1017 DeclNode->getOperand(13), // Containting Type
1018 DeclNode->getOperand(14), // isArtificial
1019 DeclNode->getOperand(15), // isOptimized
1020 SPDeclaration.getFunction()
1022 MDNode *Node =MDNode::get(VMContext, &Elts[0], 16);
1024 // Create a named metadata so that we do not lose this mdnode.
1025 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1026 NMD->addOperand(Node);
1027 return DISubprogram(Node);
1030 /// CreateGlobalVariable - Create a new descriptor for the specified global.
1032 DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1033 StringRef DisplayName,
1034 StringRef LinkageName,
1036 unsigned LineNo, DIType Ty,bool isLocalToUnit,
1037 bool isDefinition, llvm::GlobalVariable *Val) {
1039 GetTagConstant(dwarf::DW_TAG_variable),
1040 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1042 MDString::get(VMContext, Name),
1043 MDString::get(VMContext, DisplayName),
1044 MDString::get(VMContext, LinkageName),
1046 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1048 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1049 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1053 Value *const *Vs = &Elts[0];
1054 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1056 // Create a named metadata so that we do not lose this mdnode.
1057 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1058 NMD->addOperand(Node);
1060 return DIGlobalVariable(Node);
1064 /// CreateVariable - Create a new descriptor for the specified variable.
1065 DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
1069 DIType Ty, bool AlwaysPreserve) {
1071 GetTagConstant(Tag),
1073 MDString::get(VMContext, Name),
1075 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1078 MDNode *Node = MDNode::get(VMContext, &Elts[0], 6);
1079 if (AlwaysPreserve) {
1080 // The optimizer may remove local variable. If there is an interest
1081 // to preserve variable info in such situation then stash it in a
1083 DISubprogram Fn(getDISubprogram(Context));
1084 StringRef FName = "fn";
1085 if (Fn.getFunction())
1086 FName = Fn.getFunction()->getName();
1088 if (FName.startswith(StringRef(&One, 1)))
1089 FName = FName.substr(1);
1091 SmallString<32> Out;
1092 NamedMDNode *FnLocals =
1093 M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", FName).toStringRef(Out));
1094 FnLocals->addOperand(Node);
1096 return DIVariable(Node);
1100 /// CreateComplexVariable - Create a new descriptor for the specified variable
1101 /// which has a complex address expression for its address.
1102 DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
1103 const std::string &Name,
1107 SmallVector<Value *, 9> &addr) {
1108 SmallVector<Value *, 9> Elts;
1109 Elts.push_back(GetTagConstant(Tag));
1110 Elts.push_back(Context);
1111 Elts.push_back(MDString::get(VMContext, Name));
1113 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
1115 Elts.insert(Elts.end(), addr.begin(), addr.end());
1117 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
1121 /// CreateBlock - This creates a descriptor for a lexical block with the
1122 /// specified parent VMContext.
1123 DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
1124 DIFile F, unsigned LineNo,
1126 // Defeat MDNode uniqing for lexical blocks.
1127 static unsigned int unique_id = 0;
1129 GetTagConstant(dwarf::DW_TAG_lexical_block),
1131 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1132 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1134 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1136 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 6));
1139 /// CreateNameSpace - This creates new descriptor for a namespace
1140 /// with the specified parent context.
1141 DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
1145 GetTagConstant(dwarf::DW_TAG_namespace),
1147 MDString::get(VMContext, Name),
1149 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1151 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1154 /// CreateLocation - Creates a debug info location.
1155 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1156 DIScope S, DILocation OrigLoc) {
1158 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1159 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1163 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1166 //===----------------------------------------------------------------------===//
1167 // DIFactory: Routines for inserting code into a function
1168 //===----------------------------------------------------------------------===//
1170 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1171 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1172 Instruction *InsertBefore) {
1173 assert(Storage && "no storage passed to dbg.declare");
1174 assert(D.Verify() && "empty DIVariable passed to dbg.declare");
1176 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1178 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1180 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
1183 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1184 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1185 BasicBlock *InsertAtEnd) {
1186 assert(Storage && "no storage passed to dbg.declare");
1187 assert(D.Verify() && "invalid DIVariable passed to dbg.declare");
1189 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1191 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1194 // If this block already has a terminator then insert this intrinsic
1195 // before the terminator.
1196 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1197 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1199 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
1201 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1202 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1204 Instruction *InsertBefore) {
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, "", InsertBefore);
1216 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1217 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1219 BasicBlock *InsertAtEnd) {
1220 assert(V && "no value passed to dbg.value");
1221 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
1223 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1225 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1226 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1228 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1231 //===----------------------------------------------------------------------===//
1232 // DebugInfoFinder implementations.
1233 //===----------------------------------------------------------------------===//
1235 /// processModule - Process entire module and collect debug info.
1236 void DebugInfoFinder::processModule(Module &M) {
1237 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1238 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1239 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1241 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1242 processDeclare(DDI);
1244 DebugLoc Loc = BI->getDebugLoc();
1245 if (Loc.isUnknown())
1248 LLVMContext &Ctx = BI->getContext();
1249 DIDescriptor Scope(Loc.getScope(Ctx));
1251 if (Scope.isCompileUnit())
1252 addCompileUnit(DICompileUnit(Scope));
1253 else if (Scope.isSubprogram())
1254 processSubprogram(DISubprogram(Scope));
1255 else if (Scope.isLexicalBlock())
1256 processLexicalBlock(DILexicalBlock(Scope));
1258 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1259 processLocation(DILocation(IA));
1262 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
1263 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1264 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1265 if (addGlobalVariable(DIG)) {
1266 addCompileUnit(DIG.getCompileUnit());
1267 processType(DIG.getType());
1272 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
1273 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
1274 processSubprogram(DISubprogram(NMD->getOperand(i)));
1277 /// processLocation - Process DILocation.
1278 void DebugInfoFinder::processLocation(DILocation Loc) {
1279 if (!Loc.Verify()) return;
1280 DIDescriptor S(Loc.getScope());
1281 if (S.isCompileUnit())
1282 addCompileUnit(DICompileUnit(S));
1283 else if (S.isSubprogram())
1284 processSubprogram(DISubprogram(S));
1285 else if (S.isLexicalBlock())
1286 processLexicalBlock(DILexicalBlock(S));
1287 processLocation(Loc.getOrigLocation());
1290 /// processType - Process DIType.
1291 void DebugInfoFinder::processType(DIType DT) {
1295 addCompileUnit(DT.getCompileUnit());
1296 if (DT.isCompositeType()) {
1297 DICompositeType DCT(DT);
1298 processType(DCT.getTypeDerivedFrom());
1299 DIArray DA = DCT.getTypeArray();
1300 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1301 DIDescriptor D = DA.getElement(i);
1303 processType(DIType(D));
1304 else if (D.isSubprogram())
1305 processSubprogram(DISubprogram(D));
1307 } else if (DT.isDerivedType()) {
1308 DIDerivedType DDT(DT);
1309 processType(DDT.getTypeDerivedFrom());
1313 /// processLexicalBlock
1314 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1315 DIScope Context = LB.getContext();
1316 if (Context.isLexicalBlock())
1317 return processLexicalBlock(DILexicalBlock(Context));
1319 return processSubprogram(DISubprogram(Context));
1322 /// processSubprogram - Process DISubprogram.
1323 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1324 if (!addSubprogram(SP))
1326 addCompileUnit(SP.getCompileUnit());
1327 processType(SP.getType());
1330 /// processDeclare - Process DbgDeclareInst.
1331 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
1332 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1336 if (!DV.isVariable())
1339 if (!NodesSeen.insert(DV))
1342 addCompileUnit(DIVariable(N).getCompileUnit());
1343 processType(DIVariable(N).getType());
1346 /// addType - Add type into Tys.
1347 bool DebugInfoFinder::addType(DIType DT) {
1351 if (!NodesSeen.insert(DT))
1358 /// addCompileUnit - Add compile unit into CUs.
1359 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1363 if (!NodesSeen.insert(CU))
1370 /// addGlobalVariable - Add global variable into GVs.
1371 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1372 if (!DIDescriptor(DIG).isGlobalVariable())
1375 if (!NodesSeen.insert(DIG))
1382 // addSubprogram - Add subprgoram into SPs.
1383 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1384 if (!DIDescriptor(SP).isSubprogram())
1387 if (!NodesSeen.insert(SP))
1394 /// Find the debug info descriptor corresponding to this global variable.
1395 static Value *findDbgGlobalDeclare(GlobalVariable *V) {
1396 const Module *M = V->getParent();
1397 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1401 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1402 DIDescriptor DIG(cast<MDNode>(NMD->getOperand(i)));
1403 if (!DIG.isGlobalVariable())
1405 if (DIGlobalVariable(DIG).getGlobal() == V)
1411 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1412 /// It looks through pointer casts too.
1413 static const DbgDeclareInst *findDbgDeclare(const Value *V) {
1414 V = V->stripPointerCasts();
1416 if (!isa<Instruction>(V) && !isa<Argument>(V))
1419 const Function *F = NULL;
1420 if (const Instruction *I = dyn_cast<Instruction>(V))
1421 F = I->getParent()->getParent();
1422 else if (const Argument *A = dyn_cast<Argument>(V))
1425 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1426 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1428 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1429 if (DDI->getAddress() == V)
1435 bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1436 std::string &Type, unsigned &LineNo,
1437 std::string &File, std::string &Dir) {
1441 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1442 Value *DIGV = findDbgGlobalDeclare(GV);
1443 if (!DIGV) return false;
1444 DIGlobalVariable Var(cast<MDNode>(DIGV));
1446 StringRef D = Var.getDisplayName();
1449 LineNo = Var.getLineNumber();
1450 Unit = Var.getCompileUnit();
1451 TypeD = Var.getType();
1453 const DbgDeclareInst *DDI = findDbgDeclare(V);
1454 if (!DDI) return false;
1455 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1457 StringRef D = Var.getName();
1460 LineNo = Var.getLineNumber();
1461 Unit = Var.getCompileUnit();
1462 TypeD = Var.getType();
1465 StringRef T = TypeD.getName();
1468 StringRef F = Unit.getFilename();
1471 StringRef D = Unit.getDirectory();
1477 /// getDISubprogram - Find subprogram that is enclosing this scope.
1478 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
1479 DIDescriptor D(Scope);
1480 if (D.isSubprogram())
1481 return DISubprogram(Scope);
1483 if (D.isLexicalBlock())
1484 return getDISubprogram(DILexicalBlock(Scope).getContext());
1486 return DISubprogram();
1489 /// getDICompositeType - Find underlying composite type.
1490 DICompositeType llvm::getDICompositeType(DIType T) {
1491 if (T.isCompositeType())
1492 return DICompositeType(T);
1494 if (T.isDerivedType())
1495 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
1497 return DICompositeType();