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