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