blob: 9dd9b105407250ddd561303f16991d1bf3850703 [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//
10// This file implements the --echo commands in llvm-c-test.
11//
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
23#include <stdio.h>
24#include <stdlib.h>
25
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) {
93 Params = (LLVMTypeRef*) malloc(ParamCount * sizeof(LLVMTypeRef));
94 LLVMGetParamTypes(Src, Params);
95 for (unsigned i = 0; i < ParamCount; i++)
96 Params[i] = Clone(Params[i]);
97 }
98
99 LLVMTypeRef FunTy = LLVMFunctionType(Clone(LLVMGetReturnType(Src)),
100 Params, ParamCount,
101 LLVMIsFunctionVarArg(Src));
102 if (ParamCount > 0)
103 free(Params);
104 return FunTy;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000105 }
Amaury Sechetaad93532016-02-10 00:38:50 +0000106 case LLVMStructTypeKind: {
107 LLVMTypeRef S = nullptr;
108 const char *Name = LLVMGetStructName(Src);
109 if (Name) {
110 S = LLVMGetTypeByName(M, Name);
111 if (S)
112 return S;
113 S = LLVMStructCreateNamed(Ctx, Name);
114 if (LLVMIsOpaqueStruct(Src))
115 return S;
116 }
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000117
Amaury Sechetaad93532016-02-10 00:38:50 +0000118 unsigned EltCount = LLVMCountStructElementTypes(Src);
119 SmallVector<LLVMTypeRef, 8> Elts;
120 for (unsigned i = 0; i < EltCount; i++)
121 Elts.push_back(Clone(LLVMStructGetTypeAtIndex(Src, i)));
122 if (Name)
123 LLVMStructSetBody(S, Elts.data(), EltCount, LLVMIsPackedStruct(Src));
124 else
125 S = LLVMStructTypeInContext(Ctx, Elts.data(), EltCount,
126 LLVMIsPackedStruct(Src));
127 return S;
128 }
129 case LLVMArrayTypeKind:
130 return LLVMArrayType(
131 Clone(LLVMGetElementType(Src)),
132 LLVMGetArrayLength(Src)
133 );
134 case LLVMPointerTypeKind:
135 return LLVMPointerType(
136 Clone(LLVMGetElementType(Src)),
137 LLVMGetPointerAddressSpace(Src)
138 );
139 case LLVMVectorTypeKind:
140 return LLVMVectorType(
141 Clone(LLVMGetElementType(Src)),
142 LLVMGetVectorSize(Src)
143 );
144 case LLVMMetadataTypeKind:
145 break;
146 case LLVMX86_MMXTypeKind:
147 return LLVMX86MMXTypeInContext(Ctx);
148 default:
149 break;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000150 }
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000151
Amaury Sechetaad93532016-02-10 00:38:50 +0000152 fprintf(stderr, "%d is not a supported typekind\n", Kind);
153 exit(-1);
154 }
155};
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000156
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000157static ValueMap clone_params(LLVMValueRef Src, LLVMValueRef Dst) {
158 unsigned Count = LLVMCountParams(Src);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000159 if (Count != LLVMCountParams(Dst))
160 report_fatal_error("Parameter count mismatch");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000161
162 ValueMap VMap;
163 if (Count == 0)
164 return VMap;
165
166 LLVMValueRef SrcFirst = LLVMGetFirstParam(Src);
167 LLVMValueRef DstFirst = LLVMGetFirstParam(Dst);
168 LLVMValueRef SrcLast = LLVMGetLastParam(Src);
169 LLVMValueRef DstLast = LLVMGetLastParam(Dst);
170
171 LLVMValueRef SrcCur = SrcFirst;
172 LLVMValueRef DstCur = DstFirst;
173 LLVMValueRef SrcNext = nullptr;
174 LLVMValueRef DstNext = nullptr;
175 while (true) {
176 const char *Name = LLVMGetValueName(SrcCur);
177 LLVMSetValueName(DstCur, Name);
178
179 VMap[SrcCur] = DstCur;
180
181 Count--;
182 SrcNext = LLVMGetNextParam(SrcCur);
183 DstNext = LLVMGetNextParam(DstCur);
184 if (SrcNext == nullptr && DstNext == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000185 if (SrcCur != SrcLast)
186 report_fatal_error("SrcLast param does not match End");
187 if (DstCur != DstLast)
188 report_fatal_error("DstLast param does not match End");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000189 break;
190 }
191
Amaury Sechetd01c8612016-02-14 10:06:34 +0000192 if (SrcNext == nullptr)
193 report_fatal_error("SrcNext was unexpectedly null");
194 if (DstNext == nullptr)
195 report_fatal_error("DstNext was unexpectedly null");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000196
197 LLVMValueRef SrcPrev = LLVMGetPreviousParam(SrcNext);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000198 if (SrcPrev != SrcCur)
199 report_fatal_error("SrcNext.Previous param is not Current");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000200
201 LLVMValueRef DstPrev = LLVMGetPreviousParam(DstNext);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000202 if (DstPrev != DstCur)
203 report_fatal_error("DstNext.Previous param is not Current");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000204
205 SrcCur = SrcNext;
206 DstCur = DstNext;
207 }
208
Amaury Sechetd01c8612016-02-14 10:06:34 +0000209 if (Count != 0)
210 report_fatal_error("Parameter count does not match iteration");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000211
212 return VMap;
213}
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000214
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000215LLVMValueRef clone_constant(LLVMValueRef Cst, LLVMModuleRef M) {
216 if (!LLVMIsAConstant(Cst))
217 report_fatal_error("Expected a constant");
218
219 // Maybe it is a symbol
220 if (LLVMIsAGlobalValue(Cst)) {
221 const char *Name = LLVMGetValueName(Cst);
222
223 // Try function
224 LLVMValueRef Dst = LLVMGetNamedFunction(M, Name);
225 if (Dst != nullptr)
226 return Dst;
227
228 // Try global variable
229 Dst = LLVMGetNamedGlobal(M, Name);
230 if (Dst != nullptr)
231 return Dst;
232
233 fprintf(stderr, "Could not find @%s\n", Name);
234 exit(-1);
235 }
236
237 // Try literal
238 if (LLVMIsAConstantInt(Cst))
239 return LLVMConstInt(TypeCloner(M).Clone(Cst),
240 LLVMConstIntGetZExtValue(Cst), false);
241
242 // Try undef
243 if (LLVMIsUndef(Cst))
244 return LLVMGetUndef(TypeCloner(M).Clone(Cst));
245
246 // This kind of constant is not supported.
247 report_fatal_error("Unsupported contant type");
248}
249
Amaury Secheta82042e2016-02-09 22:36:41 +0000250struct FunCloner {
251 LLVMValueRef Fun;
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000252 LLVMModuleRef M;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000253
Amaury Secheta82042e2016-02-09 22:36:41 +0000254 ValueMap VMap;
255 BasicBlockMap BBMap;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000256
Amaury Sechetaad93532016-02-10 00:38:50 +0000257 FunCloner(LLVMValueRef Src, LLVMValueRef Dst): Fun(Dst),
258 M(LLVMGetGlobalParent(Fun)), VMap(clone_params(Src, Dst)) {}
259
260 LLVMTypeRef CloneType(LLVMTypeRef Src) {
261 return TypeCloner(M).Clone(Src);
262 }
263
264 LLVMTypeRef CloneType(LLVMValueRef Src) {
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000265 return TypeCloner(M).Clone(Src);
Amaury Sechetaad93532016-02-10 00:38:50 +0000266 }
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000267
Amaury Secheta82042e2016-02-09 22:36:41 +0000268 // Try to clone everything in the llvm::Value hierarchy.
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000269 LLVMValueRef CloneValue(LLVMValueRef Src) {
Amaury Secheta82042e2016-02-09 22:36:41 +0000270 // First, the value may be constant.
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000271 if (LLVMIsAConstant(Src))
272 return clone_constant(Src, M);
Amaury Secheta82042e2016-02-09 22:36:41 +0000273
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000274 // Function argument should always be in the map already.
Amaury Sechet2f432082016-02-11 21:37:54 +0000275 auto i = VMap.find(Src);
276 if (i != VMap.end())
277 return i->second;
Amaury Secheta82042e2016-02-09 22:36:41 +0000278
Amaury Sechet2f432082016-02-11 21:37:54 +0000279 if (!LLVMIsAInstruction(Src))
280 report_fatal_error("Expected an instruction");
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000281
Amaury Sechet2f432082016-02-11 21:37:54 +0000282 auto Ctx = LLVMGetModuleContext(M);
283 auto Builder = LLVMCreateBuilderInContext(Ctx);
284 auto BB = DeclareBB(LLVMGetInstructionParent(Src));
285 LLVMPositionBuilderAtEnd(Builder, BB);
286 auto Dst = CloneInstruction(Src, Builder);
287 LLVMDisposeBuilder(Builder);
288 return Dst;
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000289 }
290
291 LLVMValueRef CloneInstruction(LLVMValueRef Src, LLVMBuilderRef Builder) {
292 const char *Name = LLVMGetValueName(Src);
293 if (!LLVMIsAInstruction(Src))
294 report_fatal_error("Expected an instruction");
295
Amaury Secheta82042e2016-02-09 22:36:41 +0000296 // Check if this is something we already computed.
297 {
298 auto i = VMap.find(Src);
Amaury Sechet2f432082016-02-11 21:37:54 +0000299 if (i != VMap.end()) {
300 // If we have a hit, it means we already generated the instruction
301 // as a dependancy to somethign else. We need to make sure
302 // it is ordered properly.
303 auto I = i->second;
304 LLVMInstructionRemoveFromParent(I);
305 LLVMInsertIntoBuilderWithName(Builder, I, Name);
306 return I;
307 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000308 }
309
310 // We tried everything, it must be an instruction
311 // that hasn't been generated already.
312 LLVMValueRef Dst = nullptr;
313
314 LLVMOpcode Op = LLVMGetInstructionOpcode(Src);
315 switch(Op) {
316 case LLVMRet: {
317 int OpCount = LLVMGetNumOperands(Src);
318 if (OpCount == 0)
319 Dst = LLVMBuildRetVoid(Builder);
320 else
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000321 Dst = LLVMBuildRet(Builder, CloneValue(LLVMGetOperand(Src, 0)));
Amaury Secheta82042e2016-02-09 22:36:41 +0000322 break;
323 }
324 case LLVMBr: {
Amaury Sechete7e62172016-02-09 23:15:02 +0000325 if (!LLVMIsConditional(Src)) {
326 LLVMValueRef SrcOp = LLVMGetOperand(Src, 0);
327 LLVMBasicBlockRef SrcBB = LLVMValueAsBasicBlock(SrcOp);
328 Dst = LLVMBuildBr(Builder, DeclareBB(SrcBB));
329 break;
330 }
331
332 LLVMValueRef Cond = LLVMGetCondition(Src);
333 LLVMValueRef Else = LLVMGetOperand(Src, 1);
334 LLVMBasicBlockRef ElseBB = DeclareBB(LLVMValueAsBasicBlock(Else));
335 LLVMValueRef Then = LLVMGetOperand(Src, 2);
336 LLVMBasicBlockRef ThenBB = DeclareBB(LLVMValueAsBasicBlock(Then));
337 Dst = LLVMBuildCondBr(Builder, Cond, ThenBB, ElseBB);
Amaury Secheta82042e2016-02-09 22:36:41 +0000338 break;
339 }
340 case LLVMSwitch:
341 case LLVMIndirectBr:
342 case LLVMInvoke:
343 break;
344 case LLVMUnreachable:
345 Dst = LLVMBuildUnreachable(Builder);
346 break;
347 case LLVMAdd: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000348 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
349 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000350 Dst = LLVMBuildAdd(Builder, LHS, RHS, Name);
351 break;
352 }
353 case LLVMSub: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000354 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
355 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000356 Dst = LLVMBuildSub(Builder, LHS, RHS, Name);
357 break;
358 }
359 case LLVMMul: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000360 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
361 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000362 Dst = LLVMBuildMul(Builder, LHS, RHS, Name);
363 break;
364 }
365 case LLVMUDiv: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000366 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
367 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000368 Dst = LLVMBuildUDiv(Builder, LHS, RHS, Name);
369 break;
370 }
371 case LLVMSDiv: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000372 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
373 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000374 Dst = LLVMBuildSDiv(Builder, LHS, RHS, Name);
375 break;
376 }
377 case LLVMURem: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000378 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
379 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000380 Dst = LLVMBuildURem(Builder, LHS, RHS, Name);
381 break;
382 }
383 case LLVMSRem: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000384 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
385 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000386 Dst = LLVMBuildSRem(Builder, LHS, RHS, Name);
387 break;
388 }
389 case LLVMShl: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000390 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
391 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000392 Dst = LLVMBuildShl(Builder, LHS, RHS, Name);
393 break;
394 }
395 case LLVMLShr: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000396 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
397 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000398 Dst = LLVMBuildLShr(Builder, LHS, RHS, Name);
399 break;
400 }
401 case LLVMAShr: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000402 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
403 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000404 Dst = LLVMBuildAShr(Builder, LHS, RHS, Name);
405 break;
406 }
407 case LLVMAnd: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000408 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
409 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000410 Dst = LLVMBuildAnd(Builder, LHS, RHS, Name);
411 break;
412 }
413 case LLVMOr: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000414 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
415 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000416 Dst = LLVMBuildOr(Builder, LHS, RHS, Name);
417 break;
418 }
419 case LLVMXor: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000420 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
421 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000422 Dst = LLVMBuildXor(Builder, LHS, RHS, Name);
423 break;
424 }
425 case LLVMAlloca: {
Amaury Sechetaad93532016-02-10 00:38:50 +0000426 LLVMTypeRef Ty = CloneType(LLVMGetAllocatedType(Src));
Amaury Secheta82042e2016-02-09 22:36:41 +0000427 Dst = LLVMBuildAlloca(Builder, Ty, Name);
428 break;
429 }
Amaury Sechete7e62172016-02-09 23:15:02 +0000430 case LLVMICmp: {
431 LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src);
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000432 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
433 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Sechete7e62172016-02-09 23:15:02 +0000434 Dst = LLVMBuildICmp(Builder, Pred, LHS, RHS, Name);
435 break;
436 }
Amaury Sechet2f432082016-02-11 21:37:54 +0000437 case LLVMPHI: {
438 // We need to agressively set things here because of loops.
439 VMap[Src] = Dst = LLVMBuildPhi(Builder, CloneType(Src), Name);
440
441 SmallVector<LLVMValueRef, 8> Values;
442 SmallVector<LLVMBasicBlockRef, 8> Blocks;
443
444 unsigned IncomingCount = LLVMCountIncoming(Src);
445 for (unsigned i = 0; i < IncomingCount; ++i) {
446 Blocks.push_back(DeclareBB(LLVMGetIncomingBlock(Src, i)));
447 Values.push_back(CloneValue(LLVMGetIncomingValue(Src, i)));
448 }
449
450 LLVMAddIncoming(Dst, Values.data(), Blocks.data(), IncomingCount);
451 return Dst;
452 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000453 case LLVMCall: {
Amaury Secheta82042e2016-02-09 22:36:41 +0000454 SmallVector<LLVMValueRef, 8> Args;
Amaury Sechet5c7b3af2016-02-10 00:09:37 +0000455 int ArgCount = LLVMGetNumArgOperands(Src);
Amaury Secheta82042e2016-02-09 22:36:41 +0000456 for (int i = 0; i < ArgCount; i++)
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000457 Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
Amaury Sechet5c7b3af2016-02-10 00:09:37 +0000458 LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
Amaury Secheta82042e2016-02-09 22:36:41 +0000459 Dst = LLVMBuildCall(Builder, Fn, Args.data(), ArgCount, Name);
460 break;
461 }
Amaury Sechetaad93532016-02-10 00:38:50 +0000462 case LLVMExtractValue: {
463 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
464 if (LLVMGetNumIndices(Src) != 1)
465 report_fatal_error("Expected only one indice");
466 auto I = LLVMGetIndices(Src)[0];
467 Dst = LLVMBuildExtractValue(Builder, Agg, I, Name);
468 break;
469 }
470 case LLVMInsertValue: {
471 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
472 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 1));
473 if (LLVMGetNumIndices(Src) != 1)
474 report_fatal_error("Expected only one indice");
475 auto I = LLVMGetIndices(Src)[0];
476 Dst = LLVMBuildInsertValue(Builder, Agg, V, I, Name);
477 break;
478 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000479 default:
480 break;
481 }
482
483 if (Dst == nullptr) {
484 fprintf(stderr, "%d is not a supported opcode\n", Op);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000485 exit(-1);
486 }
487
Amaury Secheta82042e2016-02-09 22:36:41 +0000488 return VMap[Src] = Dst;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000489 }
490
Amaury Secheta82042e2016-02-09 22:36:41 +0000491 LLVMBasicBlockRef DeclareBB(LLVMBasicBlockRef Src) {
492 // Check if this is something we already computed.
493 {
494 auto i = BBMap.find(Src);
495 if (i != BBMap.end()) {
496 return i->second;
497 }
498 }
499
Amaury Secheta82042e2016-02-09 22:36:41 +0000500 LLVMValueRef V = LLVMBasicBlockAsValue(Src);
501 if (!LLVMValueIsBasicBlock(V) || LLVMValueAsBasicBlock(V) != Src)
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000502 report_fatal_error("Basic block is not a basic block");
Amaury Secheta82042e2016-02-09 22:36:41 +0000503
Amaury Sechetd01c8612016-02-14 10:06:34 +0000504 const char *Name = LLVMGetBasicBlockName(Src);
Amaury Secheta82042e2016-02-09 22:36:41 +0000505 const char *VName = LLVMGetValueName(V);
506 if (Name != VName)
507 report_fatal_error("Basic block name mismatch");
508
509 LLVMBasicBlockRef BB = LLVMAppendBasicBlock(Fun, Name);
510 return BBMap[Src] = BB;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000511 }
512
Amaury Secheta82042e2016-02-09 22:36:41 +0000513 LLVMBasicBlockRef CloneBB(LLVMBasicBlockRef Src) {
514 LLVMBasicBlockRef BB = DeclareBB(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000515
Amaury Secheta82042e2016-02-09 22:36:41 +0000516 // Make sure ordering is correct.
517 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Src);
518 if (Prev)
519 LLVMMoveBasicBlockAfter(BB, DeclareBB(Prev));
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000520
Amaury Secheta82042e2016-02-09 22:36:41 +0000521 LLVMValueRef First = LLVMGetFirstInstruction(Src);
522 LLVMValueRef Last = LLVMGetLastInstruction(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000523
Amaury Secheta82042e2016-02-09 22:36:41 +0000524 if (First == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000525 if (Last != nullptr)
526 report_fatal_error("Has no first instruction, but last one");
Amaury Secheta82042e2016-02-09 22:36:41 +0000527 return BB;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000528 }
529
Amaury Sechetaad93532016-02-10 00:38:50 +0000530 auto Ctx = LLVMGetModuleContext(M);
Amaury Secheta82042e2016-02-09 22:36:41 +0000531 LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx);
532 LLVMPositionBuilderAtEnd(Builder, BB);
533
534 LLVMValueRef Cur = First;
535 LLVMValueRef Next = nullptr;
536 while(true) {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000537 CloneInstruction(Cur, Builder);
Amaury Secheta82042e2016-02-09 22:36:41 +0000538 Next = LLVMGetNextInstruction(Cur);
539 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000540 if (Cur != Last)
541 report_fatal_error("Final instruction does not match Last");
Amaury Secheta82042e2016-02-09 22:36:41 +0000542 break;
543 }
544
545 LLVMValueRef Prev = LLVMGetPreviousInstruction(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000546 if (Prev != Cur)
547 report_fatal_error("Next.Previous instruction is not Current");
Amaury Secheta82042e2016-02-09 22:36:41 +0000548
549 Cur = Next;
550 }
551
552 LLVMDisposeBuilder(Builder);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000553 return BB;
554 }
555
Amaury Secheta82042e2016-02-09 22:36:41 +0000556 void CloneBBs(LLVMValueRef Src) {
557 unsigned Count = LLVMCountBasicBlocks(Src);
558 if (Count == 0)
559 return;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000560
Amaury Secheta82042e2016-02-09 22:36:41 +0000561 LLVMBasicBlockRef First = LLVMGetFirstBasicBlock(Src);
562 LLVMBasicBlockRef Last = LLVMGetLastBasicBlock(Src);
563
564 LLVMBasicBlockRef Cur = First;
565 LLVMBasicBlockRef Next = nullptr;
566 while(true) {
567 CloneBB(Cur);
568 Count--;
569 Next = LLVMGetNextBasicBlock(Cur);
570 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000571 if (Cur != Last)
572 report_fatal_error("Final basic block does not match Last");
Amaury Secheta82042e2016-02-09 22:36:41 +0000573 break;
574 }
575
576 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000577 if (Prev != Cur)
578 report_fatal_error("Next.Previous basic bloc is not Current");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000579
Amaury Secheta82042e2016-02-09 22:36:41 +0000580 Cur = Next;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000581 }
582
Amaury Sechetd01c8612016-02-14 10:06:34 +0000583 if (Count != 0)
584 report_fatal_error("Basic block count does not match iterration");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000585 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000586};
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000587
Amaury Sechetf64e8342016-02-16 07:08:49 +0000588static void clone_function(LLVMValueRef Src, LLVMModuleRef M) {
589 const char *Name = LLVMGetValueName(Src);
590 LLVMValueRef Fun = LLVMGetNamedFunction(M, Name);
591 if (!Fun)
592 report_fatal_error("Function must have been declared already");
593
594 FunCloner FC(Src, Fun);
595 FC.CloneBBs(Src);
596}
597
598static void declare_function(LLVMValueRef Src, LLVMModuleRef M) {
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000599 const char *Name = LLVMGetValueName(Src);
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000600 LLVMValueRef Fun = LLVMGetNamedFunction(M, Name);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000601 if (Fun != nullptr)
Amaury Sechetf64e8342016-02-16 07:08:49 +0000602 report_fatal_error("Function already cloned");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000603
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000604 LLVMTypeRef FunTy = LLVMGetElementType(TypeCloner(M).Clone(Src));
Amaury Sechetf64e8342016-02-16 07:08:49 +0000605 LLVMAddFunction(M, Name, FunTy);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000606}
607
608static void clone_functions(LLVMModuleRef Src, LLVMModuleRef Dst) {
609 LLVMValueRef Begin = LLVMGetFirstFunction(Src);
610 LLVMValueRef End = LLVMGetLastFunction(Src);
611
Amaury Sechetf64e8342016-02-16 07:08:49 +0000612 // First pass, we declare all function
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000613 LLVMValueRef Cur = Begin;
614 LLVMValueRef Next = nullptr;
615 while (true) {
Amaury Sechetf64e8342016-02-16 07:08:49 +0000616 declare_function(Cur, Dst);
617 Next = LLVMGetNextFunction(Cur);
618 if (Next == nullptr) {
619 if (Cur != End)
620 report_fatal_error("Last function does not match End");
621 break;
622 }
623
624 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
625 if (Prev != Cur)
626 report_fatal_error("Next.Previous function is not Current");
627
628 Cur = Next;
629 }
630
631 // Second pass, we define them
632 Cur = Begin;
633 Next = nullptr;
634 while (true) {
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000635 clone_function(Cur, Dst);
636 Next = LLVMGetNextFunction(Cur);
637 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000638 if (Cur != End)
639 report_fatal_error("Last function does not match End");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000640 break;
641 }
642
643 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000644 if (Prev != Cur)
645 report_fatal_error("Next.Previous function is not Current");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000646
647 Cur = Next;
648 }
649}
650
Benjamin Kramer9a3bd232016-02-05 13:31:14 +0000651int llvm_echo(void) {
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000652 LLVMEnablePrettyStackTrace();
653
Benjamin Kramer9a3bd232016-02-05 13:31:14 +0000654 LLVMModuleRef Src = llvm_load_module(false, true);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000655
656 LLVMContextRef Ctx = LLVMContextCreate();
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000657 LLVMModuleRef M = LLVMModuleCreateWithNameInContext("<stdin>", Ctx);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000658
Amaury Sechet55909672016-02-16 05:11:24 +0000659 LLVMSetTarget(M, LLVMGetTarget(Src));
660 LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src));
661 if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src)))
662 report_fatal_error("Inconsistent DataLayout string representation");
663
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000664 clone_functions(Src, M);
665 char *Str = LLVMPrintModuleToString(M);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000666 fputs(Str, stdout);
667
668 LLVMDisposeMessage(Str);
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000669 LLVMDisposeModule(M);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000670 LLVMContextDispose(Ctx);
671
672 return 0;
673}