1 //===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the helper classes used to build and interpret debug
11 // information in LLVM IR form.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Analysis/DebugInfo.h"
16 #include "llvm/Target/TargetMachine.h" // FIXME: LAYERING VIOLATION!
17 #include "llvm/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Intrinsics.h"
20 #include "llvm/IntrinsicInst.h"
21 #include "llvm/Instructions.h"
22 #include "llvm/Module.h"
23 #include "llvm/Analysis/ValueTracking.h"
24 #include "llvm/ADT/SmallPtrSet.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/Dwarf.h"
27 #include "llvm/Support/raw_ostream.h"
29 using namespace llvm::dwarf;
31 //===----------------------------------------------------------------------===//
33 //===----------------------------------------------------------------------===//
35 /// ValidDebugInfo - Return true if V represents valid debug info value.
36 /// FIXME : Add DIDescriptor.isValid()
37 bool DIDescriptor::ValidDebugInfo(MDNode *N, unsigned OptLevel) {
43 // Check current version. Allow Version7 for now.
44 unsigned Version = DI.getVersion();
45 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion7)
48 switch (DI.getTag()) {
50 assert(DIVariable(N).Verify() && "Invalid DebugInfo value");
52 case DW_TAG_compile_unit:
53 assert(DICompileUnit(N).Verify() && "Invalid DebugInfo value");
55 case DW_TAG_subprogram:
56 assert(DISubprogram(N).Verify() && "Invalid DebugInfo value");
58 case DW_TAG_lexical_block:
59 // FIXME: This interfers with the quality of generated code during
61 if (OptLevel != CodeGenOpt::None)
72 DIDescriptor::getStringField(unsigned Elt) const {
76 if (Elt < DbgNode->getNumOperands())
77 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
78 return MDS->getString();
83 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
87 if (Elt < DbgNode->getNumOperands())
88 if (ConstantInt *CI = dyn_cast<ConstantInt>(DbgNode->getOperand(Elt)))
89 return CI->getZExtValue();
94 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
96 return DIDescriptor();
98 if (Elt < DbgNode->getNumOperands())
99 return DIDescriptor(dyn_cast_or_null<MDNode>(DbgNode->getOperand(Elt)));
100 return DIDescriptor();
103 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
107 if (Elt < DbgNode->getNumOperands())
108 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
112 unsigned DIVariable::getNumAddrElements() const {
113 return DbgNode->getNumOperands()-6;
117 //===----------------------------------------------------------------------===//
119 //===----------------------------------------------------------------------===//
121 /// isBasicType - Return true if the specified tag is legal for
123 bool DIDescriptor::isBasicType() const {
124 return DbgNode && getTag() == dwarf::DW_TAG_base_type;
127 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
128 bool DIDescriptor::isDerivedType() const {
129 if (!DbgNode) return false;
131 case dwarf::DW_TAG_typedef:
132 case dwarf::DW_TAG_pointer_type:
133 case dwarf::DW_TAG_reference_type:
134 case dwarf::DW_TAG_const_type:
135 case dwarf::DW_TAG_volatile_type:
136 case dwarf::DW_TAG_restrict_type:
137 case dwarf::DW_TAG_member:
138 case dwarf::DW_TAG_inheritance:
141 // CompositeTypes are currently modelled as DerivedTypes.
142 return isCompositeType();
146 /// isCompositeType - Return true if the specified tag is legal for
148 bool DIDescriptor::isCompositeType() const {
149 if (!DbgNode) return false;
151 case dwarf::DW_TAG_array_type:
152 case dwarf::DW_TAG_structure_type:
153 case dwarf::DW_TAG_union_type:
154 case dwarf::DW_TAG_enumeration_type:
155 case dwarf::DW_TAG_vector_type:
156 case dwarf::DW_TAG_subroutine_type:
157 case dwarf::DW_TAG_class_type:
164 /// isVariable - Return true if the specified tag is legal for DIVariable.
165 bool DIDescriptor::isVariable() const {
166 if (!DbgNode) return false;
168 case dwarf::DW_TAG_auto_variable:
169 case dwarf::DW_TAG_arg_variable:
170 case dwarf::DW_TAG_return_variable:
177 /// isType - Return true if the specified tag is legal for DIType.
178 bool DIDescriptor::isType() const {
179 return isBasicType() || isCompositeType() || isDerivedType();
182 /// isSubprogram - Return true if the specified tag is legal for
184 bool DIDescriptor::isSubprogram() const {
185 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
188 /// isGlobalVariable - Return true if the specified tag is legal for
189 /// DIGlobalVariable.
190 bool DIDescriptor::isGlobalVariable() const {
191 return DbgNode && getTag() == dwarf::DW_TAG_variable;
194 /// isGlobal - Return true if the specified tag is legal for DIGlobal.
195 bool DIDescriptor::isGlobal() const {
196 return isGlobalVariable();
199 /// isScope - Return true if the specified tag is one of the scope
201 bool DIDescriptor::isScope() const {
202 if (!DbgNode) return false;
204 case dwarf::DW_TAG_compile_unit:
205 case dwarf::DW_TAG_lexical_block:
206 case dwarf::DW_TAG_subprogram:
207 case dwarf::DW_TAG_namespace:
215 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
216 bool DIDescriptor::isCompileUnit() const {
217 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
220 /// isFile - Return true if the specified tag is DW_TAG_file_type.
221 bool DIDescriptor::isFile() const {
222 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
225 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
226 bool DIDescriptor::isNameSpace() const {
227 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
230 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
231 bool DIDescriptor::isLexicalBlock() const {
232 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
235 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
236 bool DIDescriptor::isSubrange() const {
237 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
240 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
241 bool DIDescriptor::isEnumerator() const {
242 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
245 //===----------------------------------------------------------------------===//
246 // Simple Descriptor Constructors and other Methods
247 //===----------------------------------------------------------------------===//
249 DIType::DIType(MDNode *N) : DIScope(N) {
251 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
256 unsigned DIArray::getNumElements() const {
259 return DbgNode->getNumOperands();
262 /// replaceAllUsesWith - Replace all uses of debug info referenced by
263 /// this descriptor. After this completes, the current debug info value
265 void DIDerivedType::replaceAllUsesWith(DIDescriptor &D) {
269 // Since we use a TrackingVH for the node, its easy for clients to manufacture
270 // legitimate situations where they want to replaceAllUsesWith() on something
271 // which, due to uniquing, has merged with the source. We shield clients from
272 // this detail by allowing a value to be replaced with replaceAllUsesWith()
274 if (getNode() != D) {
275 MDNode *Node = DbgNode;
276 Node->replaceAllUsesWith(D);
281 /// Verify - Verify that a compile unit is well formed.
282 bool DICompileUnit::Verify() const {
285 StringRef N = getFilename();
288 // It is possible that directory and produce string is empty.
292 /// Verify - Verify that a type descriptor is well formed.
293 bool DIType::Verify() const {
296 if (!getContext().Verify())
299 DICompileUnit CU = getCompileUnit();
305 /// Verify - Verify that a composite type descriptor is well formed.
306 bool DICompositeType::Verify() const {
309 if (!getContext().Verify())
312 DICompileUnit CU = getCompileUnit();
318 /// Verify - Verify that a subprogram descriptor is well formed.
319 bool DISubprogram::Verify() const {
323 if (!getContext().Verify())
326 DICompileUnit CU = getCompileUnit();
330 DICompositeType Ty = getType();
336 /// Verify - Verify that a global variable descriptor is well formed.
337 bool DIGlobalVariable::Verify() const {
341 if (getDisplayName().empty())
344 if (!getContext().Verify())
347 DICompileUnit CU = getCompileUnit();
351 DIType Ty = getType();
361 /// Verify - Verify that a variable descriptor is well formed.
362 bool DIVariable::Verify() const {
366 if (!getContext().Verify())
369 DIType Ty = getType();
376 /// Verify - Verify that a location descriptor is well formed.
377 bool DILocation::Verify() const {
381 return DbgNode->getNumOperands() == 4;
384 /// getOriginalTypeSize - If this type is derived from a base type then
385 /// return base type size.
386 uint64_t DIDerivedType::getOriginalTypeSize() const {
387 unsigned Tag = getTag();
388 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
389 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
390 Tag == dwarf::DW_TAG_restrict_type) {
391 DIType BaseType = getTypeDerivedFrom();
392 // If this type is not derived from any type then take conservative
394 if (!BaseType.isValid())
395 return getSizeInBits();
396 if (BaseType.isDerivedType())
397 return DIDerivedType(BaseType).getOriginalTypeSize();
399 return BaseType.getSizeInBits();
402 return getSizeInBits();
405 /// isInlinedFnArgument - Return trule if this variable provides debugging
406 /// information for an inlined function arguments.
407 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
408 assert(CurFn && "Invalid function");
409 if (!getContext().isSubprogram())
411 // This variable is not inlined function argument if its scope
412 // does not describe current function.
413 return !(DISubprogram(getContext()).describes(CurFn));
416 /// describes - Return true if this subprogram provides debugging
417 /// information for the function F.
418 bool DISubprogram::describes(const Function *F) {
419 assert(F && "Invalid function");
420 StringRef Name = getLinkageName();
423 if (F->getName() == Name)
428 unsigned DISubprogram::isOptimized() const {
429 assert (DbgNode && "Invalid subprogram descriptor!");
430 if (DbgNode->getNumOperands() == 16)
431 return getUnsignedField(15);
435 StringRef DIScope::getFilename() const {
438 if (isLexicalBlock())
439 return DILexicalBlock(DbgNode).getFilename();
441 return DISubprogram(DbgNode).getFilename();
443 return DICompileUnit(DbgNode).getFilename();
445 return DINameSpace(DbgNode).getFilename();
447 return DIType(DbgNode).getFilename();
449 return DIFile(DbgNode).getFilename();
450 assert(0 && "Invalid DIScope!");
454 StringRef DIScope::getDirectory() const {
457 if (isLexicalBlock())
458 return DILexicalBlock(DbgNode).getDirectory();
460 return DISubprogram(DbgNode).getDirectory();
462 return DICompileUnit(DbgNode).getDirectory();
464 return DINameSpace(DbgNode).getDirectory();
466 return DIType(DbgNode).getDirectory();
468 return DIFile(DbgNode).getDirectory();
469 assert(0 && "Invalid DIScope!");
473 //===----------------------------------------------------------------------===//
474 // DIDescriptor: dump routines for all descriptors.
475 //===----------------------------------------------------------------------===//
478 /// print - Print descriptor.
479 void DIDescriptor::print(raw_ostream &OS) const {
480 OS << "[" << dwarf::TagString(getTag()) << "] ";
481 OS.write_hex((intptr_t) &*DbgNode) << ']';
484 /// print - Print compile unit.
485 void DICompileUnit::print(raw_ostream &OS) const {
487 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
489 OS << " [" << getDirectory() << "/" << getFilename() << " ]";
492 /// print - Print type.
493 void DIType::print(raw_ostream &OS) const {
494 if (!DbgNode) return;
496 StringRef Res = getName();
498 OS << " [" << Res << "] ";
500 unsigned Tag = getTag();
501 OS << " [" << dwarf::TagString(Tag) << "] ";
503 // TODO : Print context
504 getCompileUnit().print(OS);
506 << "line " << getLineNumber() << ", "
507 << getSizeInBits() << " bits, "
508 << getAlignInBits() << " bit alignment, "
509 << getOffsetInBits() << " bit offset"
514 else if (isProtected())
515 OS << " [protected] ";
521 DIBasicType(DbgNode).print(OS);
522 else if (isDerivedType())
523 DIDerivedType(DbgNode).print(OS);
524 else if (isCompositeType())
525 DICompositeType(DbgNode).print(OS);
527 OS << "Invalid DIType\n";
534 /// print - Print basic type.
535 void DIBasicType::print(raw_ostream &OS) const {
536 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
539 /// print - Print derived type.
540 void DIDerivedType::print(raw_ostream &OS) const {
541 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
544 /// print - Print composite type.
545 void DICompositeType::print(raw_ostream &OS) const {
546 DIArray A = getTypeArray();
547 OS << " [" << A.getNumElements() << " elements]";
550 /// print - Print global.
551 void DIGlobal::print(raw_ostream &OS) const {
552 StringRef Res = getName();
554 OS << " [" << Res << "] ";
556 unsigned Tag = getTag();
557 OS << " [" << dwarf::TagString(Tag) << "] ";
559 // TODO : Print context
560 getCompileUnit().print(OS);
561 OS << " [" << getLineNumber() << "] ";
569 if (isGlobalVariable())
570 DIGlobalVariable(DbgNode).print(OS);
575 /// print - Print subprogram.
576 void DISubprogram::print(raw_ostream &OS) const {
577 StringRef Res = getName();
579 OS << " [" << Res << "] ";
581 unsigned Tag = getTag();
582 OS << " [" << dwarf::TagString(Tag) << "] ";
584 // TODO : Print context
585 getCompileUnit().print(OS);
586 OS << " [" << getLineNumber() << "] ";
597 /// print - Print global variable.
598 void DIGlobalVariable::print(raw_ostream &OS) const {
600 getGlobal()->print(OS);
604 /// print - Print variable.
605 void DIVariable::print(raw_ostream &OS) const {
606 StringRef Res = getName();
608 OS << " [" << Res << "] ";
610 getCompileUnit().print(OS);
611 OS << " [" << getLineNumber() << "] ";
615 // FIXME: Dump complex addresses
618 /// dump - Print descriptor to dbgs() with a newline.
619 void DIDescriptor::dump() const {
620 print(dbgs()); dbgs() << '\n';
623 /// dump - Print compile unit to dbgs() with a newline.
624 void DICompileUnit::dump() const {
625 print(dbgs()); dbgs() << '\n';
628 /// dump - Print type to dbgs() with a newline.
629 void DIType::dump() const {
630 print(dbgs()); dbgs() << '\n';
633 /// dump - Print basic type to dbgs() with a newline.
634 void DIBasicType::dump() const {
635 print(dbgs()); dbgs() << '\n';
638 /// dump - Print derived type to dbgs() with a newline.
639 void DIDerivedType::dump() const {
640 print(dbgs()); dbgs() << '\n';
643 /// dump - Print composite type to dbgs() with a newline.
644 void DICompositeType::dump() const {
645 print(dbgs()); dbgs() << '\n';
648 /// dump - Print global to dbgs() with a newline.
649 void DIGlobal::dump() const {
650 print(dbgs()); dbgs() << '\n';
653 /// dump - Print subprogram to dbgs() with a newline.
654 void DISubprogram::dump() const {
655 print(dbgs()); dbgs() << '\n';
658 /// dump - Print global variable.
659 void DIGlobalVariable::dump() const {
660 print(dbgs()); dbgs() << '\n';
663 /// dump - Print variable.
664 void DIVariable::dump() const {
665 print(dbgs()); dbgs() << '\n';
668 //===----------------------------------------------------------------------===//
669 // DIFactory: Basic Helpers
670 //===----------------------------------------------------------------------===//
672 DIFactory::DIFactory(Module &m)
673 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
675 Constant *DIFactory::GetTagConstant(unsigned TAG) {
676 assert((TAG & LLVMDebugVersionMask) == 0 &&
677 "Tag too large for debug encoding!");
678 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
681 //===----------------------------------------------------------------------===//
682 // DIFactory: Primary Constructors
683 //===----------------------------------------------------------------------===//
685 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
686 /// This implicitly uniques the arrays created.
687 DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
688 SmallVector<Value*, 16> Elts;
691 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
693 for (unsigned i = 0; i != NumTys; ++i)
694 Elts.push_back(Tys[i]);
696 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
699 /// GetOrCreateSubrange - Create a descriptor for a value range. This
700 /// implicitly uniques the values returned.
701 DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
703 GetTagConstant(dwarf::DW_TAG_subrange_type),
704 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
705 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
708 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
713 /// CreateCompileUnit - Create a new descriptor for the specified compile
714 /// unit. Note that this does not unique compile units within the module.
715 DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
722 unsigned RunTimeVer) {
724 GetTagConstant(dwarf::DW_TAG_compile_unit),
725 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
726 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
727 MDString::get(VMContext, Filename),
728 MDString::get(VMContext, Directory),
729 MDString::get(VMContext, Producer),
730 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
731 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
732 MDString::get(VMContext, Flags),
733 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
736 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
739 /// CreateFile - Create a new descriptor for the specified file.
740 DIFile DIFactory::CreateFile(StringRef Filename,
744 GetTagConstant(dwarf::DW_TAG_file_type),
745 MDString::get(VMContext, Filename),
746 MDString::get(VMContext, Directory),
750 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
753 /// CreateEnumerator - Create a single enumerator value.
754 DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
756 GetTagConstant(dwarf::DW_TAG_enumerator),
757 MDString::get(VMContext, Name),
758 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
760 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
764 /// CreateBasicType - Create a basic type like int, float, etc.
765 DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
770 uint64_t AlignInBits,
771 uint64_t OffsetInBits, unsigned Flags,
774 GetTagConstant(dwarf::DW_TAG_base_type),
776 MDString::get(VMContext, Name),
778 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
779 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
780 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
781 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
782 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
783 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
785 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
789 /// CreateBasicType - Create a basic type like int, float, etc.
790 DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
794 Constant *SizeInBits,
795 Constant *AlignInBits,
796 Constant *OffsetInBits, unsigned Flags,
799 GetTagConstant(dwarf::DW_TAG_base_type),
801 MDString::get(VMContext, Name),
803 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
807 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
808 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
810 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
813 /// CreateArtificialType - Create a new DIType with "artificial" flag set.
814 DIType DIFactory::CreateArtificialType(DIType Ty) {
815 if (Ty.isArtificial())
818 SmallVector<Value *, 9> Elts;
820 assert (N && "Unexpected input DIType!");
821 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
822 if (Value *V = N->getOperand(i))
825 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
828 unsigned CurFlags = Ty.getFlags();
829 CurFlags = CurFlags | DIType::FlagArtificial;
831 // Flags are stored at this slot.
832 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
834 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
837 /// CreateDerivedType - Create a derived type like const qualified type,
838 /// pointer, typedef, etc.
839 DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
840 DIDescriptor Context,
845 uint64_t AlignInBits,
846 uint64_t OffsetInBits,
848 DIType DerivedFrom) {
852 MDString::get(VMContext, Name),
854 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
855 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
856 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
857 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
858 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
861 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
865 /// CreateDerivedType - Create a derived type like const qualified type,
866 /// pointer, typedef, etc.
867 DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
868 DIDescriptor Context,
872 Constant *SizeInBits,
873 Constant *AlignInBits,
874 Constant *OffsetInBits,
876 DIType DerivedFrom) {
880 MDString::get(VMContext, Name),
882 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
886 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
889 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
893 /// CreateCompositeType - Create a composite type like array, struct, etc.
894 DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
895 DIDescriptor Context,
900 uint64_t AlignInBits,
901 uint64_t OffsetInBits,
905 unsigned RuntimeLang,
906 MDNode *ContainingType) {
911 MDString::get(VMContext, Name),
913 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
914 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
915 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
916 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
917 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
920 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
923 return DICompositeType(MDNode::get(VMContext, &Elts[0], 13));
927 /// CreateCompositeType - Create a composite type like array, struct, etc.
928 DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
929 DIDescriptor Context,
933 Constant *SizeInBits,
934 Constant *AlignInBits,
935 Constant *OffsetInBits,
939 unsigned RuntimeLang) {
944 MDString::get(VMContext, Name),
946 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
950 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
953 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
955 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
959 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
960 /// See comments in DISubprogram for descriptions of these fields. This
961 /// method does not unique the generated descriptors.
962 DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
964 StringRef DisplayName,
965 StringRef LinkageName,
967 unsigned LineNo, DIType Ty,
970 unsigned VK, unsigned VIndex,
971 DIType ContainingType,
976 GetTagConstant(dwarf::DW_TAG_subprogram),
977 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
979 MDString::get(VMContext, Name),
980 MDString::get(VMContext, DisplayName),
981 MDString::get(VMContext, LinkageName),
983 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
985 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
986 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
987 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
988 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
990 ConstantInt::get(Type::getInt1Ty(VMContext), isArtificial),
991 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized)
993 return DISubprogram(MDNode::get(VMContext, &Elts[0], 16));
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
1021 return DISubprogram(MDNode::get(VMContext, &Elts[0], 16));
1024 /// CreateGlobalVariable - Create a new descriptor for the specified global.
1026 DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1027 StringRef DisplayName,
1028 StringRef LinkageName,
1030 unsigned LineNo, DIType Ty,bool isLocalToUnit,
1031 bool isDefinition, llvm::GlobalVariable *Val) {
1033 GetTagConstant(dwarf::DW_TAG_variable),
1034 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1036 MDString::get(VMContext, Name),
1037 MDString::get(VMContext, DisplayName),
1038 MDString::get(VMContext, LinkageName),
1040 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1042 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1043 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1047 Value *const *Vs = &Elts[0];
1048 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1050 // Create a named metadata so that we do not lose this mdnode.
1051 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1052 NMD->addOperand(Node);
1054 return DIGlobalVariable(Node);
1058 /// CreateVariable - Create a new descriptor for the specified variable.
1059 DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
1065 GetTagConstant(Tag),
1067 MDString::get(VMContext, Name),
1069 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1072 return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
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 /// CreateLocation - Creates a debug info location.
1138 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1139 DIScope S, MDNode *OrigLoc) {
1141 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1142 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1146 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1149 //===----------------------------------------------------------------------===//
1150 // DIFactory: Routines for inserting code into a function
1151 //===----------------------------------------------------------------------===//
1153 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1154 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1155 Instruction *InsertBefore) {
1156 assert(Storage && "no storage passed to dbg.declare");
1157 assert(D && "empty DIVariable passed to dbg.declare");
1159 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1161 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1163 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
1166 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1167 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1168 BasicBlock *InsertAtEnd) {
1169 assert(Storage && "no storage passed to dbg.declare");
1170 assert(D && "empty DIVariable passed to dbg.declare");
1172 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1174 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1177 // If this block already has a terminator then insert this intrinsic
1178 // before the terminator.
1179 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1180 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1182 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
1184 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1185 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1187 Instruction *InsertBefore) {
1188 assert(V && "no value passed to dbg.value");
1189 assert(D && "empty DIVariable passed to dbg.value");
1191 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1193 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1194 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1196 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1199 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1200 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1202 BasicBlock *InsertAtEnd) {
1203 assert(V && "no value passed to dbg.value");
1204 assert(D && "empty DIVariable passed to dbg.value");
1206 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1208 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1209 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1211 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1214 //===----------------------------------------------------------------------===//
1215 // DebugInfoFinder implementations.
1216 //===----------------------------------------------------------------------===//
1218 /// processModule - Process entire module and collect debug info.
1219 void DebugInfoFinder::processModule(Module &M) {
1220 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1221 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1222 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1224 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1225 processDeclare(DDI);
1227 DebugLoc Loc = BI->getDebugLoc();
1228 if (Loc.isUnknown())
1231 LLVMContext &Ctx = BI->getContext();
1232 DIDescriptor Scope(Loc.getScope(Ctx));
1234 if (Scope.isCompileUnit())
1235 addCompileUnit(DICompileUnit(Scope));
1236 else if (Scope.isSubprogram())
1237 processSubprogram(DISubprogram(Scope));
1238 else if (Scope.isLexicalBlock())
1239 processLexicalBlock(DILexicalBlock(Scope));
1241 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1242 processLocation(DILocation(IA));
1245 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
1249 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1250 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1251 if (addGlobalVariable(DIG)) {
1252 addCompileUnit(DIG.getCompileUnit());
1253 processType(DIG.getType());
1258 /// processLocation - Process DILocation.
1259 void DebugInfoFinder::processLocation(DILocation Loc) {
1260 if (!Loc.Verify()) return;
1261 DIDescriptor S(Loc.getScope());
1262 if (S.isCompileUnit())
1263 addCompileUnit(DICompileUnit(S));
1264 else if (S.isSubprogram())
1265 processSubprogram(DISubprogram(S));
1266 else if (S.isLexicalBlock())
1267 processLexicalBlock(DILexicalBlock(S));
1268 processLocation(Loc.getOrigLocation());
1271 /// processType - Process DIType.
1272 void DebugInfoFinder::processType(DIType DT) {
1276 addCompileUnit(DT.getCompileUnit());
1277 if (DT.isCompositeType()) {
1278 DICompositeType DCT(DT);
1279 processType(DCT.getTypeDerivedFrom());
1280 DIArray DA = DCT.getTypeArray();
1281 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1282 DIDescriptor D = DA.getElement(i);
1284 processType(DIType(D));
1285 else if (D.isSubprogram())
1286 processSubprogram(DISubprogram(D));
1288 } else if (DT.isDerivedType()) {
1289 DIDerivedType DDT(DT);
1290 processType(DDT.getTypeDerivedFrom());
1294 /// processLexicalBlock
1295 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1296 DIScope Context = LB.getContext();
1297 if (Context.isLexicalBlock())
1298 return processLexicalBlock(DILexicalBlock(Context));
1300 return processSubprogram(DISubprogram(Context));
1303 /// processSubprogram - Process DISubprogram.
1304 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1305 if (!addSubprogram(SP))
1307 addCompileUnit(SP.getCompileUnit());
1308 processType(SP.getType());
1311 /// processDeclare - Process DbgDeclareInst.
1312 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
1313 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1317 if (!DV.isVariable())
1320 if (!NodesSeen.insert(DV))
1323 addCompileUnit(DIVariable(N).getCompileUnit());
1324 processType(DIVariable(N).getType());
1327 /// addType - Add type into Tys.
1328 bool DebugInfoFinder::addType(DIType DT) {
1332 if (!NodesSeen.insert(DT))
1339 /// addCompileUnit - Add compile unit into CUs.
1340 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1344 if (!NodesSeen.insert(CU))
1351 /// addGlobalVariable - Add global variable into GVs.
1352 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1353 if (!DIDescriptor(DIG).isGlobalVariable())
1356 if (!NodesSeen.insert(DIG))
1363 // addSubprogram - Add subprgoram into SPs.
1364 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1365 if (!DIDescriptor(SP).isSubprogram())
1368 if (!NodesSeen.insert(SP))
1375 /// Find the debug info descriptor corresponding to this global variable.
1376 static Value *findDbgGlobalDeclare(GlobalVariable *V) {
1377 const Module *M = V->getParent();
1378 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1382 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1383 DIDescriptor DIG(cast_or_null<MDNode>(NMD->getOperand(i)));
1384 if (!DIG.isGlobalVariable())
1386 if (DIGlobalVariable(DIG).getGlobal() == V)
1392 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1393 /// It looks through pointer casts too.
1394 static const DbgDeclareInst *findDbgDeclare(const Value *V) {
1395 V = V->stripPointerCasts();
1397 if (!isa<Instruction>(V) && !isa<Argument>(V))
1400 const Function *F = NULL;
1401 if (const Instruction *I = dyn_cast<Instruction>(V))
1402 F = I->getParent()->getParent();
1403 else if (const Argument *A = dyn_cast<Argument>(V))
1406 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1407 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1409 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1410 if (DDI->getAddress() == V)
1416 bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1417 std::string &Type, unsigned &LineNo,
1418 std::string &File, std::string &Dir) {
1422 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1423 Value *DIGV = findDbgGlobalDeclare(GV);
1424 if (!DIGV) return false;
1425 DIGlobalVariable Var(cast<MDNode>(DIGV));
1427 StringRef D = Var.getDisplayName();
1430 LineNo = Var.getLineNumber();
1431 Unit = Var.getCompileUnit();
1432 TypeD = Var.getType();
1434 const DbgDeclareInst *DDI = findDbgDeclare(V);
1435 if (!DDI) return false;
1436 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1438 StringRef D = Var.getName();
1441 LineNo = Var.getLineNumber();
1442 Unit = Var.getCompileUnit();
1443 TypeD = Var.getType();
1446 StringRef T = TypeD.getName();
1449 StringRef F = Unit.getFilename();
1452 StringRef D = Unit.getDirectory();
1458 /// getDISubprogram - Find subprogram that is enclosing this scope.
1459 DISubprogram llvm::getDISubprogram(MDNode *Scope) {
1460 DIDescriptor D(Scope);
1461 if (D.isSubprogram())
1462 return DISubprogram(Scope);
1464 if (D.isLexicalBlock())
1465 return getDISubprogram(DILexicalBlock(Scope).getContext());
1467 return DISubprogram();
1470 /// getDICompositeType - Find underlying composite type.
1471 DICompositeType llvm::getDICompositeType(DIType T) {
1472 if (T.isCompositeType())
1473 return DICompositeType(T);
1475 if (T.isDerivedType())
1476 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
1478 return DICompositeType();