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 StringRef Name = getLinkageName();
412 if (F->getName() == Name)
417 unsigned DISubprogram::isOptimized() const {
418 assert (DbgNode && "Invalid subprogram descriptor!");
419 if (DbgNode->getNumOperands() == 16)
420 return getUnsignedField(15);
424 StringRef DIScope::getFilename() const {
427 if (isLexicalBlock())
428 return DILexicalBlock(DbgNode).getFilename();
430 return DISubprogram(DbgNode).getFilename();
432 return DICompileUnit(DbgNode).getFilename();
434 return DINameSpace(DbgNode).getFilename();
436 return DIType(DbgNode).getFilename();
438 return DIFile(DbgNode).getFilename();
439 assert(0 && "Invalid DIScope!");
443 StringRef DIScope::getDirectory() const {
446 if (isLexicalBlock())
447 return DILexicalBlock(DbgNode).getDirectory();
449 return DISubprogram(DbgNode).getDirectory();
451 return DICompileUnit(DbgNode).getDirectory();
453 return DINameSpace(DbgNode).getDirectory();
455 return DIType(DbgNode).getDirectory();
457 return DIFile(DbgNode).getDirectory();
458 assert(0 && "Invalid DIScope!");
462 //===----------------------------------------------------------------------===//
463 // DIDescriptor: dump routines for all descriptors.
464 //===----------------------------------------------------------------------===//
467 /// print - Print descriptor.
468 void DIDescriptor::print(raw_ostream &OS) const {
469 OS << "[" << dwarf::TagString(getTag()) << "] ";
470 OS.write_hex((intptr_t) &*DbgNode) << ']';
473 /// print - Print compile unit.
474 void DICompileUnit::print(raw_ostream &OS) const {
476 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
478 OS << " [" << getDirectory() << "/" << getFilename() << "]";
481 /// print - Print type.
482 void DIType::print(raw_ostream &OS) const {
483 if (!DbgNode) return;
485 StringRef Res = getName();
487 OS << " [" << Res << "] ";
489 unsigned Tag = getTag();
490 OS << " [" << dwarf::TagString(Tag) << "] ";
492 // TODO : Print context
493 getCompileUnit().print(OS);
495 << "line " << getLineNumber() << ", "
496 << getSizeInBits() << " bits, "
497 << getAlignInBits() << " bit alignment, "
498 << getOffsetInBits() << " bit offset"
503 else if (isProtected())
504 OS << " [protected] ";
510 DIBasicType(DbgNode).print(OS);
511 else if (isDerivedType())
512 DIDerivedType(DbgNode).print(OS);
513 else if (isCompositeType())
514 DICompositeType(DbgNode).print(OS);
516 OS << "Invalid DIType\n";
523 /// print - Print basic type.
524 void DIBasicType::print(raw_ostream &OS) const {
525 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
528 /// print - Print derived type.
529 void DIDerivedType::print(raw_ostream &OS) const {
530 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
533 /// print - Print composite type.
534 void DICompositeType::print(raw_ostream &OS) const {
535 DIArray A = getTypeArray();
536 OS << " [" << A.getNumElements() << " elements]";
539 /// print - Print subprogram.
540 void DISubprogram::print(raw_ostream &OS) const {
541 StringRef Res = getName();
543 OS << " [" << Res << "] ";
545 unsigned Tag = getTag();
546 OS << " [" << dwarf::TagString(Tag) << "] ";
548 // TODO : Print context
549 getCompileUnit().print(OS);
550 OS << " [" << getLineNumber() << "] ";
561 /// print - Print global variable.
562 void DIGlobalVariable::print(raw_ostream &OS) const {
564 StringRef Res = getName();
566 OS << " [" << Res << "] ";
568 unsigned Tag = getTag();
569 OS << " [" << dwarf::TagString(Tag) << "] ";
571 // TODO : Print context
572 getCompileUnit().print(OS);
573 OS << " [" << getLineNumber() << "] ";
581 if (isGlobalVariable())
582 DIGlobalVariable(DbgNode).print(OS);
586 /// print - Print variable.
587 void DIVariable::print(raw_ostream &OS) const {
588 StringRef Res = getName();
590 OS << " [" << Res << "] ";
592 getCompileUnit().print(OS);
593 OS << " [" << getLineNumber() << "] ";
597 // FIXME: Dump complex addresses
600 /// dump - Print descriptor to dbgs() with a newline.
601 void DIDescriptor::dump() const {
602 print(dbgs()); dbgs() << '\n';
605 /// dump - Print compile unit to dbgs() with a newline.
606 void DICompileUnit::dump() const {
607 print(dbgs()); dbgs() << '\n';
610 /// dump - Print type to dbgs() with a newline.
611 void DIType::dump() const {
612 print(dbgs()); dbgs() << '\n';
615 /// dump - Print basic type to dbgs() with a newline.
616 void DIBasicType::dump() const {
617 print(dbgs()); dbgs() << '\n';
620 /// dump - Print derived type to dbgs() with a newline.
621 void DIDerivedType::dump() const {
622 print(dbgs()); dbgs() << '\n';
625 /// dump - Print composite type to dbgs() with a newline.
626 void DICompositeType::dump() const {
627 print(dbgs()); dbgs() << '\n';
630 /// dump - Print subprogram to dbgs() with a newline.
631 void DISubprogram::dump() const {
632 print(dbgs()); dbgs() << '\n';
635 /// dump - Print global variable.
636 void DIGlobalVariable::dump() const {
637 print(dbgs()); dbgs() << '\n';
640 /// dump - Print variable.
641 void DIVariable::dump() const {
642 print(dbgs()); dbgs() << '\n';
645 //===----------------------------------------------------------------------===//
646 // DIFactory: Basic Helpers
647 //===----------------------------------------------------------------------===//
649 DIFactory::DIFactory(Module &m)
650 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
652 Constant *DIFactory::GetTagConstant(unsigned TAG) {
653 assert((TAG & LLVMDebugVersionMask) == 0 &&
654 "Tag too large for debug encoding!");
655 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
658 //===----------------------------------------------------------------------===//
659 // DIFactory: Primary Constructors
660 //===----------------------------------------------------------------------===//
662 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
663 /// This implicitly uniques the arrays created.
664 DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
665 SmallVector<Value*, 16> Elts;
668 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
670 for (unsigned i = 0; i != NumTys; ++i)
671 Elts.push_back(Tys[i]);
673 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
676 /// GetOrCreateSubrange - Create a descriptor for a value range. This
677 /// implicitly uniques the values returned.
678 DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
680 GetTagConstant(dwarf::DW_TAG_subrange_type),
681 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
682 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
685 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
690 /// CreateCompileUnit - Create a new descriptor for the specified compile
691 /// unit. Note that this does not unique compile units within the module.
692 DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
699 unsigned RunTimeVer) {
701 GetTagConstant(dwarf::DW_TAG_compile_unit),
702 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
703 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
704 MDString::get(VMContext, Filename),
705 MDString::get(VMContext, Directory),
706 MDString::get(VMContext, Producer),
707 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
708 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
709 MDString::get(VMContext, Flags),
710 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
713 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
716 /// CreateFile - Create a new descriptor for the specified file.
717 DIFile DIFactory::CreateFile(StringRef Filename,
721 GetTagConstant(dwarf::DW_TAG_file_type),
722 MDString::get(VMContext, Filename),
723 MDString::get(VMContext, Directory),
727 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
730 /// CreateEnumerator - Create a single enumerator value.
731 DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
733 GetTagConstant(dwarf::DW_TAG_enumerator),
734 MDString::get(VMContext, Name),
735 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
737 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
741 /// CreateBasicType - Create a basic type like int, float, etc.
742 DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
747 uint64_t AlignInBits,
748 uint64_t OffsetInBits, unsigned Flags,
751 GetTagConstant(dwarf::DW_TAG_base_type),
753 MDString::get(VMContext, Name),
755 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
756 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
757 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
758 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
759 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
760 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
762 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
766 /// CreateBasicType - Create a basic type like int, float, etc.
767 DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
771 Constant *SizeInBits,
772 Constant *AlignInBits,
773 Constant *OffsetInBits, unsigned Flags,
776 GetTagConstant(dwarf::DW_TAG_base_type),
778 MDString::get(VMContext, Name),
780 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
784 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
785 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
787 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
790 /// CreateArtificialType - Create a new DIType with "artificial" flag set.
791 DIType DIFactory::CreateArtificialType(DIType Ty) {
792 if (Ty.isArtificial())
795 SmallVector<Value *, 9> Elts;
797 assert (N && "Unexpected input DIType!");
798 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
799 if (Value *V = N->getOperand(i))
802 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
805 unsigned CurFlags = Ty.getFlags();
806 CurFlags = CurFlags | DIType::FlagArtificial;
808 // Flags are stored at this slot.
809 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
811 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
814 /// CreateDerivedType - Create a derived type like const qualified type,
815 /// pointer, typedef, etc.
816 DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
817 DIDescriptor Context,
822 uint64_t AlignInBits,
823 uint64_t OffsetInBits,
825 DIType DerivedFrom) {
829 MDString::get(VMContext, Name),
831 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
832 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
833 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
834 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
835 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
838 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
842 /// CreateDerivedType - Create a derived type like const qualified type,
843 /// pointer, typedef, etc.
844 DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
845 DIDescriptor Context,
849 Constant *SizeInBits,
850 Constant *AlignInBits,
851 Constant *OffsetInBits,
853 DIType DerivedFrom) {
857 MDString::get(VMContext, Name),
859 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
863 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
866 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
870 /// CreateCompositeType - Create a composite type like array, struct, etc.
871 DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
872 DIDescriptor Context,
877 uint64_t AlignInBits,
878 uint64_t OffsetInBits,
882 unsigned RuntimeLang,
883 MDNode *ContainingType) {
888 MDString::get(VMContext, Name),
890 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
891 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
892 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
893 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
894 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
897 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
900 return DICompositeType(MDNode::get(VMContext, &Elts[0], 13));
904 /// CreateCompositeType - Create a composite type like array, struct, etc.
905 DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
906 DIDescriptor Context,
910 Constant *SizeInBits,
911 Constant *AlignInBits,
912 Constant *OffsetInBits,
916 unsigned RuntimeLang) {
921 MDString::get(VMContext, Name),
923 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
927 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
930 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
932 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
936 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
937 /// See comments in DISubprogram for descriptions of these fields. This
938 /// method does not unique the generated descriptors.
939 DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
941 StringRef DisplayName,
942 StringRef LinkageName,
944 unsigned LineNo, DIType Ty,
947 unsigned VK, unsigned VIndex,
948 DIType ContainingType,
954 GetTagConstant(dwarf::DW_TAG_subprogram),
955 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
957 MDString::get(VMContext, Name),
958 MDString::get(VMContext, DisplayName),
959 MDString::get(VMContext, LinkageName),
961 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
963 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
964 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
965 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
966 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
968 ConstantInt::get(Type::getInt1Ty(VMContext), isArtificial),
969 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
972 return DISubprogram(MDNode::get(VMContext, &Elts[0], 17));
975 /// CreateSubprogramDefinition - Create new subprogram descriptor for the
976 /// given declaration.
977 DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration) {
978 if (SPDeclaration.isDefinition())
979 return DISubprogram(SPDeclaration);
981 MDNode *DeclNode = SPDeclaration;
983 GetTagConstant(dwarf::DW_TAG_subprogram),
984 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
985 DeclNode->getOperand(2), // Context
986 DeclNode->getOperand(3), // Name
987 DeclNode->getOperand(4), // DisplayName
988 DeclNode->getOperand(5), // LinkageName
989 DeclNode->getOperand(6), // CompileUnit
990 DeclNode->getOperand(7), // LineNo
991 DeclNode->getOperand(8), // Type
992 DeclNode->getOperand(9), // isLocalToUnit
993 ConstantInt::get(Type::getInt1Ty(VMContext), true),
994 DeclNode->getOperand(11), // Virtuality
995 DeclNode->getOperand(12), // VIndex
996 DeclNode->getOperand(13), // Containting Type
997 DeclNode->getOperand(14), // isArtificial
998 DeclNode->getOperand(15) // isOptimized
1000 return DISubprogram(MDNode::get(VMContext, &Elts[0], 16));
1003 /// CreateGlobalVariable - Create a new descriptor for the specified global.
1005 DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1006 StringRef DisplayName,
1007 StringRef LinkageName,
1009 unsigned LineNo, DIType Ty,bool isLocalToUnit,
1010 bool isDefinition, llvm::GlobalVariable *Val) {
1012 GetTagConstant(dwarf::DW_TAG_variable),
1013 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1015 MDString::get(VMContext, Name),
1016 MDString::get(VMContext, DisplayName),
1017 MDString::get(VMContext, LinkageName),
1019 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1021 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1022 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1026 Value *const *Vs = &Elts[0];
1027 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1029 // Create a named metadata so that we do not lose this mdnode.
1030 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1031 NMD->addOperand(Node);
1033 return DIGlobalVariable(Node);
1037 /// CreateVariable - Create a new descriptor for the specified variable.
1038 DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
1042 DIType Ty, bool AlwaysPreserve) {
1044 GetTagConstant(Tag),
1046 MDString::get(VMContext, Name),
1048 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1051 MDNode *Node = MDNode::get(VMContext, &Elts[0], 6);
1052 if (AlwaysPreserve) {
1053 // The optimizer may remove local variable. If there is an interest
1054 // to preserve variable info in such situation then stash it in a
1056 DISubprogram Fn(getDISubprogram(Context));
1057 const Twine FnLVName = Twine("llvm.dbg.lv.", Fn.getName());
1058 NamedMDNode *FnLocals = M.getNamedMetadataUsingTwine(FnLVName);
1060 FnLocals = NamedMDNode::Create(VMContext, FnLVName, NULL, 0, &M);
1061 FnLocals->addOperand(Node);
1063 return DIVariable(Node);
1067 /// CreateComplexVariable - Create a new descriptor for the specified variable
1068 /// which has a complex address expression for its address.
1069 DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
1070 const std::string &Name,
1074 SmallVector<Value *, 9> &addr) {
1075 SmallVector<Value *, 9> Elts;
1076 Elts.push_back(GetTagConstant(Tag));
1077 Elts.push_back(Context);
1078 Elts.push_back(MDString::get(VMContext, Name));
1080 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
1082 Elts.insert(Elts.end(), addr.begin(), addr.end());
1084 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
1088 /// CreateBlock - This creates a descriptor for a lexical block with the
1089 /// specified parent VMContext.
1090 DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
1091 unsigned LineNo, unsigned Col) {
1093 GetTagConstant(dwarf::DW_TAG_lexical_block),
1095 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1096 ConstantInt::get(Type::getInt32Ty(VMContext), Col)
1098 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 4));
1101 /// CreateNameSpace - This creates new descriptor for a namespace
1102 /// with the specified parent context.
1103 DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
1107 GetTagConstant(dwarf::DW_TAG_namespace),
1109 MDString::get(VMContext, Name),
1111 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1113 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1116 /// CreateLocation - Creates a debug info location.
1117 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1118 DIScope S, DILocation OrigLoc) {
1120 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1121 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1125 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1128 //===----------------------------------------------------------------------===//
1129 // DIFactory: Routines for inserting code into a function
1130 //===----------------------------------------------------------------------===//
1132 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1133 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1134 Instruction *InsertBefore) {
1135 assert(Storage && "no storage passed to dbg.declare");
1136 assert(D.Verify() && "empty DIVariable passed to dbg.declare");
1138 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1140 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1142 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
1145 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1146 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1147 BasicBlock *InsertAtEnd) {
1148 assert(Storage && "no storage passed to dbg.declare");
1149 assert(D.Verify() && "invalid DIVariable passed to dbg.declare");
1151 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1153 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1156 // If this block already has a terminator then insert this intrinsic
1157 // before the terminator.
1158 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1159 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1161 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
1163 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1164 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1166 Instruction *InsertBefore) {
1167 assert(V && "no value passed to dbg.value");
1168 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
1170 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1172 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1173 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1175 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1178 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1179 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1181 BasicBlock *InsertAtEnd) {
1182 assert(V && "no value passed to dbg.value");
1183 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
1185 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1187 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1188 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1190 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1193 //===----------------------------------------------------------------------===//
1194 // DebugInfoFinder implementations.
1195 //===----------------------------------------------------------------------===//
1197 /// processModule - Process entire module and collect debug info.
1198 void DebugInfoFinder::processModule(Module &M) {
1199 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1200 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1201 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1203 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1204 processDeclare(DDI);
1206 DebugLoc Loc = BI->getDebugLoc();
1207 if (Loc.isUnknown())
1210 LLVMContext &Ctx = BI->getContext();
1211 DIDescriptor Scope(Loc.getScope(Ctx));
1213 if (Scope.isCompileUnit())
1214 addCompileUnit(DICompileUnit(Scope));
1215 else if (Scope.isSubprogram())
1216 processSubprogram(DISubprogram(Scope));
1217 else if (Scope.isLexicalBlock())
1218 processLexicalBlock(DILexicalBlock(Scope));
1220 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1221 processLocation(DILocation(IA));
1224 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
1228 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1229 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1230 if (addGlobalVariable(DIG)) {
1231 addCompileUnit(DIG.getCompileUnit());
1232 processType(DIG.getType());
1237 /// processLocation - Process DILocation.
1238 void DebugInfoFinder::processLocation(DILocation Loc) {
1239 if (!Loc.Verify()) return;
1240 DIDescriptor S(Loc.getScope());
1241 if (S.isCompileUnit())
1242 addCompileUnit(DICompileUnit(S));
1243 else if (S.isSubprogram())
1244 processSubprogram(DISubprogram(S));
1245 else if (S.isLexicalBlock())
1246 processLexicalBlock(DILexicalBlock(S));
1247 processLocation(Loc.getOrigLocation());
1250 /// processType - Process DIType.
1251 void DebugInfoFinder::processType(DIType DT) {
1255 addCompileUnit(DT.getCompileUnit());
1256 if (DT.isCompositeType()) {
1257 DICompositeType DCT(DT);
1258 processType(DCT.getTypeDerivedFrom());
1259 DIArray DA = DCT.getTypeArray();
1260 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1261 DIDescriptor D = DA.getElement(i);
1263 processType(DIType(D));
1264 else if (D.isSubprogram())
1265 processSubprogram(DISubprogram(D));
1267 } else if (DT.isDerivedType()) {
1268 DIDerivedType DDT(DT);
1269 processType(DDT.getTypeDerivedFrom());
1273 /// processLexicalBlock
1274 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
1275 DIScope Context = LB.getContext();
1276 if (Context.isLexicalBlock())
1277 return processLexicalBlock(DILexicalBlock(Context));
1279 return processSubprogram(DISubprogram(Context));
1282 /// processSubprogram - Process DISubprogram.
1283 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1284 if (!addSubprogram(SP))
1286 addCompileUnit(SP.getCompileUnit());
1287 processType(SP.getType());
1290 /// processDeclare - Process DbgDeclareInst.
1291 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
1292 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1296 if (!DV.isVariable())
1299 if (!NodesSeen.insert(DV))
1302 addCompileUnit(DIVariable(N).getCompileUnit());
1303 processType(DIVariable(N).getType());
1306 /// addType - Add type into Tys.
1307 bool DebugInfoFinder::addType(DIType DT) {
1311 if (!NodesSeen.insert(DT))
1318 /// addCompileUnit - Add compile unit into CUs.
1319 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1323 if (!NodesSeen.insert(CU))
1330 /// addGlobalVariable - Add global variable into GVs.
1331 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1332 if (!DIDescriptor(DIG).isGlobalVariable())
1335 if (!NodesSeen.insert(DIG))
1342 // addSubprogram - Add subprgoram into SPs.
1343 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1344 if (!DIDescriptor(SP).isSubprogram())
1347 if (!NodesSeen.insert(SP))
1354 /// Find the debug info descriptor corresponding to this global variable.
1355 static Value *findDbgGlobalDeclare(GlobalVariable *V) {
1356 const Module *M = V->getParent();
1357 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1361 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1362 DIDescriptor DIG(cast_or_null<MDNode>(NMD->getOperand(i)));
1363 if (!DIG.isGlobalVariable())
1365 if (DIGlobalVariable(DIG).getGlobal() == V)
1371 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1372 /// It looks through pointer casts too.
1373 static const DbgDeclareInst *findDbgDeclare(const Value *V) {
1374 V = V->stripPointerCasts();
1376 if (!isa<Instruction>(V) && !isa<Argument>(V))
1379 const Function *F = NULL;
1380 if (const Instruction *I = dyn_cast<Instruction>(V))
1381 F = I->getParent()->getParent();
1382 else if (const Argument *A = dyn_cast<Argument>(V))
1385 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1386 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1388 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1389 if (DDI->getAddress() == V)
1395 bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1396 std::string &Type, unsigned &LineNo,
1397 std::string &File, std::string &Dir) {
1401 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1402 Value *DIGV = findDbgGlobalDeclare(GV);
1403 if (!DIGV) return false;
1404 DIGlobalVariable Var(cast<MDNode>(DIGV));
1406 StringRef D = Var.getDisplayName();
1409 LineNo = Var.getLineNumber();
1410 Unit = Var.getCompileUnit();
1411 TypeD = Var.getType();
1413 const DbgDeclareInst *DDI = findDbgDeclare(V);
1414 if (!DDI) return false;
1415 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1417 StringRef D = Var.getName();
1420 LineNo = Var.getLineNumber();
1421 Unit = Var.getCompileUnit();
1422 TypeD = Var.getType();
1425 StringRef T = TypeD.getName();
1428 StringRef F = Unit.getFilename();
1431 StringRef D = Unit.getDirectory();
1437 /// getDISubprogram - Find subprogram that is enclosing this scope.
1438 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
1439 DIDescriptor D(Scope);
1440 if (D.isSubprogram())
1441 return DISubprogram(Scope);
1443 if (D.isLexicalBlock())
1444 return getDISubprogram(DILexicalBlock(Scope).getContext());
1446 return DISubprogram();
1449 /// getDICompositeType - Find underlying composite type.
1450 DICompositeType llvm::getDICompositeType(DIType T) {
1451 if (T.isCompositeType())
1452 return DICompositeType(T);
1454 if (T.isDerivedType())
1455 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
1457 return DICompositeType();