1 //===-- Core.cpp ----------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the common infrastructure (including the C bindings) 11 // for libLLVMCore.a, which implements the LLVM intermediate representation. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm-c/Core.h" 16 #include "llvm/ADT/StringSwitch.h" 17 #include "llvm/IR/Attributes.h" 18 #include "llvm/IR/CallSite.h" 19 #include "llvm/IR/Constants.h" 20 #include "llvm/IR/DerivedTypes.h" 21 #include "llvm/IR/DiagnosticInfo.h" 22 #include "llvm/IR/DiagnosticPrinter.h" 23 #include "llvm/IR/GlobalAlias.h" 24 #include "llvm/IR/GlobalVariable.h" 25 #include "llvm/IR/IRBuilder.h" 26 #include "llvm/IR/InlineAsm.h" 27 #include "llvm/IR/IntrinsicInst.h" 28 #include "llvm/IR/LLVMContext.h" 29 #include "llvm/IR/LegacyPassManager.h" 30 #include "llvm/IR/Module.h" 31 #include "llvm/Support/Debug.h" 32 #include "llvm/Support/ErrorHandling.h" 33 #include "llvm/Support/FileSystem.h" 34 #include "llvm/Support/ManagedStatic.h" 35 #include "llvm/Support/MemoryBuffer.h" 36 #include "llvm/Support/Threading.h" 37 #include "llvm/Support/raw_ostream.h" 38 #include <cassert> 39 #include <cstdlib> 40 #include <cstring> 41 #include <system_error> 42 43 using namespace llvm; 44 45 #define DEBUG_TYPE "ir" 46 47 void llvm::initializeCore(PassRegistry &Registry) { 48 initializeDominatorTreeWrapperPassPass(Registry); 49 initializePrintModulePassWrapperPass(Registry); 50 initializePrintFunctionPassWrapperPass(Registry); 51 initializePrintBasicBlockPassPass(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 void LLVMContextDispose(LLVMContextRef C) { 111 delete unwrap(C); 112 } 113 114 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char *Name, 115 unsigned SLen) { 116 return unwrap(C)->getMDKindID(StringRef(Name, SLen)); 117 } 118 119 unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) { 120 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen); 121 } 122 123 #define GET_ATTR_KIND_FROM_NAME 124 #include "AttributesCompatFunc.inc" 125 126 unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) { 127 return getAttrKindFromName(StringRef(Name, SLen)); 128 } 129 130 unsigned LLVMGetLastEnumAttributeKind(void) { 131 return Attribute::AttrKind::EndAttrKinds; 132 } 133 134 LLVMAttributeRef LLVMCreateEnumAttribute(LLVMContextRef C, unsigned KindID, 135 uint64_t Val) { 136 return wrap(Attribute::get(*unwrap(C), (Attribute::AttrKind)KindID, Val)); 137 } 138 139 unsigned LLVMGetEnumAttributeKind(LLVMAttributeRef A) { 140 return unwrap(A).getKindAsEnum(); 141 } 142 143 uint64_t LLVMGetEnumAttributeValue(LLVMAttributeRef A) { 144 auto Attr = unwrap(A); 145 if (Attr.isEnumAttribute()) 146 return 0; 147 return Attr.getValueAsInt(); 148 } 149 150 LLVMAttributeRef LLVMCreateStringAttribute(LLVMContextRef C, 151 const char *K, unsigned KLength, 152 const char *V, unsigned VLength) { 153 return wrap(Attribute::get(*unwrap(C), StringRef(K, KLength), 154 StringRef(V, VLength))); 155 } 156 157 const char *LLVMGetStringAttributeKind(LLVMAttributeRef A, 158 unsigned *Length) { 159 auto S = unwrap(A).getKindAsString(); 160 *Length = S.size(); 161 return S.data(); 162 } 163 164 const char *LLVMGetStringAttributeValue(LLVMAttributeRef A, 165 unsigned *Length) { 166 auto S = unwrap(A).getValueAsString(); 167 *Length = S.size(); 168 return S.data(); 169 } 170 171 LLVMBool LLVMIsEnumAttribute(LLVMAttributeRef A) { 172 auto Attr = unwrap(A); 173 return Attr.isEnumAttribute() || Attr.isIntAttribute(); 174 } 175 176 LLVMBool LLVMIsStringAttribute(LLVMAttributeRef A) { 177 return unwrap(A).isStringAttribute(); 178 } 179 180 char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) { 181 std::string MsgStorage; 182 raw_string_ostream Stream(MsgStorage); 183 DiagnosticPrinterRawOStream DP(Stream); 184 185 unwrap(DI)->print(DP); 186 Stream.flush(); 187 188 return LLVMCreateMessage(MsgStorage.c_str()); 189 } 190 191 LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI) { 192 LLVMDiagnosticSeverity severity; 193 194 switch(unwrap(DI)->getSeverity()) { 195 default: 196 severity = LLVMDSError; 197 break; 198 case DS_Warning: 199 severity = LLVMDSWarning; 200 break; 201 case DS_Remark: 202 severity = LLVMDSRemark; 203 break; 204 case DS_Note: 205 severity = LLVMDSNote; 206 break; 207 } 208 209 return severity; 210 } 211 212 /*===-- Operations on modules ---------------------------------------------===*/ 213 214 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) { 215 return wrap(new Module(ModuleID, *GlobalContext)); 216 } 217 218 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID, 219 LLVMContextRef C) { 220 return wrap(new Module(ModuleID, *unwrap(C))); 221 } 222 223 void LLVMDisposeModule(LLVMModuleRef M) { 224 delete unwrap(M); 225 } 226 227 const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) { 228 auto &Str = unwrap(M)->getModuleIdentifier(); 229 *Len = Str.length(); 230 return Str.c_str(); 231 } 232 233 void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) { 234 unwrap(M)->setModuleIdentifier(StringRef(Ident, Len)); 235 } 236 237 const char *LLVMGetSourceFileName(LLVMModuleRef M, size_t *Len) { 238 auto &Str = unwrap(M)->getSourceFileName(); 239 *Len = Str.length(); 240 return Str.c_str(); 241 } 242 243 void LLVMSetSourceFileName(LLVMModuleRef M, const char *Name, size_t Len) { 244 unwrap(M)->setSourceFileName(StringRef(Name, Len)); 245 } 246 247 /*--.. Data layout .........................................................--*/ 248 const char *LLVMGetDataLayoutStr(LLVMModuleRef M) { 249 return unwrap(M)->getDataLayoutStr().c_str(); 250 } 251 252 const char *LLVMGetDataLayout(LLVMModuleRef M) { 253 return LLVMGetDataLayoutStr(M); 254 } 255 256 void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) { 257 unwrap(M)->setDataLayout(DataLayoutStr); 258 } 259 260 /*--.. Target triple .......................................................--*/ 261 const char * LLVMGetTarget(LLVMModuleRef M) { 262 return unwrap(M)->getTargetTriple().c_str(); 263 } 264 265 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) { 266 unwrap(M)->setTargetTriple(Triple); 267 } 268 269 void LLVMDumpModule(LLVMModuleRef M) { 270 unwrap(M)->print(errs(), nullptr, 271 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true); 272 } 273 274 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename, 275 char **ErrorMessage) { 276 std::error_code EC; 277 raw_fd_ostream dest(Filename, EC, sys::fs::F_Text); 278 if (EC) { 279 *ErrorMessage = strdup(EC.message().c_str()); 280 return true; 281 } 282 283 unwrap(M)->print(dest, nullptr); 284 285 dest.close(); 286 287 if (dest.has_error()) { 288 std::string E = "Error printing to file: " + dest.error().message(); 289 *ErrorMessage = strdup(E.c_str()); 290 return true; 291 } 292 293 return false; 294 } 295 296 char *LLVMPrintModuleToString(LLVMModuleRef M) { 297 std::string buf; 298 raw_string_ostream os(buf); 299 300 unwrap(M)->print(os, nullptr); 301 os.flush(); 302 303 return strdup(buf.c_str()); 304 } 305 306 /*--.. Operations on inline assembler ......................................--*/ 307 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) { 308 unwrap(M)->setModuleInlineAsm(StringRef(Asm)); 309 } 310 311 312 /*--.. Operations on module contexts ......................................--*/ 313 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) { 314 return wrap(&unwrap(M)->getContext()); 315 } 316 317 318 /*===-- Operations on types -----------------------------------------------===*/ 319 320 /*--.. Operations on all types (mostly) ....................................--*/ 321 322 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) { 323 switch (unwrap(Ty)->getTypeID()) { 324 case Type::VoidTyID: 325 return LLVMVoidTypeKind; 326 case Type::HalfTyID: 327 return LLVMHalfTypeKind; 328 case Type::FloatTyID: 329 return LLVMFloatTypeKind; 330 case Type::DoubleTyID: 331 return LLVMDoubleTypeKind; 332 case Type::X86_FP80TyID: 333 return LLVMX86_FP80TypeKind; 334 case Type::FP128TyID: 335 return LLVMFP128TypeKind; 336 case Type::PPC_FP128TyID: 337 return LLVMPPC_FP128TypeKind; 338 case Type::LabelTyID: 339 return LLVMLabelTypeKind; 340 case Type::MetadataTyID: 341 return LLVMMetadataTypeKind; 342 case Type::IntegerTyID: 343 return LLVMIntegerTypeKind; 344 case Type::FunctionTyID: 345 return LLVMFunctionTypeKind; 346 case Type::StructTyID: 347 return LLVMStructTypeKind; 348 case Type::ArrayTyID: 349 return LLVMArrayTypeKind; 350 case Type::PointerTyID: 351 return LLVMPointerTypeKind; 352 case Type::VectorTyID: 353 return LLVMVectorTypeKind; 354 case Type::X86_MMXTyID: 355 return LLVMX86_MMXTypeKind; 356 case Type::TokenTyID: 357 return LLVMTokenTypeKind; 358 } 359 llvm_unreachable("Unhandled TypeID."); 360 } 361 362 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty) 363 { 364 return unwrap(Ty)->isSized(); 365 } 366 367 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) { 368 return wrap(&unwrap(Ty)->getContext()); 369 } 370 371 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 372 LLVM_DUMP_METHOD void LLVMDumpType(LLVMTypeRef Ty) { 373 return unwrap(Ty)->dump(); 374 } 375 #endif 376 377 char *LLVMPrintTypeToString(LLVMTypeRef Ty) { 378 std::string buf; 379 raw_string_ostream os(buf); 380 381 if (unwrap(Ty)) 382 unwrap(Ty)->print(os); 383 else 384 os << "Printing <null> Type"; 385 386 os.flush(); 387 388 return strdup(buf.c_str()); 389 } 390 391 /*--.. Operations on integer types .........................................--*/ 392 393 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) { 394 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C)); 395 } 396 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) { 397 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C)); 398 } 399 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) { 400 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C)); 401 } 402 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) { 403 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C)); 404 } 405 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) { 406 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C)); 407 } 408 LLVMTypeRef LLVMInt128TypeInContext(LLVMContextRef C) { 409 return (LLVMTypeRef) Type::getInt128Ty(*unwrap(C)); 410 } 411 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) { 412 return wrap(IntegerType::get(*unwrap(C), NumBits)); 413 } 414 415 LLVMTypeRef LLVMInt1Type(void) { 416 return LLVMInt1TypeInContext(LLVMGetGlobalContext()); 417 } 418 LLVMTypeRef LLVMInt8Type(void) { 419 return LLVMInt8TypeInContext(LLVMGetGlobalContext()); 420 } 421 LLVMTypeRef LLVMInt16Type(void) { 422 return LLVMInt16TypeInContext(LLVMGetGlobalContext()); 423 } 424 LLVMTypeRef LLVMInt32Type(void) { 425 return LLVMInt32TypeInContext(LLVMGetGlobalContext()); 426 } 427 LLVMTypeRef LLVMInt64Type(void) { 428 return LLVMInt64TypeInContext(LLVMGetGlobalContext()); 429 } 430 LLVMTypeRef LLVMInt128Type(void) { 431 return LLVMInt128TypeInContext(LLVMGetGlobalContext()); 432 } 433 LLVMTypeRef LLVMIntType(unsigned NumBits) { 434 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits); 435 } 436 437 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) { 438 return unwrap<IntegerType>(IntegerTy)->getBitWidth(); 439 } 440 441 /*--.. Operations on real types ............................................--*/ 442 443 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) { 444 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C)); 445 } 446 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) { 447 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C)); 448 } 449 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) { 450 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C)); 451 } 452 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) { 453 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C)); 454 } 455 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) { 456 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C)); 457 } 458 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) { 459 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C)); 460 } 461 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) { 462 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C)); 463 } 464 465 LLVMTypeRef LLVMHalfType(void) { 466 return LLVMHalfTypeInContext(LLVMGetGlobalContext()); 467 } 468 LLVMTypeRef LLVMFloatType(void) { 469 return LLVMFloatTypeInContext(LLVMGetGlobalContext()); 470 } 471 LLVMTypeRef LLVMDoubleType(void) { 472 return LLVMDoubleTypeInContext(LLVMGetGlobalContext()); 473 } 474 LLVMTypeRef LLVMX86FP80Type(void) { 475 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext()); 476 } 477 LLVMTypeRef LLVMFP128Type(void) { 478 return LLVMFP128TypeInContext(LLVMGetGlobalContext()); 479 } 480 LLVMTypeRef LLVMPPCFP128Type(void) { 481 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext()); 482 } 483 LLVMTypeRef LLVMX86MMXType(void) { 484 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext()); 485 } 486 487 /*--.. Operations on function types ........................................--*/ 488 489 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType, 490 LLVMTypeRef *ParamTypes, unsigned ParamCount, 491 LLVMBool IsVarArg) { 492 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount); 493 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0)); 494 } 495 496 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) { 497 return unwrap<FunctionType>(FunctionTy)->isVarArg(); 498 } 499 500 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) { 501 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType()); 502 } 503 504 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) { 505 return unwrap<FunctionType>(FunctionTy)->getNumParams(); 506 } 507 508 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) { 509 FunctionType *Ty = unwrap<FunctionType>(FunctionTy); 510 for (FunctionType::param_iterator I = Ty->param_begin(), 511 E = Ty->param_end(); I != E; ++I) 512 *Dest++ = wrap(*I); 513 } 514 515 /*--.. Operations on struct types ..........................................--*/ 516 517 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes, 518 unsigned ElementCount, LLVMBool Packed) { 519 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount); 520 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0)); 521 } 522 523 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes, 524 unsigned ElementCount, LLVMBool Packed) { 525 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes, 526 ElementCount, Packed); 527 } 528 529 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name) 530 { 531 return wrap(StructType::create(*unwrap(C), Name)); 532 } 533 534 const char *LLVMGetStructName(LLVMTypeRef Ty) 535 { 536 StructType *Type = unwrap<StructType>(Ty); 537 if (!Type->hasName()) 538 return nullptr; 539 return Type->getName().data(); 540 } 541 542 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes, 543 unsigned ElementCount, LLVMBool Packed) { 544 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount); 545 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0); 546 } 547 548 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) { 549 return unwrap<StructType>(StructTy)->getNumElements(); 550 } 551 552 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) { 553 StructType *Ty = unwrap<StructType>(StructTy); 554 for (StructType::element_iterator I = Ty->element_begin(), 555 E = Ty->element_end(); I != E; ++I) 556 *Dest++ = wrap(*I); 557 } 558 559 LLVMTypeRef LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy, unsigned i) { 560 StructType *Ty = unwrap<StructType>(StructTy); 561 return wrap(Ty->getTypeAtIndex(i)); 562 } 563 564 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) { 565 return unwrap<StructType>(StructTy)->isPacked(); 566 } 567 568 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) { 569 return unwrap<StructType>(StructTy)->isOpaque(); 570 } 571 572 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) { 573 return wrap(unwrap(M)->getTypeByName(Name)); 574 } 575 576 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/ 577 578 void LLVMGetSubtypes(LLVMTypeRef Tp, LLVMTypeRef *Arr) { 579 int i = 0; 580 for (auto *T : unwrap(Tp)->subtypes()) { 581 Arr[i] = wrap(T); 582 i++; 583 } 584 } 585 586 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) { 587 return wrap(ArrayType::get(unwrap(ElementType), ElementCount)); 588 } 589 590 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) { 591 return wrap(PointerType::get(unwrap(ElementType), AddressSpace)); 592 } 593 594 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) { 595 return wrap(VectorType::get(unwrap(ElementType), ElementCount)); 596 } 597 598 LLVMTypeRef LLVMGetElementType(LLVMTypeRef WrappedTy) { 599 auto *Ty = unwrap<Type>(WrappedTy); 600 if (auto *PTy = dyn_cast<PointerType>(Ty)) 601 return wrap(PTy->getElementType()); 602 return wrap(cast<SequentialType>(Ty)->getElementType()); 603 } 604 605 unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp) { 606 return unwrap(Tp)->getNumContainedTypes(); 607 } 608 609 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) { 610 return unwrap<ArrayType>(ArrayTy)->getNumElements(); 611 } 612 613 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) { 614 return unwrap<PointerType>(PointerTy)->getAddressSpace(); 615 } 616 617 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) { 618 return unwrap<VectorType>(VectorTy)->getNumElements(); 619 } 620 621 /*--.. Operations on other types ...........................................--*/ 622 623 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) { 624 return wrap(Type::getVoidTy(*unwrap(C))); 625 } 626 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) { 627 return wrap(Type::getLabelTy(*unwrap(C))); 628 } 629 LLVMTypeRef LLVMTokenTypeInContext(LLVMContextRef C) { 630 return wrap(Type::getTokenTy(*unwrap(C))); 631 } 632 LLVMTypeRef LLVMMetadataTypeInContext(LLVMContextRef C) { 633 return wrap(Type::getMetadataTy(*unwrap(C))); 634 } 635 636 LLVMTypeRef LLVMVoidType(void) { 637 return LLVMVoidTypeInContext(LLVMGetGlobalContext()); 638 } 639 LLVMTypeRef LLVMLabelType(void) { 640 return LLVMLabelTypeInContext(LLVMGetGlobalContext()); 641 } 642 643 /*===-- Operations on values ----------------------------------------------===*/ 644 645 /*--.. Operations on all values ............................................--*/ 646 647 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) { 648 return wrap(unwrap(Val)->getType()); 649 } 650 651 LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) { 652 switch(unwrap(Val)->getValueID()) { 653 #define HANDLE_VALUE(Name) \ 654 case Value::Name##Val: \ 655 return LLVM##Name##ValueKind; 656 #include "llvm/IR/Value.def" 657 default: 658 return LLVMInstructionValueKind; 659 } 660 } 661 662 const char *LLVMGetValueName(LLVMValueRef Val) { 663 return unwrap(Val)->getName().data(); 664 } 665 666 void LLVMSetValueName(LLVMValueRef Val, const char *Name) { 667 unwrap(Val)->setName(Name); 668 } 669 670 LLVM_DUMP_METHOD void LLVMDumpValue(LLVMValueRef Val) { 671 unwrap(Val)->print(errs(), /*IsForDebug=*/true); 672 } 673 674 char* LLVMPrintValueToString(LLVMValueRef Val) { 675 std::string buf; 676 raw_string_ostream os(buf); 677 678 if (unwrap(Val)) 679 unwrap(Val)->print(os); 680 else 681 os << "Printing <null> Value"; 682 683 os.flush(); 684 685 return strdup(buf.c_str()); 686 } 687 688 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) { 689 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal)); 690 } 691 692 int LLVMHasMetadata(LLVMValueRef Inst) { 693 return unwrap<Instruction>(Inst)->hasMetadata(); 694 } 695 696 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) { 697 auto *I = unwrap<Instruction>(Inst); 698 assert(I && "Expected instruction"); 699 if (auto *MD = I->getMetadata(KindID)) 700 return wrap(MetadataAsValue::get(I->getContext(), MD)); 701 return nullptr; 702 } 703 704 // MetadataAsValue uses a canonical format which strips the actual MDNode for 705 // MDNode with just a single constant value, storing just a ConstantAsMetadata 706 // This undoes this canonicalization, reconstructing the MDNode. 707 static MDNode *extractMDNode(MetadataAsValue *MAV) { 708 Metadata *MD = MAV->getMetadata(); 709 assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) && 710 "Expected a metadata node or a canonicalized constant"); 711 712 if (MDNode *N = dyn_cast<MDNode>(MD)) 713 return N; 714 715 return MDNode::get(MAV->getContext(), MD); 716 } 717 718 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) { 719 MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr; 720 721 unwrap<Instruction>(Inst)->setMetadata(KindID, N); 722 } 723 724 /*--.. Conversion functions ................................................--*/ 725 726 #define LLVM_DEFINE_VALUE_CAST(name) \ 727 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \ 728 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \ 729 } 730 731 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST) 732 733 LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) { 734 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val))) 735 if (isa<MDNode>(MD->getMetadata()) || 736 isa<ValueAsMetadata>(MD->getMetadata())) 737 return Val; 738 return nullptr; 739 } 740 741 LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) { 742 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val))) 743 if (isa<MDString>(MD->getMetadata())) 744 return Val; 745 return nullptr; 746 } 747 748 /*--.. Operations on Uses ..................................................--*/ 749 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) { 750 Value *V = unwrap(Val); 751 Value::use_iterator I = V->use_begin(); 752 if (I == V->use_end()) 753 return nullptr; 754 return wrap(&*I); 755 } 756 757 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) { 758 Use *Next = unwrap(U)->getNext(); 759 if (Next) 760 return wrap(Next); 761 return nullptr; 762 } 763 764 LLVMValueRef LLVMGetUser(LLVMUseRef U) { 765 return wrap(unwrap(U)->getUser()); 766 } 767 768 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) { 769 return wrap(unwrap(U)->get()); 770 } 771 772 /*--.. Operations on Users .................................................--*/ 773 774 static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N, 775 unsigned Index) { 776 Metadata *Op = N->getOperand(Index); 777 if (!Op) 778 return nullptr; 779 if (auto *C = dyn_cast<ConstantAsMetadata>(Op)) 780 return wrap(C->getValue()); 781 return wrap(MetadataAsValue::get(Context, Op)); 782 } 783 784 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) { 785 Value *V = unwrap(Val); 786 if (auto *MD = dyn_cast<MetadataAsValue>(V)) { 787 if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) { 788 assert(Index == 0 && "Function-local metadata can only have one operand"); 789 return wrap(L->getValue()); 790 } 791 return getMDNodeOperandImpl(V->getContext(), 792 cast<MDNode>(MD->getMetadata()), Index); 793 } 794 795 return wrap(cast<User>(V)->getOperand(Index)); 796 } 797 798 LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) { 799 Value *V = unwrap(Val); 800 return wrap(&cast<User>(V)->getOperandUse(Index)); 801 } 802 803 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) { 804 unwrap<User>(Val)->setOperand(Index, unwrap(Op)); 805 } 806 807 int LLVMGetNumOperands(LLVMValueRef Val) { 808 Value *V = unwrap(Val); 809 if (isa<MetadataAsValue>(V)) 810 return LLVMGetMDNodeNumOperands(Val); 811 812 return cast<User>(V)->getNumOperands(); 813 } 814 815 /*--.. Operations on constants of any type .................................--*/ 816 817 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) { 818 return wrap(Constant::getNullValue(unwrap(Ty))); 819 } 820 821 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) { 822 return wrap(Constant::getAllOnesValue(unwrap(Ty))); 823 } 824 825 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) { 826 return wrap(UndefValue::get(unwrap(Ty))); 827 } 828 829 LLVMBool LLVMIsConstant(LLVMValueRef Ty) { 830 return isa<Constant>(unwrap(Ty)); 831 } 832 833 LLVMBool LLVMIsNull(LLVMValueRef Val) { 834 if (Constant *C = dyn_cast<Constant>(unwrap(Val))) 835 return C->isNullValue(); 836 return false; 837 } 838 839 LLVMBool LLVMIsUndef(LLVMValueRef Val) { 840 return isa<UndefValue>(unwrap(Val)); 841 } 842 843 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) { 844 return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty))); 845 } 846 847 /*--.. Operations on metadata nodes ........................................--*/ 848 849 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str, 850 unsigned SLen) { 851 LLVMContext &Context = *unwrap(C); 852 return wrap(MetadataAsValue::get( 853 Context, MDString::get(Context, StringRef(Str, SLen)))); 854 } 855 856 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) { 857 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen); 858 } 859 860 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals, 861 unsigned Count) { 862 LLVMContext &Context = *unwrap(C); 863 SmallVector<Metadata *, 8> MDs; 864 for (auto *OV : makeArrayRef(Vals, Count)) { 865 Value *V = unwrap(OV); 866 Metadata *MD; 867 if (!V) 868 MD = nullptr; 869 else if (auto *C = dyn_cast<Constant>(V)) 870 MD = ConstantAsMetadata::get(C); 871 else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) { 872 MD = MDV->getMetadata(); 873 assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata " 874 "outside of direct argument to call"); 875 } else { 876 // This is function-local metadata. Pretend to make an MDNode. 877 assert(Count == 1 && 878 "Expected only one operand to function-local metadata"); 879 return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V))); 880 } 881 882 MDs.push_back(MD); 883 } 884 return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs))); 885 } 886 887 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) { 888 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count); 889 } 890 891 LLVMValueRef LLVMMetadataAsValue(LLVMContextRef C, LLVMMetadataRef MD) { 892 return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD))); 893 } 894 895 LLVMMetadataRef LLVMValueAsMetadata(LLVMValueRef Val) { 896 auto *V = unwrap(Val); 897 if (auto *C = dyn_cast<Constant>(V)) 898 return wrap(ConstantAsMetadata::get(C)); 899 if (auto *MAV = dyn_cast<MetadataAsValue>(V)) 900 return wrap(MAV->getMetadata()); 901 return wrap(ValueAsMetadata::get(V)); 902 } 903 904 const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) { 905 if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V))) 906 if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) { 907 *Length = S->getString().size(); 908 return S->getString().data(); 909 } 910 *Length = 0; 911 return nullptr; 912 } 913 914 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) { 915 auto *MD = cast<MetadataAsValue>(unwrap(V)); 916 if (isa<ValueAsMetadata>(MD->getMetadata())) 917 return 1; 918 return cast<MDNode>(MD->getMetadata())->getNumOperands(); 919 } 920 921 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) { 922 auto *MD = cast<MetadataAsValue>(unwrap(V)); 923 if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) { 924 *Dest = wrap(MDV->getValue()); 925 return; 926 } 927 const auto *N = cast<MDNode>(MD->getMetadata()); 928 const unsigned numOperands = N->getNumOperands(); 929 LLVMContext &Context = unwrap(V)->getContext(); 930 for (unsigned i = 0; i < numOperands; i++) 931 Dest[i] = getMDNodeOperandImpl(Context, N, i); 932 } 933 934 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char *Name) { 935 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) { 936 return N->getNumOperands(); 937 } 938 return 0; 939 } 940 941 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char *Name, 942 LLVMValueRef *Dest) { 943 NamedMDNode *N = unwrap(M)->getNamedMetadata(Name); 944 if (!N) 945 return; 946 LLVMContext &Context = unwrap(M)->getContext(); 947 for (unsigned i=0;i<N->getNumOperands();i++) 948 Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i))); 949 } 950 951 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char *Name, 952 LLVMValueRef Val) { 953 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name); 954 if (!N) 955 return; 956 if (!Val) 957 return; 958 N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val))); 959 } 960 961 /*--.. Operations on scalar constants ......................................--*/ 962 963 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N, 964 LLVMBool SignExtend) { 965 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0)); 966 } 967 968 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy, 969 unsigned NumWords, 970 const uint64_t Words[]) { 971 IntegerType *Ty = unwrap<IntegerType>(IntTy); 972 return wrap(ConstantInt::get(Ty->getContext(), 973 APInt(Ty->getBitWidth(), 974 makeArrayRef(Words, NumWords)))); 975 } 976 977 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[], 978 uint8_t Radix) { 979 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str), 980 Radix)); 981 } 982 983 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[], 984 unsigned SLen, uint8_t Radix) { 985 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen), 986 Radix)); 987 } 988 989 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) { 990 return wrap(ConstantFP::get(unwrap(RealTy), N)); 991 } 992 993 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) { 994 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text))); 995 } 996 997 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[], 998 unsigned SLen) { 999 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen))); 1000 } 1001 1002 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) { 1003 return unwrap<ConstantInt>(ConstantVal)->getZExtValue(); 1004 } 1005 1006 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) { 1007 return unwrap<ConstantInt>(ConstantVal)->getSExtValue(); 1008 } 1009 1010 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) { 1011 ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ; 1012 Type *Ty = cFP->getType(); 1013 1014 if (Ty->isFloatTy()) { 1015 *LosesInfo = false; 1016 return cFP->getValueAPF().convertToFloat(); 1017 } 1018 1019 if (Ty->isDoubleTy()) { 1020 *LosesInfo = false; 1021 return cFP->getValueAPF().convertToDouble(); 1022 } 1023 1024 bool APFLosesInfo; 1025 APFloat APF = cFP->getValueAPF(); 1026 APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo); 1027 *LosesInfo = APFLosesInfo; 1028 return APF.convertToDouble(); 1029 } 1030 1031 /*--.. Operations on composite constants ...................................--*/ 1032 1033 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str, 1034 unsigned Length, 1035 LLVMBool DontNullTerminate) { 1036 /* Inverted the sense of AddNull because ', 0)' is a 1037 better mnemonic for null termination than ', 1)'. */ 1038 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length), 1039 DontNullTerminate == 0)); 1040 } 1041 1042 LLVMValueRef LLVMConstString(const char *Str, unsigned Length, 1043 LLVMBool DontNullTerminate) { 1044 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length, 1045 DontNullTerminate); 1046 } 1047 1048 LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef C, unsigned idx) { 1049 return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx)); 1050 } 1051 1052 LLVMBool LLVMIsConstantString(LLVMValueRef C) { 1053 return unwrap<ConstantDataSequential>(C)->isString(); 1054 } 1055 1056 const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) { 1057 StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString(); 1058 *Length = Str.size(); 1059 return Str.data(); 1060 } 1061 1062 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy, 1063 LLVMValueRef *ConstantVals, unsigned Length) { 1064 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length); 1065 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V)); 1066 } 1067 1068 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C, 1069 LLVMValueRef *ConstantVals, 1070 unsigned Count, LLVMBool Packed) { 1071 Constant **Elements = unwrap<Constant>(ConstantVals, Count); 1072 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count), 1073 Packed != 0)); 1074 } 1075 1076 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count, 1077 LLVMBool Packed) { 1078 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count, 1079 Packed); 1080 } 1081 1082 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy, 1083 LLVMValueRef *ConstantVals, 1084 unsigned Count) { 1085 Constant **Elements = unwrap<Constant>(ConstantVals, Count); 1086 StructType *Ty = cast<StructType>(unwrap(StructTy)); 1087 1088 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count))); 1089 } 1090 1091 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) { 1092 return wrap(ConstantVector::get(makeArrayRef( 1093 unwrap<Constant>(ScalarConstantVals, Size), Size))); 1094 } 1095 1096 /*-- Opcode mapping */ 1097 1098 static LLVMOpcode map_to_llvmopcode(int opcode) 1099 { 1100 switch (opcode) { 1101 default: llvm_unreachable("Unhandled Opcode."); 1102 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc; 1103 #include "llvm/IR/Instruction.def" 1104 #undef HANDLE_INST 1105 } 1106 } 1107 1108 static int map_from_llvmopcode(LLVMOpcode code) 1109 { 1110 switch (code) { 1111 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num; 1112 #include "llvm/IR/Instruction.def" 1113 #undef HANDLE_INST 1114 } 1115 llvm_unreachable("Unhandled Opcode."); 1116 } 1117 1118 /*--.. Constant expressions ................................................--*/ 1119 1120 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) { 1121 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode()); 1122 } 1123 1124 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) { 1125 return wrap(ConstantExpr::getAlignOf(unwrap(Ty))); 1126 } 1127 1128 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) { 1129 return wrap(ConstantExpr::getSizeOf(unwrap(Ty))); 1130 } 1131 1132 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) { 1133 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal))); 1134 } 1135 1136 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) { 1137 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal))); 1138 } 1139 1140 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) { 1141 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal))); 1142 } 1143 1144 1145 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) { 1146 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal))); 1147 } 1148 1149 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) { 1150 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal))); 1151 } 1152 1153 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1154 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant), 1155 unwrap<Constant>(RHSConstant))); 1156 } 1157 1158 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant, 1159 LLVMValueRef RHSConstant) { 1160 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant), 1161 unwrap<Constant>(RHSConstant))); 1162 } 1163 1164 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant, 1165 LLVMValueRef RHSConstant) { 1166 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant), 1167 unwrap<Constant>(RHSConstant))); 1168 } 1169 1170 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1171 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant), 1172 unwrap<Constant>(RHSConstant))); 1173 } 1174 1175 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1176 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant), 1177 unwrap<Constant>(RHSConstant))); 1178 } 1179 1180 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant, 1181 LLVMValueRef RHSConstant) { 1182 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant), 1183 unwrap<Constant>(RHSConstant))); 1184 } 1185 1186 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant, 1187 LLVMValueRef RHSConstant) { 1188 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant), 1189 unwrap<Constant>(RHSConstant))); 1190 } 1191 1192 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1193 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant), 1194 unwrap<Constant>(RHSConstant))); 1195 } 1196 1197 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1198 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant), 1199 unwrap<Constant>(RHSConstant))); 1200 } 1201 1202 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant, 1203 LLVMValueRef RHSConstant) { 1204 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant), 1205 unwrap<Constant>(RHSConstant))); 1206 } 1207 1208 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant, 1209 LLVMValueRef RHSConstant) { 1210 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant), 1211 unwrap<Constant>(RHSConstant))); 1212 } 1213 1214 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1215 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant), 1216 unwrap<Constant>(RHSConstant))); 1217 } 1218 1219 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1220 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant), 1221 unwrap<Constant>(RHSConstant))); 1222 } 1223 1224 LLVMValueRef LLVMConstExactUDiv(LLVMValueRef LHSConstant, 1225 LLVMValueRef RHSConstant) { 1226 return wrap(ConstantExpr::getExactUDiv(unwrap<Constant>(LHSConstant), 1227 unwrap<Constant>(RHSConstant))); 1228 } 1229 1230 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1231 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant), 1232 unwrap<Constant>(RHSConstant))); 1233 } 1234 1235 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant, 1236 LLVMValueRef RHSConstant) { 1237 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant), 1238 unwrap<Constant>(RHSConstant))); 1239 } 1240 1241 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1242 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant), 1243 unwrap<Constant>(RHSConstant))); 1244 } 1245 1246 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1247 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant), 1248 unwrap<Constant>(RHSConstant))); 1249 } 1250 1251 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1252 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant), 1253 unwrap<Constant>(RHSConstant))); 1254 } 1255 1256 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1257 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant), 1258 unwrap<Constant>(RHSConstant))); 1259 } 1260 1261 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1262 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant), 1263 unwrap<Constant>(RHSConstant))); 1264 } 1265 1266 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1267 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant), 1268 unwrap<Constant>(RHSConstant))); 1269 } 1270 1271 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1272 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant), 1273 unwrap<Constant>(RHSConstant))); 1274 } 1275 1276 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate, 1277 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1278 return wrap(ConstantExpr::getICmp(Predicate, 1279 unwrap<Constant>(LHSConstant), 1280 unwrap<Constant>(RHSConstant))); 1281 } 1282 1283 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate, 1284 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1285 return wrap(ConstantExpr::getFCmp(Predicate, 1286 unwrap<Constant>(LHSConstant), 1287 unwrap<Constant>(RHSConstant))); 1288 } 1289 1290 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1291 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant), 1292 unwrap<Constant>(RHSConstant))); 1293 } 1294 1295 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1296 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant), 1297 unwrap<Constant>(RHSConstant))); 1298 } 1299 1300 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1301 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant), 1302 unwrap<Constant>(RHSConstant))); 1303 } 1304 1305 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal, 1306 LLVMValueRef *ConstantIndices, unsigned NumIndices) { 1307 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices), 1308 NumIndices); 1309 return wrap(ConstantExpr::getGetElementPtr( 1310 nullptr, unwrap<Constant>(ConstantVal), IdxList)); 1311 } 1312 1313 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal, 1314 LLVMValueRef *ConstantIndices, 1315 unsigned NumIndices) { 1316 Constant* Val = unwrap<Constant>(ConstantVal); 1317 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices), 1318 NumIndices); 1319 return wrap(ConstantExpr::getInBoundsGetElementPtr(nullptr, Val, IdxList)); 1320 } 1321 1322 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1323 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal), 1324 unwrap(ToType))); 1325 } 1326 1327 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1328 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal), 1329 unwrap(ToType))); 1330 } 1331 1332 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1333 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal), 1334 unwrap(ToType))); 1335 } 1336 1337 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1338 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal), 1339 unwrap(ToType))); 1340 } 1341 1342 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1343 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal), 1344 unwrap(ToType))); 1345 } 1346 1347 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1348 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal), 1349 unwrap(ToType))); 1350 } 1351 1352 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1353 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal), 1354 unwrap(ToType))); 1355 } 1356 1357 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1358 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal), 1359 unwrap(ToType))); 1360 } 1361 1362 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1363 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal), 1364 unwrap(ToType))); 1365 } 1366 1367 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1368 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal), 1369 unwrap(ToType))); 1370 } 1371 1372 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1373 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal), 1374 unwrap(ToType))); 1375 } 1376 1377 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1378 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal), 1379 unwrap(ToType))); 1380 } 1381 1382 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal, 1383 LLVMTypeRef ToType) { 1384 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal), 1385 unwrap(ToType))); 1386 } 1387 1388 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal, 1389 LLVMTypeRef ToType) { 1390 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal), 1391 unwrap(ToType))); 1392 } 1393 1394 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal, 1395 LLVMTypeRef ToType) { 1396 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal), 1397 unwrap(ToType))); 1398 } 1399 1400 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal, 1401 LLVMTypeRef ToType) { 1402 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal), 1403 unwrap(ToType))); 1404 } 1405 1406 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal, 1407 LLVMTypeRef ToType) { 1408 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal), 1409 unwrap(ToType))); 1410 } 1411 1412 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType, 1413 LLVMBool isSigned) { 1414 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal), 1415 unwrap(ToType), isSigned)); 1416 } 1417 1418 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1419 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal), 1420 unwrap(ToType))); 1421 } 1422 1423 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition, 1424 LLVMValueRef ConstantIfTrue, 1425 LLVMValueRef ConstantIfFalse) { 1426 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition), 1427 unwrap<Constant>(ConstantIfTrue), 1428 unwrap<Constant>(ConstantIfFalse))); 1429 } 1430 1431 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant, 1432 LLVMValueRef IndexConstant) { 1433 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant), 1434 unwrap<Constant>(IndexConstant))); 1435 } 1436 1437 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant, 1438 LLVMValueRef ElementValueConstant, 1439 LLVMValueRef IndexConstant) { 1440 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant), 1441 unwrap<Constant>(ElementValueConstant), 1442 unwrap<Constant>(IndexConstant))); 1443 } 1444 1445 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant, 1446 LLVMValueRef VectorBConstant, 1447 LLVMValueRef MaskConstant) { 1448 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant), 1449 unwrap<Constant>(VectorBConstant), 1450 unwrap<Constant>(MaskConstant))); 1451 } 1452 1453 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList, 1454 unsigned NumIdx) { 1455 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant), 1456 makeArrayRef(IdxList, NumIdx))); 1457 } 1458 1459 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant, 1460 LLVMValueRef ElementValueConstant, 1461 unsigned *IdxList, unsigned NumIdx) { 1462 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant), 1463 unwrap<Constant>(ElementValueConstant), 1464 makeArrayRef(IdxList, NumIdx))); 1465 } 1466 1467 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString, 1468 const char *Constraints, 1469 LLVMBool HasSideEffects, 1470 LLVMBool IsAlignStack) { 1471 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString, 1472 Constraints, HasSideEffects, IsAlignStack)); 1473 } 1474 1475 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) { 1476 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB))); 1477 } 1478 1479 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/ 1480 1481 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) { 1482 return wrap(unwrap<GlobalValue>(Global)->getParent()); 1483 } 1484 1485 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) { 1486 return unwrap<GlobalValue>(Global)->isDeclaration(); 1487 } 1488 1489 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) { 1490 switch (unwrap<GlobalValue>(Global)->getLinkage()) { 1491 case GlobalValue::ExternalLinkage: 1492 return LLVMExternalLinkage; 1493 case GlobalValue::AvailableExternallyLinkage: 1494 return LLVMAvailableExternallyLinkage; 1495 case GlobalValue::LinkOnceAnyLinkage: 1496 return LLVMLinkOnceAnyLinkage; 1497 case GlobalValue::LinkOnceODRLinkage: 1498 return LLVMLinkOnceODRLinkage; 1499 case GlobalValue::WeakAnyLinkage: 1500 return LLVMWeakAnyLinkage; 1501 case GlobalValue::WeakODRLinkage: 1502 return LLVMWeakODRLinkage; 1503 case GlobalValue::AppendingLinkage: 1504 return LLVMAppendingLinkage; 1505 case GlobalValue::InternalLinkage: 1506 return LLVMInternalLinkage; 1507 case GlobalValue::PrivateLinkage: 1508 return LLVMPrivateLinkage; 1509 case GlobalValue::ExternalWeakLinkage: 1510 return LLVMExternalWeakLinkage; 1511 case GlobalValue::CommonLinkage: 1512 return LLVMCommonLinkage; 1513 } 1514 1515 llvm_unreachable("Invalid GlobalValue linkage!"); 1516 } 1517 1518 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) { 1519 GlobalValue *GV = unwrap<GlobalValue>(Global); 1520 1521 switch (Linkage) { 1522 case LLVMExternalLinkage: 1523 GV->setLinkage(GlobalValue::ExternalLinkage); 1524 break; 1525 case LLVMAvailableExternallyLinkage: 1526 GV->setLinkage(GlobalValue::AvailableExternallyLinkage); 1527 break; 1528 case LLVMLinkOnceAnyLinkage: 1529 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage); 1530 break; 1531 case LLVMLinkOnceODRLinkage: 1532 GV->setLinkage(GlobalValue::LinkOnceODRLinkage); 1533 break; 1534 case LLVMLinkOnceODRAutoHideLinkage: 1535 DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no " 1536 "longer supported."); 1537 break; 1538 case LLVMWeakAnyLinkage: 1539 GV->setLinkage(GlobalValue::WeakAnyLinkage); 1540 break; 1541 case LLVMWeakODRLinkage: 1542 GV->setLinkage(GlobalValue::WeakODRLinkage); 1543 break; 1544 case LLVMAppendingLinkage: 1545 GV->setLinkage(GlobalValue::AppendingLinkage); 1546 break; 1547 case LLVMInternalLinkage: 1548 GV->setLinkage(GlobalValue::InternalLinkage); 1549 break; 1550 case LLVMPrivateLinkage: 1551 GV->setLinkage(GlobalValue::PrivateLinkage); 1552 break; 1553 case LLVMLinkerPrivateLinkage: 1554 GV->setLinkage(GlobalValue::PrivateLinkage); 1555 break; 1556 case LLVMLinkerPrivateWeakLinkage: 1557 GV->setLinkage(GlobalValue::PrivateLinkage); 1558 break; 1559 case LLVMDLLImportLinkage: 1560 DEBUG(errs() 1561 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported."); 1562 break; 1563 case LLVMDLLExportLinkage: 1564 DEBUG(errs() 1565 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported."); 1566 break; 1567 case LLVMExternalWeakLinkage: 1568 GV->setLinkage(GlobalValue::ExternalWeakLinkage); 1569 break; 1570 case LLVMGhostLinkage: 1571 DEBUG(errs() 1572 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported."); 1573 break; 1574 case LLVMCommonLinkage: 1575 GV->setLinkage(GlobalValue::CommonLinkage); 1576 break; 1577 } 1578 } 1579 1580 const char *LLVMGetSection(LLVMValueRef Global) { 1581 // Using .data() is safe because of how GlobalObject::setSection is 1582 // implemented. 1583 return unwrap<GlobalValue>(Global)->getSection().data(); 1584 } 1585 1586 void LLVMSetSection(LLVMValueRef Global, const char *Section) { 1587 unwrap<GlobalObject>(Global)->setSection(Section); 1588 } 1589 1590 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) { 1591 return static_cast<LLVMVisibility>( 1592 unwrap<GlobalValue>(Global)->getVisibility()); 1593 } 1594 1595 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) { 1596 unwrap<GlobalValue>(Global) 1597 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz)); 1598 } 1599 1600 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) { 1601 return static_cast<LLVMDLLStorageClass>( 1602 unwrap<GlobalValue>(Global)->getDLLStorageClass()); 1603 } 1604 1605 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) { 1606 unwrap<GlobalValue>(Global)->setDLLStorageClass( 1607 static_cast<GlobalValue::DLLStorageClassTypes>(Class)); 1608 } 1609 1610 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) { 1611 return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr(); 1612 } 1613 1614 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) { 1615 unwrap<GlobalValue>(Global)->setUnnamedAddr( 1616 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global 1617 : GlobalValue::UnnamedAddr::None); 1618 } 1619 1620 /*--.. Operations on global variables, load and store instructions .........--*/ 1621 1622 unsigned LLVMGetAlignment(LLVMValueRef V) { 1623 Value *P = unwrap<Value>(V); 1624 if (GlobalValue *GV = dyn_cast<GlobalValue>(P)) 1625 return GV->getAlignment(); 1626 if (AllocaInst *AI = dyn_cast<AllocaInst>(P)) 1627 return AI->getAlignment(); 1628 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 1629 return LI->getAlignment(); 1630 if (StoreInst *SI = dyn_cast<StoreInst>(P)) 1631 return SI->getAlignment(); 1632 1633 llvm_unreachable( 1634 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment"); 1635 } 1636 1637 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) { 1638 Value *P = unwrap<Value>(V); 1639 if (GlobalObject *GV = dyn_cast<GlobalObject>(P)) 1640 GV->setAlignment(Bytes); 1641 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P)) 1642 AI->setAlignment(Bytes); 1643 else if (LoadInst *LI = dyn_cast<LoadInst>(P)) 1644 LI->setAlignment(Bytes); 1645 else if (StoreInst *SI = dyn_cast<StoreInst>(P)) 1646 SI->setAlignment(Bytes); 1647 else 1648 llvm_unreachable( 1649 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment"); 1650 } 1651 1652 /*--.. Operations on global variables ......................................--*/ 1653 1654 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) { 1655 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false, 1656 GlobalValue::ExternalLinkage, nullptr, Name)); 1657 } 1658 1659 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty, 1660 const char *Name, 1661 unsigned AddressSpace) { 1662 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false, 1663 GlobalValue::ExternalLinkage, nullptr, Name, 1664 nullptr, GlobalVariable::NotThreadLocal, 1665 AddressSpace)); 1666 } 1667 1668 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) { 1669 return wrap(unwrap(M)->getNamedGlobal(Name)); 1670 } 1671 1672 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) { 1673 Module *Mod = unwrap(M); 1674 Module::global_iterator I = Mod->global_begin(); 1675 if (I == Mod->global_end()) 1676 return nullptr; 1677 return wrap(&*I); 1678 } 1679 1680 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) { 1681 Module *Mod = unwrap(M); 1682 Module::global_iterator I = Mod->global_end(); 1683 if (I == Mod->global_begin()) 1684 return nullptr; 1685 return wrap(&*--I); 1686 } 1687 1688 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) { 1689 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar); 1690 Module::global_iterator I(GV); 1691 if (++I == GV->getParent()->global_end()) 1692 return nullptr; 1693 return wrap(&*I); 1694 } 1695 1696 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) { 1697 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar); 1698 Module::global_iterator I(GV); 1699 if (I == GV->getParent()->global_begin()) 1700 return nullptr; 1701 return wrap(&*--I); 1702 } 1703 1704 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) { 1705 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent(); 1706 } 1707 1708 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) { 1709 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar); 1710 if ( !GV->hasInitializer() ) 1711 return nullptr; 1712 return wrap(GV->getInitializer()); 1713 } 1714 1715 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) { 1716 unwrap<GlobalVariable>(GlobalVar) 1717 ->setInitializer(unwrap<Constant>(ConstantVal)); 1718 } 1719 1720 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) { 1721 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal(); 1722 } 1723 1724 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) { 1725 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0); 1726 } 1727 1728 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) { 1729 return unwrap<GlobalVariable>(GlobalVar)->isConstant(); 1730 } 1731 1732 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) { 1733 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0); 1734 } 1735 1736 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) { 1737 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) { 1738 case GlobalVariable::NotThreadLocal: 1739 return LLVMNotThreadLocal; 1740 case GlobalVariable::GeneralDynamicTLSModel: 1741 return LLVMGeneralDynamicTLSModel; 1742 case GlobalVariable::LocalDynamicTLSModel: 1743 return LLVMLocalDynamicTLSModel; 1744 case GlobalVariable::InitialExecTLSModel: 1745 return LLVMInitialExecTLSModel; 1746 case GlobalVariable::LocalExecTLSModel: 1747 return LLVMLocalExecTLSModel; 1748 } 1749 1750 llvm_unreachable("Invalid GlobalVariable thread local mode"); 1751 } 1752 1753 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) { 1754 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar); 1755 1756 switch (Mode) { 1757 case LLVMNotThreadLocal: 1758 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal); 1759 break; 1760 case LLVMGeneralDynamicTLSModel: 1761 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel); 1762 break; 1763 case LLVMLocalDynamicTLSModel: 1764 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel); 1765 break; 1766 case LLVMInitialExecTLSModel: 1767 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel); 1768 break; 1769 case LLVMLocalExecTLSModel: 1770 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel); 1771 break; 1772 } 1773 } 1774 1775 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) { 1776 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized(); 1777 } 1778 1779 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) { 1780 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit); 1781 } 1782 1783 /*--.. Operations on aliases ......................................--*/ 1784 1785 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee, 1786 const char *Name) { 1787 auto *PTy = cast<PointerType>(unwrap(Ty)); 1788 return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(), 1789 GlobalValue::ExternalLinkage, Name, 1790 unwrap<Constant>(Aliasee), unwrap(M))); 1791 } 1792 1793 /*--.. Operations on functions .............................................--*/ 1794 1795 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name, 1796 LLVMTypeRef FunctionTy) { 1797 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy), 1798 GlobalValue::ExternalLinkage, Name, unwrap(M))); 1799 } 1800 1801 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) { 1802 return wrap(unwrap(M)->getFunction(Name)); 1803 } 1804 1805 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) { 1806 Module *Mod = unwrap(M); 1807 Module::iterator I = Mod->begin(); 1808 if (I == Mod->end()) 1809 return nullptr; 1810 return wrap(&*I); 1811 } 1812 1813 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) { 1814 Module *Mod = unwrap(M); 1815 Module::iterator I = Mod->end(); 1816 if (I == Mod->begin()) 1817 return nullptr; 1818 return wrap(&*--I); 1819 } 1820 1821 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) { 1822 Function *Func = unwrap<Function>(Fn); 1823 Module::iterator I(Func); 1824 if (++I == Func->getParent()->end()) 1825 return nullptr; 1826 return wrap(&*I); 1827 } 1828 1829 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) { 1830 Function *Func = unwrap<Function>(Fn); 1831 Module::iterator I(Func); 1832 if (I == Func->getParent()->begin()) 1833 return nullptr; 1834 return wrap(&*--I); 1835 } 1836 1837 void LLVMDeleteFunction(LLVMValueRef Fn) { 1838 unwrap<Function>(Fn)->eraseFromParent(); 1839 } 1840 1841 LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) { 1842 return unwrap<Function>(Fn)->hasPersonalityFn(); 1843 } 1844 1845 LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) { 1846 return wrap(unwrap<Function>(Fn)->getPersonalityFn()); 1847 } 1848 1849 void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) { 1850 unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn)); 1851 } 1852 1853 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) { 1854 if (Function *F = dyn_cast<Function>(unwrap(Fn))) 1855 return F->getIntrinsicID(); 1856 return 0; 1857 } 1858 1859 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) { 1860 return unwrap<Function>(Fn)->getCallingConv(); 1861 } 1862 1863 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) { 1864 return unwrap<Function>(Fn)->setCallingConv( 1865 static_cast<CallingConv::ID>(CC)); 1866 } 1867 1868 const char *LLVMGetGC(LLVMValueRef Fn) { 1869 Function *F = unwrap<Function>(Fn); 1870 return F->hasGC()? F->getGC().c_str() : nullptr; 1871 } 1872 1873 void LLVMSetGC(LLVMValueRef Fn, const char *GC) { 1874 Function *F = unwrap<Function>(Fn); 1875 if (GC) 1876 F->setGC(GC); 1877 else 1878 F->clearGC(); 1879 } 1880 1881 void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx, 1882 LLVMAttributeRef A) { 1883 unwrap<Function>(F)->addAttribute(Idx, unwrap(A)); 1884 } 1885 1886 unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) { 1887 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx); 1888 return AS.getNumAttributes(); 1889 } 1890 1891 void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx, 1892 LLVMAttributeRef *Attrs) { 1893 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx); 1894 for (auto A : AS) 1895 *Attrs++ = wrap(A); 1896 } 1897 1898 LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F, 1899 LLVMAttributeIndex Idx, 1900 unsigned KindID) { 1901 return wrap(unwrap<Function>(F)->getAttribute(Idx, 1902 (Attribute::AttrKind)KindID)); 1903 } 1904 1905 LLVMAttributeRef LLVMGetStringAttributeAtIndex(LLVMValueRef F, 1906 LLVMAttributeIndex Idx, 1907 const char *K, unsigned KLen) { 1908 return wrap(unwrap<Function>(F)->getAttribute(Idx, StringRef(K, KLen))); 1909 } 1910 1911 void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx, 1912 unsigned KindID) { 1913 unwrap<Function>(F)->removeAttribute(Idx, (Attribute::AttrKind)KindID); 1914 } 1915 1916 void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx, 1917 const char *K, unsigned KLen) { 1918 unwrap<Function>(F)->removeAttribute(Idx, StringRef(K, KLen)); 1919 } 1920 1921 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A, 1922 const char *V) { 1923 Function *Func = unwrap<Function>(Fn); 1924 Attribute Attr = Attribute::get(Func->getContext(), A, V); 1925 Func->addAttribute(AttributeList::FunctionIndex, Attr); 1926 } 1927 1928 /*--.. Operations on parameters ............................................--*/ 1929 1930 unsigned LLVMCountParams(LLVMValueRef FnRef) { 1931 // This function is strictly redundant to 1932 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef))) 1933 return unwrap<Function>(FnRef)->arg_size(); 1934 } 1935 1936 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) { 1937 Function *Fn = unwrap<Function>(FnRef); 1938 for (Function::arg_iterator I = Fn->arg_begin(), 1939 E = Fn->arg_end(); I != E; I++) 1940 *ParamRefs++ = wrap(&*I); 1941 } 1942 1943 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) { 1944 Function *Fn = unwrap<Function>(FnRef); 1945 return wrap(&Fn->arg_begin()[index]); 1946 } 1947 1948 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) { 1949 return wrap(unwrap<Argument>(V)->getParent()); 1950 } 1951 1952 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) { 1953 Function *Func = unwrap<Function>(Fn); 1954 Function::arg_iterator I = Func->arg_begin(); 1955 if (I == Func->arg_end()) 1956 return nullptr; 1957 return wrap(&*I); 1958 } 1959 1960 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) { 1961 Function *Func = unwrap<Function>(Fn); 1962 Function::arg_iterator I = Func->arg_end(); 1963 if (I == Func->arg_begin()) 1964 return nullptr; 1965 return wrap(&*--I); 1966 } 1967 1968 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) { 1969 Argument *A = unwrap<Argument>(Arg); 1970 Function *Fn = A->getParent(); 1971 if (A->getArgNo() + 1 >= Fn->arg_size()) 1972 return nullptr; 1973 return wrap(&Fn->arg_begin()[A->getArgNo() + 1]); 1974 } 1975 1976 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) { 1977 Argument *A = unwrap<Argument>(Arg); 1978 if (A->getArgNo() == 0) 1979 return nullptr; 1980 return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]); 1981 } 1982 1983 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) { 1984 Argument *A = unwrap<Argument>(Arg); 1985 A->addAttr(Attribute::getWithAlignment(A->getContext(), align)); 1986 } 1987 1988 /*--.. Operations on basic blocks ..........................................--*/ 1989 1990 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) { 1991 return wrap(static_cast<Value*>(unwrap(BB))); 1992 } 1993 1994 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) { 1995 return isa<BasicBlock>(unwrap(Val)); 1996 } 1997 1998 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) { 1999 return wrap(unwrap<BasicBlock>(Val)); 2000 } 2001 2002 const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) { 2003 return unwrap(BB)->getName().data(); 2004 } 2005 2006 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) { 2007 return wrap(unwrap(BB)->getParent()); 2008 } 2009 2010 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) { 2011 return wrap(unwrap(BB)->getTerminator()); 2012 } 2013 2014 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) { 2015 return unwrap<Function>(FnRef)->size(); 2016 } 2017 2018 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){ 2019 Function *Fn = unwrap<Function>(FnRef); 2020 for (BasicBlock &BB : *Fn) 2021 *BasicBlocksRefs++ = wrap(&BB); 2022 } 2023 2024 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) { 2025 return wrap(&unwrap<Function>(Fn)->getEntryBlock()); 2026 } 2027 2028 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) { 2029 Function *Func = unwrap<Function>(Fn); 2030 Function::iterator I = Func->begin(); 2031 if (I == Func->end()) 2032 return nullptr; 2033 return wrap(&*I); 2034 } 2035 2036 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) { 2037 Function *Func = unwrap<Function>(Fn); 2038 Function::iterator I = Func->end(); 2039 if (I == Func->begin()) 2040 return nullptr; 2041 return wrap(&*--I); 2042 } 2043 2044 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) { 2045 BasicBlock *Block = unwrap(BB); 2046 Function::iterator I(Block); 2047 if (++I == Block->getParent()->end()) 2048 return nullptr; 2049 return wrap(&*I); 2050 } 2051 2052 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) { 2053 BasicBlock *Block = unwrap(BB); 2054 Function::iterator I(Block); 2055 if (I == Block->getParent()->begin()) 2056 return nullptr; 2057 return wrap(&*--I); 2058 } 2059 2060 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C, 2061 LLVMValueRef FnRef, 2062 const char *Name) { 2063 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef))); 2064 } 2065 2066 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) { 2067 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name); 2068 } 2069 2070 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C, 2071 LLVMBasicBlockRef BBRef, 2072 const char *Name) { 2073 BasicBlock *BB = unwrap(BBRef); 2074 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB)); 2075 } 2076 2077 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef, 2078 const char *Name) { 2079 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name); 2080 } 2081 2082 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) { 2083 unwrap(BBRef)->eraseFromParent(); 2084 } 2085 2086 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) { 2087 unwrap(BBRef)->removeFromParent(); 2088 } 2089 2090 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) { 2091 unwrap(BB)->moveBefore(unwrap(MovePos)); 2092 } 2093 2094 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) { 2095 unwrap(BB)->moveAfter(unwrap(MovePos)); 2096 } 2097 2098 /*--.. Operations on instructions ..........................................--*/ 2099 2100 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) { 2101 return wrap(unwrap<Instruction>(Inst)->getParent()); 2102 } 2103 2104 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) { 2105 BasicBlock *Block = unwrap(BB); 2106 BasicBlock::iterator I = Block->begin(); 2107 if (I == Block->end()) 2108 return nullptr; 2109 return wrap(&*I); 2110 } 2111 2112 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) { 2113 BasicBlock *Block = unwrap(BB); 2114 BasicBlock::iterator I = Block->end(); 2115 if (I == Block->begin()) 2116 return nullptr; 2117 return wrap(&*--I); 2118 } 2119 2120 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) { 2121 Instruction *Instr = unwrap<Instruction>(Inst); 2122 BasicBlock::iterator I(Instr); 2123 if (++I == Instr->getParent()->end()) 2124 return nullptr; 2125 return wrap(&*I); 2126 } 2127 2128 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) { 2129 Instruction *Instr = unwrap<Instruction>(Inst); 2130 BasicBlock::iterator I(Instr); 2131 if (I == Instr->getParent()->begin()) 2132 return nullptr; 2133 return wrap(&*--I); 2134 } 2135 2136 void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) { 2137 unwrap<Instruction>(Inst)->removeFromParent(); 2138 } 2139 2140 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) { 2141 unwrap<Instruction>(Inst)->eraseFromParent(); 2142 } 2143 2144 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) { 2145 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst))) 2146 return (LLVMIntPredicate)I->getPredicate(); 2147 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst))) 2148 if (CE->getOpcode() == Instruction::ICmp) 2149 return (LLVMIntPredicate)CE->getPredicate(); 2150 return (LLVMIntPredicate)0; 2151 } 2152 2153 LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) { 2154 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst))) 2155 return (LLVMRealPredicate)I->getPredicate(); 2156 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst))) 2157 if (CE->getOpcode() == Instruction::FCmp) 2158 return (LLVMRealPredicate)CE->getPredicate(); 2159 return (LLVMRealPredicate)0; 2160 } 2161 2162 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) { 2163 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst))) 2164 return map_to_llvmopcode(C->getOpcode()); 2165 return (LLVMOpcode)0; 2166 } 2167 2168 LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) { 2169 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst))) 2170 return wrap(C->clone()); 2171 return nullptr; 2172 } 2173 2174 /*--.. Call and invoke instructions ........................................--*/ 2175 2176 unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) { 2177 return CallSite(unwrap<Instruction>(Instr)).getNumArgOperands(); 2178 } 2179 2180 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) { 2181 return CallSite(unwrap<Instruction>(Instr)).getCallingConv(); 2182 } 2183 2184 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) { 2185 return CallSite(unwrap<Instruction>(Instr)) 2186 .setCallingConv(static_cast<CallingConv::ID>(CC)); 2187 } 2188 2189 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, 2190 unsigned align) { 2191 CallSite Call = CallSite(unwrap<Instruction>(Instr)); 2192 Attribute AlignAttr = Attribute::getWithAlignment(Call->getContext(), align); 2193 Call.addAttribute(index, AlignAttr); 2194 } 2195 2196 void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx, 2197 LLVMAttributeRef A) { 2198 CallSite(unwrap<Instruction>(C)).addAttribute(Idx, unwrap(A)); 2199 } 2200 2201 unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C, 2202 LLVMAttributeIndex Idx) { 2203 auto CS = CallSite(unwrap<Instruction>(C)); 2204 auto AS = CS.getAttributes().getAttributes(Idx); 2205 return AS.getNumAttributes(); 2206 } 2207 2208 void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx, 2209 LLVMAttributeRef *Attrs) { 2210 auto CS = CallSite(unwrap<Instruction>(C)); 2211 auto AS = CS.getAttributes().getAttributes(Idx); 2212 for (auto A : AS) 2213 *Attrs++ = wrap(A); 2214 } 2215 2216 LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C, 2217 LLVMAttributeIndex Idx, 2218 unsigned KindID) { 2219 return wrap(CallSite(unwrap<Instruction>(C)) 2220 .getAttribute(Idx, (Attribute::AttrKind)KindID)); 2221 } 2222 2223 LLVMAttributeRef LLVMGetCallSiteStringAttribute(LLVMValueRef C, 2224 LLVMAttributeIndex Idx, 2225 const char *K, unsigned KLen) { 2226 return wrap(CallSite(unwrap<Instruction>(C)) 2227 .getAttribute(Idx, StringRef(K, KLen))); 2228 } 2229 2230 void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C, LLVMAttributeIndex Idx, 2231 unsigned KindID) { 2232 CallSite(unwrap<Instruction>(C)) 2233 .removeAttribute(Idx, (Attribute::AttrKind)KindID); 2234 } 2235 2236 void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C, LLVMAttributeIndex Idx, 2237 const char *K, unsigned KLen) { 2238 CallSite(unwrap<Instruction>(C)).removeAttribute(Idx, StringRef(K, KLen)); 2239 } 2240 2241 LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) { 2242 return wrap(CallSite(unwrap<Instruction>(Instr)).getCalledValue()); 2243 } 2244 2245 /*--.. Operations on call instructions (only) ..............................--*/ 2246 2247 LLVMBool LLVMIsTailCall(LLVMValueRef Call) { 2248 return unwrap<CallInst>(Call)->isTailCall(); 2249 } 2250 2251 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) { 2252 unwrap<CallInst>(Call)->setTailCall(isTailCall); 2253 } 2254 2255 /*--.. Operations on invoke instructions (only) ............................--*/ 2256 2257 LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) { 2258 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest()); 2259 } 2260 2261 LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) { 2262 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest()); 2263 } 2264 2265 void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) { 2266 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B)); 2267 } 2268 2269 void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) { 2270 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B)); 2271 } 2272 2273 /*--.. Operations on terminators ...........................................--*/ 2274 2275 unsigned LLVMGetNumSuccessors(LLVMValueRef Term) { 2276 return unwrap<TerminatorInst>(Term)->getNumSuccessors(); 2277 } 2278 2279 LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) { 2280 return wrap(unwrap<TerminatorInst>(Term)->getSuccessor(i)); 2281 } 2282 2283 void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) { 2284 return unwrap<TerminatorInst>(Term)->setSuccessor(i,unwrap(block)); 2285 } 2286 2287 /*--.. Operations on branch instructions (only) ............................--*/ 2288 2289 LLVMBool LLVMIsConditional(LLVMValueRef Branch) { 2290 return unwrap<BranchInst>(Branch)->isConditional(); 2291 } 2292 2293 LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) { 2294 return wrap(unwrap<BranchInst>(Branch)->getCondition()); 2295 } 2296 2297 void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) { 2298 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond)); 2299 } 2300 2301 /*--.. Operations on switch instructions (only) ............................--*/ 2302 2303 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) { 2304 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest()); 2305 } 2306 2307 /*--.. Operations on alloca instructions (only) ............................--*/ 2308 2309 LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) { 2310 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType()); 2311 } 2312 2313 /*--.. Operations on gep instructions (only) ...............................--*/ 2314 2315 LLVMBool LLVMIsInBounds(LLVMValueRef GEP) { 2316 return unwrap<GetElementPtrInst>(GEP)->isInBounds(); 2317 } 2318 2319 void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) { 2320 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds); 2321 } 2322 2323 /*--.. Operations on phi nodes .............................................--*/ 2324 2325 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues, 2326 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) { 2327 PHINode *PhiVal = unwrap<PHINode>(PhiNode); 2328 for (unsigned I = 0; I != Count; ++I) 2329 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I])); 2330 } 2331 2332 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) { 2333 return unwrap<PHINode>(PhiNode)->getNumIncomingValues(); 2334 } 2335 2336 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) { 2337 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index)); 2338 } 2339 2340 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) { 2341 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index)); 2342 } 2343 2344 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/ 2345 2346 unsigned LLVMGetNumIndices(LLVMValueRef Inst) { 2347 auto *I = unwrap(Inst); 2348 if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) 2349 return GEP->getNumIndices(); 2350 if (auto *EV = dyn_cast<ExtractValueInst>(I)) 2351 return EV->getNumIndices(); 2352 if (auto *IV = dyn_cast<InsertValueInst>(I)) 2353 return IV->getNumIndices(); 2354 llvm_unreachable( 2355 "LLVMGetNumIndices applies only to extractvalue and insertvalue!"); 2356 } 2357 2358 const unsigned *LLVMGetIndices(LLVMValueRef Inst) { 2359 auto *I = unwrap(Inst); 2360 if (auto *EV = dyn_cast<ExtractValueInst>(I)) 2361 return EV->getIndices().data(); 2362 if (auto *IV = dyn_cast<InsertValueInst>(I)) 2363 return IV->getIndices().data(); 2364 llvm_unreachable( 2365 "LLVMGetIndices applies only to extractvalue and insertvalue!"); 2366 } 2367 2368 2369 /*===-- Instruction builders ----------------------------------------------===*/ 2370 2371 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) { 2372 return wrap(new IRBuilder<>(*unwrap(C))); 2373 } 2374 2375 LLVMBuilderRef LLVMCreateBuilder(void) { 2376 return LLVMCreateBuilderInContext(LLVMGetGlobalContext()); 2377 } 2378 2379 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block, 2380 LLVMValueRef Instr) { 2381 BasicBlock *BB = unwrap(Block); 2382 auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end(); 2383 unwrap(Builder)->SetInsertPoint(BB, I); 2384 } 2385 2386 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) { 2387 Instruction *I = unwrap<Instruction>(Instr); 2388 unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator()); 2389 } 2390 2391 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) { 2392 BasicBlock *BB = unwrap(Block); 2393 unwrap(Builder)->SetInsertPoint(BB); 2394 } 2395 2396 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) { 2397 return wrap(unwrap(Builder)->GetInsertBlock()); 2398 } 2399 2400 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) { 2401 unwrap(Builder)->ClearInsertionPoint(); 2402 } 2403 2404 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) { 2405 unwrap(Builder)->Insert(unwrap<Instruction>(Instr)); 2406 } 2407 2408 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr, 2409 const char *Name) { 2410 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name); 2411 } 2412 2413 void LLVMDisposeBuilder(LLVMBuilderRef Builder) { 2414 delete unwrap(Builder); 2415 } 2416 2417 /*--.. Metadata builders ...................................................--*/ 2418 2419 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) { 2420 MDNode *Loc = 2421 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr; 2422 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc)); 2423 } 2424 2425 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) { 2426 LLVMContext &Context = unwrap(Builder)->getContext(); 2427 return wrap(MetadataAsValue::get( 2428 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode())); 2429 } 2430 2431 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) { 2432 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst)); 2433 } 2434 2435 2436 /*--.. Instruction builders ................................................--*/ 2437 2438 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) { 2439 return wrap(unwrap(B)->CreateRetVoid()); 2440 } 2441 2442 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) { 2443 return wrap(unwrap(B)->CreateRet(unwrap(V))); 2444 } 2445 2446 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals, 2447 unsigned N) { 2448 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N)); 2449 } 2450 2451 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) { 2452 return wrap(unwrap(B)->CreateBr(unwrap(Dest))); 2453 } 2454 2455 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If, 2456 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) { 2457 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else))); 2458 } 2459 2460 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V, 2461 LLVMBasicBlockRef Else, unsigned NumCases) { 2462 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases)); 2463 } 2464 2465 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr, 2466 unsigned NumDests) { 2467 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests)); 2468 } 2469 2470 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn, 2471 LLVMValueRef *Args, unsigned NumArgs, 2472 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch, 2473 const char *Name) { 2474 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch), 2475 makeArrayRef(unwrap(Args), NumArgs), 2476 Name)); 2477 } 2478 2479 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty, 2480 LLVMValueRef PersFn, unsigned NumClauses, 2481 const char *Name) { 2482 // The personality used to live on the landingpad instruction, but now it 2483 // lives on the parent function. For compatibility, take the provided 2484 // personality and put it on the parent function. 2485 if (PersFn) 2486 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn( 2487 cast<Function>(unwrap(PersFn))); 2488 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name)); 2489 } 2490 2491 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) { 2492 return wrap(unwrap(B)->CreateResume(unwrap(Exn))); 2493 } 2494 2495 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) { 2496 return wrap(unwrap(B)->CreateUnreachable()); 2497 } 2498 2499 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal, 2500 LLVMBasicBlockRef Dest) { 2501 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest)); 2502 } 2503 2504 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) { 2505 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest)); 2506 } 2507 2508 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) { 2509 return unwrap<LandingPadInst>(LandingPad)->getNumClauses(); 2510 } 2511 2512 LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) { 2513 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx)); 2514 } 2515 2516 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) { 2517 unwrap<LandingPadInst>(LandingPad)-> 2518 addClause(cast<Constant>(unwrap(ClauseVal))); 2519 } 2520 2521 LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) { 2522 return unwrap<LandingPadInst>(LandingPad)->isCleanup(); 2523 } 2524 2525 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) { 2526 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val); 2527 } 2528 2529 /*--.. Arithmetic ..........................................................--*/ 2530 2531 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2532 const char *Name) { 2533 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name)); 2534 } 2535 2536 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2537 const char *Name) { 2538 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name)); 2539 } 2540 2541 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2542 const char *Name) { 2543 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name)); 2544 } 2545 2546 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2547 const char *Name) { 2548 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name)); 2549 } 2550 2551 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2552 const char *Name) { 2553 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name)); 2554 } 2555 2556 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2557 const char *Name) { 2558 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name)); 2559 } 2560 2561 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2562 const char *Name) { 2563 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name)); 2564 } 2565 2566 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2567 const char *Name) { 2568 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name)); 2569 } 2570 2571 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2572 const char *Name) { 2573 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name)); 2574 } 2575 2576 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2577 const char *Name) { 2578 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name)); 2579 } 2580 2581 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2582 const char *Name) { 2583 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name)); 2584 } 2585 2586 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2587 const char *Name) { 2588 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name)); 2589 } 2590 2591 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2592 const char *Name) { 2593 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name)); 2594 } 2595 2596 LLVMValueRef LLVMBuildExactUDiv(LLVMBuilderRef B, LLVMValueRef LHS, 2597 LLVMValueRef RHS, const char *Name) { 2598 return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name)); 2599 } 2600 2601 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2602 const char *Name) { 2603 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name)); 2604 } 2605 2606 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS, 2607 LLVMValueRef RHS, const char *Name) { 2608 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name)); 2609 } 2610 2611 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2612 const char *Name) { 2613 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name)); 2614 } 2615 2616 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2617 const char *Name) { 2618 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name)); 2619 } 2620 2621 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2622 const char *Name) { 2623 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name)); 2624 } 2625 2626 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2627 const char *Name) { 2628 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name)); 2629 } 2630 2631 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2632 const char *Name) { 2633 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name)); 2634 } 2635 2636 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2637 const char *Name) { 2638 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name)); 2639 } 2640 2641 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2642 const char *Name) { 2643 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name)); 2644 } 2645 2646 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2647 const char *Name) { 2648 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name)); 2649 } 2650 2651 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2652 const char *Name) { 2653 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name)); 2654 } 2655 2656 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2657 const char *Name) { 2658 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name)); 2659 } 2660 2661 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op, 2662 LLVMValueRef LHS, LLVMValueRef RHS, 2663 const char *Name) { 2664 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS), 2665 unwrap(RHS), Name)); 2666 } 2667 2668 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) { 2669 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name)); 2670 } 2671 2672 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V, 2673 const char *Name) { 2674 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name)); 2675 } 2676 2677 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V, 2678 const char *Name) { 2679 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name)); 2680 } 2681 2682 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) { 2683 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name)); 2684 } 2685 2686 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) { 2687 return wrap(unwrap(B)->CreateNot(unwrap(V), Name)); 2688 } 2689 2690 /*--.. Memory ..............................................................--*/ 2691 2692 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty, 2693 const char *Name) { 2694 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext()); 2695 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty)); 2696 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy); 2697 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 2698 ITy, unwrap(Ty), AllocSize, 2699 nullptr, nullptr, ""); 2700 return wrap(unwrap(B)->Insert(Malloc, Twine(Name))); 2701 } 2702 2703 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty, 2704 LLVMValueRef Val, const char *Name) { 2705 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext()); 2706 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty)); 2707 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy); 2708 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 2709 ITy, unwrap(Ty), AllocSize, 2710 unwrap(Val), nullptr, ""); 2711 return wrap(unwrap(B)->Insert(Malloc, Twine(Name))); 2712 } 2713 2714 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty, 2715 const char *Name) { 2716 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name)); 2717 } 2718 2719 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty, 2720 LLVMValueRef Val, const char *Name) { 2721 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name)); 2722 } 2723 2724 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) { 2725 return wrap(unwrap(B)->Insert( 2726 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock()))); 2727 } 2728 2729 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal, 2730 const char *Name) { 2731 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name)); 2732 } 2733 2734 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 2735 LLVMValueRef PointerVal) { 2736 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal))); 2737 } 2738 2739 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) { 2740 switch (Ordering) { 2741 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic; 2742 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered; 2743 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic; 2744 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire; 2745 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release; 2746 case LLVMAtomicOrderingAcquireRelease: 2747 return AtomicOrdering::AcquireRelease; 2748 case LLVMAtomicOrderingSequentiallyConsistent: 2749 return AtomicOrdering::SequentiallyConsistent; 2750 } 2751 2752 llvm_unreachable("Invalid LLVMAtomicOrdering value!"); 2753 } 2754 2755 static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) { 2756 switch (Ordering) { 2757 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic; 2758 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered; 2759 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic; 2760 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire; 2761 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease; 2762 case AtomicOrdering::AcquireRelease: 2763 return LLVMAtomicOrderingAcquireRelease; 2764 case AtomicOrdering::SequentiallyConsistent: 2765 return LLVMAtomicOrderingSequentiallyConsistent; 2766 } 2767 2768 llvm_unreachable("Invalid AtomicOrdering value!"); 2769 } 2770 2771 // TODO: Should this and other atomic instructions support building with 2772 // "syncscope"? 2773 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering, 2774 LLVMBool isSingleThread, const char *Name) { 2775 return wrap( 2776 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering), 2777 isSingleThread ? SyncScope::SingleThread 2778 : SyncScope::System, 2779 Name)); 2780 } 2781 2782 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer, 2783 LLVMValueRef *Indices, unsigned NumIndices, 2784 const char *Name) { 2785 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices); 2786 return wrap(unwrap(B)->CreateGEP(nullptr, unwrap(Pointer), IdxList, Name)); 2787 } 2788 2789 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer, 2790 LLVMValueRef *Indices, unsigned NumIndices, 2791 const char *Name) { 2792 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices); 2793 return wrap( 2794 unwrap(B)->CreateInBoundsGEP(nullptr, unwrap(Pointer), IdxList, Name)); 2795 } 2796 2797 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer, 2798 unsigned Idx, const char *Name) { 2799 return wrap(unwrap(B)->CreateStructGEP(nullptr, unwrap(Pointer), Idx, Name)); 2800 } 2801 2802 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str, 2803 const char *Name) { 2804 return wrap(unwrap(B)->CreateGlobalString(Str, Name)); 2805 } 2806 2807 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str, 2808 const char *Name) { 2809 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name)); 2810 } 2811 2812 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) { 2813 Value *P = unwrap<Value>(MemAccessInst); 2814 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 2815 return LI->isVolatile(); 2816 return cast<StoreInst>(P)->isVolatile(); 2817 } 2818 2819 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) { 2820 Value *P = unwrap<Value>(MemAccessInst); 2821 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 2822 return LI->setVolatile(isVolatile); 2823 return cast<StoreInst>(P)->setVolatile(isVolatile); 2824 } 2825 2826 LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) { 2827 Value *P = unwrap<Value>(MemAccessInst); 2828 AtomicOrdering O; 2829 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 2830 O = LI->getOrdering(); 2831 else 2832 O = cast<StoreInst>(P)->getOrdering(); 2833 return mapToLLVMOrdering(O); 2834 } 2835 2836 void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) { 2837 Value *P = unwrap<Value>(MemAccessInst); 2838 AtomicOrdering O = mapFromLLVMOrdering(Ordering); 2839 2840 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 2841 return LI->setOrdering(O); 2842 return cast<StoreInst>(P)->setOrdering(O); 2843 } 2844 2845 /*--.. Casts ...............................................................--*/ 2846 2847 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val, 2848 LLVMTypeRef DestTy, const char *Name) { 2849 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name)); 2850 } 2851 2852 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val, 2853 LLVMTypeRef DestTy, const char *Name) { 2854 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name)); 2855 } 2856 2857 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val, 2858 LLVMTypeRef DestTy, const char *Name) { 2859 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name)); 2860 } 2861 2862 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val, 2863 LLVMTypeRef DestTy, const char *Name) { 2864 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name)); 2865 } 2866 2867 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val, 2868 LLVMTypeRef DestTy, const char *Name) { 2869 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name)); 2870 } 2871 2872 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val, 2873 LLVMTypeRef DestTy, const char *Name) { 2874 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name)); 2875 } 2876 2877 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val, 2878 LLVMTypeRef DestTy, const char *Name) { 2879 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name)); 2880 } 2881 2882 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val, 2883 LLVMTypeRef DestTy, const char *Name) { 2884 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name)); 2885 } 2886 2887 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val, 2888 LLVMTypeRef DestTy, const char *Name) { 2889 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name)); 2890 } 2891 2892 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val, 2893 LLVMTypeRef DestTy, const char *Name) { 2894 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name)); 2895 } 2896 2897 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val, 2898 LLVMTypeRef DestTy, const char *Name) { 2899 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name)); 2900 } 2901 2902 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val, 2903 LLVMTypeRef DestTy, const char *Name) { 2904 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name)); 2905 } 2906 2907 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val, 2908 LLVMTypeRef DestTy, const char *Name) { 2909 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name)); 2910 } 2911 2912 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val, 2913 LLVMTypeRef DestTy, const char *Name) { 2914 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy), 2915 Name)); 2916 } 2917 2918 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val, 2919 LLVMTypeRef DestTy, const char *Name) { 2920 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy), 2921 Name)); 2922 } 2923 2924 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val, 2925 LLVMTypeRef DestTy, const char *Name) { 2926 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy), 2927 Name)); 2928 } 2929 2930 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val, 2931 LLVMTypeRef DestTy, const char *Name) { 2932 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val), 2933 unwrap(DestTy), Name)); 2934 } 2935 2936 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val, 2937 LLVMTypeRef DestTy, const char *Name) { 2938 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name)); 2939 } 2940 2941 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val, 2942 LLVMTypeRef DestTy, const char *Name) { 2943 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy), 2944 /*isSigned*/true, Name)); 2945 } 2946 2947 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val, 2948 LLVMTypeRef DestTy, const char *Name) { 2949 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name)); 2950 } 2951 2952 /*--.. Comparisons .........................................................--*/ 2953 2954 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op, 2955 LLVMValueRef LHS, LLVMValueRef RHS, 2956 const char *Name) { 2957 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op), 2958 unwrap(LHS), unwrap(RHS), Name)); 2959 } 2960 2961 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op, 2962 LLVMValueRef LHS, LLVMValueRef RHS, 2963 const char *Name) { 2964 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op), 2965 unwrap(LHS), unwrap(RHS), Name)); 2966 } 2967 2968 /*--.. Miscellaneous instructions ..........................................--*/ 2969 2970 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) { 2971 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name)); 2972 } 2973 2974 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, 2975 LLVMValueRef *Args, unsigned NumArgs, 2976 const char *Name) { 2977 return wrap(unwrap(B)->CreateCall(unwrap(Fn), 2978 makeArrayRef(unwrap(Args), NumArgs), 2979 Name)); 2980 } 2981 2982 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If, 2983 LLVMValueRef Then, LLVMValueRef Else, 2984 const char *Name) { 2985 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else), 2986 Name)); 2987 } 2988 2989 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List, 2990 LLVMTypeRef Ty, const char *Name) { 2991 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name)); 2992 } 2993 2994 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal, 2995 LLVMValueRef Index, const char *Name) { 2996 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index), 2997 Name)); 2998 } 2999 3000 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal, 3001 LLVMValueRef EltVal, LLVMValueRef Index, 3002 const char *Name) { 3003 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal), 3004 unwrap(Index), Name)); 3005 } 3006 3007 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1, 3008 LLVMValueRef V2, LLVMValueRef Mask, 3009 const char *Name) { 3010 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2), 3011 unwrap(Mask), Name)); 3012 } 3013 3014 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal, 3015 unsigned Index, const char *Name) { 3016 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name)); 3017 } 3018 3019 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal, 3020 LLVMValueRef EltVal, unsigned Index, 3021 const char *Name) { 3022 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal), 3023 Index, Name)); 3024 } 3025 3026 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val, 3027 const char *Name) { 3028 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name)); 3029 } 3030 3031 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val, 3032 const char *Name) { 3033 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name)); 3034 } 3035 3036 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS, 3037 LLVMValueRef RHS, const char *Name) { 3038 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name)); 3039 } 3040 3041 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op, 3042 LLVMValueRef PTR, LLVMValueRef Val, 3043 LLVMAtomicOrdering ordering, 3044 LLVMBool singleThread) { 3045 AtomicRMWInst::BinOp intop; 3046 switch (op) { 3047 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break; 3048 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break; 3049 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break; 3050 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break; 3051 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break; 3052 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break; 3053 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break; 3054 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break; 3055 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break; 3056 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break; 3057 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break; 3058 } 3059 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val), 3060 mapFromLLVMOrdering(ordering), singleThread ? SyncScope::SingleThread 3061 : SyncScope::System)); 3062 } 3063 3064 LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr, 3065 LLVMValueRef Cmp, LLVMValueRef New, 3066 LLVMAtomicOrdering SuccessOrdering, 3067 LLVMAtomicOrdering FailureOrdering, 3068 LLVMBool singleThread) { 3069 3070 return wrap(unwrap(B)->CreateAtomicCmpXchg(unwrap(Ptr), unwrap(Cmp), 3071 unwrap(New), mapFromLLVMOrdering(SuccessOrdering), 3072 mapFromLLVMOrdering(FailureOrdering), 3073 singleThread ? SyncScope::SingleThread : SyncScope::System)); 3074 } 3075 3076 3077 LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) { 3078 Value *P = unwrap<Value>(AtomicInst); 3079 3080 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P)) 3081 return I->getSyncScopeID() == SyncScope::SingleThread; 3082 return cast<AtomicCmpXchgInst>(P)->getSyncScopeID() == 3083 SyncScope::SingleThread; 3084 } 3085 3086 void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) { 3087 Value *P = unwrap<Value>(AtomicInst); 3088 SyncScope::ID SSID = NewValue ? SyncScope::SingleThread : SyncScope::System; 3089 3090 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P)) 3091 return I->setSyncScopeID(SSID); 3092 return cast<AtomicCmpXchgInst>(P)->setSyncScopeID(SSID); 3093 } 3094 3095 LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) { 3096 Value *P = unwrap<Value>(CmpXchgInst); 3097 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering()); 3098 } 3099 3100 void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst, 3101 LLVMAtomicOrdering Ordering) { 3102 Value *P = unwrap<Value>(CmpXchgInst); 3103 AtomicOrdering O = mapFromLLVMOrdering(Ordering); 3104 3105 return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O); 3106 } 3107 3108 LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst) { 3109 Value *P = unwrap<Value>(CmpXchgInst); 3110 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering()); 3111 } 3112 3113 void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst, 3114 LLVMAtomicOrdering Ordering) { 3115 Value *P = unwrap<Value>(CmpXchgInst); 3116 AtomicOrdering O = mapFromLLVMOrdering(Ordering); 3117 3118 return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O); 3119 } 3120 3121 /*===-- Module providers --------------------------------------------------===*/ 3122 3123 LLVMModuleProviderRef 3124 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) { 3125 return reinterpret_cast<LLVMModuleProviderRef>(M); 3126 } 3127 3128 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) { 3129 delete unwrap(MP); 3130 } 3131 3132 3133 /*===-- Memory buffers ----------------------------------------------------===*/ 3134 3135 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile( 3136 const char *Path, 3137 LLVMMemoryBufferRef *OutMemBuf, 3138 char **OutMessage) { 3139 3140 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path); 3141 if (std::error_code EC = MBOrErr.getError()) { 3142 *OutMessage = strdup(EC.message().c_str()); 3143 return 1; 3144 } 3145 *OutMemBuf = wrap(MBOrErr.get().release()); 3146 return 0; 3147 } 3148 3149 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf, 3150 char **OutMessage) { 3151 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN(); 3152 if (std::error_code EC = MBOrErr.getError()) { 3153 *OutMessage = strdup(EC.message().c_str()); 3154 return 1; 3155 } 3156 *OutMemBuf = wrap(MBOrErr.get().release()); 3157 return 0; 3158 } 3159 3160 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange( 3161 const char *InputData, 3162 size_t InputDataLength, 3163 const char *BufferName, 3164 LLVMBool RequiresNullTerminator) { 3165 3166 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength), 3167 StringRef(BufferName), 3168 RequiresNullTerminator).release()); 3169 } 3170 3171 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy( 3172 const char *InputData, 3173 size_t InputDataLength, 3174 const char *BufferName) { 3175 3176 return wrap( 3177 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength), 3178 StringRef(BufferName)).release()); 3179 } 3180 3181 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) { 3182 return unwrap(MemBuf)->getBufferStart(); 3183 } 3184 3185 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) { 3186 return unwrap(MemBuf)->getBufferSize(); 3187 } 3188 3189 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) { 3190 delete unwrap(MemBuf); 3191 } 3192 3193 /*===-- Pass Registry -----------------------------------------------------===*/ 3194 3195 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) { 3196 return wrap(PassRegistry::getPassRegistry()); 3197 } 3198 3199 /*===-- Pass Manager ------------------------------------------------------===*/ 3200 3201 LLVMPassManagerRef LLVMCreatePassManager() { 3202 return wrap(new legacy::PassManager()); 3203 } 3204 3205 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) { 3206 return wrap(new legacy::FunctionPassManager(unwrap(M))); 3207 } 3208 3209 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) { 3210 return LLVMCreateFunctionPassManagerForModule( 3211 reinterpret_cast<LLVMModuleRef>(P)); 3212 } 3213 3214 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) { 3215 return unwrap<legacy::PassManager>(PM)->run(*unwrap(M)); 3216 } 3217 3218 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) { 3219 return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization(); 3220 } 3221 3222 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) { 3223 return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F)); 3224 } 3225 3226 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) { 3227 return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization(); 3228 } 3229 3230 void LLVMDisposePassManager(LLVMPassManagerRef PM) { 3231 delete unwrap(PM); 3232 } 3233 3234 /*===-- Threading ------------------------------------------------------===*/ 3235 3236 LLVMBool LLVMStartMultithreaded() { 3237 return LLVMIsMultithreaded(); 3238 } 3239 3240 void LLVMStopMultithreaded() { 3241 } 3242 3243 LLVMBool LLVMIsMultithreaded() { 3244 return llvm_is_multithreaded(); 3245 } 3246