1 //===-- Core.cpp ----------------------------------------------------------===// 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 common infrastructure (including the C bindings) 10 // for libLLVMCore.a, which implements the LLVM intermediate representation. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm-c/Core.h" 15 #include "llvm/IR/Attributes.h" 16 #include "llvm/IR/Constants.h" 17 #include "llvm/IR/DebugInfoMetadata.h" 18 #include "llvm/IR/DerivedTypes.h" 19 #include "llvm/IR/DiagnosticInfo.h" 20 #include "llvm/IR/DiagnosticPrinter.h" 21 #include "llvm/IR/GlobalAlias.h" 22 #include "llvm/IR/GlobalVariable.h" 23 #include "llvm/IR/IRBuilder.h" 24 #include "llvm/IR/InlineAsm.h" 25 #include "llvm/IR/IntrinsicInst.h" 26 #include "llvm/IR/LLVMContext.h" 27 #include "llvm/IR/LegacyPassManager.h" 28 #include "llvm/IR/Module.h" 29 #include "llvm/InitializePasses.h" 30 #include "llvm/Support/Debug.h" 31 #include "llvm/Support/ErrorHandling.h" 32 #include "llvm/Support/FileSystem.h" 33 #include "llvm/Support/ManagedStatic.h" 34 #include "llvm/Support/MemoryBuffer.h" 35 #include "llvm/Support/Threading.h" 36 #include "llvm/Support/raw_ostream.h" 37 #include <cassert> 38 #include <cstdlib> 39 #include <cstring> 40 #include <system_error> 41 42 using namespace llvm; 43 44 #define DEBUG_TYPE "ir" 45 46 void llvm::initializeCore(PassRegistry &Registry) { 47 initializeDominatorTreeWrapperPassPass(Registry); 48 initializePrintModulePassWrapperPass(Registry); 49 initializePrintFunctionPassWrapperPass(Registry); 50 initializeSafepointIRVerifierPass(Registry); 51 initializeVerifierLegacyPassPass(Registry); 52 } 53 54 void LLVMInitializeCore(LLVMPassRegistryRef R) { 55 initializeCore(*unwrap(R)); 56 } 57 58 void LLVMShutdown() { 59 llvm_shutdown(); 60 } 61 62 /*===-- Error handling ----------------------------------------------------===*/ 63 64 char *LLVMCreateMessage(const char *Message) { 65 return strdup(Message); 66 } 67 68 void LLVMDisposeMessage(char *Message) { 69 free(Message); 70 } 71 72 73 /*===-- Operations on contexts --------------------------------------------===*/ 74 75 static ManagedStatic<LLVMContext> GlobalContext; 76 77 LLVMContextRef LLVMContextCreate() { 78 return wrap(new LLVMContext()); 79 } 80 81 LLVMContextRef LLVMGetGlobalContext() { return wrap(&*GlobalContext); } 82 83 void LLVMContextSetDiagnosticHandler(LLVMContextRef C, 84 LLVMDiagnosticHandler Handler, 85 void *DiagnosticContext) { 86 unwrap(C)->setDiagnosticHandlerCallBack( 87 LLVM_EXTENSION reinterpret_cast<DiagnosticHandler::DiagnosticHandlerTy>( 88 Handler), 89 DiagnosticContext); 90 } 91 92 LLVMDiagnosticHandler LLVMContextGetDiagnosticHandler(LLVMContextRef C) { 93 return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>( 94 unwrap(C)->getDiagnosticHandlerCallBack()); 95 } 96 97 void *LLVMContextGetDiagnosticContext(LLVMContextRef C) { 98 return unwrap(C)->getDiagnosticContext(); 99 } 100 101 void LLVMContextSetYieldCallback(LLVMContextRef C, LLVMYieldCallback Callback, 102 void *OpaqueHandle) { 103 auto YieldCallback = 104 LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback); 105 unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle); 106 } 107 108 LLVMBool LLVMContextShouldDiscardValueNames(LLVMContextRef C) { 109 return unwrap(C)->shouldDiscardValueNames(); 110 } 111 112 void LLVMContextSetDiscardValueNames(LLVMContextRef C, LLVMBool Discard) { 113 unwrap(C)->setDiscardValueNames(Discard); 114 } 115 116 void LLVMContextDispose(LLVMContextRef C) { 117 delete unwrap(C); 118 } 119 120 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char *Name, 121 unsigned SLen) { 122 return unwrap(C)->getMDKindID(StringRef(Name, SLen)); 123 } 124 125 unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) { 126 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen); 127 } 128 129 unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) { 130 return Attribute::getAttrKindFromName(StringRef(Name, SLen)); 131 } 132 133 unsigned LLVMGetLastEnumAttributeKind(void) { 134 return Attribute::AttrKind::EndAttrKinds; 135 } 136 137 LLVMAttributeRef LLVMCreateEnumAttribute(LLVMContextRef C, unsigned KindID, 138 uint64_t Val) { 139 auto &Ctx = *unwrap(C); 140 auto AttrKind = (Attribute::AttrKind)KindID; 141 142 if (AttrKind == Attribute::AttrKind::ByVal) { 143 // After r362128, byval attributes need to have a type attribute. Provide a 144 // NULL one until a proper API is added for this. 145 return wrap(Attribute::getWithByValType(Ctx, NULL)); 146 } 147 148 if (AttrKind == Attribute::AttrKind::StructRet) { 149 // Same as byval. 150 return wrap(Attribute::getWithStructRetType(Ctx, NULL)); 151 } 152 153 return wrap(Attribute::get(Ctx, AttrKind, Val)); 154 } 155 156 unsigned LLVMGetEnumAttributeKind(LLVMAttributeRef A) { 157 return unwrap(A).getKindAsEnum(); 158 } 159 160 uint64_t LLVMGetEnumAttributeValue(LLVMAttributeRef A) { 161 auto Attr = unwrap(A); 162 if (Attr.isEnumAttribute()) 163 return 0; 164 return Attr.getValueAsInt(); 165 } 166 167 LLVMAttributeRef LLVMCreateStringAttribute(LLVMContextRef C, 168 const char *K, unsigned KLength, 169 const char *V, unsigned VLength) { 170 return wrap(Attribute::get(*unwrap(C), StringRef(K, KLength), 171 StringRef(V, VLength))); 172 } 173 174 const char *LLVMGetStringAttributeKind(LLVMAttributeRef A, 175 unsigned *Length) { 176 auto S = unwrap(A).getKindAsString(); 177 *Length = S.size(); 178 return S.data(); 179 } 180 181 const char *LLVMGetStringAttributeValue(LLVMAttributeRef A, 182 unsigned *Length) { 183 auto S = unwrap(A).getValueAsString(); 184 *Length = S.size(); 185 return S.data(); 186 } 187 188 LLVMBool LLVMIsEnumAttribute(LLVMAttributeRef A) { 189 auto Attr = unwrap(A); 190 return Attr.isEnumAttribute() || Attr.isIntAttribute(); 191 } 192 193 LLVMBool LLVMIsStringAttribute(LLVMAttributeRef A) { 194 return unwrap(A).isStringAttribute(); 195 } 196 197 char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) { 198 std::string MsgStorage; 199 raw_string_ostream Stream(MsgStorage); 200 DiagnosticPrinterRawOStream DP(Stream); 201 202 unwrap(DI)->print(DP); 203 Stream.flush(); 204 205 return LLVMCreateMessage(MsgStorage.c_str()); 206 } 207 208 LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI) { 209 LLVMDiagnosticSeverity severity; 210 211 switch(unwrap(DI)->getSeverity()) { 212 default: 213 severity = LLVMDSError; 214 break; 215 case DS_Warning: 216 severity = LLVMDSWarning; 217 break; 218 case DS_Remark: 219 severity = LLVMDSRemark; 220 break; 221 case DS_Note: 222 severity = LLVMDSNote; 223 break; 224 } 225 226 return severity; 227 } 228 229 /*===-- Operations on modules ---------------------------------------------===*/ 230 231 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) { 232 return wrap(new Module(ModuleID, *GlobalContext)); 233 } 234 235 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID, 236 LLVMContextRef C) { 237 return wrap(new Module(ModuleID, *unwrap(C))); 238 } 239 240 void LLVMDisposeModule(LLVMModuleRef M) { 241 delete unwrap(M); 242 } 243 244 const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) { 245 auto &Str = unwrap(M)->getModuleIdentifier(); 246 *Len = Str.length(); 247 return Str.c_str(); 248 } 249 250 void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) { 251 unwrap(M)->setModuleIdentifier(StringRef(Ident, Len)); 252 } 253 254 const char *LLVMGetSourceFileName(LLVMModuleRef M, size_t *Len) { 255 auto &Str = unwrap(M)->getSourceFileName(); 256 *Len = Str.length(); 257 return Str.c_str(); 258 } 259 260 void LLVMSetSourceFileName(LLVMModuleRef M, const char *Name, size_t Len) { 261 unwrap(M)->setSourceFileName(StringRef(Name, Len)); 262 } 263 264 /*--.. Data layout .........................................................--*/ 265 const char *LLVMGetDataLayoutStr(LLVMModuleRef M) { 266 return unwrap(M)->getDataLayoutStr().c_str(); 267 } 268 269 const char *LLVMGetDataLayout(LLVMModuleRef M) { 270 return LLVMGetDataLayoutStr(M); 271 } 272 273 void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) { 274 unwrap(M)->setDataLayout(DataLayoutStr); 275 } 276 277 /*--.. Target triple .......................................................--*/ 278 const char * LLVMGetTarget(LLVMModuleRef M) { 279 return unwrap(M)->getTargetTriple().c_str(); 280 } 281 282 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) { 283 unwrap(M)->setTargetTriple(Triple); 284 } 285 286 /*--.. Module flags ........................................................--*/ 287 struct LLVMOpaqueModuleFlagEntry { 288 LLVMModuleFlagBehavior Behavior; 289 const char *Key; 290 size_t KeyLen; 291 LLVMMetadataRef Metadata; 292 }; 293 294 static Module::ModFlagBehavior 295 map_to_llvmModFlagBehavior(LLVMModuleFlagBehavior Behavior) { 296 switch (Behavior) { 297 case LLVMModuleFlagBehaviorError: 298 return Module::ModFlagBehavior::Error; 299 case LLVMModuleFlagBehaviorWarning: 300 return Module::ModFlagBehavior::Warning; 301 case LLVMModuleFlagBehaviorRequire: 302 return Module::ModFlagBehavior::Require; 303 case LLVMModuleFlagBehaviorOverride: 304 return Module::ModFlagBehavior::Override; 305 case LLVMModuleFlagBehaviorAppend: 306 return Module::ModFlagBehavior::Append; 307 case LLVMModuleFlagBehaviorAppendUnique: 308 return Module::ModFlagBehavior::AppendUnique; 309 } 310 llvm_unreachable("Unknown LLVMModuleFlagBehavior"); 311 } 312 313 static LLVMModuleFlagBehavior 314 map_from_llvmModFlagBehavior(Module::ModFlagBehavior Behavior) { 315 switch (Behavior) { 316 case Module::ModFlagBehavior::Error: 317 return LLVMModuleFlagBehaviorError; 318 case Module::ModFlagBehavior::Warning: 319 return LLVMModuleFlagBehaviorWarning; 320 case Module::ModFlagBehavior::Require: 321 return LLVMModuleFlagBehaviorRequire; 322 case Module::ModFlagBehavior::Override: 323 return LLVMModuleFlagBehaviorOverride; 324 case Module::ModFlagBehavior::Append: 325 return LLVMModuleFlagBehaviorAppend; 326 case Module::ModFlagBehavior::AppendUnique: 327 return LLVMModuleFlagBehaviorAppendUnique; 328 default: 329 llvm_unreachable("Unhandled Flag Behavior"); 330 } 331 } 332 333 LLVMModuleFlagEntry *LLVMCopyModuleFlagsMetadata(LLVMModuleRef M, size_t *Len) { 334 SmallVector<Module::ModuleFlagEntry, 8> MFEs; 335 unwrap(M)->getModuleFlagsMetadata(MFEs); 336 337 LLVMOpaqueModuleFlagEntry *Result = static_cast<LLVMOpaqueModuleFlagEntry *>( 338 safe_malloc(MFEs.size() * sizeof(LLVMOpaqueModuleFlagEntry))); 339 for (unsigned i = 0; i < MFEs.size(); ++i) { 340 const auto &ModuleFlag = MFEs[i]; 341 Result[i].Behavior = map_from_llvmModFlagBehavior(ModuleFlag.Behavior); 342 Result[i].Key = ModuleFlag.Key->getString().data(); 343 Result[i].KeyLen = ModuleFlag.Key->getString().size(); 344 Result[i].Metadata = wrap(ModuleFlag.Val); 345 } 346 *Len = MFEs.size(); 347 return Result; 348 } 349 350 void LLVMDisposeModuleFlagsMetadata(LLVMModuleFlagEntry *Entries) { 351 free(Entries); 352 } 353 354 LLVMModuleFlagBehavior 355 LLVMModuleFlagEntriesGetFlagBehavior(LLVMModuleFlagEntry *Entries, 356 unsigned Index) { 357 LLVMOpaqueModuleFlagEntry MFE = 358 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]); 359 return MFE.Behavior; 360 } 361 362 const char *LLVMModuleFlagEntriesGetKey(LLVMModuleFlagEntry *Entries, 363 unsigned Index, size_t *Len) { 364 LLVMOpaqueModuleFlagEntry MFE = 365 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]); 366 *Len = MFE.KeyLen; 367 return MFE.Key; 368 } 369 370 LLVMMetadataRef LLVMModuleFlagEntriesGetMetadata(LLVMModuleFlagEntry *Entries, 371 unsigned Index) { 372 LLVMOpaqueModuleFlagEntry MFE = 373 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]); 374 return MFE.Metadata; 375 } 376 377 LLVMMetadataRef LLVMGetModuleFlag(LLVMModuleRef M, 378 const char *Key, size_t KeyLen) { 379 return wrap(unwrap(M)->getModuleFlag({Key, KeyLen})); 380 } 381 382 void LLVMAddModuleFlag(LLVMModuleRef M, LLVMModuleFlagBehavior Behavior, 383 const char *Key, size_t KeyLen, 384 LLVMMetadataRef Val) { 385 unwrap(M)->addModuleFlag(map_to_llvmModFlagBehavior(Behavior), 386 {Key, KeyLen}, unwrap(Val)); 387 } 388 389 /*--.. Printing modules ....................................................--*/ 390 391 void LLVMDumpModule(LLVMModuleRef M) { 392 unwrap(M)->print(errs(), nullptr, 393 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true); 394 } 395 396 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename, 397 char **ErrorMessage) { 398 std::error_code EC; 399 raw_fd_ostream dest(Filename, EC, sys::fs::OF_Text); 400 if (EC) { 401 *ErrorMessage = strdup(EC.message().c_str()); 402 return true; 403 } 404 405 unwrap(M)->print(dest, nullptr); 406 407 dest.close(); 408 409 if (dest.has_error()) { 410 std::string E = "Error printing to file: " + dest.error().message(); 411 *ErrorMessage = strdup(E.c_str()); 412 return true; 413 } 414 415 return false; 416 } 417 418 char *LLVMPrintModuleToString(LLVMModuleRef M) { 419 std::string buf; 420 raw_string_ostream os(buf); 421 422 unwrap(M)->print(os, nullptr); 423 os.flush(); 424 425 return strdup(buf.c_str()); 426 } 427 428 /*--.. Operations on inline assembler ......................................--*/ 429 void LLVMSetModuleInlineAsm2(LLVMModuleRef M, const char *Asm, size_t Len) { 430 unwrap(M)->setModuleInlineAsm(StringRef(Asm, Len)); 431 } 432 433 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) { 434 unwrap(M)->setModuleInlineAsm(StringRef(Asm)); 435 } 436 437 void LLVMAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm, size_t Len) { 438 unwrap(M)->appendModuleInlineAsm(StringRef(Asm, Len)); 439 } 440 441 const char *LLVMGetModuleInlineAsm(LLVMModuleRef M, size_t *Len) { 442 auto &Str = unwrap(M)->getModuleInlineAsm(); 443 *Len = Str.length(); 444 return Str.c_str(); 445 } 446 447 LLVMValueRef LLVMGetInlineAsm(LLVMTypeRef Ty, 448 char *AsmString, size_t AsmStringSize, 449 char *Constraints, size_t ConstraintsSize, 450 LLVMBool HasSideEffects, LLVMBool IsAlignStack, 451 LLVMInlineAsmDialect Dialect) { 452 InlineAsm::AsmDialect AD; 453 switch (Dialect) { 454 case LLVMInlineAsmDialectATT: 455 AD = InlineAsm::AD_ATT; 456 break; 457 case LLVMInlineAsmDialectIntel: 458 AD = InlineAsm::AD_Intel; 459 break; 460 } 461 return wrap(InlineAsm::get(unwrap<FunctionType>(Ty), 462 StringRef(AsmString, AsmStringSize), 463 StringRef(Constraints, ConstraintsSize), 464 HasSideEffects, IsAlignStack, AD)); 465 } 466 467 468 /*--.. Operations on module contexts ......................................--*/ 469 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) { 470 return wrap(&unwrap(M)->getContext()); 471 } 472 473 474 /*===-- Operations on types -----------------------------------------------===*/ 475 476 /*--.. Operations on all types (mostly) ....................................--*/ 477 478 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) { 479 switch (unwrap(Ty)->getTypeID()) { 480 case Type::VoidTyID: 481 return LLVMVoidTypeKind; 482 case Type::HalfTyID: 483 return LLVMHalfTypeKind; 484 case Type::BFloatTyID: 485 return LLVMBFloatTypeKind; 486 case Type::FloatTyID: 487 return LLVMFloatTypeKind; 488 case Type::DoubleTyID: 489 return LLVMDoubleTypeKind; 490 case Type::X86_FP80TyID: 491 return LLVMX86_FP80TypeKind; 492 case Type::FP128TyID: 493 return LLVMFP128TypeKind; 494 case Type::PPC_FP128TyID: 495 return LLVMPPC_FP128TypeKind; 496 case Type::LabelTyID: 497 return LLVMLabelTypeKind; 498 case Type::MetadataTyID: 499 return LLVMMetadataTypeKind; 500 case Type::IntegerTyID: 501 return LLVMIntegerTypeKind; 502 case Type::FunctionTyID: 503 return LLVMFunctionTypeKind; 504 case Type::StructTyID: 505 return LLVMStructTypeKind; 506 case Type::ArrayTyID: 507 return LLVMArrayTypeKind; 508 case Type::PointerTyID: 509 return LLVMPointerTypeKind; 510 case Type::FixedVectorTyID: 511 return LLVMVectorTypeKind; 512 case Type::X86_MMXTyID: 513 return LLVMX86_MMXTypeKind; 514 case Type::X86_AMXTyID: 515 return LLVMX86_AMXTypeKind; 516 case Type::TokenTyID: 517 return LLVMTokenTypeKind; 518 case Type::ScalableVectorTyID: 519 return LLVMScalableVectorTypeKind; 520 } 521 llvm_unreachable("Unhandled TypeID."); 522 } 523 524 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty) 525 { 526 return unwrap(Ty)->isSized(); 527 } 528 529 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) { 530 return wrap(&unwrap(Ty)->getContext()); 531 } 532 533 void LLVMDumpType(LLVMTypeRef Ty) { 534 return unwrap(Ty)->print(errs(), /*IsForDebug=*/true); 535 } 536 537 char *LLVMPrintTypeToString(LLVMTypeRef Ty) { 538 std::string buf; 539 raw_string_ostream os(buf); 540 541 if (unwrap(Ty)) 542 unwrap(Ty)->print(os); 543 else 544 os << "Printing <null> Type"; 545 546 os.flush(); 547 548 return strdup(buf.c_str()); 549 } 550 551 /*--.. Operations on integer types .........................................--*/ 552 553 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) { 554 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C)); 555 } 556 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) { 557 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C)); 558 } 559 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) { 560 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C)); 561 } 562 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) { 563 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C)); 564 } 565 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) { 566 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C)); 567 } 568 LLVMTypeRef LLVMInt128TypeInContext(LLVMContextRef C) { 569 return (LLVMTypeRef) Type::getInt128Ty(*unwrap(C)); 570 } 571 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) { 572 return wrap(IntegerType::get(*unwrap(C), NumBits)); 573 } 574 575 LLVMTypeRef LLVMInt1Type(void) { 576 return LLVMInt1TypeInContext(LLVMGetGlobalContext()); 577 } 578 LLVMTypeRef LLVMInt8Type(void) { 579 return LLVMInt8TypeInContext(LLVMGetGlobalContext()); 580 } 581 LLVMTypeRef LLVMInt16Type(void) { 582 return LLVMInt16TypeInContext(LLVMGetGlobalContext()); 583 } 584 LLVMTypeRef LLVMInt32Type(void) { 585 return LLVMInt32TypeInContext(LLVMGetGlobalContext()); 586 } 587 LLVMTypeRef LLVMInt64Type(void) { 588 return LLVMInt64TypeInContext(LLVMGetGlobalContext()); 589 } 590 LLVMTypeRef LLVMInt128Type(void) { 591 return LLVMInt128TypeInContext(LLVMGetGlobalContext()); 592 } 593 LLVMTypeRef LLVMIntType(unsigned NumBits) { 594 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits); 595 } 596 597 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) { 598 return unwrap<IntegerType>(IntegerTy)->getBitWidth(); 599 } 600 601 /*--.. Operations on real types ............................................--*/ 602 603 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) { 604 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C)); 605 } 606 LLVMTypeRef LLVMBFloatTypeInContext(LLVMContextRef C) { 607 return (LLVMTypeRef) Type::getBFloatTy(*unwrap(C)); 608 } 609 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) { 610 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C)); 611 } 612 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) { 613 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C)); 614 } 615 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) { 616 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C)); 617 } 618 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) { 619 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C)); 620 } 621 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) { 622 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C)); 623 } 624 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) { 625 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C)); 626 } 627 LLVMTypeRef LLVMX86AMXTypeInContext(LLVMContextRef C) { 628 return (LLVMTypeRef) Type::getX86_AMXTy(*unwrap(C)); 629 } 630 631 LLVMTypeRef LLVMHalfType(void) { 632 return LLVMHalfTypeInContext(LLVMGetGlobalContext()); 633 } 634 LLVMTypeRef LLVMBFloatType(void) { 635 return LLVMBFloatTypeInContext(LLVMGetGlobalContext()); 636 } 637 LLVMTypeRef LLVMFloatType(void) { 638 return LLVMFloatTypeInContext(LLVMGetGlobalContext()); 639 } 640 LLVMTypeRef LLVMDoubleType(void) { 641 return LLVMDoubleTypeInContext(LLVMGetGlobalContext()); 642 } 643 LLVMTypeRef LLVMX86FP80Type(void) { 644 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext()); 645 } 646 LLVMTypeRef LLVMFP128Type(void) { 647 return LLVMFP128TypeInContext(LLVMGetGlobalContext()); 648 } 649 LLVMTypeRef LLVMPPCFP128Type(void) { 650 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext()); 651 } 652 LLVMTypeRef LLVMX86MMXType(void) { 653 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext()); 654 } 655 LLVMTypeRef LLVMX86AMXType(void) { 656 return LLVMX86AMXTypeInContext(LLVMGetGlobalContext()); 657 } 658 659 /*--.. Operations on function types ........................................--*/ 660 661 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType, 662 LLVMTypeRef *ParamTypes, unsigned ParamCount, 663 LLVMBool IsVarArg) { 664 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount); 665 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0)); 666 } 667 668 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) { 669 return unwrap<FunctionType>(FunctionTy)->isVarArg(); 670 } 671 672 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) { 673 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType()); 674 } 675 676 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) { 677 return unwrap<FunctionType>(FunctionTy)->getNumParams(); 678 } 679 680 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) { 681 FunctionType *Ty = unwrap<FunctionType>(FunctionTy); 682 for (Type *T : Ty->params()) 683 *Dest++ = wrap(T); 684 } 685 686 /*--.. Operations on struct types ..........................................--*/ 687 688 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes, 689 unsigned ElementCount, LLVMBool Packed) { 690 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount); 691 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0)); 692 } 693 694 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes, 695 unsigned ElementCount, LLVMBool Packed) { 696 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes, 697 ElementCount, Packed); 698 } 699 700 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name) 701 { 702 return wrap(StructType::create(*unwrap(C), Name)); 703 } 704 705 const char *LLVMGetStructName(LLVMTypeRef Ty) 706 { 707 StructType *Type = unwrap<StructType>(Ty); 708 if (!Type->hasName()) 709 return nullptr; 710 return Type->getName().data(); 711 } 712 713 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes, 714 unsigned ElementCount, LLVMBool Packed) { 715 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount); 716 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0); 717 } 718 719 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) { 720 return unwrap<StructType>(StructTy)->getNumElements(); 721 } 722 723 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) { 724 StructType *Ty = unwrap<StructType>(StructTy); 725 for (Type *T : Ty->elements()) 726 *Dest++ = wrap(T); 727 } 728 729 LLVMTypeRef LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy, unsigned i) { 730 StructType *Ty = unwrap<StructType>(StructTy); 731 return wrap(Ty->getTypeAtIndex(i)); 732 } 733 734 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) { 735 return unwrap<StructType>(StructTy)->isPacked(); 736 } 737 738 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) { 739 return unwrap<StructType>(StructTy)->isOpaque(); 740 } 741 742 LLVMBool LLVMIsLiteralStruct(LLVMTypeRef StructTy) { 743 return unwrap<StructType>(StructTy)->isLiteral(); 744 } 745 746 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) { 747 return wrap(StructType::getTypeByName(unwrap(M)->getContext(), Name)); 748 } 749 750 LLVMTypeRef LLVMGetTypeByName2(LLVMContextRef C, const char *Name) { 751 return wrap(StructType::getTypeByName(*unwrap(C), Name)); 752 } 753 754 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/ 755 756 void LLVMGetSubtypes(LLVMTypeRef Tp, LLVMTypeRef *Arr) { 757 int i = 0; 758 for (auto *T : unwrap(Tp)->subtypes()) { 759 Arr[i] = wrap(T); 760 i++; 761 } 762 } 763 764 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) { 765 return wrap(ArrayType::get(unwrap(ElementType), ElementCount)); 766 } 767 768 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) { 769 return wrap(PointerType::get(unwrap(ElementType), AddressSpace)); 770 } 771 772 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) { 773 return wrap(FixedVectorType::get(unwrap(ElementType), ElementCount)); 774 } 775 776 LLVMTypeRef LLVMScalableVectorType(LLVMTypeRef ElementType, 777 unsigned ElementCount) { 778 return wrap(ScalableVectorType::get(unwrap(ElementType), ElementCount)); 779 } 780 781 LLVMTypeRef LLVMGetElementType(LLVMTypeRef WrappedTy) { 782 auto *Ty = unwrap<Type>(WrappedTy); 783 if (auto *PTy = dyn_cast<PointerType>(Ty)) 784 return wrap(PTy->getElementType()); 785 if (auto *ATy = dyn_cast<ArrayType>(Ty)) 786 return wrap(ATy->getElementType()); 787 return wrap(cast<VectorType>(Ty)->getElementType()); 788 } 789 790 unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp) { 791 return unwrap(Tp)->getNumContainedTypes(); 792 } 793 794 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) { 795 return unwrap<ArrayType>(ArrayTy)->getNumElements(); 796 } 797 798 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) { 799 return unwrap<PointerType>(PointerTy)->getAddressSpace(); 800 } 801 802 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) { 803 return unwrap<VectorType>(VectorTy)->getElementCount().getKnownMinValue(); 804 } 805 806 /*--.. Operations on other types ...........................................--*/ 807 808 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) { 809 return wrap(Type::getVoidTy(*unwrap(C))); 810 } 811 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) { 812 return wrap(Type::getLabelTy(*unwrap(C))); 813 } 814 LLVMTypeRef LLVMTokenTypeInContext(LLVMContextRef C) { 815 return wrap(Type::getTokenTy(*unwrap(C))); 816 } 817 LLVMTypeRef LLVMMetadataTypeInContext(LLVMContextRef C) { 818 return wrap(Type::getMetadataTy(*unwrap(C))); 819 } 820 821 LLVMTypeRef LLVMVoidType(void) { 822 return LLVMVoidTypeInContext(LLVMGetGlobalContext()); 823 } 824 LLVMTypeRef LLVMLabelType(void) { 825 return LLVMLabelTypeInContext(LLVMGetGlobalContext()); 826 } 827 828 /*===-- Operations on values ----------------------------------------------===*/ 829 830 /*--.. Operations on all values ............................................--*/ 831 832 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) { 833 return wrap(unwrap(Val)->getType()); 834 } 835 836 LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) { 837 switch(unwrap(Val)->getValueID()) { 838 #define LLVM_C_API 1 839 #define HANDLE_VALUE(Name) \ 840 case Value::Name##Val: \ 841 return LLVM##Name##ValueKind; 842 #include "llvm/IR/Value.def" 843 default: 844 return LLVMInstructionValueKind; 845 } 846 } 847 848 const char *LLVMGetValueName2(LLVMValueRef Val, size_t *Length) { 849 auto *V = unwrap(Val); 850 *Length = V->getName().size(); 851 return V->getName().data(); 852 } 853 854 void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen) { 855 unwrap(Val)->setName(StringRef(Name, NameLen)); 856 } 857 858 const char *LLVMGetValueName(LLVMValueRef Val) { 859 return unwrap(Val)->getName().data(); 860 } 861 862 void LLVMSetValueName(LLVMValueRef Val, const char *Name) { 863 unwrap(Val)->setName(Name); 864 } 865 866 void LLVMDumpValue(LLVMValueRef Val) { 867 unwrap(Val)->print(errs(), /*IsForDebug=*/true); 868 } 869 870 char* LLVMPrintValueToString(LLVMValueRef Val) { 871 std::string buf; 872 raw_string_ostream os(buf); 873 874 if (unwrap(Val)) 875 unwrap(Val)->print(os); 876 else 877 os << "Printing <null> Value"; 878 879 os.flush(); 880 881 return strdup(buf.c_str()); 882 } 883 884 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) { 885 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal)); 886 } 887 888 int LLVMHasMetadata(LLVMValueRef Inst) { 889 return unwrap<Instruction>(Inst)->hasMetadata(); 890 } 891 892 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) { 893 auto *I = unwrap<Instruction>(Inst); 894 assert(I && "Expected instruction"); 895 if (auto *MD = I->getMetadata(KindID)) 896 return wrap(MetadataAsValue::get(I->getContext(), MD)); 897 return nullptr; 898 } 899 900 // MetadataAsValue uses a canonical format which strips the actual MDNode for 901 // MDNode with just a single constant value, storing just a ConstantAsMetadata 902 // This undoes this canonicalization, reconstructing the MDNode. 903 static MDNode *extractMDNode(MetadataAsValue *MAV) { 904 Metadata *MD = MAV->getMetadata(); 905 assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) && 906 "Expected a metadata node or a canonicalized constant"); 907 908 if (MDNode *N = dyn_cast<MDNode>(MD)) 909 return N; 910 911 return MDNode::get(MAV->getContext(), MD); 912 } 913 914 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) { 915 MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr; 916 917 unwrap<Instruction>(Inst)->setMetadata(KindID, N); 918 } 919 920 struct LLVMOpaqueValueMetadataEntry { 921 unsigned Kind; 922 LLVMMetadataRef Metadata; 923 }; 924 925 using MetadataEntries = SmallVectorImpl<std::pair<unsigned, MDNode *>>; 926 static LLVMValueMetadataEntry * 927 llvm_getMetadata(size_t *NumEntries, 928 llvm::function_ref<void(MetadataEntries &)> AccessMD) { 929 SmallVector<std::pair<unsigned, MDNode *>, 8> MVEs; 930 AccessMD(MVEs); 931 932 LLVMOpaqueValueMetadataEntry *Result = 933 static_cast<LLVMOpaqueValueMetadataEntry *>( 934 safe_malloc(MVEs.size() * sizeof(LLVMOpaqueValueMetadataEntry))); 935 for (unsigned i = 0; i < MVEs.size(); ++i) { 936 const auto &ModuleFlag = MVEs[i]; 937 Result[i].Kind = ModuleFlag.first; 938 Result[i].Metadata = wrap(ModuleFlag.second); 939 } 940 *NumEntries = MVEs.size(); 941 return Result; 942 } 943 944 LLVMValueMetadataEntry * 945 LLVMInstructionGetAllMetadataOtherThanDebugLoc(LLVMValueRef Value, 946 size_t *NumEntries) { 947 return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) { 948 Entries.clear(); 949 unwrap<Instruction>(Value)->getAllMetadata(Entries); 950 }); 951 } 952 953 /*--.. Conversion functions ................................................--*/ 954 955 #define LLVM_DEFINE_VALUE_CAST(name) \ 956 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \ 957 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \ 958 } 959 960 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST) 961 962 LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) { 963 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val))) 964 if (isa<MDNode>(MD->getMetadata()) || 965 isa<ValueAsMetadata>(MD->getMetadata())) 966 return Val; 967 return nullptr; 968 } 969 970 LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) { 971 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val))) 972 if (isa<MDString>(MD->getMetadata())) 973 return Val; 974 return nullptr; 975 } 976 977 /*--.. Operations on Uses ..................................................--*/ 978 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) { 979 Value *V = unwrap(Val); 980 Value::use_iterator I = V->use_begin(); 981 if (I == V->use_end()) 982 return nullptr; 983 return wrap(&*I); 984 } 985 986 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) { 987 Use *Next = unwrap(U)->getNext(); 988 if (Next) 989 return wrap(Next); 990 return nullptr; 991 } 992 993 LLVMValueRef LLVMGetUser(LLVMUseRef U) { 994 return wrap(unwrap(U)->getUser()); 995 } 996 997 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) { 998 return wrap(unwrap(U)->get()); 999 } 1000 1001 /*--.. Operations on Users .................................................--*/ 1002 1003 static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N, 1004 unsigned Index) { 1005 Metadata *Op = N->getOperand(Index); 1006 if (!Op) 1007 return nullptr; 1008 if (auto *C = dyn_cast<ConstantAsMetadata>(Op)) 1009 return wrap(C->getValue()); 1010 return wrap(MetadataAsValue::get(Context, Op)); 1011 } 1012 1013 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) { 1014 Value *V = unwrap(Val); 1015 if (auto *MD = dyn_cast<MetadataAsValue>(V)) { 1016 if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) { 1017 assert(Index == 0 && "Function-local metadata can only have one operand"); 1018 return wrap(L->getValue()); 1019 } 1020 return getMDNodeOperandImpl(V->getContext(), 1021 cast<MDNode>(MD->getMetadata()), Index); 1022 } 1023 1024 return wrap(cast<User>(V)->getOperand(Index)); 1025 } 1026 1027 LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) { 1028 Value *V = unwrap(Val); 1029 return wrap(&cast<User>(V)->getOperandUse(Index)); 1030 } 1031 1032 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) { 1033 unwrap<User>(Val)->setOperand(Index, unwrap(Op)); 1034 } 1035 1036 int LLVMGetNumOperands(LLVMValueRef Val) { 1037 Value *V = unwrap(Val); 1038 if (isa<MetadataAsValue>(V)) 1039 return LLVMGetMDNodeNumOperands(Val); 1040 1041 return cast<User>(V)->getNumOperands(); 1042 } 1043 1044 /*--.. Operations on constants of any type .................................--*/ 1045 1046 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) { 1047 return wrap(Constant::getNullValue(unwrap(Ty))); 1048 } 1049 1050 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) { 1051 return wrap(Constant::getAllOnesValue(unwrap(Ty))); 1052 } 1053 1054 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) { 1055 return wrap(UndefValue::get(unwrap(Ty))); 1056 } 1057 1058 LLVMValueRef LLVMGetPoison(LLVMTypeRef Ty) { 1059 return wrap(PoisonValue::get(unwrap(Ty))); 1060 } 1061 1062 LLVMBool LLVMIsConstant(LLVMValueRef Ty) { 1063 return isa<Constant>(unwrap(Ty)); 1064 } 1065 1066 LLVMBool LLVMIsNull(LLVMValueRef Val) { 1067 if (Constant *C = dyn_cast<Constant>(unwrap(Val))) 1068 return C->isNullValue(); 1069 return false; 1070 } 1071 1072 LLVMBool LLVMIsUndef(LLVMValueRef Val) { 1073 return isa<UndefValue>(unwrap(Val)); 1074 } 1075 1076 LLVMBool LLVMIsPoison(LLVMValueRef Val) { 1077 return isa<PoisonValue>(unwrap(Val)); 1078 } 1079 1080 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) { 1081 return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty))); 1082 } 1083 1084 /*--.. Operations on metadata nodes ........................................--*/ 1085 1086 LLVMMetadataRef LLVMMDStringInContext2(LLVMContextRef C, const char *Str, 1087 size_t SLen) { 1088 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen))); 1089 } 1090 1091 LLVMMetadataRef LLVMMDNodeInContext2(LLVMContextRef C, LLVMMetadataRef *MDs, 1092 size_t Count) { 1093 return wrap(MDNode::get(*unwrap(C), ArrayRef<Metadata*>(unwrap(MDs), Count))); 1094 } 1095 1096 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str, 1097 unsigned SLen) { 1098 LLVMContext &Context = *unwrap(C); 1099 return wrap(MetadataAsValue::get( 1100 Context, MDString::get(Context, StringRef(Str, SLen)))); 1101 } 1102 1103 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) { 1104 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen); 1105 } 1106 1107 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals, 1108 unsigned Count) { 1109 LLVMContext &Context = *unwrap(C); 1110 SmallVector<Metadata *, 8> MDs; 1111 for (auto *OV : makeArrayRef(Vals, Count)) { 1112 Value *V = unwrap(OV); 1113 Metadata *MD; 1114 if (!V) 1115 MD = nullptr; 1116 else if (auto *C = dyn_cast<Constant>(V)) 1117 MD = ConstantAsMetadata::get(C); 1118 else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) { 1119 MD = MDV->getMetadata(); 1120 assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata " 1121 "outside of direct argument to call"); 1122 } else { 1123 // This is function-local metadata. Pretend to make an MDNode. 1124 assert(Count == 1 && 1125 "Expected only one operand to function-local metadata"); 1126 return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V))); 1127 } 1128 1129 MDs.push_back(MD); 1130 } 1131 return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs))); 1132 } 1133 1134 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) { 1135 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count); 1136 } 1137 1138 LLVMValueRef LLVMMetadataAsValue(LLVMContextRef C, LLVMMetadataRef MD) { 1139 return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD))); 1140 } 1141 1142 LLVMMetadataRef LLVMValueAsMetadata(LLVMValueRef Val) { 1143 auto *V = unwrap(Val); 1144 if (auto *C = dyn_cast<Constant>(V)) 1145 return wrap(ConstantAsMetadata::get(C)); 1146 if (auto *MAV = dyn_cast<MetadataAsValue>(V)) 1147 return wrap(MAV->getMetadata()); 1148 return wrap(ValueAsMetadata::get(V)); 1149 } 1150 1151 const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) { 1152 if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V))) 1153 if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) { 1154 *Length = S->getString().size(); 1155 return S->getString().data(); 1156 } 1157 *Length = 0; 1158 return nullptr; 1159 } 1160 1161 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) { 1162 auto *MD = cast<MetadataAsValue>(unwrap(V)); 1163 if (isa<ValueAsMetadata>(MD->getMetadata())) 1164 return 1; 1165 return cast<MDNode>(MD->getMetadata())->getNumOperands(); 1166 } 1167 1168 LLVMNamedMDNodeRef LLVMGetFirstNamedMetadata(LLVMModuleRef M) { 1169 Module *Mod = unwrap(M); 1170 Module::named_metadata_iterator I = Mod->named_metadata_begin(); 1171 if (I == Mod->named_metadata_end()) 1172 return nullptr; 1173 return wrap(&*I); 1174 } 1175 1176 LLVMNamedMDNodeRef LLVMGetLastNamedMetadata(LLVMModuleRef M) { 1177 Module *Mod = unwrap(M); 1178 Module::named_metadata_iterator I = Mod->named_metadata_end(); 1179 if (I == Mod->named_metadata_begin()) 1180 return nullptr; 1181 return wrap(&*--I); 1182 } 1183 1184 LLVMNamedMDNodeRef LLVMGetNextNamedMetadata(LLVMNamedMDNodeRef NMD) { 1185 NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD); 1186 Module::named_metadata_iterator I(NamedNode); 1187 if (++I == NamedNode->getParent()->named_metadata_end()) 1188 return nullptr; 1189 return wrap(&*I); 1190 } 1191 1192 LLVMNamedMDNodeRef LLVMGetPreviousNamedMetadata(LLVMNamedMDNodeRef NMD) { 1193 NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD); 1194 Module::named_metadata_iterator I(NamedNode); 1195 if (I == NamedNode->getParent()->named_metadata_begin()) 1196 return nullptr; 1197 return wrap(&*--I); 1198 } 1199 1200 LLVMNamedMDNodeRef LLVMGetNamedMetadata(LLVMModuleRef M, 1201 const char *Name, size_t NameLen) { 1202 return wrap(unwrap(M)->getNamedMetadata(StringRef(Name, NameLen))); 1203 } 1204 1205 LLVMNamedMDNodeRef LLVMGetOrInsertNamedMetadata(LLVMModuleRef M, 1206 const char *Name, size_t NameLen) { 1207 return wrap(unwrap(M)->getOrInsertNamedMetadata({Name, NameLen})); 1208 } 1209 1210 const char *LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NMD, size_t *NameLen) { 1211 NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD); 1212 *NameLen = NamedNode->getName().size(); 1213 return NamedNode->getName().data(); 1214 } 1215 1216 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) { 1217 auto *MD = cast<MetadataAsValue>(unwrap(V)); 1218 if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) { 1219 *Dest = wrap(MDV->getValue()); 1220 return; 1221 } 1222 const auto *N = cast<MDNode>(MD->getMetadata()); 1223 const unsigned numOperands = N->getNumOperands(); 1224 LLVMContext &Context = unwrap(V)->getContext(); 1225 for (unsigned i = 0; i < numOperands; i++) 1226 Dest[i] = getMDNodeOperandImpl(Context, N, i); 1227 } 1228 1229 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char *Name) { 1230 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) { 1231 return N->getNumOperands(); 1232 } 1233 return 0; 1234 } 1235 1236 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char *Name, 1237 LLVMValueRef *Dest) { 1238 NamedMDNode *N = unwrap(M)->getNamedMetadata(Name); 1239 if (!N) 1240 return; 1241 LLVMContext &Context = unwrap(M)->getContext(); 1242 for (unsigned i=0;i<N->getNumOperands();i++) 1243 Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i))); 1244 } 1245 1246 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char *Name, 1247 LLVMValueRef Val) { 1248 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name); 1249 if (!N) 1250 return; 1251 if (!Val) 1252 return; 1253 N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val))); 1254 } 1255 1256 const char *LLVMGetDebugLocDirectory(LLVMValueRef Val, unsigned *Length) { 1257 if (!Length) return nullptr; 1258 StringRef S; 1259 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) { 1260 if (const auto &DL = I->getDebugLoc()) { 1261 S = DL->getDirectory(); 1262 } 1263 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) { 1264 SmallVector<DIGlobalVariableExpression *, 1> GVEs; 1265 GV->getDebugInfo(GVEs); 1266 if (GVEs.size()) 1267 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable()) 1268 S = DGV->getDirectory(); 1269 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) { 1270 if (const DISubprogram *DSP = F->getSubprogram()) 1271 S = DSP->getDirectory(); 1272 } else { 1273 assert(0 && "Expected Instruction, GlobalVariable or Function"); 1274 return nullptr; 1275 } 1276 *Length = S.size(); 1277 return S.data(); 1278 } 1279 1280 const char *LLVMGetDebugLocFilename(LLVMValueRef Val, unsigned *Length) { 1281 if (!Length) return nullptr; 1282 StringRef S; 1283 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) { 1284 if (const auto &DL = I->getDebugLoc()) { 1285 S = DL->getFilename(); 1286 } 1287 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) { 1288 SmallVector<DIGlobalVariableExpression *, 1> GVEs; 1289 GV->getDebugInfo(GVEs); 1290 if (GVEs.size()) 1291 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable()) 1292 S = DGV->getFilename(); 1293 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) { 1294 if (const DISubprogram *DSP = F->getSubprogram()) 1295 S = DSP->getFilename(); 1296 } else { 1297 assert(0 && "Expected Instruction, GlobalVariable or Function"); 1298 return nullptr; 1299 } 1300 *Length = S.size(); 1301 return S.data(); 1302 } 1303 1304 unsigned LLVMGetDebugLocLine(LLVMValueRef Val) { 1305 unsigned L = 0; 1306 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) { 1307 if (const auto &DL = I->getDebugLoc()) { 1308 L = DL->getLine(); 1309 } 1310 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) { 1311 SmallVector<DIGlobalVariableExpression *, 1> GVEs; 1312 GV->getDebugInfo(GVEs); 1313 if (GVEs.size()) 1314 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable()) 1315 L = DGV->getLine(); 1316 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) { 1317 if (const DISubprogram *DSP = F->getSubprogram()) 1318 L = DSP->getLine(); 1319 } else { 1320 assert(0 && "Expected Instruction, GlobalVariable or Function"); 1321 return -1; 1322 } 1323 return L; 1324 } 1325 1326 unsigned LLVMGetDebugLocColumn(LLVMValueRef Val) { 1327 unsigned C = 0; 1328 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) 1329 if (const auto &DL = I->getDebugLoc()) 1330 C = DL->getColumn(); 1331 return C; 1332 } 1333 1334 /*--.. Operations on scalar constants ......................................--*/ 1335 1336 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N, 1337 LLVMBool SignExtend) { 1338 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0)); 1339 } 1340 1341 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy, 1342 unsigned NumWords, 1343 const uint64_t Words[]) { 1344 IntegerType *Ty = unwrap<IntegerType>(IntTy); 1345 return wrap(ConstantInt::get(Ty->getContext(), 1346 APInt(Ty->getBitWidth(), 1347 makeArrayRef(Words, NumWords)))); 1348 } 1349 1350 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[], 1351 uint8_t Radix) { 1352 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str), 1353 Radix)); 1354 } 1355 1356 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[], 1357 unsigned SLen, uint8_t Radix) { 1358 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen), 1359 Radix)); 1360 } 1361 1362 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) { 1363 return wrap(ConstantFP::get(unwrap(RealTy), N)); 1364 } 1365 1366 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) { 1367 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text))); 1368 } 1369 1370 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[], 1371 unsigned SLen) { 1372 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen))); 1373 } 1374 1375 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) { 1376 return unwrap<ConstantInt>(ConstantVal)->getZExtValue(); 1377 } 1378 1379 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) { 1380 return unwrap<ConstantInt>(ConstantVal)->getSExtValue(); 1381 } 1382 1383 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) { 1384 ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ; 1385 Type *Ty = cFP->getType(); 1386 1387 if (Ty->isFloatTy()) { 1388 *LosesInfo = false; 1389 return cFP->getValueAPF().convertToFloat(); 1390 } 1391 1392 if (Ty->isDoubleTy()) { 1393 *LosesInfo = false; 1394 return cFP->getValueAPF().convertToDouble(); 1395 } 1396 1397 bool APFLosesInfo; 1398 APFloat APF = cFP->getValueAPF(); 1399 APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo); 1400 *LosesInfo = APFLosesInfo; 1401 return APF.convertToDouble(); 1402 } 1403 1404 /*--.. Operations on composite constants ...................................--*/ 1405 1406 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str, 1407 unsigned Length, 1408 LLVMBool DontNullTerminate) { 1409 /* Inverted the sense of AddNull because ', 0)' is a 1410 better mnemonic for null termination than ', 1)'. */ 1411 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length), 1412 DontNullTerminate == 0)); 1413 } 1414 1415 LLVMValueRef LLVMConstString(const char *Str, unsigned Length, 1416 LLVMBool DontNullTerminate) { 1417 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length, 1418 DontNullTerminate); 1419 } 1420 1421 LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef C, unsigned idx) { 1422 return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx)); 1423 } 1424 1425 LLVMBool LLVMIsConstantString(LLVMValueRef C) { 1426 return unwrap<ConstantDataSequential>(C)->isString(); 1427 } 1428 1429 const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) { 1430 StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString(); 1431 *Length = Str.size(); 1432 return Str.data(); 1433 } 1434 1435 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy, 1436 LLVMValueRef *ConstantVals, unsigned Length) { 1437 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length); 1438 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V)); 1439 } 1440 1441 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C, 1442 LLVMValueRef *ConstantVals, 1443 unsigned Count, LLVMBool Packed) { 1444 Constant **Elements = unwrap<Constant>(ConstantVals, Count); 1445 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count), 1446 Packed != 0)); 1447 } 1448 1449 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count, 1450 LLVMBool Packed) { 1451 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count, 1452 Packed); 1453 } 1454 1455 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy, 1456 LLVMValueRef *ConstantVals, 1457 unsigned Count) { 1458 Constant **Elements = unwrap<Constant>(ConstantVals, Count); 1459 StructType *Ty = cast<StructType>(unwrap(StructTy)); 1460 1461 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count))); 1462 } 1463 1464 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) { 1465 return wrap(ConstantVector::get(makeArrayRef( 1466 unwrap<Constant>(ScalarConstantVals, Size), Size))); 1467 } 1468 1469 /*-- Opcode mapping */ 1470 1471 static LLVMOpcode map_to_llvmopcode(int opcode) 1472 { 1473 switch (opcode) { 1474 default: llvm_unreachable("Unhandled Opcode."); 1475 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc; 1476 #include "llvm/IR/Instruction.def" 1477 #undef HANDLE_INST 1478 } 1479 } 1480 1481 static int map_from_llvmopcode(LLVMOpcode code) 1482 { 1483 switch (code) { 1484 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num; 1485 #include "llvm/IR/Instruction.def" 1486 #undef HANDLE_INST 1487 } 1488 llvm_unreachable("Unhandled Opcode."); 1489 } 1490 1491 /*--.. Constant expressions ................................................--*/ 1492 1493 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) { 1494 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode()); 1495 } 1496 1497 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) { 1498 return wrap(ConstantExpr::getAlignOf(unwrap(Ty))); 1499 } 1500 1501 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) { 1502 return wrap(ConstantExpr::getSizeOf(unwrap(Ty))); 1503 } 1504 1505 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) { 1506 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal))); 1507 } 1508 1509 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) { 1510 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal))); 1511 } 1512 1513 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) { 1514 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal))); 1515 } 1516 1517 1518 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) { 1519 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal))); 1520 } 1521 1522 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) { 1523 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal))); 1524 } 1525 1526 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1527 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant), 1528 unwrap<Constant>(RHSConstant))); 1529 } 1530 1531 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant, 1532 LLVMValueRef RHSConstant) { 1533 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant), 1534 unwrap<Constant>(RHSConstant))); 1535 } 1536 1537 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant, 1538 LLVMValueRef RHSConstant) { 1539 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant), 1540 unwrap<Constant>(RHSConstant))); 1541 } 1542 1543 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1544 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant), 1545 unwrap<Constant>(RHSConstant))); 1546 } 1547 1548 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1549 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant), 1550 unwrap<Constant>(RHSConstant))); 1551 } 1552 1553 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant, 1554 LLVMValueRef RHSConstant) { 1555 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant), 1556 unwrap<Constant>(RHSConstant))); 1557 } 1558 1559 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant, 1560 LLVMValueRef RHSConstant) { 1561 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant), 1562 unwrap<Constant>(RHSConstant))); 1563 } 1564 1565 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1566 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant), 1567 unwrap<Constant>(RHSConstant))); 1568 } 1569 1570 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1571 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant), 1572 unwrap<Constant>(RHSConstant))); 1573 } 1574 1575 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant, 1576 LLVMValueRef RHSConstant) { 1577 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant), 1578 unwrap<Constant>(RHSConstant))); 1579 } 1580 1581 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant, 1582 LLVMValueRef RHSConstant) { 1583 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant), 1584 unwrap<Constant>(RHSConstant))); 1585 } 1586 1587 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1588 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant), 1589 unwrap<Constant>(RHSConstant))); 1590 } 1591 1592 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1593 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant), 1594 unwrap<Constant>(RHSConstant))); 1595 } 1596 1597 LLVMValueRef LLVMConstExactUDiv(LLVMValueRef LHSConstant, 1598 LLVMValueRef RHSConstant) { 1599 return wrap(ConstantExpr::getExactUDiv(unwrap<Constant>(LHSConstant), 1600 unwrap<Constant>(RHSConstant))); 1601 } 1602 1603 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1604 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant), 1605 unwrap<Constant>(RHSConstant))); 1606 } 1607 1608 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant, 1609 LLVMValueRef RHSConstant) { 1610 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant), 1611 unwrap<Constant>(RHSConstant))); 1612 } 1613 1614 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1615 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant), 1616 unwrap<Constant>(RHSConstant))); 1617 } 1618 1619 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1620 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant), 1621 unwrap<Constant>(RHSConstant))); 1622 } 1623 1624 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1625 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant), 1626 unwrap<Constant>(RHSConstant))); 1627 } 1628 1629 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1630 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant), 1631 unwrap<Constant>(RHSConstant))); 1632 } 1633 1634 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1635 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant), 1636 unwrap<Constant>(RHSConstant))); 1637 } 1638 1639 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1640 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant), 1641 unwrap<Constant>(RHSConstant))); 1642 } 1643 1644 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1645 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant), 1646 unwrap<Constant>(RHSConstant))); 1647 } 1648 1649 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate, 1650 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1651 return wrap(ConstantExpr::getICmp(Predicate, 1652 unwrap<Constant>(LHSConstant), 1653 unwrap<Constant>(RHSConstant))); 1654 } 1655 1656 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate, 1657 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1658 return wrap(ConstantExpr::getFCmp(Predicate, 1659 unwrap<Constant>(LHSConstant), 1660 unwrap<Constant>(RHSConstant))); 1661 } 1662 1663 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1664 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant), 1665 unwrap<Constant>(RHSConstant))); 1666 } 1667 1668 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1669 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant), 1670 unwrap<Constant>(RHSConstant))); 1671 } 1672 1673 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1674 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant), 1675 unwrap<Constant>(RHSConstant))); 1676 } 1677 1678 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal, 1679 LLVMValueRef *ConstantIndices, unsigned NumIndices) { 1680 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices), 1681 NumIndices); 1682 Constant *Val = unwrap<Constant>(ConstantVal); 1683 Type *Ty = 1684 cast<PointerType>(Val->getType()->getScalarType())->getElementType(); 1685 return wrap(ConstantExpr::getGetElementPtr(Ty, Val, IdxList)); 1686 } 1687 1688 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal, 1689 LLVMValueRef *ConstantIndices, 1690 unsigned NumIndices) { 1691 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices), 1692 NumIndices); 1693 Constant *Val = unwrap<Constant>(ConstantVal); 1694 Type *Ty = 1695 cast<PointerType>(Val->getType()->getScalarType())->getElementType(); 1696 return wrap(ConstantExpr::getInBoundsGetElementPtr(Ty, Val, IdxList)); 1697 } 1698 1699 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1700 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal), 1701 unwrap(ToType))); 1702 } 1703 1704 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1705 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal), 1706 unwrap(ToType))); 1707 } 1708 1709 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1710 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal), 1711 unwrap(ToType))); 1712 } 1713 1714 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1715 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal), 1716 unwrap(ToType))); 1717 } 1718 1719 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1720 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal), 1721 unwrap(ToType))); 1722 } 1723 1724 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1725 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal), 1726 unwrap(ToType))); 1727 } 1728 1729 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1730 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal), 1731 unwrap(ToType))); 1732 } 1733 1734 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1735 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal), 1736 unwrap(ToType))); 1737 } 1738 1739 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1740 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal), 1741 unwrap(ToType))); 1742 } 1743 1744 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1745 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal), 1746 unwrap(ToType))); 1747 } 1748 1749 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1750 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal), 1751 unwrap(ToType))); 1752 } 1753 1754 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1755 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal), 1756 unwrap(ToType))); 1757 } 1758 1759 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal, 1760 LLVMTypeRef ToType) { 1761 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal), 1762 unwrap(ToType))); 1763 } 1764 1765 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal, 1766 LLVMTypeRef ToType) { 1767 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal), 1768 unwrap(ToType))); 1769 } 1770 1771 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal, 1772 LLVMTypeRef ToType) { 1773 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal), 1774 unwrap(ToType))); 1775 } 1776 1777 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal, 1778 LLVMTypeRef ToType) { 1779 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal), 1780 unwrap(ToType))); 1781 } 1782 1783 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal, 1784 LLVMTypeRef ToType) { 1785 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal), 1786 unwrap(ToType))); 1787 } 1788 1789 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType, 1790 LLVMBool isSigned) { 1791 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal), 1792 unwrap(ToType), isSigned)); 1793 } 1794 1795 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1796 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal), 1797 unwrap(ToType))); 1798 } 1799 1800 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition, 1801 LLVMValueRef ConstantIfTrue, 1802 LLVMValueRef ConstantIfFalse) { 1803 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition), 1804 unwrap<Constant>(ConstantIfTrue), 1805 unwrap<Constant>(ConstantIfFalse))); 1806 } 1807 1808 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant, 1809 LLVMValueRef IndexConstant) { 1810 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant), 1811 unwrap<Constant>(IndexConstant))); 1812 } 1813 1814 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant, 1815 LLVMValueRef ElementValueConstant, 1816 LLVMValueRef IndexConstant) { 1817 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant), 1818 unwrap<Constant>(ElementValueConstant), 1819 unwrap<Constant>(IndexConstant))); 1820 } 1821 1822 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant, 1823 LLVMValueRef VectorBConstant, 1824 LLVMValueRef MaskConstant) { 1825 SmallVector<int, 16> IntMask; 1826 ShuffleVectorInst::getShuffleMask(unwrap<Constant>(MaskConstant), IntMask); 1827 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant), 1828 unwrap<Constant>(VectorBConstant), 1829 IntMask)); 1830 } 1831 1832 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList, 1833 unsigned NumIdx) { 1834 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant), 1835 makeArrayRef(IdxList, NumIdx))); 1836 } 1837 1838 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant, 1839 LLVMValueRef ElementValueConstant, 1840 unsigned *IdxList, unsigned NumIdx) { 1841 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant), 1842 unwrap<Constant>(ElementValueConstant), 1843 makeArrayRef(IdxList, NumIdx))); 1844 } 1845 1846 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString, 1847 const char *Constraints, 1848 LLVMBool HasSideEffects, 1849 LLVMBool IsAlignStack) { 1850 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString, 1851 Constraints, HasSideEffects, IsAlignStack)); 1852 } 1853 1854 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) { 1855 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB))); 1856 } 1857 1858 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/ 1859 1860 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) { 1861 return wrap(unwrap<GlobalValue>(Global)->getParent()); 1862 } 1863 1864 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) { 1865 return unwrap<GlobalValue>(Global)->isDeclaration(); 1866 } 1867 1868 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) { 1869 switch (unwrap<GlobalValue>(Global)->getLinkage()) { 1870 case GlobalValue::ExternalLinkage: 1871 return LLVMExternalLinkage; 1872 case GlobalValue::AvailableExternallyLinkage: 1873 return LLVMAvailableExternallyLinkage; 1874 case GlobalValue::LinkOnceAnyLinkage: 1875 return LLVMLinkOnceAnyLinkage; 1876 case GlobalValue::LinkOnceODRLinkage: 1877 return LLVMLinkOnceODRLinkage; 1878 case GlobalValue::WeakAnyLinkage: 1879 return LLVMWeakAnyLinkage; 1880 case GlobalValue::WeakODRLinkage: 1881 return LLVMWeakODRLinkage; 1882 case GlobalValue::AppendingLinkage: 1883 return LLVMAppendingLinkage; 1884 case GlobalValue::InternalLinkage: 1885 return LLVMInternalLinkage; 1886 case GlobalValue::PrivateLinkage: 1887 return LLVMPrivateLinkage; 1888 case GlobalValue::ExternalWeakLinkage: 1889 return LLVMExternalWeakLinkage; 1890 case GlobalValue::CommonLinkage: 1891 return LLVMCommonLinkage; 1892 } 1893 1894 llvm_unreachable("Invalid GlobalValue linkage!"); 1895 } 1896 1897 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) { 1898 GlobalValue *GV = unwrap<GlobalValue>(Global); 1899 1900 switch (Linkage) { 1901 case LLVMExternalLinkage: 1902 GV->setLinkage(GlobalValue::ExternalLinkage); 1903 break; 1904 case LLVMAvailableExternallyLinkage: 1905 GV->setLinkage(GlobalValue::AvailableExternallyLinkage); 1906 break; 1907 case LLVMLinkOnceAnyLinkage: 1908 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage); 1909 break; 1910 case LLVMLinkOnceODRLinkage: 1911 GV->setLinkage(GlobalValue::LinkOnceODRLinkage); 1912 break; 1913 case LLVMLinkOnceODRAutoHideLinkage: 1914 LLVM_DEBUG( 1915 errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no " 1916 "longer supported."); 1917 break; 1918 case LLVMWeakAnyLinkage: 1919 GV->setLinkage(GlobalValue::WeakAnyLinkage); 1920 break; 1921 case LLVMWeakODRLinkage: 1922 GV->setLinkage(GlobalValue::WeakODRLinkage); 1923 break; 1924 case LLVMAppendingLinkage: 1925 GV->setLinkage(GlobalValue::AppendingLinkage); 1926 break; 1927 case LLVMInternalLinkage: 1928 GV->setLinkage(GlobalValue::InternalLinkage); 1929 break; 1930 case LLVMPrivateLinkage: 1931 GV->setLinkage(GlobalValue::PrivateLinkage); 1932 break; 1933 case LLVMLinkerPrivateLinkage: 1934 GV->setLinkage(GlobalValue::PrivateLinkage); 1935 break; 1936 case LLVMLinkerPrivateWeakLinkage: 1937 GV->setLinkage(GlobalValue::PrivateLinkage); 1938 break; 1939 case LLVMDLLImportLinkage: 1940 LLVM_DEBUG( 1941 errs() 1942 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported."); 1943 break; 1944 case LLVMDLLExportLinkage: 1945 LLVM_DEBUG( 1946 errs() 1947 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported."); 1948 break; 1949 case LLVMExternalWeakLinkage: 1950 GV->setLinkage(GlobalValue::ExternalWeakLinkage); 1951 break; 1952 case LLVMGhostLinkage: 1953 LLVM_DEBUG( 1954 errs() << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported."); 1955 break; 1956 case LLVMCommonLinkage: 1957 GV->setLinkage(GlobalValue::CommonLinkage); 1958 break; 1959 } 1960 } 1961 1962 const char *LLVMGetSection(LLVMValueRef Global) { 1963 // Using .data() is safe because of how GlobalObject::setSection is 1964 // implemented. 1965 return unwrap<GlobalValue>(Global)->getSection().data(); 1966 } 1967 1968 void LLVMSetSection(LLVMValueRef Global, const char *Section) { 1969 unwrap<GlobalObject>(Global)->setSection(Section); 1970 } 1971 1972 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) { 1973 return static_cast<LLVMVisibility>( 1974 unwrap<GlobalValue>(Global)->getVisibility()); 1975 } 1976 1977 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) { 1978 unwrap<GlobalValue>(Global) 1979 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz)); 1980 } 1981 1982 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) { 1983 return static_cast<LLVMDLLStorageClass>( 1984 unwrap<GlobalValue>(Global)->getDLLStorageClass()); 1985 } 1986 1987 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) { 1988 unwrap<GlobalValue>(Global)->setDLLStorageClass( 1989 static_cast<GlobalValue::DLLStorageClassTypes>(Class)); 1990 } 1991 1992 LLVMUnnamedAddr LLVMGetUnnamedAddress(LLVMValueRef Global) { 1993 switch (unwrap<GlobalValue>(Global)->getUnnamedAddr()) { 1994 case GlobalVariable::UnnamedAddr::None: 1995 return LLVMNoUnnamedAddr; 1996 case GlobalVariable::UnnamedAddr::Local: 1997 return LLVMLocalUnnamedAddr; 1998 case GlobalVariable::UnnamedAddr::Global: 1999 return LLVMGlobalUnnamedAddr; 2000 } 2001 llvm_unreachable("Unknown UnnamedAddr kind!"); 2002 } 2003 2004 void LLVMSetUnnamedAddress(LLVMValueRef Global, LLVMUnnamedAddr UnnamedAddr) { 2005 GlobalValue *GV = unwrap<GlobalValue>(Global); 2006 2007 switch (UnnamedAddr) { 2008 case LLVMNoUnnamedAddr: 2009 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::None); 2010 case LLVMLocalUnnamedAddr: 2011 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Local); 2012 case LLVMGlobalUnnamedAddr: 2013 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Global); 2014 } 2015 } 2016 2017 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) { 2018 return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr(); 2019 } 2020 2021 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) { 2022 unwrap<GlobalValue>(Global)->setUnnamedAddr( 2023 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global 2024 : GlobalValue::UnnamedAddr::None); 2025 } 2026 2027 LLVMTypeRef LLVMGlobalGetValueType(LLVMValueRef Global) { 2028 return wrap(unwrap<GlobalValue>(Global)->getValueType()); 2029 } 2030 2031 /*--.. Operations on global variables, load and store instructions .........--*/ 2032 2033 unsigned LLVMGetAlignment(LLVMValueRef V) { 2034 Value *P = unwrap<Value>(V); 2035 if (GlobalObject *GV = dyn_cast<GlobalObject>(P)) 2036 return GV->getAlignment(); 2037 if (AllocaInst *AI = dyn_cast<AllocaInst>(P)) 2038 return AI->getAlignment(); 2039 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 2040 return LI->getAlignment(); 2041 if (StoreInst *SI = dyn_cast<StoreInst>(P)) 2042 return SI->getAlignment(); 2043 if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P)) 2044 return RMWI->getAlign().value(); 2045 if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P)) 2046 return CXI->getAlign().value(); 2047 2048 llvm_unreachable( 2049 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, " 2050 "and AtomicCmpXchgInst have alignment"); 2051 } 2052 2053 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) { 2054 Value *P = unwrap<Value>(V); 2055 if (GlobalObject *GV = dyn_cast<GlobalObject>(P)) 2056 GV->setAlignment(MaybeAlign(Bytes)); 2057 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P)) 2058 AI->setAlignment(Align(Bytes)); 2059 else if (LoadInst *LI = dyn_cast<LoadInst>(P)) 2060 LI->setAlignment(Align(Bytes)); 2061 else if (StoreInst *SI = dyn_cast<StoreInst>(P)) 2062 SI->setAlignment(Align(Bytes)); 2063 else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P)) 2064 RMWI->setAlignment(Align(Bytes)); 2065 else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P)) 2066 CXI->setAlignment(Align(Bytes)); 2067 else 2068 llvm_unreachable( 2069 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, and " 2070 "and AtomicCmpXchgInst have alignment"); 2071 } 2072 2073 LLVMValueMetadataEntry *LLVMGlobalCopyAllMetadata(LLVMValueRef Value, 2074 size_t *NumEntries) { 2075 return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) { 2076 Entries.clear(); 2077 if (Instruction *Instr = dyn_cast<Instruction>(unwrap(Value))) { 2078 Instr->getAllMetadata(Entries); 2079 } else { 2080 unwrap<GlobalObject>(Value)->getAllMetadata(Entries); 2081 } 2082 }); 2083 } 2084 2085 unsigned LLVMValueMetadataEntriesGetKind(LLVMValueMetadataEntry *Entries, 2086 unsigned Index) { 2087 LLVMOpaqueValueMetadataEntry MVE = 2088 static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]); 2089 return MVE.Kind; 2090 } 2091 2092 LLVMMetadataRef 2093 LLVMValueMetadataEntriesGetMetadata(LLVMValueMetadataEntry *Entries, 2094 unsigned Index) { 2095 LLVMOpaqueValueMetadataEntry MVE = 2096 static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]); 2097 return MVE.Metadata; 2098 } 2099 2100 void LLVMDisposeValueMetadataEntries(LLVMValueMetadataEntry *Entries) { 2101 free(Entries); 2102 } 2103 2104 void LLVMGlobalSetMetadata(LLVMValueRef Global, unsigned Kind, 2105 LLVMMetadataRef MD) { 2106 unwrap<GlobalObject>(Global)->setMetadata(Kind, unwrap<MDNode>(MD)); 2107 } 2108 2109 void LLVMGlobalEraseMetadata(LLVMValueRef Global, unsigned Kind) { 2110 unwrap<GlobalObject>(Global)->eraseMetadata(Kind); 2111 } 2112 2113 void LLVMGlobalClearMetadata(LLVMValueRef Global) { 2114 unwrap<GlobalObject>(Global)->clearMetadata(); 2115 } 2116 2117 /*--.. Operations on global variables ......................................--*/ 2118 2119 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) { 2120 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false, 2121 GlobalValue::ExternalLinkage, nullptr, Name)); 2122 } 2123 2124 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty, 2125 const char *Name, 2126 unsigned AddressSpace) { 2127 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false, 2128 GlobalValue::ExternalLinkage, nullptr, Name, 2129 nullptr, GlobalVariable::NotThreadLocal, 2130 AddressSpace)); 2131 } 2132 2133 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) { 2134 return wrap(unwrap(M)->getNamedGlobal(Name)); 2135 } 2136 2137 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) { 2138 Module *Mod = unwrap(M); 2139 Module::global_iterator I = Mod->global_begin(); 2140 if (I == Mod->global_end()) 2141 return nullptr; 2142 return wrap(&*I); 2143 } 2144 2145 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) { 2146 Module *Mod = unwrap(M); 2147 Module::global_iterator I = Mod->global_end(); 2148 if (I == Mod->global_begin()) 2149 return nullptr; 2150 return wrap(&*--I); 2151 } 2152 2153 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) { 2154 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar); 2155 Module::global_iterator I(GV); 2156 if (++I == GV->getParent()->global_end()) 2157 return nullptr; 2158 return wrap(&*I); 2159 } 2160 2161 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) { 2162 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar); 2163 Module::global_iterator I(GV); 2164 if (I == GV->getParent()->global_begin()) 2165 return nullptr; 2166 return wrap(&*--I); 2167 } 2168 2169 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) { 2170 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent(); 2171 } 2172 2173 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) { 2174 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar); 2175 if ( !GV->hasInitializer() ) 2176 return nullptr; 2177 return wrap(GV->getInitializer()); 2178 } 2179 2180 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) { 2181 unwrap<GlobalVariable>(GlobalVar) 2182 ->setInitializer(unwrap<Constant>(ConstantVal)); 2183 } 2184 2185 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) { 2186 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal(); 2187 } 2188 2189 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) { 2190 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0); 2191 } 2192 2193 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) { 2194 return unwrap<GlobalVariable>(GlobalVar)->isConstant(); 2195 } 2196 2197 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) { 2198 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0); 2199 } 2200 2201 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) { 2202 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) { 2203 case GlobalVariable::NotThreadLocal: 2204 return LLVMNotThreadLocal; 2205 case GlobalVariable::GeneralDynamicTLSModel: 2206 return LLVMGeneralDynamicTLSModel; 2207 case GlobalVariable::LocalDynamicTLSModel: 2208 return LLVMLocalDynamicTLSModel; 2209 case GlobalVariable::InitialExecTLSModel: 2210 return LLVMInitialExecTLSModel; 2211 case GlobalVariable::LocalExecTLSModel: 2212 return LLVMLocalExecTLSModel; 2213 } 2214 2215 llvm_unreachable("Invalid GlobalVariable thread local mode"); 2216 } 2217 2218 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) { 2219 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar); 2220 2221 switch (Mode) { 2222 case LLVMNotThreadLocal: 2223 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal); 2224 break; 2225 case LLVMGeneralDynamicTLSModel: 2226 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel); 2227 break; 2228 case LLVMLocalDynamicTLSModel: 2229 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel); 2230 break; 2231 case LLVMInitialExecTLSModel: 2232 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel); 2233 break; 2234 case LLVMLocalExecTLSModel: 2235 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel); 2236 break; 2237 } 2238 } 2239 2240 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) { 2241 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized(); 2242 } 2243 2244 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) { 2245 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit); 2246 } 2247 2248 /*--.. Operations on aliases ......................................--*/ 2249 2250 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee, 2251 const char *Name) { 2252 auto *PTy = cast<PointerType>(unwrap(Ty)); 2253 return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(), 2254 GlobalValue::ExternalLinkage, Name, 2255 unwrap<Constant>(Aliasee), unwrap(M))); 2256 } 2257 2258 LLVMValueRef LLVMGetNamedGlobalAlias(LLVMModuleRef M, 2259 const char *Name, size_t NameLen) { 2260 return wrap(unwrap(M)->getNamedAlias(Name)); 2261 } 2262 2263 LLVMValueRef LLVMGetFirstGlobalAlias(LLVMModuleRef M) { 2264 Module *Mod = unwrap(M); 2265 Module::alias_iterator I = Mod->alias_begin(); 2266 if (I == Mod->alias_end()) 2267 return nullptr; 2268 return wrap(&*I); 2269 } 2270 2271 LLVMValueRef LLVMGetLastGlobalAlias(LLVMModuleRef M) { 2272 Module *Mod = unwrap(M); 2273 Module::alias_iterator I = Mod->alias_end(); 2274 if (I == Mod->alias_begin()) 2275 return nullptr; 2276 return wrap(&*--I); 2277 } 2278 2279 LLVMValueRef LLVMGetNextGlobalAlias(LLVMValueRef GA) { 2280 GlobalAlias *Alias = unwrap<GlobalAlias>(GA); 2281 Module::alias_iterator I(Alias); 2282 if (++I == Alias->getParent()->alias_end()) 2283 return nullptr; 2284 return wrap(&*I); 2285 } 2286 2287 LLVMValueRef LLVMGetPreviousGlobalAlias(LLVMValueRef GA) { 2288 GlobalAlias *Alias = unwrap<GlobalAlias>(GA); 2289 Module::alias_iterator I(Alias); 2290 if (I == Alias->getParent()->alias_begin()) 2291 return nullptr; 2292 return wrap(&*--I); 2293 } 2294 2295 LLVMValueRef LLVMAliasGetAliasee(LLVMValueRef Alias) { 2296 return wrap(unwrap<GlobalAlias>(Alias)->getAliasee()); 2297 } 2298 2299 void LLVMAliasSetAliasee(LLVMValueRef Alias, LLVMValueRef Aliasee) { 2300 unwrap<GlobalAlias>(Alias)->setAliasee(unwrap<Constant>(Aliasee)); 2301 } 2302 2303 /*--.. Operations on functions .............................................--*/ 2304 2305 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name, 2306 LLVMTypeRef FunctionTy) { 2307 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy), 2308 GlobalValue::ExternalLinkage, Name, unwrap(M))); 2309 } 2310 2311 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) { 2312 return wrap(unwrap(M)->getFunction(Name)); 2313 } 2314 2315 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) { 2316 Module *Mod = unwrap(M); 2317 Module::iterator I = Mod->begin(); 2318 if (I == Mod->end()) 2319 return nullptr; 2320 return wrap(&*I); 2321 } 2322 2323 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) { 2324 Module *Mod = unwrap(M); 2325 Module::iterator I = Mod->end(); 2326 if (I == Mod->begin()) 2327 return nullptr; 2328 return wrap(&*--I); 2329 } 2330 2331 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) { 2332 Function *Func = unwrap<Function>(Fn); 2333 Module::iterator I(Func); 2334 if (++I == Func->getParent()->end()) 2335 return nullptr; 2336 return wrap(&*I); 2337 } 2338 2339 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) { 2340 Function *Func = unwrap<Function>(Fn); 2341 Module::iterator I(Func); 2342 if (I == Func->getParent()->begin()) 2343 return nullptr; 2344 return wrap(&*--I); 2345 } 2346 2347 void LLVMDeleteFunction(LLVMValueRef Fn) { 2348 unwrap<Function>(Fn)->eraseFromParent(); 2349 } 2350 2351 LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) { 2352 return unwrap<Function>(Fn)->hasPersonalityFn(); 2353 } 2354 2355 LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) { 2356 return wrap(unwrap<Function>(Fn)->getPersonalityFn()); 2357 } 2358 2359 void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) { 2360 unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn)); 2361 } 2362 2363 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) { 2364 if (Function *F = dyn_cast<Function>(unwrap(Fn))) 2365 return F->getIntrinsicID(); 2366 return 0; 2367 } 2368 2369 static Intrinsic::ID llvm_map_to_intrinsic_id(unsigned ID) { 2370 assert(ID < llvm::Intrinsic::num_intrinsics && "Intrinsic ID out of range"); 2371 return llvm::Intrinsic::ID(ID); 2372 } 2373 2374 LLVMValueRef LLVMGetIntrinsicDeclaration(LLVMModuleRef Mod, 2375 unsigned ID, 2376 LLVMTypeRef *ParamTypes, 2377 size_t ParamCount) { 2378 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount); 2379 auto IID = llvm_map_to_intrinsic_id(ID); 2380 return wrap(llvm::Intrinsic::getDeclaration(unwrap(Mod), IID, Tys)); 2381 } 2382 2383 const char *LLVMIntrinsicGetName(unsigned ID, size_t *NameLength) { 2384 auto IID = llvm_map_to_intrinsic_id(ID); 2385 auto Str = llvm::Intrinsic::getName(IID); 2386 *NameLength = Str.size(); 2387 return Str.data(); 2388 } 2389 2390 LLVMTypeRef LLVMIntrinsicGetType(LLVMContextRef Ctx, unsigned ID, 2391 LLVMTypeRef *ParamTypes, size_t ParamCount) { 2392 auto IID = llvm_map_to_intrinsic_id(ID); 2393 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount); 2394 return wrap(llvm::Intrinsic::getType(*unwrap(Ctx), IID, Tys)); 2395 } 2396 2397 const char *LLVMIntrinsicCopyOverloadedName(unsigned ID, 2398 LLVMTypeRef *ParamTypes, 2399 size_t ParamCount, 2400 size_t *NameLength) { 2401 auto IID = llvm_map_to_intrinsic_id(ID); 2402 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount); 2403 auto Str = llvm::Intrinsic::getName(IID, Tys); 2404 *NameLength = Str.length(); 2405 return strdup(Str.c_str()); 2406 } 2407 2408 unsigned LLVMLookupIntrinsicID(const char *Name, size_t NameLen) { 2409 return Function::lookupIntrinsicID({Name, NameLen}); 2410 } 2411 2412 LLVMBool LLVMIntrinsicIsOverloaded(unsigned ID) { 2413 auto IID = llvm_map_to_intrinsic_id(ID); 2414 return llvm::Intrinsic::isOverloaded(IID); 2415 } 2416 2417 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) { 2418 return unwrap<Function>(Fn)->getCallingConv(); 2419 } 2420 2421 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) { 2422 return unwrap<Function>(Fn)->setCallingConv( 2423 static_cast<CallingConv::ID>(CC)); 2424 } 2425 2426 const char *LLVMGetGC(LLVMValueRef Fn) { 2427 Function *F = unwrap<Function>(Fn); 2428 return F->hasGC()? F->getGC().c_str() : nullptr; 2429 } 2430 2431 void LLVMSetGC(LLVMValueRef Fn, const char *GC) { 2432 Function *F = unwrap<Function>(Fn); 2433 if (GC) 2434 F->setGC(GC); 2435 else 2436 F->clearGC(); 2437 } 2438 2439 void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx, 2440 LLVMAttributeRef A) { 2441 unwrap<Function>(F)->addAttribute(Idx, unwrap(A)); 2442 } 2443 2444 unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) { 2445 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx); 2446 return AS.getNumAttributes(); 2447 } 2448 2449 void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx, 2450 LLVMAttributeRef *Attrs) { 2451 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx); 2452 for (auto A : AS) 2453 *Attrs++ = wrap(A); 2454 } 2455 2456 LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F, 2457 LLVMAttributeIndex Idx, 2458 unsigned KindID) { 2459 return wrap(unwrap<Function>(F)->getAttribute(Idx, 2460 (Attribute::AttrKind)KindID)); 2461 } 2462 2463 LLVMAttributeRef LLVMGetStringAttributeAtIndex(LLVMValueRef F, 2464 LLVMAttributeIndex Idx, 2465 const char *K, unsigned KLen) { 2466 return wrap(unwrap<Function>(F)->getAttribute(Idx, StringRef(K, KLen))); 2467 } 2468 2469 void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx, 2470 unsigned KindID) { 2471 unwrap<Function>(F)->removeAttribute(Idx, (Attribute::AttrKind)KindID); 2472 } 2473 2474 void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx, 2475 const char *K, unsigned KLen) { 2476 unwrap<Function>(F)->removeAttribute(Idx, StringRef(K, KLen)); 2477 } 2478 2479 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A, 2480 const char *V) { 2481 Function *Func = unwrap<Function>(Fn); 2482 Attribute Attr = Attribute::get(Func->getContext(), A, V); 2483 Func->addAttribute(AttributeList::FunctionIndex, Attr); 2484 } 2485 2486 /*--.. Operations on parameters ............................................--*/ 2487 2488 unsigned LLVMCountParams(LLVMValueRef FnRef) { 2489 // This function is strictly redundant to 2490 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef))) 2491 return unwrap<Function>(FnRef)->arg_size(); 2492 } 2493 2494 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) { 2495 Function *Fn = unwrap<Function>(FnRef); 2496 for (Argument &A : Fn->args()) 2497 *ParamRefs++ = wrap(&A); 2498 } 2499 2500 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) { 2501 Function *Fn = unwrap<Function>(FnRef); 2502 return wrap(&Fn->arg_begin()[index]); 2503 } 2504 2505 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) { 2506 return wrap(unwrap<Argument>(V)->getParent()); 2507 } 2508 2509 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) { 2510 Function *Func = unwrap<Function>(Fn); 2511 Function::arg_iterator I = Func->arg_begin(); 2512 if (I == Func->arg_end()) 2513 return nullptr; 2514 return wrap(&*I); 2515 } 2516 2517 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) { 2518 Function *Func = unwrap<Function>(Fn); 2519 Function::arg_iterator I = Func->arg_end(); 2520 if (I == Func->arg_begin()) 2521 return nullptr; 2522 return wrap(&*--I); 2523 } 2524 2525 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) { 2526 Argument *A = unwrap<Argument>(Arg); 2527 Function *Fn = A->getParent(); 2528 if (A->getArgNo() + 1 >= Fn->arg_size()) 2529 return nullptr; 2530 return wrap(&Fn->arg_begin()[A->getArgNo() + 1]); 2531 } 2532 2533 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) { 2534 Argument *A = unwrap<Argument>(Arg); 2535 if (A->getArgNo() == 0) 2536 return nullptr; 2537 return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]); 2538 } 2539 2540 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) { 2541 Argument *A = unwrap<Argument>(Arg); 2542 A->addAttr(Attribute::getWithAlignment(A->getContext(), Align(align))); 2543 } 2544 2545 /*--.. Operations on ifuncs ................................................--*/ 2546 2547 LLVMValueRef LLVMAddGlobalIFunc(LLVMModuleRef M, 2548 const char *Name, size_t NameLen, 2549 LLVMTypeRef Ty, unsigned AddrSpace, 2550 LLVMValueRef Resolver) { 2551 return wrap(GlobalIFunc::create(unwrap(Ty), AddrSpace, 2552 GlobalValue::ExternalLinkage, 2553 StringRef(Name, NameLen), 2554 unwrap<Constant>(Resolver), unwrap(M))); 2555 } 2556 2557 LLVMValueRef LLVMGetNamedGlobalIFunc(LLVMModuleRef M, 2558 const char *Name, size_t NameLen) { 2559 return wrap(unwrap(M)->getNamedIFunc(StringRef(Name, NameLen))); 2560 } 2561 2562 LLVMValueRef LLVMGetFirstGlobalIFunc(LLVMModuleRef M) { 2563 Module *Mod = unwrap(M); 2564 Module::ifunc_iterator I = Mod->ifunc_begin(); 2565 if (I == Mod->ifunc_end()) 2566 return nullptr; 2567 return wrap(&*I); 2568 } 2569 2570 LLVMValueRef LLVMGetLastGlobalIFunc(LLVMModuleRef M) { 2571 Module *Mod = unwrap(M); 2572 Module::ifunc_iterator I = Mod->ifunc_end(); 2573 if (I == Mod->ifunc_begin()) 2574 return nullptr; 2575 return wrap(&*--I); 2576 } 2577 2578 LLVMValueRef LLVMGetNextGlobalIFunc(LLVMValueRef IFunc) { 2579 GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc); 2580 Module::ifunc_iterator I(GIF); 2581 if (++I == GIF->getParent()->ifunc_end()) 2582 return nullptr; 2583 return wrap(&*I); 2584 } 2585 2586 LLVMValueRef LLVMGetPreviousGlobalIFunc(LLVMValueRef IFunc) { 2587 GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc); 2588 Module::ifunc_iterator I(GIF); 2589 if (I == GIF->getParent()->ifunc_begin()) 2590 return nullptr; 2591 return wrap(&*--I); 2592 } 2593 2594 LLVMValueRef LLVMGetGlobalIFuncResolver(LLVMValueRef IFunc) { 2595 return wrap(unwrap<GlobalIFunc>(IFunc)->getResolver()); 2596 } 2597 2598 void LLVMSetGlobalIFuncResolver(LLVMValueRef IFunc, LLVMValueRef Resolver) { 2599 unwrap<GlobalIFunc>(IFunc)->setResolver(unwrap<Constant>(Resolver)); 2600 } 2601 2602 void LLVMEraseGlobalIFunc(LLVMValueRef IFunc) { 2603 unwrap<GlobalIFunc>(IFunc)->eraseFromParent(); 2604 } 2605 2606 void LLVMRemoveGlobalIFunc(LLVMValueRef IFunc) { 2607 unwrap<GlobalIFunc>(IFunc)->removeFromParent(); 2608 } 2609 2610 /*--.. Operations on basic blocks ..........................................--*/ 2611 2612 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) { 2613 return wrap(static_cast<Value*>(unwrap(BB))); 2614 } 2615 2616 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) { 2617 return isa<BasicBlock>(unwrap(Val)); 2618 } 2619 2620 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) { 2621 return wrap(unwrap<BasicBlock>(Val)); 2622 } 2623 2624 const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) { 2625 return unwrap(BB)->getName().data(); 2626 } 2627 2628 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) { 2629 return wrap(unwrap(BB)->getParent()); 2630 } 2631 2632 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) { 2633 return wrap(unwrap(BB)->getTerminator()); 2634 } 2635 2636 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) { 2637 return unwrap<Function>(FnRef)->size(); 2638 } 2639 2640 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){ 2641 Function *Fn = unwrap<Function>(FnRef); 2642 for (BasicBlock &BB : *Fn) 2643 *BasicBlocksRefs++ = wrap(&BB); 2644 } 2645 2646 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) { 2647 return wrap(&unwrap<Function>(Fn)->getEntryBlock()); 2648 } 2649 2650 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) { 2651 Function *Func = unwrap<Function>(Fn); 2652 Function::iterator I = Func->begin(); 2653 if (I == Func->end()) 2654 return nullptr; 2655 return wrap(&*I); 2656 } 2657 2658 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) { 2659 Function *Func = unwrap<Function>(Fn); 2660 Function::iterator I = Func->end(); 2661 if (I == Func->begin()) 2662 return nullptr; 2663 return wrap(&*--I); 2664 } 2665 2666 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) { 2667 BasicBlock *Block = unwrap(BB); 2668 Function::iterator I(Block); 2669 if (++I == Block->getParent()->end()) 2670 return nullptr; 2671 return wrap(&*I); 2672 } 2673 2674 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) { 2675 BasicBlock *Block = unwrap(BB); 2676 Function::iterator I(Block); 2677 if (I == Block->getParent()->begin()) 2678 return nullptr; 2679 return wrap(&*--I); 2680 } 2681 2682 LLVMBasicBlockRef LLVMCreateBasicBlockInContext(LLVMContextRef C, 2683 const char *Name) { 2684 return wrap(llvm::BasicBlock::Create(*unwrap(C), Name)); 2685 } 2686 2687 void LLVMInsertExistingBasicBlockAfterInsertBlock(LLVMBuilderRef Builder, 2688 LLVMBasicBlockRef BB) { 2689 BasicBlock *ToInsert = unwrap(BB); 2690 BasicBlock *CurBB = unwrap(Builder)->GetInsertBlock(); 2691 assert(CurBB && "current insertion point is invalid!"); 2692 CurBB->getParent()->getBasicBlockList().insertAfter(CurBB->getIterator(), 2693 ToInsert); 2694 } 2695 2696 void LLVMAppendExistingBasicBlock(LLVMValueRef Fn, 2697 LLVMBasicBlockRef BB) { 2698 unwrap<Function>(Fn)->getBasicBlockList().push_back(unwrap(BB)); 2699 } 2700 2701 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C, 2702 LLVMValueRef FnRef, 2703 const char *Name) { 2704 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef))); 2705 } 2706 2707 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) { 2708 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name); 2709 } 2710 2711 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C, 2712 LLVMBasicBlockRef BBRef, 2713 const char *Name) { 2714 BasicBlock *BB = unwrap(BBRef); 2715 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB)); 2716 } 2717 2718 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef, 2719 const char *Name) { 2720 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name); 2721 } 2722 2723 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) { 2724 unwrap(BBRef)->eraseFromParent(); 2725 } 2726 2727 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) { 2728 unwrap(BBRef)->removeFromParent(); 2729 } 2730 2731 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) { 2732 unwrap(BB)->moveBefore(unwrap(MovePos)); 2733 } 2734 2735 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) { 2736 unwrap(BB)->moveAfter(unwrap(MovePos)); 2737 } 2738 2739 /*--.. Operations on instructions ..........................................--*/ 2740 2741 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) { 2742 return wrap(unwrap<Instruction>(Inst)->getParent()); 2743 } 2744 2745 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) { 2746 BasicBlock *Block = unwrap(BB); 2747 BasicBlock::iterator I = Block->begin(); 2748 if (I == Block->end()) 2749 return nullptr; 2750 return wrap(&*I); 2751 } 2752 2753 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) { 2754 BasicBlock *Block = unwrap(BB); 2755 BasicBlock::iterator I = Block->end(); 2756 if (I == Block->begin()) 2757 return nullptr; 2758 return wrap(&*--I); 2759 } 2760 2761 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) { 2762 Instruction *Instr = unwrap<Instruction>(Inst); 2763 BasicBlock::iterator I(Instr); 2764 if (++I == Instr->getParent()->end()) 2765 return nullptr; 2766 return wrap(&*I); 2767 } 2768 2769 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) { 2770 Instruction *Instr = unwrap<Instruction>(Inst); 2771 BasicBlock::iterator I(Instr); 2772 if (I == Instr->getParent()->begin()) 2773 return nullptr; 2774 return wrap(&*--I); 2775 } 2776 2777 void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) { 2778 unwrap<Instruction>(Inst)->removeFromParent(); 2779 } 2780 2781 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) { 2782 unwrap<Instruction>(Inst)->eraseFromParent(); 2783 } 2784 2785 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) { 2786 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst))) 2787 return (LLVMIntPredicate)I->getPredicate(); 2788 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst))) 2789 if (CE->getOpcode() == Instruction::ICmp) 2790 return (LLVMIntPredicate)CE->getPredicate(); 2791 return (LLVMIntPredicate)0; 2792 } 2793 2794 LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) { 2795 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst))) 2796 return (LLVMRealPredicate)I->getPredicate(); 2797 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst))) 2798 if (CE->getOpcode() == Instruction::FCmp) 2799 return (LLVMRealPredicate)CE->getPredicate(); 2800 return (LLVMRealPredicate)0; 2801 } 2802 2803 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) { 2804 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst))) 2805 return map_to_llvmopcode(C->getOpcode()); 2806 return (LLVMOpcode)0; 2807 } 2808 2809 LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) { 2810 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst))) 2811 return wrap(C->clone()); 2812 return nullptr; 2813 } 2814 2815 LLVMValueRef LLVMIsATerminatorInst(LLVMValueRef Inst) { 2816 Instruction *I = dyn_cast<Instruction>(unwrap(Inst)); 2817 return (I && I->isTerminator()) ? wrap(I) : nullptr; 2818 } 2819 2820 unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) { 2821 if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) { 2822 return FPI->getNumArgOperands(); 2823 } 2824 return unwrap<CallBase>(Instr)->getNumArgOperands(); 2825 } 2826 2827 /*--.. Call and invoke instructions ........................................--*/ 2828 2829 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) { 2830 return unwrap<CallBase>(Instr)->getCallingConv(); 2831 } 2832 2833 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) { 2834 return unwrap<CallBase>(Instr)->setCallingConv( 2835 static_cast<CallingConv::ID>(CC)); 2836 } 2837 2838 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, 2839 unsigned align) { 2840 auto *Call = unwrap<CallBase>(Instr); 2841 Attribute AlignAttr = 2842 Attribute::getWithAlignment(Call->getContext(), Align(align)); 2843 Call->addAttribute(index, AlignAttr); 2844 } 2845 2846 void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx, 2847 LLVMAttributeRef A) { 2848 unwrap<CallBase>(C)->addAttribute(Idx, unwrap(A)); 2849 } 2850 2851 unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C, 2852 LLVMAttributeIndex Idx) { 2853 auto *Call = unwrap<CallBase>(C); 2854 auto AS = Call->getAttributes().getAttributes(Idx); 2855 return AS.getNumAttributes(); 2856 } 2857 2858 void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx, 2859 LLVMAttributeRef *Attrs) { 2860 auto *Call = unwrap<CallBase>(C); 2861 auto AS = Call->getAttributes().getAttributes(Idx); 2862 for (auto A : AS) 2863 *Attrs++ = wrap(A); 2864 } 2865 2866 LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C, 2867 LLVMAttributeIndex Idx, 2868 unsigned KindID) { 2869 return wrap( 2870 unwrap<CallBase>(C)->getAttribute(Idx, (Attribute::AttrKind)KindID)); 2871 } 2872 2873 LLVMAttributeRef LLVMGetCallSiteStringAttribute(LLVMValueRef C, 2874 LLVMAttributeIndex Idx, 2875 const char *K, unsigned KLen) { 2876 return wrap(unwrap<CallBase>(C)->getAttribute(Idx, StringRef(K, KLen))); 2877 } 2878 2879 void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C, LLVMAttributeIndex Idx, 2880 unsigned KindID) { 2881 unwrap<CallBase>(C)->removeAttribute(Idx, (Attribute::AttrKind)KindID); 2882 } 2883 2884 void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C, LLVMAttributeIndex Idx, 2885 const char *K, unsigned KLen) { 2886 unwrap<CallBase>(C)->removeAttribute(Idx, StringRef(K, KLen)); 2887 } 2888 2889 LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) { 2890 return wrap(unwrap<CallBase>(Instr)->getCalledOperand()); 2891 } 2892 2893 LLVMTypeRef LLVMGetCalledFunctionType(LLVMValueRef Instr) { 2894 return wrap(unwrap<CallBase>(Instr)->getFunctionType()); 2895 } 2896 2897 /*--.. Operations on call instructions (only) ..............................--*/ 2898 2899 LLVMBool LLVMIsTailCall(LLVMValueRef Call) { 2900 return unwrap<CallInst>(Call)->isTailCall(); 2901 } 2902 2903 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) { 2904 unwrap<CallInst>(Call)->setTailCall(isTailCall); 2905 } 2906 2907 /*--.. Operations on invoke instructions (only) ............................--*/ 2908 2909 LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) { 2910 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest()); 2911 } 2912 2913 LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) { 2914 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) { 2915 return wrap(CRI->getUnwindDest()); 2916 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) { 2917 return wrap(CSI->getUnwindDest()); 2918 } 2919 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest()); 2920 } 2921 2922 void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) { 2923 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B)); 2924 } 2925 2926 void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) { 2927 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) { 2928 return CRI->setUnwindDest(unwrap(B)); 2929 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) { 2930 return CSI->setUnwindDest(unwrap(B)); 2931 } 2932 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B)); 2933 } 2934 2935 /*--.. Operations on terminators ...........................................--*/ 2936 2937 unsigned LLVMGetNumSuccessors(LLVMValueRef Term) { 2938 return unwrap<Instruction>(Term)->getNumSuccessors(); 2939 } 2940 2941 LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) { 2942 return wrap(unwrap<Instruction>(Term)->getSuccessor(i)); 2943 } 2944 2945 void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) { 2946 return unwrap<Instruction>(Term)->setSuccessor(i, unwrap(block)); 2947 } 2948 2949 /*--.. Operations on branch instructions (only) ............................--*/ 2950 2951 LLVMBool LLVMIsConditional(LLVMValueRef Branch) { 2952 return unwrap<BranchInst>(Branch)->isConditional(); 2953 } 2954 2955 LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) { 2956 return wrap(unwrap<BranchInst>(Branch)->getCondition()); 2957 } 2958 2959 void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) { 2960 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond)); 2961 } 2962 2963 /*--.. Operations on switch instructions (only) ............................--*/ 2964 2965 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) { 2966 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest()); 2967 } 2968 2969 /*--.. Operations on alloca instructions (only) ............................--*/ 2970 2971 LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) { 2972 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType()); 2973 } 2974 2975 /*--.. Operations on gep instructions (only) ...............................--*/ 2976 2977 LLVMBool LLVMIsInBounds(LLVMValueRef GEP) { 2978 return unwrap<GetElementPtrInst>(GEP)->isInBounds(); 2979 } 2980 2981 void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) { 2982 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds); 2983 } 2984 2985 /*--.. Operations on phi nodes .............................................--*/ 2986 2987 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues, 2988 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) { 2989 PHINode *PhiVal = unwrap<PHINode>(PhiNode); 2990 for (unsigned I = 0; I != Count; ++I) 2991 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I])); 2992 } 2993 2994 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) { 2995 return unwrap<PHINode>(PhiNode)->getNumIncomingValues(); 2996 } 2997 2998 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) { 2999 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index)); 3000 } 3001 3002 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) { 3003 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index)); 3004 } 3005 3006 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/ 3007 3008 unsigned LLVMGetNumIndices(LLVMValueRef Inst) { 3009 auto *I = unwrap(Inst); 3010 if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) 3011 return GEP->getNumIndices(); 3012 if (auto *EV = dyn_cast<ExtractValueInst>(I)) 3013 return EV->getNumIndices(); 3014 if (auto *IV = dyn_cast<InsertValueInst>(I)) 3015 return IV->getNumIndices(); 3016 if (auto *CE = dyn_cast<ConstantExpr>(I)) 3017 return CE->getIndices().size(); 3018 llvm_unreachable( 3019 "LLVMGetNumIndices applies only to extractvalue and insertvalue!"); 3020 } 3021 3022 const unsigned *LLVMGetIndices(LLVMValueRef Inst) { 3023 auto *I = unwrap(Inst); 3024 if (auto *EV = dyn_cast<ExtractValueInst>(I)) 3025 return EV->getIndices().data(); 3026 if (auto *IV = dyn_cast<InsertValueInst>(I)) 3027 return IV->getIndices().data(); 3028 if (auto *CE = dyn_cast<ConstantExpr>(I)) 3029 return CE->getIndices().data(); 3030 llvm_unreachable( 3031 "LLVMGetIndices applies only to extractvalue and insertvalue!"); 3032 } 3033 3034 3035 /*===-- Instruction builders ----------------------------------------------===*/ 3036 3037 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) { 3038 return wrap(new IRBuilder<>(*unwrap(C))); 3039 } 3040 3041 LLVMBuilderRef LLVMCreateBuilder(void) { 3042 return LLVMCreateBuilderInContext(LLVMGetGlobalContext()); 3043 } 3044 3045 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block, 3046 LLVMValueRef Instr) { 3047 BasicBlock *BB = unwrap(Block); 3048 auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end(); 3049 unwrap(Builder)->SetInsertPoint(BB, I); 3050 } 3051 3052 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) { 3053 Instruction *I = unwrap<Instruction>(Instr); 3054 unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator()); 3055 } 3056 3057 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) { 3058 BasicBlock *BB = unwrap(Block); 3059 unwrap(Builder)->SetInsertPoint(BB); 3060 } 3061 3062 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) { 3063 return wrap(unwrap(Builder)->GetInsertBlock()); 3064 } 3065 3066 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) { 3067 unwrap(Builder)->ClearInsertionPoint(); 3068 } 3069 3070 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) { 3071 unwrap(Builder)->Insert(unwrap<Instruction>(Instr)); 3072 } 3073 3074 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr, 3075 const char *Name) { 3076 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name); 3077 } 3078 3079 void LLVMDisposeBuilder(LLVMBuilderRef Builder) { 3080 delete unwrap(Builder); 3081 } 3082 3083 /*--.. Metadata builders ...................................................--*/ 3084 3085 LLVMMetadataRef LLVMGetCurrentDebugLocation2(LLVMBuilderRef Builder) { 3086 return wrap(unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()); 3087 } 3088 3089 void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Builder, LLVMMetadataRef Loc) { 3090 if (Loc) 3091 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(unwrap<MDNode>(Loc))); 3092 else 3093 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc()); 3094 } 3095 3096 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) { 3097 MDNode *Loc = 3098 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr; 3099 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc)); 3100 } 3101 3102 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) { 3103 LLVMContext &Context = unwrap(Builder)->getContext(); 3104 return wrap(MetadataAsValue::get( 3105 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode())); 3106 } 3107 3108 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) { 3109 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst)); 3110 } 3111 3112 void LLVMBuilderSetDefaultFPMathTag(LLVMBuilderRef Builder, 3113 LLVMMetadataRef FPMathTag) { 3114 3115 unwrap(Builder)->setDefaultFPMathTag(FPMathTag 3116 ? unwrap<MDNode>(FPMathTag) 3117 : nullptr); 3118 } 3119 3120 LLVMMetadataRef LLVMBuilderGetDefaultFPMathTag(LLVMBuilderRef Builder) { 3121 return wrap(unwrap(Builder)->getDefaultFPMathTag()); 3122 } 3123 3124 /*--.. Instruction builders ................................................--*/ 3125 3126 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) { 3127 return wrap(unwrap(B)->CreateRetVoid()); 3128 } 3129 3130 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) { 3131 return wrap(unwrap(B)->CreateRet(unwrap(V))); 3132 } 3133 3134 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals, 3135 unsigned N) { 3136 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N)); 3137 } 3138 3139 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) { 3140 return wrap(unwrap(B)->CreateBr(unwrap(Dest))); 3141 } 3142 3143 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If, 3144 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) { 3145 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else))); 3146 } 3147 3148 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V, 3149 LLVMBasicBlockRef Else, unsigned NumCases) { 3150 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases)); 3151 } 3152 3153 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr, 3154 unsigned NumDests) { 3155 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests)); 3156 } 3157 3158 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn, 3159 LLVMValueRef *Args, unsigned NumArgs, 3160 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch, 3161 const char *Name) { 3162 Value *V = unwrap(Fn); 3163 FunctionType *FnT = 3164 cast<FunctionType>(cast<PointerType>(V->getType())->getElementType()); 3165 3166 return wrap( 3167 unwrap(B)->CreateInvoke(FnT, unwrap(Fn), unwrap(Then), unwrap(Catch), 3168 makeArrayRef(unwrap(Args), NumArgs), Name)); 3169 } 3170 3171 LLVMValueRef LLVMBuildInvoke2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn, 3172 LLVMValueRef *Args, unsigned NumArgs, 3173 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch, 3174 const char *Name) { 3175 return wrap(unwrap(B)->CreateInvoke( 3176 unwrap<FunctionType>(Ty), unwrap(Fn), unwrap(Then), unwrap(Catch), 3177 makeArrayRef(unwrap(Args), NumArgs), Name)); 3178 } 3179 3180 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty, 3181 LLVMValueRef PersFn, unsigned NumClauses, 3182 const char *Name) { 3183 // The personality used to live on the landingpad instruction, but now it 3184 // lives on the parent function. For compatibility, take the provided 3185 // personality and put it on the parent function. 3186 if (PersFn) 3187 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn( 3188 cast<Function>(unwrap(PersFn))); 3189 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name)); 3190 } 3191 3192 LLVMValueRef LLVMBuildCatchPad(LLVMBuilderRef B, LLVMValueRef ParentPad, 3193 LLVMValueRef *Args, unsigned NumArgs, 3194 const char *Name) { 3195 return wrap(unwrap(B)->CreateCatchPad(unwrap(ParentPad), 3196 makeArrayRef(unwrap(Args), NumArgs), 3197 Name)); 3198 } 3199 3200 LLVMValueRef LLVMBuildCleanupPad(LLVMBuilderRef B, LLVMValueRef ParentPad, 3201 LLVMValueRef *Args, unsigned NumArgs, 3202 const char *Name) { 3203 if (ParentPad == nullptr) { 3204 Type *Ty = Type::getTokenTy(unwrap(B)->getContext()); 3205 ParentPad = wrap(Constant::getNullValue(Ty)); 3206 } 3207 return wrap(unwrap(B)->CreateCleanupPad(unwrap(ParentPad), 3208 makeArrayRef(unwrap(Args), NumArgs), 3209 Name)); 3210 } 3211 3212 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) { 3213 return wrap(unwrap(B)->CreateResume(unwrap(Exn))); 3214 } 3215 3216 LLVMValueRef LLVMBuildCatchSwitch(LLVMBuilderRef B, LLVMValueRef ParentPad, 3217 LLVMBasicBlockRef UnwindBB, 3218 unsigned NumHandlers, const char *Name) { 3219 if (ParentPad == nullptr) { 3220 Type *Ty = Type::getTokenTy(unwrap(B)->getContext()); 3221 ParentPad = wrap(Constant::getNullValue(Ty)); 3222 } 3223 return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(UnwindBB), 3224 NumHandlers, Name)); 3225 } 3226 3227 LLVMValueRef LLVMBuildCatchRet(LLVMBuilderRef B, LLVMValueRef CatchPad, 3228 LLVMBasicBlockRef BB) { 3229 return wrap(unwrap(B)->CreateCatchRet(unwrap<CatchPadInst>(CatchPad), 3230 unwrap(BB))); 3231 } 3232 3233 LLVMValueRef LLVMBuildCleanupRet(LLVMBuilderRef B, LLVMValueRef CatchPad, 3234 LLVMBasicBlockRef BB) { 3235 return wrap(unwrap(B)->CreateCleanupRet(unwrap<CleanupPadInst>(CatchPad), 3236 unwrap(BB))); 3237 } 3238 3239 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) { 3240 return wrap(unwrap(B)->CreateUnreachable()); 3241 } 3242 3243 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal, 3244 LLVMBasicBlockRef Dest) { 3245 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest)); 3246 } 3247 3248 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) { 3249 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest)); 3250 } 3251 3252 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) { 3253 return unwrap<LandingPadInst>(LandingPad)->getNumClauses(); 3254 } 3255 3256 LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) { 3257 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx)); 3258 } 3259 3260 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) { 3261 unwrap<LandingPadInst>(LandingPad)-> 3262 addClause(cast<Constant>(unwrap(ClauseVal))); 3263 } 3264 3265 LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) { 3266 return unwrap<LandingPadInst>(LandingPad)->isCleanup(); 3267 } 3268 3269 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) { 3270 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val); 3271 } 3272 3273 void LLVMAddHandler(LLVMValueRef CatchSwitch, LLVMBasicBlockRef Dest) { 3274 unwrap<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Dest)); 3275 } 3276 3277 unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) { 3278 return unwrap<CatchSwitchInst>(CatchSwitch)->getNumHandlers(); 3279 } 3280 3281 void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) { 3282 CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(CatchSwitch); 3283 for (const BasicBlock *H : CSI->handlers()) 3284 *Handlers++ = wrap(H); 3285 } 3286 3287 LLVMValueRef LLVMGetParentCatchSwitch(LLVMValueRef CatchPad) { 3288 return wrap(unwrap<CatchPadInst>(CatchPad)->getCatchSwitch()); 3289 } 3290 3291 void LLVMSetParentCatchSwitch(LLVMValueRef CatchPad, LLVMValueRef CatchSwitch) { 3292 unwrap<CatchPadInst>(CatchPad) 3293 ->setCatchSwitch(unwrap<CatchSwitchInst>(CatchSwitch)); 3294 } 3295 3296 /*--.. Funclets ...........................................................--*/ 3297 3298 LLVMValueRef LLVMGetArgOperand(LLVMValueRef Funclet, unsigned i) { 3299 return wrap(unwrap<FuncletPadInst>(Funclet)->getArgOperand(i)); 3300 } 3301 3302 void LLVMSetArgOperand(LLVMValueRef Funclet, unsigned i, LLVMValueRef value) { 3303 unwrap<FuncletPadInst>(Funclet)->setArgOperand(i, unwrap(value)); 3304 } 3305 3306 /*--.. Arithmetic ..........................................................--*/ 3307 3308 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3309 const char *Name) { 3310 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name)); 3311 } 3312 3313 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3314 const char *Name) { 3315 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name)); 3316 } 3317 3318 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3319 const char *Name) { 3320 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name)); 3321 } 3322 3323 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3324 const char *Name) { 3325 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name)); 3326 } 3327 3328 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3329 const char *Name) { 3330 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name)); 3331 } 3332 3333 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3334 const char *Name) { 3335 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name)); 3336 } 3337 3338 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3339 const char *Name) { 3340 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name)); 3341 } 3342 3343 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3344 const char *Name) { 3345 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name)); 3346 } 3347 3348 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3349 const char *Name) { 3350 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name)); 3351 } 3352 3353 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3354 const char *Name) { 3355 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name)); 3356 } 3357 3358 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3359 const char *Name) { 3360 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name)); 3361 } 3362 3363 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3364 const char *Name) { 3365 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name)); 3366 } 3367 3368 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3369 const char *Name) { 3370 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name)); 3371 } 3372 3373 LLVMValueRef LLVMBuildExactUDiv(LLVMBuilderRef B, LLVMValueRef LHS, 3374 LLVMValueRef RHS, const char *Name) { 3375 return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name)); 3376 } 3377 3378 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3379 const char *Name) { 3380 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name)); 3381 } 3382 3383 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS, 3384 LLVMValueRef RHS, const char *Name) { 3385 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name)); 3386 } 3387 3388 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3389 const char *Name) { 3390 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name)); 3391 } 3392 3393 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3394 const char *Name) { 3395 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name)); 3396 } 3397 3398 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3399 const char *Name) { 3400 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name)); 3401 } 3402 3403 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3404 const char *Name) { 3405 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name)); 3406 } 3407 3408 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3409 const char *Name) { 3410 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name)); 3411 } 3412 3413 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3414 const char *Name) { 3415 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name)); 3416 } 3417 3418 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3419 const char *Name) { 3420 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name)); 3421 } 3422 3423 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3424 const char *Name) { 3425 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name)); 3426 } 3427 3428 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3429 const char *Name) { 3430 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name)); 3431 } 3432 3433 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3434 const char *Name) { 3435 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name)); 3436 } 3437 3438 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op, 3439 LLVMValueRef LHS, LLVMValueRef RHS, 3440 const char *Name) { 3441 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS), 3442 unwrap(RHS), Name)); 3443 } 3444 3445 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) { 3446 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name)); 3447 } 3448 3449 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V, 3450 const char *Name) { 3451 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name)); 3452 } 3453 3454 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V, 3455 const char *Name) { 3456 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name)); 3457 } 3458 3459 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) { 3460 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name)); 3461 } 3462 3463 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) { 3464 return wrap(unwrap(B)->CreateNot(unwrap(V), Name)); 3465 } 3466 3467 /*--.. Memory ..............................................................--*/ 3468 3469 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty, 3470 const char *Name) { 3471 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext()); 3472 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty)); 3473 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy); 3474 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 3475 ITy, unwrap(Ty), AllocSize, 3476 nullptr, nullptr, ""); 3477 return wrap(unwrap(B)->Insert(Malloc, Twine(Name))); 3478 } 3479 3480 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty, 3481 LLVMValueRef Val, const char *Name) { 3482 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext()); 3483 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty)); 3484 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy); 3485 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 3486 ITy, unwrap(Ty), AllocSize, 3487 unwrap(Val), nullptr, ""); 3488 return wrap(unwrap(B)->Insert(Malloc, Twine(Name))); 3489 } 3490 3491 LLVMValueRef LLVMBuildMemSet(LLVMBuilderRef B, LLVMValueRef Ptr, 3492 LLVMValueRef Val, LLVMValueRef Len, 3493 unsigned Align) { 3494 return wrap(unwrap(B)->CreateMemSet(unwrap(Ptr), unwrap(Val), unwrap(Len), 3495 MaybeAlign(Align))); 3496 } 3497 3498 LLVMValueRef LLVMBuildMemCpy(LLVMBuilderRef B, 3499 LLVMValueRef Dst, unsigned DstAlign, 3500 LLVMValueRef Src, unsigned SrcAlign, 3501 LLVMValueRef Size) { 3502 return wrap(unwrap(B)->CreateMemCpy(unwrap(Dst), MaybeAlign(DstAlign), 3503 unwrap(Src), MaybeAlign(SrcAlign), 3504 unwrap(Size))); 3505 } 3506 3507 LLVMValueRef LLVMBuildMemMove(LLVMBuilderRef B, 3508 LLVMValueRef Dst, unsigned DstAlign, 3509 LLVMValueRef Src, unsigned SrcAlign, 3510 LLVMValueRef Size) { 3511 return wrap(unwrap(B)->CreateMemMove(unwrap(Dst), MaybeAlign(DstAlign), 3512 unwrap(Src), MaybeAlign(SrcAlign), 3513 unwrap(Size))); 3514 } 3515 3516 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty, 3517 const char *Name) { 3518 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name)); 3519 } 3520 3521 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty, 3522 LLVMValueRef Val, const char *Name) { 3523 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name)); 3524 } 3525 3526 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) { 3527 return wrap(unwrap(B)->Insert( 3528 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock()))); 3529 } 3530 3531 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal, 3532 const char *Name) { 3533 Value *V = unwrap(PointerVal); 3534 PointerType *Ty = cast<PointerType>(V->getType()); 3535 3536 return wrap(unwrap(B)->CreateLoad(Ty->getElementType(), V, Name)); 3537 } 3538 3539 LLVMValueRef LLVMBuildLoad2(LLVMBuilderRef B, LLVMTypeRef Ty, 3540 LLVMValueRef PointerVal, const char *Name) { 3541 return wrap(unwrap(B)->CreateLoad(unwrap(Ty), unwrap(PointerVal), Name)); 3542 } 3543 3544 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 3545 LLVMValueRef PointerVal) { 3546 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal))); 3547 } 3548 3549 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) { 3550 switch (Ordering) { 3551 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic; 3552 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered; 3553 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic; 3554 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire; 3555 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release; 3556 case LLVMAtomicOrderingAcquireRelease: 3557 return AtomicOrdering::AcquireRelease; 3558 case LLVMAtomicOrderingSequentiallyConsistent: 3559 return AtomicOrdering::SequentiallyConsistent; 3560 } 3561 3562 llvm_unreachable("Invalid LLVMAtomicOrdering value!"); 3563 } 3564 3565 static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) { 3566 switch (Ordering) { 3567 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic; 3568 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered; 3569 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic; 3570 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire; 3571 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease; 3572 case AtomicOrdering::AcquireRelease: 3573 return LLVMAtomicOrderingAcquireRelease; 3574 case AtomicOrdering::SequentiallyConsistent: 3575 return LLVMAtomicOrderingSequentiallyConsistent; 3576 } 3577 3578 llvm_unreachable("Invalid AtomicOrdering value!"); 3579 } 3580 3581 static AtomicRMWInst::BinOp mapFromLLVMRMWBinOp(LLVMAtomicRMWBinOp BinOp) { 3582 switch (BinOp) { 3583 case LLVMAtomicRMWBinOpXchg: return AtomicRMWInst::Xchg; 3584 case LLVMAtomicRMWBinOpAdd: return AtomicRMWInst::Add; 3585 case LLVMAtomicRMWBinOpSub: return AtomicRMWInst::Sub; 3586 case LLVMAtomicRMWBinOpAnd: return AtomicRMWInst::And; 3587 case LLVMAtomicRMWBinOpNand: return AtomicRMWInst::Nand; 3588 case LLVMAtomicRMWBinOpOr: return AtomicRMWInst::Or; 3589 case LLVMAtomicRMWBinOpXor: return AtomicRMWInst::Xor; 3590 case LLVMAtomicRMWBinOpMax: return AtomicRMWInst::Max; 3591 case LLVMAtomicRMWBinOpMin: return AtomicRMWInst::Min; 3592 case LLVMAtomicRMWBinOpUMax: return AtomicRMWInst::UMax; 3593 case LLVMAtomicRMWBinOpUMin: return AtomicRMWInst::UMin; 3594 case LLVMAtomicRMWBinOpFAdd: return AtomicRMWInst::FAdd; 3595 case LLVMAtomicRMWBinOpFSub: return AtomicRMWInst::FSub; 3596 } 3597 3598 llvm_unreachable("Invalid LLVMAtomicRMWBinOp value!"); 3599 } 3600 3601 static LLVMAtomicRMWBinOp mapToLLVMRMWBinOp(AtomicRMWInst::BinOp BinOp) { 3602 switch (BinOp) { 3603 case AtomicRMWInst::Xchg: return LLVMAtomicRMWBinOpXchg; 3604 case AtomicRMWInst::Add: return LLVMAtomicRMWBinOpAdd; 3605 case AtomicRMWInst::Sub: return LLVMAtomicRMWBinOpSub; 3606 case AtomicRMWInst::And: return LLVMAtomicRMWBinOpAnd; 3607 case AtomicRMWInst::Nand: return LLVMAtomicRMWBinOpNand; 3608 case AtomicRMWInst::Or: return LLVMAtomicRMWBinOpOr; 3609 case AtomicRMWInst::Xor: return LLVMAtomicRMWBinOpXor; 3610 case AtomicRMWInst::Max: return LLVMAtomicRMWBinOpMax; 3611 case AtomicRMWInst::Min: return LLVMAtomicRMWBinOpMin; 3612 case AtomicRMWInst::UMax: return LLVMAtomicRMWBinOpUMax; 3613 case AtomicRMWInst::UMin: return LLVMAtomicRMWBinOpUMin; 3614 case AtomicRMWInst::FAdd: return LLVMAtomicRMWBinOpFAdd; 3615 case AtomicRMWInst::FSub: return LLVMAtomicRMWBinOpFSub; 3616 default: break; 3617 } 3618 3619 llvm_unreachable("Invalid AtomicRMWBinOp value!"); 3620 } 3621 3622 // TODO: Should this and other atomic instructions support building with 3623 // "syncscope"? 3624 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering, 3625 LLVMBool isSingleThread, const char *Name) { 3626 return wrap( 3627 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering), 3628 isSingleThread ? SyncScope::SingleThread 3629 : SyncScope::System, 3630 Name)); 3631 } 3632 3633 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer, 3634 LLVMValueRef *Indices, unsigned NumIndices, 3635 const char *Name) { 3636 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices); 3637 Value *Val = unwrap(Pointer); 3638 Type *Ty = 3639 cast<PointerType>(Val->getType()->getScalarType())->getElementType(); 3640 return wrap(unwrap(B)->CreateGEP(Ty, Val, IdxList, Name)); 3641 } 3642 3643 LLVMValueRef LLVMBuildGEP2(LLVMBuilderRef B, LLVMTypeRef Ty, 3644 LLVMValueRef Pointer, LLVMValueRef *Indices, 3645 unsigned NumIndices, const char *Name) { 3646 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices); 3647 return wrap(unwrap(B)->CreateGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name)); 3648 } 3649 3650 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer, 3651 LLVMValueRef *Indices, unsigned NumIndices, 3652 const char *Name) { 3653 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices); 3654 Value *Val = unwrap(Pointer); 3655 Type *Ty = 3656 cast<PointerType>(Val->getType()->getScalarType())->getElementType(); 3657 return wrap(unwrap(B)->CreateInBoundsGEP(Ty, Val, IdxList, Name)); 3658 } 3659 3660 LLVMValueRef LLVMBuildInBoundsGEP2(LLVMBuilderRef B, LLVMTypeRef Ty, 3661 LLVMValueRef Pointer, LLVMValueRef *Indices, 3662 unsigned NumIndices, const char *Name) { 3663 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices); 3664 return wrap( 3665 unwrap(B)->CreateInBoundsGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name)); 3666 } 3667 3668 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer, 3669 unsigned Idx, const char *Name) { 3670 Value *Val = unwrap(Pointer); 3671 Type *Ty = 3672 cast<PointerType>(Val->getType()->getScalarType())->getElementType(); 3673 return wrap(unwrap(B)->CreateStructGEP(Ty, Val, Idx, Name)); 3674 } 3675 3676 LLVMValueRef LLVMBuildStructGEP2(LLVMBuilderRef B, LLVMTypeRef Ty, 3677 LLVMValueRef Pointer, unsigned Idx, 3678 const char *Name) { 3679 return wrap( 3680 unwrap(B)->CreateStructGEP(unwrap(Ty), unwrap(Pointer), Idx, Name)); 3681 } 3682 3683 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str, 3684 const char *Name) { 3685 return wrap(unwrap(B)->CreateGlobalString(Str, Name)); 3686 } 3687 3688 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str, 3689 const char *Name) { 3690 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name)); 3691 } 3692 3693 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) { 3694 Value *P = unwrap<Value>(MemAccessInst); 3695 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 3696 return LI->isVolatile(); 3697 if (StoreInst *SI = dyn_cast<StoreInst>(P)) 3698 return SI->isVolatile(); 3699 if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P)) 3700 return AI->isVolatile(); 3701 return cast<AtomicCmpXchgInst>(P)->isVolatile(); 3702 } 3703 3704 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) { 3705 Value *P = unwrap<Value>(MemAccessInst); 3706 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 3707 return LI->setVolatile(isVolatile); 3708 if (StoreInst *SI = dyn_cast<StoreInst>(P)) 3709 return SI->setVolatile(isVolatile); 3710 if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P)) 3711 return AI->setVolatile(isVolatile); 3712 return cast<AtomicCmpXchgInst>(P)->setVolatile(isVolatile); 3713 } 3714 3715 LLVMBool LLVMGetWeak(LLVMValueRef CmpXchgInst) { 3716 return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->isWeak(); 3717 } 3718 3719 void LLVMSetWeak(LLVMValueRef CmpXchgInst, LLVMBool isWeak) { 3720 return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->setWeak(isWeak); 3721 } 3722 3723 LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) { 3724 Value *P = unwrap<Value>(MemAccessInst); 3725 AtomicOrdering O; 3726 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 3727 O = LI->getOrdering(); 3728 else if (StoreInst *SI = dyn_cast<StoreInst>(P)) 3729 O = SI->getOrdering(); 3730 else 3731 O = cast<AtomicRMWInst>(P)->getOrdering(); 3732 return mapToLLVMOrdering(O); 3733 } 3734 3735 void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) { 3736 Value *P = unwrap<Value>(MemAccessInst); 3737 AtomicOrdering O = mapFromLLVMOrdering(Ordering); 3738 3739 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 3740 return LI->setOrdering(O); 3741 return cast<StoreInst>(P)->setOrdering(O); 3742 } 3743 3744 LLVMAtomicRMWBinOp LLVMGetAtomicRMWBinOp(LLVMValueRef Inst) { 3745 return mapToLLVMRMWBinOp(unwrap<AtomicRMWInst>(Inst)->getOperation()); 3746 } 3747 3748 void LLVMSetAtomicRMWBinOp(LLVMValueRef Inst, LLVMAtomicRMWBinOp BinOp) { 3749 unwrap<AtomicRMWInst>(Inst)->setOperation(mapFromLLVMRMWBinOp(BinOp)); 3750 } 3751 3752 /*--.. Casts ...............................................................--*/ 3753 3754 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val, 3755 LLVMTypeRef DestTy, const char *Name) { 3756 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name)); 3757 } 3758 3759 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val, 3760 LLVMTypeRef DestTy, const char *Name) { 3761 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name)); 3762 } 3763 3764 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val, 3765 LLVMTypeRef DestTy, const char *Name) { 3766 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name)); 3767 } 3768 3769 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val, 3770 LLVMTypeRef DestTy, const char *Name) { 3771 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name)); 3772 } 3773 3774 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val, 3775 LLVMTypeRef DestTy, const char *Name) { 3776 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name)); 3777 } 3778 3779 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val, 3780 LLVMTypeRef DestTy, const char *Name) { 3781 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name)); 3782 } 3783 3784 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val, 3785 LLVMTypeRef DestTy, const char *Name) { 3786 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name)); 3787 } 3788 3789 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val, 3790 LLVMTypeRef DestTy, const char *Name) { 3791 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name)); 3792 } 3793 3794 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val, 3795 LLVMTypeRef DestTy, const char *Name) { 3796 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name)); 3797 } 3798 3799 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val, 3800 LLVMTypeRef DestTy, const char *Name) { 3801 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name)); 3802 } 3803 3804 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val, 3805 LLVMTypeRef DestTy, const char *Name) { 3806 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name)); 3807 } 3808 3809 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val, 3810 LLVMTypeRef DestTy, const char *Name) { 3811 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name)); 3812 } 3813 3814 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val, 3815 LLVMTypeRef DestTy, const char *Name) { 3816 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name)); 3817 } 3818 3819 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val, 3820 LLVMTypeRef DestTy, const char *Name) { 3821 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy), 3822 Name)); 3823 } 3824 3825 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val, 3826 LLVMTypeRef DestTy, const char *Name) { 3827 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy), 3828 Name)); 3829 } 3830 3831 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val, 3832 LLVMTypeRef DestTy, const char *Name) { 3833 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy), 3834 Name)); 3835 } 3836 3837 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val, 3838 LLVMTypeRef DestTy, const char *Name) { 3839 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val), 3840 unwrap(DestTy), Name)); 3841 } 3842 3843 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val, 3844 LLVMTypeRef DestTy, const char *Name) { 3845 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name)); 3846 } 3847 3848 LLVMValueRef LLVMBuildIntCast2(LLVMBuilderRef B, LLVMValueRef Val, 3849 LLVMTypeRef DestTy, LLVMBool IsSigned, 3850 const char *Name) { 3851 return wrap( 3852 unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy), IsSigned, Name)); 3853 } 3854 3855 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val, 3856 LLVMTypeRef DestTy, const char *Name) { 3857 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy), 3858 /*isSigned*/true, Name)); 3859 } 3860 3861 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val, 3862 LLVMTypeRef DestTy, const char *Name) { 3863 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name)); 3864 } 3865 3866 /*--.. Comparisons .........................................................--*/ 3867 3868 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op, 3869 LLVMValueRef LHS, LLVMValueRef RHS, 3870 const char *Name) { 3871 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op), 3872 unwrap(LHS), unwrap(RHS), Name)); 3873 } 3874 3875 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op, 3876 LLVMValueRef LHS, LLVMValueRef RHS, 3877 const char *Name) { 3878 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op), 3879 unwrap(LHS), unwrap(RHS), Name)); 3880 } 3881 3882 /*--.. Miscellaneous instructions ..........................................--*/ 3883 3884 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) { 3885 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name)); 3886 } 3887 3888 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, 3889 LLVMValueRef *Args, unsigned NumArgs, 3890 const char *Name) { 3891 Value *V = unwrap(Fn); 3892 FunctionType *FnT = 3893 cast<FunctionType>(cast<PointerType>(V->getType())->getElementType()); 3894 3895 return wrap(unwrap(B)->CreateCall(FnT, unwrap(Fn), 3896 makeArrayRef(unwrap(Args), NumArgs), Name)); 3897 } 3898 3899 LLVMValueRef LLVMBuildCall2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn, 3900 LLVMValueRef *Args, unsigned NumArgs, 3901 const char *Name) { 3902 FunctionType *FTy = unwrap<FunctionType>(Ty); 3903 return wrap(unwrap(B)->CreateCall(FTy, unwrap(Fn), 3904 makeArrayRef(unwrap(Args), NumArgs), Name)); 3905 } 3906 3907 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If, 3908 LLVMValueRef Then, LLVMValueRef Else, 3909 const char *Name) { 3910 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else), 3911 Name)); 3912 } 3913 3914 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List, 3915 LLVMTypeRef Ty, const char *Name) { 3916 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name)); 3917 } 3918 3919 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal, 3920 LLVMValueRef Index, const char *Name) { 3921 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index), 3922 Name)); 3923 } 3924 3925 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal, 3926 LLVMValueRef EltVal, LLVMValueRef Index, 3927 const char *Name) { 3928 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal), 3929 unwrap(Index), Name)); 3930 } 3931 3932 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1, 3933 LLVMValueRef V2, LLVMValueRef Mask, 3934 const char *Name) { 3935 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2), 3936 unwrap(Mask), Name)); 3937 } 3938 3939 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal, 3940 unsigned Index, const char *Name) { 3941 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name)); 3942 } 3943 3944 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal, 3945 LLVMValueRef EltVal, unsigned Index, 3946 const char *Name) { 3947 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal), 3948 Index, Name)); 3949 } 3950 3951 LLVMValueRef LLVMBuildFreeze(LLVMBuilderRef B, LLVMValueRef Val, 3952 const char *Name) { 3953 return wrap(unwrap(B)->CreateFreeze(unwrap(Val), Name)); 3954 } 3955 3956 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val, 3957 const char *Name) { 3958 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name)); 3959 } 3960 3961 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val, 3962 const char *Name) { 3963 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name)); 3964 } 3965 3966 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS, 3967 LLVMValueRef RHS, const char *Name) { 3968 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name)); 3969 } 3970 3971 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op, 3972 LLVMValueRef PTR, LLVMValueRef Val, 3973 LLVMAtomicOrdering ordering, 3974 LLVMBool singleThread) { 3975 AtomicRMWInst::BinOp intop = mapFromLLVMRMWBinOp(op); 3976 return wrap(unwrap(B)->CreateAtomicRMW( 3977 intop, unwrap(PTR), unwrap(Val), MaybeAlign(), 3978 mapFromLLVMOrdering(ordering), 3979 singleThread ? SyncScope::SingleThread : SyncScope::System)); 3980 } 3981 3982 LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr, 3983 LLVMValueRef Cmp, LLVMValueRef New, 3984 LLVMAtomicOrdering SuccessOrdering, 3985 LLVMAtomicOrdering FailureOrdering, 3986 LLVMBool singleThread) { 3987 3988 return wrap(unwrap(B)->CreateAtomicCmpXchg( 3989 unwrap(Ptr), unwrap(Cmp), unwrap(New), MaybeAlign(), 3990 mapFromLLVMOrdering(SuccessOrdering), 3991 mapFromLLVMOrdering(FailureOrdering), 3992 singleThread ? SyncScope::SingleThread : SyncScope::System)); 3993 } 3994 3995 unsigned LLVMGetNumMaskElements(LLVMValueRef SVInst) { 3996 Value *P = unwrap<Value>(SVInst); 3997 ShuffleVectorInst *I = cast<ShuffleVectorInst>(P); 3998 return I->getShuffleMask().size(); 3999 } 4000 4001 int LLVMGetMaskValue(LLVMValueRef SVInst, unsigned Elt) { 4002 Value *P = unwrap<Value>(SVInst); 4003 ShuffleVectorInst *I = cast<ShuffleVectorInst>(P); 4004 return I->getMaskValue(Elt); 4005 } 4006 4007 int LLVMGetUndefMaskElem(void) { return UndefMaskElem; } 4008 4009 LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) { 4010 Value *P = unwrap<Value>(AtomicInst); 4011 4012 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P)) 4013 return I->getSyncScopeID() == SyncScope::SingleThread; 4014 return cast<AtomicCmpXchgInst>(P)->getSyncScopeID() == 4015 SyncScope::SingleThread; 4016 } 4017 4018 void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) { 4019 Value *P = unwrap<Value>(AtomicInst); 4020 SyncScope::ID SSID = NewValue ? SyncScope::SingleThread : SyncScope::System; 4021 4022 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P)) 4023 return I->setSyncScopeID(SSID); 4024 return cast<AtomicCmpXchgInst>(P)->setSyncScopeID(SSID); 4025 } 4026 4027 LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) { 4028 Value *P = unwrap<Value>(CmpXchgInst); 4029 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering()); 4030 } 4031 4032 void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst, 4033 LLVMAtomicOrdering Ordering) { 4034 Value *P = unwrap<Value>(CmpXchgInst); 4035 AtomicOrdering O = mapFromLLVMOrdering(Ordering); 4036 4037 return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O); 4038 } 4039 4040 LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst) { 4041 Value *P = unwrap<Value>(CmpXchgInst); 4042 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering()); 4043 } 4044 4045 void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst, 4046 LLVMAtomicOrdering Ordering) { 4047 Value *P = unwrap<Value>(CmpXchgInst); 4048 AtomicOrdering O = mapFromLLVMOrdering(Ordering); 4049 4050 return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O); 4051 } 4052 4053 /*===-- Module providers --------------------------------------------------===*/ 4054 4055 LLVMModuleProviderRef 4056 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) { 4057 return reinterpret_cast<LLVMModuleProviderRef>(M); 4058 } 4059 4060 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) { 4061 delete unwrap(MP); 4062 } 4063 4064 4065 /*===-- Memory buffers ----------------------------------------------------===*/ 4066 4067 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile( 4068 const char *Path, 4069 LLVMMemoryBufferRef *OutMemBuf, 4070 char **OutMessage) { 4071 4072 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path); 4073 if (std::error_code EC = MBOrErr.getError()) { 4074 *OutMessage = strdup(EC.message().c_str()); 4075 return 1; 4076 } 4077 *OutMemBuf = wrap(MBOrErr.get().release()); 4078 return 0; 4079 } 4080 4081 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf, 4082 char **OutMessage) { 4083 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN(); 4084 if (std::error_code EC = MBOrErr.getError()) { 4085 *OutMessage = strdup(EC.message().c_str()); 4086 return 1; 4087 } 4088 *OutMemBuf = wrap(MBOrErr.get().release()); 4089 return 0; 4090 } 4091 4092 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange( 4093 const char *InputData, 4094 size_t InputDataLength, 4095 const char *BufferName, 4096 LLVMBool RequiresNullTerminator) { 4097 4098 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength), 4099 StringRef(BufferName), 4100 RequiresNullTerminator).release()); 4101 } 4102 4103 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy( 4104 const char *InputData, 4105 size_t InputDataLength, 4106 const char *BufferName) { 4107 4108 return wrap( 4109 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength), 4110 StringRef(BufferName)).release()); 4111 } 4112 4113 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) { 4114 return unwrap(MemBuf)->getBufferStart(); 4115 } 4116 4117 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) { 4118 return unwrap(MemBuf)->getBufferSize(); 4119 } 4120 4121 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) { 4122 delete unwrap(MemBuf); 4123 } 4124 4125 /*===-- Pass Registry -----------------------------------------------------===*/ 4126 4127 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) { 4128 return wrap(PassRegistry::getPassRegistry()); 4129 } 4130 4131 /*===-- Pass Manager ------------------------------------------------------===*/ 4132 4133 LLVMPassManagerRef LLVMCreatePassManager() { 4134 return wrap(new legacy::PassManager()); 4135 } 4136 4137 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) { 4138 return wrap(new legacy::FunctionPassManager(unwrap(M))); 4139 } 4140 4141 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) { 4142 return LLVMCreateFunctionPassManagerForModule( 4143 reinterpret_cast<LLVMModuleRef>(P)); 4144 } 4145 4146 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) { 4147 return unwrap<legacy::PassManager>(PM)->run(*unwrap(M)); 4148 } 4149 4150 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) { 4151 return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization(); 4152 } 4153 4154 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) { 4155 return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F)); 4156 } 4157 4158 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) { 4159 return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization(); 4160 } 4161 4162 void LLVMDisposePassManager(LLVMPassManagerRef PM) { 4163 delete unwrap(PM); 4164 } 4165 4166 /*===-- Threading ------------------------------------------------------===*/ 4167 4168 LLVMBool LLVMStartMultithreaded() { 4169 return LLVMIsMultithreaded(); 4170 } 4171 4172 void LLVMStopMultithreaded() { 4173 } 4174 4175 LLVMBool LLVMIsMultithreaded() { 4176 return llvm_is_multithreaded(); 4177 } 4178