llvm-project

Форк
0
1637 строк · 55.7 Кб
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

29
using namespace llvm;
30

31
// Provide DenseMapInfo for C API opaque types.
32
template<typename T>
33
struct 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.
38
template<typename T>
39
struct 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

58
typedef CAPIDenseMap<LLVMValueRef>::Map ValueMap;
59
typedef CAPIDenseMap<LLVMBasicBlockRef>::Map BasicBlockMap;
60

61
struct 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 LLVMArrayType2(Clone(LLVMGetElementType(Src)),
137
                              LLVMGetArrayLength2(Src));
138
      case LLVMPointerTypeKind:
139
        if (LLVMPointerTypeIsOpaque(Src))
140
          return LLVMPointerTypeInContext(Ctx, LLVMGetPointerAddressSpace(Src));
141
        else
142
          return LLVMPointerType(Clone(LLVMGetElementType(Src)),
143
                                 LLVMGetPointerAddressSpace(Src));
144
      case LLVMVectorTypeKind:
145
        return LLVMVectorType(
146
          Clone(LLVMGetElementType(Src)),
147
          LLVMGetVectorSize(Src)
148
        );
149
      case LLVMScalableVectorTypeKind:
150
        return LLVMScalableVectorType(Clone(LLVMGetElementType(Src)),
151
                                      LLVMGetVectorSize(Src));
152
      case LLVMMetadataTypeKind:
153
        return LLVMMetadataTypeInContext(Ctx);
154
      case LLVMX86_AMXTypeKind:
155
        return LLVMX86AMXTypeInContext(Ctx);
156
      case LLVMX86_MMXTypeKind:
157
        return LLVMX86MMXTypeInContext(Ctx);
158
      case LLVMTokenTypeKind:
159
        return LLVMTokenTypeInContext(Ctx);
160
      case LLVMTargetExtTypeKind: {
161
        const char *Name = LLVMGetTargetExtTypeName(Src);
162
        unsigned NumTypeParams = LLVMGetTargetExtTypeNumTypeParams(Src);
163
        unsigned NumIntParams = LLVMGetTargetExtTypeNumIntParams(Src);
164

165
        SmallVector<LLVMTypeRef, 4> TypeParams((size_t)NumTypeParams);
166
        SmallVector<unsigned, 4> IntParams((size_t)NumIntParams);
167

168
        for (unsigned i = 0; i < TypeParams.size(); i++)
169
          TypeParams[i] = Clone(LLVMGetTargetExtTypeTypeParam(Src, i));
170

171
        for (unsigned i = 0; i < IntParams.size(); i++)
172
          IntParams[i] = LLVMGetTargetExtTypeIntParam(Src, i);
173

174
        LLVMTypeRef TargetExtTy = LLVMTargetExtTypeInContext(
175
            Ctx, Name, TypeParams.data(), TypeParams.size(), IntParams.data(),
176
            IntParams.size());
177

178
        return TargetExtTy;
179
      }
180
    }
181

182
    fprintf(stderr, "%d is not a supported typekind\n", Kind);
183
    exit(-1);
184
  }
185
};
186

187
static ValueMap clone_params(LLVMValueRef Src, LLVMValueRef Dst) {
188
  unsigned Count = LLVMCountParams(Src);
189
  if (Count != LLVMCountParams(Dst))
190
    report_fatal_error("Parameter count mismatch");
191

192
  ValueMap VMap;
193
  if (Count == 0)
194
    return VMap;
195

196
  LLVMValueRef SrcFirst = LLVMGetFirstParam(Src);
197
  LLVMValueRef DstFirst = LLVMGetFirstParam(Dst);
198
  LLVMValueRef SrcLast = LLVMGetLastParam(Src);
199
  LLVMValueRef DstLast = LLVMGetLastParam(Dst);
200

201
  LLVMValueRef SrcCur = SrcFirst;
202
  LLVMValueRef DstCur = DstFirst;
203
  LLVMValueRef SrcNext = nullptr;
204
  LLVMValueRef DstNext = nullptr;
205
  while (true) {
206
    size_t NameLen;
207
    const char *Name = LLVMGetValueName2(SrcCur, &NameLen);
208
    LLVMSetValueName2(DstCur, Name, NameLen);
209

210
    VMap[SrcCur] = DstCur;
211

212
    Count--;
213
    SrcNext = LLVMGetNextParam(SrcCur);
214
    DstNext = LLVMGetNextParam(DstCur);
215
    if (SrcNext == nullptr && DstNext == nullptr) {
216
      if (SrcCur != SrcLast)
217
        report_fatal_error("SrcLast param does not match End");
218
      if (DstCur != DstLast)
219
        report_fatal_error("DstLast param does not match End");
220
      break;
221
    }
222

223
    if (SrcNext == nullptr)
224
      report_fatal_error("SrcNext was unexpectedly null");
225
    if (DstNext == nullptr)
226
      report_fatal_error("DstNext was unexpectedly null");
227

228
    LLVMValueRef SrcPrev = LLVMGetPreviousParam(SrcNext);
229
    if (SrcPrev != SrcCur)
230
      report_fatal_error("SrcNext.Previous param is not Current");
231

232
    LLVMValueRef DstPrev = LLVMGetPreviousParam(DstNext);
233
    if (DstPrev != DstCur)
234
      report_fatal_error("DstNext.Previous param is not Current");
235

236
    SrcCur = SrcNext;
237
    DstCur = DstNext;
238
  }
239

240
  if (Count != 0)
241
    report_fatal_error("Parameter count does not match iteration");
242

243
  return VMap;
244
}
245

246
static void check_value_kind(LLVMValueRef V, LLVMValueKind K) {
247
  if (LLVMGetValueKind(V) != K)
248
    report_fatal_error("LLVMGetValueKind returned incorrect type");
249
}
250

251
static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M);
252

253
static LLVMValueRef clone_constant(LLVMValueRef Cst, LLVMModuleRef M) {
254
  LLVMValueRef Ret = clone_constant_impl(Cst, M);
255
  check_value_kind(Ret, LLVMGetValueKind(Cst));
256
  return Ret;
257
}
258

