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 //===----------------------------------------------------------------------===//
36 DIDescriptor::getStringField(unsigned Elt) const {
40 if (Elt < DbgNode->getNumOperands())
41 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
42 return MDS->getString();
47 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
51 if (Elt < DbgNode->getNumOperands())
52 if (ConstantInt *CI = dyn_cast<ConstantInt>(DbgNode->getOperand(Elt)))
53 return CI->getZExtValue();
58 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
60 return DIDescriptor();
62 if (Elt < DbgNode->getNumOperands())
63 return DIDescriptor(dyn_cast_or_null<const MDNode>(DbgNode->getOperand(Elt)));
64 return DIDescriptor();
67 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
71 if (Elt < DbgNode->getNumOperands())
72 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
76 Function *DIDescriptor::getFunctionField(unsigned Elt) const {
80 if (Elt < DbgNode->getNumOperands())
81 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
85 unsigned DIVariable::getNumAddrElements() const {
86 return DbgNode->getNumOperands()-6;
90 //===----------------------------------------------------------------------===//
92 //===----------------------------------------------------------------------===//
94 /// isBasicType - Return true if the specified tag is legal for
96 bool DIDescriptor::isBasicType() const {
97 return DbgNode && getTag() == dwarf::DW_TAG_base_type;
100 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
101 bool DIDescriptor::isDerivedType() const {
102 if (!DbgNode) return false;
104 case dwarf::DW_TAG_typedef:
105 case dwarf::DW_TAG_pointer_type:
106 case dwarf::DW_TAG_reference_type:
107 case dwarf::DW_TAG_const_type:
108 case dwarf::DW_TAG_volatile_type:
109 case dwarf::DW_TAG_restrict_type:
110 case dwarf::DW_TAG_member:
111 case dwarf::DW_TAG_inheritance:
114 // CompositeTypes are currently modelled as DerivedTypes.
115 return isCompositeType();
119 /// isCompositeType - Return true if the specified tag is legal for
121 bool DIDescriptor::isCompositeType() const {
122 if (!DbgNode) return false;
124 case dwarf::DW_TAG_array_type:
125 case dwarf::DW_TAG_structure_type:
126 case dwarf::DW_TAG_union_type:
127 case dwarf::DW_TAG_enumeration_type:
128 case dwarf::DW_TAG_vector_type:
129 case dwarf::DW_TAG_subroutine_type:
130 case dwarf::DW_TAG_class_type:
137 /// isVariable - Return true if the specified tag is legal for DIVariable.
138 bool DIDescriptor::isVariable() const {
139 if (!DbgNode) return false;
141 case dwarf::DW_TAG_auto_variable:
142 case dwarf::DW_TAG_arg_variable:
143 case dwarf::DW_TAG_return_variable:
150 /// isType - Return true if the specified tag is legal for DIType.
151 bool DIDescriptor::isType() const {
152 return isBasicType() || isCompositeType() || isDerivedType();
155 /// isSubprogram - Return true if the specified tag is legal for
157 bool DIDescriptor::isSubprogram() const {
158 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
161 /// isGlobalVariable - Return true if the specified tag is legal for
162 /// DIGlobalVariable.
163 bool DIDescriptor::isGlobalVariable() const {
164 return DbgNode && getTag() == dwarf::DW_TAG_variable;
167 /// isGlobal - Return true if the specified tag is legal for DIGlobal.
168 bool DIDescriptor::isGlobal() const {
169 return isGlobalVariable();
172 /// isScope - Return true if the specified tag is one of the scope
174 bool DIDescriptor::isScope() const {
175 if (!DbgNode) return false;
177 case dwarf::DW_TAG_compile_unit:
178 case dwarf::DW_TAG_lexical_block:
179 case dwarf::DW_TAG_subprogram:
180 case dwarf::DW_TAG_namespace:
188 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
189 bool DIDescriptor::isCompileUnit() const {
190 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
193 /// isFile - Return true if the specified tag is DW_TAG_file_type.
194 bool DIDescriptor::isFile() const {
195 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
198 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
199 bool DIDescriptor::isNameSpace() const {
200 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
203 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
204 bool DIDescriptor::isLexicalBlock() const {
205 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
208 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
209 bool DIDescriptor::isSubrange() const {
210 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
213 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
214 bool DIDescriptor::isEnumerator() const {
215 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
218 //===----------------------------------------------------------------------===//
219 // Simple Descriptor Constructors and other Methods
220 //===----------------------------------------------------------------------===//
222 DIType::DIType(const MDNode *N) : DIScope(N) {
224 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
229 unsigned DIArray::getNumElements() const {
232 return DbgNode->getNumOperands();
235 /// replaceAllUsesWith - Replace all uses of debug info referenced by
236 /// this descriptor. After this completes, the current debug info value
238 void DIDerivedType::replaceAllUsesWith(DIDescriptor &D) {
242 // Since we use a TrackingVH for the node, its easy for clients to manufacture
243 // legitimate situations where they want to replaceAllUsesWith() on something
244 // which, due to uniquing, has merged with the source. We shield clients from
245 // this detail by allowing a value to be replaced with replaceAllUsesWith()
248 MDNode *Node = const_cast<MDNode*>(DbgNode);
249 const MDNode *DN = D;
250 const Value *V = cast_or_null<Value>(DN);
251 Node->replaceAllUsesWith(const_cast<Value*>(V));
256 /// Verify - Verify that a compile unit is well formed.
257 bool DICompileUnit::Verify() const {
260 StringRef N = getFilename();
263 // It is possible that directory and produce string is empty.
267 /// Verify - Verify that a type descriptor is well formed.
268 bool DIType::Verify() const {
271 if (!getContext().Verify())
274 DICompileUnit CU = getCompileUnit();
280 /// Verify - Verify that a composite type descriptor is well formed.
281 bool DICompositeType::Verify() const {
284 if (!getContext().Verify())
287 DICompileUnit CU = getCompileUnit();
293 /// Verify - Verify that a subprogram descriptor is well formed.
294 bool DISubprogram::Verify() const {
298 if (!getContext().Verify())
301 DICompileUnit CU = getCompileUnit();
305 DICompositeType Ty = getType();
311 /// Verify - Verify that a global variable descriptor is well formed.
312 bool DIGlobalVariable::Verify() const {
316 if (getDisplayName().empty())
319 if (!getContext().Verify())
322 DICompileUnit CU = getCompileUnit();
326 DIType Ty = getType();
336 /// Verify - Verify that a variable descriptor is well formed.
337 bool DIVariable::Verify() const {
341 if (!getContext().Verify())
344 if (!getCompileUnit().Verify())
347 DIType Ty = getType();
354 /// Verify - Verify that a location descriptor is well formed.
355 bool DILocation::Verify() const {
359 return DbgNode->getNumOperands() == 4;
362 /// Verify - Verify that a namespace descriptor is well formed.
363 bool DINameSpace::Verify() const {
366 if (getName().empty())
368 if (!getCompileUnit().Verify())
373 /// getOriginalTypeSize - If this type is derived from a base type then
374 /// return base type size.
375 uint64_t DIDerivedType::getOriginalTypeSize() const {
376 unsigned Tag = getTag();
377 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
378 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
379 Tag == dwarf::DW_TAG_restrict_type) {
380 DIType BaseType = getTypeDerivedFrom();
381 // If this type is not derived from any type then take conservative
383 if (!BaseType.isValid())
384 return getSizeInBits();
385 if (BaseType.isDerivedType())
386 return DIDerivedType(BaseType).getOriginalTypeSize();
388 return BaseType.getSizeInBits();
391 return getSizeInBits();
394 /// isInlinedFnArgument - Return trule if this variable provides debugging
395 /// information for an inlined function arguments.
396 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
397 assert(CurFn && "Invalid function");
398 if (!getContext().isSubprogram())
400 // This variable is not inlined function argument if its scope
401 // does not describe current function.
402 return !(DISubprogram(getContext()).describes(CurFn));
405 /// describes - Return true if this subprogram provides debugging
406 /// information for the function F.
407 bool DISubprogram::describes(const Function *F) {
408 assert(F && "Invalid function");
409 if (F == getFunction())
411 StringRef Name = getLinkageName();
414 if (F->getName() == Name)
419 unsigned DISubprogram::isOptimized() const {
420 assert (DbgNode && "Invalid subprogram descriptor!");
421 if (DbgNode->getNumOperands() == 16)
422 return getUnsignedField(15);
426 StringRef DIScope::getFilename() const {
429 if (isLexicalBlock())
430 return DILexicalBlock(DbgNode).getFilename();
432 return DISubprogram(DbgNode).getFilename();
434 return DICompileUnit(DbgNode).getFilename();
436 return DINameSpace(DbgNode).getFilename();
438 return DIType(DbgNode).getFilename();
440 return DIFile(DbgNode).getFilename();
441 assert(0 && "Invalid DIScope!");
445 StringRef DIScope::getDirectory() const {
448 if (isLexicalBlock())
449 return DILexicalBlock(DbgNode).getDirectory();
451 return DISubprogram(DbgNode).getDirectory();
453 return DICompileUnit(DbgNode).getDirectory();
455 return DINameSpace(DbgNode).getDirectory();
457 return DIType(DbgNode).getDirectory();
459 return DIFile(DbgNode).getDirectory();
460 assert(0 && "Invalid DIScope!");
464 //===----------------------------------------------------------------------===//
465 // DIDescriptor: dump routines for all descriptors.
466 //===----------------------------------------------------------------------===//
469 /// print - Print descriptor.
470 void DIDescriptor::print(raw_ostream &OS) const {
471 OS << "[" << dwarf::TagString(getTag()) << "] ";
472 OS.write_hex((intptr_t) &*DbgNode) << ']';
475 /// print - Print compile unit.
476 void DICompileUnit::print(raw_ostream &OS) const {
478 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
480 OS << " [" << getDirectory() << "/" << getFilename() << "]";
483 /// print - Print type.
484 void DIType::print(raw_ostream &OS) const {
485 if (!DbgNode) return;
487 StringRef Res = getName();
489 OS << " [" << Res << "] ";
491 unsigned Tag = getTag();
492 OS << " [" << dwarf::TagString(Tag) << "] ";
494 // TODO : Print context
495 getCompileUnit().print(OS);
497 << "line " << getLineNumber() << ", "
498 << getSizeInBits() << " bits, "
499 << getAlignInBits() << " bit alignment, "
500 << getOffsetInBits() << " bit offset"
505 else if (isProtected())
506 OS << " [protected] ";
512 DIBasicType(DbgNode).print(OS);
513 else if (isDerivedType())
514 DIDerivedType(DbgNode).print(OS);
515 else if (isCompositeType())
516 DICompositeType(DbgNode).print(OS);
518 OS << "Invalid DIType\n";
525 /// print - Print basic type.
526 void DIBasicType::print(raw_ostream &OS) const {
527 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
530 /// print - Print derived type.
531 void DIDerivedType::print(raw_ostream &OS) const {
532 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
535 /// print - Print composite type.
536 void DICompositeType::print(raw_ostream &OS) const {
537 DIArray A = getTypeArray();
538 OS << " [" << A.getNumElements() << " elements]";
541 /// print - Print subprogram.
542 void DISubprogram::print(raw_ostream &OS) const {
543 StringRef Res = getName();
545 OS << " [" << Res << "] ";
547 unsigned Tag = getTag();
548 OS << " [" << dwarf::TagString(Tag) << "] ";
550 // TODO : Print context
551 getCompileUnit().print(OS);
552 OS << " [" << getLineNumber() << "] ";
563 /// print - Print global variable.
564 void DIGlobalVariable::print(raw_ostream &OS) const {
566 StringRef Res = getName();
568 OS << " [" << Res << "] ";
570 unsigned Tag = getTag();
571 OS << " [" << dwarf::TagString(Tag) << "] ";
573 // TODO : Print context
574 getCompileUnit().print(OS);
575 OS << " [" << getLineNumber() << "] ";
583 if (isGlobalVariable())
584 DIGlobalVariable(DbgNode).print(OS);
588 /// print - Print variable.
589 void DIVariable::print(raw_ostream &OS) const {
590 StringRef Res = getName();
592 OS << " [" << Res << "] ";
594 getCompileUnit().print(OS);
595 OS << " [" << getLineNumber() << "] ";
599 // FIXME: Dump complex addresses
602 /// dump - Print descriptor to dbgs() with a newline.
603 void DIDescriptor::dump() const {
604 print(dbgs()); dbgs() << '\n';
607 /// dump - Print compile unit to dbgs() with a newline.
608 void DICompileUnit::dump() const {
609 print(dbgs()); dbgs() << '\n';
612 /// dump - Print type to dbgs() with a newline.
613 void DIType::dump() const {
614 print(dbgs()); dbgs() << '\n';
617 /// dump - Print basic type to dbgs() with a newline.
618 void DIBasicType::dump() const {
619 print(dbgs()); dbgs() << '\n';
622 /// dump - Print derived type to dbgs() with a newline.
623 void DIDerivedType::dump() const {
624 print(dbgs()); dbgs() << '\n';
627 /// dump - Print composite type to dbgs() with a newline.
628 void DICompositeType::dump() const {
629 print(dbgs()); dbgs() << '\n';
632 /// dump - Print subprogram to dbgs() with a newline.
633 void DISubprogram::dump() const {
634 print(dbgs()); dbgs() << '\n';
637 /// dump - Print global variable.
638 void DIGlobalVariable::dump() const {
639 print(dbgs()); dbgs() << '\n';
642 /// dump - Print variable.
643 void DIVariable::dump() const {
644 print(dbgs()); dbgs() << '\n';
647 //===----------------------------------------------------------------------===//
648 // DIFactory: Basic Helpers
649 //===----------------------------------------------------------------------===//
651 DIFactory::DIFactory(Module &m)
652 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
654 Constant *DIFactory::GetTagConstant(unsigned TAG) {
655 assert((TAG & LLVMDebugVersionMask) == 0 &&
656 "Tag too large for debug encoding!");
657 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
660 //===----------------------------------------------------------------------===//
661 // DIFactory: Primary Constructors
662 //===----------------------------------------------------------------------===//
664 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
665 /// This implicitly uniques the arrays created.
666 DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
667 SmallVector<Value*, 16> Elts;
670 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
672 for (unsigned i = 0; i != NumTys; ++i)
673 Elts.push_back(Tys[i]);
675 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
678 /// GetOrCreateSubrange - Create a descriptor for a value range. This
679 /// implicitly uniques the values returned.
680 DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
682 GetTagConstant(dwarf::DW_TAG_subrange_type),
683 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
684 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
687 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
692 /// CreateCompileUnit - Create a new descriptor for the specified compile
693 /// unit. Note that this does not unique compile units within the module.
694 DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
701 unsigned RunTimeVer) {
703 GetTagConstant(dwarf::DW_TAG_compile_unit),
704 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
705 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
706 MDString::get(VMContext, Filename),
707 MDString::get(VMContext, Directory),
708 MDString::get(VMContext, Producer),
709 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
710 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
711 MDString::get(VMContext, Flags),
712 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
715 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
718 /// CreateFile - Create a new descriptor for the specified file.
719 DIFile DIFactory::CreateFile(StringRef Filename,
723 GetTagConstant(dwarf::DW_TAG_file_type),
724 MDString::get(VMContext, Filename),
725 MDString::get(VMContext, Directory),
729 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
732 /// CreateEnumerator - Create a single enumerator value.
733 DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
735 GetTagConstant(dwarf::DW_TAG_enumerator),
736 MDString::get(VMContext, Name),
737 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
739 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
743 /// CreateBasicType - Create a basic type like int, float, etc.
744 DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
749 uint64_t AlignInBits,
750 uint64_t OffsetInBits, unsigned Flags,
753 GetTagConstant(dwarf::DW_TAG_base_type),
755 MDString::get(VMContext, Name),
757 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
758 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
759 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
760 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
761 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
762 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
764 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
768 /// CreateBasicType - Create a basic type like int, float, etc.
769 DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
773 Constant *SizeInBits,
774 Constant *AlignInBits,
775 Constant *OffsetInBits, unsigned Flags,
778 GetTagConstant(dwarf::DW_TAG_base_type),
780 MDString::get(VMContext, Name),
782 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
786 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
787 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
789 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
792 /// CreateArtificialType - Create a new DIType with "artificial" flag set.
793 DIType DIFactory::CreateArtificialType(DIType Ty) {
794 if (Ty.isArtificial())
797 SmallVector<Value *, 9> Elts;
799 assert (N && "Unexpected input DIType!");
800 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
801 if (Value *V = N->getOperand(i))
804 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
807 unsigned CurFlags = Ty.getFlags();
808 CurFlags = CurFlags | DIType::FlagArtificial;
810 // Flags are stored at this slot.
811 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
813 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
816 /// CreateDerivedType - Create a derived type like const qualified type,
817 /// pointer, typedef, etc.
818 DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
819 DIDescriptor Context,
824 uint64_t AlignInBits,
825 uint64_t OffsetInBits,
827 DIType DerivedFrom) {
831 MDString::get(VMContext, Name),
833 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
834 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
835 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
836 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
837 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
840 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
844 /// CreateDerivedType - Create a derived type like const qualified type,
845 /// pointer, typedef, etc.
846 DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
847 DIDescriptor Context,
851 Constant *SizeInBits,
852 Constant *AlignInBits,
853 Constant *OffsetInBits,
855 DIType DerivedFrom) {
859 MDString::get(VMContext, Name),
861 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
865 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
868 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
872 /// CreateCompositeType - Create a composite type like array, struct, etc.
873 DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
874 DIDescriptor Context,
879 uint64_t AlignInBits,
880 uint64_t OffsetInBits,
884 unsigned RuntimeLang,
885 MDNode *ContainingType) {
890 MDString::get(VMContext, Name),
892 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
893 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
894 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
895 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
896 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
899 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
902 return DICompositeType(MDNode::get(VMContext, &Elts[0], 13));
906 /// CreateCompositeType - Create a composite type like array, struct, etc.
907 DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
908 DIDescriptor Context,
912 Constant *SizeInBits,
913 Constant *AlignInBits,
914 Constant *OffsetInBits,
918 unsigned RuntimeLang) {
923 MDString::get(VMContext, Name),
925 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
929 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
932 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
934 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
938 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
939 /// See comments in DISubprogram for descriptions of these fields. This
940 /// method does not unique the generated descriptors.
941 DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
943 StringRef DisplayName,
944 StringRef LinkageName,
946 unsigned LineNo, DIType Ty,
949 unsigned VK, unsigned VIndex,
950 DIType ContainingType,
956 GetTagConstant(dwarf::DW_TAG_subprogram),
957 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
959 MDString::get(VMContext, Name),
960 MDString::get(VMContext, DisplayName),
961 MDString::get(VMContext, LinkageName),
963 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
965 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
966 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
967 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
968 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
970 ConstantInt::get(Type::getInt1Ty(VMContext), isArtificial),
971 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
974 return DISubprogram(MDNode::get(VMContext, &Elts[0], 17));
977 /// CreateSubprogramDefinition - Create new subprogram descriptor for the
978 /// given declaration.
979 DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration) {
980 if (SPDeclaration.isDefinition())
981 return DISubprogram(SPDeclaration);
983 MDNode *DeclNode = SPDeclaration;
985 GetTagConstant(dwarf::DW_TAG_subprogram),
986 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
987 DeclNode->getOperand(2), // Context
988 DeclNode->getOperand(3), // Name
989 DeclNode->getOperand(4), // DisplayName
990 DeclNode->getOperand(5), // LinkageName
991 DeclNode->getOperand(6), // CompileUnit
992 DeclNode->getOperand(7), // LineNo
993 DeclNode->getOperand(8), // Type
994 DeclNode->getOperand(9), // isLocalToUnit
995 ConstantInt::get(Type::getInt1Ty(VMContext), true),
996 DeclNode->getOperand(11), // Virtuality
997 DeclNode->getOperand(12), // VIndex
998 DeclNode->getOperand(13), // Containting Type
999 DeclNode->getOperand(14), // isArtificial
1000 DeclNode->getOperand(15) // isOptimized
1002 return DISubprogram(MDNode::get(VMContext, &Elts[0], 16));
1005 /// CreateGlobalVariable - Create a new descriptor for the specified global.
1007 DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1008 StringRef DisplayName,
1009 StringRef LinkageName,
1011 unsigned LineNo, DIType Ty,bool isLocalToUnit,
1012 bool isDefinition, llvm::GlobalVariable *Val) {
1014 GetTagConstant(dwarf::DW_TAG_variable),
1015 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1017 MDString::get(VMContext, Name),
1018 MDString::get(VMContext, DisplayName),
1019 MDString::get(VMContext, LinkageName),
1021 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1023 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1024 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1028 Value *const *Vs = &Elts[0];
1029 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1031 // Create a named metadata so that we do not lose this mdnode.
1032 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1033 NMD->addOperand(Node);
1035 return DIGlobalVariable(Node);
1039 /// CreateVariable - Create a new descriptor for the specified variable.
1040 DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
1044 DIType Ty, bool AlwaysPreserve) {
1046 GetTagConstant(Tag),
1048 MDString::get(VMContext, Name),
1050 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1053 MDNode *Node = MDNode::get(VMContext, &Elts[0], 6);
1054 if (AlwaysPreserve) {
1055 // The optimizer may remove local variable. If there is an interest
1056 // to preserve variable info in such situation then stash it in a
1058 DISubprogram Fn(getDISubprogram(Context));
1059 StringRef FName = "fn";
1060 if (Fn.getFunction())
1061 FName = Fn.getFunction()->getName();
1062 const Twine FnLVName = Twine("llvm.dbg.lv.", FName);
1064 if (FName.startswith(StringRef(&One, 1)))
1065 FName = FName.substr(1);
1067 NamedMDNode *FnLocals = M.getNamedMetadataUsingTwine(FnLVName);
1069 FnLocals = NamedMDNode::Create(VMContext, FnLVName, NULL, 0, &M);
1070 FnLocals->addOperand(Node);
1072 return DIVariable(Node);
1076 /// CreateComplexVariable - Create a new descriptor for the specified variable
1077 /// which has a complex address expression for its address.
1078 DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
1079 const std::string &Name,
1083 SmallVector<Value *, 9> &addr) {
1084 SmallVector<Value *, 9> Elts;
1085 Elts.push_back(GetTagConstant(Tag));
1086 Elts.push_back(Context);
1087 Elts.push_back(MDString::get(VMContext, Name));
1089 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
1091 Elts.insert(Elts.end(), addr.begin(), addr.end());
1093 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
1097 /// CreateBlock - This creates a descriptor for a lexical block with the
1098 /// specified parent VMContext.
1099 DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
1100 unsigned LineNo, unsigned Col) {
1102 GetTagConstant(dwarf::DW_TAG_lexical_block),
1104 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1105 ConstantInt::get(Type::getInt32Ty(VMContext), Col)
1107 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 4));
1110 /// CreateNameSpace - This creates new descriptor for a namespace
1111 /// with the specified parent context.
1112 DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
1116 GetTagConstant(dwarf::DW_TAG_namespace),
1118 MDString::get(VMContext, Name),
1120 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1122 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1125 /// CreateLocation - Creates a debug info location.
1126 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1127 DIScope S, DILocation OrigLoc) {
1129 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1130 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1134 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1137 //===----------------------------------------------------------------------===//
1138 // DIFactory: Routines for inserting code into a function
1139 //===----------------------------------------------------------------------===//
1141 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1142 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1143 Instruction *InsertBefore) {
1144 assert(Storage && "no storage passed to dbg.declare");
1145 assert(D.Verify() && "empty DIVariable passed to dbg.declare");
1147 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1149 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1151 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
1154 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1155 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1156 BasicBlock *InsertAtEnd) {
1157 assert(Storage && "no storage passed to dbg.declare");
1158 assert(D.Verify() && "invalid DIVariable passed to dbg.declare");
1160 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1162 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1165 // If this block already has a terminator then insert this intrinsic
1166 // before the terminator.
1167 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1168 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1170 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
1172 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1173 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1175 Instruction *InsertBefore) {
1176 assert(V && "no value passed to dbg.value");
1177 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
1179 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1181 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1182 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1184 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1187 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1188 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1190 BasicBlock *InsertAtEnd) {
1191 assert(V && "no value passed to dbg.value");
1192 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
1194 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1196 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1197 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1199 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1202 //===----------------------------------------------------------------------===//
1203 // DebugInfoFinder implementations.
1204 //===----------------------------------------------------------------------===//
1206 /// processModule - Process entire module and collect debug info.
1207 void DebugInfoFinder::processModule(Module &M) {
1208 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1209 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1210 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1212 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1213 processDeclare(DDI);
1215 DebugLoc Loc = BI->getDebugLoc();
1216 if (Loc.isUnknown())
1219 LLVMContext &Ctx = BI->getContext();
1220 DIDescriptor Scope(Loc.getScope(Ctx));
1222 if (Scope.isCompileUnit())
1223 addCompileUnit(DICompileUnit(Scope));
1224 else if (Scope.isSubprogram())
1225 processSubprogram(DISubprogram(Scope));
1226 else if (Scope.isLexicalBlock())
1227 processLexicalBlock(DILexicalBlock(Scope));
1229 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1230 processLocation(DILocation(IA));
1233 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
1237 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1238 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1239 if (addGlobalVariable(DIG)) {
1240 addCompileUnit(DIG.getCompileUnit());
1241 processType(DIG.getType());
1246 /// processLocation - Process DILocation.
1247 void DebugInfoFinder::processLocation(DILocation Loc) {
1248 if (!Loc.Verify()) return;
1249 DIDescriptor S(Loc.getScope());
1250 if (S.isCompileUnit())
1251 addCompileUnit(DICompileUnit(S));
1252 else if (S.isSubprogram())
1253 processSubprogram(DISubprogram(S));
1254 else if (S.isLexicalBlock())
1255 processLexicalBlock(DILexicalBlock(S));
1256 processLocation(Loc.getOrigLocation());
1259 /// processType - Process DIType.
1260 void DebugInfoFinder::processType(DIType DT) {
1264 addCompileUnit(DT.getCompileUnit());
1265 if (DT.isCompositeType()) {
1266 DICompositeType DCT(DT);
1267 processType(DCT.getTypeDerivedFrom());
1268 DIArray DA = DCT.getTypeArray();
1269 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1270 DIDescriptor D = DA.getElement(i);
1272 processType(DIType(D));
1273 else if (D.isSubprogram())
1274 processSubprogram(DISubprogram(D));
1276 } else if (DT.isDerivedType()) {
1277 DIDerivedType DDT(DT);
1278 processType(DDT.getTypeDerivedFrom());
1282 /// processLexicalBlock
1283 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1284 DIScope Context = LB.getContext();
1285 if (Context.isLexicalBlock())
1286 return processLexicalBlock(DILexicalBlock(Context));
1288 return processSubprogram(DISubprogram(Context));
1291 /// processSubprogram - Process DISubprogram.
1292 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1293 if (!addSubprogram(SP))
1295 addCompileUnit(SP.getCompileUnit());
1296 processType(SP.getType());
1299 /// processDeclare - Process DbgDeclareInst.
1300 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
1301 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1305 if (!DV.isVariable())
1308 if (!NodesSeen.insert(DV))
1311 addCompileUnit(DIVariable(N).getCompileUnit());
1312 processType(DIVariable(N).getType());
1315 /// addType - Add type into Tys.
1316 bool DebugInfoFinder::addType(DIType DT) {
1320 if (!NodesSeen.insert(DT))
1327 /// addCompileUnit - Add compile unit into CUs.
1328 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1332 if (!NodesSeen.insert(CU))
1339 /// addGlobalVariable - Add global variable into GVs.
1340 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1341 if (!DIDescriptor(DIG).isGlobalVariable())
1344 if (!NodesSeen.insert(DIG))
1351 // addSubprogram - Add subprgoram into SPs.
1352 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1353 if (!DIDescriptor(SP).isSubprogram())
1356 if (!NodesSeen.insert(SP))
1363 /// Find the debug info descriptor corresponding to this global variable.
1364 static Value *findDbgGlobalDeclare(GlobalVariable *V) {
1365 const Module *M = V->getParent();
1366 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1370 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1371 DIDescriptor DIG(cast_or_null<MDNode>(NMD->getOperand(i)));
1372 if (!DIG.isGlobalVariable())
1374 if (DIGlobalVariable(DIG).getGlobal() == V)
1380 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1381 /// It looks through pointer casts too.
1382 static const DbgDeclareInst *findDbgDeclare(const Value *V) {
1383 V = V->stripPointerCasts();
1385 if (!isa<Instruction>(V) && !isa<Argument>(V))
1388 const Function *F = NULL;
1389 if (const Instruction *I = dyn_cast<Instruction>(V))
1390 F = I->getParent()->getParent();
1391 else if (const Argument *A = dyn_cast<Argument>(V))
1394 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1395 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1397 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1398 if (DDI->getAddress() == V)
1404 bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1405 std::string &Type, unsigned &LineNo,
1406 std::string &File, std::string &Dir) {
1410 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1411 Value *DIGV = findDbgGlobalDeclare(GV);
1412 if (!DIGV) return false;
1413 DIGlobalVariable Var(cast<MDNode>(DIGV));
1415 StringRef D = Var.getDisplayName();
1418 LineNo = Var.getLineNumber();
1419 Unit = Var.getCompileUnit();
1420 TypeD = Var.getType();
1422 const DbgDeclareInst *DDI = findDbgDeclare(V);
1423 if (!DDI) return false;
1424 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1426 StringRef D = Var.getName();
1429 LineNo = Var.getLineNumber();
1430 Unit = Var.getCompileUnit();
1431 TypeD = Var.getType();
1434 StringRef T = TypeD.getName();
1437 StringRef F = Unit.getFilename();
1440 StringRef D = Unit.getDirectory();
1446 /// getDISubprogram - Find subprogram that is enclosing this scope.
1447 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
1448 DIDescriptor D(Scope);
1449 if (D.isSubprogram())
1450 return DISubprogram(Scope);
1452 if (D.isLexicalBlock())
1453 return getDISubprogram(DILexicalBlock(Scope).getContext());
1455 return DISubprogram();
1458 /// getDICompositeType - Find underlying composite type.
1459 DICompositeType llvm::getDICompositeType(DIType T) {
1460 if (T.isCompositeType())
1461 return DICompositeType(T);
1463 if (T.isDerivedType())
1464 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
1466 return DICompositeType();