llvm.org GIT mirror llvm / 56f7ea2
[LLVM-C] Add more debug information accessors to GlobalObject and Instruction Summary: Adds missing debug information accessors to GlobalObject. This puts the finishing touches on cloning debug info in the echo tests. Reviewers: whitequark, deadalnix Reviewed By: whitequark Subscribers: aprantl, JDevlieghere, llvm-commits, harlanhaskins Differential Revision: https://reviews.llvm.org/D51522 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@343330 91177308-0d34-0410-b5e6-96231b3b80d8 Robert Widmann 2 years ago
5 changed file(s) with 194 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
21342134 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes);
21352135
21362136 /**
2137 * Sets a metadata attachment, erasing the existing metadata attachment if
2138 * it already exists for the given kind.
2139 *
2140 * @see llvm::GlobalObject::setMetadata()
2141 */
2142 void LLVMGlobalSetMetadata(LLVMValueRef Global, unsigned Kind,
2143 LLVMMetadataRef MD);
2144
2145 /**
2146 * Erases a metadata attachment of the given kind if it exists.
2147 *
2148 * @see llvm::GlobalObject::eraseMetadata()
2149 */
2150 void LLVMGlobalEraseMetadata(LLVMValueRef Global, unsigned Kind);
2151
2152 /**
2153 * Removes all metadata attachments from this value.
2154 *
2155 * @see llvm::GlobalObject::clearMetadata()
2156 */
2157 void LLVMGlobalClearMetadata(LLVMValueRef Global);
2158
2159 /**
2160 * Retrieves an array of metadata entries representing the metadata attached to
2161 * this value. The caller is responsible for freeing this array by calling
2162 * \c LLVMDisposeValueMetadataEntries.
2163 *
2164 * @see llvm::GlobalObject::getAllMetadata()
2165 */
2166 LLVMValueMetadataEntry *LLVMGlobalCopyAllMetadata(LLVMValueRef Value,
2167 size_t *NumEntries);
2168
2169 /**
2170 * Destroys value metadata entries.
2171 */
2172 void LLVMDisposeValueMetadataEntries(LLVMValueMetadataEntry *Entries);
2173
2174 /**
2175 * Returns the kind of a value metadata entry at a specific index.
2176 */
2177 unsigned LLVMValueMetadataEntriesGetKind(LLVMValueMetadataEntry *Entries,
2178 unsigned Index);
2179
2180 /**
2181 * Returns the underlying metadata node of a value metadata entry at a
2182 * specific index.
2183 */
2184 LLVMMetadataRef
2185 LLVMValueMetadataEntriesGetMetadata(LLVMValueMetadataEntry *Entries,
2186 unsigned Index);
2187
2188 /**
21372189 * @}
21382190 */
21392191
27592811 * Set metadata associated with an instruction value.
27602812 */
27612813 void LLVMSetMetadata(LLVMValueRef Val, unsigned KindID, LLVMValueRef Node);
2814
2815 /**
2816 * Returns the metadata associated with an instruction value, but filters out
2817 * all the debug locations.
2818 *
2819 * @see llvm::Instruction::getAllMetadataOtherThanDebugLoc()
2820 */
2821 LLVMValueMetadataEntry *
2822 LLVMInstructionGetAllMetadataOtherThanDebugLoc(LLVMValueRef Instr,
2823 size_t *NumEntries);
27622824
27632825 /**
27642826 * Obtain the basic block to which an instruction belongs.
9696 typedef struct LLVMOpaqueNamedMDNode *LLVMNamedMDNodeRef;
9797
9898 /**
99 * Represents an entry in a Global Object's metadata attachments.
100 *
101 * This models std::pair
102 */
103 typedef struct LLVMOpaqueValueMetadataEntry LLVMValueMetadataEntry;
104
105 /**
99106 * Represents an LLVM basic block builder.
100107 *
101108 * This models llvm::IRBuilder.
869869 MDNode *N = Val ? extractMDNode(unwrap(Val)) : nullptr;
870870
871871 unwrap(Inst)->setMetadata(KindID, N);
872 }
873
874 LLVMValueMetadataEntry *
875 LLVMInstructionGetAllMetadataOtherThanDebugLoc(LLVMValueRef Value,
876 size_t *NumEntries) {
877 return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
878 unwrap(Value)->getAllMetadata(Entries);
879 });
872880 }
873881
874882 /*--.. Conversion functions ................................................--*/
18751883 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
18761884 }
18771885
1886 struct LLVMOpaqueValueMetadataEntry {
1887 unsigned Kind;
1888 LLVMMetadataRef Metadata;
1889 };
1890
1891 using MetadataEntries = SmallVectorImpl>;
1892 static LLVMValueMetadataEntry *
1893 llvm_getMetadata(size_t *NumEntries,
1894 llvm::function_ref AccessMD) {
1895 SmallVector, 8> MVEs;
1896 AccessMD(MVEs);
1897
1898 LLVMOpaqueValueMetadataEntry *Result =
1899 static_cast(
1900 safe_malloc(MVEs.size() * sizeof(LLVMOpaqueValueMetadataEntry)));
1901 for (unsigned i = 0; i < MVEs.size(); ++i) {
1902 const auto &ModuleFlag = MVEs[i];
1903 Result[i].Kind = ModuleFlag.first;
1904 Result[i].Metadata = wrap(ModuleFlag.second);
1905 }
1906 *NumEntries = MVEs.size();
1907 return Result;
1908 }
1909
1910 LLVMValueMetadataEntry *LLVMGlobalCopyAllMetadata(LLVMValueRef Value,
1911 size_t *NumEntries) {
1912 return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
1913 if (Instruction *Instr = dyn_cast(unwrap(Value))) {
1914 Instr->getAllMetadata(Entries);
1915 } else {
1916 unwrap(Value)->getAllMetadata(Entries);
1917 }
1918 });
1919 }
1920
1921 unsigned LLVMValueMetadataEntriesGetKind(LLVMValueMetadataEntry *Entries,
1922 unsigned Index) {
1923 LLVMOpaqueValueMetadataEntry MVE =
1924 static_cast(Entries[Index]);
1925 return MVE.Kind;
1926 }
1927
1928 LLVMMetadataRef
1929 LLVMValueMetadataEntriesGetMetadata(LLVMValueMetadataEntry *Entries,
1930 unsigned Index) {
1931 LLVMOpaqueValueMetadataEntry MVE =
1932 static_cast(Entries[Index]);
1933 return MVE.Metadata;
1934 }
1935
1936 void LLVMDisposeValueMetadataEntries(LLVMValueMetadataEntry *Entries) {
1937 free(Entries);
1938 }
1939
1940 void LLVMGlobalSetMetadata(LLVMValueRef Global, unsigned Kind,
1941 LLVMMetadataRef MD) {
1942 unwrap(Global)->setMetadata(Kind, unwrapDI(MD));
1943 }
1944
1945 void LLVMGlobalEraseMetadata(LLVMValueRef Global, unsigned Kind) {
1946 unwrap(Global)->eraseMetadata(Kind);
1947 }
1948
1949 void LLVMGlobalClearMetadata(LLVMValueRef Global) {
1950 unwrap(Global)->clearMetadata();
1951 }
1952
18781953 /*--.. Operations on global variables ......................................--*/
18791954
18801955 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
165165 ret void
166166 }
167167
168 !llvm.module.flags = !{!0}
169 !named = !{!1, !2, !3}
168 define void @with_debuginfo() !dbg !4 {
169 ret void, !dbg !7
170 }
170171
171 !0 = !{i32 2, !"Debug Info Version", i32 3}
172 !1 = distinct !{}
173 !2 = distinct !{}
174 !3 = !{!4}
175 !4 = distinct !{}
172 !llvm.dbg.cu = !{!0, !2}
173 !llvm.module.flags = !{!3}
174
175 !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
176 !1 = !DIFile(filename: "echo.ll", directory: "/llvm/test/Bindings/llvm-c/echo.ll")
177 !2 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
178 !3 = !{i32 2, !"Debug Info Version", i32 3}
179 !4 = distinct !DISubprogram(name: "with_debuginfo", linkageName: "_with_debuginfo", scope: null, file: !1, line: 42, type: !5, isLocal: false, isDefinition: true, scopeLine: 1519, flags: DIFlagPrototyped, isOptimized: true, unit: !0, templateParams: !6, retainedNodes: !6)
180 !5 = !DISubroutineType(types: !6)
181 !6 = !{}
182 !7 = !DILocation(line: 42, scope: !8, inlinedAt: !11)
183 !8 = distinct !DILexicalBlock(scope: !9, file: !1, line: 42, column: 12)
184 !9 = distinct !DISubprogram(name: "fake_inlined_block", linkageName: "_fake_inlined_block", scope: null, file: !1, line: 82, type: !5, isLocal: false, isDefinition: true, scopeLine: 82, flags: DIFlagPrototyped, isOptimized: true, unit: !2, templateParams: !6, retainedNodes: !6)
185 !10 = distinct !DILocation(line: 84, scope: !8, inlinedAt: !11)
186 !11 = !DILocation(line: 42, scope: !4)
1515 //===----------------------------------------------------------------------===//
1616
1717 #include "llvm-c-test.h"
18 #include "llvm-c/DebugInfo.h"
1819 #include "llvm-c/Target.h"
1920 #include "llvm/ADT/DenseMap.h"
2021 #include "llvm/Support/ErrorHandling.h"
727728 exit(-1);
728729 }
729730
731 auto Ctx = LLVMGetModuleContext(M);
732 size_t NumMetadataEntries;
733 auto *AllMetadata =
734 LLVMInstructionGetAllMetadataOtherThanDebugLoc(Src,
735 &NumMetadataEntries);
736 for (unsigned i = 0; i < NumMetadataEntries; ++i) {
737 unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i);
738 LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i);
739 LLVMSetMetadata(Dst, Kind, LLVMMetadataAsValue(Ctx, MD));
740 }
741 LLVMDisposeValueMetadataEntries(AllMetadata);
742 LLVMSetInstDebugLocation(Builder, Dst);
743
730744 check_value_kind(Dst, LLVMInstructionValueKind);
731745 return VMap[Src] = Dst;
732746 }
9981012 if (auto I = LLVMGetInitializer(Cur))
9991013 LLVMSetInitializer(G, clone_constant(I, M));
10001014
1015 size_t NumMetadataEntries;
1016 auto *AllMetadata = LLVMGlobalCopyAllMetadata(Cur, &NumMetadataEntries);
1017 for (unsigned i = 0; i < NumMetadataEntries; ++i) {
1018 unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i);
1019 LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i);
1020 LLVMGlobalSetMetadata(G, Kind, MD);
1021 }
1022 LLVMDisposeValueMetadataEntries(AllMetadata);
1023
10011024 LLVMSetGlobalConstant(G, LLVMIsGlobalConstant(Cur));
10021025 LLVMSetThreadLocal(G, LLVMIsThreadLocal(Cur));
10031026 LLVMSetExternallyInitialized(G, LLVMIsExternallyInitialized(Cur));
10491072 LLVMSetPersonalityFn(Fun, P);
10501073 }
10511074
1075 size_t NumMetadataEntries;
1076 auto *AllMetadata = LLVMGlobalCopyAllMetadata(Cur, &NumMetadataEntries);
1077 for (unsigned i = 0; i < NumMetadataEntries; ++i) {
1078 unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i);
1079 LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i);
1080 LLVMGlobalSetMetadata(Fun, Kind, MD);
1081 }
1082 LLVMDisposeValueMetadataEntries(AllMetadata);
1083
10521084 FunCloner FC(Cur, Fun);
10531085 FC.CloneBBs(Cur);
10541086