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