blob: 19ad5bd854b9333e6b2b10b8bdfd3e39bb0ff2e6 [file] [log] [blame]
Amaury Sechete8ea7d82016-02-04 23:26:19 +00001//===-- echo.cpp - tool for testing libLLVM and llvm-c API ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Jeroen Ketemaad659c32016-04-08 09:19:02 +000010// This file implements the --echo command in llvm-c-test.
Amaury Sechete8ea7d82016-02-04 23:26:19 +000011//
12// This command uses the C API to read a module and output an exact copy of it
13// as output. It is used to check that the resulting module matches the input
14// to validate that the C API can read and write modules properly.
15//
16//===----------------------------------------------------------------------===//
17
18#include "llvm-c-test.h"
Amaury Sechet55909672016-02-16 05:11:24 +000019#include "llvm-c/Target.h"
Amaury Sechete8ea7d82016-02-04 23:26:19 +000020#include "llvm/ADT/DenseMap.h"
Amaury Secheta82042e2016-02-09 22:36:41 +000021#include "llvm/Support/ErrorHandling.h"
Amaury Sechete8ea7d82016-02-04 23:26:19 +000022
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000023#include <stdio.h>
24#include <stdlib.h>
Amaury Sechete8ea7d82016-02-04 23:26:19 +000025
26using namespace llvm;
27
28// Provide DenseMapInfo for C API opaque types.
29template<typename T>
30struct CAPIDenseMap {};
31
32// The default DenseMapInfo require to know about pointer alignement.
33// Because the C API uses opaques pointer types, their alignement is unknown.
34// As a result, we need to roll out our own implementation.
35template<typename T>
36struct CAPIDenseMap<T*> {
37 struct CAPIDenseMapInfo {
38 static inline T* getEmptyKey() {
39 uintptr_t Val = static_cast<uintptr_t>(-1);
40 return reinterpret_cast<T*>(Val);
41 }
42 static inline T* getTombstoneKey() {
43 uintptr_t Val = static_cast<uintptr_t>(-2);
44 return reinterpret_cast<T*>(Val);
45 }
46 static unsigned getHashValue(const T *PtrVal) {
47 return hash_value(PtrVal);
48 }
49 static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; }
50 };
51
52 typedef DenseMap<T*, T*, CAPIDenseMapInfo> Map;
53};
54
55typedef CAPIDenseMap<LLVMValueRef>::Map ValueMap;
Amaury Secheta82042e2016-02-09 22:36:41 +000056typedef CAPIDenseMap<LLVMBasicBlockRef>::Map BasicBlockMap;
Amaury Sechete8ea7d82016-02-04 23:26:19 +000057
Amaury Sechetaad93532016-02-10 00:38:50 +000058struct TypeCloner {
59 LLVMModuleRef M;
60 LLVMContextRef Ctx;
61
62 TypeCloner(LLVMModuleRef M): M(M), Ctx(LLVMGetModuleContext(M)) {}
63
Amaury Sechetc679dbd2016-02-14 09:30:42 +000064 LLVMTypeRef Clone(LLVMValueRef Src) {
65 return Clone(LLVMTypeOf(Src));
66 }
67
Amaury Sechetaad93532016-02-10 00:38:50 +000068 LLVMTypeRef Clone(LLVMTypeRef Src) {
69 LLVMTypeKind Kind = LLVMGetTypeKind(Src);
70 switch (Kind) {
71 case LLVMVoidTypeKind:
72 return LLVMVoidTypeInContext(Ctx);
73 case LLVMHalfTypeKind:
74 return LLVMHalfTypeInContext(Ctx);
75 case LLVMFloatTypeKind:
76 return LLVMFloatTypeInContext(Ctx);
77 case LLVMDoubleTypeKind:
78 return LLVMDoubleTypeInContext(Ctx);
79 case LLVMX86_FP80TypeKind:
80 return LLVMX86FP80TypeInContext(Ctx);
81 case LLVMFP128TypeKind:
82 return LLVMFP128TypeInContext(Ctx);
83 case LLVMPPC_FP128TypeKind:
84 return LLVMPPCFP128TypeInContext(Ctx);
85 case LLVMLabelTypeKind:
86 return LLVMLabelTypeInContext(Ctx);
87 case LLVMIntegerTypeKind:
88 return LLVMIntTypeInContext(Ctx, LLVMGetIntTypeWidth(Src));
89 case LLVMFunctionTypeKind: {
90 unsigned ParamCount = LLVMCountParamTypes(Src);
91 LLVMTypeRef* Params = nullptr;
92 if (ParamCount > 0) {
Serge Pavlov76d8cce2018-02-20 05:41:26 +000093 Params = static_cast<LLVMTypeRef*>(
94 safe_malloc(ParamCount * sizeof(LLVMTypeRef)));
Amaury Sechetaad93532016-02-10 00:38:50 +000095 LLVMGetParamTypes(Src, Params);
96 for (unsigned i = 0; i < ParamCount; i++)
97 Params[i] = Clone(Params[i]);
98 }
99
100 LLVMTypeRef FunTy = LLVMFunctionType(Clone(LLVMGetReturnType(Src)),
101 Params, ParamCount,
102 LLVMIsFunctionVarArg(Src));
103 if (ParamCount > 0)
104 free(Params);
105 return FunTy;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000106 }
Amaury Sechetaad93532016-02-10 00:38:50 +0000107 case LLVMStructTypeKind: {
108 LLVMTypeRef S = nullptr;
109 const char *Name = LLVMGetStructName(Src);
110 if (Name) {
111 S = LLVMGetTypeByName(M, Name);
112 if (S)
113 return S;
114 S = LLVMStructCreateNamed(Ctx, Name);
115 if (LLVMIsOpaqueStruct(Src))
116 return S;
117 }
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000118
Amaury Sechetaad93532016-02-10 00:38:50 +0000119 unsigned EltCount = LLVMCountStructElementTypes(Src);
120 SmallVector<LLVMTypeRef, 8> Elts;
121 for (unsigned i = 0; i < EltCount; i++)
122 Elts.push_back(Clone(LLVMStructGetTypeAtIndex(Src, i)));
123 if (Name)
124 LLVMStructSetBody(S, Elts.data(), EltCount, LLVMIsPackedStruct(Src));
125 else
126 S = LLVMStructTypeInContext(Ctx, Elts.data(), EltCount,
127 LLVMIsPackedStruct(Src));
128 return S;
129 }
130 case LLVMArrayTypeKind:
131 return LLVMArrayType(
132 Clone(LLVMGetElementType(Src)),
133 LLVMGetArrayLength(Src)
134 );
135 case LLVMPointerTypeKind:
136 return LLVMPointerType(
137 Clone(LLVMGetElementType(Src)),
138 LLVMGetPointerAddressSpace(Src)
139 );
140 case LLVMVectorTypeKind:
141 return LLVMVectorType(
142 Clone(LLVMGetElementType(Src)),
143 LLVMGetVectorSize(Src)
144 );
145 case LLVMMetadataTypeKind:
whitequark131f98f2017-10-27 11:51:40 +0000146 return LLVMMetadataTypeInContext(Ctx);
Amaury Sechetaad93532016-02-10 00:38:50 +0000147 case LLVMX86_MMXTypeKind:
148 return LLVMX86MMXTypeInContext(Ctx);
Robert Widmann478fce92018-03-30 17:49:53 +0000149 case LLVMTokenTypeKind:
150 return LLVMTokenTypeInContext(Ctx);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000151 }
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000152
Amaury Sechetaad93532016-02-10 00:38:50 +0000153 fprintf(stderr, "%d is not a supported typekind\n", Kind);
154 exit(-1);
155 }
156};
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000157
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000158static ValueMap clone_params(LLVMValueRef Src, LLVMValueRef Dst) {
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000159 unsigned Count = LLVMCountParams(Src);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000160 if (Count != LLVMCountParams(Dst))
161 report_fatal_error("Parameter count mismatch");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000162
163 ValueMap VMap;
164 if (Count == 0)
165 return VMap;
166
167 LLVMValueRef SrcFirst = LLVMGetFirstParam(Src);
168 LLVMValueRef DstFirst = LLVMGetFirstParam(Dst);
169 LLVMValueRef SrcLast = LLVMGetLastParam(Src);
170 LLVMValueRef DstLast = LLVMGetLastParam(Dst);
171
172 LLVMValueRef SrcCur = SrcFirst;
173 LLVMValueRef DstCur = DstFirst;
174 LLVMValueRef SrcNext = nullptr;
175 LLVMValueRef DstNext = nullptr;
176 while (true) {
177 const char *Name = LLVMGetValueName(SrcCur);
178 LLVMSetValueName(DstCur, Name);
179
180 VMap[SrcCur] = DstCur;
181
182 Count--;
183 SrcNext = LLVMGetNextParam(SrcCur);
184 DstNext = LLVMGetNextParam(DstCur);
185 if (SrcNext == nullptr && DstNext == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000186 if (SrcCur != SrcLast)
187 report_fatal_error("SrcLast param does not match End");
188 if (DstCur != DstLast)
189 report_fatal_error("DstLast param does not match End");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000190 break;
191 }
192
Amaury Sechetd01c8612016-02-14 10:06:34 +0000193 if (SrcNext == nullptr)
194 report_fatal_error("SrcNext was unexpectedly null");
195 if (DstNext == nullptr)
196 report_fatal_error("DstNext was unexpectedly null");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000197
198 LLVMValueRef SrcPrev = LLVMGetPreviousParam(SrcNext);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000199 if (SrcPrev != SrcCur)
200 report_fatal_error("SrcNext.Previous param is not Current");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000201
202 LLVMValueRef DstPrev = LLVMGetPreviousParam(DstNext);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000203 if (DstPrev != DstCur)
204 report_fatal_error("DstNext.Previous param is not Current");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000205
206 SrcCur = SrcNext;
207 DstCur = DstNext;
208 }
209
Amaury Sechetd01c8612016-02-14 10:06:34 +0000210 if (Count != 0)
211 report_fatal_error("Parameter count does not match iteration");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000212
213 return VMap;
214}
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000215
Amaury Sechet49491312016-04-07 05:56:20 +0000216static void check_value_kind(LLVMValueRef V, LLVMValueKind K) {
217 if (LLVMGetValueKind(V) != K)
218 report_fatal_error("LLVMGetValueKind returned incorrect type");
219}
220
221static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M);
222
223static LLVMValueRef clone_constant(LLVMValueRef Cst, LLVMModuleRef M) {
224 LLVMValueRef Ret = clone_constant_impl(Cst, M);
225 check_value_kind(Ret, LLVMGetValueKind(Cst));
226 return Ret;
227}
228
229static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M) {
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000230 if (!LLVMIsAConstant(Cst))
231 report_fatal_error("Expected a constant");
232
233 // Maybe it is a symbol
234 if (LLVMIsAGlobalValue(Cst)) {
235 const char *Name = LLVMGetValueName(Cst);
236
237 // Try function
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000238 if (LLVMIsAFunction(Cst)) {
Amaury Sechet49491312016-04-07 05:56:20 +0000239 check_value_kind(Cst, LLVMFunctionValueKind);
240 LLVMValueRef Dst = LLVMGetNamedFunction(M, Name);
241 if (Dst)
242 return Dst;
243 report_fatal_error("Could not find function");
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000244 }
Amaury Sechet49491312016-04-07 05:56:20 +0000245
246 // Try global variable
247 if (LLVMIsAGlobalVariable(Cst)) {
248 check_value_kind(Cst, LLVMGlobalVariableValueKind);
249 LLVMValueRef Dst = LLVMGetNamedGlobal(M, Name);
250 if (Dst)
251 return Dst;
252 report_fatal_error("Could not find function");
253 }
254
255 fprintf(stderr, "Could not find @%s\n", Name);
256 exit(-1);
257 }
258
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000259 // Try integer literal
Amaury Sechet49491312016-04-07 05:56:20 +0000260 if (LLVMIsAConstantInt(Cst)) {
261 check_value_kind(Cst, LLVMConstantIntValueKind);
262 return LLVMConstInt(TypeCloner(M).Clone(Cst),
263 LLVMConstIntGetZExtValue(Cst), false);
264 }
265
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000266 // Try zeroinitializer
Amaury Sechet49491312016-04-07 05:56:20 +0000267 if (LLVMIsAConstantAggregateZero(Cst)) {
268 check_value_kind(Cst, LLVMConstantAggregateZeroValueKind);
269 return LLVMConstNull(TypeCloner(M).Clone(Cst));
270 }
271
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000272 // Try constant array
Amaury Sechet49491312016-04-07 05:56:20 +0000273 if (LLVMIsAConstantArray(Cst)) {
274 check_value_kind(Cst, LLVMConstantArrayValueKind);
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000275 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
276 unsigned EltCount = LLVMGetArrayLength(Ty);
277 SmallVector<LLVMValueRef, 8> Elts;
278 for (unsigned i = 0; i < EltCount; i++)
279 Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
Amaury Sechet49491312016-04-07 05:56:20 +0000280 return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
281 }
282
Amaury Sechetb3256862016-03-13 00:58:25 +0000283 // Try contant data array
Amaury Sechet49491312016-04-07 05:56:20 +0000284 if (LLVMIsAConstantDataArray(Cst)) {
285 check_value_kind(Cst, LLVMConstantDataArrayValueKind);
Amaury Sechetb3256862016-03-13 00:58:25 +0000286 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
287 unsigned EltCount = LLVMGetArrayLength(Ty);
288 SmallVector<LLVMValueRef, 8> Elts;
289 for (unsigned i = 0; i < EltCount; i++)
290 Elts.push_back(clone_constant(LLVMGetElementAsConstant(Cst, i), M));
Amaury Sechet49491312016-04-07 05:56:20 +0000291 return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
292 }
293
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000294 // Try constant struct
Amaury Sechet49491312016-04-07 05:56:20 +0000295 if (LLVMIsAConstantStruct(Cst)) {
296 check_value_kind(Cst, LLVMConstantStructValueKind);
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000297 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
298 unsigned EltCount = LLVMCountStructElementTypes(Ty);
299 SmallVector<LLVMValueRef, 8> Elts;
300 for (unsigned i = 0; i < EltCount; i++)
301 Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
302 if (LLVMGetStructName(Ty))
Amaury Sechet49491312016-04-07 05:56:20 +0000303 return LLVMConstNamedStruct(Ty, Elts.data(), EltCount);
304 return LLVMConstStructInContext(LLVMGetModuleContext(M), Elts.data(),
305 EltCount, LLVMIsPackedStruct(Ty));
306 }
307
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000308 // Try undef
Amaury Sechet49491312016-04-07 05:56:20 +0000309 if (LLVMIsUndef(Cst)) {
310 check_value_kind(Cst, LLVMUndefValueValueKind);
311 return LLVMGetUndef(TypeCloner(M).Clone(Cst));
312 }
313
Robert Widmann478fce92018-03-30 17:49:53 +0000314 // Try null
315 if (LLVMIsNull(Cst)) {
316 check_value_kind(Cst, LLVMConstantTokenNoneValueKind);
317 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
318 return LLVMConstNull(Ty);
319 }
320
Amaury Sechet40bbe512016-02-17 23:55:59 +0000321 // Try float literal
Amaury Sechet49491312016-04-07 05:56:20 +0000322 if (LLVMIsAConstantFP(Cst)) {
323 check_value_kind(Cst, LLVMConstantFPValueKind);
Amaury Sechet40bbe512016-02-17 23:55:59 +0000324 report_fatal_error("ConstantFP is not supported");
Amaury Sechet49491312016-04-07 05:56:20 +0000325 }
326
Amaury Sechet6b16c232016-02-16 07:33:23 +0000327 // This kind of constant is not supported
Amaury Sechet49491312016-04-07 05:56:20 +0000328 if (!LLVMIsAConstantExpr(Cst))
Amaury Sechet6b16c232016-02-16 07:33:23 +0000329 report_fatal_error("Expected a constant expression");
Amaury Sechet49491312016-04-07 05:56:20 +0000330
331 // At this point, it must be a constant expression
332 check_value_kind(Cst, LLVMConstantExprValueKind);
333
334 LLVMOpcode Op = LLVMGetConstOpcode(Cst);
335 switch(Op) {
336 case LLVMBitCast:
337 return LLVMConstBitCast(clone_constant(LLVMGetOperand(Cst, 0), M),
338 TypeCloner(M).Clone(Cst));
339 default:
340 fprintf(stderr, "%d is not a supported opcode\n", Op);
341 exit(-1);
Amaury Sechet40bbe512016-02-17 23:55:59 +0000342 }
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000343}
344
Amaury Secheta82042e2016-02-09 22:36:41 +0000345struct FunCloner {
346 LLVMValueRef Fun;
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000347 LLVMModuleRef M;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000348
Amaury Secheta82042e2016-02-09 22:36:41 +0000349 ValueMap VMap;
350 BasicBlockMap BBMap;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000351
Amaury Sechetaad93532016-02-10 00:38:50 +0000352 FunCloner(LLVMValueRef Src, LLVMValueRef Dst): Fun(Dst),
353 M(LLVMGetGlobalParent(Fun)), VMap(clone_params(Src, Dst)) {}
354
355 LLVMTypeRef CloneType(LLVMTypeRef Src) {
356 return TypeCloner(M).Clone(Src);
357 }
358
359 LLVMTypeRef CloneType(LLVMValueRef Src) {
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000360 return TypeCloner(M).Clone(Src);
Amaury Sechetaad93532016-02-10 00:38:50 +0000361 }
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000362
Amaury Secheta82042e2016-02-09 22:36:41 +0000363 // Try to clone everything in the llvm::Value hierarchy.
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000364 LLVMValueRef CloneValue(LLVMValueRef Src) {
Amaury Secheta82042e2016-02-09 22:36:41 +0000365 // First, the value may be constant.
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000366 if (LLVMIsAConstant(Src))
367 return clone_constant(Src, M);
Amaury Secheta82042e2016-02-09 22:36:41 +0000368
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000369 // Function argument should always be in the map already.
Amaury Sechet2f432082016-02-11 21:37:54 +0000370 auto i = VMap.find(Src);
371 if (i != VMap.end())
372 return i->second;
Amaury Secheta82042e2016-02-09 22:36:41 +0000373
Amaury Sechet2f432082016-02-11 21:37:54 +0000374 if (!LLVMIsAInstruction(Src))
375 report_fatal_error("Expected an instruction");
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000376
Amaury Sechet2f432082016-02-11 21:37:54 +0000377 auto Ctx = LLVMGetModuleContext(M);
378 auto Builder = LLVMCreateBuilderInContext(Ctx);
379 auto BB = DeclareBB(LLVMGetInstructionParent(Src));
380 LLVMPositionBuilderAtEnd(Builder, BB);
381 auto Dst = CloneInstruction(Src, Builder);
382 LLVMDisposeBuilder(Builder);
383 return Dst;
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000384 }
385
Amaury Secheta65a2372016-06-15 05:14:29 +0000386 void CloneAttrs(LLVMValueRef Src, LLVMValueRef Dst) {
387 auto Ctx = LLVMGetModuleContext(M);
388 int ArgCount = LLVMGetNumArgOperands(Src);
389 for (int i = LLVMAttributeReturnIndex; i <= ArgCount; i++) {
390 for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) {
391 if (auto SrcA = LLVMGetCallSiteEnumAttribute(Src, i, k)) {
392 auto Val = LLVMGetEnumAttributeValue(SrcA);
393 auto A = LLVMCreateEnumAttribute(Ctx, k, Val);
394 LLVMAddCallSiteAttribute(Dst, i, A);
395 }
396 }
397 }
398 }
399
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000400 LLVMValueRef CloneInstruction(LLVMValueRef Src, LLVMBuilderRef Builder) {
Amaury Sechet49491312016-04-07 05:56:20 +0000401 check_value_kind(Src, LLVMInstructionValueKind);
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000402 if (!LLVMIsAInstruction(Src))
403 report_fatal_error("Expected an instruction");
404
Amaury Sechet49491312016-04-07 05:56:20 +0000405 const char *Name = LLVMGetValueName(Src);
Peter Zotov3e4561c2016-04-06 22:21:29 +0000406
Amaury Secheta82042e2016-02-09 22:36:41 +0000407 // Check if this is something we already computed.
408 {
409 auto i = VMap.find(Src);
Amaury Sechet2f432082016-02-11 21:37:54 +0000410 if (i != VMap.end()) {
411 // If we have a hit, it means we already generated the instruction
412 // as a dependancy to somethign else. We need to make sure
413 // it is ordered properly.
414 auto I = i->second;
415 LLVMInstructionRemoveFromParent(I);
416 LLVMInsertIntoBuilderWithName(Builder, I, Name);
417 return I;
418 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000419 }
420
421 // We tried everything, it must be an instruction
422 // that hasn't been generated already.
423 LLVMValueRef Dst = nullptr;
424
425 LLVMOpcode Op = LLVMGetInstructionOpcode(Src);
426 switch(Op) {
427 case LLVMRet: {
428 int OpCount = LLVMGetNumOperands(Src);
429 if (OpCount == 0)
430 Dst = LLVMBuildRetVoid(Builder);
431 else
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000432 Dst = LLVMBuildRet(Builder, CloneValue(LLVMGetOperand(Src, 0)));
Amaury Secheta82042e2016-02-09 22:36:41 +0000433 break;
434 }
435 case LLVMBr: {
Amaury Sechete7e62172016-02-09 23:15:02 +0000436 if (!LLVMIsConditional(Src)) {
437 LLVMValueRef SrcOp = LLVMGetOperand(Src, 0);
438 LLVMBasicBlockRef SrcBB = LLVMValueAsBasicBlock(SrcOp);
439 Dst = LLVMBuildBr(Builder, DeclareBB(SrcBB));
440 break;
441 }
442
443 LLVMValueRef Cond = LLVMGetCondition(Src);
444 LLVMValueRef Else = LLVMGetOperand(Src, 1);
445 LLVMBasicBlockRef ElseBB = DeclareBB(LLVMValueAsBasicBlock(Else));
446 LLVMValueRef Then = LLVMGetOperand(Src, 2);
447 LLVMBasicBlockRef ThenBB = DeclareBB(LLVMValueAsBasicBlock(Then));
Rafael Espindola92f35782018-02-21 19:55:11 +0000448 Dst = LLVMBuildCondBr(Builder, CloneValue(Cond), ThenBB, ElseBB);
Amaury Secheta82042e2016-02-09 22:36:41 +0000449 break;
450 }
451 case LLVMSwitch:
452 case LLVMIndirectBr:
Amaury Secheta82042e2016-02-09 22:36:41 +0000453 break;
Amaury Sechete39e8532016-02-18 20:38:32 +0000454 case LLVMInvoke: {
455 SmallVector<LLVMValueRef, 8> Args;
456 int ArgCount = LLVMGetNumArgOperands(Src);
457 for (int i = 0; i < ArgCount; i++)
458 Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
459 LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
460 LLVMBasicBlockRef Then = DeclareBB(LLVMGetNormalDest(Src));
461 LLVMBasicBlockRef Unwind = DeclareBB(LLVMGetUnwindDest(Src));
462 Dst = LLVMBuildInvoke(Builder, Fn, Args.data(), ArgCount,
463 Then, Unwind, Name);
Amaury Secheta65a2372016-06-15 05:14:29 +0000464 CloneAttrs(Src, Dst);
Amaury Sechete39e8532016-02-18 20:38:32 +0000465 break;
466 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000467 case LLVMUnreachable:
468 Dst = LLVMBuildUnreachable(Builder);
469 break;
470 case LLVMAdd: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000471 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
472 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000473 Dst = LLVMBuildAdd(Builder, LHS, RHS, Name);
474 break;
475 }
476 case LLVMSub: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000477 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
478 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000479 Dst = LLVMBuildSub(Builder, LHS, RHS, Name);
480 break;
481 }
482 case LLVMMul: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000483 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
484 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000485 Dst = LLVMBuildMul(Builder, LHS, RHS, Name);
486 break;
487 }
488 case LLVMUDiv: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000489 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
490 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000491 Dst = LLVMBuildUDiv(Builder, LHS, RHS, Name);
492 break;
493 }
494 case LLVMSDiv: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000495 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
496 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000497 Dst = LLVMBuildSDiv(Builder, LHS, RHS, Name);
498 break;
499 }
500 case LLVMURem: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000501 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
502 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000503 Dst = LLVMBuildURem(Builder, LHS, RHS, Name);
504 break;
505 }
506 case LLVMSRem: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000507 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
508 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000509 Dst = LLVMBuildSRem(Builder, LHS, RHS, Name);
510 break;
511 }
512 case LLVMShl: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000513 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
514 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000515 Dst = LLVMBuildShl(Builder, LHS, RHS, Name);
516 break;
517 }
518 case LLVMLShr: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000519 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
520 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000521 Dst = LLVMBuildLShr(Builder, LHS, RHS, Name);
522 break;
523 }
524 case LLVMAShr: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000525 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
526 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000527 Dst = LLVMBuildAShr(Builder, LHS, RHS, Name);
528 break;
529 }
530 case LLVMAnd: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000531 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
532 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000533 Dst = LLVMBuildAnd(Builder, LHS, RHS, Name);
534 break;
535 }
536 case LLVMOr: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000537 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
538 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000539 Dst = LLVMBuildOr(Builder, LHS, RHS, Name);
540 break;
541 }
542 case LLVMXor: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000543 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
544 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000545 Dst = LLVMBuildXor(Builder, LHS, RHS, Name);
546 break;
547 }
548 case LLVMAlloca: {
Amaury Sechetaad93532016-02-10 00:38:50 +0000549 LLVMTypeRef Ty = CloneType(LLVMGetAllocatedType(Src));
Amaury Secheta82042e2016-02-09 22:36:41 +0000550 Dst = LLVMBuildAlloca(Builder, Ty, Name);
551 break;
552 }
Amaury Sechet053ac452016-02-17 22:51:03 +0000553 case LLVMLoad: {
554 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
555 Dst = LLVMBuildLoad(Builder, Ptr, Name);
556 LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
557 break;
558 }
559 case LLVMStore: {
560 LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 0));
561 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 1));
562 Dst = LLVMBuildStore(Builder, Val, Ptr);
563 LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
564 break;
565 }
566 case LLVMGetElementPtr: {
567 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
568 SmallVector<LLVMValueRef, 8> Idx;
569 int NumIdx = LLVMGetNumIndices(Src);
570 for (int i = 1; i <= NumIdx; i++)
571 Idx.push_back(CloneValue(LLVMGetOperand(Src, i)));
572 if (LLVMIsInBounds(Src))
573 Dst = LLVMBuildInBoundsGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
574 else
575 Dst = LLVMBuildGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
576 break;
577 }
Mehdi Amini43165d92016-03-19 21:28:28 +0000578 case LLVMAtomicCmpXchg: {
579 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
580 LLVMValueRef Cmp = CloneValue(LLVMGetOperand(Src, 1));
581 LLVMValueRef New = CloneValue(LLVMGetOperand(Src, 2));
582 LLVMAtomicOrdering Succ = LLVMGetCmpXchgSuccessOrdering(Src);
583 LLVMAtomicOrdering Fail = LLVMGetCmpXchgFailureOrdering(Src);
584 LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src);
585
586 Dst = LLVMBuildAtomicCmpXchg(Builder, Ptr, Cmp, New, Succ, Fail,
587 SingleThread);
588 } break;
Amaury Sechet40bbe512016-02-17 23:55:59 +0000589 case LLVMBitCast: {
590 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 0));
591 Dst = LLVMBuildBitCast(Builder, V, CloneType(Src), Name);
592 break;
593 }
Amaury Sechete7e62172016-02-09 23:15:02 +0000594 case LLVMICmp: {
595 LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src);
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000596 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
597 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Sechete7e62172016-02-09 23:15:02 +0000598 Dst = LLVMBuildICmp(Builder, Pred, LHS, RHS, Name);
599 break;
600 }
Amaury Sechet2f432082016-02-11 21:37:54 +0000601 case LLVMPHI: {
Simon Pilgrim68168d12017-03-30 12:59:53 +0000602 // We need to aggressively set things here because of loops.
Amaury Sechet2f432082016-02-11 21:37:54 +0000603 VMap[Src] = Dst = LLVMBuildPhi(Builder, CloneType(Src), Name);
604
605 SmallVector<LLVMValueRef, 8> Values;
606 SmallVector<LLVMBasicBlockRef, 8> Blocks;
607
608 unsigned IncomingCount = LLVMCountIncoming(Src);
609 for (unsigned i = 0; i < IncomingCount; ++i) {
610 Blocks.push_back(DeclareBB(LLVMGetIncomingBlock(Src, i)));
611 Values.push_back(CloneValue(LLVMGetIncomingValue(Src, i)));
612 }
613
614 LLVMAddIncoming(Dst, Values.data(), Blocks.data(), IncomingCount);
615 return Dst;
616 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000617 case LLVMCall: {
Amaury Secheta82042e2016-02-09 22:36:41 +0000618 SmallVector<LLVMValueRef, 8> Args;
Amaury Sechet5c7b3af2016-02-10 00:09:37 +0000619 int ArgCount = LLVMGetNumArgOperands(Src);
Amaury Secheta82042e2016-02-09 22:36:41 +0000620 for (int i = 0; i < ArgCount; i++)
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000621 Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
Amaury Sechet5c7b3af2016-02-10 00:09:37 +0000622 LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
Amaury Secheta82042e2016-02-09 22:36:41 +0000623 Dst = LLVMBuildCall(Builder, Fn, Args.data(), ArgCount, Name);
Amaury Sechete39e8532016-02-18 20:38:32 +0000624 LLVMSetTailCall(Dst, LLVMIsTailCall(Src));
Amaury Secheta65a2372016-06-15 05:14:29 +0000625 CloneAttrs(Src, Dst);
Amaury Sechete39e8532016-02-18 20:38:32 +0000626 break;
627 }
628 case LLVMResume: {
629 Dst = LLVMBuildResume(Builder, CloneValue(LLVMGetOperand(Src, 0)));
630 break;
631 }
632 case LLVMLandingPad: {
633 // The landing pad API is a bit screwed up for historical reasons.
634 Dst = LLVMBuildLandingPad(Builder, CloneType(Src), nullptr, 0, Name);
635 unsigned NumClauses = LLVMGetNumClauses(Src);
636 for (unsigned i = 0; i < NumClauses; ++i)
637 LLVMAddClause(Dst, CloneValue(LLVMGetClause(Src, i)));
638 LLVMSetCleanup(Dst, LLVMIsCleanup(Src));
Amaury Secheta82042e2016-02-09 22:36:41 +0000639 break;
640 }
Robert Widmann478fce92018-03-30 17:49:53 +0000641 case LLVMCleanupRet: {
642 LLVMValueRef CatchPad = CloneValue(LLVMGetOperand(Src, 0));
643 LLVMBasicBlockRef Unwind = nullptr;
644 if (LLVMBasicBlockRef UDest = LLVMGetUnwindDest(Src))
645 Unwind = DeclareBB(UDest);
646 Dst = LLVMBuildCleanupRet(Builder, CatchPad, Unwind);
647 break;
648 }
649 case LLVMCatchRet: {
650 LLVMValueRef CatchPad = CloneValue(LLVMGetOperand(Src, 0));
651 LLVMBasicBlockRef SuccBB = DeclareBB(LLVMGetSuccessor(Src, 0));
652 Dst = LLVMBuildCatchRet(Builder, CatchPad, SuccBB);
653 break;
654 }
655 case LLVMCatchPad: {
656 LLVMValueRef ParentPad = CloneValue(LLVMGetParentCatchSwitch(Src));
657 SmallVector<LLVMValueRef, 8> Args;
658 int ArgCount = LLVMGetNumArgOperands(Src);
659 for (int i = 0; i < ArgCount; i++)
660 Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
661 Dst = LLVMBuildCatchPad(Builder, ParentPad,
662 Args.data(), ArgCount, Name);
663 break;
664 }
665 case LLVMCleanupPad: {
666 LLVMValueRef ParentPad = CloneValue(LLVMGetOperand(Src, 0));
667 SmallVector<LLVMValueRef, 8> Args;
668 int ArgCount = LLVMGetNumArgOperands(Src);
669 for (int i = 0; i < ArgCount; i++)
670 Args.push_back(CloneValue(LLVMGetArgOperand(Src, i)));
671 Dst = LLVMBuildCleanupPad(Builder, ParentPad,
672 Args.data(), ArgCount, Name);
673 break;
674 }
675 case LLVMCatchSwitch: {
676 LLVMValueRef ParentPad = CloneValue(LLVMGetOperand(Src, 0));
677 LLVMBasicBlockRef UnwindBB = nullptr;
678 if (LLVMBasicBlockRef UDest = LLVMGetUnwindDest(Src)) {
679 UnwindBB = DeclareBB(UDest);
680 }
681 unsigned NumHandlers = LLVMGetNumHandlers(Src);
682 Dst = LLVMBuildCatchSwitch(Builder, ParentPad, UnwindBB, NumHandlers, Name);
683 if (NumHandlers > 0) {
684 LLVMBasicBlockRef *Handlers = static_cast<LLVMBasicBlockRef*>(
685 safe_malloc(NumHandlers * sizeof(LLVMBasicBlockRef)));
686 LLVMGetHandlers(Src, Handlers);
687 for (unsigned i = 0; i < NumHandlers; i++)
688 LLVMAddHandler(Dst, DeclareBB(Handlers[i]));
689 free(Handlers);
690 }
691 break;
692 }
Amaury Sechetaad93532016-02-10 00:38:50 +0000693 case LLVMExtractValue: {
694 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
695 if (LLVMGetNumIndices(Src) != 1)
696 report_fatal_error("Expected only one indice");
697 auto I = LLVMGetIndices(Src)[0];
698 Dst = LLVMBuildExtractValue(Builder, Agg, I, Name);
699 break;
700 }
701 case LLVMInsertValue: {
702 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
703 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 1));
704 if (LLVMGetNumIndices(Src) != 1)
705 report_fatal_error("Expected only one indice");
706 auto I = LLVMGetIndices(Src)[0];
707 Dst = LLVMBuildInsertValue(Builder, Agg, V, I, Name);
708 break;
709 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000710 default:
711 break;
712 }
713
714 if (Dst == nullptr) {
715 fprintf(stderr, "%d is not a supported opcode\n", Op);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000716 exit(-1);
717 }
718
Amaury Sechet49491312016-04-07 05:56:20 +0000719 check_value_kind(Dst, LLVMInstructionValueKind);
Amaury Secheta82042e2016-02-09 22:36:41 +0000720 return VMap[Src] = Dst;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000721 }
722
Amaury Secheta82042e2016-02-09 22:36:41 +0000723 LLVMBasicBlockRef DeclareBB(LLVMBasicBlockRef Src) {
724 // Check if this is something we already computed.
725 {
726 auto i = BBMap.find(Src);
727 if (i != BBMap.end()) {
728 return i->second;
729 }
730 }
731
Amaury Secheta82042e2016-02-09 22:36:41 +0000732 LLVMValueRef V = LLVMBasicBlockAsValue(Src);
733 if (!LLVMValueIsBasicBlock(V) || LLVMValueAsBasicBlock(V) != Src)
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000734 report_fatal_error("Basic block is not a basic block");
Amaury Secheta82042e2016-02-09 22:36:41 +0000735
Amaury Sechetd01c8612016-02-14 10:06:34 +0000736 const char *Name = LLVMGetBasicBlockName(Src);
Amaury Secheta82042e2016-02-09 22:36:41 +0000737 const char *VName = LLVMGetValueName(V);
738 if (Name != VName)
739 report_fatal_error("Basic block name mismatch");
740
741 LLVMBasicBlockRef BB = LLVMAppendBasicBlock(Fun, Name);
742 return BBMap[Src] = BB;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000743 }
744
Amaury Secheta82042e2016-02-09 22:36:41 +0000745 LLVMBasicBlockRef CloneBB(LLVMBasicBlockRef Src) {
746 LLVMBasicBlockRef BB = DeclareBB(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000747
Amaury Secheta82042e2016-02-09 22:36:41 +0000748 // Make sure ordering is correct.
749 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Src);
750 if (Prev)
751 LLVMMoveBasicBlockAfter(BB, DeclareBB(Prev));
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000752
Amaury Secheta82042e2016-02-09 22:36:41 +0000753 LLVMValueRef First = LLVMGetFirstInstruction(Src);
754 LLVMValueRef Last = LLVMGetLastInstruction(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000755
Amaury Secheta82042e2016-02-09 22:36:41 +0000756 if (First == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000757 if (Last != nullptr)
758 report_fatal_error("Has no first instruction, but last one");
Amaury Secheta82042e2016-02-09 22:36:41 +0000759 return BB;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000760 }
761
Amaury Sechetaad93532016-02-10 00:38:50 +0000762 auto Ctx = LLVMGetModuleContext(M);
Amaury Secheta82042e2016-02-09 22:36:41 +0000763 LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx);
764 LLVMPositionBuilderAtEnd(Builder, BB);
765
766 LLVMValueRef Cur = First;
767 LLVMValueRef Next = nullptr;
768 while(true) {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000769 CloneInstruction(Cur, Builder);
Amaury Secheta82042e2016-02-09 22:36:41 +0000770 Next = LLVMGetNextInstruction(Cur);
771 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000772 if (Cur != Last)
773 report_fatal_error("Final instruction does not match Last");
Amaury Secheta82042e2016-02-09 22:36:41 +0000774 break;
775 }
776
777 LLVMValueRef Prev = LLVMGetPreviousInstruction(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000778 if (Prev != Cur)
779 report_fatal_error("Next.Previous instruction is not Current");
Amaury Secheta82042e2016-02-09 22:36:41 +0000780
781 Cur = Next;
782 }
783
784 LLVMDisposeBuilder(Builder);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000785 return BB;
786 }
787
Amaury Secheta82042e2016-02-09 22:36:41 +0000788 void CloneBBs(LLVMValueRef Src) {
789 unsigned Count = LLVMCountBasicBlocks(Src);
790 if (Count == 0)
791 return;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000792
Amaury Secheta82042e2016-02-09 22:36:41 +0000793 LLVMBasicBlockRef First = LLVMGetFirstBasicBlock(Src);
794 LLVMBasicBlockRef Last = LLVMGetLastBasicBlock(Src);
795
796 LLVMBasicBlockRef Cur = First;
797 LLVMBasicBlockRef Next = nullptr;
798 while(true) {
799 CloneBB(Cur);
800 Count--;
801 Next = LLVMGetNextBasicBlock(Cur);
802 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000803 if (Cur != Last)
804 report_fatal_error("Final basic block does not match Last");
Amaury Secheta82042e2016-02-09 22:36:41 +0000805 break;
806 }
807
808 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000809 if (Prev != Cur)
810 report_fatal_error("Next.Previous basic bloc is not Current");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000811
Amaury Secheta82042e2016-02-09 22:36:41 +0000812 Cur = Next;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000813 }
814
Amaury Sechetd01c8612016-02-14 10:06:34 +0000815 if (Count != 0)
816 report_fatal_error("Basic block count does not match iterration");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000817 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000818};
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000819
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000820static void declare_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000821 LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
822 LLVMValueRef End = LLVMGetLastGlobal(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000823
824 LLVMValueRef Cur = Begin;
825 LLVMValueRef Next = nullptr;
Amaury Sechet58946a92016-02-17 22:30:05 +0000826 if (!Begin) {
827 if (End != nullptr)
Hiroshi Inouee9dea6e2017-07-13 06:48:39 +0000828 report_fatal_error("Range has an end but no beginning");
Amaury Sechet58946a92016-02-17 22:30:05 +0000829 goto FunDecl;
830 }
831
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000832 while (true) {
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000833 const char *Name = LLVMGetValueName(Cur);
834 if (LLVMGetNamedGlobal(M, Name))
835 report_fatal_error("GlobalVariable already cloned");
836 LLVMAddGlobal(M, LLVMGetElementType(TypeCloner(M).Clone(Cur)), Name);
837
838 Next = LLVMGetNextGlobal(Cur);
839 if (Next == nullptr) {
840 if (Cur != End)
841 report_fatal_error("");
842 break;
843 }
844
845 LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
846 if (Prev != Cur)
847 report_fatal_error("Next.Previous global is not Current");
848
849 Cur = Next;
850 }
851
Amaury Sechet58946a92016-02-17 22:30:05 +0000852FunDecl:
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000853 Begin = LLVMGetFirstFunction(Src);
854 End = LLVMGetLastFunction(Src);
855 if (!Begin) {
856 if (End != nullptr)
Hiroshi Inouee9dea6e2017-07-13 06:48:39 +0000857 report_fatal_error("Range has an end but no beginning");
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000858 return;
859 }
860
Amaury Sechet5db224e2016-06-12 06:17:24 +0000861 auto Ctx = LLVMGetModuleContext(M);
862
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000863 Cur = Begin;
864 Next = nullptr;
865 while (true) {
866 const char *Name = LLVMGetValueName(Cur);
867 if (LLVMGetNamedFunction(M, Name))
868 report_fatal_error("Function already cloned");
Amaury Sechet5db224e2016-06-12 06:17:24 +0000869 auto Ty = LLVMGetElementType(TypeCloner(M).Clone(Cur));
870 auto F = LLVMAddFunction(M, Name, Ty);
871
872 // Copy attributes
873 for (int i = LLVMAttributeFunctionIndex, c = LLVMCountParams(F);
874 i <= c; ++i) {
875 for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) {
876 if (auto SrcA = LLVMGetEnumAttributeAtIndex(Cur, i, k)) {
877 auto Val = LLVMGetEnumAttributeValue(SrcA);
878 auto DstA = LLVMCreateEnumAttribute(Ctx, k, Val);
879 LLVMAddAttributeAtIndex(F, i, DstA);
880 }
881 }
882 }
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000883
Amaury Sechetf64e8342016-02-16 07:08:49 +0000884 Next = LLVMGetNextFunction(Cur);
885 if (Next == nullptr) {
886 if (Cur != End)
887 report_fatal_error("Last function does not match End");
888 break;
889 }
890
891 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
892 if (Prev != Cur)
893 report_fatal_error("Next.Previous function is not Current");
894
895 Cur = Next;
896 }
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000897}
Amaury Sechetf64e8342016-02-16 07:08:49 +0000898
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000899static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000900 LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
901 LLVMValueRef End = LLVMGetLastGlobal(Src);
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000902
903 LLVMValueRef Cur = Begin;
904 LLVMValueRef Next = nullptr;
Amaury Sechet58946a92016-02-17 22:30:05 +0000905 if (!Begin) {
906 if (End != nullptr)
Hiroshi Inouee9dea6e2017-07-13 06:48:39 +0000907 report_fatal_error("Range has an end but no beginning");
Amaury Sechet58946a92016-02-17 22:30:05 +0000908 goto FunClone;
909 }
910
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000911 while (true) {
912 const char *Name = LLVMGetValueName(Cur);
913 LLVMValueRef G = LLVMGetNamedGlobal(M, Name);
914 if (!G)
915 report_fatal_error("GlobalVariable must have been declared already");
916
917 if (auto I = LLVMGetInitializer(Cur))
918 LLVMSetInitializer(G, clone_constant(I, M));
919
920 LLVMSetGlobalConstant(G, LLVMIsGlobalConstant(Cur));
921 LLVMSetThreadLocal(G, LLVMIsThreadLocal(Cur));
922 LLVMSetExternallyInitialized(G, LLVMIsExternallyInitialized(Cur));
923 LLVMSetLinkage(G, LLVMGetLinkage(Cur));
924 LLVMSetSection(G, LLVMGetSection(Cur));
925 LLVMSetVisibility(G, LLVMGetVisibility(Cur));
Robert Widmann4bb481b2018-03-14 06:45:51 +0000926 LLVMSetUnnamedAddress(G, LLVMGetUnnamedAddress(Cur));
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000927 LLVMSetAlignment(G, LLVMGetAlignment(Cur));
928
929 Next = LLVMGetNextGlobal(Cur);
930 if (Next == nullptr) {
931 if (Cur != End)
932 report_fatal_error("");
933 break;
934 }
935
936 LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
937 if (Prev != Cur)
938 report_fatal_error("Next.Previous global is not Current");
939
940 Cur = Next;
941 }
942
Amaury Sechet58946a92016-02-17 22:30:05 +0000943FunClone:
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000944 Begin = LLVMGetFirstFunction(Src);
945 End = LLVMGetLastFunction(Src);
946 if (!Begin) {
947 if (End != nullptr)
Hiroshi Inouee9dea6e2017-07-13 06:48:39 +0000948 report_fatal_error("Range has an end but no beginning");
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000949 return;
950 }
951
Amaury Sechetf64e8342016-02-16 07:08:49 +0000952 Cur = Begin;
953 Next = nullptr;
954 while (true) {
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000955 const char *Name = LLVMGetValueName(Cur);
956 LLVMValueRef Fun = LLVMGetNamedFunction(M, Name);
957 if (!Fun)
958 report_fatal_error("Function must have been declared already");
Amaury Sechete39e8532016-02-18 20:38:32 +0000959
960 if (LLVMHasPersonalityFn(Cur)) {
961 const char *FName = LLVMGetValueName(LLVMGetPersonalityFn(Cur));
962 LLVMValueRef P = LLVMGetNamedFunction(M, FName);
963 if (!P)
964 report_fatal_error("Could not find personality function");
965 LLVMSetPersonalityFn(Fun, P);
966 }
967
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000968 FunCloner FC(Cur, Fun);
969 FC.CloneBBs(Cur);
970
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000971 Next = LLVMGetNextFunction(Cur);
972 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000973 if (Cur != End)
974 report_fatal_error("Last function does not match End");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000975 break;
976 }
977
978 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000979 if (Prev != Cur)
980 report_fatal_error("Next.Previous function is not Current");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000981
982 Cur = Next;
983 }
984}
985
Benjamin Kramer9a3bd232016-02-05 13:31:14 +0000986int llvm_echo(void) {
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000987 LLVMEnablePrettyStackTrace();
988
Benjamin Kramer9a3bd232016-02-05 13:31:14 +0000989 LLVMModuleRef Src = llvm_load_module(false, true);
Robert Widmann490a5802018-01-30 21:34:29 +0000990 size_t SourceFileLen;
991 const char *SourceFileName = LLVMGetSourceFileName(Src, &SourceFileLen);
992 size_t ModuleIdentLen;
993 const char *ModuleName = LLVMGetModuleIdentifier(Src, &ModuleIdentLen);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000994 LLVMContextRef Ctx = LLVMContextCreate();
Peter Zotov0a2fa0a2016-04-05 13:56:59 +0000995 LLVMModuleRef M = LLVMModuleCreateWithNameInContext(ModuleName, Ctx);
996
Robert Widmann490a5802018-01-30 21:34:29 +0000997 LLVMSetSourceFileName(M, SourceFileName, SourceFileLen);
998 LLVMSetModuleIdentifier(M, ModuleName, ModuleIdentLen);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000999
Amaury Sechet55909672016-02-16 05:11:24 +00001000 LLVMSetTarget(M, LLVMGetTarget(Src));
1001 LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src));
1002 if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src)))
1003 report_fatal_error("Inconsistent DataLayout string representation");
1004
Robert Widmannf108d572018-04-06 02:31:29 +00001005 size_t ModuleInlineAsmLen;
1006 const char *ModuleAsm = LLVMGetModuleInlineAsm(Src, &ModuleInlineAsmLen);
1007 LLVMSetModuleInlineAsm2(M, ModuleAsm, ModuleInlineAsmLen);
1008
Amaury Sechete8ba2bf2016-02-17 22:13:33 +00001009 declare_symbols(Src, M);
1010 clone_symbols(Src, M);
Amaury Sechetc679dbd2016-02-14 09:30:42 +00001011 char *Str = LLVMPrintModuleToString(M);
Amaury Sechete8ea7d82016-02-04 23:26:19 +00001012 fputs(Str, stdout);
1013
1014 LLVMDisposeMessage(Str);
Rafael Espindola92f35782018-02-21 19:55:11 +00001015 LLVMDisposeModule(Src);
Amaury Sechetc679dbd2016-02-14 09:30:42 +00001016 LLVMDisposeModule(M);
Amaury Sechete8ea7d82016-02-04 23:26:19 +00001017 LLVMContextDispose(Ctx);
1018
1019 return 0;
1020}