259
static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M) {
260
  if (!LLVMIsAConstant(Cst))
261
    report_fatal_error("Expected a constant");
262

263
  // Maybe it is a symbol
264
  if (LLVMIsAGlobalValue(Cst)) {
265
    size_t NameLen;
266
    const char *Name = LLVMGetValueName2(Cst, &NameLen);
267

268
    // Try function
269
    if (LLVMIsAFunction(Cst)) {
270
      check_value_kind(Cst, LLVMFunctionValueKind);
271

272
      LLVMValueRef Dst = nullptr;
273
      // Try an intrinsic
274
      unsigned ID = LLVMGetIntrinsicID(Cst);
275
      if (ID > 0 && !LLVMIntrinsicIsOverloaded(ID)) {
276
        Dst = LLVMGetIntrinsicDeclaration(M, ID, nullptr, 0);
277
      } else {
278
        // Try a normal function
279
        Dst = LLVMGetNamedFunction(M, Name);
280
      }
281

282
      if (Dst)
283
        return Dst;
284
      report_fatal_error("Could not find function");
285
    }
286

287
    // Try global variable
288
    if (LLVMIsAGlobalVariable(Cst)) {
289
      check_value_kind(Cst, LLVMGlobalVariableValueKind);
290
      LLVMValueRef Dst = LLVMGetNamedGlobal(M, Name);
291
      if (Dst)
292
        return Dst;
293
      report_fatal_error("Could not find variable");
294
    }
295

296
    // Try global alias
297
    if (LLVMIsAGlobalAlias(Cst)) {
298
      check_value_kind(Cst, LLVMGlobalAliasValueKind);
299
      LLVMValueRef Dst = LLVMGetNamedGlobalAlias(M, Name, NameLen);
300
      if (Dst)
301
        return Dst;
302
      report_fatal_error("Could not find alias");
303
    }
304

305
    fprintf(stderr, "Could not find @%s\n", Name);
306
    exit(-1);
307
  }
308

309
  // Try integer literal
310
  if (LLVMIsAConstantInt(Cst)) {
311
    check_value_kind(Cst, LLVMConstantIntValueKind);
312
    return LLVMConstInt(TypeCloner(M).Clone(Cst),
313
                        LLVMConstIntGetZExtValue(Cst), false);
314
  }
315

316
  // Try zeroinitializer
317
  if (LLVMIsAConstantAggregateZero(Cst)) {
318
    check_value_kind(Cst, LLVMConstantAggregateZeroValueKind);
319
    return LLVMConstNull(TypeCloner(M).Clone(Cst));
320
  }
321

322
  // Try constant array or constant data array
323
  if (LLVMIsAConstantArray(Cst) || LLVMIsAConstantDataArray(Cst)) {
324
    check_value_kind(Cst, LLVMIsAConstantArray(Cst)
325
                              ? LLVMConstantArrayValueKind
326
                              : LLVMConstantDataArrayValueKind);
327
    LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
328
    uint64_t EltCount = LLVMGetArrayLength2(Ty);
329
    SmallVector<LLVMValueRef, 8> Elts;
330
    for (uint64_t i = 0; i < EltCount; i++)
331
      Elts.push_back(clone_constant(LLVMGetAggregateElement(Cst, i), M));
332
    return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
333
  }
334

335
  // Try constant struct
336
  if (LLVMIsAConstantStruct(Cst)) {
337
    check_value_kind(Cst, LLVMConstantStructValueKind);
338
    LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
339
    unsigned EltCount = LLVMCountStructElementTypes(Ty);
340
    SmallVector<LLVMValueRef, 8> Elts;
341
    for (unsigned i = 0; i < EltCount; i++)
342
      Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
343
    if (LLVMGetStructName(Ty))
344
      return LLVMConstNamedStruct(Ty, Elts.data(), EltCount);
345
    return LLVMConstStructInContext(LLVMGetModuleContext(M), Elts.data(),
346
                                    EltCount, LLVMIsPackedStruct(Ty));
347
  }
348

349
  // Try ConstantPointerNull
350
  if (LLVMIsAConstantPointerNull(Cst)) {
351
    check_value_kind(Cst, LLVMConstantPointerNullValueKind);
352
    LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
353
    return LLVMConstNull(Ty);
354
  }
355

356
  // Try undef
357
  if (LLVMIsUndef(Cst)) {
358
    check_value_kind(Cst, LLVMUndefValueValueKind);
359
    return LLVMGetUndef(TypeCloner(M).Clone(Cst));
360
  }
361

362
  // Try poison
363
  if (LLVMIsPoison(Cst)) {
364
    check_value_kind(Cst, LLVMPoisonValueValueKind);
365
    return LLVMGetPoison(TypeCloner(M).Clone(Cst));
366
  }
367

368
  // Try null
369
  if (LLVMIsNull(Cst)) {
370
    check_value_kind(Cst, LLVMConstantTokenNoneValueKind);
371
    LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
372
    return LLVMConstNull(Ty);
373
  }
374

375
  // Try float literal
376
  if (LLVMIsAConstantFP(Cst)) {
377
    check_value_kind(Cst, LLVMConstantFPValueKind);
378
    report_fatal_error("ConstantFP is not supported");
379
  }
380

381
  // Try ConstantVector or ConstantDataVector
382
  if (LLVMIsAConstantVector(Cst) || LLVMIsAConstantDataVector(Cst)) {
383
    check_value_kind(Cst, LLVMIsAConstantVector(Cst)
384
                              ? LLVMConstantVectorValueKind
385
                              : LLVMConstantDataVectorValueKind);
386
    LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
387
    unsigned EltCount = LLVMGetVectorSize(Ty);
388
    SmallVector<LLVMValueRef, 8> Elts;
389
    for (unsigned i = 0; i < EltCount; i++)
390
      Elts.push_back(clone_constant(LLVMGetAggregateElement(Cst, i), M));
391
    return LLVMConstVector(Elts.data(), EltCount);
392
  }
393

394
  // At this point, if it's not a constant expression, it's a kind of constant
395
  // which is not supported
396
  if (!LLVMIsAConstantExpr(Cst))
397
    report_fatal_error("Unsupported constant kind");
398

399
  // At this point, it must be a constant expression
400
  check_value_kind(Cst, LLVMConstantExprValueKind);
401

402
  LLVMOpcode Op = LLVMGetConstOpcode(Cst);
403
  switch(Op) {
404
    case LLVMBitCast:
405
      return LLVMConstBitCast(clone_constant(LLVMGetOperand(Cst, 0), M),
406
                              TypeCloner(M).Clone(Cst));
407
    case LLVMGetElementPtr: {
408
      LLVMTypeRef ElemTy =
409
          TypeCloner(M).Clone(LLVMGetGEPSourceElementType(Cst));
410
      LLVMValueRef Ptr = clone_constant(LLVMGetOperand(Cst, 0), M);
411
      int NumIdx = LLVMGetNumIndices(Cst);
412
      SmallVector<LLVMValueRef, 8> Idx;
413
      for (int i = 1; i <= NumIdx; i++)
414
        Idx.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
415
      if (LLVMIsInBounds(Cst))
416
        return LLVMConstInBoundsGEP2(ElemTy, Ptr, Idx.data(), NumIdx);
417
      else
418
        return LLVMConstGEP2(ElemTy, Ptr, Idx.data(), NumIdx);
419
    }
420
    default:
421
      fprintf(stderr, "%d is not a supported opcode for constant expressions\n",
422
              Op);
423
      exit(-1);
424
  }
425
}
426

427
static LLVMValueRef clone_inline_asm(LLVMValueRef Asm, LLVMModuleRef M) {
428

429
  if (!LLVMIsAInlineAsm(Asm))
430
      report_fatal_error("Expected inline assembly");
431

432
  size_t AsmStringSize = 0;
433
  const char *AsmString = LLVMGetInlineAsmAsmString(Asm, &AsmStringSize);
434

435
  size_t ConstraintStringSize = 0;
436
  const char *ConstraintString =
437
      LLVMGetInlineAsmConstraintString(Asm, &ConstraintStringSize);
438

439
  LLVMInlineAsmDialect AsmDialect = LLVMGetInlineAsmDialect(Asm);
440

441
  LLVMTypeRef AsmFunctionType = LLVMGetInlineAsmFunctionType(Asm);
442

443
  LLVMBool HasSideEffects = LLVMGetInlineAsmHasSideEffects(Asm);
444
  LLVMBool NeedsAlignStack = LLVMGetInlineAsmNeedsAlignedStack(Asm);
445
  LLVMBool CanUnwind = LLVMGetInlineAsmCanUnwind(Asm);
446

447
  return LLVMGetInlineAsm(AsmFunctionType, AsmString, AsmStringSize,
448
                          ConstraintString, ConstraintStringSize,
449
                          HasSideEffects, NeedsAlignStack, AsmDialect,
450
                          CanUnwind);
451
}
452

453
struct FunCloner {
454
  LLVMValueRef Fun;
455
  LLVMModuleRef M;
456

457
  ValueMap VMap;
458
  BasicBlockMap BBMap;
459

460
  FunCloner(LLVMValueRef Src, LLVMValueRef Dst): Fun(Dst),
461
    M(LLVMGetGlobalParent(Fun)), VMap(clone_params(Src, Dst)) {}
462

463
  LLVMTypeRef CloneType(LLVMTypeRef Src) {
464
    return TypeCloner(M).Clone(Src);
465
  }
466

467
  LLVMTypeRef CloneType(LLVMValueRef Src) {
468
    return TypeCloner(M).Clone(Src);
469
  }
470

471
  // Try to clone everything in the llvm::Value hierarchy.
472
  LLVMValueRef CloneValue(LLVMValueRef Src) {
473
    // First, the value may be constant.
474
    if (LLVMIsAConstant(Src))
475
      return clone_constant(Src, M);
476

477
    // Function argument should always be in the map already.
478
    auto i = VMap.find(Src);
479
    if (i != VMap.end())
480
      return i->second;
481

482
    // Inline assembly is a Value, but not an Instruction
483
    if (LLVMIsAInlineAsm(Src))
484
      return clone_inline_asm(Src, M);
485

486
    if (!LLVMIsAInstruction(Src))
487
      report_fatal_error("Expected an instruction");
488

489
    auto Ctx = LLVMGetModuleContext(M);
490
    auto Builder = LLVMCreateBuilderInContext(Ctx);
491
    auto BB = DeclareBB(LLVMGetInstructionParent(Src));
492
    LLVMPositionBuilderAtEnd(Builder, BB);
493
    auto Dst = CloneInstruction(Src, Builder);
494
    LLVMDisposeBuilder(Builder);
495
    return Dst;
496
  }
497

498
  void CloneAttrs(LLVMValueRef Src, LLVMValueRef Dst) {
499
    auto Ctx = LLVMGetModuleContext(M);
500
    int ArgCount = LLVMGetNumArgOperands(Src);
501
    for (int i = LLVMAttributeReturnIndex; i <= ArgCount; i++) {
502
      for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) {
503
        if (auto SrcA = LLVMGetCallSiteEnumAttribute(Src, i, k)) {
504
          auto Val = LLVMGetEnumAttributeValue(SrcA);
505
          auto A = LLVMCreateEnumAttribute(Ctx, k, Val);
506
          LLVMAddCallSiteAttribute(Dst, i, A);
507
        }
508
      }
509
    }
510
  }
