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