1 //===-- echo.cpp - tool for testing libLLVM and llvm-c API ----------------===//
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
7 //===----------------------------------------------------------------------===//
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"
31 // Provide DenseMapInfo for C API opaque types.
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.
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
;
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
);
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;
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
)),
107 LLVMIsFunctionVarArg(Src
));
112 case LLVMStructTypeKind
: {
113 LLVMTypeRef S
= nullptr;
114 const char *Name
= LLVMGetStructName(Src
);
116 S
= LLVMGetTypeByName2(Ctx
, Name
);
119 S
= LLVMStructCreateNamed(Ctx
, Name
);
120 if (LLVMIsOpaqueStruct(Src
))
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
)));
129 LLVMStructSetBody(S
, Elts
.data(), EltCount
, LLVMIsPackedStruct(Src
));
131 S
= LLVMStructTypeInContext(Ctx
, Elts
.data(), EltCount
,
132 LLVMIsPackedStruct(Src
));
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
));
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
);
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");
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;
189 const char *Name
= LLVMGetValueName2(SrcCur
, &NameLen
);
190 LLVMSetValueName2(DstCur
, Name
, NameLen
);
192 VMap
[SrcCur
] = DstCur
;
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");
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");
223 report_fatal_error("Parameter count does not match iteration");
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
));
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
)) {
248 const char *Name
= LLVMGetValueName2(Cst
, &NameLen
);
251 if (LLVMIsAFunction(Cst
)) {
252 check_value_kind(Cst
, LLVMFunctionValueKind
);
254 LLVMValueRef Dst
= nullptr;
256 unsigned ID
= LLVMGetIntrinsicID(Cst
);
257 if (ID
> 0 && !LLVMIntrinsicIsOverloaded(ID
)) {
258 Dst
= LLVMGetIntrinsicDeclaration(M
, ID
, nullptr, 0);
260 // Try a normal function
261 Dst
= LLVMGetNamedFunction(M
, Name
);
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
);
275 report_fatal_error("Could not find variable");
279 if (LLVMIsAGlobalAlias(Cst
)) {
280 check_value_kind(Cst
, LLVMGlobalAliasValueKind
);
281 LLVMValueRef Dst
= LLVMGetNamedGlobalAlias(M
, Name
, NameLen
);
284 report_fatal_error("Could not find alias");
287 fprintf(stderr
, "Could not find @%s\n", Name
);
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
);
339 if (LLVMIsUndef(Cst
)) {
340 check_value_kind(Cst
, LLVMUndefValueValueKind
);
341 return LLVMGetUndef(TypeCloner(M
).Clone(Cst
));
345 if (LLVMIsPoison(Cst
)) {
346 check_value_kind(Cst
, LLVMPoisonValueValueKind
);
347 return LLVMGetPoison(TypeCloner(M
).Clone(Cst
));
351 if (LLVMIsNull(Cst
)) {
352 check_value_kind(Cst
, LLVMConstantTokenNoneValueKind
);
353 LLVMTypeRef Ty
= TypeCloner(M
).Clone(Cst
);
354 return LLVMConstNull(Ty
);
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
);
387 return LLVMConstBitCast(clone_constant(LLVMGetOperand(Cst
, 0), M
),
388 TypeCloner(M
).Clone(Cst
));
389 case LLVMGetElementPtr
: {
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
);
400 return LLVMConstGEP2(ElemTy
, Ptr
, Idx
.data(), NumIdx
);
403 fprintf(stderr
, "%d is not a supported opcode for constant expressions\n",
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
,
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
);
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
);
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");
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.
510 LLVMInstructionRemoveFromParent(I
);
511 LLVMInsertIntoBuilderWithName(Builder
, I
, Name
);
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
);
523 int OpCount
= LLVMGetNumOperands(Src
);
525 Dst
= LLVMBuildRetVoid(Builder
);
527 Dst
= LLVMBuildRet(Builder
, CloneValue(LLVMGetOperand(Src
, 0)));
531 if (!LLVMIsConditional(Src
)) {
532 LLVMValueRef SrcOp
= LLVMGetOperand(Src
, 0);
533 LLVMBasicBlockRef SrcBB
= LLVMValueAsBasicBlock(SrcOp
);
534 Dst
= LLVMBuildBr(Builder
, DeclareBB(SrcBB
));
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
);
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
,
560 CloneAttrs(Src
, Dst
);
563 case LLVMUnreachable
:
564 Dst
= LLVMBuildUnreachable(Builder
);
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
);
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
);
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
);
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
);
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
);
613 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
614 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
615 Dst
= LLVMBuildURem(Builder
, LHS
, RHS
, Name
);
619 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
620 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
621 Dst
= LLVMBuildSRem(Builder
, LHS
, RHS
, Name
);
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
);
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
);
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
);
651 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
652 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
653 Dst
= LLVMBuildAnd(Builder
, LHS
, RHS
, Name
);
657 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
658 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
659 Dst
= LLVMBuildOr(Builder
, LHS
, RHS
, Name
);
663 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
664 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
665 Dst
= LLVMBuildXor(Builder
, LHS
, RHS
, Name
);
669 LLVMTypeRef Ty
= CloneType(LLVMGetAllocatedType(Src
));
670 Dst
= LLVMBuildAlloca(Builder
, Ty
, Name
);
671 LLVMSetAlignment(Dst
, LLVMGetAlignment(Src
));
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
));
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
));
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
,
704 Dst
= LLVMBuildGEP2(Builder
, ElemTy
, Ptr
, Idx
.data(), NumIdx
, Name
);
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
);
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
,
729 LLVMSetAlignment(Dst
, LLVMGetAlignment(Src
));
730 LLVMSetVolatile(Dst
, LLVMGetVolatile(Src
));
731 LLVMSetWeak(Dst
, LLVMGetWeak(Src
));
732 LLVMSetValueName2(Dst
, Name
, NameLen
);
736 LLVMValueRef V
= CloneValue(LLVMGetOperand(Src
, 0));
737 Dst
= LLVMBuildBitCast(Builder
, V
, CloneType(Src
), Name
);
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
);
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
);
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
);
776 Dst
= LLVMBuildResume(Builder
, CloneValue(LLVMGetOperand(Src
, 0)));
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
));
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
);
797 LLVMValueRef CatchPad
= CloneValue(LLVMGetOperand(Src
, 0));
798 LLVMBasicBlockRef SuccBB
= DeclareBB(LLVMGetSuccessor(Src
, 0));
799 Dst
= LLVMBuildCatchRet(Builder
, CatchPad
, SuccBB
);
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
);
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
);
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
]));
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
);
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
);
861 case LLVMExtractElement
: {
862 LLVMValueRef Agg
= CloneValue(LLVMGetOperand(Src
, 0));
863 LLVMValueRef Index
= CloneValue(LLVMGetOperand(Src
, 1));
864 Dst
= LLVMBuildExtractElement(Builder
, Agg
, Index
, Name
);
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
);
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()));
884 MaskElts
.push_back(LLVMConstInt(LLVMInt64Type(), Val
, true));
887 LLVMValueRef Mask
= LLVMConstVector(MaskElts
.data(), NumMaskElts
);
888 Dst
= LLVMBuildShuffleVector(Builder
, Agg0
, Agg1
, Mask
, Name
);
892 LLVMValueRef Arg
= CloneValue(LLVMGetOperand(Src
, 0));
893 Dst
= LLVMBuildFreeze(Builder
, Arg
, Name
);
897 LLVMAtomicOrdering Ordering
= LLVMGetOrdering(Src
);
898 LLVMBool IsSingleThreaded
= LLVMIsAtomicSingleThread(Src
);
899 Dst
= LLVMBuildFence(Builder
, Ordering
, IsSingleThreaded
, Name
);
906 if (Dst
== nullptr) {
907 fprintf(stderr
, "%d is not a supported opcode\n", Op
);
911 auto Ctx
= LLVMGetModuleContext(M
);
912 size_t NumMetadataEntries
;
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()) {
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
);
943 const char *VName
= LLVMGetValueName2(V
, &NameLen
);
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
);
957 LLVMMoveBasicBlockAfter(BB
, DeclareBB(Prev
));
959 LLVMValueRef First
= LLVMGetFirstInstruction(Src
);
960 LLVMValueRef Last
= LLVMGetLastInstruction(Src
);
962 if (First
== nullptr) {
964 report_fatal_error("Has no first instruction, but last one");
968 auto Ctx
= LLVMGetModuleContext(M
);
969 LLVMBuilderRef Builder
= LLVMCreateBuilderInContext(Ctx
);
970 LLVMPositionBuilderAtEnd(Builder
, BB
);
972 LLVMValueRef Cur
= First
;
973 LLVMValueRef Next
= nullptr;
975 CloneInstruction(Cur
, Builder
);
976 Next
= LLVMGetNextInstruction(Cur
);
977 if (Next
== nullptr) {
979 report_fatal_error("Final instruction does not match Last");
983 LLVMValueRef Prev
= LLVMGetPreviousInstruction(Next
);
985 report_fatal_error("Next.Previous instruction is not Current");
990 LLVMDisposeBuilder(Builder
);
994 void CloneBBs(LLVMValueRef Src
) {
995 unsigned Count
= LLVMCountBasicBlocks(Src
);
999 LLVMBasicBlockRef First
= LLVMGetFirstBasicBlock(Src
);
1000 LLVMBasicBlockRef Last
= LLVMGetLastBasicBlock(Src
);
1002 LLVMBasicBlockRef Cur
= First
;
1003 LLVMBasicBlockRef Next
= nullptr;
1007 Next
= LLVMGetNextBasicBlock(Cur
);
1008 if (Next
== nullptr) {
1010 report_fatal_error("Final basic block does not match Last");
1014 LLVMBasicBlockRef Prev
= LLVMGetPreviousBasicBlock(Next
);
1016 report_fatal_error("Next.Previous basic bloc is not Current");
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;
1036 report_fatal_error("Range has an end but no beginning");
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) {
1050 report_fatal_error("");
1054 LLVMValueRef Prev
= LLVMGetPreviousGlobal(Next
);
1056 report_fatal_error("Next.Previous global is not Current");
1062 Begin
= LLVMGetFirstFunction(Src
);
1063 End
= LLVMGetLastFunction(Src
);
1066 report_fatal_error("Range has an end but no beginning");
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
);
1082 for (int i
= LLVMAttributeFunctionIndex
, c
= LLVMCountParams(F
);
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) {
1096 report_fatal_error("Last function does not match End");
1100 LLVMValueRef Prev
= LLVMGetPreviousFunction(Next
);
1102 report_fatal_error("Next.Previous function is not Current");
1108 Begin
= LLVMGetFirstGlobalAlias(Src
);
1109 End
= LLVMGetLastGlobalAlias(Src
);
1112 report_fatal_error("Range has an end but no beginning");
1113 goto GlobalIFuncDecl
;
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) {
1132 report_fatal_error("");
1136 LLVMValueRef Prev
= LLVMGetPreviousGlobalAlias(Next
);
1138 report_fatal_error("Next.Previous global is not Current");
1144 Begin
= LLVMGetFirstGlobalIFunc(Src
);
1145 End
= LLVMGetLastGlobalIFunc(Src
);
1148 report_fatal_error("Range has an end but no beginning");
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) {
1167 report_fatal_error("");
1171 LLVMValueRef Prev
= LLVMGetPreviousGlobalIFunc(Next
);
1173 report_fatal_error("Next.Previous global is not Current");
1179 LLVMNamedMDNodeRef BeginMD
= LLVMGetFirstNamedMetadata(Src
);
1180 LLVMNamedMDNodeRef EndMD
= LLVMGetLastNamedMetadata(Src
);
1182 if (EndMD
!= nullptr)
1183 report_fatal_error("Range has an end but no beginning");
1187 LLVMNamedMDNodeRef CurMD
= BeginMD
;
1188 LLVMNamedMDNodeRef NextMD
= nullptr;
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) {
1199 report_fatal_error("");
1203 LLVMNamedMDNodeRef PrevMD
= LLVMGetPreviousNamedMetadata(NextMD
);
1204 if (PrevMD
!= CurMD
)
1205 report_fatal_error("Next.Previous global is not Current");
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;
1219 report_fatal_error("Range has an end but no beginning");
1225 const char *Name
= LLVMGetValueName2(Cur
, &NameLen
);
1226 LLVMValueRef G
= LLVMGetNamedGlobal(M
, Name
);
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) {
1254 report_fatal_error("");
1258 LLVMValueRef Prev
= LLVMGetPreviousGlobal(Next
);
1260 report_fatal_error("Next.Previous global is not Current");
1266 Begin
= LLVMGetFirstFunction(Src
);
1267 End
= LLVMGetLastFunction(Src
);
1270 report_fatal_error("Range has an end but no beginning");
1278 const char *Name
= LLVMGetValueName2(Cur
, &NameLen
);
1279 LLVMValueRef Fun
= LLVMGetNamedFunction(M
, Name
);
1281 report_fatal_error("Function must have been declared already");
1283 if (LLVMHasPersonalityFn(Cur
)) {
1285 const char *FName
= LLVMGetValueName2(LLVMGetPersonalityFn(Cur
),
1287 LLVMValueRef P
= LLVMGetNamedFunction(M
, FName
);
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
);
1305 Next
= LLVMGetNextFunction(Cur
);
1306 if (Next
== nullptr) {
1308 report_fatal_error("Last function does not match End");
1312 LLVMValueRef Prev
= LLVMGetPreviousFunction(Next
);
1314 report_fatal_error("Next.Previous function is not Current");
1320 Begin
= LLVMGetFirstGlobalAlias(Src
);
1321 End
= LLVMGetLastGlobalAlias(Src
);
1324 report_fatal_error("Range has an end but no beginning");
1325 goto GlobalIFuncClone
;
1332 const char *Name
= LLVMGetValueName2(Cur
, &NameLen
);
1333 LLVMValueRef Alias
= LLVMGetNamedGlobalAlias(M
, Name
, NameLen
);
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) {
1347 report_fatal_error("Last global alias does not match End");
1351 LLVMValueRef Prev
= LLVMGetPreviousGlobalAlias(Next
);
1353 report_fatal_error("Next.Previous global alias is not Current");
1359 Begin
= LLVMGetFirstGlobalIFunc(Src
);
1360 End
= LLVMGetLastGlobalIFunc(Src
);
1363 report_fatal_error("Range has an end but no beginning");
1371 const char *Name
= LLVMGetValueName2(Cur
, &NameLen
);
1372 LLVMValueRef IFunc
= LLVMGetNamedGlobalIFunc(M
, Name
, NameLen
);
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) {
1386 report_fatal_error("Last global alias does not match End");
1390 LLVMValueRef Prev
= LLVMGetPreviousGlobalIFunc(Next
);
1392 report_fatal_error("Next.Previous global alias is not Current");
1398 LLVMNamedMDNodeRef BeginMD
= LLVMGetFirstNamedMetadata(Src
);
1399 LLVMNamedMDNodeRef EndMD
= LLVMGetLastNamedMetadata(Src
);
1401 if (EndMD
!= nullptr)
1402 report_fatal_error("Range has an end but no beginning");
1406 LLVMNamedMDNodeRef CurMD
= BeginMD
;
1407 LLVMNamedMDNodeRef NextMD
= nullptr;
1410 const char *Name
= LLVMGetNamedMetadataName(CurMD
, &NameLen
);
1411 LLVMNamedMDNodeRef NamedMD
= LLVMGetNamedMetadata(M
, Name
, NameLen
);
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
]);
1424 NextMD
= LLVMGetNextNamedMetadata(CurMD
);
1425 if (NextMD
== nullptr) {
1427 report_fatal_error("Last Named MD Node does not match End");
1431 LLVMNamedMDNodeRef PrevMD
= LLVMGetPreviousNamedMetadata(NextMD
);
1432 if (PrevMD
!= CurMD
)
1433 report_fatal_error("Next.Previous Named MD Node is not Current");
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
);
1467 LLVMDisposeMessage(Str
);
1468 LLVMDisposeModule(Src
);
1469 LLVMDisposeModule(M
);
1470 LLVMContextDispose(Ctx
);