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