511

512
  LLVMValueRef CloneInstruction(LLVMValueRef Src, LLVMBuilderRef Builder) {
513
    check_value_kind(Src, LLVMInstructionValueKind);
514
    if (!LLVMIsAInstruction(Src))
515
      report_fatal_error("Expected an instruction");
516

517
    size_t NameLen;
518
    const char *Name = LLVMGetValueName2(Src, &NameLen);
519

520
    // Check if this is something we already computed.
521
    {
522
      auto i = VMap.find(Src);
523
      if (i != VMap.end()) {
524
        // If we have a hit, it means we already generated the instruction
525
        // as a dependency to something else. We need to make sure
526
        // it is ordered properly.
527
        auto I = i->second;
528
        LLVMInstructionRemoveFromParent(I);
529
        LLVMInsertIntoBuilderWithName(Builder, I, Name);
530
        return I;
531
      }
532
    }
533

534
    // We tried everything, it must be an instruction
535
    // that hasn't been generated already.
536
    LLVMValueRef Dst = nullptr;
537

538
    LLVMOpcode Op = LLVMGetInstructionOpcode(Src);
539
    switch(Op) {
540
      case LLVMRet: {
541
        int OpCount = LLVMGetNumOperands(Src);
542
        if (OpCount == 0)
543
          Dst = LLVMBuildRetVoid(Builder);
544
        else
545
          Dst = LLVMBuildRet(Builder, CloneValue(LLVMGetOperand(Src, 0)));
546
        break;
547
      }
548
      case LLVMBr: {
549
        if (!LLVMIsConditional(Src)) {
550
          LLVMValueRef SrcOp = LLVMGetOperand(Src, 0);
551
          LLVMBasicBlockRef SrcBB = LLVMValueAsBasicBlock(SrcOp);
552
          Dst = LLVMBuildBr(Builder, DeclareBB(SrcBB));
553
          break;
554
        }
555

556
        LLVMValueRef Cond = LLVMGetCondition(Src);
557
        LLVMValueRef Else = LLVMGetOperand(Src, 1);
558
        LLVMBasicBlockRef ElseBB = DeclareBB(LLVMValueAsBasicBlock(Else));
559
        LLVMValueRef Then = LLVMGetOperand(Src, 2);
560
        LLVMBasicBlockRef ThenBB = DeclareBB(LLVMValueAsBasicBlock(Then));
561
        Dst = LLVMBuildCondBr(Builder, CloneValue(Cond), ThenBB, ElseBB);
562
        break;
563
      }
564
      case LLVMSwitch:
565
      case LLVMIndirectBr:
566
        break;
567
      case LLVMInvoke: {
568
        SmallVector<LLVMValueRef, 8> Args;
569
        SmallVector<LLVMOperandBundleRef, 8> Bundles;
570
        unsigned ArgCount = LLVMGetNumArgOperands(Src);
571
        for (unsigned i = 0; i < ArgCount; ++i)
572
          Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
573
        unsigned BundleCount = LLVMGetNumOperandBundles(Src);
574
        for (unsigned i = 0; i < BundleCount; ++i) {
575
          auto Bundle = LLVMGetOperandBundleAtIndex(Src, i);
576
          Bundles.push_back(CloneOB(Bundle));
577
          LLVMDisposeOperandBundle(Bundle);
578
        }
579
        LLVMTypeRef FnTy = CloneType(LLVMGetCalledFunctionType(Src));
580
        LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
581
        LLVMBasicBlockRef Then = DeclareBB(LLVMGetNormalDest(Src));
582
        LLVMBasicBlockRef Unwind = DeclareBB(LLVMGetUnwindDest(Src));
583
        Dst = LLVMBuildInvokeWithOperandBundles(
584
            Builder, FnTy, Fn, Args.data(), ArgCount, Then, Unwind,
585
            Bundles.data(), Bundles.size(), Name);
586
        CloneAttrs(Src, Dst);
587
        for (auto Bundle : Bundles)
588
          LLVMDisposeOperandBundle(Bundle);
589
        break;
590
      }
591
      case LLVMCallBr: {
592
        LLVMTypeRef FnTy = CloneType(LLVMGetCalledFunctionType(Src));
593
        LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
594

595
        LLVMBasicBlockRef DefaultDest =
596
            DeclareBB(LLVMGetCallBrDefaultDest(Src));
597

598
        // Clone indirect destinations
599
        SmallVector<LLVMBasicBlockRef, 8> IndirectDests;
600
        unsigned IndirectDestCount = LLVMGetCallBrNumIndirectDests(Src);
601
        for (unsigned i = 0; i < IndirectDestCount; ++i)
602
          IndirectDests.push_back(DeclareBB(LLVMGetCallBrIndirectDest(Src, i)));
603

604
        // Clone input arguments
605
        SmallVector<LLVMValueRef, 8> Args;
606
        unsigned ArgCount = LLVMGetNumArgOperands(Src);
607
        for (unsigned i = 0; i < ArgCount; ++i)
608
          Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
609

610
        // Clone operand bundles
611
        SmallVector<LLVMOperandBundleRef, 8> Bundles;
612
        unsigned BundleCount = LLVMGetNumOperandBundles(Src);
613
        for (unsigned i = 0; i < BundleCount; ++i) {
614
          auto Bundle = LLVMGetOperandBundleAtIndex(Src, i);
615
          Bundles.push_back(CloneOB(Bundle));
616
          LLVMDisposeOperandBundle(Bundle);
617
        }
618

619
        Dst = LLVMBuildCallBr(Builder, FnTy, Fn, DefaultDest,
620
                              IndirectDests.data(), IndirectDests.size(),
621
                              Args.data(), Args.size(), Bundles.data(),
622
                              Bundles.size(), Name);
623

624
        CloneAttrs(Src, Dst);
625

626
        for (auto Bundle : Bundles)
627
          LLVMDisposeOperandBundle(Bundle);
628

629
        break;
630
      }
631
      case LLVMUnreachable:
632
        Dst = LLVMBuildUnreachable(Builder);
633
        break;
634
      case LLVMAdd: {
635
        LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
636
        LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
637
        LLVMBool NUW = LLVMGetNUW(Src);
638
        LLVMBool NSW = LLVMGetNSW(Src);
639
        Dst = LLVMBuildAdd(Builder, LHS, RHS, Name);
640
        LLVMSetNUW(Dst, NUW);
641
        LLVMSetNSW(Dst, NSW);
642
        break;
643
      }
644
      case LLVMSub: {
645
        LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
646
        LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
647
        LLVMBool NUW = LLVMGetNUW(Src);
648
        LLVMBool NSW = LLVMGetNSW(Src);
649
        Dst = LLVMBuildSub(Builder, LHS, RHS, Name);
650
        LLVMSetNUW(Dst, NUW);
651
        LLVMSetNSW(Dst, NSW);
652
        break;
653
      }
654
      case LLVMMul: {
655
        LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
656
        LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
657
        LLVMBool NUW = LLVMGetNUW(Src);
658
        LLVMBool NSW = LLVMGetNSW(Src);
659
        Dst = LLVMBuildMul(Builder, LHS, RHS, Name);
660
        LLVMSetNUW(Dst, NUW);
661
        LLVMSetNSW(Dst, NSW);
662
        break;
663
      }
664
      case LLVMUDiv: {
665
        LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
666
        LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
667
        LLVMBool IsExact = LLVMGetExact(Src);
668
        Dst = LLVMBuildUDiv(Builder, LHS, RHS, Name);
669
        LLVMSetExact(Dst, IsExact);
670
        break;
671
      }
672
      case LLVMSDiv: {
673
        LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
674
        LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
675
        LLVMBool IsExact = LLVMGetExact(Src);
676
        Dst = LLVMBuildSDiv(Builder, LHS, RHS, Name);
677
        LLVMSetExact(Dst, IsExact);
678
        break;
679
      }
680
      case LLVMURem: {
681
        LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
682
        LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
683
        Dst = LLVMBuildURem(Builder, LHS, RHS, Name);
684
        break;
685
      }
686
      case LLVMSRem: {
687
        LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
688
        LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
689
        Dst = LLVMBuildSRem(Builder, LHS, RHS, Name);
690
        break;
691
      }
692
      case LLVMShl: {
693
        LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
694
        LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
695
        LLVMBool NUW = LLVMGetNUW(Src);
696
        LLVMBool NSW = LLVMGetNSW(Src);
697
        Dst = LLVMBuildShl(Builder, LHS, RHS, Name);
698
        LLVMSetNUW(Dst, NUW);
699
        LLVMSetNSW(Dst, NSW);
700
        break;
701
      }
702
      case LLVMLShr: {
703
        LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
704
        LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
705
        LLVMBool IsExact = LLVMGetExact(Src);
706
        Dst = LLVMBuildLShr(Builder, LHS, RHS, Name);
707
        LLVMSetExact(Dst, IsExact);
708
        break;
709
      }
710
      case LLVMAShr: {
711
        LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
712
        LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
713
        LLVMBool IsExact = LLVMGetExact(Src);
714
        Dst = LLVMBuildAShr(Builder, LHS, RHS, Name);
715
        LLVMSetExact(Dst, IsExact);
716
        break;
717
      }
718
      case LLVMAnd: {
719
        LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
720
        LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
721
        Dst = LLVMBuildAnd(Builder, LHS, RHS, Name);
722
        break;
723
      }
724
      case LLVMOr: {
725
        LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
726
        LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
727
        LLVMBool IsDisjoint = LLVMGetIsDisjoint(Src);
728
        Dst = LLVMBuildOr(Builder, LHS, RHS, Name);
729
        LLVMSetIsDisjoint(Dst, IsDisjoint);
730
        break;
731
      }
732
      case LLVMXor: {
733
        LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
734
        LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
735
        Dst = LLVMBuildXor(Builder, LHS, RHS, Name);
736
        break;
737
      }
738
      case LLVMAlloca: {
739
        LLVMTypeRef Ty = CloneType(LLVMGetAllocatedType(Src));
740
        Dst = LLVMBuildAlloca(Builder, Ty, Name);
741
        LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
742
        break;
743
      }
744
      case LLVMLoad: {
745
        LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
746
        Dst = LLVMBuildLoad2(Builder, CloneType(Src), Ptr, Name);
747
        LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
748
        LLVMSetOrdering(Dst, LLVMGetOrdering(Src));
749
        LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
750
        LLVMSetAtomicSingleThread(Dst, LLVMIsAtomicSingleThread(Src));
751
        break;
752
      }
753
      case LLVMStore: {
754
        LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 0));
755
        LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 1));
