llvm.org GIT mirror llvm / 291bb70
Remove DIFactory. Patch by Devang. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@126871 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan Sands 9 years ago
2 changed file(s) with 12 addition(s) and 871 deletion(s). Raw diff Collapse all Expand all
665665 bool Verify() const;
666666 };
667667
668 /// DIFactory - This object assists with the construction of the various
669 /// descriptors.
670 class DIFactory {
671 Module &M;
672 LLVMContext& VMContext;
673
674 Function *DeclareFn; // llvm.dbg.declare
675 Function *ValueFn; // llvm.dbg.value
676
677 DIFactory(const DIFactory &); // DO NOT IMPLEMENT
678 void operator=(const DIFactory&); // DO NOT IMPLEMENT
679 public:
680 enum ComplexAddrKind { OpPlus=1, OpDeref };
681
682 explicit DIFactory(Module &m);
683
684 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
685 /// This implicitly uniques the arrays created.
686 DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
687
688 /// GetOrCreateSubrange - Create a descriptor for a value range. This
689 /// implicitly uniques the values returned.
690 DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
691
692 /// CreateUnspecifiedParameter - Create unspeicified type descriptor
693 /// for a subroutine type.
694 DIDescriptor CreateUnspecifiedParameter();
695
696 /// CreateCompileUnit - Create a new descriptor for the specified compile
697 /// unit.
698 DICompileUnit CreateCompileUnit(unsigned LangID,
699 StringRef Filename,
700 StringRef Directory,
701 StringRef Producer,
702 bool isMain = false,
703 bool isOptimized = false,
704 StringRef Flags = "",
705 unsigned RunTimeVer = 0);
706
707 /// CreateFile - Create a new descriptor for the specified file.
708 DIFile CreateFile(StringRef Filename, StringRef Directory,
709 DICompileUnit CU);
710
711 /// CreateEnumerator - Create a single enumerator value.
712 DIEnumerator CreateEnumerator(StringRef Name, uint64_t Val);
713
714 /// CreateBasicType - Create a basic type like int, float, etc.
715 DIBasicType CreateBasicType(DIDescriptor Context, StringRef Name,
716 DIFile F, unsigned LineNumber,
717 uint64_t SizeInBits, uint64_t AlignInBits,
718 uint64_t OffsetInBits, unsigned Flags,
719 unsigned Encoding);
720
721 /// CreateBasicType - Create a basic type like int, float, etc.
722 DIBasicType CreateBasicTypeEx(DIDescriptor Context, StringRef Name,
723 DIFile F, unsigned LineNumber,
724 Constant *SizeInBits, Constant *AlignInBits,
725 Constant *OffsetInBits, unsigned Flags,
726 unsigned Encoding);
727
728 /// CreateDerivedType - Create a derived type like const qualified type,
729 /// pointer, typedef, etc.
730 DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
731 StringRef Name,
732 DIFile F,
733 unsigned LineNumber,
734 uint64_t SizeInBits, uint64_t AlignInBits,
735 uint64_t OffsetInBits, unsigned Flags,
736 DIType DerivedFrom);
737
738 /// CreateDerivedType - Create a derived type like const qualified type,
739 /// pointer, typedef, etc.
740 DIDerivedType CreateDerivedTypeEx(unsigned Tag, DIDescriptor Context,
741 StringRef Name,
742 DIFile F,
743 unsigned LineNumber,
744 Constant *SizeInBits,
745 Constant *AlignInBits,
746 Constant *OffsetInBits, unsigned Flags,
747 DIType DerivedFrom);
748
749 /// CreateCompositeType - Create a composite type like array, struct, etc.
750 DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
751 StringRef Name,
752 DIFile F,
753 unsigned LineNumber,
754 uint64_t SizeInBits,
755 uint64_t AlignInBits,
756 uint64_t OffsetInBits, unsigned Flags,
757 DIType DerivedFrom,
758 DIArray Elements,
759 unsigned RunTimeLang = 0,
760 MDNode *ContainingType = 0);
761
762 /// CreateTemporaryType - Create a temporary forward-declared type.
763 DIType CreateTemporaryType();
764 DIType CreateTemporaryType(DIFile F);
765
766 /// CreateArtificialType - Create a new DIType with "artificial" flag set.
767 DIType CreateArtificialType(DIType Ty);
768
769 /// CreateCompositeType - Create a composite type like array, struct, etc.
770 DICompositeType CreateCompositeTypeEx(unsigned Tag, DIDescriptor Context,
771 StringRef Name,
772 DIFile F,
773 unsigned LineNumber,
774 Constant *SizeInBits,
775 Constant *AlignInBits,
776 Constant *OffsetInBits,
777 unsigned Flags,
778 DIType DerivedFrom,
779 DIArray Elements,
780 unsigned RunTimeLang = 0,
781 MDNode *ContainingType = 0);
782
783 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
784 /// See comments in DISubprogram for descriptions of these fields.
785 DISubprogram CreateSubprogram(DIDescriptor Context, StringRef Name,
786 StringRef DisplayName,
787 StringRef LinkageName,
788 DIFile F, unsigned LineNo,
789 DIType Ty, bool isLocalToUnit,
790 bool isDefinition,
791 unsigned VK = 0,
792 unsigned VIndex = 0,
793 DIType ContainingType = DIType(),
794 unsigned Flags = 0,
795 bool isOptimized = false,
796 Function *Fn = 0);
797
798 /// CreateSubprogramDefinition - Create new subprogram descriptor for the
799 /// given declaration.
800 DISubprogram CreateSubprogramDefinition(DISubprogram &SPDeclaration);
801
802 /// CreateGlobalVariable - Create a new descriptor for the specified global.
803 DIGlobalVariable
804 CreateGlobalVariable(DIDescriptor Context, StringRef Name,
805 StringRef DisplayName,
806 StringRef LinkageName,
807 DIFile F,
808 unsigned LineNo, DIType Ty, bool isLocalToUnit,
809 bool isDefinition, llvm::GlobalVariable *GV);
810
811 /// CreateGlobalVariable - Create a new descriptor for the specified constant.
812 DIGlobalVariable
813 CreateGlobalVariable(DIDescriptor Context, StringRef Name,
814 StringRef DisplayName,
815 StringRef LinkageName,
816 DIFile F,
817 unsigned LineNo, DIType Ty, bool isLocalToUnit,
818 bool isDefinition, llvm::Constant *C);
819
820 /// CreateVariable - Create a new descriptor for the specified variable.
821 DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
822 StringRef Name,
823 DIFile F, unsigned LineNo,
824 DIType Ty, bool AlwaysPreserve = false,
825 unsigned Flags = 0);
826
827 /// CreateComplexVariable - Create a new descriptor for the specified
828 /// variable which has a complex address expression for its address.
829 DIVariable CreateComplexVariable(unsigned Tag, DIDescriptor Context,
830 StringRef Name, DIFile F, unsigned LineNo,
831 DIType Ty, Value *const *Addr,
832 unsigned NumAddr);
833
834 /// CreateLexicalBlock - This creates a descriptor for a lexical block
835 /// with the specified parent context.
836 DILexicalBlock CreateLexicalBlock(DIDescriptor Context, DIFile F,
837 unsigned Line = 0, unsigned Col = 0);
838
839 /// CreateNameSpace - This creates new descriptor for a namespace
840 /// with the specified parent context.
841 DINameSpace CreateNameSpace(DIDescriptor Context, StringRef Name,
842 DIFile F, unsigned LineNo);
843
844 /// CreateLocation - Creates a debug info location.
845 DILocation CreateLocation(unsigned LineNo, unsigned ColumnNo,
846 DIScope S, DILocation OrigLoc);
847
848 /// CreateLocation - Creates a debug info location.
849 DILocation CreateLocation(unsigned LineNo, unsigned ColumnNo,
850 DIScope S, MDNode *OrigLoc = 0);
851
852 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
853 Instruction *InsertDeclare(llvm::Value *Storage, DIVariable D,
854 BasicBlock *InsertAtEnd);
855
856 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
857 Instruction *InsertDeclare(llvm::Value *Storage, DIVariable D,
858 Instruction *InsertBefore);
859
860 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
861 Instruction *InsertDbgValueIntrinsic(llvm::Value *V, uint64_t Offset,
862 DIVariable D, BasicBlock *InsertAtEnd);
863
864 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
865 Instruction *InsertDbgValueIntrinsic(llvm::Value *V, uint64_t Offset,
866 DIVariable D, Instruction *InsertBefore);
867
868 // RecordType - Record DIType in a module such that it is not lost even if
869 // it is not referenced through debug info anchors.
870 void RecordType(DIType T);
871
872 private:
873 Constant *GetTagConstant(unsigned TAG);
874 };
875
876668 /// getDISubprogram - Find subprogram that is enclosing this scope.
877669 DISubprogram getDISubprogram(const MDNode *Scope);
878670
724724 print(dbgs()); dbgs() << '\n';
725725 }
726726
727 //===----------------------------------------------------------------------===//
728 // DIFactory: Basic Helpers
729 //===----------------------------------------------------------------------===//
730
731 DIFactory::DIFactory(Module &m)
732 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
733
734 Constant *DIFactory::GetTagConstant(unsigned TAG) {
735 assert((TAG & LLVMDebugVersionMask) == 0 &&
736 "Tag too large for debug encoding!");
737 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
738 }
739
740 //===----------------------------------------------------------------------===//
741 // DIFactory: Primary Constructors
742 //===----------------------------------------------------------------------===//
743
744 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
745 /// This implicitly uniques the arrays created.
746 DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
747 if (NumTys == 0) {
748 Value *Null = llvm::Constant::getNullValue(Type::getInt32Ty(VMContext));
749 return DIArray(MDNode::get(VMContext, &Null, 1));
750 }
751
752 SmallVector Elts(Tys, Tys+NumTys);
753 return DIArray(MDNode::get(VMContext, Elts.data(), Elts.size()));
754 }
755
756 /// GetOrCreateSubrange - Create a descriptor for a value range. This
757 /// implicitly uniques the values returned.
758 DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
759 Value *Elts[] = {
760 GetTagConstant(dwarf::DW_TAG_subrange_type),
761 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
762 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
763 };
764
765 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
766 }
767
768 /// CreateUnspecifiedParameter - Create unspeicified type descriptor
769 /// for the subroutine type.
770 DIDescriptor DIFactory::CreateUnspecifiedParameter() {
771 Value *Elts[] = {
772 GetTagConstant(dwarf::DW_TAG_unspecified_parameters)
773 };
774 return DIDescriptor(MDNode::get(VMContext, &Elts[0], 1));
775 }
776
777 /// CreateCompileUnit - Create a new descriptor for the specified compile
778 /// unit. Note that this does not unique compile units within the module.
779 DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
780 StringRef Filename,
781 StringRef Directory,
782 StringRef Producer,
783 bool isMain,
784 bool isOptimized,
785 StringRef Flags,
786 unsigned RunTimeVer) {
787 Value *Elts[] = {
788 GetTagConstant(dwarf::DW_TAG_compile_unit),
789 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
790 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
791 MDString::get(VMContext, Filename),
792 MDString::get(VMContext, Directory),
793 MDString::get(VMContext, Producer),
794 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
795 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
796 MDString::get(VMContext, Flags),
797 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
798 };
799
800 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
801 }
802
803 /// CreateFile - Create a new descriptor for the specified file.
804 DIFile DIFactory::CreateFile(StringRef Filename,
805 StringRef Directory,
806 DICompileUnit CU) {
807 Value *Elts[] = {
808 GetTagConstant(dwarf::DW_TAG_file_type),
809 MDString::get(VMContext, Filename),
810 MDString::get(VMContext, Directory),
811 CU
812 };
813
814 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
815 }
816
817 /// CreateEnumerator - Create a single enumerator value.
818 DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
819 Value *Elts[] = {
820 GetTagConstant(dwarf::DW_TAG_enumerator),
821 MDString::get(VMContext, Name),
822 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
823 };
824 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
825 }
826
827
828 /// CreateBasicType - Create a basic type like int, float, etc.
829 DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
830 StringRef Name,
831 DIFile F,
832 unsigned LineNumber,
833 uint64_t SizeInBits,
834 uint64_t AlignInBits,
835 uint64_t OffsetInBits, unsigned Flags,
836 unsigned Encoding) {
837 Value *Elts[] = {
838 GetTagConstant(dwarf::DW_TAG_base_type),
839 Context,
840 MDString::get(VMContext, Name),
841 F,
842 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
843 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
844 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
845 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
846 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
847 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
848 };
849 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
850 }
851
852
853 /// CreateBasicType - Create a basic type like int, float, etc.
854 DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
855 StringRef Name,
856 DIFile F,
857 unsigned LineNumber,
858 Constant *SizeInBits,
859 Constant *AlignInBits,
860 Constant *OffsetInBits, unsigned Flags,
861 unsigned Encoding) {
862 Value *Elts[] = {
863 GetTagConstant(dwarf::DW_TAG_base_type),
864 Context,
865 MDString::get(VMContext, Name),
866 F,
867 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
868 SizeInBits,
869 AlignInBits,
870 OffsetInBits,
871 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
872 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
873 };
874 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
875 }
876
877 /// CreateArtificialType - Create a new DIType with "artificial" flag set.
878 DIType DIFactory::CreateArtificialType(DIType Ty) {
879 if (Ty.isArtificial())
880 return Ty;
881
882 SmallVector Elts;
883 MDNode *N = Ty;
884 assert (N && "Unexpected input DIType!");
885 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
886 if (Value *V = N->getOperand(i))
887 Elts.push_back(V);
888 else
889 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
890 }
891
892 unsigned CurFlags = Ty.getFlags();
893 CurFlags = CurFlags | DIType::FlagArtificial;
894
895 // Flags are stored at this slot.
896 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
897
898 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
899 }
900
901 /// CreateDerivedType - Create a derived type like const qualified type,
902 /// pointer, typedef, etc.
903 DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
904 DIDescriptor Context,
905 StringRef Name,
906 DIFile F,
907 unsigned LineNumber,
908 uint64_t SizeInBits,
909 uint64_t AlignInBits,
910 uint64_t OffsetInBits,
911 unsigned Flags,
912 DIType DerivedFrom) {
913 Value *Elts[] = {
914 GetTagConstant(Tag),
915 Context,
916 MDString::get(VMContext, Name),
917 F,
918 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
919 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
920 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
921 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
922 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
923 DerivedFrom,
924 };
925 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
926 }
927
928
929 /// CreateDerivedType - Create a derived type like const qualified type,
930 /// pointer, typedef, etc.
931 DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
932 DIDescriptor Context,
933 StringRef Name,
934 DIFile F,
935 unsigned LineNumber,
936 Constant *SizeInBits,
937 Constant *AlignInBits,
938 Constant *OffsetInBits,
939 unsigned Flags,
940 DIType DerivedFrom) {
941 Value *Elts[] = {
942 GetTagConstant(Tag),
943 Context,
944 MDString::get(VMContext, Name),
945 F,
946 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
947 SizeInBits,
948 AlignInBits,
949 OffsetInBits,
950 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
951 DerivedFrom,
952 };
953 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
954 }
955
956
957 /// CreateCompositeType - Create a composite type like array, struct, etc.
958 DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
959 DIDescriptor Context,
960 StringRef Name,
961 DIFile F,
962 unsigned LineNumber,
963 uint64_t SizeInBits,
964 uint64_t AlignInBits,
965 uint64_t OffsetInBits,
966 unsigned Flags,
967 DIType DerivedFrom,
968 DIArray Elements,
969 unsigned RuntimeLang,
970 MDNode *ContainingType) {
971
972 Value *Elts[] = {
973 GetTagConstant(Tag),
974 Context,
975 MDString::get(VMContext, Name),
976 F,
977 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
978 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
979 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
980 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
981 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
982 DerivedFrom,
983 Elements,
984 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
985 ContainingType
986 };
987
988 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
989 // Create a named metadata so that we do not lose this enum info.
990 if (Tag == dwarf::DW_TAG_enumeration_type) {
991 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
992 NMD->addOperand(Node);
993 }
994 return DICompositeType(Node);
995 }
996
997 /// CreateTemporaryType - Create a temporary forward-declared type.
998 DIType DIFactory::CreateTemporaryType() {
999 // Give the temporary MDNode a tag. It doesn't matter what tag we
1000 // use here as long as DIType accepts it.
1001 Value *Elts[] = {
1002 GetTagConstant(DW_TAG_base_type)
1003 };
1004 MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
1005 return DIType(Node);
1006 }
1007
1008 /// CreateTemporaryType - Create a temporary forward-declared type.
1009 DIType DIFactory::CreateTemporaryType(DIFile F) {
1010 // Give the temporary MDNode a tag. It doesn't matter what tag we
1011 // use here as long as DIType accepts it.
1012 Value *Elts[] = {
1013 GetTagConstant(DW_TAG_base_type),
1014 F.getCompileUnit(),
1015 NULL,
1016 F
1017 };
1018 MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
1019 return DIType(Node);
1020 }
1021
1022 /// CreateCompositeType - Create a composite type like array, struct, etc.
1023 DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
1024 DIDescriptor Context,
1025 StringRef Name,
1026 DIFile F,
1027 unsigned LineNumber,
1028 Constant *SizeInBits,
1029 Constant *AlignInBits,
1030 Constant *OffsetInBits,
1031 unsigned Flags,
1032 DIType DerivedFrom,
1033 DIArray Elements,
1034 unsigned RuntimeLang,
1035 MDNode *ContainingType) {
1036 Value *Elts[] = {
1037 GetTagConstant(Tag),
1038 Context,
1039 MDString::get(VMContext, Name),
1040 F,
1041 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
1042 SizeInBits,
1043 AlignInBits,
1044 OffsetInBits,
1045 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1046 DerivedFrom,
1047 Elements,
1048 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
1049 ContainingType
1050 };
1051 MDNode *Node = MDNode::get(VMContext, &Elts[0], 13);
1052 // Create a named metadata so that we do not lose this enum info.
1053 if (Tag == dwarf::DW_TAG_enumeration_type) {
1054 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
1055 NMD->addOperand(Node);
1056 }
1057 return DICompositeType(Node);
1058 }
1059
1060
1061 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
1062 /// See comments in DISubprogram for descriptions of these fields. This
1063 /// method does not unique the generated descriptors.
1064 DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
1065 StringRef Name,
1066 StringRef DisplayName,
1067 StringRef LinkageName,
1068 DIFile F,
1069 unsigned LineNo, DIType Ty,
1070 bool isLocalToUnit,
1071 bool isDefinition,
1072 unsigned VK, unsigned VIndex,
1073 DIType ContainingType,
1074 unsigned Flags,
1075 bool isOptimized,
1076 Function *Fn) {
1077
1078 Value *Elts[] = {
1079 GetTagConstant(dwarf::DW_TAG_subprogram),
1080 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1081 Context,
1082 MDString::get(VMContext, Name),
1083 MDString::get(VMContext, DisplayName),
1084 MDString::get(VMContext, LinkageName),
1085 F,
1086 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1087 Ty,
1088 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1089 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1090 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
1091 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
1092 ContainingType,
1093 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1094 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1095 Fn
1096 };
1097 MDNode *Node = MDNode::get(VMContext, &Elts[0], 17);
1098
1099 // Create a named metadata so that we do not lose this mdnode.
1100 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1101 NMD->addOperand(Node);
1102 return DISubprogram(Node);
1103 }
1104
1105 /// CreateSubprogramDefinition - Create new subprogram descriptor for the
1106 /// given declaration.
1107 DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration){
1108 if (SPDeclaration.isDefinition())
1109 return DISubprogram(SPDeclaration);
1110
1111 MDNode *DeclNode = SPDeclaration;
1112 Value *Elts[] = {
1113 GetTagConstant(dwarf::DW_TAG_subprogram),
1114 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1115 DeclNode->getOperand(2), // Context
1116 DeclNode->getOperand(3), // Name
1117 DeclNode->getOperand(4), // DisplayName
1118 DeclNode->getOperand(5), // LinkageName
1119 DeclNode->getOperand(6), // CompileUnit
1120 DeclNode->getOperand(7), // LineNo
1121 DeclNode->getOperand(8), // Type
1122 DeclNode->getOperand(9), // isLocalToUnit
1123 ConstantInt::get(Type::getInt1Ty(VMContext), true),
1124 DeclNode->getOperand(11), // Virtuality
1125 DeclNode->getOperand(12), // VIndex
1126 DeclNode->getOperand(13), // Containting Type
1127 DeclNode->getOperand(14), // Flags
1128 DeclNode->getOperand(15), // isOptimized
1129 SPDeclaration.getFunction()
1130 };
1131 MDNode *Node =MDNode::get(VMContext, &Elts[0], 16);
1132
1133 // Create a named metadata so that we do not lose this mdnode.
1134 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1135 NMD->addOperand(Node);
1136 return DISubprogram(Node);
1137 }
1138
1139 /// CreateGlobalVariable - Create a new descriptor for the specified global.
1140 DIGlobalVariable
1141 DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1142 StringRef DisplayName,
1143 StringRef LinkageName,
1144 DIFile F,
1145 unsigned LineNo, DIType Ty,bool isLocalToUnit,
1146 bool isDefinition, llvm::GlobalVariable *Val) {
1147 Value *Elts[] = {
1148 GetTagConstant(dwarf::DW_TAG_variable),
1149 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1150 Context,
1151 MDString::get(VMContext, Name),
1152 MDString::get(VMContext, DisplayName),
1153 MDString::get(VMContext, LinkageName),
1154 F,
1155 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1156 Ty,
1157 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1158 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1159 Val
1160 };
1161
1162 Value *const *Vs = &Elts[0];
1163 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1164
1165 // Create a named metadata so that we do not lose this mdnode.
1166 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1167 NMD->addOperand(Node);
1168
1169 return DIGlobalVariable(Node);
1170 }
1171
1172 /// CreateGlobalVariable - Create a new descriptor for the specified constant.
1173 DIGlobalVariable
1174 DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1175 StringRef DisplayName,
1176 StringRef LinkageName,
1177 DIFile F,
1178 unsigned LineNo, DIType Ty,bool isLocalToUnit,
1179 bool isDefinition, llvm::Constant *Val) {
1180 Value *Elts[] = {
1181 GetTagConstant(dwarf::DW_TAG_variable),
1182 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1183 Context,
1184 MDString::get(VMContext, Name),
1185 MDString::get(VMContext, DisplayName),
1186 MDString::get(VMContext, LinkageName),
1187 F,
1188 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1189 Ty,
1190 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1191 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1192 Val
1193 };
1194
1195 Value *const *Vs = &Elts[0];
1196 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1197
1198 // Create a named metadata so that we do not lose this mdnode.
1199 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
1200 NMD->addOperand(Node);
1201
1202 return DIGlobalVariable(Node);
1203 }
1204
1205727 /// fixupObjcLikeName - Replace contains special characters used
1206728 /// in a typical Objective-C names with '.' in a given string.
1207729 static void fixupObjcLikeName(std::string &Str) {
1213735 }
1214736 }
1215737
1216 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
1217 /// to hold function specific information.
1218 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, StringRef FuncName) {
1219 SmallString<32> Out;
1220 if (FuncName.find('[') == StringRef::npos)
1221 return M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", FuncName)
1222 .toStringRef(Out));
1223 std::string Name = FuncName;
1224 fixupObjcLikeName(Name);
1225 return M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", Name)
1226 .toStringRef(Out));
1227 }
1228
1229738 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
1230739 /// suitable to hold function specific information.
1231740 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, StringRef FuncName) {
1236745 return M.getNamedMetadata(Twine("llvm.dbg.lv.", Name));
1237746 }
1238747
1239 /// CreateVariable - Create a new descriptor for the specified variable.
1240 DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
1241 StringRef Name,
1242 DIFile F,
1243 unsigned LineNo,
1244 DIType Ty, bool AlwaysPreserve,
1245 unsigned Flags) {
1246 Value *Elts[] = {
1247 GetTagConstant(Tag),
1248 Context,
1249 MDString::get(VMContext, Name),
1250 F,
1251 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1252 Ty,
1253 ConstantInt::get(Type::getInt32Ty(VMContext), Flags)
1254 };
1255 MDNode *Node = MDNode::get(VMContext, &Elts[0], 7);
1256 if (AlwaysPreserve) {
1257 // The optimizer may remove local variable. If there is an interest
1258 // to preserve variable info in such situation then stash it in a
1259 // named mdnode.
1260 DISubprogram Fn(getDISubprogram(Context));
1261 StringRef FName = "fn";
1262 if (Fn.getFunction())
1263 FName = Fn.getFunction()->getName();
1264 char One = '\1';
1265 if (FName.startswith(StringRef(&One, 1)))
1266 FName = FName.substr(1);
1267
1268
1269 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, FName);
1270 FnLocals->addOperand(Node);
1271 }
1272 return DIVariable(Node);
1273 }
1274
1275
1276 /// CreateComplexVariable - Create a new descriptor for the specified variable
1277 /// which has a complex address expression for its address.
1278 DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
1279 StringRef Name, DIFile F,
1280 unsigned LineNo,
1281 DIType Ty, Value *const *Addr,
1282 unsigned NumAddr) {
1283 SmallVector Elts;
1284 Elts.push_back(GetTagConstant(Tag));
1285 Elts.push_back(Context);
1286 Elts.push_back(MDString::get(VMContext, Name));
1287 Elts.push_back(F);
1288 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
1289 Elts.push_back(Ty);
1290 Elts.append(Addr, Addr+NumAddr);
1291
1292 return DIVariable(MDNode::get(VMContext, Elts.data(), Elts.size()));
1293 }
1294
1295
1296 /// CreateBlock - This creates a descriptor for a lexical block with the
1297 /// specified parent VMContext.
1298 DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
1299 DIFile F, unsigned LineNo,
1300 unsigned Col) {
1301 // Defeat MDNode uniqing for lexical blocks.
1302 static unsigned int unique_id = 0;
1303 Value *Elts[] = {
1304 GetTagConstant(dwarf::DW_TAG_lexical_block),
1305 Context,
1306 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1307 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1308 F,
1309 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1310 };
1311 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 6));
1312 }
1313
1314 /// CreateNameSpace - This creates new descriptor for a namespace
1315 /// with the specified parent context.
1316 DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
1317 DIFile F,
1318 unsigned LineNo) {
1319 Value *Elts[] = {
1320 GetTagConstant(dwarf::DW_TAG_namespace),
1321 Context,
1322 MDString::get(VMContext, Name),
1323 F,
1324 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1325 };
1326 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1327 }
1328
1329 /// CreateLocation - Creates a debug info location.
1330 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1331 DIScope S, DILocation OrigLoc) {
1332 Value *Elts[] = {
1333 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1334 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1335 S,
1336 OrigLoc,
1337 };
1338 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1339 }
1340
1341 //===----------------------------------------------------------------------===//
1342 // DIFactory: Routines for inserting code into a function
1343 //===----------------------------------------------------------------------===//
1344
1345 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1346 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1347 Instruction *InsertBefore) {
1348 assert(Storage && "no storage passed to dbg.declare");
1349 assert(D.Verify() && "empty DIVariable passed to dbg.declare");
1350 if (!DeclareFn)
1351 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1352
1353 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1354 D };
1355 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
1356 }
1357
1358 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1359 Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
1360 BasicBlock *InsertAtEnd) {
1361 assert(Storage && "no storage passed to dbg.declare");
1362 assert(D.Verify() && "invalid DIVariable passed to dbg.declare");
1363 if (!DeclareFn)
1364 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1365
1366 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1367 D };
1368
1369 // If this block already has a terminator then insert this intrinsic
1370 // before the terminator.
1371 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1372 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1373 else
1374 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
1375
1376 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1377 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1378 DIVariable D,
1379 Instruction *InsertBefore) {
1380 assert(V && "no value passed to dbg.value");
1381 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
1382 if (!ValueFn)
1383 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1384
1385 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1386 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1387 D };
1388 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1389 }
1390
1391 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1392 Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
1393 DIVariable D,
1394 BasicBlock *InsertAtEnd) {
1395 assert(V && "no value passed to dbg.value");
1396 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
1397 if (!ValueFn)
1398 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1399
1400 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
1401 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1402 D };
1403 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1404 }
1405
1406 // RecordType - Record DIType in a module such that it is not lost even if
1407 // it is not referenced through debug info anchors.
1408 void DIFactory::RecordType(DIType T) {
1409 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.ty");
1410 NMD->addOperand(T);
748 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
749 /// to hold function specific information.
750 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, StringRef FuncName) {
751 SmallString<32> Out;
752 if (FuncName.find('[') == StringRef::npos)
753 return M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", FuncName)
754 .toStringRef(Out));
755
756 std::string Name = FuncName;
757 fixupObjcLikeName(Name);
758 return M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", Name)
759 .toStringRef(Out));
1411760 }
1412761
1413762