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