Clang] Fix expansion of response files in -Wp after integrated-cc1 change
[llvm-project.git] / llvm / tools / llvm-c-test / echo.cpp
blob71b101596ee51378926a7e491c550f226767a7f1
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/Target.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/Support/ErrorHandling.h"
23 #include <stdio.h>
24 #include <stdlib.h>
26 using namespace llvm;
28 // Provide DenseMapInfo for C API opaque types.
29 template<typename T>
30 struct CAPIDenseMap {};
32 // The default DenseMapInfo require to know about pointer alignment.
33 // Because the C API uses opaques pointer types, their alignment is unknown.
34 // As a result, we need to roll out our own implementation.
35 template<typename T>
36 struct CAPIDenseMap<T*> {
37 struct CAPIDenseMapInfo {
38 static inline T* getEmptyKey() {
39 uintptr_t Val = static_cast<uintptr_t>(-1);
40 return reinterpret_cast<T*>(Val);
42 static inline T* getTombstoneKey() {
43 uintptr_t Val = static_cast<uintptr_t>(-2);
44 return reinterpret_cast<T*>(Val);
46 static unsigned getHashValue(const T *PtrVal) {
47 return hash_value(PtrVal);
49 static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; }
52 typedef DenseMap<T*, T*, CAPIDenseMapInfo> Map;
55 typedef CAPIDenseMap<LLVMValueRef>::Map ValueMap;
56 typedef CAPIDenseMap<LLVMBasicBlockRef>::Map BasicBlockMap;
58 struct TypeCloner {
59 LLVMModuleRef M;
60 LLVMContextRef Ctx;
62 TypeCloner(LLVMModuleRef M): M(M), Ctx(LLVMGetModuleContext(M)) {}
64 LLVMTypeRef Clone(LLVMValueRef Src) {
65 return Clone(LLVMTypeOf(Src));
68 LLVMTypeRef Clone(LLVMTypeRef Src) {
69 LLVMTypeKind Kind = LLVMGetTypeKind(Src);
70 switch (Kind) {
71 case LLVMVoidTypeKind:
72 return LLVMVoidTypeInContext(Ctx);
73 case LLVMHalfTypeKind:
74 return LLVMHalfTypeInContext(Ctx);
75 case LLVMFloatTypeKind:
76 return LLVMFloatTypeInContext(Ctx);
77 case LLVMDoubleTypeKind:
78 return LLVMDoubleTypeInContext(Ctx);
79 case LLVMX86_FP80TypeKind:
80 return LLVMX86FP80TypeInContext(Ctx);
81 case LLVMFP128TypeKind:
82 return LLVMFP128TypeInContext(Ctx);
83 case LLVMPPC_FP128TypeKind:
84 return LLVMPPCFP128TypeInContext(Ctx);
85 case LLVMLabelTypeKind:
86 return LLVMLabelTypeInContext(Ctx);
87 case LLVMIntegerTypeKind:
88 return LLVMIntTypeInContext(Ctx, LLVMGetIntTypeWidth(Src));
89 case LLVMFunctionTypeKind: {
90 unsigned ParamCount = LLVMCountParamTypes(Src);
91 LLVMTypeRef* Params = nullptr;
92 if (ParamCount > 0) {
93 Params = static_cast<LLVMTypeRef*>(
94 safe_malloc(ParamCount * sizeof(LLVMTypeRef)));
95 LLVMGetParamTypes(Src, Params);
96 for (unsigned i = 0; i < ParamCount; i++)
97 Params[i] = Clone(Params[i]);
100 LLVMTypeRef FunTy = LLVMFunctionType(Clone(LLVMGetReturnType(Src)),
101 Params, ParamCount,
102 LLVMIsFunctionVarArg(Src));
103 if (ParamCount > 0)
104 free(Params);
105 return FunTy;
107 case LLVMStructTypeKind: {
108 LLVMTypeRef S = nullptr;
109 const char *Name = LLVMGetStructName(Src);
110 if (Name) {
111 S = LLVMGetTypeByName(M, Name);
112 if (S)
113 return S;
114 S = LLVMStructCreateNamed(Ctx, Name);
115 if (LLVMIsOpaqueStruct(Src))
116 return S;
119 unsigned EltCount = LLVMCountStructElementTypes(Src);
120 SmallVector<LLVMTypeRef, 8> Elts;
121 for (unsigned i = 0; i < EltCount; i++)
122 Elts.push_back(Clone(LLVMStructGetTypeAtIndex(Src, i)));
123 if (Name)
124 LLVMStructSetBody(S, Elts.data(), EltCount, LLVMIsPackedStruct(Src));
125 else
126 S = LLVMStructTypeInContext(Ctx, Elts.data(), EltCount,
127 LLVMIsPackedStruct(Src));
128 return S;
130 case LLVMArrayTypeKind:
131 return LLVMArrayType(
132 Clone(LLVMGetElementType(Src)),
133 LLVMGetArrayLength(Src)
135 case LLVMPointerTypeKind:
136 return LLVMPointerType(
137 Clone(LLVMGetElementType(Src)),
138 LLVMGetPointerAddressSpace(Src)
140 case LLVMVectorTypeKind:
141 return LLVMVectorType(
142 Clone(LLVMGetElementType(Src)),
143 LLVMGetVectorSize(Src)
145 case LLVMMetadataTypeKind:
146 return LLVMMetadataTypeInContext(Ctx);
147 case LLVMX86_MMXTypeKind:
148 return LLVMX86MMXTypeInContext(Ctx);
149 case LLVMTokenTypeKind:
150 return LLVMTokenTypeInContext(Ctx);
153 fprintf(stderr, "%d is not a supported typekind\n", Kind);
154 exit(-1);
158 static ValueMap clone_params(LLVMValueRef Src, LLVMValueRef Dst) {
159 unsigned Count = LLVMCountParams(Src);
160 if (Count != LLVMCountParams(Dst))
161 report_fatal_error("Parameter count mismatch");
163 ValueMap VMap;
164 if (Count == 0)
165 return VMap;
167 LLVMValueRef SrcFirst = LLVMGetFirstParam(Src);
168 LLVMValueRef DstFirst = LLVMGetFirstParam(Dst);
169 LLVMValueRef SrcLast = LLVMGetLastParam(Src);
170 LLVMValueRef DstLast = LLVMGetLastParam(Dst);
172 LLVMValueRef SrcCur = SrcFirst;
173 LLVMValueRef DstCur = DstFirst;
174 LLVMValueRef SrcNext = nullptr;
175 LLVMValueRef DstNext = nullptr;
176 while (true) {
177 size_t NameLen;
178 const char *Name = LLVMGetValueName2(SrcCur, &NameLen);
179 LLVMSetValueName2(DstCur, Name, NameLen);
181 VMap[SrcCur] = DstCur;
183 Count--;
184 SrcNext = LLVMGetNextParam(SrcCur);
185 DstNext = LLVMGetNextParam(DstCur);
186 if (SrcNext == nullptr && DstNext == nullptr) {
187 if (SrcCur != SrcLast)
188 report_fatal_error("SrcLast param does not match End");
189 if (DstCur != DstLast)
190 report_fatal_error("DstLast param does not match End");
191 break;
194 if (SrcNext == nullptr)
195 report_fatal_error("SrcNext was unexpectedly null");
196 if (DstNext == nullptr)
197 report_fatal_error("DstNext was unexpectedly null");
199 LLVMValueRef SrcPrev = LLVMGetPreviousParam(SrcNext);
200 if (SrcPrev != SrcCur)
201 report_fatal_error("SrcNext.Previous param is not Current");
203 LLVMValueRef DstPrev = LLVMGetPreviousParam(DstNext);
204 if (DstPrev != DstCur)
205 report_fatal_error("DstNext.Previous param is not Current");
207 SrcCur = SrcNext;
208 DstCur = DstNext;
211 if (Count != 0)
212 report_fatal_error("Parameter count does not match iteration");
214 return VMap;
217 static void check_value_kind(LLVMValueRef V, LLVMValueKind K) {
218 if (LLVMGetValueKind(V) != K)
219 report_fatal_error("LLVMGetValueKind returned incorrect type");
222 static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M);
224 static LLVMValueRef clone_constant(LLVMValueRef Cst, LLVMModuleRef M) {
225 LLVMValueRef Ret = clone_constant_impl(Cst, M);
226 check_value_kind(Ret, LLVMGetValueKind(Cst));
227 return Ret;
230 static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M) {
231 if (!LLVMIsAConstant(Cst))
232 report_fatal_error("Expected a constant");
234 // Maybe it is a symbol
235 if (LLVMIsAGlobalValue(Cst)) {
236 size_t NameLen;
237 const char *Name = LLVMGetValueName2(Cst, &NameLen);
239 // Try function
240 if (LLVMIsAFunction(Cst)) {
241 check_value_kind(Cst, LLVMFunctionValueKind);
243 LLVMValueRef Dst = nullptr;
244 // Try an intrinsic
245 unsigned ID = LLVMGetIntrinsicID(Cst);
246 if (ID > 0 && !LLVMIntrinsicIsOverloaded(ID)) {
247 Dst = LLVMGetIntrinsicDeclaration(M, ID, nullptr, 0);
248 } else {
249 // Try a normal function
250 Dst = LLVMGetNamedFunction(M, Name);
253 if (Dst)
254 return Dst;
255 report_fatal_error("Could not find function");
258 // Try global variable
259 if (LLVMIsAGlobalVariable(Cst)) {
260 check_value_kind(Cst, LLVMGlobalVariableValueKind);
261 LLVMValueRef Dst = LLVMGetNamedGlobal(M, Name);
262 if (Dst)
263 return Dst;
264 report_fatal_error("Could not find variable");
267 // Try global alias
268 if (LLVMIsAGlobalAlias(Cst)) {
269 check_value_kind(Cst, LLVMGlobalAliasValueKind);
270 LLVMValueRef Dst = LLVMGetNamedGlobalAlias(M, Name, NameLen);
271 if (Dst)
272 return Dst;
273 report_fatal_error("Could not find alias");
276 fprintf(stderr, "Could not find @%s\n", Name);
277 exit(-1);
280 // Try integer literal
281 if (LLVMIsAConstantInt(Cst)) {
282 check_value_kind(Cst, LLVMConstantIntValueKind);
283 return LLVMConstInt(TypeCloner(M).Clone(Cst),
284 LLVMConstIntGetZExtValue(Cst), false);
287 // Try zeroinitializer
288 if (LLVMIsAConstantAggregateZero(Cst)) {
289 check_value_kind(Cst, LLVMConstantAggregateZeroValueKind);
290 return LLVMConstNull(TypeCloner(M).Clone(Cst));
293 // Try constant array
294 if (LLVMIsAConstantArray(Cst)) {
295 check_value_kind(Cst, LLVMConstantArrayValueKind);
296 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
297 unsigned EltCount = LLVMGetArrayLength(Ty);
298 SmallVector<LLVMValueRef, 8> Elts;
299 for (unsigned i = 0; i < EltCount; i++)
300 Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
301 return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
304 // Try contant data array
305 if (LLVMIsAConstantDataArray(Cst)) {
306 check_value_kind(Cst, LLVMConstantDataArrayValueKind);
307 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
308 unsigned EltCount = LLVMGetArrayLength(Ty);
309 SmallVector<LLVMValueRef, 8> Elts;
310 for (unsigned i = 0; i < EltCount; i++)
311 Elts.push_back(clone_constant(LLVMGetElementAsConstant(Cst, i), M));
312 return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
315 // Try constant struct
316 if (LLVMIsAConstantStruct(Cst)) {
317 check_value_kind(Cst, LLVMConstantStructValueKind);
318 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
319 unsigned EltCount = LLVMCountStructElementTypes(Ty);
320 SmallVector<LLVMValueRef, 8> Elts;
321 for (unsigned i = 0; i < EltCount; i++)
322 Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
323 if (LLVMGetStructName(Ty))
324 return LLVMConstNamedStruct(Ty, Elts.data(), EltCount);
325 return LLVMConstStructInContext(LLVMGetModuleContext(M), Elts.data(),
326 EltCount, LLVMIsPackedStruct(Ty));
329 // Try ConstantPointerNull
330 if (LLVMIsAConstantPointerNull(Cst)) {
331 check_value_kind(Cst, LLVMConstantPointerNullValueKind);
332 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
333 return LLVMConstNull(Ty);
336 // Try undef
337 if (LLVMIsUndef(Cst)) {
338 check_value_kind(Cst, LLVMUndefValueValueKind);
339 return LLVMGetUndef(TypeCloner(M).Clone(Cst));
342 // Try null
343 if (LLVMIsNull(Cst)) {
344 check_value_kind(Cst, LLVMConstantTokenNoneValueKind);
345 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
346 return LLVMConstNull(Ty);
349 // Try float literal
350 if (LLVMIsAConstantFP(Cst)) {
351 check_value_kind(Cst, LLVMConstantFPValueKind);
352 report_fatal_error("ConstantFP is not supported");
355 // This kind of constant is not supported
356 if (!LLVMIsAConstantExpr(Cst))
357 report_fatal_error("Expected a constant expression");
359 // At this point, it must be a constant expression
360 check_value_kind(Cst, LLVMConstantExprValueKind);
362 LLVMOpcode Op = LLVMGetConstOpcode(Cst);
363 switch(Op) {
364 case LLVMBitCast:
365 return LLVMConstBitCast(clone_constant(LLVMGetOperand(Cst, 0), M),
366 TypeCloner(M).Clone(Cst));
367 default:
368 fprintf(stderr, "%d is not a supported opcode\n", Op);
369 exit(-1);
373 struct FunCloner {
374 LLVMValueRef Fun;
375 LLVMModuleRef M;
377 ValueMap VMap;
378 BasicBlockMap BBMap;
380 FunCloner(LLVMValueRef Src, LLVMValueRef Dst): Fun(Dst),
381 M(LLVMGetGlobalParent(Fun)), VMap(clone_params(Src, Dst)) {}
383 LLVMTypeRef CloneType(LLVMTypeRef Src) {
384 return TypeCloner(M).Clone(Src);
387 LLVMTypeRef CloneType(LLVMValueRef Src) {
388 return TypeCloner(M).Clone(Src);
391 // Try to clone everything in the llvm::Value hierarchy.
392 LLVMValueRef CloneValue(LLVMValueRef Src) {
393 // First, the value may be constant.
394 if (LLVMIsAConstant(Src))
395 return clone_constant(Src, M);
397 // Function argument should always be in the map already.
398 auto i = VMap.find(Src);
399 if (i != VMap.end())
400 return i->second;
402 if (!LLVMIsAInstruction(Src))
403 report_fatal_error("Expected an instruction");
405 auto Ctx = LLVMGetModuleContext(M);
406 auto Builder = LLVMCreateBuilderInContext(Ctx);
407 auto BB = DeclareBB(LLVMGetInstructionParent(Src));
408 LLVMPositionBuilderAtEnd(Builder, BB);
409 auto Dst = CloneInstruction(Src, Builder);
410 LLVMDisposeBuilder(Builder);
411 return Dst;
414 void CloneAttrs(LLVMValueRef Src, LLVMValueRef Dst) {
415 auto Ctx = LLVMGetModuleContext(M);
416 int ArgCount = LLVMGetNumArgOperands(Src);
417 for (int i = LLVMAttributeReturnIndex; i <= ArgCount; i++) {
418 for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) {
419 if (auto SrcA = LLVMGetCallSiteEnumAttribute(Src, i, k)) {
420 auto Val = LLVMGetEnumAttributeValue(SrcA);
421 auto A = LLVMCreateEnumAttribute(Ctx, k, Val);
422 LLVMAddCallSiteAttribute(Dst, i, A);
428 LLVMValueRef CloneInstruction(LLVMValueRef Src, LLVMBuilderRef Builder) {
429 check_value_kind(Src, LLVMInstructionValueKind);
430 if (!LLVMIsAInstruction(Src))
431 report_fatal_error("Expected an instruction");
433 size_t NameLen;
434 const char *Name = LLVMGetValueName2(Src, &NameLen);
436 // Check if this is something we already computed.
438 auto i = VMap.find(Src);
439 if (i != VMap.end()) {
440 // If we have a hit, it means we already generated the instruction
441 // as a dependancy to somethign else. We need to make sure
442 // it is ordered properly.
443 auto I = i->second;
444 LLVMInstructionRemoveFromParent(I);
445 LLVMInsertIntoBuilderWithName(Builder, I, Name);
446 return I;
450 // We tried everything, it must be an instruction
451 // that hasn't been generated already.
452 LLVMValueRef Dst = nullptr;
454 LLVMOpcode Op = LLVMGetInstructionOpcode(Src);
455 switch(Op) {
456 case LLVMRet: {
457 int OpCount = LLVMGetNumOperands(Src);
458 if (OpCount == 0)
459 Dst = LLVMBuildRetVoid(Builder);
460 else
461 Dst = LLVMBuildRet(Builder, CloneValue(LLVMGetOperand(Src, 0)));
462 break;
464 case LLVMBr: {
465 if (!LLVMIsConditional(Src)) {
466 LLVMValueRef SrcOp = LLVMGetOperand(Src, 0);
467 LLVMBasicBlockRef SrcBB = LLVMValueAsBasicBlock(SrcOp);
468 Dst = LLVMBuildBr(Builder, DeclareBB(SrcBB));
469 break;
472 LLVMValueRef Cond = LLVMGetCondition(Src);
473 LLVMValueRef Else = LLVMGetOperand(Src, 1);
474 LLVMBasicBlockRef ElseBB = DeclareBB(LLVMValueAsBasicBlock(Else));
475 LLVMValueRef Then = LLVMGetOperand(Src, 2);
476 LLVMBasicBlockRef ThenBB = DeclareBB(LLVMValueAsBasicBlock(Then));
477 Dst = LLVMBuildCondBr(Builder, CloneValue(Cond), ThenBB, ElseBB);
478 break;
480 case LLVMSwitch:
481 case LLVMIndirectBr:
482 break;
483 case LLVMInvoke: {
484 SmallVector<LLVMValueRef, 8> Args;
485 int ArgCount = LLVMGetNumArgOperands(Src);
486 for (int i = 0; i < ArgCount; i++)
487 Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
488 LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
489 LLVMBasicBlockRef Then = DeclareBB(LLVMGetNormalDest(Src));
490 LLVMBasicBlockRef Unwind = DeclareBB(LLVMGetUnwindDest(Src));
491 Dst = LLVMBuildInvoke(Builder, Fn, Args.data(), ArgCount,
492 Then, Unwind, Name);
493 CloneAttrs(Src, Dst);
494 break;
496 case LLVMUnreachable:
497 Dst = LLVMBuildUnreachable(Builder);
498 break;
499 case LLVMAdd: {
500 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
501 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
502 Dst = LLVMBuildAdd(Builder, LHS, RHS, Name);
503 break;
505 case LLVMSub: {
506 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
507 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
508 Dst = LLVMBuildSub(Builder, LHS, RHS, Name);
509 break;
511 case LLVMMul: {
512 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
513 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
514 Dst = LLVMBuildMul(Builder, LHS, RHS, Name);
515 break;
517 case LLVMUDiv: {
518 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
519 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
520 Dst = LLVMBuildUDiv(Builder, LHS, RHS, Name);
521 break;
523 case LLVMSDiv: {
524 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
525 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
526 Dst = LLVMBuildSDiv(Builder, LHS, RHS, Name);
527 break;
529 case LLVMURem: {
530 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
531 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
532 Dst = LLVMBuildURem(Builder, LHS, RHS, Name);
533 break;
535 case LLVMSRem: {
536 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
537 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
538 Dst = LLVMBuildSRem(Builder, LHS, RHS, Name);
539 break;
541 case LLVMShl: {
542 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
543 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
544 Dst = LLVMBuildShl(Builder, LHS, RHS, Name);
545 break;
547 case LLVMLShr: {
548 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
549 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
550 Dst = LLVMBuildLShr(Builder, LHS, RHS, Name);
551 break;
553 case LLVMAShr: {
554 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
555 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
556 Dst = LLVMBuildAShr(Builder, LHS, RHS, Name);
557 break;
559 case LLVMAnd: {
560 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
561 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
562 Dst = LLVMBuildAnd(Builder, LHS, RHS, Name);
563 break;
565 case LLVMOr: {
566 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
567 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
568 Dst = LLVMBuildOr(Builder, LHS, RHS, Name);
569 break;
571 case LLVMXor: {
572 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
573 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
574 Dst = LLVMBuildXor(Builder, LHS, RHS, Name);
575 break;
577 case LLVMAlloca: {
578 LLVMTypeRef Ty = CloneType(LLVMGetAllocatedType(Src));
579 Dst = LLVMBuildAlloca(Builder, Ty, Name);
580 break;
582 case LLVMLoad: {
583 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
584 Dst = LLVMBuildLoad(Builder, Ptr, Name);
585 LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
586 LLVMSetOrdering(Dst, LLVMGetOrdering(Src));
587 LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
588 break;
590 case LLVMStore: {
591 LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 0));
592 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 1));
593 Dst = LLVMBuildStore(Builder, Val, Ptr);
594 LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
595 LLVMSetOrdering(Dst, LLVMGetOrdering(Src));
596 LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
597 break;
599 case LLVMGetElementPtr: {
600 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
601 SmallVector<LLVMValueRef, 8> Idx;
602 int NumIdx = LLVMGetNumIndices(Src);
603 for (int i = 1; i <= NumIdx; i++)
604 Idx.push_back(CloneValue(LLVMGetOperand(Src, i)));
605 if (LLVMIsInBounds(Src))
606 Dst = LLVMBuildInBoundsGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
607 else
608 Dst = LLVMBuildGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
609 break;
611 case LLVMAtomicRMW: {
612 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
613 LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 1));
614 LLVMAtomicRMWBinOp BinOp = LLVMGetAtomicRMWBinOp(Src);
615 LLVMAtomicOrdering Ord = LLVMGetOrdering(Src);
616 LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src);
617 Dst = LLVMBuildAtomicRMW(Builder, BinOp, Ptr, Val, Ord, SingleThread);
618 LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
619 LLVMSetValueName2(Dst, Name, NameLen);
620 break;
622 case LLVMAtomicCmpXchg: {
623 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
624 LLVMValueRef Cmp = CloneValue(LLVMGetOperand(Src, 1));
625 LLVMValueRef New = CloneValue(LLVMGetOperand(Src, 2));
626 LLVMAtomicOrdering Succ = LLVMGetCmpXchgSuccessOrdering(Src);
627 LLVMAtomicOrdering Fail = LLVMGetCmpXchgFailureOrdering(Src);
628 LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src);
630 Dst = LLVMBuildAtomicCmpXchg(Builder, Ptr, Cmp, New, Succ, Fail,
631 SingleThread);
632 LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
633 LLVMSetWeak(Dst, LLVMGetWeak(Src));
634 LLVMSetValueName2(Dst, Name, NameLen);
635 break;
637 case LLVMBitCast: {
638 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 0));
639 Dst = LLVMBuildBitCast(Builder, V, CloneType(Src), Name);
640 break;
642 case LLVMICmp: {
643 LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src);
644 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
645 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
646 Dst = LLVMBuildICmp(Builder, Pred, LHS, RHS, Name);
647 break;
649 case LLVMPHI: {
650 // We need to aggressively set things here because of loops.
651 VMap[Src] = Dst = LLVMBuildPhi(Builder, CloneType(Src), Name);
653 SmallVector<LLVMValueRef, 8> Values;
654 SmallVector<LLVMBasicBlockRef, 8> Blocks;
656 unsigned IncomingCount = LLVMCountIncoming(Src);
657 for (unsigned i = 0; i < IncomingCount; ++i) {
658 Blocks.push_back(DeclareBB(LLVMGetIncomingBlock(Src, i)));
659 Values.push_back(CloneValue(LLVMGetIncomingValue(Src, i)));
662 LLVMAddIncoming(Dst, Values.data(), Blocks.data(), IncomingCount);
663 return Dst;
665 case LLVMCall: {
666 SmallVector<LLVMValueRef, 8> Args;
667 int ArgCount = LLVMGetNumArgOperands(Src);
668 for (int i = 0; i < ArgCount; i++)
669 Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
670 LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
671 Dst = LLVMBuildCall(Builder, Fn, Args.data(), ArgCount, Name);
672 LLVMSetTailCall(Dst, LLVMIsTailCall(Src));
673 CloneAttrs(Src, Dst);
674 break;
676 case LLVMResume: {
677 Dst = LLVMBuildResume(Builder, CloneValue(LLVMGetOperand(Src, 0)));
678 break;
680 case LLVMLandingPad: {
681 // The landing pad API is a bit screwed up for historical reasons.
682 Dst = LLVMBuildLandingPad(Builder, CloneType(Src), nullptr, 0, Name);
683 unsigned NumClauses = LLVMGetNumClauses(Src);
684 for (unsigned i = 0; i < NumClauses; ++i)
685 LLVMAddClause(Dst, CloneValue(LLVMGetClause(Src, i)));
686 LLVMSetCleanup(Dst, LLVMIsCleanup(Src));
687 break;
689 case LLVMCleanupRet: {
690 LLVMValueRef CatchPad = CloneValue(LLVMGetOperand(Src, 0));
691 LLVMBasicBlockRef Unwind = nullptr;
692 if (LLVMBasicBlockRef UDest = LLVMGetUnwindDest(Src))
693 Unwind = DeclareBB(UDest);
694 Dst = LLVMBuildCleanupRet(Builder, CatchPad, Unwind);
695 break;
697 case LLVMCatchRet: {
698 LLVMValueRef CatchPad = CloneValue(LLVMGetOperand(Src, 0));
699 LLVMBasicBlockRef SuccBB = DeclareBB(LLVMGetSuccessor(Src, 0));
700 Dst = LLVMBuildCatchRet(Builder, CatchPad, SuccBB);
701 break;
703 case LLVMCatchPad: {
704 LLVMValueRef ParentPad = CloneValue(LLVMGetParentCatchSwitch(Src));
705 SmallVector<LLVMValueRef, 8> Args;
706 int ArgCount = LLVMGetNumArgOperands(Src);
707 for (int i = 0; i < ArgCount; i++)
708 Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
709 Dst = LLVMBuildCatchPad(Builder, ParentPad,
710 Args.data(), ArgCount, Name);
711 break;
713 case LLVMCleanupPad: {
714 LLVMValueRef ParentPad = CloneValue(LLVMGetOperand(Src, 0));
715 SmallVector<LLVMValueRef, 8> Args;
716 int ArgCount = LLVMGetNumArgOperands(Src);
717 for (int i = 0; i < ArgCount; i++)
718 Args.push_back(CloneValue(LLVMGetArgOperand(Src, i)));
719 Dst = LLVMBuildCleanupPad(Builder, ParentPad,
720 Args.data(), ArgCount, Name);
721 break;
723 case LLVMCatchSwitch: {
724 LLVMValueRef ParentPad = CloneValue(LLVMGetOperand(Src, 0));
725 LLVMBasicBlockRef UnwindBB = nullptr;
726 if (LLVMBasicBlockRef UDest = LLVMGetUnwindDest(Src)) {
727 UnwindBB = DeclareBB(UDest);
729 unsigned NumHandlers = LLVMGetNumHandlers(Src);
730 Dst = LLVMBuildCatchSwitch(Builder, ParentPad, UnwindBB, NumHandlers, Name);
731 if (NumHandlers > 0) {
732 LLVMBasicBlockRef *Handlers = static_cast<LLVMBasicBlockRef*>(
733 safe_malloc(NumHandlers * sizeof(LLVMBasicBlockRef)));
734 LLVMGetHandlers(Src, Handlers);
735 for (unsigned i = 0; i < NumHandlers; i++)
736 LLVMAddHandler(Dst, DeclareBB(Handlers[i]));
737 free(Handlers);
739 break;
741 case LLVMExtractValue: {
742 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
743 if (LLVMGetNumIndices(Src) != 1)
744 report_fatal_error("Expected only one indice");
745 auto I = LLVMGetIndices(Src)[0];
746 Dst = LLVMBuildExtractValue(Builder, Agg, I, Name);
747 break;
749 case LLVMInsertValue: {
750 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
751 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 1));
752 if (LLVMGetNumIndices(Src) != 1)
753 report_fatal_error("Expected only one indice");
754 auto I = LLVMGetIndices(Src)[0];
755 Dst = LLVMBuildInsertValue(Builder, Agg, V, I, Name);
756 break;
758 case LLVMFreeze: {
759 LLVMValueRef Arg = CloneValue(LLVMGetOperand(Src, 0));
760 Dst = LLVMBuildFreeze(Builder, Arg, Name);
761 break;
763 default:
764 break;
767 if (Dst == nullptr) {
768 fprintf(stderr, "%d is not a supported opcode\n", Op);
769 exit(-1);
772 auto Ctx = LLVMGetModuleContext(M);
773 size_t NumMetadataEntries;
774 auto *AllMetadata =
775 LLVMInstructionGetAllMetadataOtherThanDebugLoc(Src,
776 &NumMetadataEntries);
777 for (unsigned i = 0; i < NumMetadataEntries; ++i) {
778 unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i);
779 LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i);
780 LLVMSetMetadata(Dst, Kind, LLVMMetadataAsValue(Ctx, MD));
782 LLVMDisposeValueMetadataEntries(AllMetadata);
783 LLVMSetInstDebugLocation(Builder, Dst);
785 check_value_kind(Dst, LLVMInstructionValueKind);
786 return VMap[Src] = Dst;
789 LLVMBasicBlockRef DeclareBB(LLVMBasicBlockRef Src) {
790 // Check if this is something we already computed.
792 auto i = BBMap.find(Src);
793 if (i != BBMap.end()) {
794 return i->second;
798 LLVMValueRef V = LLVMBasicBlockAsValue(Src);
799 if (!LLVMValueIsBasicBlock(V) || LLVMValueAsBasicBlock(V) != Src)
800 report_fatal_error("Basic block is not a basic block");
802 const char *Name = LLVMGetBasicBlockName(Src);
803 size_t NameLen;
804 const char *VName = LLVMGetValueName2(V, &NameLen);
805 if (Name != VName)
806 report_fatal_error("Basic block name mismatch");
808 LLVMBasicBlockRef BB = LLVMAppendBasicBlock(Fun, Name);
809 return BBMap[Src] = BB;
812 LLVMBasicBlockRef CloneBB(LLVMBasicBlockRef Src) {
813 LLVMBasicBlockRef BB = DeclareBB(Src);
815 // Make sure ordering is correct.
816 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Src);
817 if (Prev)
818 LLVMMoveBasicBlockAfter(BB, DeclareBB(Prev));
820 LLVMValueRef First = LLVMGetFirstInstruction(Src);
821 LLVMValueRef Last = LLVMGetLastInstruction(Src);
823 if (First == nullptr) {
824 if (Last != nullptr)
825 report_fatal_error("Has no first instruction, but last one");
826 return BB;
829 auto Ctx = LLVMGetModuleContext(M);
830 LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx);
831 LLVMPositionBuilderAtEnd(Builder, BB);
833 LLVMValueRef Cur = First;
834 LLVMValueRef Next = nullptr;
835 while(true) {
836 CloneInstruction(Cur, Builder);
837 Next = LLVMGetNextInstruction(Cur);
838 if (Next == nullptr) {
839 if (Cur != Last)
840 report_fatal_error("Final instruction does not match Last");
841 break;
844 LLVMValueRef Prev = LLVMGetPreviousInstruction(Next);
845 if (Prev != Cur)
846 report_fatal_error("Next.Previous instruction is not Current");
848 Cur = Next;
851 LLVMDisposeBuilder(Builder);
852 return BB;
855 void CloneBBs(LLVMValueRef Src) {
856 unsigned Count = LLVMCountBasicBlocks(Src);
857 if (Count == 0)
858 return;
860 LLVMBasicBlockRef First = LLVMGetFirstBasicBlock(Src);
861 LLVMBasicBlockRef Last = LLVMGetLastBasicBlock(Src);
863 LLVMBasicBlockRef Cur = First;
864 LLVMBasicBlockRef Next = nullptr;
865 while(true) {
866 CloneBB(Cur);
867 Count--;
868 Next = LLVMGetNextBasicBlock(Cur);
869 if (Next == nullptr) {
870 if (Cur != Last)
871 report_fatal_error("Final basic block does not match Last");
872 break;
875 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Next);
876 if (Prev != Cur)
877 report_fatal_error("Next.Previous basic bloc is not Current");
879 Cur = Next;
882 if (Count != 0)
883 report_fatal_error("Basic block count does not match iterration");
887 static void declare_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
888 auto Ctx = LLVMGetModuleContext(M);
890 LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
891 LLVMValueRef End = LLVMGetLastGlobal(Src);
893 LLVMValueRef Cur = Begin;
894 LLVMValueRef Next = nullptr;
895 if (!Begin) {
896 if (End != nullptr)
897 report_fatal_error("Range has an end but no beginning");
898 goto FunDecl;
901 while (true) {
902 size_t NameLen;
903 const char *Name = LLVMGetValueName2(Cur, &NameLen);
904 if (LLVMGetNamedGlobal(M, Name))
905 report_fatal_error("GlobalVariable already cloned");
906 LLVMAddGlobal(M, LLVMGetElementType(TypeCloner(M).Clone(Cur)), Name);
908 Next = LLVMGetNextGlobal(Cur);
909 if (Next == nullptr) {
910 if (Cur != End)
911 report_fatal_error("");
912 break;
915 LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
916 if (Prev != Cur)
917 report_fatal_error("Next.Previous global is not Current");
919 Cur = Next;
922 FunDecl:
923 Begin = LLVMGetFirstFunction(Src);
924 End = LLVMGetLastFunction(Src);
925 if (!Begin) {
926 if (End != nullptr)
927 report_fatal_error("Range has an end but no beginning");
928 goto AliasDecl;
931 Cur = Begin;
932 Next = nullptr;
933 while (true) {
934 size_t NameLen;
935 const char *Name = LLVMGetValueName2(Cur, &NameLen);
936 if (LLVMGetNamedFunction(M, Name))
937 report_fatal_error("Function already cloned");
938 auto Ty = LLVMGetElementType(TypeCloner(M).Clone(Cur));
939 auto F = LLVMAddFunction(M, Name, Ty);
941 // Copy attributes
942 for (int i = LLVMAttributeFunctionIndex, c = LLVMCountParams(F);
943 i <= c; ++i) {
944 for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) {
945 if (auto SrcA = LLVMGetEnumAttributeAtIndex(Cur, i, k)) {
946 auto Val = LLVMGetEnumAttributeValue(SrcA);
947 auto DstA = LLVMCreateEnumAttribute(Ctx, k, Val);
948 LLVMAddAttributeAtIndex(F, i, DstA);
953 Next = LLVMGetNextFunction(Cur);
954 if (Next == nullptr) {
955 if (Cur != End)
956 report_fatal_error("Last function does not match End");
957 break;
960 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
961 if (Prev != Cur)
962 report_fatal_error("Next.Previous function is not Current");
964 Cur = Next;
967 AliasDecl:
968 Begin = LLVMGetFirstGlobalAlias(Src);
969 End = LLVMGetLastGlobalAlias(Src);
970 if (!Begin) {
971 if (End != nullptr)
972 report_fatal_error("Range has an end but no beginning");
973 goto GlobalIFuncDecl;
976 Cur = Begin;
977 Next = nullptr;
978 while (true) {
979 size_t NameLen;
980 const char *Name = LLVMGetValueName2(Cur, &NameLen);
981 if (LLVMGetNamedGlobalAlias(M, Name, NameLen))
982 report_fatal_error("Global alias already cloned");
983 LLVMTypeRef CurType = TypeCloner(M).Clone(Cur);
984 // FIXME: Allow NULL aliasee.
985 LLVMAddAlias(M, CurType, LLVMGetUndef(CurType), Name);
987 Next = LLVMGetNextGlobalAlias(Cur);
988 if (Next == nullptr) {
989 if (Cur != End)
990 report_fatal_error("");
991 break;
994 LLVMValueRef Prev = LLVMGetPreviousGlobalAlias(Next);
995 if (Prev != Cur)
996 report_fatal_error("Next.Previous global is not Current");
998 Cur = Next;
1001 GlobalIFuncDecl:
1002 Begin = LLVMGetFirstGlobalIFunc(Src);
1003 End = LLVMGetLastGlobalIFunc(Src);
1004 if (!Begin) {
1005 if (End != nullptr)
1006 report_fatal_error("Range has an end but no beginning");
1007 goto NamedMDDecl;
1010 Cur = Begin;
1011 Next = nullptr;
1012 while (true) {
1013 size_t NameLen;
1014 const char *Name = LLVMGetValueName2(Cur, &NameLen);
1015 if (LLVMGetNamedGlobalIFunc(M, Name, NameLen))
1016 report_fatal_error("Global ifunc already cloned");
1017 LLVMTypeRef CurType = TypeCloner(M).Clone(LLVMGlobalGetValueType(Cur));
1018 // FIXME: Allow NULL resolver.
1019 LLVMAddGlobalIFunc(M, Name, NameLen,
1020 CurType, /*addressSpace*/ 0, LLVMGetUndef(CurType));
1022 Next = LLVMGetNextGlobalIFunc(Cur);
1023 if (Next == nullptr) {
1024 if (Cur != End)
1025 report_fatal_error("");
1026 break;
1029 LLVMValueRef Prev = LLVMGetPreviousGlobalIFunc(Next);
1030 if (Prev != Cur)
1031 report_fatal_error("Next.Previous global is not Current");
1033 Cur = Next;
1036 NamedMDDecl:
1037 LLVMNamedMDNodeRef BeginMD = LLVMGetFirstNamedMetadata(Src);
1038 LLVMNamedMDNodeRef EndMD = LLVMGetLastNamedMetadata(Src);
1039 if (!BeginMD) {
1040 if (EndMD != nullptr)
1041 report_fatal_error("Range has an end but no beginning");
1042 return;
1045 LLVMNamedMDNodeRef CurMD = BeginMD;
1046 LLVMNamedMDNodeRef NextMD = nullptr;
1047 while (true) {
1048 size_t NameLen;
1049 const char *Name = LLVMGetNamedMetadataName(CurMD, &NameLen);
1050 if (LLVMGetNamedMetadata(M, Name, NameLen))
1051 report_fatal_error("Named Metadata Node already cloned");
1052 LLVMGetOrInsertNamedMetadata(M, Name, NameLen);
1054 NextMD = LLVMGetNextNamedMetadata(CurMD);
1055 if (NextMD == nullptr) {
1056 if (CurMD != EndMD)
1057 report_fatal_error("");
1058 break;
1061 LLVMNamedMDNodeRef PrevMD = LLVMGetPreviousNamedMetadata(NextMD);
1062 if (PrevMD != CurMD)
1063 report_fatal_error("Next.Previous global is not Current");
1065 CurMD = NextMD;
1069 static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
1070 LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
1071 LLVMValueRef End = LLVMGetLastGlobal(Src);
1073 LLVMValueRef Cur = Begin;
1074 LLVMValueRef Next = nullptr;
1075 if (!Begin) {
1076 if (End != nullptr)
1077 report_fatal_error("Range has an end but no beginning");
1078 goto FunClone;
1081 while (true) {
1082 size_t NameLen;
1083 const char *Name = LLVMGetValueName2(Cur, &NameLen);
1084 LLVMValueRef G = LLVMGetNamedGlobal(M, Name);
1085 if (!G)
1086 report_fatal_error("GlobalVariable must have been declared already");
1088 if (auto I = LLVMGetInitializer(Cur))
1089 LLVMSetInitializer(G, clone_constant(I, M));
1091 size_t NumMetadataEntries;
1092 auto *AllMetadata = LLVMGlobalCopyAllMetadata(Cur, &NumMetadataEntries);
1093 for (unsigned i = 0; i < NumMetadataEntries; ++i) {
1094 unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i);
1095 LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i);
1096 LLVMGlobalSetMetadata(G, Kind, MD);
1098 LLVMDisposeValueMetadataEntries(AllMetadata);
1100 LLVMSetGlobalConstant(G, LLVMIsGlobalConstant(Cur));
1101 LLVMSetThreadLocal(G, LLVMIsThreadLocal(Cur));
1102 LLVMSetExternallyInitialized(G, LLVMIsExternallyInitialized(Cur));
1103 LLVMSetLinkage(G, LLVMGetLinkage(Cur));
1104 LLVMSetSection(G, LLVMGetSection(Cur));
1105 LLVMSetVisibility(G, LLVMGetVisibility(Cur));
1106 LLVMSetUnnamedAddress(G, LLVMGetUnnamedAddress(Cur));
1107 LLVMSetAlignment(G, LLVMGetAlignment(Cur));
1109 Next = LLVMGetNextGlobal(Cur);
1110 if (Next == nullptr) {
1111 if (Cur != End)
1112 report_fatal_error("");
1113 break;
1116 LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
1117 if (Prev != Cur)
1118 report_fatal_error("Next.Previous global is not Current");
1120 Cur = Next;
1123 FunClone:
1124 Begin = LLVMGetFirstFunction(Src);
1125 End = LLVMGetLastFunction(Src);
1126 if (!Begin) {
1127 if (End != nullptr)
1128 report_fatal_error("Range has an end but no beginning");
1129 goto AliasClone;
1132 Cur = Begin;
1133 Next = nullptr;
1134 while (true) {
1135 size_t NameLen;
1136 const char *Name = LLVMGetValueName2(Cur, &NameLen);
1137 LLVMValueRef Fun = LLVMGetNamedFunction(M, Name);
1138 if (!Fun)
1139 report_fatal_error("Function must have been declared already");
1141 if (LLVMHasPersonalityFn(Cur)) {
1142 size_t FNameLen;
1143 const char *FName = LLVMGetValueName2(LLVMGetPersonalityFn(Cur),
1144 &FNameLen);
1145 LLVMValueRef P = LLVMGetNamedFunction(M, FName);
1146 if (!P)
1147 report_fatal_error("Could not find personality function");
1148 LLVMSetPersonalityFn(Fun, P);
1151 size_t NumMetadataEntries;
1152 auto *AllMetadata = LLVMGlobalCopyAllMetadata(Cur, &NumMetadataEntries);
1153 for (unsigned i = 0; i < NumMetadataEntries; ++i) {
1154 unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i);
1155 LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i);
1156 LLVMGlobalSetMetadata(Fun, Kind, MD);
1158 LLVMDisposeValueMetadataEntries(AllMetadata);
1160 FunCloner FC(Cur, Fun);
1161 FC.CloneBBs(Cur);
1163 Next = LLVMGetNextFunction(Cur);
1164 if (Next == nullptr) {
1165 if (Cur != End)
1166 report_fatal_error("Last function does not match End");
1167 break;
1170 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
1171 if (Prev != Cur)
1172 report_fatal_error("Next.Previous function is not Current");
1174 Cur = Next;
1177 AliasClone:
1178 Begin = LLVMGetFirstGlobalAlias(Src);
1179 End = LLVMGetLastGlobalAlias(Src);
1180 if (!Begin) {
1181 if (End != nullptr)
1182 report_fatal_error("Range has an end but no beginning");
1183 goto GlobalIFuncClone;
1186 Cur = Begin;
1187 Next = nullptr;
1188 while (true) {
1189 size_t NameLen;
1190 const char *Name = LLVMGetValueName2(Cur, &NameLen);
1191 LLVMValueRef Alias = LLVMGetNamedGlobalAlias(M, Name, NameLen);
1192 if (!Alias)
1193 report_fatal_error("Global alias must have been declared already");
1195 if (LLVMValueRef Aliasee = LLVMAliasGetAliasee(Cur)) {
1196 LLVMAliasSetAliasee(Alias, clone_constant(Aliasee, M));
1199 LLVMSetLinkage(Alias, LLVMGetLinkage(Cur));
1200 LLVMSetUnnamedAddress(Alias, LLVMGetUnnamedAddress(Cur));
1202 Next = LLVMGetNextGlobalAlias(Cur);
1203 if (Next == nullptr) {
1204 if (Cur != End)
1205 report_fatal_error("Last global alias does not match End");
1206 break;
1209 LLVMValueRef Prev = LLVMGetPreviousGlobalAlias(Next);
1210 if (Prev != Cur)
1211 report_fatal_error("Next.Previous global alias is not Current");
1213 Cur = Next;
1216 GlobalIFuncClone:
1217 Begin = LLVMGetFirstGlobalIFunc(Src);
1218 End = LLVMGetLastGlobalIFunc(Src);
1219 if (!Begin) {
1220 if (End != nullptr)
1221 report_fatal_error("Range has an end but no beginning");
1222 goto NamedMDClone;
1225 Cur = Begin;
1226 Next = nullptr;
1227 while (true) {
1228 size_t NameLen;
1229 const char *Name = LLVMGetValueName2(Cur, &NameLen);
1230 LLVMValueRef IFunc = LLVMGetNamedGlobalIFunc(M, Name, NameLen);
1231 if (!IFunc)
1232 report_fatal_error("Global ifunc must have been declared already");
1234 if (LLVMValueRef Resolver = LLVMGetGlobalIFuncResolver(Cur)) {
1235 LLVMSetGlobalIFuncResolver(IFunc, clone_constant(Resolver, M));
1238 LLVMSetLinkage(IFunc, LLVMGetLinkage(Cur));
1239 LLVMSetUnnamedAddress(IFunc, LLVMGetUnnamedAddress(Cur));
1241 Next = LLVMGetNextGlobalIFunc(Cur);
1242 if (Next == nullptr) {
1243 if (Cur != End)
1244 report_fatal_error("Last global alias does not match End");
1245 break;
1248 LLVMValueRef Prev = LLVMGetPreviousGlobalIFunc(Next);
1249 if (Prev != Cur)
1250 report_fatal_error("Next.Previous global alias is not Current");
1252 Cur = Next;
1255 NamedMDClone:
1256 LLVMNamedMDNodeRef BeginMD = LLVMGetFirstNamedMetadata(Src);
1257 LLVMNamedMDNodeRef EndMD = LLVMGetLastNamedMetadata(Src);
1258 if (!BeginMD) {
1259 if (EndMD != nullptr)
1260 report_fatal_error("Range has an end but no beginning");
1261 return;
1264 LLVMNamedMDNodeRef CurMD = BeginMD;
1265 LLVMNamedMDNodeRef NextMD = nullptr;
1266 while (true) {
1267 size_t NameLen;
1268 const char *Name = LLVMGetNamedMetadataName(CurMD, &NameLen);
1269 LLVMNamedMDNodeRef NamedMD = LLVMGetNamedMetadata(M, Name, NameLen);
1270 if (!NamedMD)
1271 report_fatal_error("Named MD Node must have been declared already");
1273 unsigned OperandCount = LLVMGetNamedMetadataNumOperands(Src, Name);
1274 LLVMValueRef *OperandBuf = static_cast<LLVMValueRef *>(
1275 safe_malloc(OperandCount * sizeof(LLVMValueRef)));
1276 LLVMGetNamedMetadataOperands(Src, Name, OperandBuf);
1277 for (unsigned i = 0, e = OperandCount; i != e; ++i) {
1278 LLVMAddNamedMetadataOperand(M, Name, OperandBuf[i]);
1280 free(OperandBuf);
1282 NextMD = LLVMGetNextNamedMetadata(CurMD);
1283 if (NextMD == nullptr) {
1284 if (CurMD != EndMD)
1285 report_fatal_error("Last Named MD Node does not match End");
1286 break;
1289 LLVMNamedMDNodeRef PrevMD = LLVMGetPreviousNamedMetadata(NextMD);
1290 if (PrevMD != CurMD)
1291 report_fatal_error("Next.Previous Named MD Node is not Current");
1293 CurMD = NextMD;
1297 int llvm_echo(void) {
1298 LLVMEnablePrettyStackTrace();
1300 LLVMModuleRef Src = llvm_load_module(false, true);
1301 size_t SourceFileLen;
1302 const char *SourceFileName = LLVMGetSourceFileName(Src, &SourceFileLen);
1303 size_t ModuleIdentLen;
1304 const char *ModuleName = LLVMGetModuleIdentifier(Src, &ModuleIdentLen);
1305 LLVMContextRef Ctx = LLVMContextCreate();
1306 LLVMModuleRef M = LLVMModuleCreateWithNameInContext(ModuleName, Ctx);
1308 LLVMSetSourceFileName(M, SourceFileName, SourceFileLen);
1309 LLVMSetModuleIdentifier(M, ModuleName, ModuleIdentLen);
1311 LLVMSetTarget(M, LLVMGetTarget(Src));
1312 LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src));
1313 if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src)))
1314 report_fatal_error("Inconsistent DataLayout string representation");
1316 size_t ModuleInlineAsmLen;
1317 const char *ModuleAsm = LLVMGetModuleInlineAsm(Src, &ModuleInlineAsmLen);
1318 LLVMSetModuleInlineAsm2(M, ModuleAsm, ModuleInlineAsmLen);
1320 declare_symbols(Src, M);
1321 clone_symbols(Src, M);
1322 char *Str = LLVMPrintModuleToString(M);
1323 fputs(Str, stdout);
1325 LLVMDisposeMessage(Str);
1326 LLVMDisposeModule(Src);
1327 LLVMDisposeModule(M);
1328 LLVMContextDispose(Ctx);
1330 return 0;