1 //===-- echo.cpp - tool for testing libLLVM and llvm-c API ----------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the --echo command in llvm-c-test. 10 // 11 // This command uses the C API to read a module and output an exact copy of it 12 // as output. It is used to check that the resulting module matches the input 13 // to validate that the C API can read and write modules properly. 14 // 15 //===----------------------------------------------------------------------===// 16 17 #include "llvm-c-test.h" 18 #include "llvm-c/DebugInfo.h" 19 #include "llvm-c/ErrorHandling.h" 20 #include "llvm-c/Target.h" 21 #include "llvm/ADT/DenseMap.h" 22 #include "llvm/ADT/Hashing.h" 23 #include "llvm/ADT/SmallVector.h" 24 #include "llvm/Support/ErrorHandling.h" 25 26 #include <stdio.h> 27 #include <stdlib.h> 28 29 using namespace llvm; 30 31 // Provide DenseMapInfo for C API opaque types. 32 template<typename T> 33 struct CAPIDenseMap {}; 34 35 // The default DenseMapInfo require to know about pointer alignment. 36 // Because the C API uses opaque pointer types, their alignment is unknown. 37 // As a result, we need to roll out our own implementation. 38 template<typename T> 39 struct CAPIDenseMap<T*> { 40 struct CAPIDenseMapInfo { 41 static inline T* getEmptyKey() { 42 uintptr_t Val = static_cast<uintptr_t>(-1); 43 return reinterpret_cast<T*>(Val); 44 } 45 static inline T* getTombstoneKey() { 46 uintptr_t Val = static_cast<uintptr_t>(-2); 47 return reinterpret_cast<T*>(Val); 48 } 49 static unsigned getHashValue(const T *PtrVal) { 50 return hash_value(PtrVal); 51 } 52 static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; } 53 }; 54 55 typedef DenseMap<T*, T*, CAPIDenseMapInfo> Map; 56 }; 57 58 typedef CAPIDenseMap<LLVMValueRef>::Map ValueMap; 59 typedef CAPIDenseMap<LLVMBasicBlockRef>::Map BasicBlockMap; 60 61 struct TypeCloner { 62 LLVMModuleRef M; 63 LLVMContextRef Ctx; 64 65 TypeCloner(LLVMModuleRef M): M(M), Ctx(LLVMGetModuleContext(M)) {} 66 67 LLVMTypeRef Clone(LLVMValueRef Src) { 68 return Clone(LLVMTypeOf(Src)); 69 } 70 71 LLVMTypeRef Clone(LLVMTypeRef Src) { 72 LLVMTypeKind Kind = LLVMGetTypeKind(Src); 73 switch (Kind) { 74 case LLVMVoidTypeKind: 75 return LLVMVoidTypeInContext(Ctx); 76 case LLVMHalfTypeKind: 77 return LLVMHalfTypeInContext(Ctx); 78 case LLVMBFloatTypeKind: 79 return LLVMHalfTypeInContext(Ctx); 80 case LLVMFloatTypeKind: 81 return LLVMFloatTypeInContext(Ctx); 82 case LLVMDoubleTypeKind: 83 return LLVMDoubleTypeInContext(Ctx); 84 case LLVMX86_FP80TypeKind: 85 return LLVMX86FP80TypeInContext(Ctx); 86 case LLVMFP128TypeKind: 87 return LLVMFP128TypeInContext(Ctx); 88 case LLVMPPC_FP128TypeKind: 89 return LLVMPPCFP128TypeInContext(Ctx); 90 case LLVMLabelTypeKind: 91 return LLVMLabelTypeInContext(Ctx); 92 case LLVMIntegerTypeKind: 93 return LLVMIntTypeInContext(Ctx, LLVMGetIntTypeWidth(Src)); 94 case LLVMFunctionTypeKind: { 95 unsigned ParamCount = LLVMCountParamTypes(Src); 96 LLVMTypeRef* Params = nullptr; 97 if (ParamCount > 0) { 98 Params = static_cast<LLVMTypeRef*>( 99 safe_malloc(ParamCount * sizeof(LLVMTypeRef))); 100 LLVMGetParamTypes(Src, Params); 101 for (unsigned i = 0; i < ParamCount; i++) 102 Params[i] = Clone(Params[i]); 103 } 104 105 LLVMTypeRef FunTy = LLVMFunctionType(Clone(LLVMGetReturnType(Src)), 106 Params, ParamCount, 107 LLVMIsFunctionVarArg(Src)); 108 if (ParamCount > 0) 109 free(Params); 110 return FunTy; 111 } 112 case LLVMStructTypeKind: { 113 LLVMTypeRef S = nullptr; 114 const char *Name = LLVMGetStructName(Src); 115 if (Name) { 116 S = LLVMGetTypeByName2(Ctx, Name); 117 if (S) 118 return S; 119 S = LLVMStructCreateNamed(Ctx, Name); 120 if (LLVMIsOpaqueStruct(Src)) 121 return S; 122 } 123 124 unsigned EltCount = LLVMCountStructElementTypes(Src); 125 SmallVector<LLVMTypeRef, 8> Elts; 126 for (unsigned i = 0; i < EltCount; i++) 127 Elts.push_back(Clone(LLVMStructGetTypeAtIndex(Src, i))); 128 if (Name) 129 LLVMStructSetBody(S, Elts.data(), EltCount, LLVMIsPackedStruct(Src)); 130 else 131 S = LLVMStructTypeInContext(Ctx, Elts.data(), EltCount, 132 LLVMIsPackedStruct(Src)); 133 return S; 134 } 135 case LLVMArrayTypeKind: 136 return LLVMArrayType( 137 Clone(LLVMGetElementType(Src)), 138 LLVMGetArrayLength(Src) 139 ); 140 case LLVMPointerTypeKind: 141 if (LLVMPointerTypeIsOpaque(Src)) 142 return LLVMPointerTypeInContext(Ctx, LLVMGetPointerAddressSpace(Src)); 143 else 144 return LLVMPointerType(Clone(LLVMGetElementType(Src)), 145 LLVMGetPointerAddressSpace(Src)); 146 case LLVMVectorTypeKind: 147 return LLVMVectorType( 148 Clone(LLVMGetElementType(Src)), 149 LLVMGetVectorSize(Src) 150 ); 151 case LLVMScalableVectorTypeKind: 152 return LLVMScalableVectorType(Clone(LLVMGetElementType(Src)), 153 LLVMGetVectorSize(Src)); 154 case LLVMMetadataTypeKind: 155 return LLVMMetadataTypeInContext(Ctx); 156 case LLVMX86_AMXTypeKind: 157 return LLVMX86AMXTypeInContext(Ctx); 158 case LLVMX86_MMXTypeKind: 159 return LLVMX86MMXTypeInContext(Ctx); 160 case LLVMTokenTypeKind: 161 return LLVMTokenTypeInContext(Ctx); 162 } 163 164 fprintf(stderr, "%d is not a supported typekind\n", Kind); 165 exit(-1); 166 } 167 }; 168 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"); 173 174 ValueMap VMap; 175 if (Count == 0) 176 return VMap; 177 178 LLVMValueRef SrcFirst = LLVMGetFirstParam(Src); 179 LLVMValueRef DstFirst = LLVMGetFirstParam(Dst); 180 LLVMValueRef SrcLast = LLVMGetLastParam(Src); 181 LLVMValueRef DstLast = LLVMGetLastParam(Dst); 182 183 LLVMValueRef SrcCur = SrcFirst; 184 LLVMValueRef DstCur = DstFirst; 185 LLVMValueRef SrcNext = nullptr; 186 LLVMValueRef DstNext = nullptr; 187 while (true) { 188 size_t NameLen; 189 const char *Name = LLVMGetValueName2(SrcCur, &NameLen); 190 LLVMSetValueName2(DstCur, Name, NameLen); 191 192 VMap[SrcCur] = DstCur; 193 194 Count--; 195 SrcNext = LLVMGetNextParam(SrcCur); 196 DstNext = LLVMGetNextParam(DstCur); 197 if (SrcNext == nullptr && DstNext == nullptr) { 198 if (SrcCur != SrcLast) 199 report_fatal_error("SrcLast param does not match End"); 200 if (DstCur != DstLast) 201 report_fatal_error("DstLast param does not match End"); 202 break; 203 } 204 205 if (SrcNext == nullptr) 206 report_fatal_error("SrcNext was unexpectedly null"); 207 if (DstNext == nullptr) 208 report_fatal_error("DstNext was unexpectedly null"); 209 210 LLVMValueRef SrcPrev = LLVMGetPreviousParam(SrcNext); 211 if (SrcPrev != SrcCur) 212 report_fatal_error("SrcNext.Previous param is not Current"); 213 214 LLVMValueRef DstPrev = LLVMGetPreviousParam(DstNext); 215 if (DstPrev != DstCur) 216 report_fatal_error("DstNext.Previous param is not Current"); 217 218 SrcCur = SrcNext; 219 DstCur = DstNext; 220 } 221 222 if (Count != 0) 223 report_fatal_error("Parameter count does not match iteration"); 224 225 return VMap; 226 } 227 228 static void check_value_kind(LLVMValueRef V, LLVMValueKind K) { 229 if (LLVMGetValueKind(V) != K) 230 report_fatal_error("LLVMGetValueKind returned incorrect type"); 231 } 232 233 static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M); 234 235 static LLVMValueRef clone_constant(LLVMValueRef Cst, LLVMModuleRef M) { 236 LLVMValueRef Ret = clone_constant_impl(Cst, M); 237 check_value_kind(Ret, LLVMGetValueKind(Cst)); 238 return Ret; 239 } 240 241 static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M) { 242 if (!LLVMIsAConstant(Cst)) 243 report_fatal_error("Expected a constant"); 244 245 // Maybe it is a symbol 246 if (LLVMIsAGlobalValue(Cst)) { 247 size_t NameLen; 248 const char *Name = LLVMGetValueName2(Cst, &NameLen); 249 250 // Try function 251 if (LLVMIsAFunction(Cst)) { 252 check_value_kind(Cst, LLVMFunctionValueKind); 253 254 LLVMValueRef Dst = nullptr; 255 // Try an intrinsic 256 unsigned ID = LLVMGetIntrinsicID(Cst); 257 if (ID > 0 && !LLVMIntrinsicIsOverloaded(ID)) { 258 Dst = LLVMGetIntrinsicDeclaration(M, ID, nullptr, 0); 259 } else { 260 // Try a normal function 261 Dst = LLVMGetNamedFunction(M, Name); 262 } 263 264 if (Dst) 265 return Dst; 266 report_fatal_error("Could not find function"); 267 } 268 269 // Try global variable 270 if (LLVMIsAGlobalVariable(Cst)) { 271 check_value_kind(Cst, LLVMGlobalVariableValueKind); 272 LLVMValueRef Dst = LLVMGetNamedGlobal(M, Name); 273 if (Dst) 274 return Dst; 275 report_fatal_error("Could not find variable"); 276 } 277 278 // Try global alias 279 if (LLVMIsAGlobalAlias(Cst)) { 280 check_value_kind(Cst, LLVMGlobalAliasValueKind); 281 LLVMValueRef Dst = LLVMGetNamedGlobalAlias(M, Name, NameLen); 282 if (Dst) 283 return Dst; 284 report_fatal_error("Could not find alias"); 285 } 286 287 fprintf(stderr, "Could not find @%s\n", Name); 288 exit(-1); 289 } 290 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); 296 } 297 298 // Try zeroinitializer 299 if (LLVMIsAConstantAggregateZero(Cst)) { 300 check_value_kind(Cst, LLVMConstantAggregateZeroValueKind); 301 return LLVMConstNull(TypeCloner(M).Clone(Cst)); 302 } 303 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 unsigned EltCount = LLVMGetArrayLength(Ty); 311 SmallVector<LLVMValueRef, 8> Elts; 312 for (unsigned i = 0; i < EltCount; i++) 313 Elts.push_back(clone_constant(LLVMGetAggregateElement(Cst, i), M)); 314 return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount); 315 } 316 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)); 329 } 330 331 // Try ConstantPointerNull 332 if (LLVMIsAConstantPointerNull(Cst)) { 333 check_value_kind(Cst, LLVMConstantPointerNullValueKind); 334 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst); 335 return LLVMConstNull(Ty); 336 } 337 338 // Try undef 339 if (LLVMIsUndef(Cst)) { 340 check_value_kind(Cst, LLVMUndefValueValueKind); 341 return LLVMGetUndef(TypeCloner(M).Clone(Cst)); 342 } 343 344 // Try poison 345 if (LLVMIsPoison(Cst)) { 346 check_value_kind(Cst, LLVMPoisonValueValueKind); 347 return LLVMGetPoison(TypeCloner(M).Clone(Cst)); 348 } 349 350 // Try null 351 if (LLVMIsNull(Cst)) { 352 check_value_kind(Cst, LLVMConstantTokenNoneValueKind); 353 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst); 354 return LLVMConstNull(Ty); 355 } 356 357 // Try float literal 358 if (LLVMIsAConstantFP(Cst)) { 359 check_value_kind(Cst, LLVMConstantFPValueKind); 360 report_fatal_error("ConstantFP is not supported"); 361 } 362 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); 374 } 375 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"); 380 381 // At this point, it must be a constant expression 382 check_value_kind(Cst, LLVMConstantExprValueKind); 383 384 LLVMOpcode Op = LLVMGetConstOpcode(Cst); 385 switch(Op) { 386 case LLVMBitCast: 387 return LLVMConstBitCast(clone_constant(LLVMGetOperand(Cst, 0), M), 388 TypeCloner(M).Clone(Cst)); 389 case LLVMGetElementPtr: { 390 LLVMTypeRef ElemTy = 391 TypeCloner(M).Clone(LLVMGetGEPSourceElementType(Cst)); 392 LLVMValueRef Ptr = clone_constant(LLVMGetOperand(Cst, 0), M); 393 int NumIdx = LLVMGetNumIndices(Cst); 394 SmallVector<LLVMValueRef, 8> Idx; 395 for (int i = 1; i <= NumIdx; i++) 396 Idx.push_back(clone_constant(LLVMGetOperand(Cst, i), M)); 397 if (LLVMIsInBounds(Cst)) 398 return LLVMConstInBoundsGEP2(ElemTy, Ptr, Idx.data(), NumIdx); 399 else 400 return LLVMConstGEP2(ElemTy, Ptr, Idx.data(), NumIdx); 401 } 402 default: 403 fprintf(stderr, "%d is not a supported opcode for constant expressions\n", 404 Op); 405 exit(-1); 406 } 407 } 408 409 struct FunCloner { 410 LLVMValueRef Fun; 411 LLVMModuleRef M; 412 413 ValueMap VMap; 414 BasicBlockMap BBMap; 415 416 FunCloner(LLVMValueRef Src, LLVMValueRef Dst): Fun(Dst), 417 M(LLVMGetGlobalParent(Fun)), VMap(clone_params(Src, Dst)) {} 418 419 LLVMTypeRef CloneType(LLVMTypeRef Src) { 420 return TypeCloner(M).Clone(Src); 421 } 422 423 LLVMTypeRef CloneType(LLVMValueRef Src) { 424 return TypeCloner(M).Clone(Src); 425 } 426 427 // Try to clone everything in the llvm::Value hierarchy. 428 LLVMValueRef CloneValue(LLVMValueRef Src) { 429 // First, the value may be constant. 430 if (LLVMIsAConstant(Src)) 431 return clone_constant(Src, M); 432 433 // Function argument should always be in the map already. 434 auto i = VMap.find(Src); 435 if (i != VMap.end()) 436 return i->second; 437 438 if (!LLVMIsAInstruction(Src)) 439 report_fatal_error("Expected an instruction"); 440 441 auto Ctx = LLVMGetModuleContext(M); 442 auto Builder = LLVMCreateBuilderInContext(Ctx); 443 auto BB = DeclareBB(LLVMGetInstructionParent(Src)); 444 LLVMPositionBuilderAtEnd(Builder, BB); 445 auto Dst = CloneInstruction(Src, Builder); 446 LLVMDisposeBuilder(Builder); 447 return Dst; 448 } 449 450 void CloneAttrs(LLVMValueRef Src, LLVMValueRef Dst) { 451 auto Ctx = LLVMGetModuleContext(M); 452 int ArgCount = LLVMGetNumArgOperands(Src); 453 for (int i = LLVMAttributeReturnIndex; i <= ArgCount; i++) { 454 for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) { 455 if (auto SrcA = LLVMGetCallSiteEnumAttribute(Src, i, k)) { 456 auto Val = LLVMGetEnumAttributeValue(SrcA); 457 auto A = LLVMCreateEnumAttribute(Ctx, k, Val); 458 LLVMAddCallSiteAttribute(Dst, i, A); 459 } 460 } 461 } 462 } 463 464 LLVMValueRef CloneInstruction(LLVMValueRef Src, LLVMBuilderRef Builder) { 465 check_value_kind(Src, LLVMInstructionValueKind); 466 if (!LLVMIsAInstruction(Src)) 467 report_fatal_error("Expected an instruction"); 468 469 size_t NameLen; 470 const char *Name = LLVMGetValueName2(Src, &NameLen); 471 472 // Check if this is something we already computed. 473 { 474 auto i = VMap.find(Src); 475 if (i != VMap.end()) { 476 // If we have a hit, it means we already generated the instruction 477 // as a dependency to something else. We need to make sure 478 // it is ordered properly. 479 auto I = i->second; 480 LLVMInstructionRemoveFromParent(I); 481 LLVMInsertIntoBuilderWithName(Builder, I, Name); 482 return I; 483 } 484 } 485 486 // We tried everything, it must be an instruction 487 // that hasn't been generated already. 488 LLVMValueRef Dst = nullptr; 489 490 LLVMOpcode Op = LLVMGetInstructionOpcode(Src); 491 switch(Op) { 492 case LLVMRet: { 493 int OpCount = LLVMGetNumOperands(Src); 494 if (OpCount == 0) 495 Dst = LLVMBuildRetVoid(Builder); 496 else 497 Dst = LLVMBuildRet(Builder, CloneValue(LLVMGetOperand(Src, 0))); 498 break; 499 } 500 case LLVMBr: { 501 if (!LLVMIsConditional(Src)) { 502 LLVMValueRef SrcOp = LLVMGetOperand(Src, 0); 503 LLVMBasicBlockRef SrcBB = LLVMValueAsBasicBlock(SrcOp); 504 Dst = LLVMBuildBr(Builder, DeclareBB(SrcBB)); 505 break; 506 } 507 508 LLVMValueRef Cond = LLVMGetCondition(Src); 509 LLVMValueRef Else = LLVMGetOperand(Src, 1); 510 LLVMBasicBlockRef ElseBB = DeclareBB(LLVMValueAsBasicBlock(Else)); 511 LLVMValueRef Then = LLVMGetOperand(Src, 2); 512 LLVMBasicBlockRef ThenBB = DeclareBB(LLVMValueAsBasicBlock(Then)); 513 Dst = LLVMBuildCondBr(Builder, CloneValue(Cond), ThenBB, ElseBB); 514 break; 515 } 516 case LLVMSwitch: 517 case LLVMIndirectBr: 518 break; 519 case LLVMInvoke: { 520 SmallVector<LLVMValueRef, 8> Args; 521 int ArgCount = LLVMGetNumArgOperands(Src); 522 for (int i = 0; i < ArgCount; i++) 523 Args.push_back(CloneValue(LLVMGetOperand(Src, i))); 524 LLVMTypeRef FnTy = CloneType(LLVMGetCalledFunctionType(Src)); 525 LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src)); 526 LLVMBasicBlockRef Then = DeclareBB(LLVMGetNormalDest(Src)); 527 LLVMBasicBlockRef Unwind = DeclareBB(LLVMGetUnwindDest(Src)); 528 Dst = LLVMBuildInvoke2(Builder, FnTy, Fn, Args.data(), ArgCount, 529 Then, Unwind, Name); 530 CloneAttrs(Src, Dst); 531 break; 532 } 533 case LLVMUnreachable: 534 Dst = LLVMBuildUnreachable(Builder); 535 break; 536 case LLVMAdd: { 537 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); 538 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); 539 Dst = LLVMBuildAdd(Builder, LHS, RHS, Name); 540 break; 541 } 542 case LLVMSub: { 543 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); 544 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); 545 Dst = LLVMBuildSub(Builder, LHS, RHS, Name); 546 break; 547 } 548 case LLVMMul: { 549 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); 550 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); 551 Dst = LLVMBuildMul(Builder, LHS, RHS, Name); 552 break; 553 } 554 case LLVMUDiv: { 555 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); 556 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); 557 Dst = LLVMBuildUDiv(Builder, LHS, RHS, Name); 558 break; 559 } 560 case LLVMSDiv: { 561 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); 562 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); 563 Dst = LLVMBuildSDiv(Builder, LHS, RHS, Name); 564 break; 565 } 566 case LLVMURem: { 567 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); 568 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); 569 Dst = LLVMBuildURem(Builder, LHS, RHS, Name); 570 break; 571 } 572 case LLVMSRem: { 573 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); 574 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); 575 Dst = LLVMBuildSRem(Builder, LHS, RHS, Name); 576 break; 577 } 578 case LLVMShl: { 579 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); 580 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); 581 Dst = LLVMBuildShl(Builder, LHS, RHS, Name); 582 break; 583 } 584 case LLVMLShr: { 585 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); 586 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); 587 Dst = LLVMBuildLShr(Builder, LHS, RHS, Name); 588 break; 589 } 590 case LLVMAShr: { 591 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); 592 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); 593 Dst = LLVMBuildAShr(Builder, LHS, RHS, Name); 594 break; 595 } 596 case LLVMAnd: { 597 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); 598 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); 599 Dst = LLVMBuildAnd(Builder, LHS, RHS, Name); 600 break; 601 } 602 case LLVMOr: { 603 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); 604 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); 605 Dst = LLVMBuildOr(Builder, LHS, RHS, Name); 606 break; 607 } 608 case LLVMXor: { 609 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); 610 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); 611 Dst = LLVMBuildXor(Builder, LHS, RHS, Name); 612 break; 613 } 614 case LLVMAlloca: { 615 LLVMTypeRef Ty = CloneType(LLVMGetAllocatedType(Src)); 616 Dst = LLVMBuildAlloca(Builder, Ty, Name); 617 LLVMSetAlignment(Dst, LLVMGetAlignment(Src)); 618 break; 619 } 620 case LLVMLoad: { 621 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0)); 622 Dst = LLVMBuildLoad2(Builder, CloneType(Src), Ptr, Name); 623 LLVMSetAlignment(Dst, LLVMGetAlignment(Src)); 624 LLVMSetOrdering(Dst, LLVMGetOrdering(Src)); 625 LLVMSetVolatile(Dst, LLVMGetVolatile(Src)); 626 break; 627 } 628 case LLVMStore: { 629 LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 0)); 630 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 1)); 631 Dst = LLVMBuildStore(Builder, Val, Ptr); 632 LLVMSetAlignment(Dst, LLVMGetAlignment(Src)); 633 LLVMSetOrdering(Dst, LLVMGetOrdering(Src)); 634 LLVMSetVolatile(Dst, LLVMGetVolatile(Src)); 635 break; 636 } 637 case LLVMGetElementPtr: { 638 LLVMTypeRef ElemTy = CloneType(LLVMGetGEPSourceElementType(Src)); 639 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0)); 640 SmallVector<LLVMValueRef, 8> Idx; 641 int NumIdx = LLVMGetNumIndices(Src); 642 for (int i = 1; i <= NumIdx; i++) 643 Idx.push_back(CloneValue(LLVMGetOperand(Src, i))); 644 if (LLVMIsInBounds(Src)) 645 Dst = LLVMBuildInBoundsGEP2(Builder, ElemTy, Ptr, Idx.data(), NumIdx, 646 Name); 647 else 648 Dst = LLVMBuildGEP2(Builder, ElemTy, Ptr, Idx.data(), NumIdx, Name); 649 break; 650 } 651 case LLVMAtomicRMW: { 652 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0)); 653 LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 1)); 654 LLVMAtomicRMWBinOp BinOp = LLVMGetAtomicRMWBinOp(Src); 655 LLVMAtomicOrdering Ord = LLVMGetOrdering(Src); 656 LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src); 657 Dst = LLVMBuildAtomicRMW(Builder, BinOp, Ptr, Val, Ord, SingleThread); 658 LLVMSetAlignment(Dst, LLVMGetAlignment(Src)); 659 LLVMSetVolatile(Dst, LLVMGetVolatile(Src)); 660 LLVMSetValueName2(Dst, Name, NameLen); 661 break; 662 } 663 case LLVMAtomicCmpXchg: { 664 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0)); 665 LLVMValueRef Cmp = CloneValue(LLVMGetOperand(Src, 1)); 666 LLVMValueRef New = CloneValue(LLVMGetOperand(Src, 2)); 667 LLVMAtomicOrdering Succ = LLVMGetCmpXchgSuccessOrdering(Src); 668 LLVMAtomicOrdering Fail = LLVMGetCmpXchgFailureOrdering(Src); 669 LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src); 670 671 Dst = LLVMBuildAtomicCmpXchg(Builder, Ptr, Cmp, New, Succ, Fail, 672 SingleThread); 673 LLVMSetAlignment(Dst, LLVMGetAlignment(Src)); 674 LLVMSetVolatile(Dst, LLVMGetVolatile(Src)); 675 LLVMSetWeak(Dst, LLVMGetWeak(Src)); 676 LLVMSetValueName2(Dst, Name, NameLen); 677 break; 678 } 679 case LLVMBitCast: { 680 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 0)); 681 Dst = LLVMBuildBitCast(Builder, V, CloneType(Src), Name); 682 break; 683 } 684 case LLVMICmp: { 685 LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src); 686 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); 687 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); 688 Dst = LLVMBuildICmp(Builder, Pred, LHS, RHS, Name); 689 break; 690 } 691 case LLVMPHI: { 692 // We need to aggressively set things here because of loops. 693 VMap[Src] = Dst = LLVMBuildPhi(Builder, CloneType(Src), Name); 694 695 SmallVector<LLVMValueRef, 8> Values; 696 SmallVector<LLVMBasicBlockRef, 8> Blocks; 697 698 unsigned IncomingCount = LLVMCountIncoming(Src); 699 for (unsigned i = 0; i < IncomingCount; ++i) { 700 Blocks.push_back(DeclareBB(LLVMGetIncomingBlock(Src, i))); 701 Values.push_back(CloneValue(LLVMGetIncomingValue(Src, i))); 702 } 703 704 LLVMAddIncoming(Dst, Values.data(), Blocks.data(), IncomingCount); 705 return Dst; 706 } 707 case LLVMCall: { 708 SmallVector<LLVMValueRef, 8> Args; 709 int ArgCount = LLVMGetNumArgOperands(Src); 710 for (int i = 0; i < ArgCount; i++) 711 Args.push_back(CloneValue(LLVMGetOperand(Src, i))); 712 LLVMTypeRef FnTy = CloneType(LLVMGetCalledFunctionType(Src)); 713 LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src)); 714 Dst = LLVMBuildCall2(Builder, FnTy, Fn, Args.data(), ArgCount, Name); 715 LLVMSetTailCall(Dst, LLVMIsTailCall(Src)); 716 CloneAttrs(Src, Dst); 717 break; 718 } 719 case LLVMResume: { 720 Dst = LLVMBuildResume(Builder, CloneValue(LLVMGetOperand(Src, 0))); 721 break; 722 } 723 case LLVMLandingPad: { 724 // The landing pad API is a bit screwed up for historical reasons. 725 Dst = LLVMBuildLandingPad(Builder, CloneType(Src), nullptr, 0, Name); 726 unsigned NumClauses = LLVMGetNumClauses(Src); 727 for (unsigned i = 0; i < NumClauses; ++i) 728 LLVMAddClause(Dst, CloneValue(LLVMGetClause(Src, i))); 729 LLVMSetCleanup(Dst, LLVMIsCleanup(Src)); 730 break; 731 } 732 case LLVMCleanupRet: { 733 LLVMValueRef CatchPad = CloneValue(LLVMGetOperand(Src, 0)); 734 LLVMBasicBlockRef Unwind = nullptr; 735 if (LLVMBasicBlockRef UDest = LLVMGetUnwindDest(Src)) 736 Unwind = DeclareBB(UDest); 737 Dst = LLVMBuildCleanupRet(Builder, CatchPad, Unwind); 738 break; 739 } 740 case LLVMCatchRet: { 741 LLVMValueRef CatchPad = CloneValue(LLVMGetOperand(Src, 0)); 742 LLVMBasicBlockRef SuccBB = DeclareBB(LLVMGetSuccessor(Src, 0)); 743 Dst = LLVMBuildCatchRet(Builder, CatchPad, SuccBB); 744 break; 745 } 746 case LLVMCatchPad: { 747 LLVMValueRef ParentPad = CloneValue(LLVMGetParentCatchSwitch(Src)); 748 SmallVector<LLVMValueRef, 8> Args; 749 int ArgCount = LLVMGetNumArgOperands(Src); 750 for (int i = 0; i < ArgCount; i++) 751 Args.push_back(CloneValue(LLVMGetOperand(Src, i))); 752 Dst = LLVMBuildCatchPad(Builder, ParentPad, 753 Args.data(), ArgCount, Name); 754 break; 755 } 756 case LLVMCleanupPad: { 757 LLVMValueRef ParentPad = CloneValue(LLVMGetOperand(Src, 0)); 758 SmallVector<LLVMValueRef, 8> Args; 759 int ArgCount = LLVMGetNumArgOperands(Src); 760 for (int i = 0; i < ArgCount; i++) 761 Args.push_back(CloneValue(LLVMGetArgOperand(Src, i))); 762 Dst = LLVMBuildCleanupPad(Builder, ParentPad, 763 Args.data(), ArgCount, Name); 764 break; 765 } 766 case LLVMCatchSwitch: { 767 LLVMValueRef ParentPad = CloneValue(LLVMGetOperand(Src, 0)); 768 LLVMBasicBlockRef UnwindBB = nullptr; 769 if (LLVMBasicBlockRef UDest = LLVMGetUnwindDest(Src)) { 770 UnwindBB = DeclareBB(UDest); 771 } 772 unsigned NumHandlers = LLVMGetNumHandlers(Src); 773 Dst = LLVMBuildCatchSwitch(Builder, ParentPad, UnwindBB, NumHandlers, Name); 774 if (NumHandlers > 0) { 775 LLVMBasicBlockRef *Handlers = static_cast<LLVMBasicBlockRef*>( 776 safe_malloc(NumHandlers * sizeof(LLVMBasicBlockRef))); 777 LLVMGetHandlers(Src, Handlers); 778 for (unsigned i = 0; i < NumHandlers; i++) 779 LLVMAddHandler(Dst, DeclareBB(Handlers[i])); 780 free(Handlers); 781 } 782 break; 783 } 784 case LLVMExtractValue: { 785 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0)); 786 if (LLVMGetNumIndices(Src) > 1) 787 report_fatal_error("ExtractValue: Expected only one index"); 788 else if (LLVMGetNumIndices(Src) < 1) 789 report_fatal_error("ExtractValue: Expected an index"); 790 auto I = LLVMGetIndices(Src)[0]; 791 Dst = LLVMBuildExtractValue(Builder, Agg, I, Name); 792 break; 793 } 794 case LLVMInsertValue: { 795 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0)); 796 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 1)); 797 if (LLVMGetNumIndices(Src) > 1) 798 report_fatal_error("InsertValue: Expected only one index"); 799 else if (LLVMGetNumIndices(Src) < 1) 800 report_fatal_error("InsertValue: Expected an index"); 801 auto I = LLVMGetIndices(Src)[0]; 802 Dst = LLVMBuildInsertValue(Builder, Agg, V, I, Name); 803 break; 804 } 805 case LLVMExtractElement: { 806 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0)); 807 LLVMValueRef Index = CloneValue(LLVMGetOperand(Src, 1)); 808 Dst = LLVMBuildExtractElement(Builder, Agg, Index, Name); 809 break; 810 } 811 case LLVMInsertElement: { 812 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0)); 813 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 1)); 814 LLVMValueRef Index = CloneValue(LLVMGetOperand(Src, 2)); 815 Dst = LLVMBuildInsertElement(Builder, Agg, V, Index, Name); 816 break; 817 } 818 case LLVMShuffleVector: { 819 LLVMValueRef Agg0 = CloneValue(LLVMGetOperand(Src, 0)); 820 LLVMValueRef Agg1 = CloneValue(LLVMGetOperand(Src, 1)); 821 SmallVector<LLVMValueRef, 8> MaskElts; 822 unsigned NumMaskElts = LLVMGetNumMaskElements(Src); 823 for (unsigned i = 0; i < NumMaskElts; i++) { 824 int Val = LLVMGetMaskValue(Src, i); 825 if (Val == LLVMGetUndefMaskElem()) { 826 MaskElts.push_back(LLVMGetUndef(LLVMInt64Type())); 827 } else { 828 MaskElts.push_back(LLVMConstInt(LLVMInt64Type(), Val, true)); 829 } 830 } 831 LLVMValueRef Mask = LLVMConstVector(MaskElts.data(), NumMaskElts); 832 Dst = LLVMBuildShuffleVector(Builder, Agg0, Agg1, Mask, Name); 833 break; 834 } 835 case LLVMFreeze: { 836 LLVMValueRef Arg = CloneValue(LLVMGetOperand(Src, 0)); 837 Dst = LLVMBuildFreeze(Builder, Arg, Name); 838 break; 839 } 840 default: 841 break; 842 } 843 844 if (Dst == nullptr) { 845 fprintf(stderr, "%d is not a supported opcode\n", Op); 846 exit(-1); 847 } 848 849 auto Ctx = LLVMGetModuleContext(M); 850 size_t NumMetadataEntries; 851 auto *AllMetadata = 852 LLVMInstructionGetAllMetadataOtherThanDebugLoc(Src, 853 &NumMetadataEntries); 854 for (unsigned i = 0; i < NumMetadataEntries; ++i) { 855 unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i); 856 LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i); 857 LLVMSetMetadata(Dst, Kind, LLVMMetadataAsValue(Ctx, MD)); 858 } 859 LLVMDisposeValueMetadataEntries(AllMetadata); 860 LLVMAddMetadataToInst(Builder, Dst); 861 862 check_value_kind(Dst, LLVMInstructionValueKind); 863 return VMap[Src] = Dst; 864 } 865 866 LLVMBasicBlockRef DeclareBB(LLVMBasicBlockRef Src) { 867 // Check if this is something we already computed. 868 { 869 auto i = BBMap.find(Src); 870 if (i != BBMap.end()) { 871 return i->second; 872 } 873 } 874 875 LLVMValueRef V = LLVMBasicBlockAsValue(Src); 876 if (!LLVMValueIsBasicBlock(V) || LLVMValueAsBasicBlock(V) != Src) 877 report_fatal_error("Basic block is not a basic block"); 878 879 const char *Name = LLVMGetBasicBlockName(Src); 880 size_t NameLen; 881 const char *VName = LLVMGetValueName2(V, &NameLen); 882 if (Name != VName) 883 report_fatal_error("Basic block name mismatch"); 884 885 LLVMBasicBlockRef BB = LLVMAppendBasicBlock(Fun, Name); 886 return BBMap[Src] = BB; 887 } 888 889 LLVMBasicBlockRef CloneBB(LLVMBasicBlockRef Src) { 890 LLVMBasicBlockRef BB = DeclareBB(Src); 891 892 // Make sure ordering is correct. 893 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Src); 894 if (Prev) 895 LLVMMoveBasicBlockAfter(BB, DeclareBB(Prev)); 896 897 LLVMValueRef First = LLVMGetFirstInstruction(Src); 898 LLVMValueRef Last = LLVMGetLastInstruction(Src); 899 900 if (First == nullptr) { 901 if (Last != nullptr) 902 report_fatal_error("Has no first instruction, but last one"); 903 return BB; 904 } 905 906 auto Ctx = LLVMGetModuleContext(M); 907 LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx); 908 LLVMPositionBuilderAtEnd(Builder, BB); 909 910 LLVMValueRef Cur = First; 911 LLVMValueRef Next = nullptr; 912 while(true) { 913 CloneInstruction(Cur, Builder); 914 Next = LLVMGetNextInstruction(Cur); 915 if (Next == nullptr) { 916 if (Cur != Last) 917 report_fatal_error("Final instruction does not match Last"); 918 break; 919 } 920 921 LLVMValueRef Prev = LLVMGetPreviousInstruction(Next); 922 if (Prev != Cur) 923 report_fatal_error("Next.Previous instruction is not Current"); 924 925 Cur = Next; 926 } 927 928 LLVMDisposeBuilder(Builder); 929 return BB; 930 } 931 932 void CloneBBs(LLVMValueRef Src) { 933 unsigned Count = LLVMCountBasicBlocks(Src); 934 if (Count == 0) 935 return; 936 937 LLVMBasicBlockRef First = LLVMGetFirstBasicBlock(Src); 938 LLVMBasicBlockRef Last = LLVMGetLastBasicBlock(Src); 939 940 LLVMBasicBlockRef Cur = First; 941 LLVMBasicBlockRef Next = nullptr; 942 while(true) { 943 CloneBB(Cur); 944 Count--; 945 Next = LLVMGetNextBasicBlock(Cur); 946 if (Next == nullptr) { 947 if (Cur != Last) 948 report_fatal_error("Final basic block does not match Last"); 949 break; 950 } 951 952 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Next); 953 if (Prev != Cur) 954 report_fatal_error("Next.Previous basic bloc is not Current"); 955 956 Cur = Next; 957 } 958 959 if (Count != 0) 960 report_fatal_error("Basic block count does not match iterration"); 961 } 962 }; 963 964 static void declare_symbols(LLVMModuleRef Src, LLVMModuleRef M) { 965 auto Ctx = LLVMGetModuleContext(M); 966 967 LLVMValueRef Begin = LLVMGetFirstGlobal(Src); 968 LLVMValueRef End = LLVMGetLastGlobal(Src); 969 970 LLVMValueRef Cur = Begin; 971 LLVMValueRef Next = nullptr; 972 if (!Begin) { 973 if (End != nullptr) 974 report_fatal_error("Range has an end but no beginning"); 975 goto FunDecl; 976 } 977 978 while (true) { 979 size_t NameLen; 980 const char *Name = LLVMGetValueName2(Cur, &NameLen); 981 if (LLVMGetNamedGlobal(M, Name)) 982 report_fatal_error("GlobalVariable already cloned"); 983 LLVMAddGlobal(M, TypeCloner(M).Clone(LLVMGlobalGetValueType(Cur)), Name); 984 985 Next = LLVMGetNextGlobal(Cur); 986 if (Next == nullptr) { 987 if (Cur != End) 988 report_fatal_error(""); 989 break; 990 } 991 992 LLVMValueRef Prev = LLVMGetPreviousGlobal(Next); 993 if (Prev != Cur) 994 report_fatal_error("Next.Previous global is not Current"); 995 996 Cur = Next; 997 } 998 999 FunDecl: 1000 Begin = LLVMGetFirstFunction(Src); 1001 End = LLVMGetLastFunction(Src); 1002 if (!Begin) { 1003 if (End != nullptr) 1004 report_fatal_error("Range has an end but no beginning"); 1005 goto AliasDecl; 1006 } 1007 1008 Cur = Begin; 1009 Next = nullptr; 1010 while (true) { 1011 size_t NameLen; 1012 const char *Name = LLVMGetValueName2(Cur, &NameLen); 1013 if (LLVMGetNamedFunction(M, Name)) 1014 report_fatal_error("Function already cloned"); 1015 LLVMTypeRef Ty = TypeCloner(M).Clone(LLVMGlobalGetValueType(Cur)); 1016 1017 auto F = LLVMAddFunction(M, Name, Ty); 1018 1019 // Copy attributes 1020 for (int i = LLVMAttributeFunctionIndex, c = LLVMCountParams(F); 1021 i <= c; ++i) { 1022 for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) { 1023 if (auto SrcA = LLVMGetEnumAttributeAtIndex(Cur, i, k)) { 1024 auto Val = LLVMGetEnumAttributeValue(SrcA); 1025 auto DstA = LLVMCreateEnumAttribute(Ctx, k, Val); 1026 LLVMAddAttributeAtIndex(F, i, DstA); 1027 } 1028 } 1029 } 1030 1031 Next = LLVMGetNextFunction(Cur); 1032 if (Next == nullptr) { 1033 if (Cur != End) 1034 report_fatal_error("Last function does not match End"); 1035 break; 1036 } 1037 1038 LLVMValueRef Prev = LLVMGetPreviousFunction(Next); 1039 if (Prev != Cur) 1040 report_fatal_error("Next.Previous function is not Current"); 1041 1042 Cur = Next; 1043 } 1044 1045 AliasDecl: 1046 Begin = LLVMGetFirstGlobalAlias(Src); 1047 End = LLVMGetLastGlobalAlias(Src); 1048 if (!Begin) { 1049 if (End != nullptr) 1050 report_fatal_error("Range has an end but no beginning"); 1051 goto GlobalIFuncDecl; 1052 } 1053 1054 Cur = Begin; 1055 Next = nullptr; 1056 while (true) { 1057 size_t NameLen; 1058 const char *Name = LLVMGetValueName2(Cur, &NameLen); 1059 if (LLVMGetNamedGlobalAlias(M, Name, NameLen)) 1060 report_fatal_error("Global alias already cloned"); 1061 LLVMTypeRef PtrType = TypeCloner(M).Clone(Cur); 1062 LLVMTypeRef ValType = TypeCloner(M).Clone(LLVMGlobalGetValueType(Cur)); 1063 unsigned AddrSpace = LLVMGetPointerAddressSpace(PtrType); 1064 // FIXME: Allow NULL aliasee. 1065 LLVMAddAlias2(M, ValType, AddrSpace, LLVMGetUndef(PtrType), Name); 1066 1067 Next = LLVMGetNextGlobalAlias(Cur); 1068 if (Next == nullptr) { 1069 if (Cur != End) 1070 report_fatal_error(""); 1071 break; 1072 } 1073 1074 LLVMValueRef Prev = LLVMGetPreviousGlobalAlias(Next); 1075 if (Prev != Cur) 1076 report_fatal_error("Next.Previous global is not Current"); 1077 1078 Cur = Next; 1079 } 1080 1081 GlobalIFuncDecl: 1082 Begin = LLVMGetFirstGlobalIFunc(Src); 1083 End = LLVMGetLastGlobalIFunc(Src); 1084 if (!Begin) { 1085 if (End != nullptr) 1086 report_fatal_error("Range has an end but no beginning"); 1087 goto NamedMDDecl; 1088 } 1089 1090 Cur = Begin; 1091 Next = nullptr; 1092 while (true) { 1093 size_t NameLen; 1094 const char *Name = LLVMGetValueName2(Cur, &NameLen); 1095 if (LLVMGetNamedGlobalIFunc(M, Name, NameLen)) 1096 report_fatal_error("Global ifunc already cloned"); 1097 LLVMTypeRef CurType = TypeCloner(M).Clone(LLVMGlobalGetValueType(Cur)); 1098 // FIXME: Allow NULL resolver. 1099 LLVMAddGlobalIFunc(M, Name, NameLen, 1100 CurType, /*addressSpace*/ 0, LLVMGetUndef(CurType)); 1101 1102 Next = LLVMGetNextGlobalIFunc(Cur); 1103 if (Next == nullptr) { 1104 if (Cur != End) 1105 report_fatal_error(""); 1106 break; 1107 } 1108 1109 LLVMValueRef Prev = LLVMGetPreviousGlobalIFunc(Next); 1110 if (Prev != Cur) 1111 report_fatal_error("Next.Previous global is not Current"); 1112 1113 Cur = Next; 1114 } 1115 1116 NamedMDDecl: 1117 LLVMNamedMDNodeRef BeginMD = LLVMGetFirstNamedMetadata(Src); 1118 LLVMNamedMDNodeRef EndMD = LLVMGetLastNamedMetadata(Src); 1119 if (!BeginMD) { 1120 if (EndMD != nullptr) 1121 report_fatal_error("Range has an end but no beginning"); 1122 return; 1123 } 1124 1125 LLVMNamedMDNodeRef CurMD = BeginMD; 1126 LLVMNamedMDNodeRef NextMD = nullptr; 1127 while (true) { 1128 size_t NameLen; 1129 const char *Name = LLVMGetNamedMetadataName(CurMD, &NameLen); 1130 if (LLVMGetNamedMetadata(M, Name, NameLen)) 1131 report_fatal_error("Named Metadata Node already cloned"); 1132 LLVMGetOrInsertNamedMetadata(M, Name, NameLen); 1133 1134 NextMD = LLVMGetNextNamedMetadata(CurMD); 1135 if (NextMD == nullptr) { 1136 if (CurMD != EndMD) 1137 report_fatal_error(""); 1138 break; 1139 } 1140 1141 LLVMNamedMDNodeRef PrevMD = LLVMGetPreviousNamedMetadata(NextMD); 1142 if (PrevMD != CurMD) 1143 report_fatal_error("Next.Previous global is not Current"); 1144 1145 CurMD = NextMD; 1146 } 1147 } 1148 1149 static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) { 1150 LLVMValueRef Begin = LLVMGetFirstGlobal(Src); 1151 LLVMValueRef End = LLVMGetLastGlobal(Src); 1152 1153 LLVMValueRef Cur = Begin; 1154 LLVMValueRef Next = nullptr; 1155 if (!Begin) { 1156 if (End != nullptr) 1157 report_fatal_error("Range has an end but no beginning"); 1158 goto FunClone; 1159 } 1160 1161 while (true) { 1162 size_t NameLen; 1163 const char *Name = LLVMGetValueName2(Cur, &NameLen); 1164 LLVMValueRef G = LLVMGetNamedGlobal(M, Name); 1165 if (!G) 1166 report_fatal_error("GlobalVariable must have been declared already"); 1167 1168 if (auto I = LLVMGetInitializer(Cur)) 1169 LLVMSetInitializer(G, clone_constant(I, M)); 1170 1171 size_t NumMetadataEntries; 1172 auto *AllMetadata = LLVMGlobalCopyAllMetadata(Cur, &NumMetadataEntries); 1173 for (unsigned i = 0; i < NumMetadataEntries; ++i) { 1174 unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i); 1175 LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i); 1176 LLVMGlobalSetMetadata(G, Kind, MD); 1177 } 1178 LLVMDisposeValueMetadataEntries(AllMetadata); 1179 1180 LLVMSetGlobalConstant(G, LLVMIsGlobalConstant(Cur)); 1181 LLVMSetThreadLocal(G, LLVMIsThreadLocal(Cur)); 1182 LLVMSetExternallyInitialized(G, LLVMIsExternallyInitialized(Cur)); 1183 LLVMSetLinkage(G, LLVMGetLinkage(Cur)); 1184 LLVMSetSection(G, LLVMGetSection(Cur)); 1185 LLVMSetVisibility(G, LLVMGetVisibility(Cur)); 1186 LLVMSetUnnamedAddress(G, LLVMGetUnnamedAddress(Cur)); 1187 LLVMSetAlignment(G, LLVMGetAlignment(Cur)); 1188 1189 Next = LLVMGetNextGlobal(Cur); 1190 if (Next == nullptr) { 1191 if (Cur != End) 1192 report_fatal_error(""); 1193 break; 1194 } 1195 1196 LLVMValueRef Prev = LLVMGetPreviousGlobal(Next); 1197 if (Prev != Cur) 1198 report_fatal_error("Next.Previous global is not Current"); 1199 1200 Cur = Next; 1201 } 1202 1203 FunClone: 1204 Begin = LLVMGetFirstFunction(Src); 1205 End = LLVMGetLastFunction(Src); 1206 if (!Begin) { 1207 if (End != nullptr) 1208 report_fatal_error("Range has an end but no beginning"); 1209 goto AliasClone; 1210 } 1211 1212 Cur = Begin; 1213 Next = nullptr; 1214 while (true) { 1215 size_t NameLen; 1216 const char *Name = LLVMGetValueName2(Cur, &NameLen); 1217 LLVMValueRef Fun = LLVMGetNamedFunction(M, Name); 1218 if (!Fun) 1219 report_fatal_error("Function must have been declared already"); 1220 1221 if (LLVMHasPersonalityFn(Cur)) { 1222 size_t FNameLen; 1223 const char *FName = LLVMGetValueName2(LLVMGetPersonalityFn(Cur), 1224 &FNameLen); 1225 LLVMValueRef P = LLVMGetNamedFunction(M, FName); 1226 if (!P) 1227 report_fatal_error("Could not find personality function"); 1228 LLVMSetPersonalityFn(Fun, P); 1229 } 1230 1231 size_t NumMetadataEntries; 1232 auto *AllMetadata = LLVMGlobalCopyAllMetadata(Cur, &NumMetadataEntries); 1233 for (unsigned i = 0; i < NumMetadataEntries; ++i) { 1234 unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i); 1235 LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i); 1236 LLVMGlobalSetMetadata(Fun, Kind, MD); 1237 } 1238 LLVMDisposeValueMetadataEntries(AllMetadata); 1239 1240 FunCloner FC(Cur, Fun); 1241 FC.CloneBBs(Cur); 1242 1243 Next = LLVMGetNextFunction(Cur); 1244 if (Next == nullptr) { 1245 if (Cur != End) 1246 report_fatal_error("Last function does not match End"); 1247 break; 1248 } 1249 1250 LLVMValueRef Prev = LLVMGetPreviousFunction(Next); 1251 if (Prev != Cur) 1252 report_fatal_error("Next.Previous function is not Current"); 1253 1254 Cur = Next; 1255 } 1256 1257 AliasClone: 1258 Begin = LLVMGetFirstGlobalAlias(Src); 1259 End = LLVMGetLastGlobalAlias(Src); 1260 if (!Begin) { 1261 if (End != nullptr) 1262 report_fatal_error("Range has an end but no beginning"); 1263 goto GlobalIFuncClone; 1264 } 1265 1266 Cur = Begin; 1267 Next = nullptr; 1268 while (true) { 1269 size_t NameLen; 1270 const char *Name = LLVMGetValueName2(Cur, &NameLen); 1271 LLVMValueRef Alias = LLVMGetNamedGlobalAlias(M, Name, NameLen); 1272 if (!Alias) 1273 report_fatal_error("Global alias must have been declared already"); 1274 1275 if (LLVMValueRef Aliasee = LLVMAliasGetAliasee(Cur)) { 1276 LLVMAliasSetAliasee(Alias, clone_constant(Aliasee, M)); 1277 } 1278 1279 LLVMSetLinkage(Alias, LLVMGetLinkage(Cur)); 1280 LLVMSetUnnamedAddress(Alias, LLVMGetUnnamedAddress(Cur)); 1281 1282 Next = LLVMGetNextGlobalAlias(Cur); 1283 if (Next == nullptr) { 1284 if (Cur != End) 1285 report_fatal_error("Last global alias does not match End"); 1286 break; 1287 } 1288 1289 LLVMValueRef Prev = LLVMGetPreviousGlobalAlias(Next); 1290 if (Prev != Cur) 1291 report_fatal_error("Next.Previous global alias is not Current"); 1292 1293 Cur = Next; 1294 } 1295 1296 GlobalIFuncClone: 1297 Begin = LLVMGetFirstGlobalIFunc(Src); 1298 End = LLVMGetLastGlobalIFunc(Src); 1299 if (!Begin) { 1300 if (End != nullptr) 1301 report_fatal_error("Range has an end but no beginning"); 1302 goto NamedMDClone; 1303 } 1304 1305 Cur = Begin; 1306 Next = nullptr; 1307 while (true) { 1308 size_t NameLen; 1309 const char *Name = LLVMGetValueName2(Cur, &NameLen); 1310 LLVMValueRef IFunc = LLVMGetNamedGlobalIFunc(M, Name, NameLen); 1311 if (!IFunc) 1312 report_fatal_error("Global ifunc must have been declared already"); 1313 1314 if (LLVMValueRef Resolver = LLVMGetGlobalIFuncResolver(Cur)) { 1315 LLVMSetGlobalIFuncResolver(IFunc, clone_constant(Resolver, M)); 1316 } 1317 1318 LLVMSetLinkage(IFunc, LLVMGetLinkage(Cur)); 1319 LLVMSetUnnamedAddress(IFunc, LLVMGetUnnamedAddress(Cur)); 1320 1321 Next = LLVMGetNextGlobalIFunc(Cur); 1322 if (Next == nullptr) { 1323 if (Cur != End) 1324 report_fatal_error("Last global alias does not match End"); 1325 break; 1326 } 1327 1328 LLVMValueRef Prev = LLVMGetPreviousGlobalIFunc(Next); 1329 if (Prev != Cur) 1330 report_fatal_error("Next.Previous global alias is not Current"); 1331 1332 Cur = Next; 1333 } 1334 1335 NamedMDClone: 1336 LLVMNamedMDNodeRef BeginMD = LLVMGetFirstNamedMetadata(Src); 1337 LLVMNamedMDNodeRef EndMD = LLVMGetLastNamedMetadata(Src); 1338 if (!BeginMD) { 1339 if (EndMD != nullptr) 1340 report_fatal_error("Range has an end but no beginning"); 1341 return; 1342 } 1343 1344 LLVMNamedMDNodeRef CurMD = BeginMD; 1345 LLVMNamedMDNodeRef NextMD = nullptr; 1346 while (true) { 1347 size_t NameLen; 1348 const char *Name = LLVMGetNamedMetadataName(CurMD, &NameLen); 1349 LLVMNamedMDNodeRef NamedMD = LLVMGetNamedMetadata(M, Name, NameLen); 1350 if (!NamedMD) 1351 report_fatal_error("Named MD Node must have been declared already"); 1352 1353 unsigned OperandCount = LLVMGetNamedMetadataNumOperands(Src, Name); 1354 LLVMValueRef *OperandBuf = static_cast<LLVMValueRef *>( 1355 safe_malloc(OperandCount * sizeof(LLVMValueRef))); 1356 LLVMGetNamedMetadataOperands(Src, Name, OperandBuf); 1357 for (unsigned i = 0, e = OperandCount; i != e; ++i) { 1358 LLVMAddNamedMetadataOperand(M, Name, OperandBuf[i]); 1359 } 1360 free(OperandBuf); 1361 1362 NextMD = LLVMGetNextNamedMetadata(CurMD); 1363 if (NextMD == nullptr) { 1364 if (CurMD != EndMD) 1365 report_fatal_error("Last Named MD Node does not match End"); 1366 break; 1367 } 1368 1369 LLVMNamedMDNodeRef PrevMD = LLVMGetPreviousNamedMetadata(NextMD); 1370 if (PrevMD != CurMD) 1371 report_fatal_error("Next.Previous Named MD Node is not Current"); 1372 1373 CurMD = NextMD; 1374 } 1375 } 1376 1377 int llvm_echo(bool OpaquePointers) { 1378 LLVMEnablePrettyStackTrace(); 1379 1380 LLVMModuleRef Src = llvm_load_module(false, true); 1381 size_t SourceFileLen; 1382 const char *SourceFileName = LLVMGetSourceFileName(Src, &SourceFileLen); 1383 size_t ModuleIdentLen; 1384 const char *ModuleName = LLVMGetModuleIdentifier(Src, &ModuleIdentLen); 1385 LLVMContextRef Ctx = LLVMContextCreate(); 1386 if (!OpaquePointers) 1387 LLVMContextSetOpaquePointers(Ctx, false); 1388 LLVMModuleRef M = LLVMModuleCreateWithNameInContext(ModuleName, Ctx); 1389 1390 LLVMSetSourceFileName(M, SourceFileName, SourceFileLen); 1391 LLVMSetModuleIdentifier(M, ModuleName, ModuleIdentLen); 1392 1393 LLVMSetTarget(M, LLVMGetTarget(Src)); 1394 LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src)); 1395 if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src))) 1396 report_fatal_error("Inconsistent DataLayout string representation"); 1397 1398 size_t ModuleInlineAsmLen; 1399 const char *ModuleAsm = LLVMGetModuleInlineAsm(Src, &ModuleInlineAsmLen); 1400 LLVMSetModuleInlineAsm2(M, ModuleAsm, ModuleInlineAsmLen); 1401 1402 declare_symbols(Src, M); 1403 clone_symbols(Src, M); 1404 char *Str = LLVMPrintModuleToString(M); 1405 fputs(Str, stdout); 1406 1407 LLVMDisposeMessage(Str); 1408 LLVMDisposeModule(Src); 1409 LLVMDisposeModule(M); 1410 LLVMContextDispose(Ctx); 1411 1412 return 0; 1413 } 1414