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 Constant *DIDescriptor::getConstantField(unsigned Elt) const {
96 if (Elt < DbgNode->getNumOperands())
97 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
101 Function *DIDescriptor::getFunctionField(unsigned Elt) const {
105 if (Elt < DbgNode->getNumOperands())
106 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
110 unsigned DIVariable::getNumAddrElements() const {
111 return DbgNode->getNumOperands()-6;
115 //===----------------------------------------------------------------------===//
117 //===----------------------------------------------------------------------===//
119 /// isBasicType - Return true if the specified tag is legal for
121 bool DIDescriptor::isBasicType() const {
122 return DbgNode && getTag() == dwarf::DW_TAG_base_type;
125 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
126 bool DIDescriptor::isDerivedType() const {
127 if (!DbgNode) return false;
129 case dwarf::DW_TAG_typedef:
130 case dwarf::DW_TAG_pointer_type:
131 case dwarf::DW_TAG_reference_type:
132 case dwarf::DW_TAG_const_type:
133 case dwarf::DW_TAG_volatile_type:
134 case dwarf::DW_TAG_restrict_type:
135 case dwarf::DW_TAG_member:
136 case dwarf::DW_TAG_inheritance:
139 // CompositeTypes are currently modelled as DerivedTypes.
140 return isCompositeType();
144 /// isCompositeType - Return true if the specified tag is legal for
146 bool DIDescriptor::isCompositeType() const {
147 if (!DbgNode) return false;
149 case dwarf::DW_TAG_array_type:
150 case dwarf::DW_TAG_structure_type:
151 case dwarf::DW_TAG_union_type:
152 case dwarf::DW_TAG_enumeration_type:
153 case dwarf::DW_TAG_vector_type:
154 case dwarf::DW_TAG_subroutine_type:
155 case dwarf::DW_TAG_class_type:
162 /// isVariable - Return true if the specified tag is legal for DIVariable.
163 bool DIDescriptor::isVariable() const {
164 if (!DbgNode) return false;
166 case dwarf::DW_TAG_auto_variable:
167 case dwarf::DW_TAG_arg_variable:
168 case dwarf::DW_TAG_return_variable:
175 /// isType - Return true if the specified tag is legal for DIType.
176 bool DIDescriptor::isType() const {
177 return isBasicType() || isCompositeType() || isDerivedType();
180 /// isSubprogram - Return true if the specified tag is legal for
182 bool DIDescriptor::isSubprogram() const {
183 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
186 /// isGlobalVariable - Return true if the specified tag is legal for
187 /// DIGlobalVariable.
188 bool DIDescriptor::isGlobalVariable() const {
189 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
190 getTag() == dwarf::DW_TAG_constant);
193 /// isGlobal - Return true if the specified tag is legal for DIGlobal.
194 bool DIDescriptor::isGlobal() const {
195 return isGlobalVariable();
198 /// isScope - Return true if the specified tag is one of the scope
200 bool DIDescriptor::isScope() const {
201 if (!DbgNode) return false;
203 case dwarf::DW_TAG_compile_unit:
204 case dwarf::DW_TAG_lexical_block:
205 case dwarf::DW_TAG_subprogram:
206 case dwarf::DW_TAG_namespace:
214 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
215 bool DIDescriptor::isCompileUnit() const {
216 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
219 /// isFile - Return true if the specified tag is DW_TAG_file_type.
220 bool DIDescriptor::isFile() const {
221 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
224 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
225 bool DIDescriptor::isNameSpace() const {
226 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
229 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
230 bool DIDescriptor::isLexicalBlock() const {
231 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
234 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
235 bool DIDescriptor::isSubrange() const {
236 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
239 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
240 bool DIDescriptor::isEnumerator() const {
241 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
244 //===----------------------------------------------------------------------===//
245 // Simple Descriptor Constructors and other Methods
246 //===----------------------------------------------------------------------===//
248 DIType::DIType(const MDNode *N) : DIScope(N) {
250 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
255 unsigned DIArray::getNumElements() const {
258 return DbgNode->getNumOperands();
261 /// replaceAllUsesWith - Replace all uses of debug info referenced by
263 void DIDerivedType::replaceAllUsesWith(DIDescriptor &D) {
267 // Since we use a TrackingVH for the node, its easy for clients to manufacture
268 // legitimate situations where they want to replaceAllUsesWith() on something
269 // which, due to uniquing, has merged with the source. We shield clients from
270 // this detail by allowing a value to be replaced with replaceAllUsesWith()
273 MDNode *Node = const_cast<MDNode*>(DbgNode);
274 const MDNode *DN = D;
275 const Value *V = cast_or_null<Value>(DN);
276 Node->replaceAllUsesWith(const_cast<Value*>(V));
280 /// Verify - Verify that a compile unit is well formed.
281 bool DICompileUnit::Verify() const {
284 StringRef N = getFilename();
287 // It is possible that directory and produce string is empty.
291 /// Verify - Verify that a type descriptor is well formed.
292 bool DIType::Verify() const {
295 if (!getContext().Verify())
298 DICompileUnit CU = getCompileUnit();
304 /// Verify - Verify that a composite type descriptor is well formed.
305 bool DICompositeType::Verify() const {
308 if (!getContext().Verify())
311 DICompileUnit CU = getCompileUnit();
317 /// Verify - Verify that a subprogram descriptor is well formed.
318 bool DISubprogram::Verify() const {
322 if (!getContext().Verify())
325 DICompileUnit CU = getCompileUnit();
329 DICompositeType Ty = getType();
335 /// Verify - Verify that a global variable descriptor is well formed.
336 bool DIGlobalVariable::Verify() const {
340 if (getDisplayName().empty())
343 if (!getContext().Verify())
346 DICompileUnit CU = getCompileUnit();
350 DIType Ty = getType();
354 if (!getGlobal() && !getConstant())
360 /// Verify - Verify that a variable descriptor is well formed.
361 bool DIVariable::Verify() const {
365 if (!getContext().Verify())
368 if (!getCompileUnit().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 /// Verify - Verify that a namespace descriptor is well formed.
387 bool DINameSpace::Verify() const {
390 if (getName().empty())
392 if (!getCompileUnit().Verify())
397 /// getOriginalTypeSize - If this type is derived from a base type then
398 /// return base type size.
399 uint64_t DIDerivedType::getOriginalTypeSize() const {
400 unsigned Tag = getTag();
401 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
402 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
403 Tag == dwarf::DW_TAG_restrict_type) {
404 DIType BaseType = getTypeDerivedFrom();
405 // If this type is not derived from any type then take conservative
407 if (!BaseType.isValid())
408 return getSizeInBits();
409 if (BaseType.isDerivedType())
410 return DIDerivedType(BaseType).getOriginalTypeSize();
412 return BaseType.getSizeInBits();
415 return getSizeInBits();
418 /// isInlinedFnArgument - Return true if this variable provides debugging
419 /// information for an inlined function arguments.
420 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
421 assert(CurFn && "Invalid function");
422 if (!getContext().isSubprogram())
424 // This variable is not inlined function argument if its scope
425 // does not describe current function.
426 return !(DISubprogram(getContext()).describes(CurFn));
429 /// describes - Return true if this subprogram provides debugging
430 /// information for the function F.
431 bool DISubprogram::describes(const Function *F) {
432 assert(F && "Invalid function");
433 if (F == getFunction())
435 StringRef Name = getLinkageName();
438 if (F->getName() == Name)
443 unsigned DISubprogram::isOptimized() const {
444 assert (DbgNode && "Invalid subprogram descriptor!");
445 if (DbgNode->getNumOperands() == 16)
446 return getUnsignedField(15);
450 StringRef DIScope::getFilename() const {
453 if (isLexicalBlock())
454 return DILexicalBlock(DbgNode).getFilename();
456 return DISubprogram(DbgNode).getFilename();
458 return DICompileUnit(DbgNode).getFilename();
460 return DINameSpace(DbgNode).getFilename();
462 return DIType(DbgNode).getFilename();
464 return DIFile(DbgNode).getFilename();
465 assert(0 && "Invalid DIScope!");
469 StringRef DIScope::getDirectory() const {
472 if (isLexicalBlock())
473 return DILexicalBlock(DbgNode).getDirectory();
475 return DISubprogram(DbgNode).getDirectory();
477 return DICompileUnit(DbgNode).getDirectory();
479 return DINameSpace(DbgNode).getDirectory();
481 return DIType(DbgNode).getDirectory();
483 return DIFile(DbgNode).getDirectory();
484 assert(0 && "Invalid DIScope!");
488 //===----------------------------------------------------------------------===//
489 // DIDescriptor: dump routines for all descriptors.
490 //===----------------------------------------------------------------------===//
493 /// print - Print descriptor.
494 void DIDescriptor::print(raw_ostream &OS) const {
495 OS << "[" << dwarf::TagString(getTag()) << "] ";
496 OS.write_hex((intptr_t) &*DbgNode) << ']';
499 /// print - Print compile unit.
500 void DICompileUnit::print(raw_ostream &OS) const {
502 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
504 OS << " [" << getDirectory() << "/" << getFilename() << "]";
507 /// print - Print type.
508 void DIType::print(raw_ostream &OS) const {
509 if (!DbgNode) return;
511 StringRef Res = getName();
513 OS << " [" << Res << "] ";
515 unsigned Tag = getTag();
516 OS << " [" << dwarf::TagString(Tag) << "] ";
518 // TODO : Print context
519 getCompileUnit().print(OS);
521 << "line " << getLineNumber() << ", "
522 << getSizeInBits() << " bits, "
523 << getAlignInBits() << " bit alignment, "
524 << getOffsetInBits() << " bit offset"
529 else if (isProtected())
530 OS << " [protected] ";
536 DIBasicType(DbgNode).print(OS);
537 else if (isDerivedType())
538 DIDerivedType(DbgNode).print(OS);
539 else if (isCompositeType())
540 DICompositeType(DbgNode).print(OS);
542 OS << "Invalid DIType\n";
549 /// print - Print basic type.
550 void DIBasicType::print(raw_ostream &OS) const {
551 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
554 /// print - Print derived type.
555 void DIDerivedType::print(raw_ostream &OS) const {
556 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
559 /// print - Print composite type.
560 void DICompositeType::print(raw_ostream &OS) const {
561 DIArray A = getTypeArray();
562 OS << " [" << A.getNumElements() << " elements]";
565 /// print - Print subprogram.
566 void DISubprogram::print(raw_ostream &OS) const {
567 StringRef Res = getName();
569 OS << " [" << Res << "] ";
571 unsigned Tag = getTag();
572 OS << " [" << dwarf::TagString(Tag) << "] ";
574 // TODO : Print context
575 getCompileUnit().print(OS);
576 OS << " [" << getLineNumber() << "] ";
587 /// print - Print global variable.
588 void DIGlobalVariable::print(raw_ostream &OS) const {
590 StringRef Res = getName();
592 OS << " [" << Res << "] ";
594 unsigned Tag = getTag();
595 OS << " [" << dwarf::TagString(Tag) << "] ";
597 // TODO : Print context
598 getCompileUnit().print(OS);
599 OS << " [" << getLineNumber() << "] ";
607 if (isGlobalVariable())
608 DIGlobalVariable(DbgNode).print(OS);
612 /// print - Print variable.
613 void DIVariable::print(raw_ostream &OS) const {
614 StringRef Res = getName();
616 OS << " [" << Res << "] ";
618 getCompileUnit().print(OS);
619 OS << " [" << getLineNumber() << "] ";
623 // FIXME: Dump complex addresses
626 /// dump - Print descriptor to dbgs() with a newline.
627 void DIDescriptor::dump() const {
628 print(dbgs()); dbgs() << '\n';
631 /// dump - Print compile unit to dbgs() with a newline.
632 void DICompileUnit::dump() const {
633 print(dbgs()); dbgs() << '\n';
636 /// dump - Print type to dbgs() with a newline.
637 void DIType::dump() const {
638 print(dbgs()); dbgs() << '\n';
641 /// dump - Print basic type to dbgs() with a newline.
642 void DIBasicType::dump() const {
643 print(dbgs()); dbgs() << '\n';
646 /// dump - Print derived type to dbgs() with a newline.
647 void DIDerivedType::dump() const {
648 print(dbgs()); dbgs() << '\n';
651 /// dump - Print composite type to dbgs() with a newline.
652 void DICompositeType::dump() const {
653 print(dbgs()); dbgs() << '\n';
656 /// dump - Print subprogram to dbgs() with a newline.
657 void DISubprogram::dump() const {
658 print(dbgs()); dbgs() << '\n';
661 /// dump - Print global variable.
662 void DIGlobalVariable::dump() const {
663 print(dbgs()); dbgs() << '\n';
666 /// dump - Print variable.
667 void DIVariable::dump() const {
668 print(dbgs()); dbgs() << '\n';
671 //===----------------------------------------------------------------------===//
672 // DIFactory: Basic Helpers
673 //===----------------------------------------------------------------------===//
675 DIFactory::DIFactory(Module &m)
676 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
678 Constant *DIFactory::GetTagConstant(unsigned TAG) {
679 assert((TAG & LLVMDebugVersionMask) == 0 &&
680 "Tag too large for debug encoding!");
681 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
684 //===----------------------------------------------------------------------===//
685 // DIFactory: Primary Constructors
686 //===----------------------------------------------------------------------===//
688 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
689 /// This implicitly uniques the arrays created.
690 DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
691 SmallVector<Value*, 16> Elts;
694 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
696 for (unsigned i = 0; i != NumTys; ++i)
697 Elts.push_back(Tys[i]);
699 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
702 /// GetOrCreateSubrange - Create a descriptor for a value range. This
703 /// implicitly uniques the values returned.
704 DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
706 GetTagConstant(dwarf::DW_TAG_subrange_type),
707 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
708 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
711 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
716 /// CreateCompileUnit - Create a new descriptor for the specified compile
717 /// unit. Note that this does not unique compile units within the module.
718 DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
725 unsigned RunTimeVer) {
727 GetTagConstant(dwarf::DW_TAG_compile_unit),
728 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
729 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
730 MDString::get(VMContext, Filename),
731 MDString::get(VMContext, Directory),
732 MDString::get(VMContext, Producer),
733 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
734 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
735 MDString::get(VMContext, Flags),
736 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
739 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
742 /// CreateFile - Create a new descriptor for the specified file.
743 DIFile DIFactory::CreateFile(StringRef Filename,
747 GetTagConstant(dwarf::DW_TAG_file_type),
748 MDString::get(VMContext, Filename),
749 MDString::get(VMContext, Directory),
753 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
756 /// CreateEnumerator - Create a single enumerator value.
757 DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
759 GetTagConstant(dwarf::DW_TAG_enumerator),
760 MDString::get(VMContext, Name),
761 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
763 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
767 /// CreateBasicType - Create a basic type like int, float, etc.
768 DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
773 uint64_t AlignInBits,
774 uint64_t OffsetInBits, unsigned Flags,
777 GetTagConstant(dwarf::DW_TAG_base_type),
779 MDString::get(VMContext, Name),
781 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
782 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
783 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
784 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
785 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
786 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
788 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
792 /// CreateBasicType - Create a basic type like int, float, etc.
793 DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
797 Constant *SizeInBits,
798 Constant *AlignInBits,
799 Constant *OffsetInBits, unsigned Flags,
802 GetTagConstant(dwarf::DW_TAG_base_type),
804 MDString::get(VMContext, Name),
806 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
810 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
811 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
813 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
816 /// CreateArtificialType - Create a new DIType with "artificial" flag set.
817 DIType DIFactory::CreateArtificialType(DIType Ty) {
818 if (Ty.isArtificial())
821 SmallVector<Value *, 9> Elts;
823 assert (N && "Unexpected input DIType!");
824 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
825 if (Value *V = N->getOperand(i))
828 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
831 unsigned CurFlags = Ty.getFlags();
832 CurFlags = CurFlags | DIType::FlagArtificial;
834 // Flags are stored at this slot.
835 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
837 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
840 /// CreateDerivedType - Create a derived type like const qualified type,
841 /// pointer, typedef, etc.
842 DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
843 DIDescriptor Context,
848 uint64_t AlignInBits,
849 uint64_t OffsetInBits,
851 DIType DerivedFrom) {
855 MDString::get(VMContext, Name),
857 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
858 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
859 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
860 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
861 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
864 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
868 /// CreateDerivedType - Create a derived type like const qualified type,
869 /// pointer, typedef, etc.
870 DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
871 DIDescriptor Context,
875 Constant *SizeInBits,
876 Constant *AlignInBits,
877 Constant *OffsetInBits,
879 DIType DerivedFrom) {
883 MDString::get(VMContext, Name),
885 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
889 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
892 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
896 /// CreateCompositeType - Create a composite type like array, struct, etc.
897 DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
898 DIDescriptor Context,
903 uint64_t AlignInBits,
904 uint64_t OffsetInBits,
908 unsigned RuntimeLang,
909 MDNode *ContainingType) {
914 MDString::get(VMContext, Name),
916 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
917 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
918 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
919 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
920 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
923 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
927 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
928 // Create a named metadata so that we do not lose this enum info.
929 if (Tag == dwarf::DW_TAG_enumeration_type) {
930 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
931 NMD->addOperand(Node);
933 return DICompositeType(Node);
937 /// CreateCompositeType - Create a composite type like array, struct, etc.
938 DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
939 DIDescriptor Context,
943 Constant *SizeInBits,
944 Constant *AlignInBits,
945 Constant *OffsetInBits,
949 unsigned RuntimeLang) {
954 MDString::get(VMContext, Name),
956 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
960 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
963 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
965 MDNode *Node = MDNode::get(VMContext, &Elts[0], 12);
966 // Create a named metadata so that we do not lose this enum info.
967 if (Tag == dwarf::DW_TAG_enumeration_type) {
968 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
969 NMD->addOperand(Node);
971 return DICompositeType(Node);
975 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
976 /// See comments in DISubprogram for descriptions of these fields. This
977 /// method does not unique the generated descriptors.
978 DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
980 StringRef DisplayName,
981 StringRef LinkageName,
983 unsigned LineNo, DIType Ty,
986 unsigned VK, unsigned VIndex,
987 DIType ContainingType,
993 GetTagConstant(dwarf::DW_TAG_subprogram),
994 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
996 MDString::get(VMContext, Name),
997 MDString::get(VMContext, DisplayName),
998 MDString::get(VMContext, LinkageName),
1000 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1002 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1003 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1004 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
1005 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
1007 ConstantInt::get(Type::getInt1Ty(VMContext), isArtificial),
1008 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1011 MDNode *Node = MDNode::get(VMContext, &Elts[0], 17);
1013 // Create a named metadata so that we do not lose this mdnode.
1014 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1015 NMD->addOperand(Node);
1016 return DISubprogram(Node);
1019 /// CreateSubprogramDefinition - Create new subprogram descriptor for the
1020 /// given declaration.
1021 DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration){
1022 if (SPDeclaration.isDefinition())
1023 return DISubprogram(SPDeclaration);
1025 MDNode *DeclNode = SPDeclaration;
1027 GetTagConstant(dwarf::DW_TAG_subprogram),
1028 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1029 DeclNode->getOperand(2), // Context
1030 DeclNode->getOperand(3), // Name
1031 DeclNode->getOperand(4), // DisplayName
1032 DeclNode->getOperand(5), // LinkageName
1033 DeclNode->getOperand(6), // CompileUnit
1034 DeclNode->getOperand(7), // LineNo
1035 DeclNode->getOperand(8), // Type
1036 DeclNode->getOperand(9), // isLocalToUnit
1037 ConstantInt::get(Type::getInt1Ty(VMContext), true),
1038 DeclNode->getOperand(11), // Virtuality
1039 DeclNode->getOperand(12), // VIndex
1040 DeclNode->getOperand(13), // Containting Type
1041 DeclNode->getOperand(14), // isArtificial
1042 DeclNode->getOperand(15), // isOptimized
1043 SPDeclaration.getFunction()
1045 MDNode *Node =MDNode::get(VMContext, &Elts[0], 16);
1047 // Create a named metadata so that we do not lose this mdnode.
1048 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1049 NMD->addOperand(Node);
1050 return DISubprogram(Node);
1053 /// CreateGlobalVariable - Create a new descriptor for the specified global.
1055 DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1056 StringRef DisplayName,
1057 StringRef LinkageName,
1059 unsigned LineNo, DIType Ty,bool isLocalToUnit,
1060 bool isDefinition, llvm::GlobalVariable *Val) {
1062 GetTagConstant(dwarf::DW_TAG_variable),
1063 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1065 MDString::get(VMContext, Name),
1066 MDString::get(VMContext, DisplayName),
1067 MDString::get(VMContext, LinkageName),
1069 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1071 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1072 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1076 Value *const *Vs = &Elts[0];
1077 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1079 // Create a named metadata so that we do not lose this mdnode.
1080 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1081 NMD->addOperand(Node);
1083 return DIGlobalVariable(Node);
1086 /// CreateGlobalVariable - Create a new descriptor for the specified constant.
1088 DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1089 StringRef DisplayName,
1090 StringRef LinkageName,
1092 unsigned LineNo, DIType Ty,bool isLocalToUnit,
1093 bool isDefinition, llvm::Constant *Val) {
1095 GetTagConstant(dwarf::DW_TAG_constant),
1096 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1098 MDString::get(VMContext, Name),
1099 MDString::get(VMContext, DisplayName),
1100 MDString::get(VMContext, LinkageName),
1102 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1104 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1105 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1109 Value *const *Vs = &Elts[0];
1110 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1112 // Create a named metadata so that we do not lose this mdnode.
1113 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1114 NMD->addOperand(Node);
1116 return DIGlobalVariable(Node);
1119 /// CreateVariable - Create a new descriptor for the specified variable.
1120 DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
1124 DIType Ty, bool AlwaysPreserve) {
1126 GetTagConstant(Tag),
1128 MDString::get(VMContext, Name),
1130 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1133 MDNode *Node = MDNode::get(VMContext, &Elts[0], 6);
1134 if (AlwaysPreserve) {
1135 // The optimizer may remove local variable. If there is an interest
1136 // to preserve variable info in such situation then stash it in a
1138 DISubprogram Fn(getDISubprogram(Context));
1139 StringRef FName = "fn";
1140 if (Fn.getFunction())
1141 FName = Fn.getFunction()->getName();
1143 if (FName.startswith(StringRef(&One, 1)))
1144 FName = FName.substr(1);
1146 SmallString<32> Out;
1147 NamedMDNode *FnLocals =
1148 M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", FName).toStringRef(Out));
1149 FnLocals->addOperand(Node);
1151 return DIVariable(Node);
1155 /// CreateComplexVariable - Create a new descriptor for the specified variable
1156 /// which has a complex address expression for its address.
1157 DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
1158 const std::string &Name,
1162 SmallVector<Value *, 9> &addr) {
1163 SmallVector<Value *, 9> Elts;
1164 Elts.push_back(GetTagConstant(Tag));
1165 Elts.push_back(Context);
1166 Elts.push_back(MDString::get(VMContext, Name));
1168 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
1170 Elts.insert(Elts.end(), addr.begin(), addr.end());
1172 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
1176 /// CreateBlock - This creates a descriptor for a lexical block with the
1177 /// specified parent VMContext.
1178 DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
1179 DIFile F, unsigned LineNo,
1181 // Defeat MDNode uniqing for lexical blocks.
1182 static unsigned int unique_id = 0;
1184 GetTagConstant(dwarf::DW_TAG_lexical_block),
1186 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1187 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1189 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1191 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 6));
1194 /// CreateNameSpace - This creates new descriptor for a namespace
1195 /// with the specified parent context.
1196 DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
1200 GetTagConstant(dwarf::DW_TAG_namespace),
1202 MDString::get(VMContext, Name),
1204 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1206 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1209 /// CreateLocation - Creates a debug info location.
1210 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1211 DIScope S, DILocation OrigLoc) {
1213 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1214 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1218 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1221 //===----------------------------------------------------------------------===//
1222 // DIFactory: Routines for inserting code into a function
1223 //===----------------------------------------------------------------------===//
1225 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1226 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1227 Instruction *InsertBefore) {
1228 assert(Storage && "no storage passed to dbg.declare");
1229 assert(D.Verify() && "empty DIVariable passed to dbg.declare");
1231 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1233 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1235 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
1238 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1239 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1240 BasicBlock *InsertAtEnd) {
1241 assert(Storage && "no storage passed to dbg.declare");
1242 assert(D.Verify() && "invalid DIVariable passed to dbg.declare");
1244 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1246 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1249 // If this block already has a terminator then insert this intrinsic
1250 // before the terminator.
1251 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1252 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1254 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
1256 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1257 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1259 Instruction *InsertBefore) {
1260 assert(V && "no value passed to dbg.value");
1261 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
1263 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1265 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1266 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1268 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1271 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1272 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1274 BasicBlock *InsertAtEnd) {
1275 assert(V && "no value passed to dbg.value");
1276 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
1278 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1280 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1281 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1283 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1286 //===----------------------------------------------------------------------===//
1287 // DebugInfoFinder implementations.
1288 //===----------------------------------------------------------------------===//
1290 /// processModule - Process entire module and collect debug info.
1291 void DebugInfoFinder::processModule(Module &M) {
1292 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1293 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1294 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1296 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1297 processDeclare(DDI);
1299 DebugLoc Loc = BI->getDebugLoc();
1300 if (Loc.isUnknown())
1303 LLVMContext &Ctx = BI->getContext();
1304 DIDescriptor Scope(Loc.getScope(Ctx));
1306 if (Scope.isCompileUnit())
1307 addCompileUnit(DICompileUnit(Scope));
1308 else if (Scope.isSubprogram())
1309 processSubprogram(DISubprogram(Scope));
1310 else if (Scope.isLexicalBlock())
1311 processLexicalBlock(DILexicalBlock(Scope));
1313 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1314 processLocation(DILocation(IA));
1317 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
1318 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1319 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1320 if (addGlobalVariable(DIG)) {
1321 addCompileUnit(DIG.getCompileUnit());
1322 processType(DIG.getType());
1327 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
1328 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
1329 processSubprogram(DISubprogram(NMD->getOperand(i)));
1332 /// processLocation - Process DILocation.
1333 void DebugInfoFinder::processLocation(DILocation Loc) {
1334 if (!Loc.Verify()) return;
1335 DIDescriptor S(Loc.getScope());
1336 if (S.isCompileUnit())
1337 addCompileUnit(DICompileUnit(S));
1338 else if (S.isSubprogram())
1339 processSubprogram(DISubprogram(S));
1340 else if (S.isLexicalBlock())
1341 processLexicalBlock(DILexicalBlock(S));
1342 processLocation(Loc.getOrigLocation());
1345 /// processType - Process DIType.
1346 void DebugInfoFinder::processType(DIType DT) {
1350 addCompileUnit(DT.getCompileUnit());
1351 if (DT.isCompositeType()) {
1352 DICompositeType DCT(DT);
1353 processType(DCT.getTypeDerivedFrom());
1354 DIArray DA = DCT.getTypeArray();
1355 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1356 DIDescriptor D = DA.getElement(i);
1358 processType(DIType(D));
1359 else if (D.isSubprogram())
1360 processSubprogram(DISubprogram(D));
1362 } else if (DT.isDerivedType()) {
1363 DIDerivedType DDT(DT);
1364 processType(DDT.getTypeDerivedFrom());
1368 /// processLexicalBlock
1369 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1370 DIScope Context = LB.getContext();
1371 if (Context.isLexicalBlock())
1372 return processLexicalBlock(DILexicalBlock(Context));
1374 return processSubprogram(DISubprogram(Context));
1377 /// processSubprogram - Process DISubprogram.
1378 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1379 if (!addSubprogram(SP))
1381 addCompileUnit(SP.getCompileUnit());
1382 processType(SP.getType());
1385 /// processDeclare - Process DbgDeclareInst.
1386 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
1387 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1391 if (!DV.isVariable())
1394 if (!NodesSeen.insert(DV))
1397 addCompileUnit(DIVariable(N).getCompileUnit());
1398 processType(DIVariable(N).getType());
1401 /// addType - Add type into Tys.
1402 bool DebugInfoFinder::addType(DIType DT) {
1406 if (!NodesSeen.insert(DT))
1413 /// addCompileUnit - Add compile unit into CUs.
1414 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1418 if (!NodesSeen.insert(CU))
1425 /// addGlobalVariable - Add global variable into GVs.
1426 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1427 if (!DIDescriptor(DIG).isGlobalVariable())
1430 if (!NodesSeen.insert(DIG))
1437 // addSubprogram - Add subprgoram into SPs.
1438 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1439 if (!DIDescriptor(SP).isSubprogram())
1442 if (!NodesSeen.insert(SP))
1449 /// Find the debug info descriptor corresponding to this global variable.
1450 static Value *findDbgGlobalDeclare(GlobalVariable *V) {
1451 const Module *M = V->getParent();
1452 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1456 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1457 DIDescriptor DIG(cast<MDNode>(NMD->getOperand(i)));
1458 if (!DIG.isGlobalVariable())
1460 if (DIGlobalVariable(DIG).getGlobal() == V)
1466 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1467 /// It looks through pointer casts too.
1468 static const DbgDeclareInst *findDbgDeclare(const Value *V) {
1469 V = V->stripPointerCasts();
1471 if (!isa<Instruction>(V) && !isa<Argument>(V))
1474 const Function *F = NULL;
1475 if (const Instruction *I = dyn_cast<Instruction>(V))
1476 F = I->getParent()->getParent();
1477 else if (const Argument *A = dyn_cast<Argument>(V))
1480 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1481 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1483 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1484 if (DDI->getAddress() == V)
1490 bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1491 std::string &Type, unsigned &LineNo,
1492 std::string &File, std::string &Dir) {
1496 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1497 Value *DIGV = findDbgGlobalDeclare(GV);
1498 if (!DIGV) return false;
1499 DIGlobalVariable Var(cast<MDNode>(DIGV));
1501 StringRef D = Var.getDisplayName();
1504 LineNo = Var.getLineNumber();
1505 Unit = Var.getCompileUnit();
1506 TypeD = Var.getType();
1508 const DbgDeclareInst *DDI = findDbgDeclare(V);
1509 if (!DDI) return false;
1510 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1512 StringRef D = Var.getName();
1515 LineNo = Var.getLineNumber();
1516 Unit = Var.getCompileUnit();
1517 TypeD = Var.getType();
1520 StringRef T = TypeD.getName();
1523 StringRef F = Unit.getFilename();
1526 StringRef D = Unit.getDirectory();
1532 /// getDISubprogram - Find subprogram that is enclosing this scope.
1533 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
1534 DIDescriptor D(Scope);
1535 if (D.isSubprogram())
1536 return DISubprogram(Scope);
1538 if (D.isLexicalBlock())
1539 return getDISubprogram(DILexicalBlock(Scope).getContext());
1541 return DISubprogram();
1544 /// getDICompositeType - Find underlying composite type.
1545 DICompositeType llvm::getDICompositeType(DIType T) {
1546 if (T.isCompositeType())
1547 return DICompositeType(T);
1549 if (T.isDerivedType())
1550 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
1552 return DICompositeType();