[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / llvm / tools / llvm-c-test / echo.cpp
blob06966ce528eae4ddbde820464fb9c733da8e2899
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.
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.
15 //===----------------------------------------------------------------------===//
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"
26 #include <stdio.h>
27 #include <stdlib.h>
29 using namespace llvm;
31 // Provide DenseMapInfo for C API opaque types.
32 template<typename T>
33 struct CAPIDenseMap {};
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);
45 static inline T* getTombstoneKey() {
46 uintptr_t Val = static_cast<uintptr_t>(-2);
47 return reinterpret_cast<T*>(Val);
49 static unsigned getHashValue(const T *PtrVal) {
50 return hash_value(PtrVal);
52 static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; }
55 typedef DenseMap<T*, T*, CAPIDenseMapInfo> Map;
58 typedef CAPIDenseMap<LLVMValueRef>::Map ValueMap;
59 typedef CAPIDenseMap<LLVMBasicBlockRef>::Map BasicBlockMap;
61 struct TypeCloner {
62 LLVMModuleRef M;
63 LLVMContextRef Ctx;
65 TypeCloner(LLVMModuleRef M): M(M), Ctx(LLVMGetModuleContext(M)) {}
67 LLVMTypeRef Clone(LLVMValueRef Src) {
68 return Clone(LLVMTypeOf(Src));
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]);
105 LLVMTypeRef FunTy = LLVMFunctionType(Clone(LLVMGetReturnType(Src)),
106 Params, ParamCount,
107 LLVMIsFunctionVarArg(Src));
108 if (ParamCount > 0)
109 free(Params);
110 return FunTy;
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;
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;
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)
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 assert(false && "Implement me");
164 fprintf(stderr, "%d is not a supported typekind\n", Kind);
165 exit(-1);
169 static ValueMap clone_params(LLVMValueRef Src, LLVMValueRef Dst) {
170 unsigned Count = LLVMCountParams(Src);
171 if (Count != LLVMCountParams(Dst))
172 report_fatal_error("Parameter count mismatch");
174 ValueMap VMap;
175 if (Count == 0)
176 return VMap;
178 LLVMValueRef SrcFirst = LLVMGetFirstParam(Src);
179 LLVMValueRef DstFirst = LLVMGetFirstParam(Dst);
180 LLVMValueRef SrcLast = LLVMGetLastParam(Src);
181 LLVMValueRef DstLast = LLVMGetLastParam(Dst);
183 LLVMValueRef SrcCur = SrcFirst;
184 LLVMValueRef DstCur = DstFirst;
185 LLVMValueRef SrcNext = nullptr;
186 LLVMValueRef DstNext = nullptr;
187 while (true) {
188 size_t NameLen;
189 const char *Name = LLVMGetValueName2(SrcCur, &NameLen);
190 LLVMSetValueName2(DstCur, Name, NameLen);
192 VMap[SrcCur] = DstCur;
194 Count--;
195 SrcNext = LLVMGetNextParam(SrcCur);
196 DstNext = LLVMGetNextParam(DstCur);
197 if (SrcNext == nullptr && DstNext == nullptr) {
198 if (SrcCur != SrcLast)
199 report_fatal_error("SrcLast param does not match End");
200 if (DstCur != DstLast)
201 report_fatal_error("DstLast param does not match End");
202 break;
205 if (SrcNext == nullptr)
206 report_fatal_error("SrcNext was unexpectedly null");
207 if (DstNext == nullptr)
208 report_fatal_error("DstNext was unexpectedly null");
210 LLVMValueRef SrcPrev = LLVMGetPreviousParam(SrcNext);
211 if (SrcPrev != SrcCur)
212 report_fatal_error("SrcNext.Previous param is not Current");
214 LLVMValueRef DstPrev = LLVMGetPreviousParam(DstNext);
215 if (DstPrev != DstCur)
216 report_fatal_error("DstNext.Previous param is not Current");
218 SrcCur = SrcNext;
219 DstCur = DstNext;
222 if (Count != 0)
223 report_fatal_error("Parameter count does not match iteration");
225 return VMap;
228 static void check_value_kind(LLVMValueRef V, LLVMValueKind K) {
229 if (LLVMGetValueKind(V) != K)
230 report_fatal_error("LLVMGetValueKind returned incorrect type");
233 static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M);
235 static LLVMValueRef clone_constant(LLVMValueRef Cst, LLVMModuleRef M) {
236 LLVMValueRef Ret = clone_constant_impl(Cst, M);
237 check_value_kind(Ret, LLVMGetValueKind(Cst));
238 return Ret;
241 static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M) {
242 if (!LLVMIsAConstant(Cst))
243 report_fatal_error("Expected a constant");
245 // Maybe it is a symbol
246 if (LLVMIsAGlobalValue(Cst)) {
247 size_t NameLen;
248 const char *Name = LLVMGetValueName2(Cst, &NameLen);
250 // Try function
251 if (LLVMIsAFunction(Cst)) {
252 check_value_kind(Cst, LLVMFunctionValueKind);
254 LLVMValueRef Dst = nullptr;
255 // Try an intrinsic
256 unsigned ID = LLVMGetIntrinsicID(Cst);
257 if (ID > 0 && !LLVMIntrinsicIsOverloaded(ID)) {
258 Dst = LLVMGetIntrinsicDeclaration(M, ID, nullptr, 0);
259 } else {
260 // Try a normal function
261 Dst = LLVMGetNamedFunction(M, Name);
264 if (Dst)
265 return Dst;
266 report_fatal_error("Could not find function");
269 // Try global variable
270 if (LLVMIsAGlobalVariable(Cst)) {
271 check_value_kind(Cst, LLVMGlobalVariableValueKind);
272 LLVMValueRef Dst = LLVMGetNamedGlobal(M, Name);
273 if (Dst)
274 return Dst;
275 report_fatal_error("Could not find variable");
278 // Try global alias
279 if (LLVMIsAGlobalAlias(Cst)) {
280 check_value_kind(Cst, LLVMGlobalAliasValueKind);
281 LLVMValueRef Dst = LLVMGetNamedGlobalAlias(M, Name, NameLen);
282 if (Dst)
283 return Dst;
284 report_fatal_error("Could not find alias");
287 fprintf(stderr, "Could not find @%s\n", Name);
288 exit(-1);
291 // Try integer literal
292 if (LLVMIsAConstantInt(Cst)) {
293 check_value_kind(Cst, LLVMConstantIntValueKind);
294 return LLVMConstInt(TypeCloner(M).Clone(Cst),
295 LLVMConstIntGetZExtValue(Cst), false);
298 // Try zeroinitializer
299 if (LLVMIsAConstantAggregateZero(Cst)) {
300 check_value_kind(Cst, LLVMConstantAggregateZeroValueKind);
301 return LLVMConstNull(TypeCloner(M).Clone(Cst));
304 // Try constant array or constant data array
305 if (LLVMIsAConstantArray(Cst) || LLVMIsAConstantDataArray(Cst)) {
306 check_value_kind(Cst, LLVMIsAConstantArray(Cst)
307 ? LLVMConstantArrayValueKind
308 : LLVMConstantDataArrayValueKind);
309 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
310 uint64_t EltCount = LLVMGetArrayLength2(Ty);
311 SmallVector<LLVMValueRef, 8> Elts;
312 for (uint64_t i = 0; i < EltCount; i++)
313 Elts.push_back(clone_constant(LLVMGetAggregateElement(Cst, i), M));
314 return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
317 // Try constant struct
318 if (LLVMIsAConstantStruct(Cst)) {
319 check_value_kind(Cst, LLVMConstantStructValueKind);
320 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
321 unsigned EltCount = LLVMCountStructElementTypes(Ty);
322 SmallVector<LLVMValueRef, 8> Elts;
323 for (unsigned i = 0; i < EltCount; i++)
324 Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
325 if (LLVMGetStructName(Ty))
326 return LLVMConstNamedStruct(Ty, Elts.data(), EltCount);
327 return LLVMConstStructInContext(LLVMGetModuleContext(M), Elts.data(),
328 EltCount, LLVMIsPackedStruct(Ty));
331 // Try ConstantPointerNull
332 if (LLVMIsAConstantPointerNull(Cst)) {
333 check_value_kind(Cst, LLVMConstantPointerNullValueKind);
334 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
335 return LLVMConstNull(Ty);
338 // Try undef
339 if (LLVMIsUndef(Cst)) {
340 check_value_kind(Cst, LLVMUndefValueValueKind);
341 return LLVMGetUndef(TypeCloner(M).Clone(Cst));
344 // Try poison
345 if (LLVMIsPoison(Cst)) {
346 check_value_kind(Cst, LLVMPoisonValueValueKind);
347 return LLVMGetPoison(TypeCloner(M).Clone(Cst));
350 // Try null
351 if (LLVMIsNull(Cst)) {
352 check_value_kind(Cst, LLVMConstantTokenNoneValueKind);
353 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
354 return LLVMConstNull(Ty);
357 // Try float literal
358 if (LLVMIsAConstantFP(Cst)) {
359 check_value_kind(Cst, LLVMConstantFPValueKind);
360 report_fatal_error("ConstantFP is not supported");
363 // Try ConstantVector or ConstantDataVector
364 if (LLVMIsAConstantVector(Cst) || LLVMIsAConstantDataVector(Cst)) {
365 check_value_kind(Cst, LLVMIsAConstantVector(Cst)
366 ? LLVMConstantVectorValueKind
367 : LLVMConstantDataVectorValueKind);
368 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
369 unsigned EltCount = LLVMGetVectorSize(Ty);
370 SmallVector<LLVMValueRef, 8> Elts;
371 for (unsigned i = 0; i < EltCount; i++)
372 Elts.push_back(clone_constant(LLVMGetAggregateElement(Cst, i), M));
373 return LLVMConstVector(Elts.data(), EltCount);
376 // At this point, if it's not a constant expression, it's a kind of constant
377 // which is not supported
378 if (!LLVMIsAConstantExpr(Cst))
379 report_fatal_error("Unsupported constant kind");
381 // At this point, it must be a constant expression
382 check_value_kind(Cst, LLVMConstantExprValueKind);
384 LLVMOpcode Op = LLVMGetConstOpcode(Cst);
385 switch(Op) {
386 case LLVMBitCast:
387 return LLVMConstBitCast(clone_constant(LLVMGetOperand(Cst, 0), M),
388 TypeCloner(M).Clone(Cst));
389 case LLVMGetElementPtr: {
390 LLVMTypeRef ElemTy =
391 TypeCloner(M).Clone(LLVMGetGEPSourceElementType(Cst));
392 LLVMValueRef Ptr = clone_constant(LLVMGetOperand(Cst, 0), M);
393 int NumIdx = LLVMGetNumIndices(Cst);
394 SmallVector<LLVMValueRef, 8> Idx;
395 for (int i = 1; i <= NumIdx; i++)
396 Idx.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
397 if (LLVMIsInBounds(Cst))
398 return LLVMConstInBoundsGEP2(ElemTy, Ptr, Idx.data(), NumIdx);
399 else
400 return LLVMConstGEP2(ElemTy, Ptr, Idx.data(), NumIdx);
402 default:
403 fprintf(stderr, "%d is not a supported opcode for constant expressions\n",
404 Op);
405 exit(-1);
409 static LLVMValueRef clone_inline_asm(LLVMValueRef Asm, LLVMModuleRef M) {
411 if (!LLVMIsAInlineAsm(Asm))
412 report_fatal_error("Expected inline assembly");
414 size_t AsmStringSize = 0;
415 const char *AsmString = LLVMGetInlineAsmAsmString(Asm, &AsmStringSize);
417 size_t ConstraintStringSize = 0;
418 const char *ConstraintString =
419 LLVMGetInlineAsmConstraintString(Asm, &ConstraintStringSize);
421 LLVMInlineAsmDialect AsmDialect = LLVMGetInlineAsmDialect(Asm);
423 LLVMTypeRef AsmFunctionType = LLVMGetInlineAsmFunctionType(Asm);
425 LLVMBool HasSideEffects = LLVMGetInlineAsmHasSideEffects(Asm);
426 LLVMBool NeedsAlignStack = LLVMGetInlineAsmNeedsAlignedStack(Asm);
427 LLVMBool CanUnwind = LLVMGetInlineAsmCanUnwind(Asm);
429 return LLVMGetInlineAsm(AsmFunctionType, AsmString, AsmStringSize,
430 ConstraintString, ConstraintStringSize,
431 HasSideEffects, NeedsAlignStack, AsmDialect,
432 CanUnwind);
435 struct FunCloner {
436 LLVMValueRef Fun;
437 LLVMModuleRef M;
439 ValueMap VMap;
440 BasicBlockMap BBMap;
442 FunCloner(LLVMValueRef Src, LLVMValueRef Dst): Fun(Dst),
443 M(LLVMGetGlobalParent(Fun)), VMap(clone_params(Src, Dst)) {}
445 LLVMTypeRef CloneType(LLVMTypeRef Src) {
446 return TypeCloner(M).Clone(Src);
449 LLVMTypeRef CloneType(LLVMValueRef Src) {
450 return TypeCloner(M).Clone(Src);
453 // Try to clone everything in the llvm::Value hierarchy.
454 LLVMValueRef CloneValue(LLVMValueRef Src) {
455 // First, the value may be constant.
456 if (LLVMIsAConstant(Src))
457 return clone_constant(Src, M);
459 // Function argument should always be in the map already.
460 auto i = VMap.find(Src);
461 if (i != VMap.end())
462 return i->second;
464 // Inline assembly is a Value, but not an Instruction
465 if (LLVMIsAInlineAsm(Src))
466 return clone_inline_asm(Src, M);
468 if (!LLVMIsAInstruction(Src))
469 report_fatal_error("Expected an instruction");
471 auto Ctx = LLVMGetModuleContext(M);
472 auto Builder = LLVMCreateBuilderInContext(Ctx);
473 auto BB = DeclareBB(LLVMGetInstructionParent(Src));
474 LLVMPositionBuilderAtEnd(Builder, BB);
475 auto Dst = CloneInstruction(Src, Builder);
476 LLVMDisposeBuilder(Builder);
477 return Dst;
480 void CloneAttrs(LLVMValueRef Src, LLVMValueRef Dst) {
481 auto Ctx = LLVMGetModuleContext(M);
482 int ArgCount = LLVMGetNumArgOperands(Src);
483 for (int i = LLVMAttributeReturnIndex; i <= ArgCount; i++) {
484 for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) {
485 if (auto SrcA = LLVMGetCallSiteEnumAttribute(Src, i, k)) {
486 auto Val = LLVMGetEnumAttributeValue(SrcA);
487 auto A = LLVMCreateEnumAttribute(Ctx, k, Val);
488 LLVMAddCallSiteAttribute(Dst, i, A);
494 LLVMValueRef CloneInstruction(LLVMValueRef Src, LLVMBuilderRef Builder) {
495 check_value_kind(Src, LLVMInstructionValueKind);
496 if (!LLVMIsAInstruction(Src))
497 report_fatal_error("Expected an instruction");
499 size_t NameLen;
500 const char *Name = LLVMGetValueName2(Src, &NameLen);
502 // Check if this is something we already computed.
504 auto i = VMap.find(Src);
505 if (i != VMap.end()) {
506 // If we have a hit, it means we already generated the instruction
507 // as a dependency to something else. We need to make sure
508 // it is ordered properly.
509 auto I = i->second;
510 LLVMInstructionRemoveFromParent(I);
511 LLVMInsertIntoBuilderWithName(Builder, I, Name);
512 return I;
516 // We tried everything, it must be an instruction
517 // that hasn't been generated already.
518 LLVMValueRef Dst = nullptr;
520 LLVMOpcode Op = LLVMGetInstructionOpcode(Src);
521 switch(Op) {
522 case LLVMRet: {
523 int OpCount = LLVMGetNumOperands(Src);
524 if (OpCount == 0)
525 Dst = LLVMBuildRetVoid(Builder);
526 else
527 Dst = LLVMBuildRet(Builder, CloneValue(LLVMGetOperand(Src, 0)));
528 break;
530 case LLVMBr: {
531 if (!LLVMIsConditional(Src)) {
532 LLVMValueRef SrcOp = LLVMGetOperand(Src, 0);
533 LLVMBasicBlockRef SrcBB = LLVMValueAsBasicBlock(SrcOp);
534 Dst = LLVMBuildBr(Builder, DeclareBB(SrcBB));
535 break;
538 LLVMValueRef Cond = LLVMGetCondition(Src);
539 LLVMValueRef Else = LLVMGetOperand(Src, 1);
540 LLVMBasicBlockRef ElseBB = DeclareBB(LLVMValueAsBasicBlock(Else));
541 LLVMValueRef Then = LLVMGetOperand(Src, 2);
542 LLVMBasicBlockRef ThenBB = DeclareBB(LLVMValueAsBasicBlock(Then));
543 Dst = LLVMBuildCondBr(Builder, CloneValue(Cond), ThenBB, ElseBB);
544 break;
546 case LLVMSwitch:
547 case LLVMIndirectBr:
548 break;
549 case LLVMInvoke: {
550 SmallVector<LLVMValueRef, 8> Args;
551 int ArgCount = LLVMGetNumArgOperands(Src);
552 for (int i = 0; i < ArgCount; i++)
553 Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
554 LLVMTypeRef FnTy = CloneType(LLVMGetCalledFunctionType(Src));
555 LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
556 LLVMBasicBlockRef Then = DeclareBB(LLVMGetNormalDest(Src));
557 LLVMBasicBlockRef Unwind = DeclareBB(LLVMGetUnwindDest(Src));
558 Dst = LLVMBuildInvoke2(Builder, FnTy, Fn, Args.data(), ArgCount,
559 Then, Unwind, Name);
560 CloneAttrs(Src, Dst);
561 break;
563 case LLVMUnreachable:
564 Dst = LLVMBuildUnreachable(Builder);
565 break;
566 case LLVMAdd: {
567 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
568 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
569 LLVMBool NUW = LLVMGetNUW(Src);
570 LLVMBool NSW = LLVMGetNSW(Src);
571 Dst = LLVMBuildAdd(Builder, LHS, RHS, Name);
572 LLVMSetNUW(Dst, NUW);
573 LLVMSetNSW(Dst, NSW);
574 break;
576 case LLVMSub: {
577 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
578 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
579 LLVMBool NUW = LLVMGetNUW(Src);
580 LLVMBool NSW = LLVMGetNSW(Src);
581 Dst = LLVMBuildSub(Builder, LHS, RHS, Name);
582 LLVMSetNUW(Dst, NUW);
583 LLVMSetNSW(Dst, NSW);
584 break;
586 case LLVMMul: {
587 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
588 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
589 LLVMBool NUW = LLVMGetNUW(Src);
590 LLVMBool NSW = LLVMGetNSW(Src);
591 Dst = LLVMBuildMul(Builder, LHS, RHS, Name);
592 LLVMSetNUW(Dst, NUW);
593 LLVMSetNSW(Dst, NSW);
594 break;
596 case LLVMUDiv: {
597 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
598 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
599 LLVMBool IsExact = LLVMGetExact(Src);
600 Dst = LLVMBuildUDiv(Builder, LHS, RHS, Name);
601 LLVMSetExact(Dst, IsExact);
602 break;
604 case LLVMSDiv: {
605 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
606 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
607 LLVMBool IsExact = LLVMGetExact(Src);
608 Dst = LLVMBuildSDiv(Builder, LHS, RHS, Name);
609 LLVMSetExact(Dst, IsExact);
610 break;
612 case LLVMURem: {
613 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
614 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
615 Dst = LLVMBuildURem(Builder, LHS, RHS, Name);
616 break;
618 case LLVMSRem: {
619 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
620 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
621 Dst = LLVMBuildSRem(Builder, LHS, RHS, Name);
622 break;
624 case LLVMShl: {
625 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
626 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
627 LLVMBool NUW = LLVMGetNUW(Src);
628 LLVMBool NSW = LLVMGetNSW(Src);
629 Dst = LLVMBuildShl(Builder, LHS, RHS, Name);
630 LLVMSetNUW(Dst, NUW);
631 LLVMSetNSW(Dst, NSW);
632 break;
634 case LLVMLShr: {
635 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
636 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
637 LLVMBool IsExact = LLVMGetExact(Src);
638 Dst = LLVMBuildLShr(Builder, LHS, RHS, Name);
639 LLVMSetExact(Dst, IsExact);
640 break;
642 case LLVMAShr: {
643 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
644 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
645 LLVMBool IsExact = LLVMGetExact(Src);
646 Dst = LLVMBuildAShr(Builder, LHS, RHS, Name);
647 LLVMSetExact(Dst, IsExact);
648 break;
650 case LLVMAnd: {
651 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
652 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
653 Dst = LLVMBuildAnd(Builder, LHS, RHS, Name);
654 break;
656 case LLVMOr: {
657 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
658 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
659 Dst = LLVMBuildOr(Builder, LHS, RHS, Name);
660 break;
662 case LLVMXor: {
663 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
664 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
665 Dst = LLVMBuildXor(Builder, LHS, RHS, Name);
666 break;
668 case LLVMAlloca: {
669 LLVMTypeRef Ty = CloneType(LLVMGetAllocatedType(Src));
670 Dst = LLVMBuildAlloca(Builder, Ty, Name);
671 LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
672 break;
674 case LLVMLoad: {
675 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
676 Dst = LLVMBuildLoad2(Builder, CloneType(Src), Ptr, Name);
677 LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
678 LLVMSetOrdering(Dst, LLVMGetOrdering(Src));
679 LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
680 LLVMSetAtomicSingleThread(Dst, LLVMIsAtomicSingleThread(Src));
681 break;
683 case LLVMStore: {
684 LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 0));
685 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 1));
686 Dst = LLVMBuildStore(Builder, Val, Ptr);
687 LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
688 LLVMSetOrdering(Dst, LLVMGetOrdering(Src));
689 LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
690 LLVMSetAtomicSingleThread(Dst, LLVMIsAtomicSingleThread(Src));
691 break;
693 case LLVMGetElementPtr: {
694 LLVMTypeRef ElemTy = CloneType(LLVMGetGEPSourceElementType(Src));
695 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
696 SmallVector<LLVMValueRef, 8> Idx;
697 int NumIdx = LLVMGetNumIndices(Src);
698 for (int i = 1; i <= NumIdx; i++)
699 Idx.push_back(CloneValue(LLVMGetOperand(Src, i)));
700 if (LLVMIsInBounds(Src))
701 Dst = LLVMBuildInBoundsGEP2(Builder, ElemTy, Ptr, Idx.data(), NumIdx,
702 Name);
703 else
704 Dst = LLVMBuildGEP2(Builder, ElemTy, Ptr, Idx.data(), NumIdx, Name);
705 break;
707 case LLVMAtomicRMW: {
708 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
709 LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 1));
710 LLVMAtomicRMWBinOp BinOp = LLVMGetAtomicRMWBinOp(Src);
711 LLVMAtomicOrdering Ord = LLVMGetOrdering(Src);
712 LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src);
713 Dst = LLVMBuildAtomicRMW(Builder, BinOp, Ptr, Val, Ord, SingleThread);
714 LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
715 LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
716 LLVMSetValueName2(Dst, Name, NameLen);
717 break;
719 case LLVMAtomicCmpXchg: {
720 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
721 LLVMValueRef Cmp = CloneValue(LLVMGetOperand(Src, 1));
722 LLVMValueRef New = CloneValue(LLVMGetOperand(Src, 2));
723 LLVMAtomicOrdering Succ = LLVMGetCmpXchgSuccessOrdering(Src);
724 LLVMAtomicOrdering Fail = LLVMGetCmpXchgFailureOrdering(Src);
725 LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src);
727 Dst = LLVMBuildAtomicCmpXchg(Builder, Ptr, Cmp, New, Succ, Fail,
728 SingleThread);
729 LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
730 LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
731 LLVMSetWeak(Dst, LLVMGetWeak(Src));
732 LLVMSetValueName2(Dst, Name, NameLen);
733 break;
735 case LLVMBitCast: {
736 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 0));
737 Dst = LLVMBuildBitCast(Builder, V, CloneType(Src), Name);
738 break;
740 case LLVMICmp: {
741 LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src);
742 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
743 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
744 Dst = LLVMBuildICmp(Builder, Pred, LHS, RHS, Name);
745 break;
747 case LLVMPHI: {
748 // We need to aggressively set things here because of loops.
749 VMap[Src] = Dst = LLVMBuildPhi(Builder, CloneType(Src), Name);
751 SmallVector<LLVMValueRef, 8> Values;
752 SmallVector<LLVMBasicBlockRef, 8> Blocks;
754 unsigned IncomingCount = LLVMCountIncoming(Src);
755 for (unsigned i = 0; i < IncomingCount; ++i) {
756 Blocks.push_back(DeclareBB(LLVMGetIncomingBlock(Src, i)));
757 Values.push_back(CloneValue(LLVMGetIncomingValue(Src, i)));
760 LLVMAddIncoming(Dst, Values.data(), Blocks.data(), IncomingCount);
761 return Dst;
763 case LLVMCall: {
764 SmallVector<LLVMValueRef, 8> Args;
765 int ArgCount = LLVMGetNumArgOperands(Src);
766 for (int i = 0; i < ArgCount; i++)
767 Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
768 LLVMTypeRef FnTy = CloneType(LLVMGetCalledFunctionType(Src));
769 LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
770 Dst = LLVMBuildCall2(Builder, FnTy, Fn, Args.data(), ArgCount, Name);
771 LLVMSetTailCallKind(Dst, LLVMGetTailCallKind(Src));
772 CloneAttrs(Src, Dst);
773 break;
775 case LLVMResume: {
776 Dst = LLVMBuildResume(Builder, CloneValue(LLVMGetOperand(Src, 0)));
777 break;
779 case LLVMLandingPad: {
780 // The landing pad API is a bit screwed up for historical reasons.
781 Dst = LLVMBuildLandingPad(Builder, CloneType(Src), nullptr, 0, Name);
782 unsigned NumClauses = LLVMGetNumClauses(Src);
783 for (unsigned i = 0; i < NumClauses; ++i)
784 LLVMAddClause(Dst, CloneValue(LLVMGetClause(Src, i)));
785 LLVMSetCleanup(Dst, LLVMIsCleanup(Src));
786 break;
788 case LLVMCleanupRet: {
789 LLVMValueRef CatchPad = CloneValue(LLVMGetOperand(Src, 0));
790 LLVMBasicBlockRef Unwind = nullptr;
791 if (LLVMBasicBlockRef UDest = LLVMGetUnwindDest(Src))
792 Unwind = DeclareBB(UDest);
793 Dst = LLVMBuildCleanupRet(Builder, CatchPad, Unwind);
794 break;
796 case LLVMCatchRet: {
797 LLVMValueRef CatchPad = CloneValue(LLVMGetOperand(Src, 0));
798 LLVMBasicBlockRef SuccBB = DeclareBB(LLVMGetSuccessor(Src, 0));
799 Dst = LLVMBuildCatchRet(Builder, CatchPad, SuccBB);
800 break;
802 case LLVMCatchPad: {
803 LLVMValueRef ParentPad = CloneValue(LLVMGetParentCatchSwitch(Src));
804 SmallVector<LLVMValueRef, 8> Args;
805 int ArgCount = LLVMGetNumArgOperands(Src);
806 for (int i = 0; i < ArgCount; i++)
807 Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
808 Dst = LLVMBuildCatchPad(Builder, ParentPad,
809 Args.data(), ArgCount, Name);
810 break;
812 case LLVMCleanupPad: {
813 LLVMValueRef ParentPad = CloneValue(LLVMGetOperand(Src, 0));
814 SmallVector<LLVMValueRef, 8> Args;
815 int ArgCount = LLVMGetNumArgOperands(Src);
816 for (int i = 0; i < ArgCount; i++)
817 Args.push_back(CloneValue(LLVMGetArgOperand(Src, i)));
818 Dst = LLVMBuildCleanupPad(Builder, ParentPad,
819 Args.data(), ArgCount, Name);
820 break;
822 case LLVMCatchSwitch: {
823 LLVMValueRef ParentPad = CloneValue(LLVMGetOperand(Src, 0));
824 LLVMBasicBlockRef UnwindBB = nullptr;
825 if (LLVMBasicBlockRef UDest = LLVMGetUnwindDest(Src)) {
826 UnwindBB = DeclareBB(UDest);
828 unsigned NumHandlers = LLVMGetNumHandlers(Src);
829 Dst = LLVMBuildCatchSwitch(Builder, ParentPad, UnwindBB, NumHandlers, Name);
830 if (NumHandlers > 0) {
831 LLVMBasicBlockRef *Handlers = static_cast<LLVMBasicBlockRef*>(
832 safe_malloc(NumHandlers * sizeof(LLVMBasicBlockRef)));
833 LLVMGetHandlers(Src, Handlers);
834 for (unsigned i = 0; i < NumHandlers; i++)
835 LLVMAddHandler(Dst, DeclareBB(Handlers[i]));
836 free(Handlers);
838 break;
840 case LLVMExtractValue: {
841 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
842 if (LLVMGetNumIndices(Src) > 1)
843 report_fatal_error("ExtractValue: Expected only one index");
844 else if (LLVMGetNumIndices(Src) < 1)
845 report_fatal_error("ExtractValue: Expected an index");
846 auto I = LLVMGetIndices(Src)[0];
847 Dst = LLVMBuildExtractValue(Builder, Agg, I, Name);
848 break;
850 case LLVMInsertValue: {
851 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
852 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 1));
853 if (LLVMGetNumIndices(Src) > 1)
854 report_fatal_error("InsertValue: Expected only one index");
855 else if (LLVMGetNumIndices(Src) < 1)
856 report_fatal_error("InsertValue: Expected an index");
857 auto I = LLVMGetIndices(Src)[0];
858 Dst = LLVMBuildInsertValue(Builder, Agg, V, I, Name);
859 break;
861 case LLVMExtractElement: {
862 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
863 LLVMValueRef Index = CloneValue(LLVMGetOperand(Src, 1));
864 Dst = LLVMBuildExtractElement(Builder, Agg, Index, Name);
865 break;
867 case LLVMInsertElement: {
868 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
869 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 1));
870 LLVMValueRef Index = CloneValue(LLVMGetOperand(Src, 2));
871 Dst = LLVMBuildInsertElement(Builder, Agg, V, Index, Name);
872 break;
874 case LLVMShuffleVector: {
875 LLVMValueRef Agg0 = CloneValue(LLVMGetOperand(Src, 0));
876 LLVMValueRef Agg1 = CloneValue(LLVMGetOperand(Src, 1));
877 SmallVector<LLVMValueRef, 8> MaskElts;
878 unsigned NumMaskElts = LLVMGetNumMaskElements(Src);
879 for (unsigned i = 0; i < NumMaskElts; i++) {
880 int Val = LLVMGetMaskValue(Src, i);
881 if (Val == LLVMGetUndefMaskElem()) {
882 MaskElts.push_back(LLVMGetUndef(LLVMInt64Type()));
883 } else {
884 MaskElts.push_back(LLVMConstInt(LLVMInt64Type(), Val, true));
887 LLVMValueRef Mask = LLVMConstVector(MaskElts.data(), NumMaskElts);
888 Dst = LLVMBuildShuffleVector(Builder, Agg0, Agg1, Mask, Name);
889 break;
891 case LLVMFreeze: {
892 LLVMValueRef Arg = CloneValue(LLVMGetOperand(Src, 0));
893 Dst = LLVMBuildFreeze(Builder, Arg, Name);
894 break;
896 case LLVMFence: {
897 LLVMAtomicOrdering Ordering = LLVMGetOrdering(Src);
898 LLVMBool IsSingleThreaded = LLVMIsAtomicSingleThread(Src);
899 Dst = LLVMBuildFence(Builder, Ordering, IsSingleThreaded, Name);
900 break;
902 default:
903 break;
906 if (Dst == nullptr) {
907 fprintf(stderr, "%d is not a supported opcode\n", Op);
908 exit(-1);
911 auto Ctx = LLVMGetModuleContext(M);
912 size_t NumMetadataEntries;
913 auto *AllMetadata =
914 LLVMInstructionGetAllMetadataOtherThanDebugLoc(Src,
915 &NumMetadataEntries);
916 for (unsigned i = 0; i < NumMetadataEntries; ++i) {
917 unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i);
918 LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i);
919 LLVMSetMetadata(Dst, Kind, LLVMMetadataAsValue(Ctx, MD));
921 LLVMDisposeValueMetadataEntries(AllMetadata);
922 LLVMAddMetadataToInst(Builder, Dst);
924 check_value_kind(Dst, LLVMInstructionValueKind);
925 return VMap[Src] = Dst;
928 LLVMBasicBlockRef DeclareBB(LLVMBasicBlockRef Src) {
929 // Check if this is something we already computed.
931 auto i = BBMap.find(Src);
932 if (i != BBMap.end()) {
933 return i->second;
937 LLVMValueRef V = LLVMBasicBlockAsValue(Src);
938 if (!LLVMValueIsBasicBlock(V) || LLVMValueAsBasicBlock(V) != Src)
939 report_fatal_error("Basic block is not a basic block");
941 const char *Name = LLVMGetBasicBlockName(Src);
942 size_t NameLen;
943 const char *VName = LLVMGetValueName2(V, &NameLen);
944 if (Name != VName)
945 report_fatal_error("Basic block name mismatch");
947 LLVMBasicBlockRef BB = LLVMAppendBasicBlock(Fun, Name);
948 return BBMap[Src] = BB;
951 LLVMBasicBlockRef CloneBB(LLVMBasicBlockRef Src) {
952 LLVMBasicBlockRef BB = DeclareBB(Src);
954 // Make sure ordering is correct.
955 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Src);
956 if (Prev)
957 LLVMMoveBasicBlockAfter(BB, DeclareBB(Prev));
959 LLVMValueRef First = LLVMGetFirstInstruction(Src);
960 LLVMValueRef Last = LLVMGetLastInstruction(Src);
962 if (First == nullptr) {
963 if (Last != nullptr)
964 report_fatal_error("Has no first instruction, but last one");
965 return BB;
968 auto Ctx = LLVMGetModuleContext(M);
969 LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx);
970 LLVMPositionBuilderAtEnd(Builder, BB);
972 LLVMValueRef Cur = First;
973 LLVMValueRef Next = nullptr;
974 while(true) {
975 CloneInstruction(Cur, Builder);
976 Next = LLVMGetNextInstruction(Cur);
977 if (Next == nullptr) {
978 if (Cur != Last)
979 report_fatal_error("Final instruction does not match Last");
980 break;
983 LLVMValueRef Prev = LLVMGetPreviousInstruction(Next);
984 if (Prev != Cur)
985 report_fatal_error("Next.Previous instruction is not Current");
987 Cur = Next;
990 LLVMDisposeBuilder(Builder);
991 return BB;
994 void CloneBBs(LLVMValueRef Src) {
995 unsigned Count = LLVMCountBasicBlocks(Src);
996 if (Count == 0)
997 return;
999 LLVMBasicBlockRef First = LLVMGetFirstBasicBlock(Src);
1000 LLVMBasicBlockRef Last = LLVMGetLastBasicBlock(Src);
1002 LLVMBasicBlockRef Cur = First;
1003 LLVMBasicBlockRef Next = nullptr;
1004 while(true) {
1005 CloneBB(Cur);
1006 Count--;
1007 Next = LLVMGetNextBasicBlock(Cur);
1008 if (Next == nullptr) {
1009 if (Cur != Last)
1010 report_fatal_error("Final basic block does not match Last");
1011 break;
1014 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Next);
1015 if (Prev != Cur)
1016 report_fatal_error("Next.Previous basic bloc is not Current");
1018 Cur = Next;
1021 if (Count != 0)
1022 report_fatal_error("Basic block count does not match iterration");
1026 static void declare_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
1027 auto Ctx = LLVMGetModuleContext(M);
1029 LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
1030 LLVMValueRef End = LLVMGetLastGlobal(Src);
1032 LLVMValueRef Cur = Begin;
1033 LLVMValueRef Next = nullptr;
1034 if (!Begin) {
1035 if (End != nullptr)
1036 report_fatal_error("Range has an end but no beginning");
1037 goto FunDecl;
1040 while (true) {
1041 size_t NameLen;
1042 const char *Name = LLVMGetValueName2(Cur, &NameLen);
1043 if (LLVMGetNamedGlobal(M, Name))
1044 report_fatal_error("GlobalVariable already cloned");
1045 LLVMAddGlobal(M, TypeCloner(M).Clone(LLVMGlobalGetValueType(Cur)), Name);
1047 Next = LLVMGetNextGlobal(Cur);
1048 if (Next == nullptr) {
1049 if (Cur != End)
1050 report_fatal_error("");
1051 break;
1054 LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
1055 if (Prev != Cur)
1056 report_fatal_error("Next.Previous global is not Current");
1058 Cur = Next;
1061 FunDecl:
1062 Begin = LLVMGetFirstFunction(Src);
1063 End = LLVMGetLastFunction(Src);
1064 if (!Begin) {
1065 if (End != nullptr)
1066 report_fatal_error("Range has an end but no beginning");
1067 goto AliasDecl;
1070 Cur = Begin;
1071 Next = nullptr;
1072 while (true) {
1073 size_t NameLen;
1074 const char *Name = LLVMGetValueName2(Cur, &NameLen);
1075 if (LLVMGetNamedFunction(M, Name))
1076 report_fatal_error("Function already cloned");
1077 LLVMTypeRef Ty = TypeCloner(M).Clone(LLVMGlobalGetValueType(Cur));
1079 auto F = LLVMAddFunction(M, Name, Ty);
1081 // Copy attributes
1082 for (int i = LLVMAttributeFunctionIndex, c = LLVMCountParams(F);
1083 i <= c; ++i) {
1084 for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) {
1085 if (auto SrcA = LLVMGetEnumAttributeAtIndex(Cur, i, k)) {
1086 auto Val = LLVMGetEnumAttributeValue(SrcA);
1087 auto DstA = LLVMCreateEnumAttribute(Ctx, k, Val);
1088 LLVMAddAttributeAtIndex(F, i, DstA);
1093 Next = LLVMGetNextFunction(Cur);
1094 if (Next == nullptr) {
1095 if (Cur != End)
1096 report_fatal_error("Last function does not match End");
1097 break;
1100 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
1101 if (Prev != Cur)
1102 report_fatal_error("Next.Previous function is not Current");
1104 Cur = Next;
1107 AliasDecl:
1108 Begin = LLVMGetFirstGlobalAlias(Src);
1109 End = LLVMGetLastGlobalAlias(Src);
1110 if (!Begin) {
1111 if (End != nullptr)
1112 report_fatal_error("Range has an end but no beginning");
1113 goto GlobalIFuncDecl;
1116 Cur = Begin;
1117 Next = nullptr;
1118 while (true) {
1119 size_t NameLen;
1120 const char *Name = LLVMGetValueName2(Cur, &NameLen);
1121 if (LLVMGetNamedGlobalAlias(M, Name, NameLen))
1122 report_fatal_error("Global alias already cloned");
1123 LLVMTypeRef PtrType = TypeCloner(M).Clone(Cur);
1124 LLVMTypeRef ValType = TypeCloner(M).Clone(LLVMGlobalGetValueType(Cur));
1125 unsigned AddrSpace = LLVMGetPointerAddressSpace(PtrType);
1126 // FIXME: Allow NULL aliasee.
1127 LLVMAddAlias2(M, ValType, AddrSpace, LLVMGetUndef(PtrType), Name);
1129 Next = LLVMGetNextGlobalAlias(Cur);
1130 if (Next == nullptr) {
1131 if (Cur != End)
1132 report_fatal_error("");
1133 break;
1136 LLVMValueRef Prev = LLVMGetPreviousGlobalAlias(Next);
1137 if (Prev != Cur)
1138 report_fatal_error("Next.Previous global is not Current");
1140 Cur = Next;
1143 GlobalIFuncDecl:
1144 Begin = LLVMGetFirstGlobalIFunc(Src);
1145 End = LLVMGetLastGlobalIFunc(Src);
1146 if (!Begin) {
1147 if (End != nullptr)
1148 report_fatal_error("Range has an end but no beginning");
1149 goto NamedMDDecl;
1152 Cur = Begin;
1153 Next = nullptr;
1154 while (true) {
1155 size_t NameLen;
1156 const char *Name = LLVMGetValueName2(Cur, &NameLen);
1157 if (LLVMGetNamedGlobalIFunc(M, Name, NameLen))
1158 report_fatal_error("Global ifunc already cloned");
1159 LLVMTypeRef CurType = TypeCloner(M).Clone(LLVMGlobalGetValueType(Cur));
1160 // FIXME: Allow NULL resolver.
1161 LLVMAddGlobalIFunc(M, Name, NameLen,
1162 CurType, /*addressSpace*/ 0, LLVMGetUndef(CurType));
1164 Next = LLVMGetNextGlobalIFunc(Cur);
1165 if (Next == nullptr) {
1166 if (Cur != End)
1167 report_fatal_error("");
1168 break;
1171 LLVMValueRef Prev = LLVMGetPreviousGlobalIFunc(Next);
1172 if (Prev != Cur)
1173 report_fatal_error("Next.Previous global is not Current");
1175 Cur = Next;
1178 NamedMDDecl:
1179 LLVMNamedMDNodeRef BeginMD = LLVMGetFirstNamedMetadata(Src);
1180 LLVMNamedMDNodeRef EndMD = LLVMGetLastNamedMetadata(Src);
1181 if (!BeginMD) {
1182 if (EndMD != nullptr)
1183 report_fatal_error("Range has an end but no beginning");
1184 return;
1187 LLVMNamedMDNodeRef CurMD = BeginMD;
1188 LLVMNamedMDNodeRef NextMD = nullptr;
1189 while (true) {
1190 size_t NameLen;
1191 const char *Name = LLVMGetNamedMetadataName(CurMD, &NameLen);
1192 if (LLVMGetNamedMetadata(M, Name, NameLen))
1193 report_fatal_error("Named Metadata Node already cloned");
1194 LLVMGetOrInsertNamedMetadata(M, Name, NameLen);
1196 NextMD = LLVMGetNextNamedMetadata(CurMD);
1197 if (NextMD == nullptr) {
1198 if (CurMD != EndMD)
1199 report_fatal_error("");
1200 break;
1203 LLVMNamedMDNodeRef PrevMD = LLVMGetPreviousNamedMetadata(NextMD);
1204 if (PrevMD != CurMD)
1205 report_fatal_error("Next.Previous global is not Current");
1207 CurMD = NextMD;
1211 static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
1212 LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
1213 LLVMValueRef End = LLVMGetLastGlobal(Src);
1215 LLVMValueRef Cur = Begin;
1216 LLVMValueRef Next = nullptr;
1217 if (!Begin) {
1218 if (End != nullptr)
1219 report_fatal_error("Range has an end but no beginning");
1220 goto FunClone;
1223 while (true) {
1224 size_t NameLen;
1225 const char *Name = LLVMGetValueName2(Cur, &NameLen);
1226 LLVMValueRef G = LLVMGetNamedGlobal(M, Name);
1227 if (!G)
1228 report_fatal_error("GlobalVariable must have been declared already");
1230 if (auto I = LLVMGetInitializer(Cur))
1231 LLVMSetInitializer(G, clone_constant(I, M));
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(G, Kind, MD);
1240 LLVMDisposeValueMetadataEntries(AllMetadata);
1242 LLVMSetGlobalConstant(G, LLVMIsGlobalConstant(Cur));
1243 LLVMSetThreadLocal(G, LLVMIsThreadLocal(Cur));
1244 LLVMSetExternallyInitialized(G, LLVMIsExternallyInitialized(Cur));
1245 LLVMSetLinkage(G, LLVMGetLinkage(Cur));
1246 LLVMSetSection(G, LLVMGetSection(Cur));
1247 LLVMSetVisibility(G, LLVMGetVisibility(Cur));
1248 LLVMSetUnnamedAddress(G, LLVMGetUnnamedAddress(Cur));
1249 LLVMSetAlignment(G, LLVMGetAlignment(Cur));
1251 Next = LLVMGetNextGlobal(Cur);
1252 if (Next == nullptr) {
1253 if (Cur != End)
1254 report_fatal_error("");
1255 break;
1258 LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
1259 if (Prev != Cur)
1260 report_fatal_error("Next.Previous global is not Current");
1262 Cur = Next;
1265 FunClone:
1266 Begin = LLVMGetFirstFunction(Src);
1267 End = LLVMGetLastFunction(Src);
1268 if (!Begin) {
1269 if (End != nullptr)
1270 report_fatal_error("Range has an end but no beginning");
1271 goto AliasClone;
1274 Cur = Begin;
1275 Next = nullptr;
1276 while (true) {
1277 size_t NameLen;
1278 const char *Name = LLVMGetValueName2(Cur, &NameLen);
1279 LLVMValueRef Fun = LLVMGetNamedFunction(M, Name);
1280 if (!Fun)
1281 report_fatal_error("Function must have been declared already");
1283 if (LLVMHasPersonalityFn(Cur)) {
1284 size_t FNameLen;
1285 const char *FName = LLVMGetValueName2(LLVMGetPersonalityFn(Cur),
1286 &FNameLen);
1287 LLVMValueRef P = LLVMGetNamedFunction(M, FName);
1288 if (!P)
1289 report_fatal_error("Could not find personality function");
1290 LLVMSetPersonalityFn(Fun, P);
1293 size_t NumMetadataEntries;
1294 auto *AllMetadata = LLVMGlobalCopyAllMetadata(Cur, &NumMetadataEntries);
1295 for (unsigned i = 0; i < NumMetadataEntries; ++i) {
1296 unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i);
1297 LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i);
1298 LLVMGlobalSetMetadata(Fun, Kind, MD);
1300 LLVMDisposeValueMetadataEntries(AllMetadata);
1302 FunCloner FC(Cur, Fun);
1303 FC.CloneBBs(Cur);
1305 Next = LLVMGetNextFunction(Cur);
1306 if (Next == nullptr) {
1307 if (Cur != End)
1308 report_fatal_error("Last function does not match End");
1309 break;
1312 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
1313 if (Prev != Cur)
1314 report_fatal_error("Next.Previous function is not Current");
1316 Cur = Next;
1319 AliasClone:
1320 Begin = LLVMGetFirstGlobalAlias(Src);
1321 End = LLVMGetLastGlobalAlias(Src);
1322 if (!Begin) {
1323 if (End != nullptr)
1324 report_fatal_error("Range has an end but no beginning");
1325 goto GlobalIFuncClone;
1328 Cur = Begin;
1329 Next = nullptr;
1330 while (true) {
1331 size_t NameLen;
1332 const char *Name = LLVMGetValueName2(Cur, &NameLen);
1333 LLVMValueRef Alias = LLVMGetNamedGlobalAlias(M, Name, NameLen);
1334 if (!Alias)
1335 report_fatal_error("Global alias must have been declared already");
1337 if (LLVMValueRef Aliasee = LLVMAliasGetAliasee(Cur)) {
1338 LLVMAliasSetAliasee(Alias, clone_constant(Aliasee, M));
1341 LLVMSetLinkage(Alias, LLVMGetLinkage(Cur));
1342 LLVMSetUnnamedAddress(Alias, LLVMGetUnnamedAddress(Cur));
1344 Next = LLVMGetNextGlobalAlias(Cur);
1345 if (Next == nullptr) {
1346 if (Cur != End)
1347 report_fatal_error("Last global alias does not match End");
1348 break;
1351 LLVMValueRef Prev = LLVMGetPreviousGlobalAlias(Next);
1352 if (Prev != Cur)
1353 report_fatal_error("Next.Previous global alias is not Current");
1355 Cur = Next;
1358 GlobalIFuncClone:
1359 Begin = LLVMGetFirstGlobalIFunc(Src);
1360 End = LLVMGetLastGlobalIFunc(Src);
1361 if (!Begin) {
1362 if (End != nullptr)
1363 report_fatal_error("Range has an end but no beginning");
1364 goto NamedMDClone;
1367 Cur = Begin;
1368 Next = nullptr;
1369 while (true) {
1370 size_t NameLen;
1371 const char *Name = LLVMGetValueName2(Cur, &NameLen);
1372 LLVMValueRef IFunc = LLVMGetNamedGlobalIFunc(M, Name, NameLen);
1373 if (!IFunc)
1374 report_fatal_error("Global ifunc must have been declared already");
1376 if (LLVMValueRef Resolver = LLVMGetGlobalIFuncResolver(Cur)) {
1377 LLVMSetGlobalIFuncResolver(IFunc, clone_constant(Resolver, M));
1380 LLVMSetLinkage(IFunc, LLVMGetLinkage(Cur));
1381 LLVMSetUnnamedAddress(IFunc, LLVMGetUnnamedAddress(Cur));
1383 Next = LLVMGetNextGlobalIFunc(Cur);
1384 if (Next == nullptr) {
1385 if (Cur != End)
1386 report_fatal_error("Last global alias does not match End");
1387 break;
1390 LLVMValueRef Prev = LLVMGetPreviousGlobalIFunc(Next);
1391 if (Prev != Cur)
1392 report_fatal_error("Next.Previous global alias is not Current");
1394 Cur = Next;
1397 NamedMDClone:
1398 LLVMNamedMDNodeRef BeginMD = LLVMGetFirstNamedMetadata(Src);
1399 LLVMNamedMDNodeRef EndMD = LLVMGetLastNamedMetadata(Src);
1400 if (!BeginMD) {
1401 if (EndMD != nullptr)
1402 report_fatal_error("Range has an end but no beginning");
1403 return;
1406 LLVMNamedMDNodeRef CurMD = BeginMD;
1407 LLVMNamedMDNodeRef NextMD = nullptr;
1408 while (true) {
1409 size_t NameLen;
1410 const char *Name = LLVMGetNamedMetadataName(CurMD, &NameLen);
1411 LLVMNamedMDNodeRef NamedMD = LLVMGetNamedMetadata(M, Name, NameLen);
1412 if (!NamedMD)
1413 report_fatal_error("Named MD Node must have been declared already");
1415 unsigned OperandCount = LLVMGetNamedMetadataNumOperands(Src, Name);
1416 LLVMValueRef *OperandBuf = static_cast<LLVMValueRef *>(
1417 safe_malloc(OperandCount * sizeof(LLVMValueRef)));
1418 LLVMGetNamedMetadataOperands(Src, Name, OperandBuf);
1419 for (unsigned i = 0, e = OperandCount; i != e; ++i) {
1420 LLVMAddNamedMetadataOperand(M, Name, OperandBuf[i]);
1422 free(OperandBuf);
1424 NextMD = LLVMGetNextNamedMetadata(CurMD);
1425 if (NextMD == nullptr) {
1426 if (CurMD != EndMD)
1427 report_fatal_error("Last Named MD Node does not match End");
1428 break;
1431 LLVMNamedMDNodeRef PrevMD = LLVMGetPreviousNamedMetadata(NextMD);
1432 if (PrevMD != CurMD)
1433 report_fatal_error("Next.Previous Named MD Node is not Current");
1435 CurMD = NextMD;
1439 int llvm_echo(void) {
1440 LLVMEnablePrettyStackTrace();
1442 LLVMModuleRef Src = llvm_load_module(false, true);
1443 size_t SourceFileLen;
1444 const char *SourceFileName = LLVMGetSourceFileName(Src, &SourceFileLen);
1445 size_t ModuleIdentLen;
1446 const char *ModuleName = LLVMGetModuleIdentifier(Src, &ModuleIdentLen);
1447 LLVMContextRef Ctx = LLVMContextCreate();
1448 LLVMModuleRef M = LLVMModuleCreateWithNameInContext(ModuleName, Ctx);
1450 LLVMSetSourceFileName(M, SourceFileName, SourceFileLen);
1451 LLVMSetModuleIdentifier(M, ModuleName, ModuleIdentLen);
1453 LLVMSetTarget(M, LLVMGetTarget(Src));
1454 LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src));
1455 if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src)))
1456 report_fatal_error("Inconsistent DataLayout string representation");
1458 size_t ModuleInlineAsmLen;
1459 const char *ModuleAsm = LLVMGetModuleInlineAsm(Src, &ModuleInlineAsmLen);
1460 LLVMSetModuleInlineAsm2(M, ModuleAsm, ModuleInlineAsmLen);
1462 declare_symbols(Src, M);
1463 clone_symbols(Src, M);
1464 char *Str = LLVMPrintModuleToString(M);
1465 fputs(Str, stdout);
1467 LLVMDisposeMessage(Str);
1468 LLVMDisposeModule(Src);
1469 LLVMDisposeModule(M);
1470 LLVMContextDispose(Ctx);
1472 return 0;