blob: 8ff75fb6de515b7c6a4709e29833eb31973d4792 [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
Amaury Sechet6b16c232016-02-16 07:33:23 +0000224 if (LLVMIsAFunction(Cst))
225 return LLVMGetNamedFunction(M, Name);
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000226
227 // Try global variable
Amaury Sechet6b16c232016-02-16 07:33:23 +0000228 if (LLVMIsAGlobalVariable(Cst))
229 return LLVMGetNamedGlobal(M, Name);
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000230
231 fprintf(stderr, "Could not find @%s\n", Name);
232 exit(-1);
233 }
234
235 // Try literal
236 if (LLVMIsAConstantInt(Cst))
237 return LLVMConstInt(TypeCloner(M).Clone(Cst),
238 LLVMConstIntGetZExtValue(Cst), false);
239
240 // Try undef
241 if (LLVMIsUndef(Cst))
242 return LLVMGetUndef(TypeCloner(M).Clone(Cst));
243
Amaury Sechet6b16c232016-02-16 07:33:23 +0000244 // This kind of constant is not supported
245 if (!LLVMIsAConstantExpr(Cst))
246 report_fatal_error("Expected a constant expression");
247
248 // At this point, it must be a constant expression
249 report_fatal_error("ConstantExpression are not supported");
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000250}
251
Amaury Secheta82042e2016-02-09 22:36:41 +0000252struct FunCloner {
253 LLVMValueRef Fun;
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000254 LLVMModuleRef M;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000255
Amaury Secheta82042e2016-02-09 22:36:41 +0000256 ValueMap VMap;
257 BasicBlockMap BBMap;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000258
Amaury Sechetaad93532016-02-10 00:38:50 +0000259 FunCloner(LLVMValueRef Src, LLVMValueRef Dst): Fun(Dst),
260 M(LLVMGetGlobalParent(Fun)), VMap(clone_params(Src, Dst)) {}
261
262 LLVMTypeRef CloneType(LLVMTypeRef Src) {
263 return TypeCloner(M).Clone(Src);
264 }
265
266 LLVMTypeRef CloneType(LLVMValueRef Src) {
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000267 return TypeCloner(M).Clone(Src);
Amaury Sechetaad93532016-02-10 00:38:50 +0000268 }
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000269
Amaury Secheta82042e2016-02-09 22:36:41 +0000270 // Try to clone everything in the llvm::Value hierarchy.
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000271 LLVMValueRef CloneValue(LLVMValueRef Src) {
Amaury Secheta82042e2016-02-09 22:36:41 +0000272 // First, the value may be constant.
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000273 if (LLVMIsAConstant(Src))
274 return clone_constant(Src, M);
Amaury Secheta82042e2016-02-09 22:36:41 +0000275
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000276 // Function argument should always be in the map already.
Amaury Sechet2f432082016-02-11 21:37:54 +0000277 auto i = VMap.find(Src);
278 if (i != VMap.end())
279 return i->second;
Amaury Secheta82042e2016-02-09 22:36:41 +0000280
Amaury Sechet2f432082016-02-11 21:37:54 +0000281 if (!LLVMIsAInstruction(Src))
282 report_fatal_error("Expected an instruction");
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000283
Amaury Sechet2f432082016-02-11 21:37:54 +0000284 auto Ctx = LLVMGetModuleContext(M);
285 auto Builder = LLVMCreateBuilderInContext(Ctx);
286 auto BB = DeclareBB(LLVMGetInstructionParent(Src));
287 LLVMPositionBuilderAtEnd(Builder, BB);
288 auto Dst = CloneInstruction(Src, Builder);
289 LLVMDisposeBuilder(Builder);
290 return Dst;
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000291 }
292
293 LLVMValueRef CloneInstruction(LLVMValueRef Src, LLVMBuilderRef Builder) {
294 const char *Name = LLVMGetValueName(Src);
295 if (!LLVMIsAInstruction(Src))
296 report_fatal_error("Expected an instruction");
297
Amaury Secheta82042e2016-02-09 22:36:41 +0000298 // Check if this is something we already computed.
299 {
300 auto i = VMap.find(Src);
Amaury Sechet2f432082016-02-11 21:37:54 +0000301 if (i != VMap.end()) {
302 // If we have a hit, it means we already generated the instruction
303 // as a dependancy to somethign else. We need to make sure
304 // it is ordered properly.
305 auto I = i->second;
306 LLVMInstructionRemoveFromParent(I);
307 LLVMInsertIntoBuilderWithName(Builder, I, Name);
308 return I;
309 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000310 }
311
312 // We tried everything, it must be an instruction
313 // that hasn't been generated already.
314 LLVMValueRef Dst = nullptr;
315
316 LLVMOpcode Op = LLVMGetInstructionOpcode(Src);
317 switch(Op) {
318 case LLVMRet: {
319 int OpCount = LLVMGetNumOperands(Src);
320 if (OpCount == 0)
321 Dst = LLVMBuildRetVoid(Builder);
322 else
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000323 Dst = LLVMBuildRet(Builder, CloneValue(LLVMGetOperand(Src, 0)));
Amaury Secheta82042e2016-02-09 22:36:41 +0000324 break;
325 }
326 case LLVMBr: {
Amaury Sechete7e62172016-02-09 23:15:02 +0000327 if (!LLVMIsConditional(Src)) {
328 LLVMValueRef SrcOp = LLVMGetOperand(Src, 0);
329 LLVMBasicBlockRef SrcBB = LLVMValueAsBasicBlock(SrcOp);
330 Dst = LLVMBuildBr(Builder, DeclareBB(SrcBB));
331 break;
332 }
333
334 LLVMValueRef Cond = LLVMGetCondition(Src);
335 LLVMValueRef Else = LLVMGetOperand(Src, 1);
336 LLVMBasicBlockRef ElseBB = DeclareBB(LLVMValueAsBasicBlock(Else));
337 LLVMValueRef Then = LLVMGetOperand(Src, 2);
338 LLVMBasicBlockRef ThenBB = DeclareBB(LLVMValueAsBasicBlock(Then));
339 Dst = LLVMBuildCondBr(Builder, Cond, ThenBB, ElseBB);
Amaury Secheta82042e2016-02-09 22:36:41 +0000340 break;
341 }
342 case LLVMSwitch:
343 case LLVMIndirectBr:
344 case LLVMInvoke:
345 break;
346 case LLVMUnreachable:
347 Dst = LLVMBuildUnreachable(Builder);
348 break;
349 case LLVMAdd: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000350 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
351 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000352 Dst = LLVMBuildAdd(Builder, LHS, RHS, Name);
353 break;
354 }
355 case LLVMSub: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000356 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
357 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000358 Dst = LLVMBuildSub(Builder, LHS, RHS, Name);
359 break;
360 }
361 case LLVMMul: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000362 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
363 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000364 Dst = LLVMBuildMul(Builder, LHS, RHS, Name);
365 break;
366 }
367 case LLVMUDiv: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000368 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
369 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000370 Dst = LLVMBuildUDiv(Builder, LHS, RHS, Name);
371 break;
372 }
373 case LLVMSDiv: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000374 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
375 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000376 Dst = LLVMBuildSDiv(Builder, LHS, RHS, Name);
377 break;
378 }
379 case LLVMURem: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000380 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
381 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000382 Dst = LLVMBuildURem(Builder, LHS, RHS, Name);
383 break;
384 }
385 case LLVMSRem: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000386 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
387 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000388 Dst = LLVMBuildSRem(Builder, LHS, RHS, Name);
389 break;
390 }
391 case LLVMShl: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000392 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
393 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000394 Dst = LLVMBuildShl(Builder, LHS, RHS, Name);
395 break;
396 }
397 case LLVMLShr: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000398 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
399 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000400 Dst = LLVMBuildLShr(Builder, LHS, RHS, Name);
401 break;
402 }
403 case LLVMAShr: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000404 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
405 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000406 Dst = LLVMBuildAShr(Builder, LHS, RHS, Name);
407 break;
408 }
409 case LLVMAnd: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000410 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
411 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000412 Dst = LLVMBuildAnd(Builder, LHS, RHS, Name);
413 break;
414 }
415 case LLVMOr: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000416 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
417 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000418 Dst = LLVMBuildOr(Builder, LHS, RHS, Name);
419 break;
420 }
421 case LLVMXor: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000422 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
423 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000424 Dst = LLVMBuildXor(Builder, LHS, RHS, Name);
425 break;
426 }
427 case LLVMAlloca: {
Amaury Sechetaad93532016-02-10 00:38:50 +0000428 LLVMTypeRef Ty = CloneType(LLVMGetAllocatedType(Src));
Amaury Secheta82042e2016-02-09 22:36:41 +0000429 Dst = LLVMBuildAlloca(Builder, Ty, Name);
430 break;
431 }
Amaury Sechete7e62172016-02-09 23:15:02 +0000432 case LLVMICmp: {
433 LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src);
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000434 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
435 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Sechete7e62172016-02-09 23:15:02 +0000436 Dst = LLVMBuildICmp(Builder, Pred, LHS, RHS, Name);
437 break;
438 }
Amaury Sechet2f432082016-02-11 21:37:54 +0000439 case LLVMPHI: {
440 // We need to agressively set things here because of loops.
441 VMap[Src] = Dst = LLVMBuildPhi(Builder, CloneType(Src), Name);
442
443 SmallVector<LLVMValueRef, 8> Values;
444 SmallVector<LLVMBasicBlockRef, 8> Blocks;
445
446 unsigned IncomingCount = LLVMCountIncoming(Src);
447 for (unsigned i = 0; i < IncomingCount; ++i) {
448 Blocks.push_back(DeclareBB(LLVMGetIncomingBlock(Src, i)));
449 Values.push_back(CloneValue(LLVMGetIncomingValue(Src, i)));
450 }
451
452 LLVMAddIncoming(Dst, Values.data(), Blocks.data(), IncomingCount);
453 return Dst;
454 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000455 case LLVMCall: {
Amaury Secheta82042e2016-02-09 22:36:41 +0000456 SmallVector<LLVMValueRef, 8> Args;
Amaury Sechet5c7b3af2016-02-10 00:09:37 +0000457 int ArgCount = LLVMGetNumArgOperands(Src);
Amaury Secheta82042e2016-02-09 22:36:41 +0000458 for (int i = 0; i < ArgCount; i++)
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000459 Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
Amaury Sechet5c7b3af2016-02-10 00:09:37 +0000460 LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
Amaury Secheta82042e2016-02-09 22:36:41 +0000461 Dst = LLVMBuildCall(Builder, Fn, Args.data(), ArgCount, Name);
462 break;
463 }
Amaury Sechetaad93532016-02-10 00:38:50 +0000464 case LLVMExtractValue: {
465 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
466 if (LLVMGetNumIndices(Src) != 1)
467 report_fatal_error("Expected only one indice");
468 auto I = LLVMGetIndices(Src)[0];
469 Dst = LLVMBuildExtractValue(Builder, Agg, I, Name);
470 break;
471 }
472 case LLVMInsertValue: {
473 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
474 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 1));
475 if (LLVMGetNumIndices(Src) != 1)
476 report_fatal_error("Expected only one indice");
477 auto I = LLVMGetIndices(Src)[0];
478 Dst = LLVMBuildInsertValue(Builder, Agg, V, I, Name);
479 break;
480 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000481 default:
482 break;
483 }
484
485 if (Dst == nullptr) {
486 fprintf(stderr, "%d is not a supported opcode\n", Op);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000487 exit(-1);
488 }
489
Amaury Secheta82042e2016-02-09 22:36:41 +0000490 return VMap[Src] = Dst;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000491 }
492
Amaury Secheta82042e2016-02-09 22:36:41 +0000493 LLVMBasicBlockRef DeclareBB(LLVMBasicBlockRef Src) {
494 // Check if this is something we already computed.
495 {
496 auto i = BBMap.find(Src);
497 if (i != BBMap.end()) {
498 return i->second;
499 }
500 }
501
Amaury Secheta82042e2016-02-09 22:36:41 +0000502 LLVMValueRef V = LLVMBasicBlockAsValue(Src);
503 if (!LLVMValueIsBasicBlock(V) || LLVMValueAsBasicBlock(V) != Src)
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000504 report_fatal_error("Basic block is not a basic block");
Amaury Secheta82042e2016-02-09 22:36:41 +0000505
Amaury Sechetd01c8612016-02-14 10:06:34 +0000506 const char *Name = LLVMGetBasicBlockName(Src);
Amaury Secheta82042e2016-02-09 22:36:41 +0000507 const char *VName = LLVMGetValueName(V);
508 if (Name != VName)
509 report_fatal_error("Basic block name mismatch");
510
511 LLVMBasicBlockRef BB = LLVMAppendBasicBlock(Fun, Name);
512 return BBMap[Src] = BB;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000513 }
514
Amaury Secheta82042e2016-02-09 22:36:41 +0000515 LLVMBasicBlockRef CloneBB(LLVMBasicBlockRef Src) {
516 LLVMBasicBlockRef BB = DeclareBB(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000517
Amaury Secheta82042e2016-02-09 22:36:41 +0000518 // Make sure ordering is correct.
519 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Src);
520 if (Prev)
521 LLVMMoveBasicBlockAfter(BB, DeclareBB(Prev));
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000522
Amaury Secheta82042e2016-02-09 22:36:41 +0000523 LLVMValueRef First = LLVMGetFirstInstruction(Src);
524 LLVMValueRef Last = LLVMGetLastInstruction(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000525
Amaury Secheta82042e2016-02-09 22:36:41 +0000526 if (First == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000527 if (Last != nullptr)
528 report_fatal_error("Has no first instruction, but last one");
Amaury Secheta82042e2016-02-09 22:36:41 +0000529 return BB;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000530 }
531
Amaury Sechetaad93532016-02-10 00:38:50 +0000532 auto Ctx = LLVMGetModuleContext(M);
Amaury Secheta82042e2016-02-09 22:36:41 +0000533 LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx);
534 LLVMPositionBuilderAtEnd(Builder, BB);
535
536 LLVMValueRef Cur = First;
537 LLVMValueRef Next = nullptr;
538 while(true) {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000539 CloneInstruction(Cur, Builder);
Amaury Secheta82042e2016-02-09 22:36:41 +0000540 Next = LLVMGetNextInstruction(Cur);
541 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000542 if (Cur != Last)
543 report_fatal_error("Final instruction does not match Last");
Amaury Secheta82042e2016-02-09 22:36:41 +0000544 break;
545 }
546
547 LLVMValueRef Prev = LLVMGetPreviousInstruction(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000548 if (Prev != Cur)
549 report_fatal_error("Next.Previous instruction is not Current");
Amaury Secheta82042e2016-02-09 22:36:41 +0000550
551 Cur = Next;
552 }
553
554 LLVMDisposeBuilder(Builder);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000555 return BB;
556 }
557
Amaury Secheta82042e2016-02-09 22:36:41 +0000558 void CloneBBs(LLVMValueRef Src) {
559 unsigned Count = LLVMCountBasicBlocks(Src);
560 if (Count == 0)
561 return;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000562
Amaury Secheta82042e2016-02-09 22:36:41 +0000563 LLVMBasicBlockRef First = LLVMGetFirstBasicBlock(Src);
564 LLVMBasicBlockRef Last = LLVMGetLastBasicBlock(Src);
565
566 LLVMBasicBlockRef Cur = First;
567 LLVMBasicBlockRef Next = nullptr;
568 while(true) {
569 CloneBB(Cur);
570 Count--;
571 Next = LLVMGetNextBasicBlock(Cur);
572 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000573 if (Cur != Last)
574 report_fatal_error("Final basic block does not match Last");
Amaury Secheta82042e2016-02-09 22:36:41 +0000575 break;
576 }
577
578 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000579 if (Prev != Cur)
580 report_fatal_error("Next.Previous basic bloc is not Current");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000581
Amaury Secheta82042e2016-02-09 22:36:41 +0000582 Cur = Next;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000583 }
584
Amaury Sechetd01c8612016-02-14 10:06:34 +0000585 if (Count != 0)
586 report_fatal_error("Basic block count does not match iterration");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000587 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000588};
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000589
Amaury Sechetf64e8342016-02-16 07:08:49 +0000590static void clone_function(LLVMValueRef Src, LLVMModuleRef M) {
591 const char *Name = LLVMGetValueName(Src);
592 LLVMValueRef Fun = LLVMGetNamedFunction(M, Name);
593 if (!Fun)
594 report_fatal_error("Function must have been declared already");
595
596 FunCloner FC(Src, Fun);
597 FC.CloneBBs(Src);
598}
599
600static void declare_function(LLVMValueRef Src, LLVMModuleRef M) {
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000601 const char *Name = LLVMGetValueName(Src);
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000602 LLVMValueRef Fun = LLVMGetNamedFunction(M, Name);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000603 if (Fun != nullptr)
Amaury Sechetf64e8342016-02-16 07:08:49 +0000604 report_fatal_error("Function already cloned");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000605
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000606 LLVMTypeRef FunTy = LLVMGetElementType(TypeCloner(M).Clone(Src));
Amaury Sechetf64e8342016-02-16 07:08:49 +0000607 LLVMAddFunction(M, Name, FunTy);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000608}
609
610static void clone_functions(LLVMModuleRef Src, LLVMModuleRef Dst) {
611 LLVMValueRef Begin = LLVMGetFirstFunction(Src);
612 LLVMValueRef End = LLVMGetLastFunction(Src);
Amaury Sechetf447a6b2016-02-16 08:37:01 +0000613 if (!Begin) {
614 if (End != nullptr)
615 report_fatal_error("Range has an end but no start");
616 return;
617 }
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000618
Amaury Sechetf64e8342016-02-16 07:08:49 +0000619 // First pass, we declare all function
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000620 LLVMValueRef Cur = Begin;
621 LLVMValueRef Next = nullptr;
622 while (true) {
Amaury Sechetf64e8342016-02-16 07:08:49 +0000623 declare_function(Cur, Dst);
624 Next = LLVMGetNextFunction(Cur);
625 if (Next == nullptr) {
626 if (Cur != End)
627 report_fatal_error("Last function does not match End");
628 break;
629 }
630
631 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
632 if (Prev != Cur)
633 report_fatal_error("Next.Previous function is not Current");
634
635 Cur = Next;
636 }
637
638 // Second pass, we define them
639 Cur = Begin;
640 Next = nullptr;
641 while (true) {
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000642 clone_function(Cur, Dst);
643 Next = LLVMGetNextFunction(Cur);
644 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000645 if (Cur != End)
646 report_fatal_error("Last function does not match End");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000647 break;
648 }
649
650 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000651 if (Prev != Cur)
652 report_fatal_error("Next.Previous function is not Current");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000653
654 Cur = Next;
655 }
656}
657
Benjamin Kramer9a3bd232016-02-05 13:31:14 +0000658int llvm_echo(void) {
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000659 LLVMEnablePrettyStackTrace();
660
Benjamin Kramer9a3bd232016-02-05 13:31:14 +0000661 LLVMModuleRef Src = llvm_load_module(false, true);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000662
663 LLVMContextRef Ctx = LLVMContextCreate();
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000664 LLVMModuleRef M = LLVMModuleCreateWithNameInContext("<stdin>", Ctx);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000665
Amaury Sechet55909672016-02-16 05:11:24 +0000666 LLVMSetTarget(M, LLVMGetTarget(Src));
667 LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src));
668 if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src)))
669 report_fatal_error("Inconsistent DataLayout string representation");
670
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000671 clone_functions(Src, M);
672 char *Str = LLVMPrintModuleToString(M);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000673 fputs(Str, stdout);
674
675 LLVMDisposeMessage(Str);
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000676 LLVMDisposeModule(M);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000677 LLVMContextDispose(Ctx);
678
679 return 0;
680}