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         break;
580       }
581       case LLVMStore: {
582         LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 0));
583         LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 1));
584         Dst = LLVMBuildStore(Builder, Val, Ptr);
585         LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
586         break;
587       }
588       case LLVMGetElementPtr: {
589         LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
590         SmallVector<LLVMValueRef, 8> Idx;
591         int NumIdx = LLVMGetNumIndices(Src);
592         for (int i = 1; i <= NumIdx; i++)
593           Idx.push_back(CloneValue(LLVMGetOperand(Src, i)));
594         if (LLVMIsInBounds(Src))
595           Dst = LLVMBuildInBoundsGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
596         else
597           Dst = LLVMBuildGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
598         break;
599       }
600       case LLVMAtomicCmpXchg: {
601         LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
602         LLVMValueRef Cmp = CloneValue(LLVMGetOperand(Src, 1));
603         LLVMValueRef New = CloneValue(LLVMGetOperand(Src, 2));
604         LLVMAtomicOrdering Succ = LLVMGetCmpXchgSuccessOrdering(Src);
605         LLVMAtomicOrdering Fail = LLVMGetCmpXchgFailureOrdering(Src);
606         LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src);
607 
608         Dst = LLVMBuildAtomicCmpXchg(Builder, Ptr, Cmp, New, Succ, Fail,
609                                      SingleThread);
610       } break;
611       case LLVMBitCast: {
612         LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 0));
613         Dst = LLVMBuildBitCast(Builder, V, CloneType(Src), Name);
614         break;
615       }
616       case LLVMICmp: {
617         LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src);
618         LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
619         LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
620         Dst = LLVMBuildICmp(Builder, Pred, LHS, RHS, Name);
621         break;
622       }
623       case LLVMPHI: {
624         // We need to aggressively set things here because of loops.
625         VMap[Src] = Dst = LLVMBuildPhi(Builder, CloneType(Src), Name);
626 
627         SmallVector<LLVMValueRef, 8> Values;
628         SmallVector<LLVMBasicBlockRef, 8> Blocks;
629 
630         unsigned IncomingCount = LLVMCountIncoming(Src);
631         for (unsigned i = 0; i < IncomingCount; ++i) {
632           Blocks.push_back(DeclareBB(LLVMGetIncomingBlock(Src, i)));
633           Values.push_back(CloneValue(LLVMGetIncomingValue(Src, i)));
634         }
635 
636         LLVMAddIncoming(Dst, Values.data(), Blocks.data(), IncomingCount);
637         return Dst;
638       }
639       case LLVMCall: {
640         SmallVector<LLVMValueRef, 8> Args;
641         int ArgCount = LLVMGetNumArgOperands(Src);
642         for (int i = 0; i < ArgCount; i++)
643           Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
644         LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
645         Dst = LLVMBuildCall(Builder, Fn, Args.data(), ArgCount, Name);
646         LLVMSetTailCall(Dst, LLVMIsTailCall(Src));
647         CloneAttrs(Src, Dst);
648         break;
649       }
650       case LLVMResume: {
651         Dst = LLVMBuildResume(Builder, CloneValue(LLVMGetOperand(Src, 0)));
652         break;
653       }
654       case LLVMLandingPad: {
655         // The landing pad API is a bit screwed up for historical reasons.
656         Dst = LLVMBuildLandingPad(Builder, CloneType(Src), nullptr, 0, Name);
657         unsigned NumClauses = LLVMGetNumClauses(Src);
658         for (unsigned i = 0; i < NumClauses; ++i)
659           LLVMAddClause(Dst, CloneValue(LLVMGetClause(Src, i)));
660         LLVMSetCleanup(Dst, LLVMIsCleanup(Src));
661         break;
662       }
663       case LLVMCleanupRet: {
664         LLVMValueRef CatchPad = CloneValue(LLVMGetOperand(Src, 0));
665         LLVMBasicBlockRef Unwind = nullptr;
666         if (LLVMBasicBlockRef UDest = LLVMGetUnwindDest(Src))
667           Unwind = DeclareBB(UDest);
668         Dst = LLVMBuildCleanupRet(Builder, CatchPad, Unwind);
669         break;
670       }
671       case LLVMCatchRet: {
672         LLVMValueRef CatchPad = CloneValue(LLVMGetOperand(Src, 0));
673         LLVMBasicBlockRef SuccBB = DeclareBB(LLVMGetSuccessor(Src, 0));
674         Dst = LLVMBuildCatchRet(Builder, CatchPad, SuccBB);
675         break;
676       }
677       case LLVMCatchPad: {
678         LLVMValueRef ParentPad = CloneValue(LLVMGetParentCatchSwitch(Src));
679         SmallVector<LLVMValueRef, 8> Args;
680         int ArgCount = LLVMGetNumArgOperands(Src);
681         for (int i = 0; i < ArgCount; i++)
682           Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
683         Dst = LLVMBuildCatchPad(Builder, ParentPad,
684                                 Args.data(), ArgCount, Name);
685         break;
686       }
687       case LLVMCleanupPad: {
688         LLVMValueRef ParentPad = CloneValue(LLVMGetOperand(Src, 0));
689         SmallVector<LLVMValueRef, 8> Args;
690         int ArgCount = LLVMGetNumArgOperands(Src);
691         for (int i = 0; i < ArgCount; i++)
692           Args.push_back(CloneValue(LLVMGetArgOperand(Src, i)));
693         Dst = LLVMBuildCleanupPad(Builder, ParentPad,
694                                   Args.data(), ArgCount, Name);
695         break;
696       }
697       case LLVMCatchSwitch: {
698         LLVMValueRef ParentPad = CloneValue(LLVMGetOperand(Src, 0));
699         LLVMBasicBlockRef UnwindBB = nullptr;
700         if (LLVMBasicBlockRef UDest = LLVMGetUnwindDest(Src)) {
701           UnwindBB = DeclareBB(UDest);
702         }
703         unsigned NumHandlers = LLVMGetNumHandlers(Src);
704         Dst = LLVMBuildCatchSwitch(Builder, ParentPad, UnwindBB, NumHandlers, Name);
705         if (NumHandlers > 0) {
706           LLVMBasicBlockRef *Handlers = static_cast<LLVMBasicBlockRef*>(
707                        safe_malloc(NumHandlers * sizeof(LLVMBasicBlockRef)));
708           LLVMGetHandlers(Src, Handlers);
709           for (unsigned i = 0; i < NumHandlers; i++)
710             LLVMAddHandler(Dst, DeclareBB(Handlers[i]));
711           free(Handlers);
712         }
713         break;
714       }
715       case LLVMExtractValue: {
716         LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
717         if (LLVMGetNumIndices(Src) != 1)
718           report_fatal_error("Expected only one indice");
719         auto I = LLVMGetIndices(Src)[0];
720         Dst = LLVMBuildExtractValue(Builder, Agg, I, Name);
721         break;
722       }
723       case LLVMInsertValue: {
724         LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
725         LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 1));
726         if (LLVMGetNumIndices(Src) != 1)
727           report_fatal_error("Expected only one indice");
728         auto I = LLVMGetIndices(Src)[0];
729         Dst = LLVMBuildInsertValue(Builder, Agg, V, I, Name);
730         break;
731       }
732       default:
733         break;
734     }
735 
736     if (Dst == nullptr) {
737       fprintf(stderr, "%d is not a supported opcode\n", Op);
738       exit(-1);
739     }
740 
741     auto Ctx = LLVMGetModuleContext(M);
742     size_t NumMetadataEntries;
743     auto *AllMetadata =
744         LLVMInstructionGetAllMetadataOtherThanDebugLoc(Src,
745                                                        &NumMetadataEntries);
746     for (unsigned i = 0; i < NumMetadataEntries; ++i) {
747       unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i);
748       LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i);
749       LLVMSetMetadata(Dst, Kind, LLVMMetadataAsValue(Ctx, MD));
750     }
751     LLVMDisposeValueMetadataEntries(AllMetadata);
752     LLVMSetInstDebugLocation(Builder, Dst);
753 
754     check_value_kind(Dst, LLVMInstructionValueKind);
755     return VMap[Src] = Dst;
756   }
757 
758   LLVMBasicBlockRef DeclareBB(LLVMBasicBlockRef Src) {
759     // Check if this is something we already computed.
760     {
761       auto i = BBMap.find(Src);
762       if (i != BBMap.end()) {
763         return i->second;
764       }
765     }
766 
767     LLVMValueRef V = LLVMBasicBlockAsValue(Src);
768     if (!LLVMValueIsBasicBlock(V) || LLVMValueAsBasicBlock(V) != Src)
769       report_fatal_error("Basic block is not a basic block");
770 
771     const char *Name = LLVMGetBasicBlockName(Src);
772     size_t NameLen;
773     const char *VName = LLVMGetValueName2(V, &NameLen);
774     if (Name != VName)
775       report_fatal_error("Basic block name mismatch");
776 
777     LLVMBasicBlockRef BB = LLVMAppendBasicBlock(Fun, Name);
778     return BBMap[Src] = BB;
779   }
780 
781   LLVMBasicBlockRef CloneBB(LLVMBasicBlockRef Src) {
782     LLVMBasicBlockRef BB = DeclareBB(Src);
783 
784     // Make sure ordering is correct.
785     LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Src);
786     if (Prev)
787       LLVMMoveBasicBlockAfter(BB, DeclareBB(Prev));
788 
789     LLVMValueRef First = LLVMGetFirstInstruction(Src);
790     LLVMValueRef Last = LLVMGetLastInstruction(Src);
791 
792     if (First == nullptr) {
793       if (Last != nullptr)
794         report_fatal_error("Has no first instruction, but last one");
795       return BB;
796     }
797 
798     auto Ctx = LLVMGetModuleContext(M);
799     LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx);
800     LLVMPositionBuilderAtEnd(Builder, BB);
801 
802     LLVMValueRef Cur = First;
803     LLVMValueRef Next = nullptr;
804     while(true) {
805       CloneInstruction(Cur, Builder);
806       Next = LLVMGetNextInstruction(Cur);
807       if (Next == nullptr) {
808         if (Cur != Last)
809           report_fatal_error("Final instruction does not match Last");
810         break;
811       }
812 
813       LLVMValueRef Prev = LLVMGetPreviousInstruction(Next);
814       if (Prev != Cur)
815         report_fatal_error("Next.Previous instruction is not Current");
816 
817       Cur = Next;
818     }
819 
820     LLVMDisposeBuilder(Builder);
821     return BB;
822   }
823 
824   void CloneBBs(LLVMValueRef Src) {
825     unsigned Count = LLVMCountBasicBlocks(Src);
826     if (Count == 0)
827       return;
828 
829     LLVMBasicBlockRef First = LLVMGetFirstBasicBlock(Src);
830     LLVMBasicBlockRef Last = LLVMGetLastBasicBlock(Src);
831 
832     LLVMBasicBlockRef Cur = First;
833     LLVMBasicBlockRef Next = nullptr;
834     while(true) {
835       CloneBB(Cur);
836       Count--;
837       Next = LLVMGetNextBasicBlock(Cur);
838       if (Next == nullptr) {
839         if (Cur != Last)
840           report_fatal_error("Final basic block does not match Last");
841         break;
842       }
843 
844       LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Next);
845       if (Prev != Cur)
846         report_fatal_error("Next.Previous basic bloc is not Current");
847 
848       Cur = Next;
849     }
850 
851     if (Count != 0)
852       report_fatal_error("Basic block count does not match iterration");
853   }
854 };
855 
856 static void declare_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
857   auto Ctx = LLVMGetModuleContext(M);
858 
859   LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
860   LLVMValueRef End = LLVMGetLastGlobal(Src);
861 
862   LLVMValueRef Cur = Begin;
863   LLVMValueRef Next = nullptr;
864   if (!Begin) {
865     if (End != nullptr)
866       report_fatal_error("Range has an end but no beginning");
867     goto FunDecl;
868   }
869 
870   while (true) {
871     size_t NameLen;
872     const char *Name = LLVMGetValueName2(Cur, &NameLen);
873     if (LLVMGetNamedGlobal(M, Name))
874       report_fatal_error("GlobalVariable already cloned");
875     LLVMAddGlobal(M, LLVMGetElementType(TypeCloner(M).Clone(Cur)), Name);
876 
877     Next = LLVMGetNextGlobal(Cur);
878     if (Next == nullptr) {
879       if (Cur != End)
880         report_fatal_error("");
881       break;
882     }
883 
884     LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
885     if (Prev != Cur)
886       report_fatal_error("Next.Previous global is not Current");
887 
888     Cur = Next;
889   }
890 
891 FunDecl:
892   Begin = LLVMGetFirstFunction(Src);
893   End = LLVMGetLastFunction(Src);
894   if (!Begin) {
895     if (End != nullptr)
896       report_fatal_error("Range has an end but no beginning");
897     goto AliasDecl;
898   }
899 
900   Cur = Begin;
901   Next = nullptr;
902   while (true) {
903     size_t NameLen;
904     const char *Name = LLVMGetValueName2(Cur, &NameLen);
905     if (LLVMGetNamedFunction(M, Name))
906       report_fatal_error("Function already cloned");
907     auto Ty = LLVMGetElementType(TypeCloner(M).Clone(Cur));
908     auto F = LLVMAddFunction(M, Name, Ty);
909 
910     // Copy attributes
911     for (int i = LLVMAttributeFunctionIndex, c = LLVMCountParams(F);
912          i <= c; ++i) {
913       for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) {
914         if (auto SrcA = LLVMGetEnumAttributeAtIndex(Cur, i, k)) {
915           auto Val = LLVMGetEnumAttributeValue(SrcA);
916           auto DstA = LLVMCreateEnumAttribute(Ctx, k, Val);
917           LLVMAddAttributeAtIndex(F, i, DstA);
918         }
919       }
920     }
921 
922     Next = LLVMGetNextFunction(Cur);
923     if (Next == nullptr) {
924       if (Cur != End)
925         report_fatal_error("Last function does not match End");
926       break;
927     }
928 
929     LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
930     if (Prev != Cur)
931       report_fatal_error("Next.Previous function is not Current");
932 
933     Cur = Next;
934   }
935 
936 AliasDecl:
937   Begin = LLVMGetFirstGlobalAlias(Src);
938   End = LLVMGetLastGlobalAlias(Src);
939   if (!Begin) {
940     if (End != nullptr)
941       report_fatal_error("Range has an end but no beginning");
942     goto GlobalIFuncDecl;
943   }
944 
945   Cur = Begin;
946   Next = nullptr;
947   while (true) {
948     size_t NameLen;
949     const char *Name = LLVMGetValueName2(Cur, &NameLen);
950     if (LLVMGetNamedGlobalAlias(M, Name, NameLen))
951       report_fatal_error("Global alias already cloned");
952     LLVMTypeRef CurType = TypeCloner(M).Clone(Cur);
953     // FIXME: Allow NULL aliasee.
954     LLVMAddAlias(M, CurType, LLVMGetUndef(CurType), Name);
955 
956     Next = LLVMGetNextGlobalAlias(Cur);
957     if (Next == nullptr) {
958       if (Cur != End)
959         report_fatal_error("");
960       break;
961     }
962 
963     LLVMValueRef Prev = LLVMGetPreviousGlobalAlias(Next);
964     if (Prev != Cur)
965       report_fatal_error("Next.Previous global is not Current");
966 
967     Cur = Next;
968   }
969 
970 GlobalIFuncDecl:
971   Begin = LLVMGetFirstGlobalIFunc(Src);
972   End = LLVMGetLastGlobalIFunc(Src);
973   if (!Begin) {
974     if (End != nullptr)
975       report_fatal_error("Range has an end but no beginning");
976     goto NamedMDDecl;
977   }
978 
979   Cur = Begin;
980   Next = nullptr;
981   while (true) {
982     size_t NameLen;
983     const char *Name = LLVMGetValueName2(Cur, &NameLen);
984     if (LLVMGetNamedGlobalIFunc(M, Name, NameLen))
985       report_fatal_error("Global ifunc already cloned");
986     LLVMTypeRef CurType = TypeCloner(M).Clone(LLVMGlobalGetValueType(Cur));
987     // FIXME: Allow NULL resolver.
988     LLVMAddGlobalIFunc(M, Name, NameLen,
989                        CurType, /*addressSpace*/ 0, LLVMGetUndef(CurType));
990 
991     Next = LLVMGetNextGlobalIFunc(Cur);
992     if (Next == nullptr) {
993       if (Cur != End)
994         report_fatal_error("");
995       break;
996     }
997 
998     LLVMValueRef Prev = LLVMGetPreviousGlobalIFunc(Next);
999     if (Prev != Cur)
1000       report_fatal_error("Next.Previous global is not Current");
1001 
1002     Cur = Next;
1003   }
1004 
1005 NamedMDDecl:
1006   LLVMNamedMDNodeRef BeginMD = LLVMGetFirstNamedMetadata(Src);
1007   LLVMNamedMDNodeRef EndMD = LLVMGetLastNamedMetadata(Src);
1008   if (!BeginMD) {
1009     if (EndMD != nullptr)
1010       report_fatal_error("Range has an end but no beginning");
1011     return;
1012   }
1013 
1014   LLVMNamedMDNodeRef CurMD = BeginMD;
1015   LLVMNamedMDNodeRef NextMD = nullptr;
1016   while (true) {
1017     size_t NameLen;
1018     const char *Name = LLVMGetNamedMetadataName(CurMD, &NameLen);
1019     if (LLVMGetNamedMetadata(M, Name, NameLen))
1020       report_fatal_error("Named Metadata Node already cloned");
1021     LLVMGetOrInsertNamedMetadata(M, Name, NameLen);
1022 
1023     NextMD = LLVMGetNextNamedMetadata(CurMD);
1024     if (NextMD == nullptr) {
1025       if (CurMD != EndMD)
1026         report_fatal_error("");
1027       break;
1028     }
1029 
1030     LLVMNamedMDNodeRef PrevMD = LLVMGetPreviousNamedMetadata(NextMD);
1031     if (PrevMD != CurMD)
1032       report_fatal_error("Next.Previous global is not Current");
1033 
1034     CurMD = NextMD;
1035   }
1036 }
1037 
1038 static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
1039   LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
1040   LLVMValueRef End = LLVMGetLastGlobal(Src);
1041 
1042   LLVMValueRef Cur = Begin;
1043   LLVMValueRef Next = nullptr;
1044   if (!Begin) {
1045     if (End != nullptr)
1046       report_fatal_error("Range has an end but no beginning");
1047     goto FunClone;
1048   }
1049 
1050   while (true) {
1051     size_t NameLen;
1052     const char *Name = LLVMGetValueName2(Cur, &NameLen);
1053     LLVMValueRef G = LLVMGetNamedGlobal(M, Name);
1054     if (!G)
1055       report_fatal_error("GlobalVariable must have been declared already");
1056 
1057     if (auto I = LLVMGetInitializer(Cur))
1058       LLVMSetInitializer(G, clone_constant(I, M));
1059 
1060     size_t NumMetadataEntries;
1061     auto *AllMetadata = LLVMGlobalCopyAllMetadata(Cur, &NumMetadataEntries);
1062     for (unsigned i = 0; i < NumMetadataEntries; ++i) {
1063       unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i);
1064       LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i);
1065       LLVMGlobalSetMetadata(G, Kind, MD);
1066     }
1067     LLVMDisposeValueMetadataEntries(AllMetadata);
1068 
1069     LLVMSetGlobalConstant(G, LLVMIsGlobalConstant(Cur));
1070     LLVMSetThreadLocal(G, LLVMIsThreadLocal(Cur));
1071     LLVMSetExternallyInitialized(G, LLVMIsExternallyInitialized(Cur));
1072     LLVMSetLinkage(G, LLVMGetLinkage(Cur));
1073     LLVMSetSection(G, LLVMGetSection(Cur));
1074     LLVMSetVisibility(G, LLVMGetVisibility(Cur));
1075     LLVMSetUnnamedAddress(G, LLVMGetUnnamedAddress(Cur));
1076     LLVMSetAlignment(G, LLVMGetAlignment(Cur));
1077 
1078     Next = LLVMGetNextGlobal(Cur);
1079     if (Next == nullptr) {
1080       if (Cur != End)
1081         report_fatal_error("");
1082       break;
1083     }
1084 
1085     LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
1086     if (Prev != Cur)
1087       report_fatal_error("Next.Previous global is not Current");
1088 
1089     Cur = Next;
1090   }
1091 
1092 FunClone:
1093   Begin = LLVMGetFirstFunction(Src);
1094   End = LLVMGetLastFunction(Src);
1095   if (!Begin) {
1096     if (End != nullptr)
1097       report_fatal_error("Range has an end but no beginning");
1098     goto AliasClone;
1099   }
1100 
1101   Cur = Begin;
1102   Next = nullptr;
1103   while (true) {
1104     size_t NameLen;
1105     const char *Name = LLVMGetValueName2(Cur, &NameLen);
1106     LLVMValueRef Fun = LLVMGetNamedFunction(M, Name);
1107     if (!Fun)
1108       report_fatal_error("Function must have been declared already");
1109 
1110     if (LLVMHasPersonalityFn(Cur)) {
1111       size_t FNameLen;
1112       const char *FName = LLVMGetValueName2(LLVMGetPersonalityFn(Cur),
1113                                            &FNameLen);
1114       LLVMValueRef P = LLVMGetNamedFunction(M, FName);
1115       if (!P)
1116         report_fatal_error("Could not find personality function");
1117       LLVMSetPersonalityFn(Fun, P);
1118     }
1119 
1120     size_t NumMetadataEntries;
1121     auto *AllMetadata = LLVMGlobalCopyAllMetadata(Cur, &NumMetadataEntries);
1122     for (unsigned i = 0; i < NumMetadataEntries; ++i) {
1123       unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i);
1124       LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i);
1125       LLVMGlobalSetMetadata(Fun, Kind, MD);
1126     }
1127     LLVMDisposeValueMetadataEntries(AllMetadata);
1128 
1129     FunCloner FC(Cur, Fun);
1130     FC.CloneBBs(Cur);
1131 
1132     Next = LLVMGetNextFunction(Cur);
1133     if (Next == nullptr) {
1134       if (Cur != End)
1135         report_fatal_error("Last function does not match End");
1136       break;
1137     }
1138 
1139     LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
1140     if (Prev != Cur)
1141       report_fatal_error("Next.Previous function is not Current");
1142 
1143     Cur = Next;
1144   }
1145 
1146 AliasClone:
1147   Begin = LLVMGetFirstGlobalAlias(Src);
1148   End = LLVMGetLastGlobalAlias(Src);
1149   if (!Begin) {
1150     if (End != nullptr)
1151       report_fatal_error("Range has an end but no beginning");
1152     goto GlobalIFuncClone;
1153   }
1154 
1155   Cur = Begin;
1156   Next = nullptr;
1157   while (true) {
1158     size_t NameLen;
1159     const char *Name = LLVMGetValueName2(Cur, &NameLen);
1160     LLVMValueRef Alias = LLVMGetNamedGlobalAlias(M, Name, NameLen);
1161     if (!Alias)
1162       report_fatal_error("Global alias must have been declared already");
1163 
1164     if (LLVMValueRef Aliasee = LLVMAliasGetAliasee(Cur)) {
1165       LLVMAliasSetAliasee(Alias, clone_constant(Aliasee, M));
1166     }
1167 
1168     LLVMSetLinkage(Alias, LLVMGetLinkage(Cur));
1169     LLVMSetUnnamedAddress(Alias, LLVMGetUnnamedAddress(Cur));
1170 
1171     Next = LLVMGetNextGlobalAlias(Cur);
1172     if (Next == nullptr) {
1173       if (Cur != End)
1174         report_fatal_error("Last global alias does not match End");
1175       break;
1176     }
1177 
1178     LLVMValueRef Prev = LLVMGetPreviousGlobalAlias(Next);
1179     if (Prev != Cur)
1180       report_fatal_error("Next.Previous global alias is not Current");
1181 
1182     Cur = Next;
1183   }
1184 
1185 GlobalIFuncClone:
1186   Begin = LLVMGetFirstGlobalIFunc(Src);
1187   End = LLVMGetLastGlobalIFunc(Src);
1188   if (!Begin) {
1189     if (End != nullptr)
1190       report_fatal_error("Range has an end but no beginning");
1191     goto NamedMDClone;
1192   }
1193 
1194   Cur = Begin;
1195   Next = nullptr;
1196   while (true) {
1197     size_t NameLen;
1198     const char *Name = LLVMGetValueName2(Cur, &NameLen);
1199     LLVMValueRef IFunc = LLVMGetNamedGlobalIFunc(M, Name, NameLen);
1200     if (!IFunc)
1201       report_fatal_error("Global ifunc must have been declared already");
1202 
1203     if (LLVMValueRef Resolver = LLVMGetGlobalIFuncResolver(Cur)) {
1204       LLVMSetGlobalIFuncResolver(IFunc, clone_constant(Resolver, M));
1205     }
1206 
1207     LLVMSetLinkage(IFunc, LLVMGetLinkage(Cur));
1208     LLVMSetUnnamedAddress(IFunc, LLVMGetUnnamedAddress(Cur));
1209 
1210     Next = LLVMGetNextGlobalIFunc(Cur);
1211     if (Next == nullptr) {
1212       if (Cur != End)
1213         report_fatal_error("Last global alias does not match End");
1214       break;
1215     }
1216 
1217     LLVMValueRef Prev = LLVMGetPreviousGlobalIFunc(Next);
1218     if (Prev != Cur)
1219       report_fatal_error("Next.Previous global alias is not Current");
1220 
1221     Cur = Next;
1222   }
1223 
1224 NamedMDClone:
1225   LLVMNamedMDNodeRef BeginMD = LLVMGetFirstNamedMetadata(Src);
1226   LLVMNamedMDNodeRef EndMD = LLVMGetLastNamedMetadata(Src);
1227   if (!BeginMD) {
1228     if (EndMD != nullptr)
1229       report_fatal_error("Range has an end but no beginning");
1230     return;
1231   }
1232 
1233   LLVMNamedMDNodeRef CurMD = BeginMD;
1234   LLVMNamedMDNodeRef NextMD = nullptr;
1235   while (true) {
1236     size_t NameLen;
1237     const char *Name = LLVMGetNamedMetadataName(CurMD, &NameLen);
1238     LLVMNamedMDNodeRef NamedMD = LLVMGetNamedMetadata(M, Name, NameLen);
1239     if (!NamedMD)
1240       report_fatal_error("Named MD Node must have been declared already");
1241 
1242     unsigned OperandCount = LLVMGetNamedMetadataNumOperands(Src, Name);
1243     LLVMValueRef *OperandBuf = static_cast<LLVMValueRef *>(
1244               safe_malloc(OperandCount * sizeof(LLVMValueRef)));
1245     LLVMGetNamedMetadataOperands(Src, Name, OperandBuf);
1246     for (unsigned i = 0, e = OperandCount; i != e; ++i) {
1247       LLVMAddNamedMetadataOperand(M, Name, OperandBuf[i]);
1248     }
1249     free(OperandBuf);
1250 
1251     NextMD = LLVMGetNextNamedMetadata(CurMD);
1252     if (NextMD == nullptr) {
1253       if (CurMD != EndMD)
1254         report_fatal_error("Last Named MD Node does not match End");
1255       break;
1256     }
1257 
1258     LLVMNamedMDNodeRef PrevMD = LLVMGetPreviousNamedMetadata(NextMD);
1259     if (PrevMD != CurMD)
1260       report_fatal_error("Next.Previous Named MD Node is not Current");
1261 
1262     CurMD = NextMD;
1263   }
1264 }
1265 
1266 int llvm_echo(void) {
1267   LLVMEnablePrettyStackTrace();
1268 
1269   LLVMModuleRef Src = llvm_load_module(false, true);
1270   size_t SourceFileLen;
1271   const char *SourceFileName = LLVMGetSourceFileName(Src, &SourceFileLen);
1272   size_t ModuleIdentLen;
1273   const char *ModuleName = LLVMGetModuleIdentifier(Src, &ModuleIdentLen);
1274   LLVMContextRef Ctx = LLVMContextCreate();
1275   LLVMModuleRef M = LLVMModuleCreateWithNameInContext(ModuleName, Ctx);
1276 
1277   LLVMSetSourceFileName(M, SourceFileName, SourceFileLen);
1278   LLVMSetModuleIdentifier(M, ModuleName, ModuleIdentLen);
1279 
1280   LLVMSetTarget(M, LLVMGetTarget(Src));
1281   LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src));
1282   if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src)))
1283     report_fatal_error("Inconsistent DataLayout string representation");
1284 
1285   size_t ModuleInlineAsmLen;
1286   const char *ModuleAsm = LLVMGetModuleInlineAsm(Src, &ModuleInlineAsmLen);
1287   LLVMSetModuleInlineAsm2(M, ModuleAsm, ModuleInlineAsmLen);
1288 
1289   declare_symbols(Src, M);
1290   clone_symbols(Src, M);
1291   char *Str = LLVMPrintModuleToString(M);
1292   fputs(Str, stdout);
1293 
1294   LLVMDisposeMessage(Str);
1295   LLVMDisposeModule(Src);
1296   LLVMDisposeModule(M);
1297   LLVMContextDispose(Ctx);
1298 
1299   return 0;
1300 }
1301