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