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