756
        Dst = LLVMBuildStore(Builder, Val, Ptr);
757
        LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
758
        LLVMSetOrdering(Dst, LLVMGetOrdering(Src));
759
        LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
760
        LLVMSetAtomicSingleThread(Dst, LLVMIsAtomicSingleThread(Src));
761
        break;
762
      }
763
      case LLVMGetElementPtr: {
764
        LLVMTypeRef ElemTy = CloneType(LLVMGetGEPSourceElementType(Src));
765
        LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
766
        SmallVector<LLVMValueRef, 8> Idx;
767
        int NumIdx = LLVMGetNumIndices(Src);
768
        for (int i = 1; i <= NumIdx; i++)
769
          Idx.push_back(CloneValue(LLVMGetOperand(Src, i)));
770
        if (LLVMIsInBounds(Src))
771
          Dst = LLVMBuildInBoundsGEP2(Builder, ElemTy, Ptr, Idx.data(), NumIdx,
772
                                      Name);
773
        else
774
          Dst = LLVMBuildGEP2(Builder, ElemTy, Ptr, Idx.data(), NumIdx, Name);
775
        break;
776
      }
777
      case LLVMAtomicRMW: {
778
        LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
779
        LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 1));
780
        LLVMAtomicRMWBinOp BinOp = LLVMGetAtomicRMWBinOp(Src);
781
        LLVMAtomicOrdering Ord = LLVMGetOrdering(Src);
782
        LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src);
783
        Dst = LLVMBuildAtomicRMW(Builder, BinOp, Ptr, Val, Ord, SingleThread);
784
        LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
785
        LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
786
        LLVMSetValueName2(Dst, Name, NameLen);
787
        break;
788
      }
789
      case LLVMAtomicCmpXchg: {
790
        LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
791
        LLVMValueRef Cmp = CloneValue(LLVMGetOperand(Src, 1));
792
        LLVMValueRef New = CloneValue(LLVMGetOperand(Src, 2));
793
        LLVMAtomicOrdering Succ = LLVMGetCmpXchgSuccessOrdering(Src);
794
        LLVMAtomicOrdering Fail = LLVMGetCmpXchgFailureOrdering(Src);
795
        LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src);
796

797
        Dst = LLVMBuildAtomicCmpXchg(Builder, Ptr, Cmp, New, Succ, Fail,
798
                                     SingleThread);
799
        LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
800
        LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
801
        LLVMSetWeak(Dst, LLVMGetWeak(Src));
802
        LLVMSetValueName2(Dst, Name, NameLen);
803
        break;
804
      }
805
      case LLVMBitCast: {
806
        LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 0));
807
        Dst = LLVMBuildBitCast(Builder, V, CloneType(Src), Name);
808
        break;
809
      }
810
      case LLVMICmp: {
811
        LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src);
812
        LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
813
        LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
814
        Dst = LLVMBuildICmp(Builder, Pred, LHS, RHS, Name);
815
        break;
816
      }
817
      case LLVMPHI: {
818
        // We need to aggressively set things here because of loops.
819
        VMap[Src] = Dst = LLVMBuildPhi(Builder, CloneType(Src), Name);
820

821
        SmallVector<LLVMValueRef, 8> Values;
822
        SmallVector<LLVMBasicBlockRef, 8> Blocks;
823

824
        unsigned IncomingCount = LLVMCountIncoming(Src);
825
        for (unsigned i = 0; i < IncomingCount; ++i) {
826
          Blocks.push_back(DeclareBB(LLVMGetIncomingBlock(Src, i)));
827
          Values.push_back(CloneValue(LLVMGetIncomingValue(Src, i)));
828
        }
829

830
        LLVMAddIncoming(Dst, Values.data(), Blocks.data(), IncomingCount);
831
        // Copy fast math flags here since we return early
832
        if (LLVMCanValueUseFastMathFlags(Src))
833
          LLVMSetFastMathFlags(Dst, LLVMGetFastMathFlags(Src));
834
        return Dst;
835
      }
836
      case LLVMSelect: {
837
        LLVMValueRef If = CloneValue(LLVMGetOperand(Src, 0));
838
        LLVMValueRef Then = CloneValue(LLVMGetOperand(Src, 1));
839
        LLVMValueRef Else = CloneValue(LLVMGetOperand(Src, 2));
840
        Dst = LLVMBuildSelect(Builder, If, Then, Else, Name);
841
        break;
842
      }
843
      case LLVMCall: {
844
        SmallVector<LLVMValueRef, 8> Args;
845
        SmallVector<LLVMOperandBundleRef, 8> Bundles;
846
        unsigned ArgCount = LLVMGetNumArgOperands(Src);
847
        for (unsigned i = 0; i < ArgCount; ++i)
848
          Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
849
        unsigned BundleCount = LLVMGetNumOperandBundles(Src);
850
        for (unsigned i = 0; i < BundleCount; ++i) {
851
          auto Bundle = LLVMGetOperandBundleAtIndex(Src, i);
852
          Bundles.push_back(CloneOB(Bundle));
853
          LLVMDisposeOperandBundle(Bundle);
854
        }
855
        LLVMTypeRef FnTy = CloneType(LLVMGetCalledFunctionType(Src));
856
        LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
857
        Dst = LLVMBuildCallWithOperandBundles(Builder, FnTy, Fn, Args.data(),
858
                                              ArgCount, Bundles.data(),
859
                                              Bundles.size(), Name);
860
        LLVMSetTailCallKind(Dst, LLVMGetTailCallKind(Src));
861
        CloneAttrs(Src, Dst);
862
        for (auto Bundle : Bundles)
863
          LLVMDisposeOperandBundle(Bundle);
864
        break;
865
      }
866
      case LLVMResume: {
867
        Dst = LLVMBuildResume(Builder, CloneValue(LLVMGetOperand(Src, 0)));
868
        break;
869
      }
