1 //===-- echo.cpp - tool for testing libLLVM and llvm-c API ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the --echo command in llvm-c-test.
10 //
11 // This command uses the C API to read a module and output an exact copy of it
12 // as output. It is used to check that the resulting module matches the input
13 // to validate that the C API can read and write modules properly.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm-c-test.h"
18 #include "llvm-c/DebugInfo.h"
19 #include "llvm-c/Target.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/Support/ErrorHandling.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 using namespace llvm;
27
28 // Provide DenseMapInfo for C API opaque types.
29 template<typename T>
30 struct CAPIDenseMap {};
31
32 // The default DenseMapInfo require to know about pointer alignment.
33 // Because the C API uses opaque pointer types, their alignment is unknown.
34 // As a result, we need to roll out our own implementation.
35 template<typename T>
36 struct CAPIDenseMap<T*> {
37 struct CAPIDenseMapInfo {
getEmptyKeyCAPIDenseMap::CAPIDenseMapInfo38 static inline T* getEmptyKey() {
39 uintptr_t Val = static_cast<uintptr_t>(-1);
40 return reinterpret_cast<T*>(Val);
41 }
getTombstoneKeyCAPIDenseMap::CAPIDenseMapInfo42 static inline T* getTombstoneKey() {
43 uintptr_t Val = static_cast<uintptr_t>(-2);
44 return reinterpret_cast<T*>(Val);
45 }
getHashValueCAPIDenseMap::CAPIDenseMapInfo46 static unsigned getHashValue(const T *PtrVal) {
47 return hash_value(PtrVal);
48 }
isEqualCAPIDenseMap::CAPIDenseMapInfo49 static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; }
50 };
51
52 typedef DenseMap<T*, T*, CAPIDenseMapInfo> Map;
53 };
54
55 typedef CAPIDenseMap<LLVMValueRef>::Map ValueMap;
56 typedef CAPIDenseMap<LLVMBasicBlockRef>::Map BasicBlockMap;
57
58 struct TypeCloner {
59 LLVMModuleRef M;
60 LLVMContextRef Ctx;
61
TypeClonerTypeCloner62 TypeCloner(LLVMModuleRef M): M(M), Ctx(LLVMGetModuleContext(M)) {}
63
CloneTypeCloner64 LLVMTypeRef Clone(LLVMValueRef Src) {
65 return Clone(LLVMTypeOf(Src));
66 }
67
CloneTypeCloner68 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 LLVMBFloatTypeKind:
76 return LLVMHalfTypeInContext(Ctx);
77 case LLVMFloatTypeKind:
78 return LLVMFloatTypeInContext(Ctx);
79 case LLVMDoubleTypeKind:
80 return LLVMDoubleTypeInContext(Ctx);
81 case LLVMX86_FP80TypeKind:
82 return LLVMX86FP80TypeInContext(Ctx);
83 case LLVMFP128TypeKind:
84 return LLVMFP128TypeInContext(Ctx);
85 case LLVMPPC_FP128TypeKind:
86 return LLVMPPCFP128TypeInContext(Ctx);
87 case LLVMLabelTypeKind:
88 return LLVMLabelTypeInContext(Ctx);
89 case LLVMIntegerTypeKind:
90 return LLVMIntTypeInContext(Ctx, LLVMGetIntTypeWidth(Src));
91 case LLVMFunctionTypeKind: {
92 unsigned ParamCount = LLVMCountParamTypes(Src);
93 LLVMTypeRef* Params = nullptr;
94 if (ParamCount > 0) {
95 Params = static_cast<LLVMTypeRef*>(
96 safe_malloc(ParamCount * sizeof(LLVMTypeRef)));
97 LLVMGetParamTypes(Src, Params);
98 for (unsigned i = 0; i < ParamCount; i++)
99 Params[i] = Clone(Params[i]);
100 }
101
102 LLVMTypeRef FunTy = LLVMFunctionType(Clone(LLVMGetReturnType(Src)),
103 Params, ParamCount,
104 LLVMIsFunctionVarArg(Src));
105 if (ParamCount > 0)
106 free(Params);
107 return FunTy;
108 }
109 case LLVMStructTypeKind: {
110 LLVMTypeRef S = nullptr;
111 const char *Name = LLVMGetStructName(Src);
112 if (Name) {
113 S = LLVMGetTypeByName2(Ctx, Name);
114 if (S)
115 return S;
116 S = LLVMStructCreateNamed(Ctx, Name);
117 if (LLVMIsOpaqueStruct(Src))
118 return S;
119 }
120
121 unsigned EltCount = LLVMCountStructElementTypes(Src);
122 SmallVector<LLVMTypeRef, 8> Elts;
123 for (unsigned i = 0; i < EltCount; i++)
124 Elts.push_back(Clone(LLVMStructGetTypeAtIndex(Src, i)));
125 if (Name)
126 LLVMStructSetBody(S, Elts.data(), EltCount, LLVMIsPackedStruct(Src));
127 else
128 S = LLVMStructTypeInContext(Ctx, Elts.data(), EltCount,
129 LLVMIsPackedStruct(Src));
130 return S;
131 }
132 case LLVMArrayTypeKind:
133 return LLVMArrayType(
134 Clone(LLVMGetElementType(Src)),
135 LLVMGetArrayLength(Src)
136 );
137 case LLVMPointerTypeKind:
138 return LLVMPointerType(
139 Clone(LLVMGetElementType(Src)),
140 LLVMGetPointerAddressSpace(Src)
141 );
142 case LLVMVectorTypeKind:
143 return LLVMVectorType(
144 Clone(LLVMGetElementType(Src)),
145 LLVMGetVectorSize(Src)
146 );
147 case LLVMScalableVectorTypeKind:
148 return LLVMScalableVectorType(Clone(LLVMGetElementType(Src)),
149 LLVMGetVectorSize(Src));
150 case LLVMMetadataTypeKind:
151 return LLVMMetadataTypeInContext(Ctx);
152 case LLVMX86_AMXTypeKind:
153 return LLVMX86AMXTypeInContext(Ctx);
154 case LLVMX86_MMXTypeKind:
155 return LLVMX86MMXTypeInContext(Ctx);
156 case LLVMTokenTypeKind:
157 return LLVMTokenTypeInContext(Ctx);
158 }
159
160 fprintf(stderr, "%d is not a supported typekind\n", Kind);
161 exit(-1);
162 }
163 };
164
clone_params(LLVMValueRef Src,LLVMValueRef Dst)165 static ValueMap clone_params(LLVMValueRef Src, LLVMValueRef Dst) {
166 unsigned Count = LLVMCountParams(Src);
167 if (Count != LLVMCountParams(Dst))
168 report_fatal_error("Parameter count mismatch");
169
170 ValueMap VMap;
171 if (Count == 0)
172 return VMap;
173
174 LLVMValueRef SrcFirst = LLVMGetFirstParam(Src);
175 LLVMValueRef DstFirst = LLVMGetFirstParam(Dst);
176 LLVMValueRef SrcLast = LLVMGetLastParam(Src);
177 LLVMValueRef DstLast = LLVMGetLastParam(Dst);
178
179 LLVMValueRef SrcCur = SrcFirst;
180 LLVMValueRef DstCur = DstFirst;
181 LLVMValueRef SrcNext = nullptr;
182 LLVMValueRef DstNext = nullptr;
183 while (true) {
184 size_t NameLen;
185 const char *Name = LLVMGetValueName2(SrcCur, &NameLen);
186 LLVMSetValueName2(DstCur, Name, NameLen);
187
188 VMap[SrcCur] = DstCur;
189
190 Count--;
191 SrcNext = LLVMGetNextParam(SrcCur);
192 DstNext = LLVMGetNextParam(DstCur);
193 if (SrcNext == nullptr && DstNext == nullptr) {
194 if (SrcCur != SrcLast)
195 report_fatal_error("SrcLast param does not match End");
196 if (DstCur != DstLast)
197 report_fatal_error("DstLast param does not match End");
198 break;
199 }
200
201 if (SrcNext == nullptr)
202 report_fatal_error("SrcNext was unexpectedly null");
203 if (DstNext == nullptr)
204 report_fatal_error("DstNext was unexpectedly null");
205
206 LLVMValueRef SrcPrev = LLVMGetPreviousParam(SrcNext);
207 if (SrcPrev != SrcCur)
208 report_fatal_error("SrcNext.Previous param is not Current");
209
210 LLVMValueRef DstPrev = LLVMGetPreviousParam(DstNext);
211 if (DstPrev != DstCur)
212 report_fatal_error("DstNext.Previous param is not Current");
213
214 SrcCur = SrcNext;
215 DstCur = DstNext;
216 }
217
218 if (Count != 0)
219 report_fatal_error("Parameter count does not match iteration");
220
221 return VMap;
222 }
223
check_value_kind(LLVMValueRef V,LLVMValueKind K)224 static void check_value_kind(LLVMValueRef V, LLVMValueKind K) {
225 if (LLVMGetValueKind(V) != K)
226 report_fatal_error("LLVMGetValueKind returned incorrect type");
227 }
228
229 static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M);
230
clone_constant(LLVMValueRef Cst,LLVMModuleRef M)231 static LLVMValueRef clone_constant(LLVMValueRef Cst, LLVMModuleRef M) {
232 LLVMValueRef Ret = clone_constant_impl(Cst, M);
233 check_value_kind(Ret, LLVMGetValueKind(Cst));
234 return Ret;
235 }
236
clone_constant_impl(LLVMValueRef Cst,LLVMModuleRef M)237 static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M) {
238 if (!LLVMIsAConstant(Cst))
239 report_fatal_error("Expected a constant");
240
241 // Maybe it is a symbol
242 if (LLVMIsAGlobalValue(Cst)) {
243 size_t NameLen;
244 const char *Name = LLVMGetValueName2(Cst, &NameLen);
245
246 // Try function
247 if (LLVMIsAFunction(Cst)) {
248 check_value_kind(Cst, LLVMFunctionValueKind);
249
250 LLVMValueRef Dst = nullptr;
251 // Try an intrinsic
252 unsigned ID = LLVMGetIntrinsicID(Cst);
253 if (ID > 0 && !LLVMIntrinsicIsOverloaded(ID)) {
254 Dst = LLVMGetIntrinsicDeclaration(M, ID, nullptr, 0);
255 } else {
256 // Try a normal function
257 Dst = LLVMGetNamedFunction(M, Name);
258 }
259
260 if (Dst)
261 return Dst;
262 report_fatal_error("Could not find function");
263 }
264
265 // Try global variable
266 if (LLVMIsAGlobalVariable(Cst)) {
267 check_value_kind(Cst, LLVMGlobalVariableValueKind);
268 LLVMValueRef Dst = LLVMGetNamedGlobal(M, Name);
269 if (Dst)
270 return Dst;
271 report_fatal_error("Could not find variable");
272 }
273
274 // Try global alias
275 if (LLVMIsAGlobalAlias(Cst)) {
276 check_value_kind(Cst, LLVMGlobalAliasValueKind);
277 LLVMValueRef Dst = LLVMGetNamedGlobalAlias(M, Name, NameLen);
278 if (Dst)
279 return Dst;
280 report_fatal_error("Could not find alias");
281 }
282
283 fprintf(stderr, "Could not find @%s\n", Name);
284 exit(-1);
285 }
286
287 // Try integer literal
288 if (LLVMIsAConstantInt(Cst)) {
289 check_value_kind(Cst, LLVMConstantIntValueKind);
290 return LLVMConstInt(TypeCloner(M).Clone(Cst),
291 LLVMConstIntGetZExtValue(Cst), false);
292 }
293
294 // Try zeroinitializer
295 if (LLVMIsAConstantAggregateZero(Cst)) {
296 check_value_kind(Cst, LLVMConstantAggregateZeroValueKind);
297 return LLVMConstNull(TypeCloner(M).Clone(Cst));
298 }
299
300 // Try constant array
301 if (LLVMIsAConstantArray(Cst)) {
302 check_value_kind(Cst, LLVMConstantArrayValueKind);
303 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
304 unsigned EltCount = LLVMGetArrayLength(Ty);
305 SmallVector<LLVMValueRef, 8> Elts;
306 for (unsigned i = 0; i < EltCount; i++)
307 Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
308 return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
309 }
310
311 // Try constant data array
312 if (LLVMIsAConstantDataArray(Cst)) {
313 check_value_kind(Cst, LLVMConstantDataArrayValueKind);
314 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
315 unsigned EltCount = LLVMGetArrayLength(Ty);
316 SmallVector<LLVMValueRef, 8> Elts;
317 for (unsigned i = 0; i < EltCount; i++)
318 Elts.push_back(clone_constant(LLVMGetElementAsConstant(Cst, i), M));
319 return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
320 }
321
322 // Try constant struct
323 if (LLVMIsAConstantStruct(Cst)) {
324 check_value_kind(Cst, LLVMConstantStructValueKind);
325 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
326 unsigned EltCount = LLVMCountStructElementTypes(Ty);
327 SmallVector<LLVMValueRef, 8> Elts;
328 for (unsigned i = 0; i < EltCount; i++)
329 Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
330 if (LLVMGetStructName(Ty))
331 return LLVMConstNamedStruct(Ty, Elts.data(), EltCount);
332 return LLVMConstStructInContext(LLVMGetModuleContext(M), Elts.data(),
333 EltCount, LLVMIsPackedStruct(Ty));
334 }
335
336 // Try ConstantPointerNull
337 if (LLVMIsAConstantPointerNull(Cst)) {
338 check_value_kind(Cst, LLVMConstantPointerNullValueKind);
339 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
340 return LLVMConstNull(Ty);
341 }
342
343 // Try undef
344 if (LLVMIsUndef(Cst)) {
345 check_value_kind(Cst, LLVMUndefValueValueKind);
346 return LLVMGetUndef(TypeCloner(M).Clone(Cst));
347 }
348
349 // Try poison
350 if (LLVMIsPoison(Cst)) {
351 check_value_kind(Cst, LLVMPoisonValueValueKind);
352 return LLVMGetPoison(TypeCloner(M).Clone(Cst));
353 }
354
355 // Try null
356 if (LLVMIsNull(Cst)) {
357 check_value_kind(Cst, LLVMConstantTokenNoneValueKind);
358 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
359 return LLVMConstNull(Ty);
360 }
361
362 // Try float literal
363 if (LLVMIsAConstantFP(Cst)) {
364 check_value_kind(Cst, LLVMConstantFPValueKind);
365 report_fatal_error("ConstantFP is not supported");
366 }
367
368 // Try ConstantVector
369 if (LLVMIsAConstantVector(Cst)) {
370 check_value_kind(Cst, LLVMConstantVectorValueKind);
371 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
372 unsigned EltCount = LLVMGetVectorSize(Ty);
373 SmallVector<LLVMValueRef, 8> Elts;
374 for (unsigned i = 0; i < EltCount; i++)
375 Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
376 return LLVMConstVector(Elts.data(), EltCount);
377 }
378
379 // Try ConstantDataVector
380 if (LLVMIsAConstantDataVector(Cst)) {
381 check_value_kind(Cst, LLVMConstantDataVectorValueKind);
382 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
383 unsigned EltCount = LLVMGetVectorSize(Ty);
384 SmallVector<LLVMValueRef, 8> Elts;
385 for (unsigned i = 0; i < EltCount; i++)
386 Elts.push_back(clone_constant(LLVMGetElementAsConstant(Cst, i), M));
387 return LLVMConstVector(Elts.data(), EltCount);
388 }
389
390 // At this point, if it's not a constant expression, it's a kind of constant
391 // which is not supported
392 if (!LLVMIsAConstantExpr(Cst))
393 report_fatal_error("Unsupported constant kind");
394
395 // At this point, it must be a constant expression
396 check_value_kind(Cst, LLVMConstantExprValueKind);
397
398 LLVMOpcode Op = LLVMGetConstOpcode(Cst);
399 switch(Op) {
400 case LLVMBitCast:
401 return LLVMConstBitCast(clone_constant(LLVMGetOperand(Cst, 0), M),
402 TypeCloner(M).Clone(Cst));
403 default:
404 fprintf(stderr, "%d is not a supported opcode for constant expressions\n",
405 Op);
406 exit(-1);
407 }
408 }
409
410 struct FunCloner {
411 LLVMValueRef Fun;
412 LLVMModuleRef M;
413
414 ValueMap VMap;
415 BasicBlockMap BBMap;
416
FunClonerFunCloner417 FunCloner(LLVMValueRef Src, LLVMValueRef Dst): Fun(Dst),
418 M(LLVMGetGlobalParent(Fun)), VMap(clone_params(Src, Dst)) {}
419
CloneTypeFunCloner420 LLVMTypeRef CloneType(LLVMTypeRef Src) {
421 return TypeCloner(M).Clone(Src);
422 }
423
CloneTypeFunCloner424 LLVMTypeRef CloneType(LLVMValueRef Src) {
425 return TypeCloner(M).Clone(Src);
426 }
427
428 // Try to clone everything in the llvm::Value hierarchy.
CloneValueFunCloner429 LLVMValueRef CloneValue(LLVMValueRef Src) {
430 // First, the value may be constant.
431 if (LLVMIsAConstant(Src))
432 return clone_constant(Src, M);
433
434 // Function argument should always be in the map already.
435 auto i = VMap.find(Src);
436 if (i != VMap.end())
437 return i->second;
438
439 if (!LLVMIsAInstruction(Src))
440 report_fatal_error("Expected an instruction");
441
442 auto Ctx = LLVMGetModuleContext(M);
443 auto Builder = LLVMCreateBuilderInContext(Ctx);
444 auto BB = DeclareBB(LLVMGetInstructionParent(Src));
445 LLVMPositionBuilderAtEnd(Builder, BB);
446 auto Dst = CloneInstruction(Src, Builder);
447 LLVMDisposeBuilder(Builder);
448 return Dst;
449 }
450
CloneAttrsFunCloner451 void CloneAttrs(LLVMValueRef Src, LLVMValueRef Dst) {
452 auto Ctx = LLVMGetModuleContext(M);
453 int ArgCount = LLVMGetNumArgOperands(Src);
454 for (int i = LLVMAttributeReturnIndex; i <= ArgCount; i++) {
455 for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) {
456 if (auto SrcA = LLVMGetCallSiteEnumAttribute(Src, i, k)) {
457 auto Val = LLVMGetEnumAttributeValue(SrcA);
458 auto A = LLVMCreateEnumAttribute(Ctx, k, Val);
459 LLVMAddCallSiteAttribute(Dst, i, A);
460 }
461 }
462 }
463 }
464
CloneInstructionFunCloner465 LLVMValueRef CloneInstruction(LLVMValueRef Src, LLVMBuilderRef Builder) {
466 check_value_kind(Src, LLVMInstructionValueKind);
467 if (!LLVMIsAInstruction(Src))
468 report_fatal_error("Expected an instruction");
469
470 size_t NameLen;
471 const char *Name = LLVMGetValueName2(Src, &NameLen);
472
473 // Check if this is something we already computed.
474 {
475 auto i = VMap.find(Src);
476 if (i != VMap.end()) {
477 // If we have a hit, it means we already generated the instruction
478 // as a dependency to something else. We need to make sure
479 // it is ordered properly.
480 auto I = i->second;
481 LLVMInstructionRemoveFromParent(I);
482 LLVMInsertIntoBuilderWithName(Builder, I, Name);
483 return I;
484 }
485 }
486
487 // We tried everything, it must be an instruction
488 // that hasn't been generated already.
489 LLVMValueRef Dst = nullptr;
490
491 LLVMOpcode Op = LLVMGetInstructionOpcode(Src);
492 switch(Op) {
493 case LLVMRet: {
494 int OpCount = LLVMGetNumOperands(Src);
495 if (OpCount == 0)
496 Dst = LLVMBuildRetVoid(Builder);
497 else
498 Dst = LLVMBuildRet(Builder, CloneValue(LLVMGetOperand(Src, 0)));
499 break;
500 }
501 case LLVMBr: {
502 if (!LLVMIsConditional(Src)) {
503 LLVMValueRef SrcOp = LLVMGetOperand(Src, 0);
504 LLVMBasicBlockRef SrcBB = LLVMValueAsBasicBlock(SrcOp);
505 Dst = LLVMBuildBr(Builder, DeclareBB(SrcBB));
506 break;
507 }
508
509 LLVMValueRef Cond = LLVMGetCondition(Src);
510 LLVMValueRef Else = LLVMGetOperand(Src, 1);
511 LLVMBasicBlockRef ElseBB = DeclareBB(LLVMValueAsBasicBlock(Else));
512 LLVMValueRef Then = LLVMGetOperand(Src, 2);
513 LLVMBasicBlockRef ThenBB = DeclareBB(LLVMValueAsBasicBlock(Then));
514 Dst = LLVMBuildCondBr(Builder, CloneValue(Cond), ThenBB, ElseBB);
515 break;
516 }
517 case LLVMSwitch:
518 case LLVMIndirectBr:
519 break;
520 case LLVMInvoke: {
521 SmallVector<LLVMValueRef, 8> Args;
522 int ArgCount = LLVMGetNumArgOperands(Src);
523 for (int i = 0; i < ArgCount; i++)
524 Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
525 LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
526 LLVMBasicBlockRef Then = DeclareBB(LLVMGetNormalDest(Src));
527 LLVMBasicBlockRef Unwind = DeclareBB(LLVMGetUnwindDest(Src));
528 Dst = LLVMBuildInvoke(Builder, Fn, Args.data(), ArgCount,
529 Then, Unwind, Name);
530 CloneAttrs(Src, Dst);
531 break;
532 }
533 case LLVMUnreachable:
534 Dst = LLVMBuildUnreachable(Builder);
535 break;
536 case LLVMAdd: {
537 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
538 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
539 Dst = LLVMBuildAdd(Builder, LHS, RHS, Name);
540 break;
541 }
542 case LLVMSub: {
543 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
544 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
545 Dst = LLVMBuildSub(Builder, LHS, RHS, Name);
546 break;
547 }
548 case LLVMMul: {
549 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
550 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
551 Dst = LLVMBuildMul(Builder, LHS, RHS, Name);
552 break;
553 }
554 case LLVMUDiv: {
555 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
556 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
557 Dst = LLVMBuildUDiv(Builder, LHS, RHS, Name);
558 break;
559 }
560 case LLVMSDiv: {
561 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
562 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
563 Dst = LLVMBuildSDiv(Builder, LHS, RHS, Name);
564 break;
565 }
566 case LLVMURem: {
567 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
568 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
569 Dst = LLVMBuildURem(Builder, LHS, RHS, Name);
570 break;
571 }
572 case LLVMSRem: {
573 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
574 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
575 Dst = LLVMBuildSRem(Builder, LHS, RHS, Name);
576 break;
577 }
578 case LLVMShl: {
579 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
580 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
581 Dst = LLVMBuildShl(Builder, LHS, RHS, Name);
582 break;
583 }
584 case LLVMLShr: {
585 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
586 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
587 Dst = LLVMBuildLShr(Builder, LHS, RHS, Name);
588 break;
589 }
590 case LLVMAShr: {
591 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
592 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
593 Dst = LLVMBuildAShr(Builder, LHS, RHS, Name);
594 break;
595 }
596 case LLVMAnd: {
597 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
598 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
599 Dst = LLVMBuildAnd(Builder, LHS, RHS, Name);
600 break;
601 }
602 case LLVMOr: {
603 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
604 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
605 Dst = LLVMBuildOr(Builder, LHS, RHS, Name);
606 break;
607 }
608 case LLVMXor: {
609 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
610 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
611 Dst = LLVMBuildXor(Builder, LHS, RHS, Name);
612 break;
613 }
614 case LLVMAlloca: {
615 LLVMTypeRef Ty = CloneType(LLVMGetAllocatedType(Src));
616 Dst = LLVMBuildAlloca(Builder, Ty, Name);
617 LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
618 break;
619 }
620 case LLVMLoad: {
621 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
622 Dst = LLVMBuildLoad(Builder, Ptr, Name);
623 LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
624 LLVMSetOrdering(Dst, LLVMGetOrdering(Src));
625 LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
626 break;
627 }
628 case LLVMStore: {
629 LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 0));
630 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 1));
631 Dst = LLVMBuildStore(Builder, Val, Ptr);
632 LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
633 LLVMSetOrdering(Dst, LLVMGetOrdering(Src));
634 LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
635 break;
636 }
637 case LLVMGetElementPtr: {
638 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
639 SmallVector<LLVMValueRef, 8> Idx;
640 int NumIdx = LLVMGetNumIndices(Src);
641 for (int i = 1; i <= NumIdx; i++)
642 Idx.push_back(CloneValue(LLVMGetOperand(Src, i)));
643 if (LLVMIsInBounds(Src))
644 Dst = LLVMBuildInBoundsGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
645 else
646 Dst = LLVMBuildGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
647 break;
648 }
649 case LLVMAtomicRMW: {
650 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
651 LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 1));
652 LLVMAtomicRMWBinOp BinOp = LLVMGetAtomicRMWBinOp(Src);
653 LLVMAtomicOrdering Ord = LLVMGetOrdering(Src);
654 LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src);
655 Dst = LLVMBuildAtomicRMW(Builder, BinOp, Ptr, Val, Ord, SingleThread);
656 LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
657 LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
658 LLVMSetValueName2(Dst, Name, NameLen);
659 break;
660 }
661 case LLVMAtomicCmpXchg: {
662 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
663 LLVMValueRef Cmp = CloneValue(LLVMGetOperand(Src, 1));
664 LLVMValueRef New = CloneValue(LLVMGetOperand(Src, 2));
665 LLVMAtomicOrdering Succ = LLVMGetCmpXchgSuccessOrdering(Src);
666 LLVMAtomicOrdering Fail = LLVMGetCmpXchgFailureOrdering(Src);
667 LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src);
668
669 Dst = LLVMBuildAtomicCmpXchg(Builder, Ptr, Cmp, New, Succ, Fail,
670 SingleThread);
671 LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
672 LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
673 LLVMSetWeak(Dst, LLVMGetWeak(Src));
674 LLVMSetValueName2(Dst, Name, NameLen);
675 break;
676 }
677 case LLVMBitCast: {
678 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 0));
679 Dst = LLVMBuildBitCast(Builder, V, CloneType(Src), Name);
680 break;
681 }
682 case LLVMICmp: {
683 LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src);
684 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
685 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
686 Dst = LLVMBuildICmp(Builder, Pred, LHS, RHS, Name);
687 break;
688 }
689 case LLVMPHI: {
690 // We need to aggressively set things here because of loops.
691 VMap[Src] = Dst = LLVMBuildPhi(Builder, CloneType(Src), Name);
692
693 SmallVector<LLVMValueRef, 8> Values;
694 SmallVector<LLVMBasicBlockRef, 8> Blocks;
695
696 unsigned IncomingCount = LLVMCountIncoming(Src);
697 for (unsigned i = 0; i < IncomingCount; ++i) {
698 Blocks.push_back(DeclareBB(LLVMGetIncomingBlock(Src, i)));
699 Values.push_back(CloneValue(LLVMGetIncomingValue(Src, i)));
700 }
701
702 LLVMAddIncoming(Dst, Values.data(), Blocks.data(), IncomingCount);
703 return Dst;
704 }
705 case LLVMCall: {
706 SmallVector<LLVMValueRef, 8> Args;
707 int ArgCount = LLVMGetNumArgOperands(Src);
708 for (int i = 0; i < ArgCount; i++)
709 Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
710 LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
711 Dst = LLVMBuildCall(Builder, Fn, Args.data(), ArgCount, Name);
712 LLVMSetTailCall(Dst, LLVMIsTailCall(Src));
713 CloneAttrs(Src, Dst);
714 break;
715 }
716 case LLVMResume: {
717 Dst = LLVMBuildResume(Builder, CloneValue(LLVMGetOperand(Src, 0)));
718 break;
719 }
720 case LLVMLandingPad: {
721 // The landing pad API is a bit screwed up for historical reasons.
722 Dst = LLVMBuildLandingPad(Builder, CloneType(Src), nullptr, 0, Name);
723 unsigned NumClauses = LLVMGetNumClauses(Src);
724 for (unsigned i = 0; i < NumClauses; ++i)
725 LLVMAddClause(Dst, CloneValue(LLVMGetClause(Src, i)));
726 LLVMSetCleanup(Dst, LLVMIsCleanup(Src));
727 break;
728 }
729 case LLVMCleanupRet: {
730 LLVMValueRef CatchPad = CloneValue(LLVMGetOperand(Src, 0));
731 LLVMBasicBlockRef Unwind = nullptr;
732 if (LLVMBasicBlockRef UDest = LLVMGetUnwindDest(Src))
733 Unwind = DeclareBB(UDest);
734 Dst = LLVMBuildCleanupRet(Builder, CatchPad, Unwind);
735 break;
736 }
737 case LLVMCatchRet: {
738 LLVMValueRef CatchPad = CloneValue(LLVMGetOperand(Src, 0));
739 LLVMBasicBlockRef SuccBB = DeclareBB(LLVMGetSuccessor(Src, 0));
740 Dst = LLVMBuildCatchRet(Builder, CatchPad, SuccBB);
741 break;
742 }
743 case LLVMCatchPad: {
744 LLVMValueRef ParentPad = CloneValue(LLVMGetParentCatchSwitch(Src));
745 SmallVector<LLVMValueRef, 8> Args;
746 int ArgCount = LLVMGetNumArgOperands(Src);
747 for (int i = 0; i < ArgCount; i++)
748 Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
749 Dst = LLVMBuildCatchPad(Builder, ParentPad,
750 Args.data(), ArgCount, Name);
751 break;
752 }
753 case LLVMCleanupPad: {
754 LLVMValueRef ParentPad = CloneValue(LLVMGetOperand(Src, 0));
755 SmallVector<LLVMValueRef, 8> Args;
756 int ArgCount = LLVMGetNumArgOperands(Src);
757 for (int i = 0; i < ArgCount; i++)
758 Args.push_back(CloneValue(LLVMGetArgOperand(Src, i)));
759 Dst = LLVMBuildCleanupPad(Builder, ParentPad,
760 Args.data(), ArgCount, Name);
761 break;
762 }
763 case LLVMCatchSwitch: {
764 LLVMValueRef ParentPad = CloneValue(LLVMGetOperand(Src, 0));
765 LLVMBasicBlockRef UnwindBB = nullptr;
766 if (LLVMBasicBlockRef UDest = LLVMGetUnwindDest(Src)) {
767 UnwindBB = DeclareBB(UDest);
768 }
769 unsigned NumHandlers = LLVMGetNumHandlers(Src);
770 Dst = LLVMBuildCatchSwitch(Builder, ParentPad, UnwindBB, NumHandlers, Name);
771 if (NumHandlers > 0) {
772 LLVMBasicBlockRef *Handlers = static_cast<LLVMBasicBlockRef*>(
773 safe_malloc(NumHandlers * sizeof(LLVMBasicBlockRef)));
774 LLVMGetHandlers(Src, Handlers);
775 for (unsigned i = 0; i < NumHandlers; i++)
776 LLVMAddHandler(Dst, DeclareBB(Handlers[i]));
777 free(Handlers);
778 }
779 break;
780 }
781 case LLVMExtractValue: {
782 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
783 if (LLVMGetNumIndices(Src) > 1)
784 report_fatal_error("ExtractValue: Expected only one index");
785 else if (LLVMGetNumIndices(Src) < 1)
786 report_fatal_error("ExtractValue: Expected an index");
787 auto I = LLVMGetIndices(Src)[0];
788 Dst = LLVMBuildExtractValue(Builder, Agg, I, Name);
789 break;
790 }
791 case LLVMInsertValue: {
792 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
793 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 1));
794 if (LLVMGetNumIndices(Src) > 1)
795 report_fatal_error("InsertValue: Expected only one index");
796 else if (LLVMGetNumIndices(Src) < 1)
797 report_fatal_error("InsertValue: Expected an index");
798 auto I = LLVMGetIndices(Src)[0];
799 Dst = LLVMBuildInsertValue(Builder, Agg, V, I, Name);
800 break;
801 }
802 case LLVMExtractElement: {
803 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
804 LLVMValueRef Index = CloneValue(LLVMGetOperand(Src, 1));
805 Dst = LLVMBuildExtractElement(Builder, Agg, Index, Name);
806 break;
807 }
808 case LLVMInsertElement: {
809 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
810 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 1));
811 LLVMValueRef Index = CloneValue(LLVMGetOperand(Src, 2));
812 Dst = LLVMBuildInsertElement(Builder, Agg, V, Index, Name);
813 break;
814 }
815 case LLVMShuffleVector: {
816 LLVMValueRef Agg0 = CloneValue(LLVMGetOperand(Src, 0));
817 LLVMValueRef Agg1 = CloneValue(LLVMGetOperand(Src, 1));
818 SmallVector<LLVMValueRef, 8> MaskElts;
819 unsigned NumMaskElts = LLVMGetNumMaskElements(Src);
820 for (unsigned i = 0; i < NumMaskElts; i++) {
821 int Val = LLVMGetMaskValue(Src, i);
822 if (Val == LLVMGetUndefMaskElem()) {
823 MaskElts.push_back(LLVMGetUndef(LLVMInt64Type()));
824 } else {
825 MaskElts.push_back(LLVMConstInt(LLVMInt64Type(), Val, true));
826 }
827 }
828 LLVMValueRef Mask = LLVMConstVector(MaskElts.data(), NumMaskElts);
829 Dst = LLVMBuildShuffleVector(Builder, Agg0, Agg1, Mask, Name);
830 break;
831 }
832 case LLVMFreeze: {
833 LLVMValueRef Arg = CloneValue(LLVMGetOperand(Src, 0));
834 Dst = LLVMBuildFreeze(Builder, Arg, Name);
835 break;
836 }
837 default:
838 break;
839 }
840
841 if (Dst == nullptr) {
842 fprintf(stderr, "%d is not a supported opcode\n", Op);
843 exit(-1);
844 }
845
846 auto Ctx = LLVMGetModuleContext(M);
847 size_t NumMetadataEntries;
848 auto *AllMetadata =
849 LLVMInstructionGetAllMetadataOtherThanDebugLoc(Src,
850 &NumMetadataEntries);
851 for (unsigned i = 0; i < NumMetadataEntries; ++i) {
852 unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i);
853 LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i);
854 LLVMSetMetadata(Dst, Kind, LLVMMetadataAsValue(Ctx, MD));
855 }
856 LLVMDisposeValueMetadataEntries(AllMetadata);
857 LLVMSetInstDebugLocation(Builder, Dst);
858
859 check_value_kind(Dst, LLVMInstructionValueKind);
860 return VMap[Src] = Dst;
861 }
862
DeclareBBFunCloner863 LLVMBasicBlockRef DeclareBB(LLVMBasicBlockRef Src) {
864 // Check if this is something we already computed.
865 {
866 auto i = BBMap.find(Src);
867 if (i != BBMap.end()) {
868 return i->second;
869 }
870 }
871
872 LLVMValueRef V = LLVMBasicBlockAsValue(Src);
873 if (!LLVMValueIsBasicBlock(V) || LLVMValueAsBasicBlock(V) != Src)
874 report_fatal_error("Basic block is not a basic block");
875
876 const char *Name = LLVMGetBasicBlockName(Src);
877 size_t NameLen;
878 const char *VName = LLVMGetValueName2(V, &NameLen);
879 if (Name != VName)
880 report_fatal_error("Basic block name mismatch");
881
882 LLVMBasicBlockRef BB = LLVMAppendBasicBlock(Fun, Name);
883 return BBMap[Src] = BB;
884 }
885
CloneBBFunCloner886 LLVMBasicBlockRef CloneBB(LLVMBasicBlockRef Src) {
887 LLVMBasicBlockRef BB = DeclareBB(Src);
888
889 // Make sure ordering is correct.
890 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Src);
891 if (Prev)
892 LLVMMoveBasicBlockAfter(BB, DeclareBB(Prev));
893
894 LLVMValueRef First = LLVMGetFirstInstruction(Src);
895 LLVMValueRef Last = LLVMGetLastInstruction(Src);
896
897 if (First == nullptr) {
898 if (Last != nullptr)
899 report_fatal_error("Has no first instruction, but last one");
900 return BB;
901 }
902
903 auto Ctx = LLVMGetModuleContext(M);
904 LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx);
905 LLVMPositionBuilderAtEnd(Builder, BB);
906
907 LLVMValueRef Cur = First;
908 LLVMValueRef Next = nullptr;
909 while(true) {
910 CloneInstruction(Cur, Builder);
911 Next = LLVMGetNextInstruction(Cur);
912 if (Next == nullptr) {
913 if (Cur != Last)
914 report_fatal_error("Final instruction does not match Last");
915 break;
916 }
917
918 LLVMValueRef Prev = LLVMGetPreviousInstruction(Next);
919 if (Prev != Cur)
920 report_fatal_error("Next.Previous instruction is not Current");
921
922 Cur = Next;
923 }
924
925 LLVMDisposeBuilder(Builder);
926 return BB;
927 }
928
CloneBBsFunCloner929 void CloneBBs(LLVMValueRef Src) {
930 unsigned Count = LLVMCountBasicBlocks(Src);
931 if (Count == 0)
932 return;
933
934 LLVMBasicBlockRef First = LLVMGetFirstBasicBlock(Src);
935 LLVMBasicBlockRef Last = LLVMGetLastBasicBlock(Src);
936
937 LLVMBasicBlockRef Cur = First;
938 LLVMBasicBlockRef Next = nullptr;
939 while(true) {
940 CloneBB(Cur);
941 Count--;
942 Next = LLVMGetNextBasicBlock(Cur);
943 if (Next == nullptr) {
944 if (Cur != Last)
945 report_fatal_error("Final basic block does not match Last");
946 break;
947 }
948
949 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Next);
950 if (Prev != Cur)
951 report_fatal_error("Next.Previous basic bloc is not Current");
952
953 Cur = Next;
954 }
955
956 if (Count != 0)
957 report_fatal_error("Basic block count does not match iterration");
958 }
959 };
960
declare_symbols(LLVMModuleRef Src,LLVMModuleRef M)961 static void declare_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
962 auto Ctx = LLVMGetModuleContext(M);
963
964 LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
965 LLVMValueRef End = LLVMGetLastGlobal(Src);
966
967 LLVMValueRef Cur = Begin;
968 LLVMValueRef Next = nullptr;
969 if (!Begin) {
970 if (End != nullptr)
971 report_fatal_error("Range has an end but no beginning");
972 goto FunDecl;
973 }
974
975 while (true) {
976 size_t NameLen;
977 const char *Name = LLVMGetValueName2(Cur, &NameLen);
978 if (LLVMGetNamedGlobal(M, Name))
979 report_fatal_error("GlobalVariable already cloned");
980 LLVMAddGlobal(M, LLVMGetElementType(TypeCloner(M).Clone(Cur)), Name);
981
982 Next = LLVMGetNextGlobal(Cur);
983 if (Next == nullptr) {
984 if (Cur != End)
985 report_fatal_error("");
986 break;
987 }
988
989 LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
990 if (Prev != Cur)
991 report_fatal_error("Next.Previous global is not Current");
992
993 Cur = Next;
994 }
995
996 FunDecl:
997 Begin = LLVMGetFirstFunction(Src);
998 End = LLVMGetLastFunction(Src);
999 if (!Begin) {
1000 if (End != nullptr)
1001 report_fatal_error("Range has an end but no beginning");
1002 goto AliasDecl;
1003 }
1004
1005 Cur = Begin;
1006 Next = nullptr;
1007 while (true) {
1008 size_t NameLen;
1009 const char *Name = LLVMGetValueName2(Cur, &NameLen);
1010 if (LLVMGetNamedFunction(M, Name))
1011 report_fatal_error("Function already cloned");
1012 auto Ty = LLVMGetElementType(TypeCloner(M).Clone(Cur));
1013 auto F = LLVMAddFunction(M, Name, Ty);
1014
1015 // Copy attributes
1016 for (int i = LLVMAttributeFunctionIndex, c = LLVMCountParams(F);
1017 i <= c; ++i) {
1018 for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) {
1019 if (auto SrcA = LLVMGetEnumAttributeAtIndex(Cur, i, k)) {
1020 auto Val = LLVMGetEnumAttributeValue(SrcA);
1021 auto DstA = LLVMCreateEnumAttribute(Ctx, k, Val);
1022 LLVMAddAttributeAtIndex(F, i, DstA);
1023 }
1024 }
1025 }
1026
1027 Next = LLVMGetNextFunction(Cur);
1028 if (Next == nullptr) {
1029 if (Cur != End)
1030 report_fatal_error("Last function does not match End");
1031 break;
1032 }
1033
1034 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
1035 if (Prev != Cur)
1036 report_fatal_error("Next.Previous function is not Current");
1037
1038 Cur = Next;
1039 }
1040
1041 AliasDecl:
1042 Begin = LLVMGetFirstGlobalAlias(Src);
1043 End = LLVMGetLastGlobalAlias(Src);
1044 if (!Begin) {
1045 if (End != nullptr)
1046 report_fatal_error("Range has an end but no beginning");
1047 goto GlobalIFuncDecl;
1048 }
1049
1050 Cur = Begin;
1051 Next = nullptr;
1052 while (true) {
1053 size_t NameLen;
1054 const char *Name = LLVMGetValueName2(Cur, &NameLen);
1055 if (LLVMGetNamedGlobalAlias(M, Name, NameLen))
1056 report_fatal_error("Global alias already cloned");
1057 LLVMTypeRef CurType = TypeCloner(M).Clone(Cur);
1058 // FIXME: Allow NULL aliasee.
1059 LLVMAddAlias(M, CurType, LLVMGetUndef(CurType), Name);
1060
1061 Next = LLVMGetNextGlobalAlias(Cur);
1062 if (Next == nullptr) {
1063 if (Cur != End)
1064 report_fatal_error("");
1065 break;
1066 }
1067
1068 LLVMValueRef Prev = LLVMGetPreviousGlobalAlias(Next);
1069 if (Prev != Cur)
1070 report_fatal_error("Next.Previous global is not Current");
1071
1072 Cur = Next;
1073 }
1074
1075 GlobalIFuncDecl:
1076 Begin = LLVMGetFirstGlobalIFunc(Src);
1077 End = LLVMGetLastGlobalIFunc(Src);
1078 if (!Begin) {
1079 if (End != nullptr)
1080 report_fatal_error("Range has an end but no beginning");
1081 goto NamedMDDecl;
1082 }
1083
1084 Cur = Begin;
1085 Next = nullptr;
1086 while (true) {
1087 size_t NameLen;
1088 const char *Name = LLVMGetValueName2(Cur, &NameLen);
1089 if (LLVMGetNamedGlobalIFunc(M, Name, NameLen))
1090 report_fatal_error("Global ifunc already cloned");
1091 LLVMTypeRef CurType = TypeCloner(M).Clone(LLVMGlobalGetValueType(Cur));
1092 // FIXME: Allow NULL resolver.
1093 LLVMAddGlobalIFunc(M, Name, NameLen,
1094 CurType, /*addressSpace*/ 0, LLVMGetUndef(CurType));
1095
1096 Next = LLVMGetNextGlobalIFunc(Cur);
1097 if (Next == nullptr) {
1098 if (Cur != End)
1099 report_fatal_error("");
1100 break;
1101 }
1102
1103 LLVMValueRef Prev = LLVMGetPreviousGlobalIFunc(Next);
1104 if (Prev != Cur)
1105 report_fatal_error("Next.Previous global is not Current");
1106
1107 Cur = Next;
1108 }
1109
1110 NamedMDDecl:
1111 LLVMNamedMDNodeRef BeginMD = LLVMGetFirstNamedMetadata(Src);
1112 LLVMNamedMDNodeRef EndMD = LLVMGetLastNamedMetadata(Src);
1113 if (!BeginMD) {
1114 if (EndMD != nullptr)
1115 report_fatal_error("Range has an end but no beginning");
1116 return;
1117 }
1118
1119 LLVMNamedMDNodeRef CurMD = BeginMD;
1120 LLVMNamedMDNodeRef NextMD = nullptr;
1121 while (true) {
1122 size_t NameLen;
1123 const char *Name = LLVMGetNamedMetadataName(CurMD, &NameLen);
1124 if (LLVMGetNamedMetadata(M, Name, NameLen))
1125 report_fatal_error("Named Metadata Node already cloned");
1126 LLVMGetOrInsertNamedMetadata(M, Name, NameLen);
1127
1128 NextMD = LLVMGetNextNamedMetadata(CurMD);
1129 if (NextMD == nullptr) {
1130 if (CurMD != EndMD)
1131 report_fatal_error("");
1132 break;
1133 }
1134
1135 LLVMNamedMDNodeRef PrevMD = LLVMGetPreviousNamedMetadata(NextMD);
1136 if (PrevMD != CurMD)
1137 report_fatal_error("Next.Previous global is not Current");
1138
1139 CurMD = NextMD;
1140 }
1141 }
1142
clone_symbols(LLVMModuleRef Src,LLVMModuleRef M)1143 static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
1144 LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
1145 LLVMValueRef End = LLVMGetLastGlobal(Src);
1146
1147 LLVMValueRef Cur = Begin;
1148 LLVMValueRef Next = nullptr;
1149 if (!Begin) {
1150 if (End != nullptr)
1151 report_fatal_error("Range has an end but no beginning");
1152 goto FunClone;
1153 }
1154
1155 while (true) {
1156 size_t NameLen;
1157 const char *Name = LLVMGetValueName2(Cur, &NameLen);
1158 LLVMValueRef G = LLVMGetNamedGlobal(M, Name);
1159 if (!G)
1160 report_fatal_error("GlobalVariable must have been declared already");
1161
1162 if (auto I = LLVMGetInitializer(Cur))
1163 LLVMSetInitializer(G, clone_constant(I, M));
1164
1165 size_t NumMetadataEntries;
1166 auto *AllMetadata = LLVMGlobalCopyAllMetadata(Cur, &NumMetadataEntries);
1167 for (unsigned i = 0; i < NumMetadataEntries; ++i) {
1168 unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i);
1169 LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i);
1170 LLVMGlobalSetMetadata(G, Kind, MD);
1171 }
1172 LLVMDisposeValueMetadataEntries(AllMetadata);
1173
1174 LLVMSetGlobalConstant(G, LLVMIsGlobalConstant(Cur));
1175 LLVMSetThreadLocal(G, LLVMIsThreadLocal(Cur));
1176 LLVMSetExternallyInitialized(G, LLVMIsExternallyInitialized(Cur));
1177 LLVMSetLinkage(G, LLVMGetLinkage(Cur));
1178 LLVMSetSection(G, LLVMGetSection(Cur));
1179 LLVMSetVisibility(G, LLVMGetVisibility(Cur));
1180 LLVMSetUnnamedAddress(G, LLVMGetUnnamedAddress(Cur));
1181 LLVMSetAlignment(G, LLVMGetAlignment(Cur));
1182
1183 Next = LLVMGetNextGlobal(Cur);
1184 if (Next == nullptr) {
1185 if (Cur != End)
1186 report_fatal_error("");
1187 break;
1188 }
1189
1190 LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
1191 if (Prev != Cur)
1192 report_fatal_error("Next.Previous global is not Current");
1193
1194 Cur = Next;
1195 }
1196
1197 FunClone:
1198 Begin = LLVMGetFirstFunction(Src);
1199 End = LLVMGetLastFunction(Src);
1200 if (!Begin) {
1201 if (End != nullptr)
1202 report_fatal_error("Range has an end but no beginning");
1203 goto AliasClone;
1204 }
1205
1206 Cur = Begin;
1207 Next = nullptr;
1208 while (true) {
1209 size_t NameLen;
1210 const char *Name = LLVMGetValueName2(Cur, &NameLen);
1211 LLVMValueRef Fun = LLVMGetNamedFunction(M, Name);
1212 if (!Fun)
1213 report_fatal_error("Function must have been declared already");
1214
1215 if (LLVMHasPersonalityFn(Cur)) {
1216 size_t FNameLen;
1217 const char *FName = LLVMGetValueName2(LLVMGetPersonalityFn(Cur),
1218 &FNameLen);
1219 LLVMValueRef P = LLVMGetNamedFunction(M, FName);
1220 if (!P)
1221 report_fatal_error("Could not find personality function");
1222 LLVMSetPersonalityFn(Fun, P);
1223 }
1224
1225 size_t NumMetadataEntries;
1226 auto *AllMetadata = LLVMGlobalCopyAllMetadata(Cur, &NumMetadataEntries);
1227 for (unsigned i = 0; i < NumMetadataEntries; ++i) {
1228 unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i);
1229 LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i);
1230 LLVMGlobalSetMetadata(Fun, Kind, MD);
1231 }
1232 LLVMDisposeValueMetadataEntries(AllMetadata);
1233
1234 FunCloner FC(Cur, Fun);
1235 FC.CloneBBs(Cur);
1236
1237 Next = LLVMGetNextFunction(Cur);
1238 if (Next == nullptr) {
1239 if (Cur != End)
1240 report_fatal_error("Last function does not match End");
1241 break;
1242 }
1243
1244 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
1245 if (Prev != Cur)
1246 report_fatal_error("Next.Previous function is not Current");
1247
1248 Cur = Next;
1249 }
1250
1251 AliasClone:
1252 Begin = LLVMGetFirstGlobalAlias(Src);
1253 End = LLVMGetLastGlobalAlias(Src);
1254 if (!Begin) {
1255 if (End != nullptr)
1256 report_fatal_error("Range has an end but no beginning");
1257 goto GlobalIFuncClone;
1258 }
1259
1260 Cur = Begin;
1261 Next = nullptr;
1262 while (true) {
1263 size_t NameLen;
1264 const char *Name = LLVMGetValueName2(Cur, &NameLen);
1265 LLVMValueRef Alias = LLVMGetNamedGlobalAlias(M, Name, NameLen);
1266 if (!Alias)
1267 report_fatal_error("Global alias must have been declared already");
1268
1269 if (LLVMValueRef Aliasee = LLVMAliasGetAliasee(Cur)) {
1270 LLVMAliasSetAliasee(Alias, clone_constant(Aliasee, M));
1271 }
1272
1273 LLVMSetLinkage(Alias, LLVMGetLinkage(Cur));
1274 LLVMSetUnnamedAddress(Alias, LLVMGetUnnamedAddress(Cur));
1275
1276 Next = LLVMGetNextGlobalAlias(Cur);
1277 if (Next == nullptr) {
1278 if (Cur != End)
1279 report_fatal_error("Last global alias does not match End");
1280 break;
1281 }
1282
1283 LLVMValueRef Prev = LLVMGetPreviousGlobalAlias(Next);
1284 if (Prev != Cur)
1285 report_fatal_error("Next.Previous global alias is not Current");
1286
1287 Cur = Next;
1288 }
1289
1290 GlobalIFuncClone:
1291 Begin = LLVMGetFirstGlobalIFunc(Src);
1292 End = LLVMGetLastGlobalIFunc(Src);
1293 if (!Begin) {
1294 if (End != nullptr)
1295 report_fatal_error("Range has an end but no beginning");
1296 goto NamedMDClone;
1297 }
1298
1299 Cur = Begin;
1300 Next = nullptr;
1301 while (true) {
1302 size_t NameLen;
1303 const char *Name = LLVMGetValueName2(Cur, &NameLen);
1304 LLVMValueRef IFunc = LLVMGetNamedGlobalIFunc(M, Name, NameLen);
1305 if (!IFunc)
1306 report_fatal_error("Global ifunc must have been declared already");
1307
1308 if (LLVMValueRef Resolver = LLVMGetGlobalIFuncResolver(Cur)) {
1309 LLVMSetGlobalIFuncResolver(IFunc, clone_constant(Resolver, M));
1310 }
1311
1312 LLVMSetLinkage(IFunc, LLVMGetLinkage(Cur));
1313 LLVMSetUnnamedAddress(IFunc, LLVMGetUnnamedAddress(Cur));
1314
1315 Next = LLVMGetNextGlobalIFunc(Cur);
1316 if (Next == nullptr) {
1317 if (Cur != End)
1318 report_fatal_error("Last global alias does not match End");
1319 break;
1320 }
1321
1322 LLVMValueRef Prev = LLVMGetPreviousGlobalIFunc(Next);
1323 if (Prev != Cur)
1324 report_fatal_error("Next.Previous global alias is not Current");
1325
1326 Cur = Next;
1327 }
1328
1329 NamedMDClone:
1330 LLVMNamedMDNodeRef BeginMD = LLVMGetFirstNamedMetadata(Src);
1331 LLVMNamedMDNodeRef EndMD = LLVMGetLastNamedMetadata(Src);
1332 if (!BeginMD) {
1333 if (EndMD != nullptr)
1334 report_fatal_error("Range has an end but no beginning");
1335 return;
1336 }
1337
1338 LLVMNamedMDNodeRef CurMD = BeginMD;
1339 LLVMNamedMDNodeRef NextMD = nullptr;
1340 while (true) {
1341 size_t NameLen;
1342 const char *Name = LLVMGetNamedMetadataName(CurMD, &NameLen);
1343 LLVMNamedMDNodeRef NamedMD = LLVMGetNamedMetadata(M, Name, NameLen);
1344 if (!NamedMD)
1345 report_fatal_error("Named MD Node must have been declared already");
1346
1347 unsigned OperandCount = LLVMGetNamedMetadataNumOperands(Src, Name);
1348 LLVMValueRef *OperandBuf = static_cast<LLVMValueRef *>(
1349 safe_malloc(OperandCount * sizeof(LLVMValueRef)));
1350 LLVMGetNamedMetadataOperands(Src, Name, OperandBuf);
1351 for (unsigned i = 0, e = OperandCount; i != e; ++i) {
1352 LLVMAddNamedMetadataOperand(M, Name, OperandBuf[i]);
1353 }
1354 free(OperandBuf);
1355
1356 NextMD = LLVMGetNextNamedMetadata(CurMD);
1357 if (NextMD == nullptr) {
1358 if (CurMD != EndMD)
1359 report_fatal_error("Last Named MD Node does not match End");
1360 break;
1361 }
1362
1363 LLVMNamedMDNodeRef PrevMD = LLVMGetPreviousNamedMetadata(NextMD);
1364 if (PrevMD != CurMD)
1365 report_fatal_error("Next.Previous Named MD Node is not Current");
1366
1367 CurMD = NextMD;
1368 }
1369 }
1370
llvm_echo(void)1371 int llvm_echo(void) {
1372 LLVMEnablePrettyStackTrace();
1373
1374 LLVMModuleRef Src = llvm_load_module(false, true);
1375 size_t SourceFileLen;
1376 const char *SourceFileName = LLVMGetSourceFileName(Src, &SourceFileLen);
1377 size_t ModuleIdentLen;
1378 const char *ModuleName = LLVMGetModuleIdentifier(Src, &ModuleIdentLen);
1379 LLVMContextRef Ctx = LLVMContextCreate();
1380 LLVMModuleRef M = LLVMModuleCreateWithNameInContext(ModuleName, Ctx);
1381
1382 LLVMSetSourceFileName(M, SourceFileName, SourceFileLen);
1383 LLVMSetModuleIdentifier(M, ModuleName, ModuleIdentLen);
1384
1385 LLVMSetTarget(M, LLVMGetTarget(Src));
1386 LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src));
1387 if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src)))
1388 report_fatal_error("Inconsistent DataLayout string representation");
1389
1390 size_t ModuleInlineAsmLen;
1391 const char *ModuleAsm = LLVMGetModuleInlineAsm(Src, &ModuleInlineAsmLen);
1392 LLVMSetModuleInlineAsm2(M, ModuleAsm, ModuleInlineAsmLen);
1393
1394 declare_symbols(Src, M);
1395 clone_symbols(Src, M);
1396 char *Str = LLVMPrintModuleToString(M);
1397 fputs(Str, stdout);
1398
1399 LLVMDisposeMessage(Str);
1400 LLVMDisposeModule(Src);
1401 LLVMDisposeModule(M);
1402 LLVMContextDispose(Ctx);
1403
1404 return 0;
1405 }
1406