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