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