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