870
      case LLVMLandingPad: {
871
        // The landing pad API is a bit screwed up for historical reasons.
872
        Dst = LLVMBuildLandingPad(Builder, CloneType(Src), nullptr, 0, Name);
873
        unsigned NumClauses = LLVMGetNumClauses(Src);
874
        for (unsigned i = 0; i < NumClauses; ++i)
875
          LLVMAddClause(Dst, CloneValue(LLVMGetClause(Src, i)));
876
        LLVMSetCleanup(Dst, LLVMIsCleanup(Src));
877
        break;
878
      }
879
      case LLVMCleanupRet: {
880
        LLVMValueRef CatchPad = CloneValue(LLVMGetOperand(Src, 0));
881
        LLVMBasicBlockRef Unwind = nullptr;
882
        if (LLVMBasicBlockRef UDest = LLVMGetUnwindDest(Src))
883
          Unwind = DeclareBB(UDest);
884
        Dst = LLVMBuildCleanupRet(Builder, CatchPad, Unwind);
885
        break;
886
      }
887
      case LLVMCatchRet: {
888
        LLVMValueRef CatchPad = CloneValue(LLVMGetOperand(Src, 0));
889
        LLVMBasicBlockRef SuccBB = DeclareBB(LLVMGetSuccessor(Src, 0));
890
        Dst = LLVMBuildCatchRet(Builder, CatchPad, SuccBB);
891
        break;
892
      }
893
      case LLVMCatchPad: {
894
        LLVMValueRef ParentPad = CloneValue(LLVMGetParentCatchSwitch(Src));
895
        SmallVector<LLVMValueRef, 8> Args;
896
        int ArgCount = LLVMGetNumArgOperands(Src);
897
        for (int i = 0; i < ArgCount; i++)
898
          Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
899
        Dst = LLVMBuildCatchPad(Builder, ParentPad,
900
                                Args.data(), ArgCount, Name);
901
        break;
902
      }
903
      case LLVMCleanupPad: {
904
        LLVMValueRef ParentPad = CloneValue(LLVMGetOperand(Src, 0));
905
        SmallVector<LLVMValueRef, 8> Args;
906
        int ArgCount = LLVMGetNumArgOperands(Src);
907
        for (int i = 0; i < ArgCount; i++)
908
          Args.push_back(CloneValue(LLVMGetArgOperand(Src, i)));
909
        Dst = LLVMBuildCleanupPad(Builder, ParentPad,
910
                                  Args.data(), ArgCount, Name);
911
        break;
912
      }
913
      case LLVMCatchSwitch: {
914
        LLVMValueRef ParentPad = CloneValue(LLVMGetOperand(Src, 0));
915
        LLVMBasicBlockRef UnwindBB = nullptr;
916
        if (LLVMBasicBlockRef UDest = LLVMGetUnwindDest(Src)) {
917
          UnwindBB = DeclareBB(UDest);
918
        }
919
        unsigned NumHandlers = LLVMGetNumHandlers(Src);
920
        Dst = LLVMBuildCatchSwitch(Builder, ParentPad, UnwindBB, NumHandlers, Name);
921
        if (NumHandlers > 0) {
922
          LLVMBasicBlockRef *Handlers = static_cast<LLVMBasicBlockRef*>(
923
                       safe_malloc(NumHandlers * sizeof(LLVMBasicBlockRef)));
924
          LLVMGetHandlers(Src, Handlers);
925
          for (unsigned i = 0; i < NumHandlers; i++)
926
            LLVMAddHandler(Dst, DeclareBB(Handlers[i]));
927
          free(Handlers);
928
        }
929
        break;
930
      }
931
      case LLVMExtractValue: {
932
        LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
933
        if (LLVMGetNumIndices(Src) > 1)
934
          report_fatal_error("ExtractValue: Expected only one index");
935
        else if (LLVMGetNumIndices(Src) < 1)
936
          report_fatal_error("ExtractValue: Expected an index");
937
        auto I = LLVMGetIndices(Src)[0];
938
        Dst = LLVMBuildExtractValue(Builder, Agg, I, Name);
939
        break;
940
      }
941
      case LLVMInsertValue: {
942
        LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
943
        LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 1));
944
        if (LLVMGetNumIndices(Src) > 1)
945
          report_fatal_error("InsertValue: Expected only one index");
946
        else if (LLVMGetNumIndices(Src) < 1)
947
          report_fatal_error("InsertValue: Expected an index");
948
        auto I = LLVMGetIndices(Src)[0];
949
        Dst = LLVMBuildInsertValue(Builder, Agg, V, I, Name);
950
        break;
951
      }
952
      case LLVMExtractElement: {
953
        LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
954
        LLVMValueRef Index = CloneValue(LLVMGetOperand(Src, 1));
955
        Dst = LLVMBuildExtractElement(Builder, Agg, Index, Name);
956
        break;
957
      }
958
      case LLVMInsertElement: {
959
        LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
960
        LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 1));
961
        LLVMValueRef Index = CloneValue(LLVMGetOperand(Src, 2));
962
        Dst = LLVMBuildInsertElement(Builder, Agg, V, Index, Name);
963
        break;
964
      }
965
      case LLVMShuffleVector: {
966
        LLVMValueRef Agg0 = CloneValue(LLVMGetOperand(Src, 0));
967
        LLVMValueRef Agg1 = CloneValue(LLVMGetOperand(Src, 1));
968
        SmallVector<LLVMValueRef, 8> MaskElts;
969
        unsigned NumMaskElts = LLVMGetNumMaskElements(Src);
970
        for (unsigned i = 0; i < NumMaskElts; i++) {
971
          int Val = LLVMGetMaskValue(Src, i);
972
          if (Val == LLVMGetUndefMaskElem()) {
973
            MaskElts.push_back(LLVMGetUndef(LLVMInt64Type()));
974
          } else {
975
            MaskElts.push_back(LLVMConstInt(LLVMInt64Type(), Val, true));
976
          }
977
        }
978
        LLVMValueRef Mask = LLVMConstVector(MaskElts.data(), NumMaskElts);
979
        Dst = LLVMBuildShuffleVector(Builder, Agg0, Agg1, Mask, Name);
980
        break;
981
      }
982
      case LLVMFreeze: {
983
        LLVMValueRef Arg = CloneValue(LLVMGetOperand(Src, 0));
984
        Dst = LLVMBuildFreeze(Builder, Arg, Name);
985
        break;
986
      }
987
      case LLVMFence: {
988
        LLVMAtomicOrdering Ordering = LLVMGetOrdering(Src);
989
        LLVMBool IsSingleThreaded = LLVMIsAtomicSingleThread(Src);
990
        Dst = LLVMBuildFence(Builder, Ordering, IsSingleThreaded, Name);
991
        break;
992
      }
993
      case LLVMZExt: {
994
        LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 0));
995
        LLVMTypeRef DestTy = CloneType(LLVMTypeOf(Src));
996
        LLVMBool NNeg = LLVMGetNNeg(Src);
997
        Dst = LLVMBuildZExt(Builder, Val, DestTy, Name);
998
        LLVMSetNNeg(Dst, NNeg);
999
        break;
1000
      }
1001
      case LLVMFAdd: {
1002
        LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
1003
        LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
1004
        Dst = LLVMBuildFAdd(Builder, LHS, RHS, Name);
1005
        break;
1006
      }
1007
      case LLVMFSub: {
1008
        LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
1009
        LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
1010
        Dst = LLVMBuildFSub(Builder, LHS, RHS, Name);
1011
        break;
1012
      }
1013
      case LLVMFMul: {
1014
        LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
1015
        LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
1016
        Dst = LLVMBuildFMul(Builder, LHS, RHS, Name);
1017
        break;
1018
      }
1019
      case LLVMFDiv: {
1020
        LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
1021
        LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
1022
        Dst = LLVMBuildFDiv(Builder, LHS, RHS, Name);
1023
        break;
1024
      }
1025
      case LLVMFRem: {
1026
        LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
1027
        LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
1028
        Dst = LLVMBuildFRem(Builder, LHS, RHS, Name);
1029
        break;
1030
      }
1031
      case LLVMFNeg: {
1032
        LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 0));
1033
        Dst = LLVMBuildFNeg(Builder, Val, Name);
1034
        break;
1035
      }
1036
      case LLVMFCmp: {
1037
        LLVMRealPredicate Pred = LLVMGetFCmpPredicate(Src);
1038
        LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
1039
        LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
1040
        Dst = LLVMBuildFCmp(Builder, Pred, LHS, RHS, Name);
1041
        break;
1042
      }
1043
      default:
1044
        break;
1045
    }
1046

1047
    if (Dst == nullptr) {
1048
      fprintf(stderr, "%d is not a supported opcode\n", Op);
1049
      exit(-1);
1050
    }
1051

