1 //===-- echo.cpp - tool for testing libLLVM and llvm-c API ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the --echo command in llvm-c-test.
10 //
11 // This command uses the C API to read a module and output an exact copy of it
12 // as output. It is used to check that the resulting module matches the input
13 // to validate that the C API can read and write modules properly.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #include "llvm-c-test.h"
18 #include "llvm-c/DebugInfo.h"
19 #include "llvm-c/Target.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/Support/ErrorHandling.h"
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 
26 using namespace llvm;
27 
28 // Provide DenseMapInfo for C API opaque types.
29 template<typename T>
30 struct CAPIDenseMap {};
31 
32 // The default DenseMapInfo require to know about pointer alignment.
33 // Because the C API uses opaques pointer types, their alignment is unknown.
34 // As a result, we need to roll out our own implementation.
35 template<typename T>
36 struct CAPIDenseMap<T*> {
37   struct CAPIDenseMapInfo {
getEmptyKeyCAPIDenseMap::CAPIDenseMapInfo38     static inline T* getEmptyKey() {
39       uintptr_t Val = static_cast<uintptr_t>(-1);
40       return reinterpret_cast<T*>(Val);
41     }
getTombstoneKeyCAPIDenseMap::CAPIDenseMapInfo42     static inline T* getTombstoneKey() {
43       uintptr_t Val = static_cast<uintptr_t>(-2);
44       return reinterpret_cast<T*>(Val);
45     }
getHashValueCAPIDenseMap::CAPIDenseMapInfo46     static unsigned getHashValue(const T *PtrVal) {
47       return hash_value(PtrVal);
48     }
isEqualCAPIDenseMap::CAPIDenseMapInfo49     static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; }
50   };
51 
52   typedef DenseMap<T*, T*, CAPIDenseMapInfo> Map;
53 };
54 
55 typedef CAPIDenseMap<LLVMValueRef>::Map ValueMap;
56 typedef CAPIDenseMap<LLVMBasicBlockRef>::Map BasicBlockMap;
57 
58 struct TypeCloner {
59   LLVMModuleRef M;
60   LLVMContextRef Ctx;
61 
TypeClonerTypeCloner62   TypeCloner(LLVMModuleRef M): M(M), Ctx(LLVMGetModuleContext(M)) {}
63 
CloneTypeCloner64   LLVMTypeRef Clone(LLVMValueRef Src) {
65     return Clone(LLVMTypeOf(Src));
66   }
67 
CloneTypeCloner68   LLVMTypeRef Clone(LLVMTypeRef Src) {
69     LLVMTypeKind Kind = LLVMGetTypeKind(Src);
70     switch (Kind) {
71       case LLVMVoidTypeKind:
72         return LLVMVoidTypeInContext(Ctx);
73       case LLVMHalfTypeKind:
74         return LLVMHalfTypeInContext(Ctx);
75       case 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 
clone_params(LLVMValueRef Src,LLVMValueRef Dst)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 
check_value_kind(LLVMValueRef V,LLVMValueKind K)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 
clone_constant(LLVMValueRef Cst,LLVMModuleRef M)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 
clone_constant_impl(LLVMValueRef Cst,LLVMModuleRef M)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 ConstantPointerNull
330   if (LLVMIsAConstantPointerNull(Cst)) {
331     check_value_kind(Cst, LLVMConstantPointerNullValueKind);
332     LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
333     return LLVMConstNull(Ty);
334   }
335 
336   // Try undef
337   if (LLVMIsUndef(Cst)) {
338     check_value_kind(Cst, LLVMUndefValueValueKind);
339     return LLVMGetUndef(TypeCloner(M).Clone(Cst));
340   }
341 
342   // Try null
343   if (LLVMIsNull(Cst)) {
344     check_value_kind(Cst, LLVMConstantTokenNoneValueKind);
345     LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
346     return LLVMConstNull(Ty);
347   }
348 
349   // Try float literal
350   if (LLVMIsAConstantFP(Cst)) {
351     check_value_kind(Cst, LLVMConstantFPValueKind);
352     report_fatal_error("ConstantFP is not supported");
353   }
354 
355   // This kind of constant is not supported
356   if (!LLVMIsAConstantExpr(Cst))
357     report_fatal_error("Expected a constant expression");
358 
359   // At this point, it must be a constant expression
360   check_value_kind(Cst, LLVMConstantExprValueKind);
361 
362   LLVMOpcode Op = LLVMGetConstOpcode(Cst);
363   switch(Op) {
364     case LLVMBitCast:
365       return LLVMConstBitCast(clone_constant(LLVMGetOperand(Cst, 0), M),
366                               TypeCloner(M).Clone(Cst));
367     default:
368       fprintf(stderr, "%d is not a supported opcode\n", Op);
369       exit(-1);
370   }
371 }
372 
373 struct FunCloner {
374   LLVMValueRef Fun;
375   LLVMModuleRef M;
376 
377   ValueMap VMap;
378   BasicBlockMap BBMap;
379 
FunClonerFunCloner380   FunCloner(LLVMValueRef Src, LLVMValueRef Dst): Fun(Dst),
381     M(LLVMGetGlobalParent(Fun)), VMap(clone_params(Src, Dst)) {}
382 
CloneTypeFunCloner383   LLVMTypeRef CloneType(LLVMTypeRef Src) {
384     return TypeCloner(M).Clone(Src);
385   }
386 
CloneTypeFunCloner387   LLVMTypeRef CloneType(LLVMValueRef Src) {
388     return TypeCloner(M).Clone(Src);
389   }
390 
391   // Try to clone everything in the llvm::Value hierarchy.
CloneValueFunCloner392   LLVMValueRef CloneValue(LLVMValueRef Src) {
393     // First, the value may be constant.
394     if (LLVMIsAConstant(Src))
395       return clone_constant(Src, M);
396 
397     // Function argument should always be in the map already.
398     auto i = VMap.find(Src);
399     if (i != VMap.end())
400       return i->second;
401 
402     if (!LLVMIsAInstruction(Src))
403       report_fatal_error("Expected an instruction");
404 
405     auto Ctx = LLVMGetModuleContext(M);
406     auto Builder = LLVMCreateBuilderInContext(Ctx);
407     auto BB = DeclareBB(LLVMGetInstructionParent(Src));
408     LLVMPositionBuilderAtEnd(Builder, BB);
409     auto Dst = CloneInstruction(Src, Builder);
410     LLVMDisposeBuilder(Builder);
411     return Dst;
412   }
413 
CloneAttrsFunCloner414   void CloneAttrs(LLVMValueRef Src, LLVMValueRef Dst) {
415     auto Ctx = LLVMGetModuleContext(M);
416     int ArgCount = LLVMGetNumArgOperands(Src);
417     for (int i = LLVMAttributeReturnIndex; i <= ArgCount; i++) {
418       for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) {
419         if (auto SrcA = LLVMGetCallSiteEnumAttribute(Src, i, k)) {
420           auto Val = LLVMGetEnumAttributeValue(SrcA);
421           auto A = LLVMCreateEnumAttribute(Ctx, k, Val);
422           LLVMAddCallSiteAttribute(Dst, i, A);
423         }
424       }
425     }
426   }
427 
CloneInstructionFunCloner428   LLVMValueRef CloneInstruction(LLVMValueRef Src, LLVMBuilderRef Builder) {
429     check_value_kind(Src, LLVMInstructionValueKind);
430     if (!LLVMIsAInstruction(Src))
431       report_fatal_error("Expected an instruction");
432 
433     size_t NameLen;
434     const char *Name = LLVMGetValueName2(Src, &NameLen);
435 
436     // Check if this is something we already computed.
437     {
438       auto i = VMap.find(Src);
439       if (i != VMap.end()) {
440         // If we have a hit, it means we already generated the instruction
441         // as a dependancy to somethign else. We need to make sure
442         // it is ordered properly.
443         auto I = i->second;
444         LLVMInstructionRemoveFromParent(I);
445         LLVMInsertIntoBuilderWithName(Builder, I, Name);
446         return I;
447       }
448     }
449 
450     // We tried everything, it must be an instruction
451     // that hasn't been generated already.
452     LLVMValueRef Dst = nullptr;
453 
454     LLVMOpcode Op = LLVMGetInstructionOpcode(Src);
455     switch(Op) {
456       case LLVMRet: {
457         int OpCount = LLVMGetNumOperands(Src);
458         if (OpCount == 0)
459           Dst = LLVMBuildRetVoid(Builder);
460         else
461           Dst = LLVMBuildRet(Builder, CloneValue(LLVMGetOperand(Src, 0)));
462         break;
463       }
464       case LLVMBr: {
465         if (!LLVMIsConditional(Src)) {
466           LLVMValueRef SrcOp = LLVMGetOperand(Src, 0);
467           LLVMBasicBlockRef SrcBB = LLVMValueAsBasicBlock(SrcOp);
468           Dst = LLVMBuildBr(Builder, DeclareBB(SrcBB));
469           break;
470         }
471 
472         LLVMValueRef Cond = LLVMGetCondition(Src);
473         LLVMValueRef Else = LLVMGetOperand(Src, 1);
474         LLVMBasicBlockRef ElseBB = DeclareBB(LLVMValueAsBasicBlock(Else));
475         LLVMValueRef Then = LLVMGetOperand(Src, 2);
476         LLVMBasicBlockRef ThenBB = DeclareBB(LLVMValueAsBasicBlock(Then));
477         Dst = LLVMBuildCondBr(Builder, CloneValue(Cond), ThenBB, ElseBB);
478         break;
479       }
480       case LLVMSwitch:
481       case LLVMIndirectBr:
482         break;
483       case LLVMInvoke: {
484         SmallVector<LLVMValueRef, 8> Args;
485         int ArgCount = LLVMGetNumArgOperands(Src);
486         for (int i = 0; i < ArgCount; i++)
487           Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
488         LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
489         LLVMBasicBlockRef Then = DeclareBB(LLVMGetNormalDest(Src));
490         LLVMBasicBlockRef Unwind = DeclareBB(LLVMGetUnwindDest(Src));
491         Dst = LLVMBuildInvoke(Builder, Fn, Args.data(), ArgCount,
492                               Then, Unwind, Name);
493         CloneAttrs(Src, Dst);
494         break;
495       }
496       case LLVMUnreachable:
497         Dst = LLVMBuildUnreachable(Builder);
498         break;
499       case LLVMAdd: {
500         LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
501         LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
502         Dst = LLVMBuildAdd(Builder, LHS, RHS, Name);
503         break;
504       }
505       case LLVMSub: {
506         LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
507         LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
508         Dst = LLVMBuildSub(Builder, LHS, RHS, Name);
509         break;
510       }
511       case LLVMMul: {
512         LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
513         LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
514         Dst = LLVMBuildMul(Builder, LHS, RHS, Name);
515         break;
516       }
517       case LLVMUDiv: {
518         LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
519         LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
520         Dst = LLVMBuildUDiv(Builder, LHS, RHS, Name);
521         break;
522       }
523       case LLVMSDiv: {
524         LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
525         LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
526         Dst = LLVMBuildSDiv(Builder, LHS, RHS, Name);
527         break;
528       }
529       case LLVMURem: {
530         LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
531         LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
532         Dst = LLVMBuildURem(Builder, LHS, RHS, Name);
533         break;
534       }
535       case LLVMSRem: {
536         LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
537         LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
538         Dst = LLVMBuildSRem(Builder, LHS, RHS, Name);
539         break;
540       }
541       case LLVMShl: {
542         LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
543         LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
544         Dst = LLVMBuildShl(Builder, LHS, RHS, Name);
545         break;
546       }
547       case LLVMLShr: {
548         LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
549         LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
550         Dst = LLVMBuildLShr(Builder, LHS, RHS, Name);
551         break;
552       }
553       case LLVMAShr: {
554         LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
555         LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
556         Dst = LLVMBuildAShr(Builder, LHS, RHS, Name);
557         break;
558       }
559       case LLVMAnd: {
560         LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
561         LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
562         Dst = LLVMBuildAnd(Builder, LHS, RHS, Name);
563         break;
564       }
565       case LLVMOr: {
566         LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
567         LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
568         Dst = LLVMBuildOr(Builder, LHS, RHS, Name);
569         break;
570       }
571       case LLVMXor: {
572         LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
573         LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
574         Dst = LLVMBuildXor(Builder, LHS, RHS, Name);
575         break;
576       }
577       case LLVMAlloca: {
578         LLVMTypeRef Ty = CloneType(LLVMGetAllocatedType(Src));
579         Dst = LLVMBuildAlloca(Builder, Ty, Name);
580         break;
581       }
582       case LLVMLoad: {
583         LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
584         Dst = LLVMBuildLoad(Builder, Ptr, Name);
585         LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
586         LLVMSetOrdering(Dst, LLVMGetOrdering(Src));
587         LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
588         break;
589       }
590       case LLVMStore: {
591         LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 0));
592         LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 1));
593         Dst = LLVMBuildStore(Builder, Val, Ptr);
594         LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
595         LLVMSetOrdering(Dst, LLVMGetOrdering(Src));
596         LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
597         break;
598       }
599       case LLVMGetElementPtr: {
600         LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
601         SmallVector<LLVMValueRef, 8> Idx;
602         int NumIdx = LLVMGetNumIndices(Src);
603         for (int i = 1; i <= NumIdx; i++)
604           Idx.push_back(CloneValue(LLVMGetOperand(Src, i)));
605         if (LLVMIsInBounds(Src))
606           Dst = LLVMBuildInBoundsGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
607         else
608           Dst = LLVMBuildGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
609         break;
610       }
611       case LLVMAtomicRMW: {
612         LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
613         LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 1));
614         LLVMAtomicRMWBinOp BinOp = LLVMGetAtomicRMWBinOp(Src);
615         LLVMAtomicOrdering Ord = LLVMGetOrdering(Src);
616         LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src);
617         Dst = LLVMBuildAtomicRMW(Builder, BinOp, Ptr, Val, Ord, SingleThread);
618         LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
619         LLVMSetValueName2(Dst, Name, NameLen);
620         break;
621       }
622       case LLVMAtomicCmpXchg: {
623         LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
624         LLVMValueRef Cmp = CloneValue(LLVMGetOperand(Src, 1));
625         LLVMValueRef New = CloneValue(LLVMGetOperand(Src, 2));
626         LLVMAtomicOrdering Succ = LLVMGetCmpXchgSuccessOrdering(Src);
627         LLVMAtomicOrdering Fail = LLVMGetCmpXchgFailureOrdering(Src);
628         LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src);
629 
630         Dst = LLVMBuildAtomicCmpXchg(Builder, Ptr, Cmp, New, Succ, Fail,
631                                      SingleThread);
632         LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
633         LLVMSetWeak(Dst, LLVMGetWeak(Src));
634         LLVMSetValueName2(Dst, Name, NameLen);
635         break;
636       }
637       case LLVMBitCast: {
638         LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 0));
639         Dst = LLVMBuildBitCast(Builder, V, CloneType(Src), Name);
640         break;
641       }
642       case LLVMICmp: {
643         LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src);
644         LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
645         LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
646         Dst = LLVMBuildICmp(Builder, Pred, LHS, RHS, Name);
647         break;
648       }
649       case LLVMPHI: {
650         // We need to aggressively set things here because of loops.
651         VMap[Src] = Dst = LLVMBuildPhi(Builder, CloneType(Src), Name);
652 
653         SmallVector<LLVMValueRef, 8> Values;
654         SmallVector<LLVMBasicBlockRef, 8> Blocks;
655 
656         unsigned IncomingCount = LLVMCountIncoming(Src);
657         for (unsigned i = 0; i < IncomingCount; ++i) {
658           Blocks.push_back(DeclareBB(LLVMGetIncomingBlock(Src, i)));
659           Values.push_back(CloneValue(LLVMGetIncomingValue(Src, i)));
660         }
661 
662         LLVMAddIncoming(Dst, Values.data(), Blocks.data(), IncomingCount);
663         return Dst;
664       }
665       case LLVMCall: {
666         SmallVector<LLVMValueRef, 8> Args;
667         int ArgCount = LLVMGetNumArgOperands(Src);
668         for (int i = 0; i < ArgCount; i++)
669           Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
670         LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
671         Dst = LLVMBuildCall(Builder, Fn, Args.data(), ArgCount, Name);
672         LLVMSetTailCall(Dst, LLVMIsTailCall(Src));
673         CloneAttrs(Src, Dst);
674         break;
675       }
676       case LLVMResume: {
677         Dst = LLVMBuildResume(Builder, CloneValue(LLVMGetOperand(Src, 0)));
678         break;
679       }
680       case LLVMLandingPad: {
681         // The landing pad API is a bit screwed up for historical reasons.
682         Dst = LLVMBuildLandingPad(Builder, CloneType(Src), nullptr, 0, Name);
683         unsigned NumClauses = LLVMGetNumClauses(Src);
684         for (unsigned i = 0; i < NumClauses; ++i)
685           LLVMAddClause(Dst, CloneValue(LLVMGetClause(Src, i)));
686         LLVMSetCleanup(Dst, LLVMIsCleanup(Src));
687         break;
688       }
689       case LLVMCleanupRet: {
690         LLVMValueRef CatchPad = CloneValue(LLVMGetOperand(Src, 0));
691         LLVMBasicBlockRef Unwind = nullptr;
692         if (LLVMBasicBlockRef UDest = LLVMGetUnwindDest(Src))
693           Unwind = DeclareBB(UDest);
694         Dst = LLVMBuildCleanupRet(Builder, CatchPad, Unwind);
695         break;
696       }
697       case LLVMCatchRet: {
698         LLVMValueRef CatchPad = CloneValue(LLVMGetOperand(Src, 0));
699         LLVMBasicBlockRef SuccBB = DeclareBB(LLVMGetSuccessor(Src, 0));
700         Dst = LLVMBuildCatchRet(Builder, CatchPad, SuccBB);
701         break;
702       }
703       case LLVMCatchPad: {
704         LLVMValueRef ParentPad = CloneValue(LLVMGetParentCatchSwitch(Src));
705         SmallVector<LLVMValueRef, 8> Args;
706         int ArgCount = LLVMGetNumArgOperands(Src);
707         for (int i = 0; i < ArgCount; i++)
708           Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
709         Dst = LLVMBuildCatchPad(Builder, ParentPad,
710                                 Args.data(), ArgCount, Name);
711         break;
712       }
713       case LLVMCleanupPad: {
714         LLVMValueRef ParentPad = CloneValue(LLVMGetOperand(Src, 0));
715         SmallVector<LLVMValueRef, 8> Args;
716         int ArgCount = LLVMGetNumArgOperands(Src);
717         for (int i = 0; i < ArgCount; i++)
718           Args.push_back(CloneValue(LLVMGetArgOperand(Src, i)));
719         Dst = LLVMBuildCleanupPad(Builder, ParentPad,
720                                   Args.data(), ArgCount, Name);
721         break;
722       }
723       case LLVMCatchSwitch: {
724         LLVMValueRef ParentPad = CloneValue(LLVMGetOperand(Src, 0));
725         LLVMBasicBlockRef UnwindBB = nullptr;
726         if (LLVMBasicBlockRef UDest = LLVMGetUnwindDest(Src)) {
727           UnwindBB = DeclareBB(UDest);
728         }
729         unsigned NumHandlers = LLVMGetNumHandlers(Src);
730         Dst = LLVMBuildCatchSwitch(Builder, ParentPad, UnwindBB, NumHandlers, Name);
731         if (NumHandlers > 0) {
732           LLVMBasicBlockRef *Handlers = static_cast<LLVMBasicBlockRef*>(
733                        safe_malloc(NumHandlers * sizeof(LLVMBasicBlockRef)));
734           LLVMGetHandlers(Src, Handlers);
735           for (unsigned i = 0; i < NumHandlers; i++)
736             LLVMAddHandler(Dst, DeclareBB(Handlers[i]));
737           free(Handlers);
738         }
739         break;
740       }
741       case LLVMExtractValue: {
742         LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
743         if (LLVMGetNumIndices(Src) != 1)
744           report_fatal_error("Expected only one indice");
745         auto I = LLVMGetIndices(Src)[0];
746         Dst = LLVMBuildExtractValue(Builder, Agg, I, Name);
747         break;
748       }
749       case LLVMInsertValue: {
750         LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
751         LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 1));
752         if (LLVMGetNumIndices(Src) != 1)
753           report_fatal_error("Expected only one indice");
754         auto I = LLVMGetIndices(Src)[0];
755         Dst = LLVMBuildInsertValue(Builder, Agg, V, I, Name);
756         break;
757       }
758       case LLVMFreeze: {
759         LLVMValueRef Arg = CloneValue(LLVMGetOperand(Src, 0));
760         Dst = LLVMBuildFreeze(Builder, Arg, Name);
761         break;
762       }
763       default:
764         break;
765     }
766 
767     if (Dst == nullptr) {
768       fprintf(stderr, "%d is not a supported opcode\n", Op);
769       exit(-1);
770     }
771 
772     auto Ctx = LLVMGetModuleContext(M);
773     size_t NumMetadataEntries;
774     auto *AllMetadata =
775         LLVMInstructionGetAllMetadataOtherThanDebugLoc(Src,
776                                                        &NumMetadataEntries);
777     for (unsigned i = 0; i < NumMetadataEntries; ++i) {
778       unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i);
779       LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i);
780       LLVMSetMetadata(Dst, Kind, LLVMMetadataAsValue(Ctx, MD));
781     }
782     LLVMDisposeValueMetadataEntries(AllMetadata);
783     LLVMSetInstDebugLocation(Builder, Dst);
784 
785     check_value_kind(Dst, LLVMInstructionValueKind);
786     return VMap[Src] = Dst;
787   }
788 
DeclareBBFunCloner789   LLVMBasicBlockRef DeclareBB(LLVMBasicBlockRef Src) {
790     // Check if this is something we already computed.
791     {
792       auto i = BBMap.find(Src);
793       if (i != BBMap.end()) {
794         return i->second;
795       }
796     }
797 
798     LLVMValueRef V = LLVMBasicBlockAsValue(Src);
799     if (!LLVMValueIsBasicBlock(V) || LLVMValueAsBasicBlock(V) != Src)
800       report_fatal_error("Basic block is not a basic block");
801 
802     const char *Name = LLVMGetBasicBlockName(Src);
803     size_t NameLen;
804     const char *VName = LLVMGetValueName2(V, &NameLen);
805     if (Name != VName)
806       report_fatal_error("Basic block name mismatch");
807 
808     LLVMBasicBlockRef BB = LLVMAppendBasicBlock(Fun, Name);
809     return BBMap[Src] = BB;
810   }
811 
CloneBBFunCloner812   LLVMBasicBlockRef CloneBB(LLVMBasicBlockRef Src) {
813     LLVMBasicBlockRef BB = DeclareBB(Src);
814 
815     // Make sure ordering is correct.
816     LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Src);
817     if (Prev)
818       LLVMMoveBasicBlockAfter(BB, DeclareBB(Prev));
819 
820     LLVMValueRef First = LLVMGetFirstInstruction(Src);
821     LLVMValueRef Last = LLVMGetLastInstruction(Src);
822 
823     if (First == nullptr) {
824       if (Last != nullptr)
825         report_fatal_error("Has no first instruction, but last one");
826       return BB;
827     }
828 
829     auto Ctx = LLVMGetModuleContext(M);
830     LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx);
831     LLVMPositionBuilderAtEnd(Builder, BB);
832 
833     LLVMValueRef Cur = First;
834     LLVMValueRef Next = nullptr;
835     while(true) {
836       CloneInstruction(Cur, Builder);
837       Next = LLVMGetNextInstruction(Cur);
838       if (Next == nullptr) {
839         if (Cur != Last)
840           report_fatal_error("Final instruction does not match Last");
841         break;
842       }
843 
844       LLVMValueRef Prev = LLVMGetPreviousInstruction(Next);
845       if (Prev != Cur)
846         report_fatal_error("Next.Previous instruction is not Current");
847 
848       Cur = Next;
849     }
850 
851     LLVMDisposeBuilder(Builder);
852     return BB;
853   }
854 
CloneBBsFunCloner855   void CloneBBs(LLVMValueRef Src) {
856     unsigned Count = LLVMCountBasicBlocks(Src);
857     if (Count == 0)
858       return;
859 
860     LLVMBasicBlockRef First = LLVMGetFirstBasicBlock(Src);
861     LLVMBasicBlockRef Last = LLVMGetLastBasicBlock(Src);
862 
863     LLVMBasicBlockRef Cur = First;
864     LLVMBasicBlockRef Next = nullptr;
865     while(true) {
866       CloneBB(Cur);
867       Count--;
868       Next = LLVMGetNextBasicBlock(Cur);
869       if (Next == nullptr) {
870         if (Cur != Last)
871           report_fatal_error("Final basic block does not match Last");
872         break;
873       }
874 
875       LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Next);
876       if (Prev != Cur)
877         report_fatal_error("Next.Previous basic bloc is not Current");
878 
879       Cur = Next;
880     }
881 
882     if (Count != 0)
883       report_fatal_error("Basic block count does not match iterration");
884   }
885 };
886 
declare_symbols(LLVMModuleRef Src,LLVMModuleRef M)887 static void declare_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
888   auto Ctx = LLVMGetModuleContext(M);
889 
890   LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
891   LLVMValueRef End = LLVMGetLastGlobal(Src);
892 
893   LLVMValueRef Cur = Begin;
894   LLVMValueRef Next = nullptr;
895   if (!Begin) {
896     if (End != nullptr)
897       report_fatal_error("Range has an end but no beginning");
898     goto FunDecl;
899   }
900 
901   while (true) {
902     size_t NameLen;
903     const char *Name = LLVMGetValueName2(Cur, &NameLen);
904     if (LLVMGetNamedGlobal(M, Name))
905       report_fatal_error("GlobalVariable already cloned");
906     LLVMAddGlobal(M, LLVMGetElementType(TypeCloner(M).Clone(Cur)), Name);
907 
908     Next = LLVMGetNextGlobal(Cur);
909     if (Next == nullptr) {
910       if (Cur != End)
911         report_fatal_error("");
912       break;
913     }
914 
915     LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
916     if (Prev != Cur)
917       report_fatal_error("Next.Previous global is not Current");
918 
919     Cur = Next;
920   }
921 
922 FunDecl:
923   Begin = LLVMGetFirstFunction(Src);
924   End = LLVMGetLastFunction(Src);
925   if (!Begin) {
926     if (End != nullptr)
927       report_fatal_error("Range has an end but no beginning");
928     goto AliasDecl;
929   }
930 
931   Cur = Begin;
932   Next = nullptr;
933   while (true) {
934     size_t NameLen;
935     const char *Name = LLVMGetValueName2(Cur, &NameLen);
936     if (LLVMGetNamedFunction(M, Name))
937       report_fatal_error("Function already cloned");
938     auto Ty = LLVMGetElementType(TypeCloner(M).Clone(Cur));
939     auto F = LLVMAddFunction(M, Name, Ty);
940 
941     // Copy attributes
942     for (int i = LLVMAttributeFunctionIndex, c = LLVMCountParams(F);
943          i <= c; ++i) {
944       for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) {
945         if (auto SrcA = LLVMGetEnumAttributeAtIndex(Cur, i, k)) {
946           auto Val = LLVMGetEnumAttributeValue(SrcA);
947           auto DstA = LLVMCreateEnumAttribute(Ctx, k, Val);
948           LLVMAddAttributeAtIndex(F, i, DstA);
949         }
950       }
951     }
952 
953     Next = LLVMGetNextFunction(Cur);
954     if (Next == nullptr) {
955       if (Cur != End)
956         report_fatal_error("Last function does not match End");
957       break;
958     }
959 
960     LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
961     if (Prev != Cur)
962       report_fatal_error("Next.Previous function is not Current");
963 
964     Cur = Next;
965   }
966 
967 AliasDecl:
968   Begin = LLVMGetFirstGlobalAlias(Src);
969   End = LLVMGetLastGlobalAlias(Src);
970   if (!Begin) {
971     if (End != nullptr)
972       report_fatal_error("Range has an end but no beginning");
973     goto GlobalIFuncDecl;
974   }
975 
976   Cur = Begin;
977   Next = nullptr;
978   while (true) {
979     size_t NameLen;
980     const char *Name = LLVMGetValueName2(Cur, &NameLen);
981     if (LLVMGetNamedGlobalAlias(M, Name, NameLen))
982       report_fatal_error("Global alias already cloned");
983     LLVMTypeRef CurType = TypeCloner(M).Clone(Cur);
984     // FIXME: Allow NULL aliasee.
985     LLVMAddAlias(M, CurType, LLVMGetUndef(CurType), Name);
986 
987     Next = LLVMGetNextGlobalAlias(Cur);
988     if (Next == nullptr) {
989       if (Cur != End)
990         report_fatal_error("");
991       break;
992     }
993 
994     LLVMValueRef Prev = LLVMGetPreviousGlobalAlias(Next);
995     if (Prev != Cur)
996       report_fatal_error("Next.Previous global is not Current");
997 
998     Cur = Next;
999   }
1000 
1001 GlobalIFuncDecl:
1002   Begin = LLVMGetFirstGlobalIFunc(Src);
1003   End = LLVMGetLastGlobalIFunc(Src);
1004   if (!Begin) {
1005     if (End != nullptr)
1006       report_fatal_error("Range has an end but no beginning");
1007     goto NamedMDDecl;
1008   }
1009 
1010   Cur = Begin;
1011   Next = nullptr;
1012   while (true) {
1013     size_t NameLen;
1014     const char *Name = LLVMGetValueName2(Cur, &NameLen);
1015     if (LLVMGetNamedGlobalIFunc(M, Name, NameLen))
1016       report_fatal_error("Global ifunc already cloned");
1017     LLVMTypeRef CurType = TypeCloner(M).Clone(LLVMGlobalGetValueType(Cur));
1018     // FIXME: Allow NULL resolver.
1019     LLVMAddGlobalIFunc(M, Name, NameLen,
1020                        CurType, /*addressSpace*/ 0, LLVMGetUndef(CurType));
1021 
1022     Next = LLVMGetNextGlobalIFunc(Cur);
1023     if (Next == nullptr) {
1024       if (Cur != End)
1025         report_fatal_error("");
1026       break;
1027     }
1028 
1029     LLVMValueRef Prev = LLVMGetPreviousGlobalIFunc(Next);
1030     if (Prev != Cur)
1031       report_fatal_error("Next.Previous global is not Current");
1032 
1033     Cur = Next;
1034   }
1035 
1036 NamedMDDecl:
1037   LLVMNamedMDNodeRef BeginMD = LLVMGetFirstNamedMetadata(Src);
1038   LLVMNamedMDNodeRef EndMD = LLVMGetLastNamedMetadata(Src);
1039   if (!BeginMD) {
1040     if (EndMD != nullptr)
1041       report_fatal_error("Range has an end but no beginning");
1042     return;
1043   }
1044 
1045   LLVMNamedMDNodeRef CurMD = BeginMD;
1046   LLVMNamedMDNodeRef NextMD = nullptr;
1047   while (true) {
1048     size_t NameLen;
1049     const char *Name = LLVMGetNamedMetadataName(CurMD, &NameLen);
1050     if (LLVMGetNamedMetadata(M, Name, NameLen))
1051       report_fatal_error("Named Metadata Node already cloned");
1052     LLVMGetOrInsertNamedMetadata(M, Name, NameLen);
1053 
1054     NextMD = LLVMGetNextNamedMetadata(CurMD);
1055     if (NextMD == nullptr) {
1056       if (CurMD != EndMD)
1057         report_fatal_error("");
1058       break;
1059     }
1060 
1061     LLVMNamedMDNodeRef PrevMD = LLVMGetPreviousNamedMetadata(NextMD);
1062     if (PrevMD != CurMD)
1063       report_fatal_error("Next.Previous global is not Current");
1064 
1065     CurMD = NextMD;
1066   }
1067 }
1068 
clone_symbols(LLVMModuleRef Src,LLVMModuleRef M)1069 static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
1070   LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
1071   LLVMValueRef End = LLVMGetLastGlobal(Src);
1072 
1073   LLVMValueRef Cur = Begin;
1074   LLVMValueRef Next = nullptr;
1075   if (!Begin) {
1076     if (End != nullptr)
1077       report_fatal_error("Range has an end but no beginning");
1078     goto FunClone;
1079   }
1080 
1081   while (true) {
1082     size_t NameLen;
1083     const char *Name = LLVMGetValueName2(Cur, &NameLen);
1084     LLVMValueRef G = LLVMGetNamedGlobal(M, Name);
1085     if (!G)
1086       report_fatal_error("GlobalVariable must have been declared already");
1087 
1088     if (auto I = LLVMGetInitializer(Cur))
1089       LLVMSetInitializer(G, clone_constant(I, M));
1090 
1091     size_t NumMetadataEntries;
1092     auto *AllMetadata = LLVMGlobalCopyAllMetadata(Cur, &NumMetadataEntries);
1093     for (unsigned i = 0; i < NumMetadataEntries; ++i) {
1094       unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i);
1095       LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i);
1096       LLVMGlobalSetMetadata(G, Kind, MD);
1097     }
1098     LLVMDisposeValueMetadataEntries(AllMetadata);
1099 
1100     LLVMSetGlobalConstant(G, LLVMIsGlobalConstant(Cur));
1101     LLVMSetThreadLocal(G, LLVMIsThreadLocal(Cur));
1102     LLVMSetExternallyInitialized(G, LLVMIsExternallyInitialized(Cur));
1103     LLVMSetLinkage(G, LLVMGetLinkage(Cur));
1104     LLVMSetSection(G, LLVMGetSection(Cur));
1105     LLVMSetVisibility(G, LLVMGetVisibility(Cur));
1106     LLVMSetUnnamedAddress(G, LLVMGetUnnamedAddress(Cur));
1107     LLVMSetAlignment(G, LLVMGetAlignment(Cur));
1108 
1109     Next = LLVMGetNextGlobal(Cur);
1110     if (Next == nullptr) {
1111       if (Cur != End)
1112         report_fatal_error("");
1113       break;
1114     }
1115 
1116     LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
1117     if (Prev != Cur)
1118       report_fatal_error("Next.Previous global is not Current");
1119 
1120     Cur = Next;
1121   }
1122 
1123 FunClone:
1124   Begin = LLVMGetFirstFunction(Src);
1125   End = LLVMGetLastFunction(Src);
1126   if (!Begin) {
1127     if (End != nullptr)
1128       report_fatal_error("Range has an end but no beginning");
1129     goto AliasClone;
1130   }
1131 
1132   Cur = Begin;
1133   Next = nullptr;
1134   while (true) {
1135     size_t NameLen;
1136     const char *Name = LLVMGetValueName2(Cur, &NameLen);
1137     LLVMValueRef Fun = LLVMGetNamedFunction(M, Name);
1138     if (!Fun)
1139       report_fatal_error("Function must have been declared already");
1140 
1141     if (LLVMHasPersonalityFn(Cur)) {
1142       size_t FNameLen;
1143       const char *FName = LLVMGetValueName2(LLVMGetPersonalityFn(Cur),
1144                                            &FNameLen);
1145       LLVMValueRef P = LLVMGetNamedFunction(M, FName);
1146       if (!P)
1147         report_fatal_error("Could not find personality function");
1148       LLVMSetPersonalityFn(Fun, P);
1149     }
1150 
1151     size_t NumMetadataEntries;
1152     auto *AllMetadata = LLVMGlobalCopyAllMetadata(Cur, &NumMetadataEntries);
1153     for (unsigned i = 0; i < NumMetadataEntries; ++i) {
1154       unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i);
1155       LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i);
1156       LLVMGlobalSetMetadata(Fun, Kind, MD);
1157     }
1158     LLVMDisposeValueMetadataEntries(AllMetadata);
1159 
1160     FunCloner FC(Cur, Fun);
1161     FC.CloneBBs(Cur);
1162 
1163     Next = LLVMGetNextFunction(Cur);
1164     if (Next == nullptr) {
1165       if (Cur != End)
1166         report_fatal_error("Last function does not match End");
1167       break;
1168     }
1169 
1170     LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
1171     if (Prev != Cur)
1172       report_fatal_error("Next.Previous function is not Current");
1173 
1174     Cur = Next;
1175   }
1176 
1177 AliasClone:
1178   Begin = LLVMGetFirstGlobalAlias(Src);
1179   End = LLVMGetLastGlobalAlias(Src);
1180   if (!Begin) {
1181     if (End != nullptr)
1182       report_fatal_error("Range has an end but no beginning");
1183     goto GlobalIFuncClone;
1184   }
1185 
1186   Cur = Begin;
1187   Next = nullptr;
1188   while (true) {
1189     size_t NameLen;
1190     const char *Name = LLVMGetValueName2(Cur, &NameLen);
1191     LLVMValueRef Alias = LLVMGetNamedGlobalAlias(M, Name, NameLen);
1192     if (!Alias)
1193       report_fatal_error("Global alias must have been declared already");
1194 
1195     if (LLVMValueRef Aliasee = LLVMAliasGetAliasee(Cur)) {
1196       LLVMAliasSetAliasee(Alias, clone_constant(Aliasee, M));
1197     }
1198 
1199     LLVMSetLinkage(Alias, LLVMGetLinkage(Cur));
1200     LLVMSetUnnamedAddress(Alias, LLVMGetUnnamedAddress(Cur));
1201 
1202     Next = LLVMGetNextGlobalAlias(Cur);
1203     if (Next == nullptr) {
1204       if (Cur != End)
1205         report_fatal_error("Last global alias does not match End");
1206       break;
1207     }
1208 
1209     LLVMValueRef Prev = LLVMGetPreviousGlobalAlias(Next);
1210     if (Prev != Cur)
1211       report_fatal_error("Next.Previous global alias is not Current");
1212 
1213     Cur = Next;
1214   }
1215 
1216 GlobalIFuncClone:
1217   Begin = LLVMGetFirstGlobalIFunc(Src);
1218   End = LLVMGetLastGlobalIFunc(Src);
1219   if (!Begin) {
1220     if (End != nullptr)
1221       report_fatal_error("Range has an end but no beginning");
1222     goto NamedMDClone;
1223   }
1224 
1225   Cur = Begin;
1226   Next = nullptr;
1227   while (true) {
1228     size_t NameLen;
1229     const char *Name = LLVMGetValueName2(Cur, &NameLen);
1230     LLVMValueRef IFunc = LLVMGetNamedGlobalIFunc(M, Name, NameLen);
1231     if (!IFunc)
1232       report_fatal_error("Global ifunc must have been declared already");
1233 
1234     if (LLVMValueRef Resolver = LLVMGetGlobalIFuncResolver(Cur)) {
1235       LLVMSetGlobalIFuncResolver(IFunc, clone_constant(Resolver, M));
1236     }
1237 
1238     LLVMSetLinkage(IFunc, LLVMGetLinkage(Cur));
1239     LLVMSetUnnamedAddress(IFunc, LLVMGetUnnamedAddress(Cur));
1240 
1241     Next = LLVMGetNextGlobalIFunc(Cur);
1242     if (Next == nullptr) {
1243       if (Cur != End)
1244         report_fatal_error("Last global alias does not match End");
1245       break;
1246     }
1247 
1248     LLVMValueRef Prev = LLVMGetPreviousGlobalIFunc(Next);
1249     if (Prev != Cur)
1250       report_fatal_error("Next.Previous global alias is not Current");
1251 
1252     Cur = Next;
1253   }
1254 
1255 NamedMDClone:
1256   LLVMNamedMDNodeRef BeginMD = LLVMGetFirstNamedMetadata(Src);
1257   LLVMNamedMDNodeRef EndMD = LLVMGetLastNamedMetadata(Src);
1258   if (!BeginMD) {
1259     if (EndMD != nullptr)
1260       report_fatal_error("Range has an end but no beginning");
1261     return;
1262   }
1263 
1264   LLVMNamedMDNodeRef CurMD = BeginMD;
1265   LLVMNamedMDNodeRef NextMD = nullptr;
1266   while (true) {
1267     size_t NameLen;
1268     const char *Name = LLVMGetNamedMetadataName(CurMD, &NameLen);
1269     LLVMNamedMDNodeRef NamedMD = LLVMGetNamedMetadata(M, Name, NameLen);
1270     if (!NamedMD)
1271       report_fatal_error("Named MD Node must have been declared already");
1272 
1273     unsigned OperandCount = LLVMGetNamedMetadataNumOperands(Src, Name);
1274     LLVMValueRef *OperandBuf = static_cast<LLVMValueRef *>(
1275               safe_malloc(OperandCount * sizeof(LLVMValueRef)));
1276     LLVMGetNamedMetadataOperands(Src, Name, OperandBuf);
1277     for (unsigned i = 0, e = OperandCount; i != e; ++i) {
1278       LLVMAddNamedMetadataOperand(M, Name, OperandBuf[i]);
1279     }
1280     free(OperandBuf);
1281 
1282     NextMD = LLVMGetNextNamedMetadata(CurMD);
1283     if (NextMD == nullptr) {
1284       if (CurMD != EndMD)
1285         report_fatal_error("Last Named MD Node does not match End");
1286       break;
1287     }
1288 
1289     LLVMNamedMDNodeRef PrevMD = LLVMGetPreviousNamedMetadata(NextMD);
1290     if (PrevMD != CurMD)
1291       report_fatal_error("Next.Previous Named MD Node is not Current");
1292 
1293     CurMD = NextMD;
1294   }
1295 }
1296 
llvm_echo(void)1297 int llvm_echo(void) {
1298   LLVMEnablePrettyStackTrace();
1299 
1300   LLVMModuleRef Src = llvm_load_module(false, true);
1301   size_t SourceFileLen;
1302   const char *SourceFileName = LLVMGetSourceFileName(Src, &SourceFileLen);
1303   size_t ModuleIdentLen;
1304   const char *ModuleName = LLVMGetModuleIdentifier(Src, &ModuleIdentLen);
1305   LLVMContextRef Ctx = LLVMContextCreate();
1306   LLVMModuleRef M = LLVMModuleCreateWithNameInContext(ModuleName, Ctx);
1307 
1308   LLVMSetSourceFileName(M, SourceFileName, SourceFileLen);
1309   LLVMSetModuleIdentifier(M, ModuleName, ModuleIdentLen);
1310 
1311   LLVMSetTarget(M, LLVMGetTarget(Src));
1312   LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src));
1313   if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src)))
1314     report_fatal_error("Inconsistent DataLayout string representation");
1315 
1316   size_t ModuleInlineAsmLen;
1317   const char *ModuleAsm = LLVMGetModuleInlineAsm(Src, &ModuleInlineAsmLen);
1318   LLVMSetModuleInlineAsm2(M, ModuleAsm, ModuleInlineAsmLen);
1319 
1320   declare_symbols(Src, M);
1321   clone_symbols(Src, M);
1322   char *Str = LLVMPrintModuleToString(M);
1323   fputs(Str, stdout);
1324 
1325   LLVMDisposeMessage(Str);
1326   LLVMDisposeModule(Src);
1327   LLVMDisposeModule(M);
1328   LLVMContextDispose(Ctx);
1329 
1330   return 0;
1331 }
1332