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, nullptr)); 146 } 147 148 if (AttrKind == Attribute::AttrKind::StructRet) { 149 // Same as byval. 150 return wrap(Attribute::getWithStructRetType(Ctx, nullptr)); 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 LLVMConstGEP2(LLVMTypeRef Ty, LLVMValueRef ConstantVal, 1700 LLVMValueRef *ConstantIndices, unsigned NumIndices) { 1701 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices), 1702 NumIndices); 1703 Constant *Val = unwrap<Constant>(ConstantVal); 1704 return wrap(ConstantExpr::getGetElementPtr(unwrap(Ty), Val, IdxList)); 1705 } 1706 1707 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal, 1708 LLVMValueRef *ConstantIndices, 1709 unsigned NumIndices) { 1710 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices), 1711 NumIndices); 1712 Constant *Val = unwrap<Constant>(ConstantVal); 1713 Type *Ty = 1714 cast<PointerType>(Val->getType()->getScalarType())->getElementType(); 1715 return wrap(ConstantExpr::getInBoundsGetElementPtr(Ty, Val, IdxList)); 1716 } 1717 1718 LLVMValueRef LLVMConstInBoundsGEP2(LLVMTypeRef Ty, LLVMValueRef ConstantVal, 1719 LLVMValueRef *ConstantIndices, 1720 unsigned NumIndices) { 1721 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices), 1722 NumIndices); 1723 Constant *Val = unwrap<Constant>(ConstantVal); 1724 return wrap(ConstantExpr::getInBoundsGetElementPtr(unwrap(Ty), Val, IdxList)); 1725 } 1726 1727 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1728 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal), 1729 unwrap(ToType))); 1730 } 1731 1732 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1733 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal), 1734 unwrap(ToType))); 1735 } 1736 1737 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1738 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal), 1739 unwrap(ToType))); 1740 } 1741 1742 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1743 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal), 1744 unwrap(ToType))); 1745 } 1746 1747 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1748 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal), 1749 unwrap(ToType))); 1750 } 1751 1752 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1753 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal), 1754 unwrap(ToType))); 1755 } 1756 1757 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1758 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal), 1759 unwrap(ToType))); 1760 } 1761 1762 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1763 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal), 1764 unwrap(ToType))); 1765 } 1766 1767 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1768 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal), 1769 unwrap(ToType))); 1770 } 1771 1772 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1773 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal), 1774 unwrap(ToType))); 1775 } 1776 1777 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1778 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal), 1779 unwrap(ToType))); 1780 } 1781 1782 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1783 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal), 1784 unwrap(ToType))); 1785 } 1786 1787 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal, 1788 LLVMTypeRef ToType) { 1789 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal), 1790 unwrap(ToType))); 1791 } 1792 1793 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal, 1794 LLVMTypeRef ToType) { 1795 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal), 1796 unwrap(ToType))); 1797 } 1798 1799 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal, 1800 LLVMTypeRef ToType) { 1801 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal), 1802 unwrap(ToType))); 1803 } 1804 1805 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal, 1806 LLVMTypeRef ToType) { 1807 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal), 1808 unwrap(ToType))); 1809 } 1810 1811 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal, 1812 LLVMTypeRef ToType) { 1813 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal), 1814 unwrap(ToType))); 1815 } 1816 1817 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType, 1818 LLVMBool isSigned) { 1819 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal), 1820 unwrap(ToType), isSigned)); 1821 } 1822 1823 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1824 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal), 1825 unwrap(ToType))); 1826 } 1827 1828 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition, 1829 LLVMValueRef ConstantIfTrue, 1830 LLVMValueRef ConstantIfFalse) { 1831 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition), 1832 unwrap<Constant>(ConstantIfTrue), 1833 unwrap<Constant>(ConstantIfFalse))); 1834 } 1835 1836 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant, 1837 LLVMValueRef IndexConstant) { 1838 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant), 1839 unwrap<Constant>(IndexConstant))); 1840 } 1841 1842 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant, 1843 LLVMValueRef ElementValueConstant, 1844 LLVMValueRef IndexConstant) { 1845 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant), 1846 unwrap<Constant>(ElementValueConstant), 1847 unwrap<Constant>(IndexConstant))); 1848 } 1849 1850 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant, 1851 LLVMValueRef VectorBConstant, 1852 LLVMValueRef MaskConstant) { 1853 SmallVector<int, 16> IntMask; 1854 ShuffleVectorInst::getShuffleMask(unwrap<Constant>(MaskConstant), IntMask); 1855 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant), 1856 unwrap<Constant>(VectorBConstant), 1857 IntMask)); 1858 } 1859 1860 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList, 1861 unsigned NumIdx) { 1862 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant), 1863 makeArrayRef(IdxList, NumIdx))); 1864 } 1865 1866 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant, 1867 LLVMValueRef ElementValueConstant, 1868 unsigned *IdxList, unsigned NumIdx) { 1869 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant), 1870 unwrap<Constant>(ElementValueConstant), 1871 makeArrayRef(IdxList, NumIdx))); 1872 } 1873 1874 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString, 1875 const char *Constraints, 1876 LLVMBool HasSideEffects, 1877 LLVMBool IsAlignStack) { 1878 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString, 1879 Constraints, HasSideEffects, IsAlignStack)); 1880 } 1881 1882 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) { 1883 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB))); 1884 } 1885 1886 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/ 1887 1888 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) { 1889 return wrap(unwrap<GlobalValue>(Global)->getParent()); 1890 } 1891 1892 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) { 1893 return unwrap<GlobalValue>(Global)->isDeclaration(); 1894 } 1895 1896 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) { 1897 switch (unwrap<GlobalValue>(Global)->getLinkage()) { 1898 case GlobalValue::ExternalLinkage: 1899 return LLVMExternalLinkage; 1900 case GlobalValue::AvailableExternallyLinkage: 1901 return LLVMAvailableExternallyLinkage; 1902 case GlobalValue::LinkOnceAnyLinkage: 1903 return LLVMLinkOnceAnyLinkage; 1904 case GlobalValue::LinkOnceODRLinkage: 1905 return LLVMLinkOnceODRLinkage; 1906 case GlobalValue::WeakAnyLinkage: 1907 return LLVMWeakAnyLinkage; 1908 case GlobalValue::WeakODRLinkage: 1909 return LLVMWeakODRLinkage; 1910 case GlobalValue::AppendingLinkage: 1911 return LLVMAppendingLinkage; 1912 case GlobalValue::InternalLinkage: 1913 return LLVMInternalLinkage; 1914 case GlobalValue::PrivateLinkage: 1915 return LLVMPrivateLinkage; 1916 case GlobalValue::ExternalWeakLinkage: 1917 return LLVMExternalWeakLinkage; 1918 case GlobalValue::CommonLinkage: 1919 return LLVMCommonLinkage; 1920 } 1921 1922 llvm_unreachable("Invalid GlobalValue linkage!"); 1923 } 1924 1925 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) { 1926 GlobalValue *GV = unwrap<GlobalValue>(Global); 1927 1928 switch (Linkage) { 1929 case LLVMExternalLinkage: 1930 GV->setLinkage(GlobalValue::ExternalLinkage); 1931 break; 1932 case LLVMAvailableExternallyLinkage: 1933 GV->setLinkage(GlobalValue::AvailableExternallyLinkage); 1934 break; 1935 case LLVMLinkOnceAnyLinkage: 1936 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage); 1937 break; 1938 case LLVMLinkOnceODRLinkage: 1939 GV->setLinkage(GlobalValue::LinkOnceODRLinkage); 1940 break; 1941 case LLVMLinkOnceODRAutoHideLinkage: 1942 LLVM_DEBUG( 1943 errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no " 1944 "longer supported."); 1945 break; 1946 case LLVMWeakAnyLinkage: 1947 GV->setLinkage(GlobalValue::WeakAnyLinkage); 1948 break; 1949 case LLVMWeakODRLinkage: 1950 GV->setLinkage(GlobalValue::WeakODRLinkage); 1951 break; 1952 case LLVMAppendingLinkage: 1953 GV->setLinkage(GlobalValue::AppendingLinkage); 1954 break; 1955 case LLVMInternalLinkage: 1956 GV->setLinkage(GlobalValue::InternalLinkage); 1957 break; 1958 case LLVMPrivateLinkage: 1959 GV->setLinkage(GlobalValue::PrivateLinkage); 1960 break; 1961 case LLVMLinkerPrivateLinkage: 1962 GV->setLinkage(GlobalValue::PrivateLinkage); 1963 break; 1964 case LLVMLinkerPrivateWeakLinkage: 1965 GV->setLinkage(GlobalValue::PrivateLinkage); 1966 break; 1967 case LLVMDLLImportLinkage: 1968 LLVM_DEBUG( 1969 errs() 1970 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported."); 1971 break; 1972 case LLVMDLLExportLinkage: 1973 LLVM_DEBUG( 1974 errs() 1975 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported."); 1976 break; 1977 case LLVMExternalWeakLinkage: 1978 GV->setLinkage(GlobalValue::ExternalWeakLinkage); 1979 break; 1980 case LLVMGhostLinkage: 1981 LLVM_DEBUG( 1982 errs() << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported."); 1983 break; 1984 case LLVMCommonLinkage: 1985 GV->setLinkage(GlobalValue::CommonLinkage); 1986 break; 1987 } 1988 } 1989 1990 const char *LLVMGetSection(LLVMValueRef Global) { 1991 // Using .data() is safe because of how GlobalObject::setSection is 1992 // implemented. 1993 return unwrap<GlobalValue>(Global)->getSection().data(); 1994 } 1995 1996 void LLVMSetSection(LLVMValueRef Global, const char *Section) { 1997 unwrap<GlobalObject>(Global)->setSection(Section); 1998 } 1999 2000 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) { 2001 return static_cast<LLVMVisibility>( 2002 unwrap<GlobalValue>(Global)->getVisibility()); 2003 } 2004 2005 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) { 2006 unwrap<GlobalValue>(Global) 2007 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz)); 2008 } 2009 2010 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) { 2011 return static_cast<LLVMDLLStorageClass>( 2012 unwrap<GlobalValue>(Global)->getDLLStorageClass()); 2013 } 2014 2015 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) { 2016 unwrap<GlobalValue>(Global)->setDLLStorageClass( 2017 static_cast<GlobalValue::DLLStorageClassTypes>(Class)); 2018 } 2019 2020 LLVMUnnamedAddr LLVMGetUnnamedAddress(LLVMValueRef Global) { 2021 switch (unwrap<GlobalValue>(Global)->getUnnamedAddr()) { 2022 case GlobalVariable::UnnamedAddr::None: 2023 return LLVMNoUnnamedAddr; 2024 case GlobalVariable::UnnamedAddr::Local: 2025 return LLVMLocalUnnamedAddr; 2026 case GlobalVariable::UnnamedAddr::Global: 2027 return LLVMGlobalUnnamedAddr; 2028 } 2029 llvm_unreachable("Unknown UnnamedAddr kind!"); 2030 } 2031 2032 void LLVMSetUnnamedAddress(LLVMValueRef Global, LLVMUnnamedAddr UnnamedAddr) { 2033 GlobalValue *GV = unwrap<GlobalValue>(Global); 2034 2035 switch (UnnamedAddr) { 2036 case LLVMNoUnnamedAddr: 2037 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::None); 2038 case LLVMLocalUnnamedAddr: 2039 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Local); 2040 case LLVMGlobalUnnamedAddr: 2041 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Global); 2042 } 2043 } 2044 2045 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) { 2046 return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr(); 2047 } 2048 2049 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) { 2050 unwrap<GlobalValue>(Global)->setUnnamedAddr( 2051 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global 2052 : GlobalValue::UnnamedAddr::None); 2053 } 2054 2055 LLVMTypeRef LLVMGlobalGetValueType(LLVMValueRef Global) { 2056 return wrap(unwrap<GlobalValue>(Global)->getValueType()); 2057 } 2058 2059 /*--.. Operations on global variables, load and store instructions .........--*/ 2060 2061 unsigned LLVMGetAlignment(LLVMValueRef V) { 2062 Value *P = unwrap<Value>(V); 2063 if (GlobalObject *GV = dyn_cast<GlobalObject>(P)) 2064 return GV->getAlignment(); 2065 if (AllocaInst *AI = dyn_cast<AllocaInst>(P)) 2066 return AI->getAlignment(); 2067 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 2068 return LI->getAlignment(); 2069 if (StoreInst *SI = dyn_cast<StoreInst>(P)) 2070 return SI->getAlignment(); 2071 if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P)) 2072 return RMWI->getAlign().value(); 2073 if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P)) 2074 return CXI->getAlign().value(); 2075 2076 llvm_unreachable( 2077 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, " 2078 "and AtomicCmpXchgInst have alignment"); 2079 } 2080 2081 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) { 2082 Value *P = unwrap<Value>(V); 2083 if (GlobalObject *GV = dyn_cast<GlobalObject>(P)) 2084 GV->setAlignment(MaybeAlign(Bytes)); 2085 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P)) 2086 AI->setAlignment(Align(Bytes)); 2087 else if (LoadInst *LI = dyn_cast<LoadInst>(P)) 2088 LI->setAlignment(Align(Bytes)); 2089 else if (StoreInst *SI = dyn_cast<StoreInst>(P)) 2090 SI->setAlignment(Align(Bytes)); 2091 else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P)) 2092 RMWI->setAlignment(Align(Bytes)); 2093 else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P)) 2094 CXI->setAlignment(Align(Bytes)); 2095 else 2096 llvm_unreachable( 2097 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, and " 2098 "and AtomicCmpXchgInst have alignment"); 2099 } 2100 2101 LLVMValueMetadataEntry *LLVMGlobalCopyAllMetadata(LLVMValueRef Value, 2102 size_t *NumEntries) { 2103 return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) { 2104 Entries.clear(); 2105 if (Instruction *Instr = dyn_cast<Instruction>(unwrap(Value))) { 2106 Instr->getAllMetadata(Entries); 2107 } else { 2108 unwrap<GlobalObject>(Value)->getAllMetadata(Entries); 2109 } 2110 }); 2111 } 2112 2113 unsigned LLVMValueMetadataEntriesGetKind(LLVMValueMetadataEntry *Entries, 2114 unsigned Index) { 2115 LLVMOpaqueValueMetadataEntry MVE = 2116 static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]); 2117 return MVE.Kind; 2118 } 2119 2120 LLVMMetadataRef 2121 LLVMValueMetadataEntriesGetMetadata(LLVMValueMetadataEntry *Entries, 2122 unsigned Index) { 2123 LLVMOpaqueValueMetadataEntry MVE = 2124 static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]); 2125 return MVE.Metadata; 2126 } 2127 2128 void LLVMDisposeValueMetadataEntries(LLVMValueMetadataEntry *Entries) { 2129 free(Entries); 2130 } 2131 2132 void LLVMGlobalSetMetadata(LLVMValueRef Global, unsigned Kind, 2133 LLVMMetadataRef MD) { 2134 unwrap<GlobalObject>(Global)->setMetadata(Kind, unwrap<MDNode>(MD)); 2135 } 2136 2137 void LLVMGlobalEraseMetadata(LLVMValueRef Global, unsigned Kind) { 2138 unwrap<GlobalObject>(Global)->eraseMetadata(Kind); 2139 } 2140 2141 void LLVMGlobalClearMetadata(LLVMValueRef Global) { 2142 unwrap<GlobalObject>(Global)->clearMetadata(); 2143 } 2144 2145 /*--.. Operations on global variables ......................................--*/ 2146 2147 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) { 2148 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false, 2149 GlobalValue::ExternalLinkage, nullptr, Name)); 2150 } 2151 2152 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty, 2153 const char *Name, 2154 unsigned AddressSpace) { 2155 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false, 2156 GlobalValue::ExternalLinkage, nullptr, Name, 2157 nullptr, GlobalVariable::NotThreadLocal, 2158 AddressSpace)); 2159 } 2160 2161 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) { 2162 return wrap(unwrap(M)->getNamedGlobal(Name)); 2163 } 2164 2165 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) { 2166 Module *Mod = unwrap(M); 2167 Module::global_iterator I = Mod->global_begin(); 2168 if (I == Mod->global_end()) 2169 return nullptr; 2170 return wrap(&*I); 2171 } 2172 2173 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) { 2174 Module *Mod = unwrap(M); 2175 Module::global_iterator I = Mod->global_end(); 2176 if (I == Mod->global_begin()) 2177 return nullptr; 2178 return wrap(&*--I); 2179 } 2180 2181 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) { 2182 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar); 2183 Module::global_iterator I(GV); 2184 if (++I == GV->getParent()->global_end()) 2185 return nullptr; 2186 return wrap(&*I); 2187 } 2188 2189 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) { 2190 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar); 2191 Module::global_iterator I(GV); 2192 if (I == GV->getParent()->global_begin()) 2193 return nullptr; 2194 return wrap(&*--I); 2195 } 2196 2197 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) { 2198 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent(); 2199 } 2200 2201 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) { 2202 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar); 2203 if ( !GV->hasInitializer() ) 2204 return nullptr; 2205 return wrap(GV->getInitializer()); 2206 } 2207 2208 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) { 2209 unwrap<GlobalVariable>(GlobalVar) 2210 ->setInitializer(unwrap<Constant>(ConstantVal)); 2211 } 2212 2213 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) { 2214 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal(); 2215 } 2216 2217 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) { 2218 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0); 2219 } 2220 2221 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) { 2222 return unwrap<GlobalVariable>(GlobalVar)->isConstant(); 2223 } 2224 2225 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) { 2226 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0); 2227 } 2228 2229 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) { 2230 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) { 2231 case GlobalVariable::NotThreadLocal: 2232 return LLVMNotThreadLocal; 2233 case GlobalVariable::GeneralDynamicTLSModel: 2234 return LLVMGeneralDynamicTLSModel; 2235 case GlobalVariable::LocalDynamicTLSModel: 2236 return LLVMLocalDynamicTLSModel; 2237 case GlobalVariable::InitialExecTLSModel: 2238 return LLVMInitialExecTLSModel; 2239 case GlobalVariable::LocalExecTLSModel: 2240 return LLVMLocalExecTLSModel; 2241 } 2242 2243 llvm_unreachable("Invalid GlobalVariable thread local mode"); 2244 } 2245 2246 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) { 2247 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar); 2248 2249 switch (Mode) { 2250 case LLVMNotThreadLocal: 2251 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal); 2252 break; 2253 case LLVMGeneralDynamicTLSModel: 2254 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel); 2255 break; 2256 case LLVMLocalDynamicTLSModel: 2257 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel); 2258 break; 2259 case LLVMInitialExecTLSModel: 2260 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel); 2261 break; 2262 case LLVMLocalExecTLSModel: 2263 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel); 2264 break; 2265 } 2266 } 2267 2268 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) { 2269 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized(); 2270 } 2271 2272 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) { 2273 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit); 2274 } 2275 2276 /*--.. Operations on aliases ......................................--*/ 2277 2278 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee, 2279 const char *Name) { 2280 auto *PTy = cast<PointerType>(unwrap(Ty)); 2281 return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(), 2282 GlobalValue::ExternalLinkage, Name, 2283 unwrap<Constant>(Aliasee), unwrap(M))); 2284 } 2285 2286 LLVMValueRef LLVMAddAlias2(LLVMModuleRef M, LLVMTypeRef ValueTy, 2287 unsigned AddrSpace, LLVMValueRef Aliasee, 2288 const char *Name) { 2289 return wrap(GlobalAlias::create(unwrap(ValueTy), AddrSpace, 2290 GlobalValue::ExternalLinkage, Name, 2291 unwrap<Constant>(Aliasee), unwrap(M))); 2292 } 2293 2294 LLVMValueRef LLVMGetNamedGlobalAlias(LLVMModuleRef M, 2295 const char *Name, size_t NameLen) { 2296 return wrap(unwrap(M)->getNamedAlias(StringRef(Name, NameLen))); 2297 } 2298 2299 LLVMValueRef LLVMGetFirstGlobalAlias(LLVMModuleRef M) { 2300 Module *Mod = unwrap(M); 2301 Module::alias_iterator I = Mod->alias_begin(); 2302 if (I == Mod->alias_end()) 2303 return nullptr; 2304 return wrap(&*I); 2305 } 2306 2307 LLVMValueRef LLVMGetLastGlobalAlias(LLVMModuleRef M) { 2308 Module *Mod = unwrap(M); 2309 Module::alias_iterator I = Mod->alias_end(); 2310 if (I == Mod->alias_begin()) 2311 return nullptr; 2312 return wrap(&*--I); 2313 } 2314 2315 LLVMValueRef LLVMGetNextGlobalAlias(LLVMValueRef GA) { 2316 GlobalAlias *Alias = unwrap<GlobalAlias>(GA); 2317 Module::alias_iterator I(Alias); 2318 if (++I == Alias->getParent()->alias_end()) 2319 return nullptr; 2320 return wrap(&*I); 2321 } 2322 2323 LLVMValueRef LLVMGetPreviousGlobalAlias(LLVMValueRef GA) { 2324 GlobalAlias *Alias = unwrap<GlobalAlias>(GA); 2325 Module::alias_iterator I(Alias); 2326 if (I == Alias->getParent()->alias_begin()) 2327 return nullptr; 2328 return wrap(&*--I); 2329 } 2330 2331 LLVMValueRef LLVMAliasGetAliasee(LLVMValueRef Alias) { 2332 return wrap(unwrap<GlobalAlias>(Alias)->getAliasee()); 2333 } 2334 2335 void LLVMAliasSetAliasee(LLVMValueRef Alias, LLVMValueRef Aliasee) { 2336 unwrap<GlobalAlias>(Alias)->setAliasee(unwrap<Constant>(Aliasee)); 2337 } 2338 2339 /*--.. Operations on functions .............................................--*/ 2340 2341 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name, 2342 LLVMTypeRef FunctionTy) { 2343 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy), 2344 GlobalValue::ExternalLinkage, Name, unwrap(M))); 2345 } 2346 2347 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) { 2348 return wrap(unwrap(M)->getFunction(Name)); 2349 } 2350 2351 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) { 2352 Module *Mod = unwrap(M); 2353 Module::iterator I = Mod->begin(); 2354 if (I == Mod->end()) 2355 return nullptr; 2356 return wrap(&*I); 2357 } 2358 2359 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) { 2360 Module *Mod = unwrap(M); 2361 Module::iterator I = Mod->end(); 2362 if (I == Mod->begin()) 2363 return nullptr; 2364 return wrap(&*--I); 2365 } 2366 2367 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) { 2368 Function *Func = unwrap<Function>(Fn); 2369 Module::iterator I(Func); 2370 if (++I == Func->getParent()->end()) 2371 return nullptr; 2372 return wrap(&*I); 2373 } 2374 2375 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) { 2376 Function *Func = unwrap<Function>(Fn); 2377 Module::iterator I(Func); 2378 if (I == Func->getParent()->begin()) 2379 return nullptr; 2380 return wrap(&*--I); 2381 } 2382 2383 void LLVMDeleteFunction(LLVMValueRef Fn) { 2384 unwrap<Function>(Fn)->eraseFromParent(); 2385 } 2386 2387 LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) { 2388 return unwrap<Function>(Fn)->hasPersonalityFn(); 2389 } 2390 2391 LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) { 2392 return wrap(unwrap<Function>(Fn)->getPersonalityFn()); 2393 } 2394 2395 void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) { 2396 unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn)); 2397 } 2398 2399 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) { 2400 if (Function *F = dyn_cast<Function>(unwrap(Fn))) 2401 return F->getIntrinsicID(); 2402 return 0; 2403 } 2404 2405 static Intrinsic::ID llvm_map_to_intrinsic_id(unsigned ID) { 2406 assert(ID < llvm::Intrinsic::num_intrinsics && "Intrinsic ID out of range"); 2407 return llvm::Intrinsic::ID(ID); 2408 } 2409 2410 LLVMValueRef LLVMGetIntrinsicDeclaration(LLVMModuleRef Mod, 2411 unsigned ID, 2412 LLVMTypeRef *ParamTypes, 2413 size_t ParamCount) { 2414 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount); 2415 auto IID = llvm_map_to_intrinsic_id(ID); 2416 return wrap(llvm::Intrinsic::getDeclaration(unwrap(Mod), IID, Tys)); 2417 } 2418 2419 const char *LLVMIntrinsicGetName(unsigned ID, size_t *NameLength) { 2420 auto IID = llvm_map_to_intrinsic_id(ID); 2421 auto Str = llvm::Intrinsic::getName(IID); 2422 *NameLength = Str.size(); 2423 return Str.data(); 2424 } 2425 2426 LLVMTypeRef LLVMIntrinsicGetType(LLVMContextRef Ctx, unsigned ID, 2427 LLVMTypeRef *ParamTypes, size_t ParamCount) { 2428 auto IID = llvm_map_to_intrinsic_id(ID); 2429 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount); 2430 return wrap(llvm::Intrinsic::getType(*unwrap(Ctx), IID, Tys)); 2431 } 2432 2433 const char *LLVMIntrinsicCopyOverloadedName(unsigned ID, 2434 LLVMTypeRef *ParamTypes, 2435 size_t ParamCount, 2436 size_t *NameLength) { 2437 auto IID = llvm_map_to_intrinsic_id(ID); 2438 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount); 2439 auto Str = llvm::Intrinsic::getNameNoUnnamedTypes(IID, Tys); 2440 *NameLength = Str.length(); 2441 return strdup(Str.c_str()); 2442 } 2443 2444 const char *LLVMIntrinsicCopyOverloadedName2(LLVMModuleRef Mod, unsigned ID, 2445 LLVMTypeRef *ParamTypes, 2446 size_t ParamCount, 2447 size_t *NameLength) { 2448 auto IID = llvm_map_to_intrinsic_id(ID); 2449 ArrayRef<Type *> Tys(unwrap(ParamTypes), ParamCount); 2450 auto Str = llvm::Intrinsic::getName(IID, Tys, unwrap(Mod)); 2451 *NameLength = Str.length(); 2452 return strdup(Str.c_str()); 2453 } 2454 2455 unsigned LLVMLookupIntrinsicID(const char *Name, size_t NameLen) { 2456 return Function::lookupIntrinsicID({Name, NameLen}); 2457 } 2458 2459 LLVMBool LLVMIntrinsicIsOverloaded(unsigned ID) { 2460 auto IID = llvm_map_to_intrinsic_id(ID); 2461 return llvm::Intrinsic::isOverloaded(IID); 2462 } 2463 2464 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) { 2465 return unwrap<Function>(Fn)->getCallingConv(); 2466 } 2467 2468 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) { 2469 return unwrap<Function>(Fn)->setCallingConv( 2470 static_cast<CallingConv::ID>(CC)); 2471 } 2472 2473 const char *LLVMGetGC(LLVMValueRef Fn) { 2474 Function *F = unwrap<Function>(Fn); 2475 return F->hasGC()? F->getGC().c_str() : nullptr; 2476 } 2477 2478 void LLVMSetGC(LLVMValueRef Fn, const char *GC) { 2479 Function *F = unwrap<Function>(Fn); 2480 if (GC) 2481 F->setGC(GC); 2482 else 2483 F->clearGC(); 2484 } 2485 2486 void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx, 2487 LLVMAttributeRef A) { 2488 unwrap<Function>(F)->addAttributeAtIndex(Idx, unwrap(A)); 2489 } 2490 2491 unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) { 2492 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx); 2493 return AS.getNumAttributes(); 2494 } 2495 2496 void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx, 2497 LLVMAttributeRef *Attrs) { 2498 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx); 2499 for (auto A : AS) 2500 *Attrs++ = wrap(A); 2501 } 2502 2503 LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F, 2504 LLVMAttributeIndex Idx, 2505 unsigned KindID) { 2506 return wrap(unwrap<Function>(F)->getAttributeAtIndex( 2507 Idx, (Attribute::AttrKind)KindID)); 2508 } 2509 2510 LLVMAttributeRef LLVMGetStringAttributeAtIndex(LLVMValueRef F, 2511 LLVMAttributeIndex Idx, 2512 const char *K, unsigned KLen) { 2513 return wrap( 2514 unwrap<Function>(F)->getAttributeAtIndex(Idx, StringRef(K, KLen))); 2515 } 2516 2517 void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx, 2518 unsigned KindID) { 2519 unwrap<Function>(F)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID); 2520 } 2521 2522 void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx, 2523 const char *K, unsigned KLen) { 2524 unwrap<Function>(F)->removeAttributeAtIndex(Idx, StringRef(K, KLen)); 2525 } 2526 2527 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A, 2528 const char *V) { 2529 Function *Func = unwrap<Function>(Fn); 2530 Attribute Attr = Attribute::get(Func->getContext(), A, V); 2531 Func->addFnAttr(Attr); 2532 } 2533 2534 /*--.. Operations on parameters ............................................--*/ 2535 2536 unsigned LLVMCountParams(LLVMValueRef FnRef) { 2537 // This function is strictly redundant to 2538 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef))) 2539 return unwrap<Function>(FnRef)->arg_size(); 2540 } 2541 2542 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) { 2543 Function *Fn = unwrap<Function>(FnRef); 2544 for (Argument &A : Fn->args()) 2545 *ParamRefs++ = wrap(&A); 2546 } 2547 2548 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) { 2549 Function *Fn = unwrap<Function>(FnRef); 2550 return wrap(&Fn->arg_begin()[index]); 2551 } 2552 2553 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) { 2554 return wrap(unwrap<Argument>(V)->getParent()); 2555 } 2556 2557 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) { 2558 Function *Func = unwrap<Function>(Fn); 2559 Function::arg_iterator I = Func->arg_begin(); 2560 if (I == Func->arg_end()) 2561 return nullptr; 2562 return wrap(&*I); 2563 } 2564 2565 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) { 2566 Function *Func = unwrap<Function>(Fn); 2567 Function::arg_iterator I = Func->arg_end(); 2568 if (I == Func->arg_begin()) 2569 return nullptr; 2570 return wrap(&*--I); 2571 } 2572 2573 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) { 2574 Argument *A = unwrap<Argument>(Arg); 2575 Function *Fn = A->getParent(); 2576 if (A->getArgNo() + 1 >= Fn->arg_size()) 2577 return nullptr; 2578 return wrap(&Fn->arg_begin()[A->getArgNo() + 1]); 2579 } 2580 2581 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) { 2582 Argument *A = unwrap<Argument>(Arg); 2583 if (A->getArgNo() == 0) 2584 return nullptr; 2585 return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]); 2586 } 2587 2588 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) { 2589 Argument *A = unwrap<Argument>(Arg); 2590 A->addAttr(Attribute::getWithAlignment(A->getContext(), Align(align))); 2591 } 2592 2593 /*--.. Operations on ifuncs ................................................--*/ 2594 2595 LLVMValueRef LLVMAddGlobalIFunc(LLVMModuleRef M, 2596 const char *Name, size_t NameLen, 2597 LLVMTypeRef Ty, unsigned AddrSpace, 2598 LLVMValueRef Resolver) { 2599 return wrap(GlobalIFunc::create(unwrap(Ty), AddrSpace, 2600 GlobalValue::ExternalLinkage, 2601 StringRef(Name, NameLen), 2602 unwrap<Constant>(Resolver), unwrap(M))); 2603 } 2604 2605 LLVMValueRef LLVMGetNamedGlobalIFunc(LLVMModuleRef M, 2606 const char *Name, size_t NameLen) { 2607 return wrap(unwrap(M)->getNamedIFunc(StringRef(Name, NameLen))); 2608 } 2609 2610 LLVMValueRef LLVMGetFirstGlobalIFunc(LLVMModuleRef M) { 2611 Module *Mod = unwrap(M); 2612 Module::ifunc_iterator I = Mod->ifunc_begin(); 2613 if (I == Mod->ifunc_end()) 2614 return nullptr; 2615 return wrap(&*I); 2616 } 2617 2618 LLVMValueRef LLVMGetLastGlobalIFunc(LLVMModuleRef M) { 2619 Module *Mod = unwrap(M); 2620 Module::ifunc_iterator I = Mod->ifunc_end(); 2621 if (I == Mod->ifunc_begin()) 2622 return nullptr; 2623 return wrap(&*--I); 2624 } 2625 2626 LLVMValueRef LLVMGetNextGlobalIFunc(LLVMValueRef IFunc) { 2627 GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc); 2628 Module::ifunc_iterator I(GIF); 2629 if (++I == GIF->getParent()->ifunc_end()) 2630 return nullptr; 2631 return wrap(&*I); 2632 } 2633 2634 LLVMValueRef LLVMGetPreviousGlobalIFunc(LLVMValueRef IFunc) { 2635 GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc); 2636 Module::ifunc_iterator I(GIF); 2637 if (I == GIF->getParent()->ifunc_begin()) 2638 return nullptr; 2639 return wrap(&*--I); 2640 } 2641 2642 LLVMValueRef LLVMGetGlobalIFuncResolver(LLVMValueRef IFunc) { 2643 return wrap(unwrap<GlobalIFunc>(IFunc)->getResolver()); 2644 } 2645 2646 void LLVMSetGlobalIFuncResolver(LLVMValueRef IFunc, LLVMValueRef Resolver) { 2647 unwrap<GlobalIFunc>(IFunc)->setResolver(unwrap<Constant>(Resolver)); 2648 } 2649 2650 void LLVMEraseGlobalIFunc(LLVMValueRef IFunc) { 2651 unwrap<GlobalIFunc>(IFunc)->eraseFromParent(); 2652 } 2653 2654 void LLVMRemoveGlobalIFunc(LLVMValueRef IFunc) { 2655 unwrap<GlobalIFunc>(IFunc)->removeFromParent(); 2656 } 2657 2658 /*--.. Operations on basic blocks ..........................................--*/ 2659 2660 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) { 2661 return wrap(static_cast<Value*>(unwrap(BB))); 2662 } 2663 2664 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) { 2665 return isa<BasicBlock>(unwrap(Val)); 2666 } 2667 2668 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) { 2669 return wrap(unwrap<BasicBlock>(Val)); 2670 } 2671 2672 const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) { 2673 return unwrap(BB)->getName().data(); 2674 } 2675 2676 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) { 2677 return wrap(unwrap(BB)->getParent()); 2678 } 2679 2680 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) { 2681 return wrap(unwrap(BB)->getTerminator()); 2682 } 2683 2684 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) { 2685 return unwrap<Function>(FnRef)->size(); 2686 } 2687 2688 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){ 2689 Function *Fn = unwrap<Function>(FnRef); 2690 for (BasicBlock &BB : *Fn) 2691 *BasicBlocksRefs++ = wrap(&BB); 2692 } 2693 2694 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) { 2695 return wrap(&unwrap<Function>(Fn)->getEntryBlock()); 2696 } 2697 2698 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) { 2699 Function *Func = unwrap<Function>(Fn); 2700 Function::iterator I = Func->begin(); 2701 if (I == Func->end()) 2702 return nullptr; 2703 return wrap(&*I); 2704 } 2705 2706 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) { 2707 Function *Func = unwrap<Function>(Fn); 2708 Function::iterator I = Func->end(); 2709 if (I == Func->begin()) 2710 return nullptr; 2711 return wrap(&*--I); 2712 } 2713 2714 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) { 2715 BasicBlock *Block = unwrap(BB); 2716 Function::iterator I(Block); 2717 if (++I == Block->getParent()->end()) 2718 return nullptr; 2719 return wrap(&*I); 2720 } 2721 2722 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) { 2723 BasicBlock *Block = unwrap(BB); 2724 Function::iterator I(Block); 2725 if (I == Block->getParent()->begin()) 2726 return nullptr; 2727 return wrap(&*--I); 2728 } 2729 2730 LLVMBasicBlockRef LLVMCreateBasicBlockInContext(LLVMContextRef C, 2731 const char *Name) { 2732 return wrap(llvm::BasicBlock::Create(*unwrap(C), Name)); 2733 } 2734 2735 void LLVMInsertExistingBasicBlockAfterInsertBlock(LLVMBuilderRef Builder, 2736 LLVMBasicBlockRef BB) { 2737 BasicBlock *ToInsert = unwrap(BB); 2738 BasicBlock *CurBB = unwrap(Builder)->GetInsertBlock(); 2739 assert(CurBB && "current insertion point is invalid!"); 2740 CurBB->getParent()->getBasicBlockList().insertAfter(CurBB->getIterator(), 2741 ToInsert); 2742 } 2743 2744 void LLVMAppendExistingBasicBlock(LLVMValueRef Fn, 2745 LLVMBasicBlockRef BB) { 2746 unwrap<Function>(Fn)->getBasicBlockList().push_back(unwrap(BB)); 2747 } 2748 2749 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C, 2750 LLVMValueRef FnRef, 2751 const char *Name) { 2752 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef))); 2753 } 2754 2755 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) { 2756 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name); 2757 } 2758 2759 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C, 2760 LLVMBasicBlockRef BBRef, 2761 const char *Name) { 2762 BasicBlock *BB = unwrap(BBRef); 2763 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB)); 2764 } 2765 2766 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef, 2767 const char *Name) { 2768 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name); 2769 } 2770 2771 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) { 2772 unwrap(BBRef)->eraseFromParent(); 2773 } 2774 2775 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) { 2776 unwrap(BBRef)->removeFromParent(); 2777 } 2778 2779 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) { 2780 unwrap(BB)->moveBefore(unwrap(MovePos)); 2781 } 2782 2783 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) { 2784 unwrap(BB)->moveAfter(unwrap(MovePos)); 2785 } 2786 2787 /*--.. Operations on instructions ..........................................--*/ 2788 2789 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) { 2790 return wrap(unwrap<Instruction>(Inst)->getParent()); 2791 } 2792 2793 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) { 2794 BasicBlock *Block = unwrap(BB); 2795 BasicBlock::iterator I = Block->begin(); 2796 if (I == Block->end()) 2797 return nullptr; 2798 return wrap(&*I); 2799 } 2800 2801 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) { 2802 BasicBlock *Block = unwrap(BB); 2803 BasicBlock::iterator I = Block->end(); 2804 if (I == Block->begin()) 2805 return nullptr; 2806 return wrap(&*--I); 2807 } 2808 2809 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) { 2810 Instruction *Instr = unwrap<Instruction>(Inst); 2811 BasicBlock::iterator I(Instr); 2812 if (++I == Instr->getParent()->end()) 2813 return nullptr; 2814 return wrap(&*I); 2815 } 2816 2817 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) { 2818 Instruction *Instr = unwrap<Instruction>(Inst); 2819 BasicBlock::iterator I(Instr); 2820 if (I == Instr->getParent()->begin()) 2821 return nullptr; 2822 return wrap(&*--I); 2823 } 2824 2825 void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) { 2826 unwrap<Instruction>(Inst)->removeFromParent(); 2827 } 2828 2829 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) { 2830 unwrap<Instruction>(Inst)->eraseFromParent(); 2831 } 2832 2833 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) { 2834 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst))) 2835 return (LLVMIntPredicate)I->getPredicate(); 2836 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst))) 2837 if (CE->getOpcode() == Instruction::ICmp) 2838 return (LLVMIntPredicate)CE->getPredicate(); 2839 return (LLVMIntPredicate)0; 2840 } 2841 2842 LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) { 2843 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst))) 2844 return (LLVMRealPredicate)I->getPredicate(); 2845 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst))) 2846 if (CE->getOpcode() == Instruction::FCmp) 2847 return (LLVMRealPredicate)CE->getPredicate(); 2848 return (LLVMRealPredicate)0; 2849 } 2850 2851 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) { 2852 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst))) 2853 return map_to_llvmopcode(C->getOpcode()); 2854 return (LLVMOpcode)0; 2855 } 2856 2857 LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) { 2858 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst))) 2859 return wrap(C->clone()); 2860 return nullptr; 2861 } 2862 2863 LLVMValueRef LLVMIsATerminatorInst(LLVMValueRef Inst) { 2864 Instruction *I = dyn_cast<Instruction>(unwrap(Inst)); 2865 return (I && I->isTerminator()) ? wrap(I) : nullptr; 2866 } 2867 2868 unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) { 2869 if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) { 2870 return FPI->getNumArgOperands(); 2871 } 2872 return unwrap<CallBase>(Instr)->arg_size(); 2873 } 2874 2875 /*--.. Call and invoke instructions ........................................--*/ 2876 2877 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) { 2878 return unwrap<CallBase>(Instr)->getCallingConv(); 2879 } 2880 2881 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) { 2882 return unwrap<CallBase>(Instr)->setCallingConv( 2883 static_cast<CallingConv::ID>(CC)); 2884 } 2885 2886 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, LLVMAttributeIndex Idx, 2887 unsigned align) { 2888 auto *Call = unwrap<CallBase>(Instr); 2889 Attribute AlignAttr = 2890 Attribute::getWithAlignment(Call->getContext(), Align(align)); 2891 Call->addAttributeAtIndex(Idx, AlignAttr); 2892 } 2893 2894 void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx, 2895 LLVMAttributeRef A) { 2896 unwrap<CallBase>(C)->addAttributeAtIndex(Idx, unwrap(A)); 2897 } 2898 2899 unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C, 2900 LLVMAttributeIndex Idx) { 2901 auto *Call = unwrap<CallBase>(C); 2902 auto AS = Call->getAttributes().getAttributes(Idx); 2903 return AS.getNumAttributes(); 2904 } 2905 2906 void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx, 2907 LLVMAttributeRef *Attrs) { 2908 auto *Call = unwrap<CallBase>(C); 2909 auto AS = Call->getAttributes().getAttributes(Idx); 2910 for (auto A : AS) 2911 *Attrs++ = wrap(A); 2912 } 2913 2914 LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C, 2915 LLVMAttributeIndex Idx, 2916 unsigned KindID) { 2917 return wrap(unwrap<CallBase>(C)->getAttributeAtIndex( 2918 Idx, (Attribute::AttrKind)KindID)); 2919 } 2920 2921 LLVMAttributeRef LLVMGetCallSiteStringAttribute(LLVMValueRef C, 2922 LLVMAttributeIndex Idx, 2923 const char *K, unsigned KLen) { 2924 return wrap( 2925 unwrap<CallBase>(C)->getAttributeAtIndex(Idx, StringRef(K, KLen))); 2926 } 2927 2928 void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C, LLVMAttributeIndex Idx, 2929 unsigned KindID) { 2930 unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID); 2931 } 2932 2933 void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C, LLVMAttributeIndex Idx, 2934 const char *K, unsigned KLen) { 2935 unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, StringRef(K, KLen)); 2936 } 2937 2938 LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) { 2939 return wrap(unwrap<CallBase>(Instr)->getCalledOperand()); 2940 } 2941 2942 LLVMTypeRef LLVMGetCalledFunctionType(LLVMValueRef Instr) { 2943 return wrap(unwrap<CallBase>(Instr)->getFunctionType()); 2944 } 2945 2946 /*--.. Operations on call instructions (only) ..............................--*/ 2947 2948 LLVMBool LLVMIsTailCall(LLVMValueRef Call) { 2949 return unwrap<CallInst>(Call)->isTailCall(); 2950 } 2951 2952 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) { 2953 unwrap<CallInst>(Call)->setTailCall(isTailCall); 2954 } 2955 2956 /*--.. Operations on invoke instructions (only) ............................--*/ 2957 2958 LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) { 2959 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest()); 2960 } 2961 2962 LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) { 2963 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) { 2964 return wrap(CRI->getUnwindDest()); 2965 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) { 2966 return wrap(CSI->getUnwindDest()); 2967 } 2968 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest()); 2969 } 2970 2971 void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) { 2972 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B)); 2973 } 2974 2975 void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) { 2976 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) { 2977 return CRI->setUnwindDest(unwrap(B)); 2978 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) { 2979 return CSI->setUnwindDest(unwrap(B)); 2980 } 2981 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B)); 2982 } 2983 2984 /*--.. Operations on terminators ...........................................--*/ 2985 2986 unsigned LLVMGetNumSuccessors(LLVMValueRef Term) { 2987 return unwrap<Instruction>(Term)->getNumSuccessors(); 2988 } 2989 2990 LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) { 2991 return wrap(unwrap<Instruction>(Term)->getSuccessor(i)); 2992 } 2993 2994 void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) { 2995 return unwrap<Instruction>(Term)->setSuccessor(i, unwrap(block)); 2996 } 2997 2998 /*--.. Operations on branch instructions (only) ............................--*/ 2999 3000 LLVMBool LLVMIsConditional(LLVMValueRef Branch) { 3001 return unwrap<BranchInst>(Branch)->isConditional(); 3002 } 3003 3004 LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) { 3005 return wrap(unwrap<BranchInst>(Branch)->getCondition()); 3006 } 3007 3008 void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) { 3009 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond)); 3010 } 3011 3012 /*--.. Operations on switch instructions (only) ............................--*/ 3013 3014 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) { 3015 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest()); 3016 } 3017 3018 /*--.. Operations on alloca instructions (only) ............................--*/ 3019 3020 LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) { 3021 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType()); 3022 } 3023 3024 /*--.. Operations on gep instructions (only) ...............................--*/ 3025 3026 LLVMBool LLVMIsInBounds(LLVMValueRef GEP) { 3027 return unwrap<GEPOperator>(GEP)->isInBounds(); 3028 } 3029 3030 void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) { 3031 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds); 3032 } 3033 3034 LLVMTypeRef LLVMGetGEPSourceElementType(LLVMValueRef GEP) { 3035 return wrap(unwrap<GEPOperator>(GEP)->getSourceElementType()); 3036 } 3037 3038 /*--.. Operations on phi nodes .............................................--*/ 3039 3040 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues, 3041 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) { 3042 PHINode *PhiVal = unwrap<PHINode>(PhiNode); 3043 for (unsigned I = 0; I != Count; ++I) 3044 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I])); 3045 } 3046 3047 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) { 3048 return unwrap<PHINode>(PhiNode)->getNumIncomingValues(); 3049 } 3050 3051 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) { 3052 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index)); 3053 } 3054 3055 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) { 3056 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index)); 3057 } 3058 3059 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/ 3060 3061 unsigned LLVMGetNumIndices(LLVMValueRef Inst) { 3062 auto *I = unwrap(Inst); 3063 if (auto *GEP = dyn_cast<GEPOperator>(I)) 3064 return GEP->getNumIndices(); 3065 if (auto *EV = dyn_cast<ExtractValueInst>(I)) 3066 return EV->getNumIndices(); 3067 if (auto *IV = dyn_cast<InsertValueInst>(I)) 3068 return IV->getNumIndices(); 3069 if (auto *CE = dyn_cast<ConstantExpr>(I)) 3070 return CE->getIndices().size(); 3071 llvm_unreachable( 3072 "LLVMGetNumIndices applies only to extractvalue and insertvalue!"); 3073 } 3074 3075 const unsigned *LLVMGetIndices(LLVMValueRef Inst) { 3076 auto *I = unwrap(Inst); 3077 if (auto *EV = dyn_cast<ExtractValueInst>(I)) 3078 return EV->getIndices().data(); 3079 if (auto *IV = dyn_cast<InsertValueInst>(I)) 3080 return IV->getIndices().data(); 3081 if (auto *CE = dyn_cast<ConstantExpr>(I)) 3082 return CE->getIndices().data(); 3083 llvm_unreachable( 3084 "LLVMGetIndices applies only to extractvalue and insertvalue!"); 3085 } 3086 3087 3088 /*===-- Instruction builders ----------------------------------------------===*/ 3089 3090 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) { 3091 return wrap(new IRBuilder<>(*unwrap(C))); 3092 } 3093 3094 LLVMBuilderRef LLVMCreateBuilder(void) { 3095 return LLVMCreateBuilderInContext(LLVMGetGlobalContext()); 3096 } 3097 3098 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block, 3099 LLVMValueRef Instr) { 3100 BasicBlock *BB = unwrap(Block); 3101 auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end(); 3102 unwrap(Builder)->SetInsertPoint(BB, I); 3103 } 3104 3105 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) { 3106 Instruction *I = unwrap<Instruction>(Instr); 3107 unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator()); 3108 } 3109 3110 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) { 3111 BasicBlock *BB = unwrap(Block); 3112 unwrap(Builder)->SetInsertPoint(BB); 3113 } 3114 3115 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) { 3116 return wrap(unwrap(Builder)->GetInsertBlock()); 3117 } 3118 3119 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) { 3120 unwrap(Builder)->ClearInsertionPoint(); 3121 } 3122 3123 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) { 3124 unwrap(Builder)->Insert(unwrap<Instruction>(Instr)); 3125 } 3126 3127 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr, 3128 const char *Name) { 3129 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name); 3130 } 3131 3132 void LLVMDisposeBuilder(LLVMBuilderRef Builder) { 3133 delete unwrap(Builder); 3134 } 3135 3136 /*--.. Metadata builders ...................................................--*/ 3137 3138 LLVMMetadataRef LLVMGetCurrentDebugLocation2(LLVMBuilderRef Builder) { 3139 return wrap(unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()); 3140 } 3141 3142 void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Builder, LLVMMetadataRef Loc) { 3143 if (Loc) 3144 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(unwrap<MDNode>(Loc))); 3145 else 3146 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc()); 3147 } 3148 3149 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) { 3150 MDNode *Loc = 3151 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr; 3152 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc)); 3153 } 3154 3155 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) { 3156 LLVMContext &Context = unwrap(Builder)->getContext(); 3157 return wrap(MetadataAsValue::get( 3158 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode())); 3159 } 3160 3161 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) { 3162 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst)); 3163 } 3164 3165 void LLVMAddMetadataToInst(LLVMBuilderRef Builder, LLVMValueRef Inst) { 3166 unwrap(Builder)->AddMetadataToInst(unwrap<Instruction>(Inst)); 3167 } 3168 3169 void LLVMBuilderSetDefaultFPMathTag(LLVMBuilderRef Builder, 3170 LLVMMetadataRef FPMathTag) { 3171 3172 unwrap(Builder)->setDefaultFPMathTag(FPMathTag 3173 ? unwrap<MDNode>(FPMathTag) 3174 : nullptr); 3175 } 3176 3177 LLVMMetadataRef LLVMBuilderGetDefaultFPMathTag(LLVMBuilderRef Builder) { 3178 return wrap(unwrap(Builder)->getDefaultFPMathTag()); 3179 } 3180 3181 /*--.. Instruction builders ................................................--*/ 3182 3183 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) { 3184 return wrap(unwrap(B)->CreateRetVoid()); 3185 } 3186 3187 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) { 3188 return wrap(unwrap(B)->CreateRet(unwrap(V))); 3189 } 3190 3191 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals, 3192 unsigned N) { 3193 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N)); 3194 } 3195 3196 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) { 3197 return wrap(unwrap(B)->CreateBr(unwrap(Dest))); 3198 } 3199 3200 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If, 3201 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) { 3202 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else))); 3203 } 3204 3205 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V, 3206 LLVMBasicBlockRef Else, unsigned NumCases) { 3207 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases)); 3208 } 3209 3210 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr, 3211 unsigned NumDests) { 3212 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests)); 3213 } 3214 3215 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn, 3216 LLVMValueRef *Args, unsigned NumArgs, 3217 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch, 3218 const char *Name) { 3219 Value *V = unwrap(Fn); 3220 FunctionType *FnT = 3221 cast<FunctionType>(cast<PointerType>(V->getType())->getElementType()); 3222 3223 return wrap( 3224 unwrap(B)->CreateInvoke(FnT, unwrap(Fn), unwrap(Then), unwrap(Catch), 3225 makeArrayRef(unwrap(Args), NumArgs), Name)); 3226 } 3227 3228 LLVMValueRef LLVMBuildInvoke2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn, 3229 LLVMValueRef *Args, unsigned NumArgs, 3230 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch, 3231 const char *Name) { 3232 return wrap(unwrap(B)->CreateInvoke( 3233 unwrap<FunctionType>(Ty), unwrap(Fn), unwrap(Then), unwrap(Catch), 3234 makeArrayRef(unwrap(Args), NumArgs), Name)); 3235 } 3236 3237 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty, 3238 LLVMValueRef PersFn, unsigned NumClauses, 3239 const char *Name) { 3240 // The personality used to live on the landingpad instruction, but now it 3241 // lives on the parent function. For compatibility, take the provided 3242 // personality and put it on the parent function. 3243 if (PersFn) 3244 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn( 3245 cast<Function>(unwrap(PersFn))); 3246 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name)); 3247 } 3248 3249 LLVMValueRef LLVMBuildCatchPad(LLVMBuilderRef B, LLVMValueRef ParentPad, 3250 LLVMValueRef *Args, unsigned NumArgs, 3251 const char *Name) { 3252 return wrap(unwrap(B)->CreateCatchPad(unwrap(ParentPad), 3253 makeArrayRef(unwrap(Args), NumArgs), 3254 Name)); 3255 } 3256 3257 LLVMValueRef LLVMBuildCleanupPad(LLVMBuilderRef B, LLVMValueRef ParentPad, 3258 LLVMValueRef *Args, unsigned NumArgs, 3259 const char *Name) { 3260 if (ParentPad == nullptr) { 3261 Type *Ty = Type::getTokenTy(unwrap(B)->getContext()); 3262 ParentPad = wrap(Constant::getNullValue(Ty)); 3263 } 3264 return wrap(unwrap(B)->CreateCleanupPad(unwrap(ParentPad), 3265 makeArrayRef(unwrap(Args), NumArgs), 3266 Name)); 3267 } 3268 3269 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) { 3270 return wrap(unwrap(B)->CreateResume(unwrap(Exn))); 3271 } 3272 3273 LLVMValueRef LLVMBuildCatchSwitch(LLVMBuilderRef B, LLVMValueRef ParentPad, 3274 LLVMBasicBlockRef UnwindBB, 3275 unsigned NumHandlers, const char *Name) { 3276 if (ParentPad == nullptr) { 3277 Type *Ty = Type::getTokenTy(unwrap(B)->getContext()); 3278 ParentPad = wrap(Constant::getNullValue(Ty)); 3279 } 3280 return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(UnwindBB), 3281 NumHandlers, Name)); 3282 } 3283 3284 LLVMValueRef LLVMBuildCatchRet(LLVMBuilderRef B, LLVMValueRef CatchPad, 3285 LLVMBasicBlockRef BB) { 3286 return wrap(unwrap(B)->CreateCatchRet(unwrap<CatchPadInst>(CatchPad), 3287 unwrap(BB))); 3288 } 3289 3290 LLVMValueRef LLVMBuildCleanupRet(LLVMBuilderRef B, LLVMValueRef CatchPad, 3291 LLVMBasicBlockRef BB) { 3292 return wrap(unwrap(B)->CreateCleanupRet(unwrap<CleanupPadInst>(CatchPad), 3293 unwrap(BB))); 3294 } 3295 3296 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) { 3297 return wrap(unwrap(B)->CreateUnreachable()); 3298 } 3299 3300 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal, 3301 LLVMBasicBlockRef Dest) { 3302 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest)); 3303 } 3304 3305 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) { 3306 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest)); 3307 } 3308 3309 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) { 3310 return unwrap<LandingPadInst>(LandingPad)->getNumClauses(); 3311 } 3312 3313 LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) { 3314 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx)); 3315 } 3316 3317 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) { 3318 unwrap<LandingPadInst>(LandingPad)-> 3319 addClause(cast<Constant>(unwrap(ClauseVal))); 3320 } 3321 3322 LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) { 3323 return unwrap<LandingPadInst>(LandingPad)->isCleanup(); 3324 } 3325 3326 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) { 3327 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val); 3328 } 3329 3330 void LLVMAddHandler(LLVMValueRef CatchSwitch, LLVMBasicBlockRef Dest) { 3331 unwrap<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Dest)); 3332 } 3333 3334 unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) { 3335 return unwrap<CatchSwitchInst>(CatchSwitch)->getNumHandlers(); 3336 } 3337 3338 void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) { 3339 CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(CatchSwitch); 3340 for (const BasicBlock *H : CSI->handlers()) 3341 *Handlers++ = wrap(H); 3342 } 3343 3344 LLVMValueRef LLVMGetParentCatchSwitch(LLVMValueRef CatchPad) { 3345 return wrap(unwrap<CatchPadInst>(CatchPad)->getCatchSwitch()); 3346 } 3347 3348 void LLVMSetParentCatchSwitch(LLVMValueRef CatchPad, LLVMValueRef CatchSwitch) { 3349 unwrap<CatchPadInst>(CatchPad) 3350 ->setCatchSwitch(unwrap<CatchSwitchInst>(CatchSwitch)); 3351 } 3352 3353 /*--.. Funclets ...........................................................--*/ 3354 3355 LLVMValueRef LLVMGetArgOperand(LLVMValueRef Funclet, unsigned i) { 3356 return wrap(unwrap<FuncletPadInst>(Funclet)->getArgOperand(i)); 3357 } 3358 3359 void LLVMSetArgOperand(LLVMValueRef Funclet, unsigned i, LLVMValueRef value) { 3360 unwrap<FuncletPadInst>(Funclet)->setArgOperand(i, unwrap(value)); 3361 } 3362 3363 /*--.. Arithmetic ..........................................................--*/ 3364 3365 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3366 const char *Name) { 3367 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name)); 3368 } 3369 3370 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3371 const char *Name) { 3372 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name)); 3373 } 3374 3375 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3376 const char *Name) { 3377 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name)); 3378 } 3379 3380 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3381 const char *Name) { 3382 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name)); 3383 } 3384 3385 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3386 const char *Name) { 3387 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name)); 3388 } 3389 3390 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3391 const char *Name) { 3392 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name)); 3393 } 3394 3395 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3396 const char *Name) { 3397 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name)); 3398 } 3399 3400 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3401 const char *Name) { 3402 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name)); 3403 } 3404 3405 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3406 const char *Name) { 3407 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name)); 3408 } 3409 3410 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3411 const char *Name) { 3412 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name)); 3413 } 3414 3415 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3416 const char *Name) { 3417 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name)); 3418 } 3419 3420 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3421 const char *Name) { 3422 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name)); 3423 } 3424 3425 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3426 const char *Name) { 3427 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name)); 3428 } 3429 3430 LLVMValueRef LLVMBuildExactUDiv(LLVMBuilderRef B, LLVMValueRef LHS, 3431 LLVMValueRef RHS, const char *Name) { 3432 return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name)); 3433 } 3434 3435 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3436 const char *Name) { 3437 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name)); 3438 } 3439 3440 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS, 3441 LLVMValueRef RHS, const char *Name) { 3442 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name)); 3443 } 3444 3445 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3446 const char *Name) { 3447 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name)); 3448 } 3449 3450 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3451 const char *Name) { 3452 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name)); 3453 } 3454 3455 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3456 const char *Name) { 3457 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name)); 3458 } 3459 3460 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3461 const char *Name) { 3462 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name)); 3463 } 3464 3465 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3466 const char *Name) { 3467 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name)); 3468 } 3469 3470 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3471 const char *Name) { 3472 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name)); 3473 } 3474 3475 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3476 const char *Name) { 3477 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name)); 3478 } 3479 3480 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3481 const char *Name) { 3482 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name)); 3483 } 3484 3485 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3486 const char *Name) { 3487 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name)); 3488 } 3489 3490 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 3491 const char *Name) { 3492 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name)); 3493 } 3494 3495 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op, 3496 LLVMValueRef LHS, LLVMValueRef RHS, 3497 const char *Name) { 3498 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS), 3499 unwrap(RHS), Name)); 3500 } 3501 3502 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) { 3503 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name)); 3504 } 3505 3506 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V, 3507 const char *Name) { 3508 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name)); 3509 } 3510 3511 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V, 3512 const char *Name) { 3513 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name)); 3514 } 3515 3516 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) { 3517 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name)); 3518 } 3519 3520 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) { 3521 return wrap(unwrap(B)->CreateNot(unwrap(V), Name)); 3522 } 3523 3524 /*--.. Memory ..............................................................--*/ 3525 3526 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty, 3527 const char *Name) { 3528 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext()); 3529 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty)); 3530 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy); 3531 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 3532 ITy, unwrap(Ty), AllocSize, 3533 nullptr, nullptr, ""); 3534 return wrap(unwrap(B)->Insert(Malloc, Twine(Name))); 3535 } 3536 3537 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty, 3538 LLVMValueRef Val, const char *Name) { 3539 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext()); 3540 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty)); 3541 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy); 3542 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 3543 ITy, unwrap(Ty), AllocSize, 3544 unwrap(Val), nullptr, ""); 3545 return wrap(unwrap(B)->Insert(Malloc, Twine(Name))); 3546 } 3547 3548 LLVMValueRef LLVMBuildMemSet(LLVMBuilderRef B, LLVMValueRef Ptr, 3549 LLVMValueRef Val, LLVMValueRef Len, 3550 unsigned Align) { 3551 return wrap(unwrap(B)->CreateMemSet(unwrap(Ptr), unwrap(Val), unwrap(Len), 3552 MaybeAlign(Align))); 3553 } 3554 3555 LLVMValueRef LLVMBuildMemCpy(LLVMBuilderRef B, 3556 LLVMValueRef Dst, unsigned DstAlign, 3557 LLVMValueRef Src, unsigned SrcAlign, 3558 LLVMValueRef Size) { 3559 return wrap(unwrap(B)->CreateMemCpy(unwrap(Dst), MaybeAlign(DstAlign), 3560 unwrap(Src), MaybeAlign(SrcAlign), 3561 unwrap(Size))); 3562 } 3563 3564 LLVMValueRef LLVMBuildMemMove(LLVMBuilderRef B, 3565 LLVMValueRef Dst, unsigned DstAlign, 3566 LLVMValueRef Src, unsigned SrcAlign, 3567 LLVMValueRef Size) { 3568 return wrap(unwrap(B)->CreateMemMove(unwrap(Dst), MaybeAlign(DstAlign), 3569 unwrap(Src), MaybeAlign(SrcAlign), 3570 unwrap(Size))); 3571 } 3572 3573 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty, 3574 const char *Name) { 3575 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name)); 3576 } 3577 3578 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty, 3579 LLVMValueRef Val, const char *Name) { 3580 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name)); 3581 } 3582 3583 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) { 3584 return wrap(unwrap(B)->Insert( 3585 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock()))); 3586 } 3587 3588 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal, 3589 const char *Name) { 3590 Value *V = unwrap(PointerVal); 3591 PointerType *Ty = cast<PointerType>(V->getType()); 3592 3593 return wrap(unwrap(B)->CreateLoad(Ty->getElementType(), V, Name)); 3594 } 3595 3596 LLVMValueRef LLVMBuildLoad2(LLVMBuilderRef B, LLVMTypeRef Ty, 3597 LLVMValueRef PointerVal, const char *Name) { 3598 return wrap(unwrap(B)->CreateLoad(unwrap(Ty), unwrap(PointerVal), Name)); 3599 } 3600 3601 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 3602 LLVMValueRef PointerVal) { 3603 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal))); 3604 } 3605 3606 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) { 3607 switch (Ordering) { 3608 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic; 3609 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered; 3610 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic; 3611 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire; 3612 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release; 3613 case LLVMAtomicOrderingAcquireRelease: 3614 return AtomicOrdering::AcquireRelease; 3615 case LLVMAtomicOrderingSequentiallyConsistent: 3616 return AtomicOrdering::SequentiallyConsistent; 3617 } 3618 3619 llvm_unreachable("Invalid LLVMAtomicOrdering value!"); 3620 } 3621 3622 static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) { 3623 switch (Ordering) { 3624 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic; 3625 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered; 3626 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic; 3627 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire; 3628 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease; 3629 case AtomicOrdering::AcquireRelease: 3630 return LLVMAtomicOrderingAcquireRelease; 3631 case AtomicOrdering::SequentiallyConsistent: 3632 return LLVMAtomicOrderingSequentiallyConsistent; 3633 } 3634 3635 llvm_unreachable("Invalid AtomicOrdering value!"); 3636 } 3637 3638 static AtomicRMWInst::BinOp mapFromLLVMRMWBinOp(LLVMAtomicRMWBinOp BinOp) { 3639 switch (BinOp) { 3640 case LLVMAtomicRMWBinOpXchg: return AtomicRMWInst::Xchg; 3641 case LLVMAtomicRMWBinOpAdd: return AtomicRMWInst::Add; 3642 case LLVMAtomicRMWBinOpSub: return AtomicRMWInst::Sub; 3643 case LLVMAtomicRMWBinOpAnd: return AtomicRMWInst::And; 3644 case LLVMAtomicRMWBinOpNand: return AtomicRMWInst::Nand; 3645 case LLVMAtomicRMWBinOpOr: return AtomicRMWInst::Or; 3646 case LLVMAtomicRMWBinOpXor: return AtomicRMWInst::Xor; 3647 case LLVMAtomicRMWBinOpMax: return AtomicRMWInst::Max; 3648 case LLVMAtomicRMWBinOpMin: return AtomicRMWInst::Min; 3649 case LLVMAtomicRMWBinOpUMax: return AtomicRMWInst::UMax; 3650 case LLVMAtomicRMWBinOpUMin: return AtomicRMWInst::UMin; 3651 case LLVMAtomicRMWBinOpFAdd: return AtomicRMWInst::FAdd; 3652 case LLVMAtomicRMWBinOpFSub: return AtomicRMWInst::FSub; 3653 } 3654 3655 llvm_unreachable("Invalid LLVMAtomicRMWBinOp value!"); 3656 } 3657 3658 static LLVMAtomicRMWBinOp mapToLLVMRMWBinOp(AtomicRMWInst::BinOp BinOp) { 3659 switch (BinOp) { 3660 case AtomicRMWInst::Xchg: return LLVMAtomicRMWBinOpXchg; 3661 case AtomicRMWInst::Add: return LLVMAtomicRMWBinOpAdd; 3662 case AtomicRMWInst::Sub: return LLVMAtomicRMWBinOpSub; 3663 case AtomicRMWInst::And: return LLVMAtomicRMWBinOpAnd; 3664 case AtomicRMWInst::Nand: return LLVMAtomicRMWBinOpNand; 3665 case AtomicRMWInst::Or: return LLVMAtomicRMWBinOpOr; 3666 case AtomicRMWInst::Xor: return LLVMAtomicRMWBinOpXor; 3667 case AtomicRMWInst::Max: return LLVMAtomicRMWBinOpMax; 3668 case AtomicRMWInst::Min: return LLVMAtomicRMWBinOpMin; 3669 case AtomicRMWInst::UMax: return LLVMAtomicRMWBinOpUMax; 3670 case AtomicRMWInst::UMin: return LLVMAtomicRMWBinOpUMin; 3671 case AtomicRMWInst::FAdd: return LLVMAtomicRMWBinOpFAdd; 3672 case AtomicRMWInst::FSub: return LLVMAtomicRMWBinOpFSub; 3673 default: break; 3674 } 3675 3676 llvm_unreachable("Invalid AtomicRMWBinOp value!"); 3677 } 3678 3679 // TODO: Should this and other atomic instructions support building with 3680 // "syncscope"? 3681 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering, 3682 LLVMBool isSingleThread, const char *Name) { 3683 return wrap( 3684 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering), 3685 isSingleThread ? SyncScope::SingleThread 3686 : SyncScope::System, 3687 Name)); 3688 } 3689 3690 LLVMValueRef LLVMBuildGEP(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)->CreateGEP(Ty, Val, IdxList, Name)); 3698 } 3699 3700 LLVMValueRef LLVMBuildGEP2(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(unwrap(B)->CreateGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name)); 3705 } 3706 3707 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer, 3708 LLVMValueRef *Indices, unsigned NumIndices, 3709 const char *Name) { 3710 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices); 3711 Value *Val = unwrap(Pointer); 3712 Type *Ty = 3713 cast<PointerType>(Val->getType()->getScalarType())->getElementType(); 3714 return wrap(unwrap(B)->CreateInBoundsGEP(Ty, Val, IdxList, Name)); 3715 } 3716 3717 LLVMValueRef LLVMBuildInBoundsGEP2(LLVMBuilderRef B, LLVMTypeRef Ty, 3718 LLVMValueRef Pointer, LLVMValueRef *Indices, 3719 unsigned NumIndices, const char *Name) { 3720 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices); 3721 return wrap( 3722 unwrap(B)->CreateInBoundsGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name)); 3723 } 3724 3725 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer, 3726 unsigned Idx, const char *Name) { 3727 Value *Val = unwrap(Pointer); 3728 Type *Ty = 3729 cast<PointerType>(Val->getType()->getScalarType())->getElementType(); 3730 return wrap(unwrap(B)->CreateStructGEP(Ty, Val, Idx, Name)); 3731 } 3732 3733 LLVMValueRef LLVMBuildStructGEP2(LLVMBuilderRef B, LLVMTypeRef Ty, 3734 LLVMValueRef Pointer, unsigned Idx, 3735 const char *Name) { 3736 return wrap( 3737 unwrap(B)->CreateStructGEP(unwrap(Ty), unwrap(Pointer), Idx, Name)); 3738 } 3739 3740 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str, 3741 const char *Name) { 3742 return wrap(unwrap(B)->CreateGlobalString(Str, Name)); 3743 } 3744 3745 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str, 3746 const char *Name) { 3747 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name)); 3748 } 3749 3750 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) { 3751 Value *P = unwrap<Value>(MemAccessInst); 3752 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 3753 return LI->isVolatile(); 3754 if (StoreInst *SI = dyn_cast<StoreInst>(P)) 3755 return SI->isVolatile(); 3756 if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P)) 3757 return AI->isVolatile(); 3758 return cast<AtomicCmpXchgInst>(P)->isVolatile(); 3759 } 3760 3761 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) { 3762 Value *P = unwrap<Value>(MemAccessInst); 3763 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 3764 return LI->setVolatile(isVolatile); 3765 if (StoreInst *SI = dyn_cast<StoreInst>(P)) 3766 return SI->setVolatile(isVolatile); 3767 if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P)) 3768 return AI->setVolatile(isVolatile); 3769 return cast<AtomicCmpXchgInst>(P)->setVolatile(isVolatile); 3770 } 3771 3772 LLVMBool LLVMGetWeak(LLVMValueRef CmpXchgInst) { 3773 return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->isWeak(); 3774 } 3775 3776 void LLVMSetWeak(LLVMValueRef CmpXchgInst, LLVMBool isWeak) { 3777 return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->setWeak(isWeak); 3778 } 3779 3780 LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) { 3781 Value *P = unwrap<Value>(MemAccessInst); 3782 AtomicOrdering O; 3783 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 3784 O = LI->getOrdering(); 3785 else if (StoreInst *SI = dyn_cast<StoreInst>(P)) 3786 O = SI->getOrdering(); 3787 else 3788 O = cast<AtomicRMWInst>(P)->getOrdering(); 3789 return mapToLLVMOrdering(O); 3790 } 3791 3792 void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) { 3793 Value *P = unwrap<Value>(MemAccessInst); 3794 AtomicOrdering O = mapFromLLVMOrdering(Ordering); 3795 3796 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 3797 return LI->setOrdering(O); 3798 return cast<StoreInst>(P)->setOrdering(O); 3799 } 3800 3801 LLVMAtomicRMWBinOp LLVMGetAtomicRMWBinOp(LLVMValueRef Inst) { 3802 return mapToLLVMRMWBinOp(unwrap<AtomicRMWInst>(Inst)->getOperation()); 3803 } 3804 3805 void LLVMSetAtomicRMWBinOp(LLVMValueRef Inst, LLVMAtomicRMWBinOp BinOp) { 3806 unwrap<AtomicRMWInst>(Inst)->setOperation(mapFromLLVMRMWBinOp(BinOp)); 3807 } 3808 3809 /*--.. Casts ...............................................................--*/ 3810 3811 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val, 3812 LLVMTypeRef DestTy, const char *Name) { 3813 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name)); 3814 } 3815 3816 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val, 3817 LLVMTypeRef DestTy, const char *Name) { 3818 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name)); 3819 } 3820 3821 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val, 3822 LLVMTypeRef DestTy, const char *Name) { 3823 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name)); 3824 } 3825 3826 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val, 3827 LLVMTypeRef DestTy, const char *Name) { 3828 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name)); 3829 } 3830 3831 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val, 3832 LLVMTypeRef DestTy, const char *Name) { 3833 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name)); 3834 } 3835 3836 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val, 3837 LLVMTypeRef DestTy, const char *Name) { 3838 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name)); 3839 } 3840 3841 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val, 3842 LLVMTypeRef DestTy, const char *Name) { 3843 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name)); 3844 } 3845 3846 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val, 3847 LLVMTypeRef DestTy, const char *Name) { 3848 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name)); 3849 } 3850 3851 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val, 3852 LLVMTypeRef DestTy, const char *Name) { 3853 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name)); 3854 } 3855 3856 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val, 3857 LLVMTypeRef DestTy, const char *Name) { 3858 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name)); 3859 } 3860 3861 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val, 3862 LLVMTypeRef DestTy, const char *Name) { 3863 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name)); 3864 } 3865 3866 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val, 3867 LLVMTypeRef DestTy, const char *Name) { 3868 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name)); 3869 } 3870 3871 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val, 3872 LLVMTypeRef DestTy, const char *Name) { 3873 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name)); 3874 } 3875 3876 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val, 3877 LLVMTypeRef DestTy, const char *Name) { 3878 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy), 3879 Name)); 3880 } 3881 3882 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val, 3883 LLVMTypeRef DestTy, const char *Name) { 3884 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy), 3885 Name)); 3886 } 3887 3888 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val, 3889 LLVMTypeRef DestTy, const char *Name) { 3890 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy), 3891 Name)); 3892 } 3893 3894 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val, 3895 LLVMTypeRef DestTy, const char *Name) { 3896 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val), 3897 unwrap(DestTy), Name)); 3898 } 3899 3900 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val, 3901 LLVMTypeRef DestTy, const char *Name) { 3902 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name)); 3903 } 3904 3905 LLVMValueRef LLVMBuildIntCast2(LLVMBuilderRef B, LLVMValueRef Val, 3906 LLVMTypeRef DestTy, LLVMBool IsSigned, 3907 const char *Name) { 3908 return wrap( 3909 unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy), IsSigned, Name)); 3910 } 3911 3912 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val, 3913 LLVMTypeRef DestTy, const char *Name) { 3914 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy), 3915 /*isSigned*/true, Name)); 3916 } 3917 3918 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val, 3919 LLVMTypeRef DestTy, const char *Name) { 3920 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name)); 3921 } 3922 3923 /*--.. Comparisons .........................................................--*/ 3924 3925 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op, 3926 LLVMValueRef LHS, LLVMValueRef RHS, 3927 const char *Name) { 3928 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op), 3929 unwrap(LHS), unwrap(RHS), Name)); 3930 } 3931 3932 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op, 3933 LLVMValueRef LHS, LLVMValueRef RHS, 3934 const char *Name) { 3935 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op), 3936 unwrap(LHS), unwrap(RHS), Name)); 3937 } 3938 3939 /*--.. Miscellaneous instructions ..........................................--*/ 3940 3941 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) { 3942 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name)); 3943 } 3944 3945 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, 3946 LLVMValueRef *Args, unsigned NumArgs, 3947 const char *Name) { 3948 Value *V = unwrap(Fn); 3949 FunctionType *FnT = 3950 cast<FunctionType>(cast<PointerType>(V->getType())->getElementType()); 3951 3952 return wrap(unwrap(B)->CreateCall(FnT, unwrap(Fn), 3953 makeArrayRef(unwrap(Args), NumArgs), Name)); 3954 } 3955 3956 LLVMValueRef LLVMBuildCall2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn, 3957 LLVMValueRef *Args, unsigned NumArgs, 3958 const char *Name) { 3959 FunctionType *FTy = unwrap<FunctionType>(Ty); 3960 return wrap(unwrap(B)->CreateCall(FTy, unwrap(Fn), 3961 makeArrayRef(unwrap(Args), NumArgs), Name)); 3962 } 3963 3964 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If, 3965 LLVMValueRef Then, LLVMValueRef Else, 3966 const char *Name) { 3967 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else), 3968 Name)); 3969 } 3970 3971 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List, 3972 LLVMTypeRef Ty, const char *Name) { 3973 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name)); 3974 } 3975 3976 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal, 3977 LLVMValueRef Index, const char *Name) { 3978 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index), 3979 Name)); 3980 } 3981 3982 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal, 3983 LLVMValueRef EltVal, LLVMValueRef Index, 3984 const char *Name) { 3985 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal), 3986 unwrap(Index), Name)); 3987 } 3988 3989 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1, 3990 LLVMValueRef V2, LLVMValueRef Mask, 3991 const char *Name) { 3992 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2), 3993 unwrap(Mask), Name)); 3994 } 3995 3996 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal, 3997 unsigned Index, const char *Name) { 3998 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name)); 3999 } 4000 4001 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal, 4002 LLVMValueRef EltVal, unsigned Index, 4003 const char *Name) { 4004 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal), 4005 Index, Name)); 4006 } 4007 4008 LLVMValueRef LLVMBuildFreeze(LLVMBuilderRef B, LLVMValueRef Val, 4009 const char *Name) { 4010 return wrap(unwrap(B)->CreateFreeze(unwrap(Val), Name)); 4011 } 4012 4013 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val, 4014 const char *Name) { 4015 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name)); 4016 } 4017 4018 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val, 4019 const char *Name) { 4020 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name)); 4021 } 4022 4023 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS, 4024 LLVMValueRef RHS, const char *Name) { 4025 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name)); 4026 } 4027 4028 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op, 4029 LLVMValueRef PTR, LLVMValueRef Val, 4030 LLVMAtomicOrdering ordering, 4031 LLVMBool singleThread) { 4032 AtomicRMWInst::BinOp intop = mapFromLLVMRMWBinOp(op); 4033 return wrap(unwrap(B)->CreateAtomicRMW( 4034 intop, unwrap(PTR), unwrap(Val), MaybeAlign(), 4035 mapFromLLVMOrdering(ordering), 4036 singleThread ? SyncScope::SingleThread : SyncScope::System)); 4037 } 4038 4039 LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr, 4040 LLVMValueRef Cmp, LLVMValueRef New, 4041 LLVMAtomicOrdering SuccessOrdering, 4042 LLVMAtomicOrdering FailureOrdering, 4043 LLVMBool singleThread) { 4044 4045 return wrap(unwrap(B)->CreateAtomicCmpXchg( 4046 unwrap(Ptr), unwrap(Cmp), unwrap(New), MaybeAlign(), 4047 mapFromLLVMOrdering(SuccessOrdering), 4048 mapFromLLVMOrdering(FailureOrdering), 4049 singleThread ? SyncScope::SingleThread : SyncScope::System)); 4050 } 4051 4052 unsigned LLVMGetNumMaskElements(LLVMValueRef SVInst) { 4053 Value *P = unwrap<Value>(SVInst); 4054 ShuffleVectorInst *I = cast<ShuffleVectorInst>(P); 4055 return I->getShuffleMask().size(); 4056 } 4057 4058 int LLVMGetMaskValue(LLVMValueRef SVInst, unsigned Elt) { 4059 Value *P = unwrap<Value>(SVInst); 4060 ShuffleVectorInst *I = cast<ShuffleVectorInst>(P); 4061 return I->getMaskValue(Elt); 4062 } 4063 4064 int LLVMGetUndefMaskElem(void) { return UndefMaskElem; } 4065 4066 LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) { 4067 Value *P = unwrap<Value>(AtomicInst); 4068 4069 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P)) 4070 return I->getSyncScopeID() == SyncScope::SingleThread; 4071 return cast<AtomicCmpXchgInst>(P)->getSyncScopeID() == 4072 SyncScope::SingleThread; 4073 } 4074 4075 void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) { 4076 Value *P = unwrap<Value>(AtomicInst); 4077 SyncScope::ID SSID = NewValue ? SyncScope::SingleThread : SyncScope::System; 4078 4079 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P)) 4080 return I->setSyncScopeID(SSID); 4081 return cast<AtomicCmpXchgInst>(P)->setSyncScopeID(SSID); 4082 } 4083 4084 LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) { 4085 Value *P = unwrap<Value>(CmpXchgInst); 4086 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering()); 4087 } 4088 4089 void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst, 4090 LLVMAtomicOrdering Ordering) { 4091 Value *P = unwrap<Value>(CmpXchgInst); 4092 AtomicOrdering O = mapFromLLVMOrdering(Ordering); 4093 4094 return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O); 4095 } 4096 4097 LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst) { 4098 Value *P = unwrap<Value>(CmpXchgInst); 4099 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering()); 4100 } 4101 4102 void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst, 4103 LLVMAtomicOrdering Ordering) { 4104 Value *P = unwrap<Value>(CmpXchgInst); 4105 AtomicOrdering O = mapFromLLVMOrdering(Ordering); 4106 4107 return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O); 4108 } 4109 4110 /*===-- Module providers --------------------------------------------------===*/ 4111 4112 LLVMModuleProviderRef 4113 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) { 4114 return reinterpret_cast<LLVMModuleProviderRef>(M); 4115 } 4116 4117 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) { 4118 delete unwrap(MP); 4119 } 4120 4121 4122 /*===-- Memory buffers ----------------------------------------------------===*/ 4123 4124 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile( 4125 const char *Path, 4126 LLVMMemoryBufferRef *OutMemBuf, 4127 char **OutMessage) { 4128 4129 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path); 4130 if (std::error_code EC = MBOrErr.getError()) { 4131 *OutMessage = strdup(EC.message().c_str()); 4132 return 1; 4133 } 4134 *OutMemBuf = wrap(MBOrErr.get().release()); 4135 return 0; 4136 } 4137 4138 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf, 4139 char **OutMessage) { 4140 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN(); 4141 if (std::error_code EC = MBOrErr.getError()) { 4142 *OutMessage = strdup(EC.message().c_str()); 4143 return 1; 4144 } 4145 *OutMemBuf = wrap(MBOrErr.get().release()); 4146 return 0; 4147 } 4148 4149 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange( 4150 const char *InputData, 4151 size_t InputDataLength, 4152 const char *BufferName, 4153 LLVMBool RequiresNullTerminator) { 4154 4155 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength), 4156 StringRef(BufferName), 4157 RequiresNullTerminator).release()); 4158 } 4159 4160 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy( 4161 const char *InputData, 4162 size_t InputDataLength, 4163 const char *BufferName) { 4164 4165 return wrap( 4166 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength), 4167 StringRef(BufferName)).release()); 4168 } 4169 4170 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) { 4171 return unwrap(MemBuf)->getBufferStart(); 4172 } 4173 4174 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) { 4175 return unwrap(MemBuf)->getBufferSize(); 4176 } 4177 4178 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) { 4179 delete unwrap(MemBuf); 4180 } 4181 4182 /*===-- Pass Registry -----------------------------------------------------===*/ 4183 4184 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) { 4185 return wrap(PassRegistry::getPassRegistry()); 4186 } 4187 4188 /*===-- Pass Manager ------------------------------------------------------===*/ 4189 4190 LLVMPassManagerRef LLVMCreatePassManager() { 4191 return wrap(new legacy::PassManager()); 4192 } 4193 4194 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) { 4195 return wrap(new legacy::FunctionPassManager(unwrap(M))); 4196 } 4197 4198 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) { 4199 return LLVMCreateFunctionPassManagerForModule( 4200 reinterpret_cast<LLVMModuleRef>(P)); 4201 } 4202 4203 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) { 4204 return unwrap<legacy::PassManager>(PM)->run(*unwrap(M)); 4205 } 4206 4207 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) { 4208 return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization(); 4209 } 4210 4211 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) { 4212 return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F)); 4213 } 4214 4215 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) { 4216 return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization(); 4217 } 4218 4219 void LLVMDisposePassManager(LLVMPassManagerRef PM) { 4220 delete unwrap(PM); 4221 } 4222 4223 /*===-- Threading ------------------------------------------------------===*/ 4224 4225 LLVMBool LLVMStartMultithreaded() { 4226 return LLVMIsMultithreaded(); 4227 } 4228 4229 void LLVMStopMultithreaded() { 4230 } 4231 4232 LLVMBool LLVMIsMultithreaded() { 4233 return llvm_is_multithreaded(); 4234 } 4235