1052
    // Copy fast-math flags on instructions that support them
1053
    if (LLVMCanValueUseFastMathFlags(Src))
1054
      LLVMSetFastMathFlags(Dst, LLVMGetFastMathFlags(Src));
1055

1056
    auto Ctx = LLVMGetModuleContext(M);
1057
    size_t NumMetadataEntries;
1058
    auto *AllMetadata =
1059
        LLVMInstructionGetAllMetadataOtherThanDebugLoc(Src,
1060
                                                       &NumMetadataEntries);
1061
    for (unsigned i = 0; i < NumMetadataEntries; ++i) {
1062
      unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i);
1063
      LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i);
1064
      LLVMSetMetadata(Dst, Kind, LLVMMetadataAsValue(Ctx, MD));
1065
    }
1066
    LLVMDisposeValueMetadataEntries(AllMetadata);
1067
    LLVMAddMetadataToInst(Builder, Dst);
1068

1069
    check_value_kind(Dst, LLVMInstructionValueKind);
1070
    return VMap[Src] = Dst;
1071
  }
1072

1073
  LLVMOperandBundleRef CloneOB(LLVMOperandBundleRef Src) {
1074
    size_t TagLen;
1075
    const char *Tag = LLVMGetOperandBundleTag(Src, &TagLen);
1076

1077
    SmallVector<LLVMValueRef, 8> Args;
1078
    for (unsigned i = 0, n = LLVMGetNumOperandBundleArgs(Src); i != n; ++i)
1079
      Args.push_back(CloneValue(LLVMGetOperandBundleArgAtIndex(Src, i)));
1080

1081
    return LLVMCreateOperandBundle(Tag, TagLen, Args.data(), Args.size());
1082
  }
1083

1084
  LLVMBasicBlockRef DeclareBB(LLVMBasicBlockRef Src) {
1085
    // Check if this is something we already computed.
1086
    {
1087
      auto i = BBMap.find(Src);
1088
      if (i != BBMap.end()) {
1089
        return i->second;
1090
      }
1091
    }
1092

1093
    LLVMValueRef V = LLVMBasicBlockAsValue(Src);
1094
    if (!LLVMValueIsBasicBlock(V) || LLVMValueAsBasicBlock(V) != Src)
1095
      report_fatal_error("Basic block is not a basic block");
1096

1097
    const char *Name = LLVMGetBasicBlockName(Src);
1098
    size_t NameLen;
1099
    const char *VName = LLVMGetValueName2(V, &NameLen);
1100
    if (Name != VName)
1101
      report_fatal_error("Basic block name mismatch");
1102

1103
    LLVMBasicBlockRef BB = LLVMAppendBasicBlock(Fun, Name);
1104
    return BBMap[Src] = BB;
1105
  }
1106

1107
  LLVMBasicBlockRef CloneBB(LLVMBasicBlockRef Src) {
1108
    LLVMBasicBlockRef BB = DeclareBB(Src);
1109

1110
    // Make sure ordering is correct.
1111
    LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Src);
1112
    if (Prev)
1113
      LLVMMoveBasicBlockAfter(BB, DeclareBB(Prev));
1114

1115
    LLVMValueRef First = LLVMGetFirstInstruction(Src);
1116
    LLVMValueRef Last = LLVMGetLastInstruction(Src);
1117

1118
    if (First == nullptr) {
1119
      if (Last != nullptr)
1120
        report_fatal_error("Has no first instruction, but last one");
1121
      return BB;
1122
    }
1123

1124
    auto Ctx = LLVMGetModuleContext(M);
1125
    LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx);
1126
    LLVMPositionBuilderAtEnd(Builder, BB);
1127

1128
    LLVMValueRef Cur = First;
1129
    LLVMValueRef Next = nullptr;
1130
    while(true) {
1131
      CloneInstruction(Cur, Builder);
1132
      Next = LLVMGetNextInstruction(Cur);
1133
      if (Next == nullptr) {
1134
        if (Cur != Last)
1135
          report_fatal_error("Final instruction does not match Last");
1136
        break;
1137
      }
1138

1139
      LLVMValueRef Prev = LLVMGetPreviousInstruction(Next);
1140
      if (Prev != Cur)
1141
        report_fatal_error("Next.Previous instruction is not Current");
1142

1143
      Cur = Next;
1144
    }
1145

1146
    LLVMDisposeBuilder(Builder);
1147
    return BB;
1148
  }
1149

1150
  void CloneBBs(LLVMValueRef Src) {
1151
    unsigned Count = LLVMCountBasicBlocks(Src);
1152
    if (Count == 0)
1153
      return;
1154

1155
    LLVMBasicBlockRef First = LLVMGetFirstBasicBlock(Src);
1156
    LLVMBasicBlockRef Last = LLVMGetLastBasicBlock(Src);
1157

1158
    LLVMBasicBlockRef Cur = First;
1159
    LLVMBasicBlockRef Next = nullptr;
1160
    while(true) {
1161
      CloneBB(Cur);
1162
      Count--;
1163
      Next = LLVMGetNextBasicBlock(Cur);
1164
      if (Next == nullptr) {
1165
        if (Cur != Last)
1166
          report_fatal_error("Final basic block does not match Last");
1167
        break;
1168
      }
1169

1170
      LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Next);
1171
      if (Prev != Cur)
1172
        report_fatal_error("Next.Previous basic bloc is not Current");
1173

1174
      Cur = Next;
1175
    }
1176

1177
    if (Count != 0)
1178
      report_fatal_error("Basic block count does not match iterration");
1179
  }
1180
};
1181

1182
static void declare_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
1183
  auto Ctx = LLVMGetModuleContext(M);
1184

1185
  LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
1186
  LLVMValueRef End = LLVMGetLastGlobal(Src);
1187

1188
  LLVMValueRef Cur = Begin;
1189
  LLVMValueRef Next = nullptr;
1190
  if (!Begin) {
1191
    if (End != nullptr)
1192
      report_fatal_error("Range has an end but no beginning");
1193
    goto FunDecl;
1194
  }
1195

1196
  while (true) {
1197
    size_t NameLen;
1198
    const char *Name = LLVMGetValueName2(Cur, &NameLen);
1199
    if (LLVMGetNamedGlobal(M, Name))
1200
      report_fatal_error("GlobalVariable already cloned");
1201
    LLVMAddGlobal(M, TypeCloner(M).Clone(LLVMGlobalGetValueType(Cur)), Name);
1202

1203
    Next = LLVMGetNextGlobal(Cur);
1204
    if (Next == nullptr) {
1205
      if (Cur != End)
1206
        report_fatal_error("");
1207
      break;
1208
    }
1209

1210
    LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
1211
    if (Prev != Cur)
1212
      report_fatal_error("Next.Previous global is not Current");
1213

1214
    Cur = Next;
1215
  }
1216

1217
FunDecl:
1218
  Begin = LLVMGetFirstFunction(Src);
1219
  End = LLVMGetLastFunction(Src);
1220
  if (!Begin) {
1221
    if (End != nullptr)
1222
      report_fatal_error("Range has an end but no beginning");
1223
    goto AliasDecl;
1224
  }
1225

1226
  Cur = Begin;
1227
  Next = nullptr;
1228
  while (true) {
1229
    size_t NameLen;
1230
    const char *Name = LLVMGetValueName2(Cur, &NameLen);
1231
    if (LLVMGetNamedFunction(M, Name))
1232
      report_fatal_error("Function already cloned");
1233
    LLVMTypeRef Ty = TypeCloner(M).Clone(LLVMGlobalGetValueType(Cur));
1234

1235
    auto F = LLVMAddFunction(M, Name, Ty);
1236

1237
    // Copy attributes
1238
    for (int i = LLVMAttributeFunctionIndex, c = LLVMCountParams(F);
1239
         i <= c; ++i) {
1240
      for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) {
1241
        if (auto SrcA = LLVMGetEnumAttributeAtIndex(Cur, i, k)) {
1242
          auto Val = LLVMGetEnumAttributeValue(SrcA);
1243
          auto DstA = LLVMCreateEnumAttribute(Ctx, k, Val);
1244
          LLVMAddAttributeAtIndex(F, i, DstA);
1245
        }
1246
      }
1247
    }
1248

1249
    Next = LLVMGetNextFunction(Cur);
1250
    if (Next == nullptr) {
1251
      if (Cur != End)
1252
        report_fatal_error("Last function does not match End");
1253
      break;
1254
    }
1255

1256
    LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
1257
    if (Prev != Cur)
1258
      report_fatal_error("Next.Previous function is not Current");
