| 1 | //===-- echo.cpp - tool for testing libLLVM and llvm-c API ----------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the --echo command in llvm-c-test. |
| 10 | // |
| 11 | // This command uses the C API to read a module and output an exact copy of it |
| 12 | // as output. It is used to check that the resulting module matches the input |
| 13 | // to validate that the C API can read and write modules properly. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "llvm-c-test.h" |
| 18 | #include "llvm-c/ErrorHandling.h" |
| 19 | #include "llvm/ADT/DenseMap.h" |
| 20 | #include "llvm/ADT/Hashing.h" |
| 21 | #include "llvm/ADT/SmallVector.h" |
| 22 | #include "llvm/Support/ErrorHandling.h" |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | |
| 27 | using namespace llvm; |
| 28 | |
| 29 | // Provide DenseMapInfo for C API opaque types. |
| 30 | template<typename T> |
| 31 | struct CAPIDenseMap {}; |
| 32 | |
| 33 | // The default DenseMapInfo require to know about pointer alignment. |
| 34 | // Because the C API uses opaque pointer types, their alignment is unknown. |
| 35 | // As a result, we need to roll out our own implementation. |
| 36 | template<typename T> |
| 37 | struct CAPIDenseMap<T*> { |
| 38 | struct CAPIDenseMapInfo { |
| 39 | static unsigned getHashValue(const T *PtrVal) { |
| 40 | return hash_value(PtrVal); |
| 41 | } |
| 42 | static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; } |
| 43 | }; |
| 44 | |
| 45 | typedef DenseMap<T*, T*, CAPIDenseMapInfo> Map; |
| 46 | }; |
| 47 | |
| 48 | typedef CAPIDenseMap<LLVMValueRef>::Map ValueMap; |
| 49 | typedef CAPIDenseMap<LLVMBasicBlockRef>::Map BasicBlockMap; |
| 50 | |
| 51 | struct TypeCloner { |
| 52 | LLVMModuleRef M; |
| 53 | LLVMContextRef Ctx; |
| 54 | |
| 55 | TypeCloner(LLVMModuleRef M): M(M), Ctx(LLVMGetModuleContext(M)) {} |
| 56 | |
| 57 | LLVMTypeRef Clone(LLVMValueRef Src) { |
| 58 | return Clone(Src: LLVMTypeOf(Val: Src)); |
| 59 | } |
| 60 | |
| 61 | LLVMTypeRef Clone(LLVMTypeRef Src) { |
| 62 | LLVMTypeKind Kind = LLVMGetTypeKind(Ty: Src); |
| 63 | switch (Kind) { |
| 64 | case LLVMVoidTypeKind: |
| 65 | return LLVMVoidTypeInContext(C: Ctx); |
| 66 | case LLVMHalfTypeKind: |
| 67 | return LLVMHalfTypeInContext(C: Ctx); |
| 68 | case LLVMBFloatTypeKind: |
| 69 | return LLVMHalfTypeInContext(C: Ctx); |
| 70 | case LLVMFloatTypeKind: |
| 71 | return LLVMFloatTypeInContext(C: Ctx); |
| 72 | case LLVMDoubleTypeKind: |
| 73 | return LLVMDoubleTypeInContext(C: Ctx); |
| 74 | case LLVMX86_FP80TypeKind: |
| 75 | return LLVMX86FP80TypeInContext(C: Ctx); |
| 76 | case LLVMFP128TypeKind: |
| 77 | return LLVMFP128TypeInContext(C: Ctx); |
| 78 | case LLVMPPC_FP128TypeKind: |
| 79 | return LLVMPPCFP128TypeInContext(C: Ctx); |
| 80 | case LLVMLabelTypeKind: |
| 81 | return LLVMLabelTypeInContext(C: Ctx); |
| 82 | case LLVMByteTypeKind: |
| 83 | return LLVMByteTypeInContext(C: Ctx, NumBits: LLVMGetByteTypeWidth(ByteTy: Src)); |
| 84 | case LLVMIntegerTypeKind: |
| 85 | return LLVMIntTypeInContext(C: Ctx, NumBits: LLVMGetIntTypeWidth(IntegerTy: Src)); |
| 86 | case LLVMFunctionTypeKind: { |
| 87 | unsigned ParamCount = LLVMCountParamTypes(FunctionTy: Src); |
| 88 | LLVMTypeRef* Params = nullptr; |
| 89 | if (ParamCount > 0) { |
| 90 | Params = static_cast<LLVMTypeRef*>( |
| 91 | safe_malloc(Sz: ParamCount * sizeof(LLVMTypeRef))); |
| 92 | LLVMGetParamTypes(FunctionTy: Src, Dest: Params); |
| 93 | for (unsigned i = 0; i < ParamCount; i++) |
| 94 | Params[i] = Clone(Src: Params[i]); |
| 95 | } |
| 96 | |
| 97 | LLVMTypeRef FunTy = LLVMFunctionType(ReturnType: Clone(Src: LLVMGetReturnType(FunctionTy: Src)), |
| 98 | ParamTypes: Params, ParamCount, |
| 99 | IsVarArg: LLVMIsFunctionVarArg(FunctionTy: Src)); |
| 100 | if (ParamCount > 0) |
| 101 | free(ptr: Params); |
| 102 | return FunTy; |
| 103 | } |
| 104 | case LLVMStructTypeKind: { |
| 105 | LLVMTypeRef S = nullptr; |
| 106 | const char *Name = LLVMGetStructName(Ty: Src); |
| 107 | if (Name) { |
| 108 | S = LLVMGetTypeByName2(C: Ctx, Name); |
| 109 | if (S) |
| 110 | return S; |
| 111 | S = LLVMStructCreateNamed(C: Ctx, Name); |
| 112 | if (LLVMIsOpaqueStruct(StructTy: Src)) |
| 113 | return S; |
| 114 | } |
| 115 | |
| 116 | unsigned EltCount = LLVMCountStructElementTypes(StructTy: Src); |
| 117 | SmallVector<LLVMTypeRef, 8> Elts; |
| 118 | for (unsigned i = 0; i < EltCount; i++) |
| 119 | Elts.push_back(Elt: Clone(Src: LLVMStructGetTypeAtIndex(StructTy: Src, i))); |
| 120 | if (Name) |
| 121 | LLVMStructSetBody(StructTy: S, ElementTypes: Elts.data(), ElementCount: EltCount, Packed: LLVMIsPackedStruct(StructTy: Src)); |
| 122 | else |
| 123 | S = LLVMStructTypeInContext(C: Ctx, ElementTypes: Elts.data(), ElementCount: EltCount, |
| 124 | Packed: LLVMIsPackedStruct(StructTy: Src)); |
| 125 | return S; |
| 126 | } |
| 127 | case LLVMArrayTypeKind: |
| 128 | return LLVMArrayType2(ElementType: Clone(Src: LLVMGetElementType(Ty: Src)), |
| 129 | ElementCount: LLVMGetArrayLength2(ArrayTy: Src)); |
| 130 | case LLVMPointerTypeKind: |
| 131 | if (LLVMPointerTypeIsOpaque(Ty: Src)) |
| 132 | return LLVMPointerTypeInContext(C: Ctx, AddressSpace: LLVMGetPointerAddressSpace(PointerTy: Src)); |
| 133 | else |
| 134 | return LLVMPointerType(ElementType: Clone(Src: LLVMGetElementType(Ty: Src)), |
| 135 | AddressSpace: LLVMGetPointerAddressSpace(PointerTy: Src)); |
| 136 | case LLVMVectorTypeKind: |
| 137 | return LLVMVectorType( |
| 138 | ElementType: Clone(Src: LLVMGetElementType(Ty: Src)), |
| 139 | ElementCount: LLVMGetVectorSize(VectorTy: Src) |
| 140 | ); |
| 141 | case LLVMScalableVectorTypeKind: |
| 142 | return LLVMScalableVectorType(ElementType: Clone(Src: LLVMGetElementType(Ty: Src)), |
| 143 | ElementCount: LLVMGetVectorSize(VectorTy: Src)); |
| 144 | case LLVMMetadataTypeKind: |
| 145 | return LLVMMetadataTypeInContext(C: Ctx); |
| 146 | case LLVMX86_AMXTypeKind: |
| 147 | return LLVMX86AMXTypeInContext(C: Ctx); |
| 148 | case LLVMTokenTypeKind: |
| 149 | return LLVMTokenTypeInContext(C: Ctx); |
| 150 | case LLVMTargetExtTypeKind: { |
| 151 | const char *Name = LLVMGetTargetExtTypeName(TargetExtTy: Src); |
| 152 | unsigned NumTypeParams = LLVMGetTargetExtTypeNumTypeParams(TargetExtTy: Src); |
| 153 | unsigned NumIntParams = LLVMGetTargetExtTypeNumIntParams(TargetExtTy: Src); |
| 154 | |
| 155 | SmallVector<LLVMTypeRef, 4> TypeParams((size_t)NumTypeParams); |
| 156 | SmallVector<unsigned, 4> IntParams((size_t)NumIntParams); |
| 157 | |
| 158 | for (unsigned i = 0; i < TypeParams.size(); i++) |
| 159 | TypeParams[i] = Clone(Src: LLVMGetTargetExtTypeTypeParam(TargetExtTy: Src, Idx: i)); |
| 160 | |
| 161 | for (unsigned i = 0; i < IntParams.size(); i++) |
| 162 | IntParams[i] = LLVMGetTargetExtTypeIntParam(TargetExtTy: Src, Idx: i); |
| 163 | |
| 164 | LLVMTypeRef TargetExtTy = LLVMTargetExtTypeInContext( |
| 165 | C: Ctx, Name, TypeParams: TypeParams.data(), TypeParamCount: TypeParams.size(), IntParams: IntParams.data(), |
| 166 | IntParamCount: IntParams.size()); |
| 167 | |
| 168 | return TargetExtTy; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | fprintf(stderr, format: "%d is not a supported typekind\n" , Kind); |
| 173 | exit(status: -1); |
| 174 | } |
| 175 | }; |
| 176 | |
| 177 | static ValueMap clone_params(LLVMValueRef Src, LLVMValueRef Dst) { |
| 178 | unsigned Count = LLVMCountParams(Fn: Src); |
| 179 | if (Count != LLVMCountParams(Fn: Dst)) |
| 180 | report_fatal_error(reason: "Parameter count mismatch" ); |
| 181 | |
| 182 | ValueMap VMap; |
| 183 | if (Count == 0) |
| 184 | return VMap; |
| 185 | |
| 186 | LLVMValueRef SrcFirst = LLVMGetFirstParam(Fn: Src); |
| 187 | LLVMValueRef DstFirst = LLVMGetFirstParam(Fn: Dst); |
| 188 | LLVMValueRef SrcLast = LLVMGetLastParam(Fn: Src); |
| 189 | LLVMValueRef DstLast = LLVMGetLastParam(Fn: Dst); |
| 190 | |
| 191 | LLVMValueRef SrcCur = SrcFirst; |
| 192 | LLVMValueRef DstCur = DstFirst; |
| 193 | LLVMValueRef SrcNext = nullptr; |
| 194 | LLVMValueRef DstNext = nullptr; |
| 195 | while (true) { |
| 196 | size_t NameLen; |
| 197 | const char *Name = LLVMGetValueName2(Val: SrcCur, Length: &NameLen); |
| 198 | LLVMSetValueName2(Val: DstCur, Name, NameLen); |
| 199 | |
| 200 | VMap[SrcCur] = DstCur; |
| 201 | |
| 202 | Count--; |
| 203 | SrcNext = LLVMGetNextParam(Arg: SrcCur); |
| 204 | DstNext = LLVMGetNextParam(Arg: DstCur); |
| 205 | if (SrcNext == nullptr && DstNext == nullptr) { |
| 206 | if (SrcCur != SrcLast) |
| 207 | report_fatal_error(reason: "SrcLast param does not match End" ); |
| 208 | if (DstCur != DstLast) |
| 209 | report_fatal_error(reason: "DstLast param does not match End" ); |
| 210 | break; |
| 211 | } |
| 212 | |
| 213 | if (SrcNext == nullptr) |
| 214 | report_fatal_error(reason: "SrcNext was unexpectedly null" ); |
| 215 | if (DstNext == nullptr) |
| 216 | report_fatal_error(reason: "DstNext was unexpectedly null" ); |
| 217 | |
| 218 | LLVMValueRef SrcPrev = LLVMGetPreviousParam(Arg: SrcNext); |
| 219 | if (SrcPrev != SrcCur) |
| 220 | report_fatal_error(reason: "SrcNext.Previous param is not Current" ); |
| 221 | |
| 222 | LLVMValueRef DstPrev = LLVMGetPreviousParam(Arg: DstNext); |
| 223 | if (DstPrev != DstCur) |
| 224 | report_fatal_error(reason: "DstNext.Previous param is not Current" ); |
| 225 | |
| 226 | SrcCur = SrcNext; |
| 227 | DstCur = DstNext; |
| 228 | } |
| 229 | |
| 230 | if (Count != 0) |
| 231 | report_fatal_error(reason: "Parameter count does not match iteration" ); |
| 232 | |
| 233 | return VMap; |
| 234 | } |
| 235 | |
| 236 | static void check_value_kind(LLVMValueRef V, LLVMValueKind K) { |
| 237 | if (LLVMGetValueKind(Val: V) != K) |
| 238 | report_fatal_error(reason: "LLVMGetValueKind returned incorrect type" ); |
| 239 | } |
| 240 | |
| 241 | static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M); |
| 242 | |
| 243 | static LLVMValueRef clone_constant(LLVMValueRef Cst, LLVMModuleRef M) { |
| 244 | LLVMValueRef Ret = clone_constant_impl(Cst, M); |
| 245 | check_value_kind(V: Ret, K: LLVMGetValueKind(Val: Cst)); |
| 246 | return Ret; |
| 247 | } |
| 248 | |
| 249 | static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M) { |
| 250 | if (!LLVMIsAConstant(Val: Cst)) |
| 251 | report_fatal_error(reason: "Expected a constant" ); |
| 252 | |
| 253 | // Maybe it is a symbol |
| 254 | if (LLVMIsAGlobalValue(Val: Cst)) { |
| 255 | size_t NameLen; |
| 256 | const char *Name = LLVMGetValueName2(Val: Cst, Length: &NameLen); |
| 257 | |
| 258 | // Try function |
| 259 | if (LLVMIsAFunction(Val: Cst)) { |
| 260 | check_value_kind(V: Cst, K: LLVMFunctionValueKind); |
| 261 | |
| 262 | LLVMValueRef Dst = nullptr; |
| 263 | // Try an intrinsic |
| 264 | unsigned ID = LLVMGetIntrinsicID(Fn: Cst); |
| 265 | if (ID > 0 && !LLVMIntrinsicIsOverloaded(ID)) { |
| 266 | Dst = LLVMGetIntrinsicDeclaration(Mod: M, ID, OverloadTypes: nullptr, OverloadCount: 0); |
| 267 | } else { |
| 268 | // Try a normal function |
| 269 | Dst = LLVMGetNamedFunction(M, Name); |
| 270 | } |
| 271 | |
| 272 | if (Dst) |
| 273 | return Dst; |
| 274 | report_fatal_error(reason: "Could not find function" ); |
| 275 | } |
| 276 | |
| 277 | // Try global variable |
| 278 | if (LLVMIsAGlobalVariable(Val: Cst)) { |
| 279 | check_value_kind(V: Cst, K: LLVMGlobalVariableValueKind); |
| 280 | LLVMValueRef Dst = LLVMGetNamedGlobal(M, Name); |
| 281 | if (Dst) |
| 282 | return Dst; |
| 283 | report_fatal_error(reason: "Could not find variable" ); |
| 284 | } |
| 285 | |
| 286 | // Try global alias |
| 287 | if (LLVMIsAGlobalAlias(Val: Cst)) { |
| 288 | check_value_kind(V: Cst, K: LLVMGlobalAliasValueKind); |
| 289 | LLVMValueRef Dst = LLVMGetNamedGlobalAlias(M, Name, NameLen); |
| 290 | if (Dst) |
| 291 | return Dst; |
| 292 | report_fatal_error(reason: "Could not find alias" ); |
| 293 | } |
| 294 | |
| 295 | fprintf(stderr, format: "Could not find @%s\n" , Name); |
| 296 | exit(status: -1); |
| 297 | } |
| 298 | |
| 299 | // Try integer literal |
| 300 | if (LLVMIsAConstantInt(Val: Cst)) { |
| 301 | check_value_kind(V: Cst, K: LLVMConstantIntValueKind); |
| 302 | return LLVMConstInt(IntTy: TypeCloner(M).Clone(Src: Cst), |
| 303 | N: LLVMConstIntGetZExtValue(ConstantVal: Cst), SignExtend: false); |
| 304 | } |
| 305 | |
| 306 | // Try zeroinitializer |
| 307 | if (LLVMIsAConstantAggregateZero(Val: Cst)) { |
| 308 | check_value_kind(V: Cst, K: LLVMConstantAggregateZeroValueKind); |
| 309 | return LLVMConstNull(Ty: TypeCloner(M).Clone(Src: Cst)); |
| 310 | } |
| 311 | |
| 312 | // Try constant data array |
| 313 | if (LLVMIsAConstantDataArray(Val: Cst)) { |
| 314 | check_value_kind(V: Cst, K: LLVMConstantDataArrayValueKind); |
| 315 | LLVMTypeRef Ty = TypeCloner(M).Clone(Src: Cst); |
| 316 | size_t SizeInBytes; |
| 317 | const char *Data = LLVMGetRawDataValues(c: Cst, SizeInBytes: &SizeInBytes); |
| 318 | return LLVMConstDataArray(ElementTy: LLVMGetElementType(Ty), Data, SizeInBytes); |
| 319 | } |
| 320 | |
| 321 | // Try constant array |
| 322 | if (LLVMIsAConstantArray(Val: Cst)) { |
| 323 | check_value_kind(V: Cst, K: LLVMConstantArrayValueKind); |
| 324 | LLVMTypeRef Ty = TypeCloner(M).Clone(Src: Cst); |
| 325 | uint64_t EltCount = LLVMGetArrayLength2(ArrayTy: Ty); |
| 326 | SmallVector<LLVMValueRef, 8> Elts; |
| 327 | for (uint64_t i = 0; i < EltCount; i++) |
| 328 | Elts.push_back(Elt: clone_constant(Cst: LLVMGetAggregateElement(C: Cst, Idx: i), M)); |
| 329 | return LLVMConstArray(ElementTy: LLVMGetElementType(Ty), ConstantVals: Elts.data(), Length: EltCount); |
| 330 | } |
| 331 | |
| 332 | // Try constant struct |
| 333 | if (LLVMIsAConstantStruct(Val: Cst)) { |
| 334 | check_value_kind(V: Cst, K: LLVMConstantStructValueKind); |
| 335 | LLVMTypeRef Ty = TypeCloner(M).Clone(Src: Cst); |
| 336 | unsigned EltCount = LLVMCountStructElementTypes(StructTy: Ty); |
| 337 | SmallVector<LLVMValueRef, 8> Elts; |
| 338 | for (unsigned i = 0; i < EltCount; i++) |
| 339 | Elts.push_back(Elt: clone_constant(Cst: LLVMGetOperand(Val: Cst, Index: i), M)); |
| 340 | if (LLVMGetStructName(Ty)) |
| 341 | return LLVMConstNamedStruct(StructTy: Ty, ConstantVals: Elts.data(), Count: EltCount); |
| 342 | return LLVMConstStructInContext(C: LLVMGetModuleContext(M), ConstantVals: Elts.data(), |
| 343 | Count: EltCount, Packed: LLVMIsPackedStruct(StructTy: Ty)); |
| 344 | } |
| 345 | |
| 346 | // Try ConstantPointerNull |
| 347 | if (LLVMIsAConstantPointerNull(Val: Cst)) { |
| 348 | check_value_kind(V: Cst, K: LLVMConstantPointerNullValueKind); |
| 349 | LLVMTypeRef Ty = TypeCloner(M).Clone(Src: Cst); |
| 350 | return LLVMConstNull(Ty); |
| 351 | } |
| 352 | |
| 353 | // Try undef |
| 354 | if (LLVMIsUndef(Val: Cst)) { |
| 355 | check_value_kind(V: Cst, K: LLVMUndefValueValueKind); |
| 356 | return LLVMGetUndef(Ty: TypeCloner(M).Clone(Src: Cst)); |
| 357 | } |
| 358 | |
| 359 | // Try poison |
| 360 | if (LLVMIsPoison(Val: Cst)) { |
| 361 | check_value_kind(V: Cst, K: LLVMPoisonValueValueKind); |
| 362 | return LLVMGetPoison(Ty: TypeCloner(M).Clone(Src: Cst)); |
| 363 | } |
| 364 | |
| 365 | // Try null |
| 366 | if (LLVMIsNull(Val: Cst)) { |
| 367 | check_value_kind(V: Cst, K: LLVMConstantTokenNoneValueKind); |
| 368 | LLVMTypeRef Ty = TypeCloner(M).Clone(Src: Cst); |
| 369 | return LLVMConstNull(Ty); |
| 370 | } |
| 371 | |
| 372 | // Try float literal |
| 373 | if (LLVMIsAConstantFP(Val: Cst)) { |
| 374 | check_value_kind(V: Cst, K: LLVMConstantFPValueKind); |
| 375 | report_fatal_error(reason: "ConstantFP is not supported" ); |
| 376 | } |
| 377 | |
| 378 | // Try ConstantVector or ConstantDataVector |
| 379 | if (LLVMIsAConstantVector(Val: Cst) || LLVMIsAConstantDataVector(Val: Cst)) { |
| 380 | check_value_kind(V: Cst, K: LLVMIsAConstantVector(Val: Cst) |
| 381 | ? LLVMConstantVectorValueKind |
| 382 | : LLVMConstantDataVectorValueKind); |
| 383 | LLVMTypeRef Ty = TypeCloner(M).Clone(Src: Cst); |
| 384 | unsigned EltCount = LLVMGetVectorSize(VectorTy: Ty); |
| 385 | SmallVector<LLVMValueRef, 8> Elts; |
| 386 | for (unsigned i = 0; i < EltCount; i++) |
| 387 | Elts.push_back(Elt: clone_constant(Cst: LLVMGetAggregateElement(C: Cst, Idx: i), M)); |
| 388 | return LLVMConstVector(ScalarConstantVals: Elts.data(), Size: EltCount); |
| 389 | } |
| 390 | |
| 391 | if (LLVMIsAConstantPtrAuth(Val: Cst)) { |
| 392 | LLVMValueRef Ptr = clone_constant(Cst: LLVMGetConstantPtrAuthPointer(PtrAuth: Cst), M); |
| 393 | LLVMValueRef Key = clone_constant(Cst: LLVMGetConstantPtrAuthKey(PtrAuth: Cst), M); |
| 394 | LLVMValueRef Disc = |
| 395 | clone_constant(Cst: LLVMGetConstantPtrAuthDiscriminator(PtrAuth: Cst), M); |
| 396 | LLVMValueRef AddrDisc = |
| 397 | clone_constant(Cst: LLVMGetConstantPtrAuthAddrDiscriminator(PtrAuth: Cst), M); |
| 398 | return LLVMConstantPtrAuth(Ptr, Key, Disc, AddrDisc); |
| 399 | } |
| 400 | |
| 401 | // At this point, if it's not a constant expression, it's a kind of constant |
| 402 | // which is not supported |
| 403 | if (!LLVMIsAConstantExpr(Val: Cst)) |
| 404 | report_fatal_error(reason: "Unsupported constant kind" ); |
| 405 | |
| 406 | // At this point, it must be a constant expression |
| 407 | check_value_kind(V: Cst, K: LLVMConstantExprValueKind); |
| 408 | |
| 409 | LLVMOpcode Op = LLVMGetConstOpcode(ConstantVal: Cst); |
| 410 | switch(Op) { |
| 411 | case LLVMBitCast: |
| 412 | return LLVMConstBitCast(ConstantVal: clone_constant(Cst: LLVMGetOperand(Val: Cst, Index: 0), M), |
| 413 | ToType: TypeCloner(M).Clone(Src: Cst)); |
| 414 | case LLVMGetElementPtr: { |
| 415 | LLVMTypeRef ElemTy = |
| 416 | TypeCloner(M).Clone(Src: LLVMGetGEPSourceElementType(GEP: Cst)); |
| 417 | LLVMValueRef Ptr = clone_constant(Cst: LLVMGetOperand(Val: Cst, Index: 0), M); |
| 418 | int NumIdx = LLVMGetNumIndices(Inst: Cst); |
| 419 | SmallVector<LLVMValueRef, 8> Idx; |
| 420 | for (int i = 1; i <= NumIdx; i++) |
| 421 | Idx.push_back(Elt: clone_constant(Cst: LLVMGetOperand(Val: Cst, Index: i), M)); |
| 422 | |
| 423 | return LLVMConstPtrAddFromIndices(DataLayout: LLVMGetModuleDataLayout(M), Ty: ElemTy, ConstantVal: Ptr, |
| 424 | ConstantIndices: Idx.data(), NumIndices: NumIdx, |
| 425 | NoWrapFlags: LLVMGEPGetNoWrapFlags(GEP: Cst)); |
| 426 | } |
| 427 | default: |
| 428 | fprintf(stderr, format: "%d is not a supported opcode for constant expressions\n" , |
| 429 | Op); |
| 430 | exit(status: -1); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | static LLVMValueRef clone_inline_asm(LLVMValueRef Asm, LLVMModuleRef M) { |
| 435 | |
| 436 | if (!LLVMIsAInlineAsm(Val: Asm)) |
| 437 | report_fatal_error(reason: "Expected inline assembly" ); |
| 438 | |
| 439 | size_t AsmStringSize = 0; |
| 440 | const char *AsmString = LLVMGetInlineAsmAsmString(InlineAsmVal: Asm, Len: &AsmStringSize); |
| 441 | |
| 442 | size_t ConstraintStringSize = 0; |
| 443 | const char *ConstraintString = |
| 444 | LLVMGetInlineAsmConstraintString(InlineAsmVal: Asm, Len: &ConstraintStringSize); |
| 445 | |
| 446 | LLVMInlineAsmDialect AsmDialect = LLVMGetInlineAsmDialect(InlineAsmVal: Asm); |
| 447 | |
| 448 | LLVMTypeRef AsmFunctionType = LLVMGetInlineAsmFunctionType(InlineAsmVal: Asm); |
| 449 | |
| 450 | LLVMBool HasSideEffects = LLVMGetInlineAsmHasSideEffects(InlineAsmVal: Asm); |
| 451 | LLVMBool NeedsAlignStack = LLVMGetInlineAsmNeedsAlignedStack(InlineAsmVal: Asm); |
| 452 | LLVMBool CanUnwind = LLVMGetInlineAsmCanUnwind(InlineAsmVal: Asm); |
| 453 | |
| 454 | return LLVMGetInlineAsm(Ty: AsmFunctionType, AsmString, AsmStringSize, |
| 455 | Constraints: ConstraintString, ConstraintsSize: ConstraintStringSize, |
| 456 | HasSideEffects, IsAlignStack: NeedsAlignStack, Dialect: AsmDialect, |
| 457 | CanThrow: CanUnwind); |
| 458 | } |
| 459 | |
| 460 | struct FunCloner { |
| 461 | LLVMValueRef Fun; |
| 462 | LLVMModuleRef M; |
| 463 | |
| 464 | ValueMap VMap; |
| 465 | BasicBlockMap BBMap; |
| 466 | |
| 467 | FunCloner(LLVMValueRef Src, LLVMValueRef Dst): Fun(Dst), |
| 468 | M(LLVMGetGlobalParent(Global: Fun)), VMap(clone_params(Src, Dst)) {} |
| 469 | |
| 470 | LLVMTypeRef CloneType(LLVMTypeRef Src) { |
| 471 | return TypeCloner(M).Clone(Src); |
| 472 | } |
| 473 | |
| 474 | LLVMTypeRef CloneType(LLVMValueRef Src) { |
| 475 | return TypeCloner(M).Clone(Src); |
| 476 | } |
| 477 | |
| 478 | // Try to clone everything in the llvm::Value hierarchy. |
| 479 | LLVMValueRef CloneValue(LLVMValueRef Src) { |
| 480 | // First, the value may be constant. |
| 481 | if (LLVMIsAConstant(Val: Src)) |
| 482 | return clone_constant(Cst: Src, M); |
| 483 | |
| 484 | // Function argument should always be in the map already. |
| 485 | auto i = VMap.find(Val: Src); |
| 486 | if (i != VMap.end()) |
| 487 | return i->second; |
| 488 | |
| 489 | // Inline assembly is a Value, but not an Instruction |
| 490 | if (LLVMIsAInlineAsm(Val: Src)) |
| 491 | return clone_inline_asm(Asm: Src, M); |
| 492 | |
| 493 | if (!LLVMIsAInstruction(Val: Src)) |
| 494 | report_fatal_error(reason: "Expected an instruction" ); |
| 495 | |
| 496 | auto Builder = LLVMCreateBuilderForModule(M); |
| 497 | auto BB = DeclareBB(Src: LLVMGetInstructionParent(Inst: Src)); |
| 498 | LLVMPositionBuilderAtEnd(Builder, Block: BB); |
| 499 | auto Dst = CloneInstruction(Src, Builder); |
| 500 | LLVMDisposeBuilder(Builder); |
| 501 | return Dst; |
| 502 | } |
| 503 | |
| 504 | void CloneAttrs(LLVMValueRef Src, LLVMValueRef Dst) { |
| 505 | auto Ctx = LLVMGetModuleContext(M); |
| 506 | int ArgCount = LLVMGetNumArgOperands(Instr: Src); |
| 507 | for (int i = LLVMAttributeReturnIndex; i <= ArgCount; i++) { |
| 508 | for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) { |
| 509 | if (auto SrcA = LLVMGetCallSiteEnumAttribute(C: Src, Idx: i, KindID: k)) { |
| 510 | auto Val = LLVMGetEnumAttributeValue(A: SrcA); |
| 511 | auto A = LLVMCreateEnumAttribute(C: Ctx, KindID: k, Val); |
| 512 | LLVMAddCallSiteAttribute(C: Dst, Idx: i, A); |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | LLVMValueRef CloneInstruction(LLVMValueRef Src, LLVMBuilderRef Builder) { |
| 519 | check_value_kind(V: Src, K: LLVMInstructionValueKind); |
| 520 | if (!LLVMIsAInstruction(Val: Src)) |
| 521 | report_fatal_error(reason: "Expected an instruction" ); |
| 522 | LLVMContextRef Ctx = LLVMGetTypeContext(Ty: LLVMTypeOf(Val: Src)); |
| 523 | |
| 524 | size_t NameLen; |
| 525 | const char *Name = LLVMGetValueName2(Val: Src, Length: &NameLen); |
| 526 | |
| 527 | // Check if this is something we already computed. |
| 528 | { |
| 529 | auto i = VMap.find(Val: Src); |
| 530 | if (i != VMap.end()) { |
| 531 | // If we have a hit, it means we already generated the instruction |
| 532 | // as a dependency to something else. We need to make sure |
| 533 | // it is ordered properly. |
| 534 | auto I = i->second; |
| 535 | LLVMInstructionRemoveFromParent(Inst: I); |
| 536 | LLVMInsertIntoBuilderWithName(Builder, Instr: I, Name); |
| 537 | return I; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | // We tried everything, it must be an instruction |
| 542 | // that hasn't been generated already. |
| 543 | LLVMValueRef Dst = nullptr; |
| 544 | |
| 545 | LLVMOpcode Op = LLVMGetInstructionOpcode(Inst: Src); |
| 546 | switch(Op) { |
| 547 | case LLVMRet: { |
| 548 | int OpCount = LLVMGetNumOperands(Val: Src); |
| 549 | if (OpCount == 0) |
| 550 | Dst = LLVMBuildRetVoid(Builder); |
| 551 | else |
| 552 | Dst = LLVMBuildRet(Builder, V: CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0))); |
| 553 | break; |
| 554 | } |
| 555 | case LLVMUncondBr: { |
| 556 | LLVMValueRef SrcOp = LLVMGetOperand(Val: Src, Index: 0); |
| 557 | LLVMBasicBlockRef SrcBB = LLVMValueAsBasicBlock(Val: SrcOp); |
| 558 | Dst = LLVMBuildBr(Builder, Dest: DeclareBB(Src: SrcBB)); |
| 559 | break; |
| 560 | } |
| 561 | case LLVMCondBr: { |
| 562 | LLVMValueRef Cond = LLVMGetCondition(Branch: Src); |
| 563 | LLVMValueRef Then = LLVMGetOperand(Val: Src, Index: 1); |
| 564 | LLVMBasicBlockRef ThenBB = DeclareBB(Src: LLVMValueAsBasicBlock(Val: Then)); |
| 565 | LLVMValueRef Else = LLVMGetOperand(Val: Src, Index: 2); |
| 566 | LLVMBasicBlockRef ElseBB = DeclareBB(Src: LLVMValueAsBasicBlock(Val: Else)); |
| 567 | Dst = LLVMBuildCondBr(Builder, If: CloneValue(Src: Cond), Then: ThenBB, Else: ElseBB); |
| 568 | break; |
| 569 | } |
| 570 | case LLVMSwitch: |
| 571 | case LLVMIndirectBr: |
| 572 | break; |
| 573 | case LLVMInvoke: { |
| 574 | SmallVector<LLVMValueRef, 8> Args; |
| 575 | SmallVector<LLVMOperandBundleRef, 8> Bundles; |
| 576 | unsigned ArgCount = LLVMGetNumArgOperands(Instr: Src); |
| 577 | for (unsigned i = 0; i < ArgCount; ++i) |
| 578 | Args.push_back(Elt: CloneValue(Src: LLVMGetOperand(Val: Src, Index: i))); |
| 579 | unsigned BundleCount = LLVMGetNumOperandBundles(C: Src); |
| 580 | for (unsigned i = 0; i < BundleCount; ++i) { |
| 581 | auto Bundle = LLVMGetOperandBundleAtIndex(C: Src, Index: i); |
| 582 | Bundles.push_back(Elt: CloneOB(Src: Bundle)); |
| 583 | LLVMDisposeOperandBundle(Bundle); |
| 584 | } |
| 585 | LLVMTypeRef FnTy = CloneType(Src: LLVMGetCalledFunctionType(C: Src)); |
| 586 | LLVMValueRef Fn = CloneValue(Src: LLVMGetCalledValue(Instr: Src)); |
| 587 | LLVMBasicBlockRef Then = DeclareBB(Src: LLVMGetNormalDest(InvokeInst: Src)); |
| 588 | LLVMBasicBlockRef Unwind = DeclareBB(Src: LLVMGetUnwindDest(InvokeInst: Src)); |
| 589 | Dst = LLVMBuildInvokeWithOperandBundles( |
| 590 | Builder, Ty: FnTy, Fn, Args: Args.data(), NumArgs: ArgCount, Then, Catch: Unwind, |
| 591 | Bundles: Bundles.data(), NumBundles: Bundles.size(), Name); |
| 592 | CloneAttrs(Src, Dst); |
| 593 | for (auto Bundle : Bundles) |
| 594 | LLVMDisposeOperandBundle(Bundle); |
| 595 | break; |
| 596 | } |
| 597 | case LLVMCallBr: { |
| 598 | LLVMTypeRef FnTy = CloneType(Src: LLVMGetCalledFunctionType(C: Src)); |
| 599 | LLVMValueRef Fn = CloneValue(Src: LLVMGetCalledValue(Instr: Src)); |
| 600 | |
| 601 | LLVMBasicBlockRef DefaultDest = |
| 602 | DeclareBB(Src: LLVMGetCallBrDefaultDest(CallBr: Src)); |
| 603 | |
| 604 | // Clone indirect destinations |
| 605 | SmallVector<LLVMBasicBlockRef, 8> IndirectDests; |
| 606 | unsigned IndirectDestCount = LLVMGetCallBrNumIndirectDests(CallBr: Src); |
| 607 | for (unsigned i = 0; i < IndirectDestCount; ++i) |
| 608 | IndirectDests.push_back(Elt: DeclareBB(Src: LLVMGetCallBrIndirectDest(CallBr: Src, Idx: i))); |
| 609 | |
| 610 | // Clone input arguments |
| 611 | SmallVector<LLVMValueRef, 8> Args; |
| 612 | unsigned ArgCount = LLVMGetNumArgOperands(Instr: Src); |
| 613 | for (unsigned i = 0; i < ArgCount; ++i) |
| 614 | Args.push_back(Elt: CloneValue(Src: LLVMGetOperand(Val: Src, Index: i))); |
| 615 | |
| 616 | // Clone operand bundles |
| 617 | SmallVector<LLVMOperandBundleRef, 8> Bundles; |
| 618 | unsigned BundleCount = LLVMGetNumOperandBundles(C: Src); |
| 619 | for (unsigned i = 0; i < BundleCount; ++i) { |
| 620 | auto Bundle = LLVMGetOperandBundleAtIndex(C: Src, Index: i); |
| 621 | Bundles.push_back(Elt: CloneOB(Src: Bundle)); |
| 622 | LLVMDisposeOperandBundle(Bundle); |
| 623 | } |
| 624 | |
| 625 | Dst = LLVMBuildCallBr(B: Builder, Ty: FnTy, Fn, DefaultDest, |
| 626 | IndirectDests: IndirectDests.data(), NumIndirectDests: IndirectDests.size(), |
| 627 | Args: Args.data(), NumArgs: Args.size(), Bundles: Bundles.data(), |
| 628 | NumBundles: Bundles.size(), Name); |
| 629 | |
| 630 | CloneAttrs(Src, Dst); |
| 631 | |
| 632 | for (auto Bundle : Bundles) |
| 633 | LLVMDisposeOperandBundle(Bundle); |
| 634 | |
| 635 | break; |
| 636 | } |
| 637 | case LLVMUnreachable: |
| 638 | Dst = LLVMBuildUnreachable(Builder); |
| 639 | break; |
| 640 | case LLVMAdd: { |
| 641 | LLVMValueRef LHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 642 | LLVMValueRef RHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 643 | LLVMBool NUW = LLVMGetNUW(ArithInst: Src); |
| 644 | LLVMBool NSW = LLVMGetNSW(ArithInst: Src); |
| 645 | Dst = LLVMBuildAdd(Builder, LHS, RHS, Name); |
| 646 | LLVMSetNUW(ArithInst: Dst, HasNUW: NUW); |
| 647 | LLVMSetNSW(ArithInst: Dst, HasNSW: NSW); |
| 648 | break; |
| 649 | } |
| 650 | case LLVMSub: { |
| 651 | LLVMValueRef LHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 652 | LLVMValueRef RHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 653 | LLVMBool NUW = LLVMGetNUW(ArithInst: Src); |
| 654 | LLVMBool NSW = LLVMGetNSW(ArithInst: Src); |
| 655 | Dst = LLVMBuildSub(Builder, LHS, RHS, Name); |
| 656 | LLVMSetNUW(ArithInst: Dst, HasNUW: NUW); |
| 657 | LLVMSetNSW(ArithInst: Dst, HasNSW: NSW); |
| 658 | break; |
| 659 | } |
| 660 | case LLVMMul: { |
| 661 | LLVMValueRef LHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 662 | LLVMValueRef RHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 663 | LLVMBool NUW = LLVMGetNUW(ArithInst: Src); |
| 664 | LLVMBool NSW = LLVMGetNSW(ArithInst: Src); |
| 665 | Dst = LLVMBuildMul(Builder, LHS, RHS, Name); |
| 666 | LLVMSetNUW(ArithInst: Dst, HasNUW: NUW); |
| 667 | LLVMSetNSW(ArithInst: Dst, HasNSW: NSW); |
| 668 | break; |
| 669 | } |
| 670 | case LLVMUDiv: { |
| 671 | LLVMValueRef LHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 672 | LLVMValueRef RHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 673 | LLVMBool IsExact = LLVMGetExact(DivOrShrInst: Src); |
| 674 | Dst = LLVMBuildUDiv(Builder, LHS, RHS, Name); |
| 675 | LLVMSetExact(DivOrShrInst: Dst, IsExact); |
| 676 | break; |
| 677 | } |
| 678 | case LLVMSDiv: { |
| 679 | LLVMValueRef LHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 680 | LLVMValueRef RHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 681 | LLVMBool IsExact = LLVMGetExact(DivOrShrInst: Src); |
| 682 | Dst = LLVMBuildSDiv(Builder, LHS, RHS, Name); |
| 683 | LLVMSetExact(DivOrShrInst: Dst, IsExact); |
| 684 | break; |
| 685 | } |
| 686 | case LLVMURem: { |
| 687 | LLVMValueRef LHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 688 | LLVMValueRef RHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 689 | Dst = LLVMBuildURem(Builder, LHS, RHS, Name); |
| 690 | break; |
| 691 | } |
| 692 | case LLVMSRem: { |
| 693 | LLVMValueRef LHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 694 | LLVMValueRef RHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 695 | Dst = LLVMBuildSRem(Builder, LHS, RHS, Name); |
| 696 | break; |
| 697 | } |
| 698 | case LLVMShl: { |
| 699 | LLVMValueRef LHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 700 | LLVMValueRef RHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 701 | LLVMBool NUW = LLVMGetNUW(ArithInst: Src); |
| 702 | LLVMBool NSW = LLVMGetNSW(ArithInst: Src); |
| 703 | Dst = LLVMBuildShl(Builder, LHS, RHS, Name); |
| 704 | LLVMSetNUW(ArithInst: Dst, HasNUW: NUW); |
| 705 | LLVMSetNSW(ArithInst: Dst, HasNSW: NSW); |
| 706 | break; |
| 707 | } |
| 708 | case LLVMLShr: { |
| 709 | LLVMValueRef LHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 710 | LLVMValueRef RHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 711 | LLVMBool IsExact = LLVMGetExact(DivOrShrInst: Src); |
| 712 | Dst = LLVMBuildLShr(Builder, LHS, RHS, Name); |
| 713 | LLVMSetExact(DivOrShrInst: Dst, IsExact); |
| 714 | break; |
| 715 | } |
| 716 | case LLVMAShr: { |
| 717 | LLVMValueRef LHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 718 | LLVMValueRef RHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 719 | LLVMBool IsExact = LLVMGetExact(DivOrShrInst: Src); |
| 720 | Dst = LLVMBuildAShr(Builder, LHS, RHS, Name); |
| 721 | LLVMSetExact(DivOrShrInst: Dst, IsExact); |
| 722 | break; |
| 723 | } |
| 724 | case LLVMAnd: { |
| 725 | LLVMValueRef LHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 726 | LLVMValueRef RHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 727 | Dst = LLVMBuildAnd(Builder, LHS, RHS, Name); |
| 728 | break; |
| 729 | } |
| 730 | case LLVMOr: { |
| 731 | LLVMValueRef LHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 732 | LLVMValueRef RHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 733 | LLVMBool IsDisjoint = LLVMGetIsDisjoint(Inst: Src); |
| 734 | Dst = LLVMBuildOr(Builder, LHS, RHS, Name); |
| 735 | LLVMSetIsDisjoint(Inst: Dst, IsDisjoint); |
| 736 | break; |
| 737 | } |
| 738 | case LLVMXor: { |
| 739 | LLVMValueRef LHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 740 | LLVMValueRef RHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 741 | Dst = LLVMBuildXor(Builder, LHS, RHS, Name); |
| 742 | break; |
| 743 | } |
| 744 | case LLVMAlloca: { |
| 745 | LLVMTypeRef Ty = CloneType(Src: LLVMGetAllocatedType(Alloca: Src)); |
| 746 | Dst = LLVMBuildAlloca(Builder, Ty, Name); |
| 747 | LLVMSetAlignment(V: Dst, Bytes: LLVMGetAlignment(V: Src)); |
| 748 | break; |
| 749 | } |
| 750 | case LLVMLoad: { |
| 751 | LLVMValueRef Ptr = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 752 | Dst = LLVMBuildLoad2(Builder, Ty: CloneType(Src), PointerVal: Ptr, Name); |
| 753 | LLVMSetAlignment(V: Dst, Bytes: LLVMGetAlignment(V: Src)); |
| 754 | LLVMSetOrdering(MemoryAccessInst: Dst, Ordering: LLVMGetOrdering(MemoryAccessInst: Src)); |
| 755 | LLVMSetVolatile(MemoryAccessInst: Dst, IsVolatile: LLVMGetVolatile(Inst: Src)); |
| 756 | if (LLVMIsAtomic(Inst: Src)) |
| 757 | LLVMSetAtomicSyncScopeID(AtomicInst: Dst, SSID: LLVMGetAtomicSyncScopeID(AtomicInst: Src)); |
| 758 | break; |
| 759 | } |
| 760 | case LLVMStore: { |
| 761 | LLVMValueRef Val = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 762 | LLVMValueRef Ptr = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 763 | Dst = LLVMBuildStore(Builder, Val, Ptr); |
| 764 | LLVMSetAlignment(V: Dst, Bytes: LLVMGetAlignment(V: Src)); |
| 765 | LLVMSetOrdering(MemoryAccessInst: Dst, Ordering: LLVMGetOrdering(MemoryAccessInst: Src)); |
| 766 | LLVMSetVolatile(MemoryAccessInst: Dst, IsVolatile: LLVMGetVolatile(Inst: Src)); |
| 767 | if (LLVMIsAtomic(Inst: Src)) |
| 768 | LLVMSetAtomicSyncScopeID(AtomicInst: Dst, SSID: LLVMGetAtomicSyncScopeID(AtomicInst: Src)); |
| 769 | break; |
| 770 | } |
| 771 | case LLVMGetElementPtr: { |
| 772 | LLVMTypeRef ElemTy = CloneType(Src: LLVMGetGEPSourceElementType(GEP: Src)); |
| 773 | LLVMValueRef Ptr = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 774 | SmallVector<LLVMValueRef, 8> Idx; |
| 775 | int NumIdx = LLVMGetNumIndices(Inst: Src); |
| 776 | for (int i = 1; i <= NumIdx; i++) |
| 777 | Idx.push_back(Elt: CloneValue(Src: LLVMGetOperand(Val: Src, Index: i))); |
| 778 | |
| 779 | Dst = LLVMBuildGEPWithNoWrapFlags(B: Builder, Ty: ElemTy, Pointer: Ptr, Indices: Idx.data(), |
| 780 | NumIndices: NumIdx, Name, |
| 781 | NoWrapFlags: LLVMGEPGetNoWrapFlags(GEP: Src)); |
| 782 | break; |
| 783 | } |
| 784 | case LLVMAtomicRMW: { |
| 785 | LLVMValueRef Ptr = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 786 | LLVMValueRef Val = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 787 | LLVMAtomicRMWBinOp BinOp = LLVMGetAtomicRMWBinOp(AtomicRMWInst: Src); |
| 788 | LLVMAtomicOrdering Ord = LLVMGetOrdering(MemoryAccessInst: Src); |
| 789 | Dst = LLVMBuildAtomicRMWSyncScope(B: Builder, op: BinOp, PTR: Ptr, Val, ordering: Ord, |
| 790 | SSID: LLVMGetAtomicSyncScopeID(AtomicInst: Src)); |
| 791 | LLVMSetAlignment(V: Dst, Bytes: LLVMGetAlignment(V: Src)); |
| 792 | LLVMSetVolatile(MemoryAccessInst: Dst, IsVolatile: LLVMGetVolatile(Inst: Src)); |
| 793 | LLVMSetValueName2(Val: Dst, Name, NameLen); |
| 794 | break; |
| 795 | } |
| 796 | case LLVMAtomicCmpXchg: { |
| 797 | LLVMValueRef Ptr = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 798 | LLVMValueRef Cmp = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 799 | LLVMValueRef New = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 2)); |
| 800 | LLVMAtomicOrdering Succ = LLVMGetCmpXchgSuccessOrdering(CmpXchgInst: Src); |
| 801 | LLVMAtomicOrdering Fail = LLVMGetCmpXchgFailureOrdering(CmpXchgInst: Src); |
| 802 | Dst = LLVMBuildAtomicCmpXchgSyncScope( |
| 803 | B: Builder, Ptr, Cmp, New, SuccessOrdering: Succ, FailureOrdering: Fail, SSID: LLVMGetAtomicSyncScopeID(AtomicInst: Src)); |
| 804 | LLVMSetAlignment(V: Dst, Bytes: LLVMGetAlignment(V: Src)); |
| 805 | LLVMSetVolatile(MemoryAccessInst: Dst, IsVolatile: LLVMGetVolatile(Inst: Src)); |
| 806 | LLVMSetWeak(CmpXchgInst: Dst, IsWeak: LLVMGetWeak(CmpXchgInst: Src)); |
| 807 | LLVMSetValueName2(Val: Dst, Name, NameLen); |
| 808 | break; |
| 809 | } |
| 810 | case LLVMBitCast: { |
| 811 | LLVMValueRef V = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 812 | Dst = LLVMBuildBitCast(Builder, Val: V, DestTy: CloneType(Src), Name); |
| 813 | break; |
| 814 | } |
| 815 | case LLVMICmp: { |
| 816 | LLVMIntPredicate Pred = LLVMGetICmpPredicate(Inst: Src); |
| 817 | LLVMBool IsSameSign = LLVMGetICmpSameSign(Inst: Src); |
| 818 | LLVMValueRef LHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 819 | LLVMValueRef RHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 820 | Dst = LLVMBuildICmp(Builder, Op: Pred, LHS, RHS, Name); |
| 821 | LLVMSetICmpSameSign(Inst: Dst, SameSign: IsSameSign); |
| 822 | break; |
| 823 | } |
| 824 | case LLVMPHI: { |
| 825 | // We need to aggressively set things here because of loops. |
| 826 | VMap[Src] = Dst = LLVMBuildPhi(Builder, Ty: CloneType(Src), Name); |
| 827 | |
| 828 | SmallVector<LLVMValueRef, 8> Values; |
| 829 | SmallVector<LLVMBasicBlockRef, 8> Blocks; |
| 830 | |
| 831 | unsigned IncomingCount = LLVMCountIncoming(PhiNode: Src); |
| 832 | for (unsigned i = 0; i < IncomingCount; ++i) { |
| 833 | Blocks.push_back(Elt: DeclareBB(Src: LLVMGetIncomingBlock(PhiNode: Src, Index: i))); |
| 834 | Values.push_back(Elt: CloneValue(Src: LLVMGetIncomingValue(PhiNode: Src, Index: i))); |
| 835 | } |
| 836 | |
| 837 | LLVMAddIncoming(PhiNode: Dst, IncomingValues: Values.data(), IncomingBlocks: Blocks.data(), Count: IncomingCount); |
| 838 | // Copy fast math flags here since we return early |
| 839 | if (LLVMCanValueUseFastMathFlags(Inst: Src)) |
| 840 | LLVMSetFastMathFlags(FPMathInst: Dst, FMF: LLVMGetFastMathFlags(FPMathInst: Src)); |
| 841 | return Dst; |
| 842 | } |
| 843 | case LLVMSelect: { |
| 844 | LLVMValueRef If = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 845 | LLVMValueRef Then = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 846 | LLVMValueRef Else = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 2)); |
| 847 | Dst = LLVMBuildSelect(Builder, If, Then, Else, Name); |
| 848 | break; |
| 849 | } |
| 850 | case LLVMCall: { |
| 851 | SmallVector<LLVMValueRef, 8> Args; |
| 852 | SmallVector<LLVMOperandBundleRef, 8> Bundles; |
| 853 | unsigned ArgCount = LLVMGetNumArgOperands(Instr: Src); |
| 854 | for (unsigned i = 0; i < ArgCount; ++i) |
| 855 | Args.push_back(Elt: CloneValue(Src: LLVMGetOperand(Val: Src, Index: i))); |
| 856 | unsigned BundleCount = LLVMGetNumOperandBundles(C: Src); |
| 857 | for (unsigned i = 0; i < BundleCount; ++i) { |
| 858 | auto Bundle = LLVMGetOperandBundleAtIndex(C: Src, Index: i); |
| 859 | Bundles.push_back(Elt: CloneOB(Src: Bundle)); |
| 860 | LLVMDisposeOperandBundle(Bundle); |
| 861 | } |
| 862 | LLVMTypeRef FnTy = CloneType(Src: LLVMGetCalledFunctionType(C: Src)); |
| 863 | LLVMValueRef Fn = CloneValue(Src: LLVMGetCalledValue(Instr: Src)); |
| 864 | Dst = LLVMBuildCallWithOperandBundles(Builder, FnTy, Fn, Args: Args.data(), |
| 865 | NumArgs: ArgCount, Bundles: Bundles.data(), |
| 866 | NumBundles: Bundles.size(), Name); |
| 867 | LLVMSetTailCallKind(CallInst: Dst, kind: LLVMGetTailCallKind(CallInst: Src)); |
| 868 | CloneAttrs(Src, Dst); |
| 869 | for (auto Bundle : Bundles) |
| 870 | LLVMDisposeOperandBundle(Bundle); |
| 871 | break; |
| 872 | } |
| 873 | case LLVMResume: { |
| 874 | Dst = LLVMBuildResume(B: Builder, Exn: CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0))); |
| 875 | break; |
| 876 | } |
| 877 | case LLVMLandingPad: { |
| 878 | // The landing pad API is a bit screwed up for historical reasons. |
| 879 | Dst = LLVMBuildLandingPad(B: Builder, Ty: CloneType(Src), PersFn: nullptr, NumClauses: 0, Name); |
| 880 | unsigned NumClauses = LLVMGetNumClauses(LandingPad: Src); |
| 881 | for (unsigned i = 0; i < NumClauses; ++i) |
| 882 | LLVMAddClause(LandingPad: Dst, ClauseVal: CloneValue(Src: LLVMGetClause(LandingPad: Src, Idx: i))); |
| 883 | LLVMSetCleanup(LandingPad: Dst, Val: LLVMIsCleanup(LandingPad: Src)); |
| 884 | break; |
| 885 | } |
| 886 | case LLVMCleanupRet: { |
| 887 | LLVMValueRef CatchPad = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 888 | LLVMBasicBlockRef Unwind = nullptr; |
| 889 | if (LLVMBasicBlockRef UDest = LLVMGetUnwindDest(InvokeInst: Src)) |
| 890 | Unwind = DeclareBB(Src: UDest); |
| 891 | Dst = LLVMBuildCleanupRet(B: Builder, CatchPad, BB: Unwind); |
| 892 | break; |
| 893 | } |
| 894 | case LLVMCatchRet: { |
| 895 | LLVMValueRef CatchPad = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 896 | LLVMBasicBlockRef SuccBB = DeclareBB(Src: LLVMGetSuccessor(Term: Src, i: 0)); |
| 897 | Dst = LLVMBuildCatchRet(B: Builder, CatchPad, BB: SuccBB); |
| 898 | break; |
| 899 | } |
| 900 | case LLVMCatchPad: { |
| 901 | LLVMValueRef ParentPad = CloneValue(Src: LLVMGetParentCatchSwitch(CatchPad: Src)); |
| 902 | SmallVector<LLVMValueRef, 8> Args; |
| 903 | int ArgCount = LLVMGetNumArgOperands(Instr: Src); |
| 904 | for (int i = 0; i < ArgCount; i++) |
| 905 | Args.push_back(Elt: CloneValue(Src: LLVMGetOperand(Val: Src, Index: i))); |
| 906 | Dst = LLVMBuildCatchPad(B: Builder, ParentPad, |
| 907 | Args: Args.data(), NumArgs: ArgCount, Name); |
| 908 | break; |
| 909 | } |
| 910 | case LLVMCleanupPad: { |
| 911 | LLVMValueRef ParentPad = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 912 | SmallVector<LLVMValueRef, 8> Args; |
| 913 | int ArgCount = LLVMGetNumArgOperands(Instr: Src); |
| 914 | for (int i = 0; i < ArgCount; i++) |
| 915 | Args.push_back(Elt: CloneValue(Src: LLVMGetArgOperand(Funclet: Src, i))); |
| 916 | Dst = LLVMBuildCleanupPad(B: Builder, ParentPad, |
| 917 | Args: Args.data(), NumArgs: ArgCount, Name); |
| 918 | break; |
| 919 | } |
| 920 | case LLVMCatchSwitch: { |
| 921 | LLVMValueRef ParentPad = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 922 | LLVMBasicBlockRef UnwindBB = nullptr; |
| 923 | if (LLVMBasicBlockRef UDest = LLVMGetUnwindDest(InvokeInst: Src)) { |
| 924 | UnwindBB = DeclareBB(Src: UDest); |
| 925 | } |
| 926 | unsigned NumHandlers = LLVMGetNumHandlers(CatchSwitch: Src); |
| 927 | Dst = LLVMBuildCatchSwitch(B: Builder, ParentPad, UnwindBB, NumHandlers, Name); |
| 928 | if (NumHandlers > 0) { |
| 929 | LLVMBasicBlockRef *Handlers = static_cast<LLVMBasicBlockRef*>( |
| 930 | safe_malloc(Sz: NumHandlers * sizeof(LLVMBasicBlockRef))); |
| 931 | LLVMGetHandlers(CatchSwitch: Src, Handlers); |
| 932 | for (unsigned i = 0; i < NumHandlers; i++) |
| 933 | LLVMAddHandler(CatchSwitch: Dst, Dest: DeclareBB(Src: Handlers[i])); |
| 934 | free(ptr: Handlers); |
| 935 | } |
| 936 | break; |
| 937 | } |
| 938 | case LLVMExtractValue: { |
| 939 | LLVMValueRef Agg = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 940 | if (LLVMGetNumIndices(Inst: Src) > 1) |
| 941 | report_fatal_error(reason: "ExtractValue: Expected only one index" ); |
| 942 | else if (LLVMGetNumIndices(Inst: Src) < 1) |
| 943 | report_fatal_error(reason: "ExtractValue: Expected an index" ); |
| 944 | auto I = LLVMGetIndices(Inst: Src)[0]; |
| 945 | Dst = LLVMBuildExtractValue(Builder, AggVal: Agg, Index: I, Name); |
| 946 | break; |
| 947 | } |
| 948 | case LLVMInsertValue: { |
| 949 | LLVMValueRef Agg = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 950 | LLVMValueRef V = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 951 | if (LLVMGetNumIndices(Inst: Src) > 1) |
| 952 | report_fatal_error(reason: "InsertValue: Expected only one index" ); |
| 953 | else if (LLVMGetNumIndices(Inst: Src) < 1) |
| 954 | report_fatal_error(reason: "InsertValue: Expected an index" ); |
| 955 | auto I = LLVMGetIndices(Inst: Src)[0]; |
| 956 | Dst = LLVMBuildInsertValue(Builder, AggVal: Agg, EltVal: V, Index: I, Name); |
| 957 | break; |
| 958 | } |
| 959 | case LLVMExtractElement: { |
| 960 | LLVMValueRef Agg = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 961 | LLVMValueRef Index = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 962 | Dst = LLVMBuildExtractElement(Builder, VecVal: Agg, Index, Name); |
| 963 | break; |
| 964 | } |
| 965 | case LLVMInsertElement: { |
| 966 | LLVMValueRef Agg = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 967 | LLVMValueRef V = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 968 | LLVMValueRef Index = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 2)); |
| 969 | Dst = LLVMBuildInsertElement(Builder, VecVal: Agg, EltVal: V, Index, Name); |
| 970 | break; |
| 971 | } |
| 972 | case LLVMShuffleVector: { |
| 973 | LLVMValueRef Agg0 = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 974 | LLVMValueRef Agg1 = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 975 | SmallVector<LLVMValueRef, 8> MaskElts; |
| 976 | unsigned NumMaskElts = LLVMGetNumMaskElements(ShuffleVectorInst: Src); |
| 977 | for (unsigned i = 0; i < NumMaskElts; i++) { |
| 978 | int Val = LLVMGetMaskValue(ShuffleVectorInst: Src, Elt: i); |
| 979 | if (Val == LLVMGetUndefMaskElem()) { |
| 980 | MaskElts.push_back(Elt: LLVMGetUndef(Ty: LLVMInt64TypeInContext(C: Ctx))); |
| 981 | } else { |
| 982 | MaskElts.push_back( |
| 983 | Elt: LLVMConstInt(IntTy: LLVMInt64TypeInContext(C: Ctx), N: Val, SignExtend: true)); |
| 984 | } |
| 985 | } |
| 986 | LLVMValueRef Mask = LLVMConstVector(ScalarConstantVals: MaskElts.data(), Size: NumMaskElts); |
| 987 | Dst = LLVMBuildShuffleVector(Builder, V1: Agg0, V2: Agg1, Mask, Name); |
| 988 | break; |
| 989 | } |
| 990 | case LLVMFreeze: { |
| 991 | LLVMValueRef Arg = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 992 | Dst = LLVMBuildFreeze(Builder, Val: Arg, Name); |
| 993 | break; |
| 994 | } |
| 995 | case LLVMBitInsert: { |
| 996 | LLVMValueRef Base = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 997 | LLVMValueRef Val = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 998 | LLVMValueRef Offset = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 2)); |
| 999 | Dst = LLVMBuildBitInsert(Builder, Base, Val, Offset, Name); |
| 1000 | break; |
| 1001 | } |
| 1002 | case LLVMBitExtract: { |
| 1003 | LLVMTypeRef Type = CloneType(Src: LLVMTypeOf(Val: Src)); |
| 1004 | LLVMValueRef SrcV = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 1005 | LLVMValueRef Offset = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 1006 | Dst = LLVMBuildBitExtract(Builder, Type, Src: SrcV, Offset, Name); |
| 1007 | break; |
| 1008 | } |
| 1009 | case LLVMFence: { |
| 1010 | LLVMAtomicOrdering Ordering = LLVMGetOrdering(MemoryAccessInst: Src); |
| 1011 | Dst = LLVMBuildFenceSyncScope(B: Builder, ordering: Ordering, |
| 1012 | SSID: LLVMGetAtomicSyncScopeID(AtomicInst: Src), Name); |
| 1013 | break; |
| 1014 | } |
| 1015 | case LLVMZExt: { |
| 1016 | LLVMValueRef Val = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 1017 | LLVMTypeRef DestTy = CloneType(Src: LLVMTypeOf(Val: Src)); |
| 1018 | LLVMBool NNeg = LLVMGetNNeg(NonNegInst: Src); |
| 1019 | Dst = LLVMBuildZExt(Builder, Val, DestTy, Name); |
| 1020 | LLVMSetNNeg(NonNegInst: Dst, IsNonNeg: NNeg); |
| 1021 | break; |
| 1022 | } |
| 1023 | case LLVMFAdd: { |
| 1024 | LLVMValueRef LHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 1025 | LLVMValueRef RHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 1026 | Dst = LLVMBuildFAdd(Builder, LHS, RHS, Name); |
| 1027 | break; |
| 1028 | } |
| 1029 | case LLVMFSub: { |
| 1030 | LLVMValueRef LHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 1031 | LLVMValueRef RHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 1032 | Dst = LLVMBuildFSub(Builder, LHS, RHS, Name); |
| 1033 | break; |
| 1034 | } |
| 1035 | case LLVMFMul: { |
| 1036 | LLVMValueRef LHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 1037 | LLVMValueRef RHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 1038 | Dst = LLVMBuildFMul(Builder, LHS, RHS, Name); |
| 1039 | break; |
| 1040 | } |
| 1041 | case LLVMFDiv: { |
| 1042 | LLVMValueRef LHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 1043 | LLVMValueRef RHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 1044 | Dst = LLVMBuildFDiv(Builder, LHS, RHS, Name); |
| 1045 | break; |
| 1046 | } |
| 1047 | case LLVMFRem: { |
| 1048 | LLVMValueRef LHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 1049 | LLVMValueRef RHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 1050 | Dst = LLVMBuildFRem(Builder, LHS, RHS, Name); |
| 1051 | break; |
| 1052 | } |
| 1053 | case LLVMFNeg: { |
| 1054 | LLVMValueRef Val = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 1055 | Dst = LLVMBuildFNeg(Builder, V: Val, Name); |
| 1056 | break; |
| 1057 | } |
| 1058 | case LLVMFCmp: { |
| 1059 | LLVMRealPredicate Pred = LLVMGetFCmpPredicate(Inst: Src); |
| 1060 | LLVMValueRef LHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 0)); |
| 1061 | LLVMValueRef RHS = CloneValue(Src: LLVMGetOperand(Val: Src, Index: 1)); |
| 1062 | Dst = LLVMBuildFCmp(Builder, Op: Pred, LHS, RHS, Name); |
| 1063 | break; |
| 1064 | } |
| 1065 | default: |
| 1066 | break; |
| 1067 | } |
| 1068 | |
| 1069 | if (Dst == nullptr) { |
| 1070 | fprintf(stderr, format: "%d is not a supported opcode\n" , Op); |
| 1071 | exit(status: -1); |
| 1072 | } |
| 1073 | |
| 1074 | // Copy fast-math flags on instructions that support them |
| 1075 | if (LLVMCanValueUseFastMathFlags(Inst: Src)) |
| 1076 | LLVMSetFastMathFlags(FPMathInst: Dst, FMF: LLVMGetFastMathFlags(FPMathInst: Src)); |
| 1077 | |
| 1078 | size_t NumMetadataEntries; |
| 1079 | auto *AllMetadata = |
| 1080 | LLVMInstructionGetAllMetadataOtherThanDebugLoc(Instr: Src, |
| 1081 | NumEntries: &NumMetadataEntries); |
| 1082 | for (unsigned i = 0; i < NumMetadataEntries; ++i) { |
| 1083 | unsigned Kind = LLVMValueMetadataEntriesGetKind(Entries: AllMetadata, Index: i); |
| 1084 | LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(Entries: AllMetadata, Index: i); |
| 1085 | LLVMSetMetadata(Val: Dst, KindID: Kind, Node: LLVMMetadataAsValue(C: Ctx, MD)); |
| 1086 | } |
| 1087 | LLVMDisposeValueMetadataEntries(Entries: AllMetadata); |
| 1088 | LLVMAddMetadataToInst(Builder, Inst: Dst); |
| 1089 | |
| 1090 | check_value_kind(V: Dst, K: LLVMInstructionValueKind); |
| 1091 | return VMap[Src] = Dst; |
| 1092 | } |
| 1093 | |
| 1094 | LLVMOperandBundleRef CloneOB(LLVMOperandBundleRef Src) { |
| 1095 | size_t TagLen; |
| 1096 | const char *Tag = LLVMGetOperandBundleTag(Bundle: Src, Len: &TagLen); |
| 1097 | |
| 1098 | SmallVector<LLVMValueRef, 8> Args; |
| 1099 | for (unsigned i = 0, n = LLVMGetNumOperandBundleArgs(Bundle: Src); i != n; ++i) |
| 1100 | Args.push_back(Elt: CloneValue(Src: LLVMGetOperandBundleArgAtIndex(Bundle: Src, Index: i))); |
| 1101 | |
| 1102 | return LLVMCreateOperandBundle(Tag, TagLen, Args: Args.data(), NumArgs: Args.size()); |
| 1103 | } |
| 1104 | |
| 1105 | LLVMBasicBlockRef DeclareBB(LLVMBasicBlockRef Src) { |
| 1106 | // Check if this is something we already computed. |
| 1107 | { |
| 1108 | auto i = BBMap.find(Val: Src); |
| 1109 | if (i != BBMap.end()) { |
| 1110 | return i->second; |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | LLVMValueRef V = LLVMBasicBlockAsValue(BB: Src); |
| 1115 | if (!LLVMValueIsBasicBlock(Val: V) || LLVMValueAsBasicBlock(Val: V) != Src) |
| 1116 | report_fatal_error(reason: "Basic block is not a basic block" ); |
| 1117 | |
| 1118 | const char *Name = LLVMGetBasicBlockName(BB: Src); |
| 1119 | size_t NameLen; |
| 1120 | const char *VName = LLVMGetValueName2(Val: V, Length: &NameLen); |
| 1121 | if (Name != VName) |
| 1122 | report_fatal_error(reason: "Basic block name mismatch" ); |
| 1123 | |
| 1124 | LLVMContextRef Ctx = LLVMGetModuleContext(M); |
| 1125 | LLVMBasicBlockRef BB = LLVMAppendBasicBlockInContext(C: Ctx, Fn: Fun, Name); |
| 1126 | if (LLVMGetBasicBlockTerminator(BB) != nullptr) |
| 1127 | report_fatal_error(reason: "Basic block must not have terminator" ); |
| 1128 | return BBMap[Src] = BB; |
| 1129 | } |
| 1130 | |
| 1131 | LLVMBasicBlockRef CloneBB(LLVMBasicBlockRef Src) { |
| 1132 | LLVMBasicBlockRef BB = DeclareBB(Src); |
| 1133 | |
| 1134 | // Make sure ordering is correct. |
| 1135 | LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(BB: Src); |
| 1136 | if (Prev) |
| 1137 | LLVMMoveBasicBlockAfter(BB, MovePos: DeclareBB(Src: Prev)); |
| 1138 | |
| 1139 | LLVMValueRef First = LLVMGetFirstInstruction(BB: Src); |
| 1140 | LLVMValueRef Last = LLVMGetLastInstruction(BB: Src); |
| 1141 | |
| 1142 | if (First == nullptr) { |
| 1143 | if (Last != nullptr) |
| 1144 | report_fatal_error(reason: "Has no first instruction, but last one" ); |
| 1145 | return BB; |
| 1146 | } |
| 1147 | |
| 1148 | LLVMBuilderRef Builder = LLVMCreateBuilderForModule(M); |
| 1149 | LLVMPositionBuilderAtEnd(Builder, Block: BB); |
| 1150 | |
| 1151 | LLVMValueRef Cur = First; |
| 1152 | LLVMValueRef Next = nullptr; |
| 1153 | while(true) { |
| 1154 | CloneInstruction(Src: Cur, Builder); |
| 1155 | Next = LLVMGetNextInstruction(Inst: Cur); |
| 1156 | if (Next == nullptr) { |
| 1157 | if (Cur != Last) |
| 1158 | report_fatal_error(reason: "Final instruction does not match Last" ); |
| 1159 | break; |
| 1160 | } |
| 1161 | |
| 1162 | LLVMValueRef Prev = LLVMGetPreviousInstruction(Inst: Next); |
| 1163 | if (Prev != Cur) |
| 1164 | report_fatal_error(reason: "Next.Previous instruction is not Current" ); |
| 1165 | |
| 1166 | Cur = Next; |
| 1167 | } |
| 1168 | |
| 1169 | if (LLVMGetBasicBlockTerminator(BB) != LLVMGetLastInstruction(BB)) |
| 1170 | report_fatal_error(reason: "Basic block terminator mismatch" ); |
| 1171 | |
| 1172 | LLVMDisposeBuilder(Builder); |
| 1173 | return BB; |
| 1174 | } |
| 1175 | |
| 1176 | void CloneBBs(LLVMValueRef Src) { |
| 1177 | unsigned Count = LLVMCountBasicBlocks(Fn: Src); |
| 1178 | if (Count == 0) |
| 1179 | return; |
| 1180 | |
| 1181 | LLVMBasicBlockRef First = LLVMGetFirstBasicBlock(Fn: Src); |
| 1182 | LLVMBasicBlockRef Last = LLVMGetLastBasicBlock(Fn: Src); |
| 1183 | |
| 1184 | LLVMBasicBlockRef Cur = First; |
| 1185 | LLVMBasicBlockRef Next = nullptr; |
| 1186 | while(true) { |
| 1187 | CloneBB(Src: Cur); |
| 1188 | Count--; |
| 1189 | Next = LLVMGetNextBasicBlock(BB: Cur); |
| 1190 | if (Next == nullptr) { |
| 1191 | if (Cur != Last) |
| 1192 | report_fatal_error(reason: "Final basic block does not match Last" ); |
| 1193 | break; |
| 1194 | } |
| 1195 | |
| 1196 | LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(BB: Next); |
| 1197 | if (Prev != Cur) |
| 1198 | report_fatal_error(reason: "Next.Previous basic bloc is not Current" ); |
| 1199 | |
| 1200 | Cur = Next; |
| 1201 | } |
| 1202 | |
| 1203 | if (Count != 0) |
| 1204 | report_fatal_error(reason: "Basic block count does not match iterration" ); |
| 1205 | } |
| 1206 | }; |
| 1207 | |
| 1208 | static void declare_symbols(LLVMModuleRef Src, LLVMModuleRef M) { |
| 1209 | auto Ctx = LLVMGetModuleContext(M); |
| 1210 | |
| 1211 | LLVMValueRef Begin = LLVMGetFirstGlobal(M: Src); |
| 1212 | LLVMValueRef End = LLVMGetLastGlobal(M: Src); |
| 1213 | |
| 1214 | LLVMValueRef Cur = Begin; |
| 1215 | LLVMValueRef Next = nullptr; |
| 1216 | if (!Begin) { |
| 1217 | if (End != nullptr) |
| 1218 | report_fatal_error(reason: "Range has an end but no beginning" ); |
| 1219 | goto FunDecl; |
| 1220 | } |
| 1221 | |
| 1222 | while (true) { |
| 1223 | size_t NameLen; |
| 1224 | const char *Name = LLVMGetValueName2(Val: Cur, Length: &NameLen); |
| 1225 | if (LLVMGetNamedGlobal(M, Name)) |
| 1226 | report_fatal_error(reason: "GlobalVariable already cloned" ); |
| 1227 | LLVMAddGlobal(M, Ty: TypeCloner(M).Clone(Src: LLVMGlobalGetValueType(Global: Cur)), Name); |
| 1228 | |
| 1229 | Next = LLVMGetNextGlobal(GlobalVar: Cur); |
| 1230 | if (Next == nullptr) { |
| 1231 | if (Cur != End) |
| 1232 | report_fatal_error(reason: "" ); |
| 1233 | break; |
| 1234 | } |
| 1235 | |
| 1236 | LLVMValueRef Prev = LLVMGetPreviousGlobal(GlobalVar: Next); |
| 1237 | if (Prev != Cur) |
| 1238 | report_fatal_error(reason: "Next.Previous global is not Current" ); |
| 1239 | |
| 1240 | Cur = Next; |
| 1241 | } |
| 1242 | |
| 1243 | FunDecl: |
| 1244 | Begin = LLVMGetFirstFunction(M: Src); |
| 1245 | End = LLVMGetLastFunction(M: Src); |
| 1246 | if (!Begin) { |
| 1247 | if (End != nullptr) |
| 1248 | report_fatal_error(reason: "Range has an end but no beginning" ); |
| 1249 | goto AliasDecl; |
| 1250 | } |
| 1251 | |
| 1252 | Cur = Begin; |
| 1253 | Next = nullptr; |
| 1254 | while (true) { |
| 1255 | size_t NameLen; |
| 1256 | const char *Name = LLVMGetValueName2(Val: Cur, Length: &NameLen); |
| 1257 | if (LLVMGetNamedFunction(M, Name)) |
| 1258 | report_fatal_error(reason: "Function already cloned" ); |
| 1259 | LLVMTypeRef Ty = TypeCloner(M).Clone(Src: LLVMGlobalGetValueType(Global: Cur)); |
| 1260 | |
| 1261 | auto F = LLVMAddFunction(M, Name, FunctionTy: Ty); |
| 1262 | LLVMSetDSOLocal(Global: F, IsDSOLocal: LLVMIsDSOLocal(Global: Cur)); |
| 1263 | |
| 1264 | // Copy attributes |
| 1265 | for (int i = LLVMAttributeFunctionIndex, c = LLVMCountParams(Fn: F); |
| 1266 | i <= c; ++i) { |
| 1267 | for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) { |
| 1268 | if (auto SrcA = LLVMGetEnumAttributeAtIndex(F: Cur, Idx: i, KindID: k)) { |
| 1269 | auto Val = LLVMGetEnumAttributeValue(A: SrcA); |
| 1270 | auto DstA = LLVMCreateEnumAttribute(C: Ctx, KindID: k, Val); |
| 1271 | LLVMAddAttributeAtIndex(F, Idx: i, A: DstA); |
| 1272 | } |
| 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | Next = LLVMGetNextFunction(Fn: Cur); |
| 1277 | if (Next == nullptr) { |
| 1278 | if (Cur != End) |
| 1279 | report_fatal_error(reason: "Last function does not match End" ); |
| 1280 | break; |
| 1281 | } |
| 1282 | |
| 1283 | LLVMValueRef Prev = LLVMGetPreviousFunction(Fn: Next); |
| 1284 | if (Prev != Cur) |
| 1285 | report_fatal_error(reason: "Next.Previous function is not Current" ); |
| 1286 | |
| 1287 | Cur = Next; |
| 1288 | } |
| 1289 | |
| 1290 | AliasDecl: |
| 1291 | Begin = LLVMGetFirstGlobalAlias(M: Src); |
| 1292 | End = LLVMGetLastGlobalAlias(M: Src); |
| 1293 | if (!Begin) { |
| 1294 | if (End != nullptr) |
| 1295 | report_fatal_error(reason: "Range has an end but no beginning" ); |
| 1296 | goto GlobalIFuncDecl; |
| 1297 | } |
| 1298 | |
| 1299 | Cur = Begin; |
| 1300 | Next = nullptr; |
| 1301 | while (true) { |
| 1302 | size_t NameLen; |
| 1303 | const char *Name = LLVMGetValueName2(Val: Cur, Length: &NameLen); |
| 1304 | if (LLVMGetNamedGlobalAlias(M, Name, NameLen)) |
| 1305 | report_fatal_error(reason: "Global alias already cloned" ); |
| 1306 | LLVMTypeRef PtrType = TypeCloner(M).Clone(Src: Cur); |
| 1307 | LLVMTypeRef ValType = TypeCloner(M).Clone(Src: LLVMGlobalGetValueType(Global: Cur)); |
| 1308 | unsigned AddrSpace = LLVMGetPointerAddressSpace(PointerTy: PtrType); |
| 1309 | // FIXME: Allow NULL aliasee. |
| 1310 | LLVMAddAlias2(M, ValueTy: ValType, AddrSpace, Aliasee: LLVMGetUndef(Ty: PtrType), Name); |
| 1311 | |
| 1312 | Next = LLVMGetNextGlobalAlias(GA: Cur); |
| 1313 | if (Next == nullptr) { |
| 1314 | if (Cur != End) |
| 1315 | report_fatal_error(reason: "" ); |
| 1316 | break; |
| 1317 | } |
| 1318 | |
| 1319 | LLVMValueRef Prev = LLVMGetPreviousGlobalAlias(GA: Next); |
| 1320 | if (Prev != Cur) |
| 1321 | report_fatal_error(reason: "Next.Previous global is not Current" ); |
| 1322 | |
| 1323 | Cur = Next; |
| 1324 | } |
| 1325 | |
| 1326 | GlobalIFuncDecl: |
| 1327 | Begin = LLVMGetFirstGlobalIFunc(M: Src); |
| 1328 | End = LLVMGetLastGlobalIFunc(M: Src); |
| 1329 | if (!Begin) { |
| 1330 | if (End != nullptr) |
| 1331 | report_fatal_error(reason: "Range has an end but no beginning" ); |
| 1332 | goto NamedMDDecl; |
| 1333 | } |
| 1334 | |
| 1335 | Cur = Begin; |
| 1336 | Next = nullptr; |
| 1337 | while (true) { |
| 1338 | size_t NameLen; |
| 1339 | const char *Name = LLVMGetValueName2(Val: Cur, Length: &NameLen); |
| 1340 | if (LLVMGetNamedGlobalIFunc(M, Name, NameLen)) |
| 1341 | report_fatal_error(reason: "Global ifunc already cloned" ); |
| 1342 | LLVMTypeRef CurType = TypeCloner(M).Clone(Src: LLVMGlobalGetValueType(Global: Cur)); |
| 1343 | // FIXME: Allow NULL resolver. |
| 1344 | LLVMAddGlobalIFunc(M, Name, NameLen, |
| 1345 | Ty: CurType, /*addressSpace*/ AddrSpace: 0, Resolver: LLVMGetUndef(Ty: CurType)); |
| 1346 | |
| 1347 | Next = LLVMGetNextGlobalIFunc(IFunc: Cur); |
| 1348 | if (Next == nullptr) { |
| 1349 | if (Cur != End) |
| 1350 | report_fatal_error(reason: "" ); |
| 1351 | break; |
| 1352 | } |
| 1353 | |
| 1354 | LLVMValueRef Prev = LLVMGetPreviousGlobalIFunc(IFunc: Next); |
| 1355 | if (Prev != Cur) |
| 1356 | report_fatal_error(reason: "Next.Previous global is not Current" ); |
| 1357 | |
| 1358 | Cur = Next; |
| 1359 | } |
| 1360 | |
| 1361 | NamedMDDecl: |
| 1362 | LLVMNamedMDNodeRef BeginMD = LLVMGetFirstNamedMetadata(M: Src); |
| 1363 | LLVMNamedMDNodeRef EndMD = LLVMGetLastNamedMetadata(M: Src); |
| 1364 | if (!BeginMD) { |
| 1365 | if (EndMD != nullptr) |
| 1366 | report_fatal_error(reason: "Range has an end but no beginning" ); |
| 1367 | return; |
| 1368 | } |
| 1369 | |
| 1370 | LLVMNamedMDNodeRef CurMD = BeginMD; |
| 1371 | LLVMNamedMDNodeRef NextMD = nullptr; |
| 1372 | while (true) { |
| 1373 | size_t NameLen; |
| 1374 | const char *Name = LLVMGetNamedMetadataName(NamedMD: CurMD, NameLen: &NameLen); |
| 1375 | if (LLVMGetNamedMetadata(M, Name, NameLen)) |
| 1376 | report_fatal_error(reason: "Named Metadata Node already cloned" ); |
| 1377 | LLVMGetOrInsertNamedMetadata(M, Name, NameLen); |
| 1378 | |
| 1379 | NextMD = LLVMGetNextNamedMetadata(NamedMDNode: CurMD); |
| 1380 | if (NextMD == nullptr) { |
| 1381 | if (CurMD != EndMD) |
| 1382 | report_fatal_error(reason: "" ); |
| 1383 | break; |
| 1384 | } |
| 1385 | |
| 1386 | LLVMNamedMDNodeRef PrevMD = LLVMGetPreviousNamedMetadata(NamedMDNode: NextMD); |
| 1387 | if (PrevMD != CurMD) |
| 1388 | report_fatal_error(reason: "Next.Previous global is not Current" ); |
| 1389 | |
| 1390 | CurMD = NextMD; |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) { |
| 1395 | LLVMValueRef Begin = LLVMGetFirstGlobal(M: Src); |
| 1396 | LLVMValueRef End = LLVMGetLastGlobal(M: Src); |
| 1397 | |
| 1398 | LLVMValueRef Cur = Begin; |
| 1399 | LLVMValueRef Next = nullptr; |
| 1400 | if (!Begin) { |
| 1401 | if (End != nullptr) |
| 1402 | report_fatal_error(reason: "Range has an end but no beginning" ); |
| 1403 | goto FunClone; |
| 1404 | } |
| 1405 | |
| 1406 | while (true) { |
| 1407 | size_t NameLen; |
| 1408 | const char *Name = LLVMGetValueName2(Val: Cur, Length: &NameLen); |
| 1409 | LLVMValueRef G = LLVMGetNamedGlobal(M, Name); |
| 1410 | if (!G) |
| 1411 | report_fatal_error(reason: "GlobalVariable must have been declared already" ); |
| 1412 | |
| 1413 | if (auto I = LLVMGetInitializer(GlobalVar: Cur)) |
| 1414 | LLVMSetInitializer(GlobalVar: G, ConstantVal: clone_constant(Cst: I, M)); |
| 1415 | |
| 1416 | size_t NumMetadataEntries; |
| 1417 | auto *AllMetadata = LLVMGlobalCopyAllMetadata(Value: Cur, NumEntries: &NumMetadataEntries); |
| 1418 | for (unsigned i = 0; i < NumMetadataEntries; ++i) { |
| 1419 | unsigned Kind = LLVMValueMetadataEntriesGetKind(Entries: AllMetadata, Index: i); |
| 1420 | LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(Entries: AllMetadata, Index: i); |
| 1421 | LLVMGlobalSetMetadata(Global: G, Kind, MD); |
| 1422 | } |
| 1423 | LLVMDisposeValueMetadataEntries(Entries: AllMetadata); |
| 1424 | |
| 1425 | LLVMSetGlobalConstant(GlobalVar: G, IsConstant: LLVMIsGlobalConstant(GlobalVar: Cur)); |
| 1426 | LLVMSetThreadLocal(GlobalVar: G, IsThreadLocal: LLVMIsThreadLocal(GlobalVar: Cur)); |
| 1427 | LLVMSetExternallyInitialized(GlobalVar: G, IsExtInit: LLVMIsExternallyInitialized(GlobalVar: Cur)); |
| 1428 | LLVMSetLinkage(Global: G, Linkage: LLVMGetLinkage(Global: Cur)); |
| 1429 | LLVMSetSection(Global: G, Section: LLVMGetSection(Global: Cur)); |
| 1430 | LLVMSetVisibility(Global: G, Viz: LLVMGetVisibility(Global: Cur)); |
| 1431 | LLVMSetUnnamedAddress(Global: G, UnnamedAddr: LLVMGetUnnamedAddress(Global: Cur)); |
| 1432 | LLVMSetDSOLocal(Global: G, IsDSOLocal: LLVMIsDSOLocal(Global: Cur)); |
| 1433 | LLVMSetAlignment(V: G, Bytes: LLVMGetAlignment(V: Cur)); |
| 1434 | |
| 1435 | Next = LLVMGetNextGlobal(GlobalVar: Cur); |
| 1436 | if (Next == nullptr) { |
| 1437 | if (Cur != End) |
| 1438 | report_fatal_error(reason: "" ); |
| 1439 | break; |
| 1440 | } |
| 1441 | |
| 1442 | LLVMValueRef Prev = LLVMGetPreviousGlobal(GlobalVar: Next); |
| 1443 | if (Prev != Cur) |
| 1444 | report_fatal_error(reason: "Next.Previous global is not Current" ); |
| 1445 | |
| 1446 | Cur = Next; |
| 1447 | } |
| 1448 | |
| 1449 | FunClone: |
| 1450 | Begin = LLVMGetFirstFunction(M: Src); |
| 1451 | End = LLVMGetLastFunction(M: Src); |
| 1452 | if (!Begin) { |
| 1453 | if (End != nullptr) |
| 1454 | report_fatal_error(reason: "Range has an end but no beginning" ); |
| 1455 | goto AliasClone; |
| 1456 | } |
| 1457 | |
| 1458 | Cur = Begin; |
| 1459 | Next = nullptr; |
| 1460 | while (true) { |
| 1461 | size_t NameLen; |
| 1462 | const char *Name = LLVMGetValueName2(Val: Cur, Length: &NameLen); |
| 1463 | LLVMValueRef Fun = LLVMGetNamedFunction(M, Name); |
| 1464 | if (!Fun) |
| 1465 | report_fatal_error(reason: "Function must have been declared already" ); |
| 1466 | |
| 1467 | if (LLVMHasPersonalityFn(Fn: Cur)) { |
| 1468 | size_t FNameLen; |
| 1469 | const char *FName = LLVMGetValueName2(Val: LLVMGetPersonalityFn(Fn: Cur), |
| 1470 | Length: &FNameLen); |
| 1471 | LLVMValueRef P = LLVMGetNamedFunction(M, Name: FName); |
| 1472 | if (!P) |
| 1473 | report_fatal_error(reason: "Could not find personality function" ); |
| 1474 | LLVMSetPersonalityFn(Fn: Fun, PersonalityFn: P); |
| 1475 | } |
| 1476 | |
| 1477 | size_t NumMetadataEntries; |
| 1478 | auto *AllMetadata = LLVMGlobalCopyAllMetadata(Value: Cur, NumEntries: &NumMetadataEntries); |
| 1479 | for (unsigned i = 0; i < NumMetadataEntries; ++i) { |
| 1480 | unsigned Kind = LLVMValueMetadataEntriesGetKind(Entries: AllMetadata, Index: i); |
| 1481 | LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(Entries: AllMetadata, Index: i); |
| 1482 | LLVMGlobalSetMetadata(Global: Fun, Kind, MD); |
| 1483 | } |
| 1484 | LLVMDisposeValueMetadataEntries(Entries: AllMetadata); |
| 1485 | |
| 1486 | // Copy any prefix data that may be on the function |
| 1487 | if (LLVMHasPrefixData(Fn: Cur)) |
| 1488 | LLVMSetPrefixData(Fn: Fun, prefixData: clone_constant(Cst: LLVMGetPrefixData(Fn: Cur), M)); |
| 1489 | |
| 1490 | // Copy any prologue data that may be on the function |
| 1491 | if (LLVMHasPrologueData(Fn: Cur)) |
| 1492 | LLVMSetPrologueData(Fn: Fun, prologueData: clone_constant(Cst: LLVMGetPrologueData(Fn: Cur), M)); |
| 1493 | |
| 1494 | FunCloner FC(Cur, Fun); |
| 1495 | FC.CloneBBs(Src: Cur); |
| 1496 | |
| 1497 | Next = LLVMGetNextFunction(Fn: Cur); |
| 1498 | if (Next == nullptr) { |
| 1499 | if (Cur != End) |
| 1500 | report_fatal_error(reason: "Last function does not match End" ); |
| 1501 | break; |
| 1502 | } |
| 1503 | |
| 1504 | LLVMValueRef Prev = LLVMGetPreviousFunction(Fn: Next); |
| 1505 | if (Prev != Cur) |
| 1506 | report_fatal_error(reason: "Next.Previous function is not Current" ); |
| 1507 | |
| 1508 | Cur = Next; |
| 1509 | } |
| 1510 | |
| 1511 | AliasClone: |
| 1512 | Begin = LLVMGetFirstGlobalAlias(M: Src); |
| 1513 | End = LLVMGetLastGlobalAlias(M: Src); |
| 1514 | if (!Begin) { |
| 1515 | if (End != nullptr) |
| 1516 | report_fatal_error(reason: "Range has an end but no beginning" ); |
| 1517 | goto GlobalIFuncClone; |
| 1518 | } |
| 1519 | |
| 1520 | Cur = Begin; |
| 1521 | Next = nullptr; |
| 1522 | while (true) { |
| 1523 | size_t NameLen; |
| 1524 | const char *Name = LLVMGetValueName2(Val: Cur, Length: &NameLen); |
| 1525 | LLVMValueRef Alias = LLVMGetNamedGlobalAlias(M, Name, NameLen); |
| 1526 | if (!Alias) |
| 1527 | report_fatal_error(reason: "Global alias must have been declared already" ); |
| 1528 | |
| 1529 | if (LLVMValueRef Aliasee = LLVMAliasGetAliasee(Alias: Cur)) { |
| 1530 | LLVMAliasSetAliasee(Alias, Aliasee: clone_constant(Cst: Aliasee, M)); |
| 1531 | } |
| 1532 | |
| 1533 | LLVMSetLinkage(Global: Alias, Linkage: LLVMGetLinkage(Global: Cur)); |
| 1534 | LLVMSetUnnamedAddress(Global: Alias, UnnamedAddr: LLVMGetUnnamedAddress(Global: Cur)); |
| 1535 | LLVMSetDSOLocal(Global: Alias, IsDSOLocal: LLVMIsDSOLocal(Global: Cur)); |
| 1536 | |
| 1537 | Next = LLVMGetNextGlobalAlias(GA: Cur); |
| 1538 | if (Next == nullptr) { |
| 1539 | if (Cur != End) |
| 1540 | report_fatal_error(reason: "Last global alias does not match End" ); |
| 1541 | break; |
| 1542 | } |
| 1543 | |
| 1544 | LLVMValueRef Prev = LLVMGetPreviousGlobalAlias(GA: Next); |
| 1545 | if (Prev != Cur) |
| 1546 | report_fatal_error(reason: "Next.Previous global alias is not Current" ); |
| 1547 | |
| 1548 | Cur = Next; |
| 1549 | } |
| 1550 | |
| 1551 | GlobalIFuncClone: |
| 1552 | Begin = LLVMGetFirstGlobalIFunc(M: Src); |
| 1553 | End = LLVMGetLastGlobalIFunc(M: Src); |
| 1554 | if (!Begin) { |
| 1555 | if (End != nullptr) |
| 1556 | report_fatal_error(reason: "Range has an end but no beginning" ); |
| 1557 | goto NamedMDClone; |
| 1558 | } |
| 1559 | |
| 1560 | Cur = Begin; |
| 1561 | Next = nullptr; |
| 1562 | while (true) { |
| 1563 | size_t NameLen; |
| 1564 | const char *Name = LLVMGetValueName2(Val: Cur, Length: &NameLen); |
| 1565 | LLVMValueRef IFunc = LLVMGetNamedGlobalIFunc(M, Name, NameLen); |
| 1566 | if (!IFunc) |
| 1567 | report_fatal_error(reason: "Global ifunc must have been declared already" ); |
| 1568 | |
| 1569 | if (LLVMValueRef Resolver = LLVMGetGlobalIFuncResolver(IFunc: Cur)) { |
| 1570 | LLVMSetGlobalIFuncResolver(IFunc, Resolver: clone_constant(Cst: Resolver, M)); |
| 1571 | } |
| 1572 | |
| 1573 | LLVMSetLinkage(Global: IFunc, Linkage: LLVMGetLinkage(Global: Cur)); |
| 1574 | LLVMSetUnnamedAddress(Global: IFunc, UnnamedAddr: LLVMGetUnnamedAddress(Global: Cur)); |
| 1575 | |
| 1576 | Next = LLVMGetNextGlobalIFunc(IFunc: Cur); |
| 1577 | if (Next == nullptr) { |
| 1578 | if (Cur != End) |
| 1579 | report_fatal_error(reason: "Last global alias does not match End" ); |
| 1580 | break; |
| 1581 | } |
| 1582 | |
| 1583 | LLVMValueRef Prev = LLVMGetPreviousGlobalIFunc(IFunc: Next); |
| 1584 | if (Prev != Cur) |
| 1585 | report_fatal_error(reason: "Next.Previous global alias is not Current" ); |
| 1586 | |
| 1587 | Cur = Next; |
| 1588 | } |
| 1589 | |
| 1590 | NamedMDClone: |
| 1591 | LLVMNamedMDNodeRef BeginMD = LLVMGetFirstNamedMetadata(M: Src); |
| 1592 | LLVMNamedMDNodeRef EndMD = LLVMGetLastNamedMetadata(M: Src); |
| 1593 | if (!BeginMD) { |
| 1594 | if (EndMD != nullptr) |
| 1595 | report_fatal_error(reason: "Range has an end but no beginning" ); |
| 1596 | return; |
| 1597 | } |
| 1598 | |
| 1599 | LLVMNamedMDNodeRef CurMD = BeginMD; |
| 1600 | LLVMNamedMDNodeRef NextMD = nullptr; |
| 1601 | while (true) { |
| 1602 | size_t NameLen; |
| 1603 | const char *Name = LLVMGetNamedMetadataName(NamedMD: CurMD, NameLen: &NameLen); |
| 1604 | LLVMNamedMDNodeRef NamedMD = LLVMGetNamedMetadata(M, Name, NameLen); |
| 1605 | if (!NamedMD) |
| 1606 | report_fatal_error(reason: "Named MD Node must have been declared already" ); |
| 1607 | |
| 1608 | unsigned OperandCount = LLVMGetNamedMetadataNumOperands(M: Src, Name); |
| 1609 | LLVMValueRef *OperandBuf = static_cast<LLVMValueRef *>( |
| 1610 | safe_malloc(Sz: OperandCount * sizeof(LLVMValueRef))); |
| 1611 | LLVMGetNamedMetadataOperands(M: Src, Name, Dest: OperandBuf); |
| 1612 | for (unsigned i = 0, e = OperandCount; i != e; ++i) { |
| 1613 | LLVMAddNamedMetadataOperand(M, Name, Val: OperandBuf[i]); |
| 1614 | } |
| 1615 | free(ptr: OperandBuf); |
| 1616 | |
| 1617 | NextMD = LLVMGetNextNamedMetadata(NamedMDNode: CurMD); |
| 1618 | if (NextMD == nullptr) { |
| 1619 | if (CurMD != EndMD) |
| 1620 | report_fatal_error(reason: "Last Named MD Node does not match End" ); |
| 1621 | break; |
| 1622 | } |
| 1623 | |
| 1624 | LLVMNamedMDNodeRef PrevMD = LLVMGetPreviousNamedMetadata(NamedMDNode: NextMD); |
| 1625 | if (PrevMD != CurMD) |
| 1626 | report_fatal_error(reason: "Next.Previous Named MD Node is not Current" ); |
| 1627 | |
| 1628 | CurMD = NextMD; |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | int llvm_echo(void) { |
| 1633 | LLVMEnablePrettyStackTrace(); |
| 1634 | |
| 1635 | LLVMContextRef Ctx = LLVMContextCreate(); |
| 1636 | LLVMModuleRef Src = llvm_load_module(C: Ctx, Lazy: false, New: true); |
| 1637 | size_t SourceFileLen; |
| 1638 | const char *SourceFileName = LLVMGetSourceFileName(M: Src, Len: &SourceFileLen); |
| 1639 | size_t ModuleIdentLen; |
| 1640 | const char *ModuleName = LLVMGetModuleIdentifier(M: Src, Len: &ModuleIdentLen); |
| 1641 | LLVMModuleRef M = LLVMModuleCreateWithNameInContext(ModuleID: ModuleName, C: Ctx); |
| 1642 | |
| 1643 | LLVMSetSourceFileName(M, Name: SourceFileName, Len: SourceFileLen); |
| 1644 | LLVMSetModuleIdentifier(M, Ident: ModuleName, Len: ModuleIdentLen); |
| 1645 | |
| 1646 | LLVMSetTarget(M, Triple: LLVMGetTarget(M: Src)); |
| 1647 | LLVMSetModuleDataLayout(M, DL: LLVMGetModuleDataLayout(M: Src)); |
| 1648 | if (strcmp(s1: LLVMGetDataLayoutStr(M), s2: LLVMGetDataLayoutStr(M: Src))) |
| 1649 | report_fatal_error(reason: "Inconsistent DataLayout string representation" ); |
| 1650 | |
| 1651 | size_t ModuleInlineAsmLen; |
| 1652 | const char *ModuleAsm = LLVMGetModuleInlineAsm(M: Src, Len: &ModuleInlineAsmLen); |
| 1653 | LLVMSetModuleInlineAsm2(M, Asm: ModuleAsm, Len: ModuleInlineAsmLen); |
| 1654 | |
| 1655 | declare_symbols(Src, M); |
| 1656 | clone_symbols(Src, M); |
| 1657 | char *Str = LLVMPrintModuleToString(M); |
| 1658 | fputs(s: Str, stdout); |
| 1659 | |
| 1660 | LLVMDisposeMessage(Message: Str); |
| 1661 | LLVMDisposeModule(M: Src); |
| 1662 | LLVMDisposeModule(M); |
| 1663 | LLVMContextDispose(C: Ctx); |
| 1664 | |
| 1665 | return 0; |
| 1666 | } |
| 1667 | |