1259

1260
    Cur = Next;
1261
  }
1262

1263
AliasDecl:
1264
  Begin = LLVMGetFirstGlobalAlias(Src);
1265
  End = LLVMGetLastGlobalAlias(Src);
1266
  if (!Begin) {
1267
    if (End != nullptr)
1268
      report_fatal_error("Range has an end but no beginning");
1269
    goto GlobalIFuncDecl;
1270
  }
1271

1272
  Cur = Begin;
1273
  Next = nullptr;
1274
  while (true) {
1275
    size_t NameLen;
1276
    const char *Name = LLVMGetValueName2(Cur, &NameLen);
1277
    if (LLVMGetNamedGlobalAlias(M, Name, NameLen))
1278
      report_fatal_error("Global alias already cloned");
1279
    LLVMTypeRef PtrType = TypeCloner(M).Clone(Cur);
1280
    LLVMTypeRef ValType = TypeCloner(M).Clone(LLVMGlobalGetValueType(Cur));
1281
    unsigned AddrSpace = LLVMGetPointerAddressSpace(PtrType);
1282
    // FIXME: Allow NULL aliasee.
1283
    LLVMAddAlias2(M, ValType, AddrSpace, LLVMGetUndef(PtrType), Name);
1284

1285
    Next = LLVMGetNextGlobalAlias(Cur);
1286
    if (Next == nullptr) {
1287
      if (Cur != End)
1288
        report_fatal_error("");
1289
      break;
1290
    }
1291

1292
    LLVMValueRef Prev = LLVMGetPreviousGlobalAlias(Next);
1293
    if (Prev != Cur)
1294
      report_fatal_error("Next.Previous global is not Current");
1295

1296
    Cur = Next;
1297
  }
1298

1299
GlobalIFuncDecl:
1300
  Begin = LLVMGetFirstGlobalIFunc(Src);
1301
  End = LLVMGetLastGlobalIFunc(Src);
1302
  if (!Begin) {
1303
    if (End != nullptr)
1304
      report_fatal_error("Range has an end but no beginning");
1305
    goto NamedMDDecl;
1306
  }
1307

1308
  Cur = Begin;
1309
  Next = nullptr;
1310
  while (true) {
1311
    size_t NameLen;
1312
    const char *Name = LLVMGetValueName2(Cur, &NameLen);
1313
    if (LLVMGetNamedGlobalIFunc(M, Name, NameLen))
1314
      report_fatal_error("Global ifunc already cloned");
1315
    LLVMTypeRef CurType = TypeCloner(M).Clone(LLVMGlobalGetValueType(Cur));
1316
    // FIXME: Allow NULL resolver.
1317
    LLVMAddGlobalIFunc(M, Name, NameLen,
1318
                       CurType, /*addressSpace*/ 0, LLVMGetUndef(CurType));
1319

1320
    Next = LLVMGetNextGlobalIFunc(Cur);
1321
    if (Next == nullptr) {
1322
      if (Cur != End)
1323
        report_fatal_error("");
1324
      break;
1325
    }
1326

1327
    LLVMValueRef Prev = LLVMGetPreviousGlobalIFunc(Next);
1328
    if (Prev != Cur)
1329
      report_fatal_error("Next.Previous global is not Current");
1330

1331
    Cur = Next;
1332
  }
1333

1334
NamedMDDecl:
1335
  LLVMNamedMDNodeRef BeginMD = LLVMGetFirstNamedMetadata(Src);
1336
  LLVMNamedMDNodeRef EndMD = LLVMGetLastNamedMetadata(Src);
1337
  if (!BeginMD) {
1338
    if (EndMD != nullptr)
1339
      report_fatal_error("Range has an end but no beginning");
1340
    return;
1341
  }
1342

1343
  LLVMNamedMDNodeRef CurMD = BeginMD;
1344
  LLVMNamedMDNodeRef NextMD = nullptr;
1345
  while (true) {
1346
    size_t NameLen;
1347
    const char *Name = LLVMGetNamedMetadataName(CurMD, &NameLen);
1348
    if (LLVMGetNamedMetadata(M, Name, NameLen))
1349
      report_fatal_error("Named Metadata Node already cloned");
1350
    LLVMGetOrInsertNamedMetadata(M, Name, NameLen);
1351

1352
    NextMD = LLVMGetNextNamedMetadata(CurMD);
1353
    if (NextMD == nullptr) {
1354
      if (CurMD != EndMD)
1355
        report_fatal_error("");
1356
      break;
1357
    }
1358

1359
    LLVMNamedMDNodeRef PrevMD = LLVMGetPreviousNamedMetadata(NextMD);
1360
    if (PrevMD != CurMD)
1361
      report_fatal_error("Next.Previous global is not Current");
1362

1363
    CurMD = NextMD;
1364
  }
1365
}
1366

1367
static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
1368
  LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
1369
  LLVMValueRef End = LLVMGetLastGlobal(Src);
1370

1371
  LLVMValueRef Cur = Begin;
1372
  LLVMValueRef Next = nullptr;
1373
  if (!Begin) {
1374
    if (End != nullptr)
1375
      report_fatal_error("Range has an end but no beginning");
1376
    goto FunClone;
1377
  }
1378

1379
  while (true) {
1380
    size_t NameLen;
1381
    const char *Name = LLVMGetValueName2(Cur, &NameLen);
1382
    LLVMValueRef G = LLVMGetNamedGlobal(M, Name);
1383
    if (!G)
1384
      report_fatal_error("GlobalVariable must have been declared already");
1385

1386
    if (auto I = LLVMGetInitializer(Cur))
1387
      LLVMSetInitializer(G, clone_constant(I, M));
1388

1389
    size_t NumMetadataEntries;
1390
    auto *AllMetadata = LLVMGlobalCopyAllMetadata(Cur, &NumMetadataEntries);
1391
    for (unsigned i = 0; i < NumMetadataEntries; ++i) {
1392
      unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i);
1393
      LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i);
1394
      LLVMGlobalSetMetadata(G, Kind, MD);
1395
    }
1396
    LLVMDisposeValueMetadataEntries(AllMetadata);
1397

1398
    LLVMSetGlobalConstant(G, LLVMIsGlobalConstant(Cur));
1399
    LLVMSetThreadLocal(G, LLVMIsThreadLocal(Cur));
1400
    LLVMSetExternallyInitialized(G, LLVMIsExternallyInitialized(Cur));
1401
    LLVMSetLinkage(G, LLVMGetLinkage(Cur));
1402
    LLVMSetSection(G, LLVMGetSection(Cur));
1403
    LLVMSetVisibility(G, LLVMGetVisibility(Cur));
1404
    LLVMSetUnnamedAddress(G, LLVMGetUnnamedAddress(Cur));
1405
    LLVMSetAlignment(G, LLVMGetAlignment(Cur));
1406

1407
    Next = LLVMGetNextGlobal(Cur);
1408
    if (Next == nullptr) {
1409
      if (Cur != End)
1410
        report_fatal_error("");
1411
      break;
1412
    }
1413

1414
    LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
1415
    if (Prev != Cur)
1416
      report_fatal_error("Next.Previous global is not Current");
1417

1418
    Cur = Next;
1419
  }
1420

1421
FunClone:
1422
  Begin = LLVMGetFirstFunction(Src);
1423
  End = LLVMGetLastFunction(Src);
1424
  if (!Begin) {
1425
    if (End != nullptr)
1426
      report_fatal_error("Range has an end but no beginning");
1427
    goto AliasClone;
1428
  }
1429

1430
  Cur = Begin;
1431
  Next = nullptr;
1432
  while (true) {
1433
    size_t NameLen;
1434
    const char *Name = LLVMGetValueName2(Cur, &NameLen);
1435
    LLVMValueRef Fun = LLVMGetNamedFunction(M, Name);
1436
    if (!Fun)
1437
      report_fatal_error("Function must have been declared already");
1438

1439
    if (LLVMHasPersonalityFn(Cur)) {
1440
      size_t FNameLen;
1441
      const char *FName = LLVMGetValueName2(LLVMGetPersonalityFn(Cur),
1442
                                           &FNameLen);
1443
      LLVMValueRef P = LLVMGetNamedFunction(M, FName);
1444
      if (!P)
1445
        report_fatal_error("Could not find personality function");
1446
      LLVMSetPersonalityFn(Fun, P);
1447
    }
1448

1449
    size_t NumMetadataEntries;
1450
    auto *AllMetadata = LLVMGlobalCopyAllMetadata(Cur, &NumMetadataEntries);
1451
    for (unsigned i = 0; i < NumMetadataEntries; ++i) {
1452
      unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i);
1453
      LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i);
1454
      LLVMGlobalSetMetadata(Fun, Kind, MD);
1455
    }
1456
    LLVMDisposeValueMetadataEntries(AllMetadata);
1457

1458
    // Copy any prefix data that may be on the function
1459
    if (LLVMHasPrefixData(Cur))
1460
      LLVMSetPrefixData(Fun, clone_constant(LLVMGetPrefixData(Cur), M));
1461

1462
    // Copy any prologue data that may be on the function
1463
    if (LLVMHasPrologueData(Cur))
1464
      LLVMSetPrologueData(Fun, clone_constant(LLVMGetPrologueData(Cur), M));
1465

1466
    FunCloner FC(Cur, Fun);
1467
    FC.CloneBBs(Cur);
1468

1469
    Next = LLVMGetNextFunction(Cur);
1470
    if (Next == nullptr) {
1471
      if (Cur != End)
1472
        report_fatal_error("Last function does not match End");
1473
      break;
1474
    }
1475

1476
    LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
1477
    if (Prev != Cur)
1478
      report_fatal_error("Next.Previous function is not Current");
1479

1480
    Cur = Next;
1481
  }
1482

1483
AliasClone:
1484
  Begin = LLVMGetFirstGlobalAlias(Src);
1485
  End = LLVMGetLastGlobalAlias(Src);
1486
  if (!Begin) {
1487
    if (End != nullptr)
1488
      report_fatal_error("Range has an end but no beginning");
1489
    goto GlobalIFuncClone;
1490
  }
1491

1492
  Cur = Begin;
1493
  Next = nullptr;
1494
  while (true) {
1495
    size_t NameLen;
1496
    const char *Name = LLVMGetValueName2(Cur, &NameLen);
1497
    LLVMValueRef Alias = LLVMGetNamedGlobalAlias(M, Name, NameLen);
1498
    if (!Alias)
1499
      report_fatal_error("Global alias must have been declared already");
1500

1501
    if (LLVMValueRef Aliasee = LLVMAliasGetAliasee(Cur)) {
1502
      LLVMAliasSetAliasee(Alias, clone_constant(Aliasee, M));
1503
    }
1504

1505
    LLVMSetLinkage(Alias, LLVMGetLinkage(Cur));
1506
    LLVMSetUnnamedAddress(Alias, LLVMGetUnnamedAddress(Cur));
1507

1508
    Next = LLVMGetNextGlobalAlias(Cur);
1509
    if (Next == nullptr) {
1510
      if (Cur != End)
1511
        report_fatal_error("Last global alias does not match End");
1512
      break;
1513
    }
1514

1515
    LLVMValueRef Prev = LLVMGetPreviousGlobalAlias(Next);
1516
    if (Prev != Cur)
1517
      report_fatal_error("Next.Previous global alias is not Current");
1518

1519
    Cur = Next;
1520
  }
1521

1522
GlobalIFuncClone:
1523
  Begin = LLVMGetFirstGlobalIFunc(Src);
1524
  End = LLVMGetLastGlobalIFunc(Src);
1525
  if (!Begin) {
1526
    if (End != nullptr)
1527
      report_fatal_error("Range has an end but no beginning");
1528
    goto NamedMDClone;
1529
  }
1530

1531
  Cur = Begin;
1532
  Next = nullptr;
1533
  while (true) {
1534
    size_t NameLen;
1535
    const char *Name = LLVMGetValueName2(Cur, &NameLen);
1536
    LLVMValueRef IFunc = LLVMGetNamedGlobalIFunc(M, Name, NameLen);
1537
    if (!IFunc)
1538
      report_fatal_error("Global ifunc must have been declared already");
1539

1540
    if (LLVMValueRef Resolver = LLVMGetGlobalIFuncResolver(Cur)) {
1541
      LLVMSetGlobalIFuncResolver(IFunc, clone_constant(Resolver, M));
1542
    }
1543

1544
    LLVMSetLinkage(IFunc, LLVMGetLinkage(Cur));
1545
    LLVMSetUnnamedAddress(IFunc, LLVMGetUnnamedAddress(Cur));
1546

1547
    Next = LLVMGetNextGlobalIFunc(Cur);
1548
    if (Next == nullptr) {
1549
      if (Cur != End)
1550
        report_fatal_error("Last global alias does not match End");
1551
      break;
1552
    }
1553

1554
    LLVMValueRef Prev = LLVMGetPreviousGlobalIFunc(Next);
1555
    if (Prev != Cur)
1556
      report_fatal_error("Next.Previous global alias is not Current");
1557

1558
    Cur = Next;
1559
  }
1560

1561
NamedMDClone:
1562
  LLVMNamedMDNodeRef BeginMD = LLVMGetFirstNamedMetadata(Src);
1563
  LLVMNamedMDNodeRef EndMD = LLVMGetLastNamedMetadata(Src);
1564
  if (!BeginMD) {
1565
    if (EndMD != nullptr)
1566
      report_fatal_error("Range has an end but no beginning");
1567
    return;
1568
  }
1569

1570
  LLVMNamedMDNodeRef CurMD = BeginMD;
1571
  LLVMNamedMDNodeRef NextMD = nullptr;
1572
  while (true) {
1573
    size_t NameLen;
1574
    const char *Name = LLVMGetNamedMetadataName(CurMD, &NameLen);
1575
    LLVMNamedMDNodeRef NamedMD = LLVMGetNamedMetadata(M, Name, NameLen);
1576
    if (!NamedMD)
1577
      report_fatal_error("Named MD Node must have been declared already");
1578

1579
    unsigned OperandCount = LLVMGetNamedMetadataNumOperands(Src, Name);
1580
    LLVMValueRef *OperandBuf = static_cast<LLVMValueRef *>(
1581
              safe_malloc(OperandCount * sizeof(LLVMValueRef)));
1582
    LLVMGetNamedMetadataOperands(Src, Name, OperandBuf);
1583
    for (unsigned i = 0, e = OperandCount; i != e; ++i) {
1584
      LLVMAddNamedMetadataOperand(M, Name, OperandBuf[i]);
1585
    }
1586
    free(OperandBuf);
1587

1588
    NextMD = LLVMGetNextNamedMetadata(CurMD);
1589
    if (NextMD == nullptr) {
1590
      if (CurMD != EndMD)
1591
        report_fatal_error("Last Named MD Node does not match End");
1592
      break;
1593
    }
1594

1595
    LLVMNamedMDNodeRef PrevMD = LLVMGetPreviousNamedMetadata(NextMD);
1596
    if (PrevMD != CurMD)
1597
      report_fatal_error("Next.Previous Named MD Node is not Current");
1598

1599
    CurMD = NextMD;
1600
  }
1601
}
1602

1603
int llvm_echo(void) {
1604
  LLVMEnablePrettyStackTrace();
1605

1606
  LLVMModuleRef Src = llvm_load_module(false, true);
1607
  size_t SourceFileLen;
1608
  const char *SourceFileName = LLVMGetSourceFileName(Src, &SourceFileLen);
1609
  size_t ModuleIdentLen;
1610
  const char *ModuleName = LLVMGetModuleIdentifier(Src, &ModuleIdentLen);
1611
  LLVMContextRef Ctx = LLVMContextCreate();
1612
  LLVMModuleRef M = LLVMModuleCreateWithNameInContext(ModuleName, Ctx);
1613

1614
  LLVMSetSourceFileName(M, SourceFileName, SourceFileLen);
1615
  LLVMSetModuleIdentifier(M, ModuleName, ModuleIdentLen);
1616

1617
  LLVMSetTarget(M, LLVMGetTarget(Src));
1618
  LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src));
1619
  if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src)))
1620
    report_fatal_error("Inconsistent DataLayout string representation");
1621

1622
  size_t ModuleInlineAsmLen;
1623
  const char *ModuleAsm = LLVMGetModuleInlineAsm(Src, &ModuleInlineAsmLen);
1624
  LLVMSetModuleInlineAsm2(M, ModuleAsm, ModuleInlineAsmLen);
1625

1626
  declare_symbols(Src, M);
1627
  clone_symbols(Src, M);
1628
  char *Str = LLVMPrintModuleToString(M);
1629
  fputs(Str, stdout);
1630

1631
  LLVMDisposeMessage(Str);
1632
  LLVMDisposeModule(Src);
1633
  LLVMDisposeModule(M);
1634
  LLVMContextDispose(Ctx);
1635

1636
  return 0;
1637
}
1638

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.