Path: blob/main/contrib/llvm-project/llvm/lib/IR/Core.cpp
35233 views
//===-- Core.cpp ----------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements the common infrastructure (including the C bindings)9// for libLLVMCore.a, which implements the LLVM intermediate representation.10//11//===----------------------------------------------------------------------===//1213#include "llvm-c/Core.h"14#include "llvm/IR/Attributes.h"15#include "llvm/IR/BasicBlock.h"16#include "llvm/IR/ConstantRange.h"17#include "llvm/IR/Constants.h"18#include "llvm/IR/DebugInfoMetadata.h"19#include "llvm/IR/DerivedTypes.h"20#include "llvm/IR/DiagnosticInfo.h"21#include "llvm/IR/DiagnosticPrinter.h"22#include "llvm/IR/GlobalAlias.h"23#include "llvm/IR/GlobalVariable.h"24#include "llvm/IR/IRBuilder.h"25#include "llvm/IR/InlineAsm.h"26#include "llvm/IR/IntrinsicInst.h"27#include "llvm/IR/LLVMContext.h"28#include "llvm/IR/LegacyPassManager.h"29#include "llvm/IR/Module.h"30#include "llvm/InitializePasses.h"31#include "llvm/PassRegistry.h"32#include "llvm/Support/Debug.h"33#include "llvm/Support/ErrorHandling.h"34#include "llvm/Support/FileSystem.h"35#include "llvm/Support/ManagedStatic.h"36#include "llvm/Support/MathExtras.h"37#include "llvm/Support/MemoryBuffer.h"38#include "llvm/Support/Threading.h"39#include "llvm/Support/raw_ostream.h"40#include <cassert>41#include <cstdlib>42#include <cstring>43#include <system_error>4445using namespace llvm;4647DEFINE_SIMPLE_CONVERSION_FUNCTIONS(OperandBundleDef, LLVMOperandBundleRef)4849inline BasicBlock **unwrap(LLVMBasicBlockRef *BBs) {50return reinterpret_cast<BasicBlock **>(BBs);51}5253#define DEBUG_TYPE "ir"5455void llvm::initializeCore(PassRegistry &Registry) {56initializeDominatorTreeWrapperPassPass(Registry);57initializePrintModulePassWrapperPass(Registry);58initializePrintFunctionPassWrapperPass(Registry);59initializeSafepointIRVerifierPass(Registry);60initializeVerifierLegacyPassPass(Registry);61}6263void LLVMShutdown() {64llvm_shutdown();65}6667/*===-- Version query -----------------------------------------------------===*/6869void LLVMGetVersion(unsigned *Major, unsigned *Minor, unsigned *Patch) {70if (Major)71*Major = LLVM_VERSION_MAJOR;72if (Minor)73*Minor = LLVM_VERSION_MINOR;74if (Patch)75*Patch = LLVM_VERSION_PATCH;76}7778/*===-- Error handling ----------------------------------------------------===*/7980char *LLVMCreateMessage(const char *Message) {81return strdup(Message);82}8384void LLVMDisposeMessage(char *Message) {85free(Message);86}878889/*===-- Operations on contexts --------------------------------------------===*/9091static LLVMContext &getGlobalContext() {92static LLVMContext GlobalContext;93return GlobalContext;94}9596LLVMContextRef LLVMContextCreate() {97return wrap(new LLVMContext());98}99100LLVMContextRef LLVMGetGlobalContext() { return wrap(&getGlobalContext()); }101102void LLVMContextSetDiagnosticHandler(LLVMContextRef C,103LLVMDiagnosticHandler Handler,104void *DiagnosticContext) {105unwrap(C)->setDiagnosticHandlerCallBack(106LLVM_EXTENSION reinterpret_cast<DiagnosticHandler::DiagnosticHandlerTy>(107Handler),108DiagnosticContext);109}110111LLVMDiagnosticHandler LLVMContextGetDiagnosticHandler(LLVMContextRef C) {112return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>(113unwrap(C)->getDiagnosticHandlerCallBack());114}115116void *LLVMContextGetDiagnosticContext(LLVMContextRef C) {117return unwrap(C)->getDiagnosticContext();118}119120void LLVMContextSetYieldCallback(LLVMContextRef C, LLVMYieldCallback Callback,121void *OpaqueHandle) {122auto YieldCallback =123LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);124unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);125}126127LLVMBool LLVMContextShouldDiscardValueNames(LLVMContextRef C) {128return unwrap(C)->shouldDiscardValueNames();129}130131void LLVMContextSetDiscardValueNames(LLVMContextRef C, LLVMBool Discard) {132unwrap(C)->setDiscardValueNames(Discard);133}134135void LLVMContextDispose(LLVMContextRef C) {136delete unwrap(C);137}138139unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char *Name,140unsigned SLen) {141return unwrap(C)->getMDKindID(StringRef(Name, SLen));142}143144unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) {145return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);146}147148unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) {149return Attribute::getAttrKindFromName(StringRef(Name, SLen));150}151152unsigned LLVMGetLastEnumAttributeKind(void) {153return Attribute::AttrKind::EndAttrKinds;154}155156LLVMAttributeRef LLVMCreateEnumAttribute(LLVMContextRef C, unsigned KindID,157uint64_t Val) {158auto &Ctx = *unwrap(C);159auto AttrKind = (Attribute::AttrKind)KindID;160return wrap(Attribute::get(Ctx, AttrKind, Val));161}162163unsigned LLVMGetEnumAttributeKind(LLVMAttributeRef A) {164return unwrap(A).getKindAsEnum();165}166167uint64_t LLVMGetEnumAttributeValue(LLVMAttributeRef A) {168auto Attr = unwrap(A);169if (Attr.isEnumAttribute())170return 0;171return Attr.getValueAsInt();172}173174LLVMAttributeRef LLVMCreateTypeAttribute(LLVMContextRef C, unsigned KindID,175LLVMTypeRef type_ref) {176auto &Ctx = *unwrap(C);177auto AttrKind = (Attribute::AttrKind)KindID;178return wrap(Attribute::get(Ctx, AttrKind, unwrap(type_ref)));179}180181LLVMTypeRef LLVMGetTypeAttributeValue(LLVMAttributeRef A) {182auto Attr = unwrap(A);183return wrap(Attr.getValueAsType());184}185186LLVMAttributeRef LLVMCreateConstantRangeAttribute(LLVMContextRef C,187unsigned KindID,188unsigned NumBits,189const uint64_t LowerWords[],190const uint64_t UpperWords[]) {191auto &Ctx = *unwrap(C);192auto AttrKind = (Attribute::AttrKind)KindID;193unsigned NumWords = divideCeil(NumBits, 64);194return wrap(Attribute::get(195Ctx, AttrKind,196ConstantRange(APInt(NumBits, ArrayRef(LowerWords, NumWords)),197APInt(NumBits, ArrayRef(UpperWords, NumWords)))));198}199200LLVMAttributeRef LLVMCreateStringAttribute(LLVMContextRef C,201const char *K, unsigned KLength,202const char *V, unsigned VLength) {203return wrap(Attribute::get(*unwrap(C), StringRef(K, KLength),204StringRef(V, VLength)));205}206207const char *LLVMGetStringAttributeKind(LLVMAttributeRef A,208unsigned *Length) {209auto S = unwrap(A).getKindAsString();210*Length = S.size();211return S.data();212}213214const char *LLVMGetStringAttributeValue(LLVMAttributeRef A,215unsigned *Length) {216auto S = unwrap(A).getValueAsString();217*Length = S.size();218return S.data();219}220221LLVMBool LLVMIsEnumAttribute(LLVMAttributeRef A) {222auto Attr = unwrap(A);223return Attr.isEnumAttribute() || Attr.isIntAttribute();224}225226LLVMBool LLVMIsStringAttribute(LLVMAttributeRef A) {227return unwrap(A).isStringAttribute();228}229230LLVMBool LLVMIsTypeAttribute(LLVMAttributeRef A) {231return unwrap(A).isTypeAttribute();232}233234char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) {235std::string MsgStorage;236raw_string_ostream Stream(MsgStorage);237DiagnosticPrinterRawOStream DP(Stream);238239unwrap(DI)->print(DP);240Stream.flush();241242return LLVMCreateMessage(MsgStorage.c_str());243}244245LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI) {246LLVMDiagnosticSeverity severity;247248switch(unwrap(DI)->getSeverity()) {249default:250severity = LLVMDSError;251break;252case DS_Warning:253severity = LLVMDSWarning;254break;255case DS_Remark:256severity = LLVMDSRemark;257break;258case DS_Note:259severity = LLVMDSNote;260break;261}262263return severity;264}265266/*===-- Operations on modules ---------------------------------------------===*/267268LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {269return wrap(new Module(ModuleID, getGlobalContext()));270}271272LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,273LLVMContextRef C) {274return wrap(new Module(ModuleID, *unwrap(C)));275}276277void LLVMDisposeModule(LLVMModuleRef M) {278delete unwrap(M);279}280281const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) {282auto &Str = unwrap(M)->getModuleIdentifier();283*Len = Str.length();284return Str.c_str();285}286287void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) {288unwrap(M)->setModuleIdentifier(StringRef(Ident, Len));289}290291const char *LLVMGetSourceFileName(LLVMModuleRef M, size_t *Len) {292auto &Str = unwrap(M)->getSourceFileName();293*Len = Str.length();294return Str.c_str();295}296297void LLVMSetSourceFileName(LLVMModuleRef M, const char *Name, size_t Len) {298unwrap(M)->setSourceFileName(StringRef(Name, Len));299}300301/*--.. Data layout .........................................................--*/302const char *LLVMGetDataLayoutStr(LLVMModuleRef M) {303return unwrap(M)->getDataLayoutStr().c_str();304}305306const char *LLVMGetDataLayout(LLVMModuleRef M) {307return LLVMGetDataLayoutStr(M);308}309310void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {311unwrap(M)->setDataLayout(DataLayoutStr);312}313314/*--.. Target triple .......................................................--*/315const char * LLVMGetTarget(LLVMModuleRef M) {316return unwrap(M)->getTargetTriple().c_str();317}318319void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {320unwrap(M)->setTargetTriple(Triple);321}322323/*--.. Module flags ........................................................--*/324struct LLVMOpaqueModuleFlagEntry {325LLVMModuleFlagBehavior Behavior;326const char *Key;327size_t KeyLen;328LLVMMetadataRef Metadata;329};330331static Module::ModFlagBehavior332map_to_llvmModFlagBehavior(LLVMModuleFlagBehavior Behavior) {333switch (Behavior) {334case LLVMModuleFlagBehaviorError:335return Module::ModFlagBehavior::Error;336case LLVMModuleFlagBehaviorWarning:337return Module::ModFlagBehavior::Warning;338case LLVMModuleFlagBehaviorRequire:339return Module::ModFlagBehavior::Require;340case LLVMModuleFlagBehaviorOverride:341return Module::ModFlagBehavior::Override;342case LLVMModuleFlagBehaviorAppend:343return Module::ModFlagBehavior::Append;344case LLVMModuleFlagBehaviorAppendUnique:345return Module::ModFlagBehavior::AppendUnique;346}347llvm_unreachable("Unknown LLVMModuleFlagBehavior");348}349350static LLVMModuleFlagBehavior351map_from_llvmModFlagBehavior(Module::ModFlagBehavior Behavior) {352switch (Behavior) {353case Module::ModFlagBehavior::Error:354return LLVMModuleFlagBehaviorError;355case Module::ModFlagBehavior::Warning:356return LLVMModuleFlagBehaviorWarning;357case Module::ModFlagBehavior::Require:358return LLVMModuleFlagBehaviorRequire;359case Module::ModFlagBehavior::Override:360return LLVMModuleFlagBehaviorOverride;361case Module::ModFlagBehavior::Append:362return LLVMModuleFlagBehaviorAppend;363case Module::ModFlagBehavior::AppendUnique:364return LLVMModuleFlagBehaviorAppendUnique;365default:366llvm_unreachable("Unhandled Flag Behavior");367}368}369370LLVMModuleFlagEntry *LLVMCopyModuleFlagsMetadata(LLVMModuleRef M, size_t *Len) {371SmallVector<Module::ModuleFlagEntry, 8> MFEs;372unwrap(M)->getModuleFlagsMetadata(MFEs);373374LLVMOpaqueModuleFlagEntry *Result = static_cast<LLVMOpaqueModuleFlagEntry *>(375safe_malloc(MFEs.size() * sizeof(LLVMOpaqueModuleFlagEntry)));376for (unsigned i = 0; i < MFEs.size(); ++i) {377const auto &ModuleFlag = MFEs[i];378Result[i].Behavior = map_from_llvmModFlagBehavior(ModuleFlag.Behavior);379Result[i].Key = ModuleFlag.Key->getString().data();380Result[i].KeyLen = ModuleFlag.Key->getString().size();381Result[i].Metadata = wrap(ModuleFlag.Val);382}383*Len = MFEs.size();384return Result;385}386387void LLVMDisposeModuleFlagsMetadata(LLVMModuleFlagEntry *Entries) {388free(Entries);389}390391LLVMModuleFlagBehavior392LLVMModuleFlagEntriesGetFlagBehavior(LLVMModuleFlagEntry *Entries,393unsigned Index) {394LLVMOpaqueModuleFlagEntry MFE =395static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);396return MFE.Behavior;397}398399const char *LLVMModuleFlagEntriesGetKey(LLVMModuleFlagEntry *Entries,400unsigned Index, size_t *Len) {401LLVMOpaqueModuleFlagEntry MFE =402static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);403*Len = MFE.KeyLen;404return MFE.Key;405}406407LLVMMetadataRef LLVMModuleFlagEntriesGetMetadata(LLVMModuleFlagEntry *Entries,408unsigned Index) {409LLVMOpaqueModuleFlagEntry MFE =410static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);411return MFE.Metadata;412}413414LLVMMetadataRef LLVMGetModuleFlag(LLVMModuleRef M,415const char *Key, size_t KeyLen) {416return wrap(unwrap(M)->getModuleFlag({Key, KeyLen}));417}418419void LLVMAddModuleFlag(LLVMModuleRef M, LLVMModuleFlagBehavior Behavior,420const char *Key, size_t KeyLen,421LLVMMetadataRef Val) {422unwrap(M)->addModuleFlag(map_to_llvmModFlagBehavior(Behavior),423{Key, KeyLen}, unwrap(Val));424}425426LLVMBool LLVMIsNewDbgInfoFormat(LLVMModuleRef M) {427return unwrap(M)->IsNewDbgInfoFormat;428}429430void LLVMSetIsNewDbgInfoFormat(LLVMModuleRef M, LLVMBool UseNewFormat) {431unwrap(M)->setIsNewDbgInfoFormat(UseNewFormat);432}433434/*--.. Printing modules ....................................................--*/435436void LLVMDumpModule(LLVMModuleRef M) {437unwrap(M)->print(errs(), nullptr,438/*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);439}440441LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,442char **ErrorMessage) {443std::error_code EC;444raw_fd_ostream dest(Filename, EC, sys::fs::OF_TextWithCRLF);445if (EC) {446*ErrorMessage = strdup(EC.message().c_str());447return true;448}449450unwrap(M)->print(dest, nullptr);451452dest.close();453454if (dest.has_error()) {455std::string E = "Error printing to file: " + dest.error().message();456*ErrorMessage = strdup(E.c_str());457return true;458}459460return false;461}462463char *LLVMPrintModuleToString(LLVMModuleRef M) {464std::string buf;465raw_string_ostream os(buf);466467unwrap(M)->print(os, nullptr);468os.flush();469470return strdup(buf.c_str());471}472473/*--.. Operations on inline assembler ......................................--*/474void LLVMSetModuleInlineAsm2(LLVMModuleRef M, const char *Asm, size_t Len) {475unwrap(M)->setModuleInlineAsm(StringRef(Asm, Len));476}477478void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {479unwrap(M)->setModuleInlineAsm(StringRef(Asm));480}481482void LLVMAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm, size_t Len) {483unwrap(M)->appendModuleInlineAsm(StringRef(Asm, Len));484}485486const char *LLVMGetModuleInlineAsm(LLVMModuleRef M, size_t *Len) {487auto &Str = unwrap(M)->getModuleInlineAsm();488*Len = Str.length();489return Str.c_str();490}491492LLVMValueRef LLVMGetInlineAsm(LLVMTypeRef Ty, const char *AsmString,493size_t AsmStringSize, const char *Constraints,494size_t ConstraintsSize, LLVMBool HasSideEffects,495LLVMBool IsAlignStack,496LLVMInlineAsmDialect Dialect, LLVMBool CanThrow) {497InlineAsm::AsmDialect AD;498switch (Dialect) {499case LLVMInlineAsmDialectATT:500AD = InlineAsm::AD_ATT;501break;502case LLVMInlineAsmDialectIntel:503AD = InlineAsm::AD_Intel;504break;505}506return wrap(InlineAsm::get(unwrap<FunctionType>(Ty),507StringRef(AsmString, AsmStringSize),508StringRef(Constraints, ConstraintsSize),509HasSideEffects, IsAlignStack, AD, CanThrow));510}511512const char *LLVMGetInlineAsmAsmString(LLVMValueRef InlineAsmVal, size_t *Len) {513514Value *Val = unwrap<Value>(InlineAsmVal);515const std::string &AsmString = cast<InlineAsm>(Val)->getAsmString();516517*Len = AsmString.length();518return AsmString.c_str();519}520521const char *LLVMGetInlineAsmConstraintString(LLVMValueRef InlineAsmVal,522size_t *Len) {523Value *Val = unwrap<Value>(InlineAsmVal);524const std::string &ConstraintString =525cast<InlineAsm>(Val)->getConstraintString();526527*Len = ConstraintString.length();528return ConstraintString.c_str();529}530531LLVMInlineAsmDialect LLVMGetInlineAsmDialect(LLVMValueRef InlineAsmVal) {532533Value *Val = unwrap<Value>(InlineAsmVal);534InlineAsm::AsmDialect Dialect = cast<InlineAsm>(Val)->getDialect();535536switch (Dialect) {537case InlineAsm::AD_ATT:538return LLVMInlineAsmDialectATT;539case InlineAsm::AD_Intel:540return LLVMInlineAsmDialectIntel;541}542543llvm_unreachable("Unrecognized inline assembly dialect");544return LLVMInlineAsmDialectATT;545}546547LLVMTypeRef LLVMGetInlineAsmFunctionType(LLVMValueRef InlineAsmVal) {548Value *Val = unwrap<Value>(InlineAsmVal);549return (LLVMTypeRef)cast<InlineAsm>(Val)->getFunctionType();550}551552LLVMBool LLVMGetInlineAsmHasSideEffects(LLVMValueRef InlineAsmVal) {553Value *Val = unwrap<Value>(InlineAsmVal);554return cast<InlineAsm>(Val)->hasSideEffects();555}556557LLVMBool LLVMGetInlineAsmNeedsAlignedStack(LLVMValueRef InlineAsmVal) {558Value *Val = unwrap<Value>(InlineAsmVal);559return cast<InlineAsm>(Val)->isAlignStack();560}561562LLVMBool LLVMGetInlineAsmCanUnwind(LLVMValueRef InlineAsmVal) {563Value *Val = unwrap<Value>(InlineAsmVal);564return cast<InlineAsm>(Val)->canThrow();565}566567/*--.. Operations on module contexts ......................................--*/568LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {569return wrap(&unwrap(M)->getContext());570}571572573/*===-- Operations on types -----------------------------------------------===*/574575/*--.. Operations on all types (mostly) ....................................--*/576577LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {578switch (unwrap(Ty)->getTypeID()) {579case Type::VoidTyID:580return LLVMVoidTypeKind;581case Type::HalfTyID:582return LLVMHalfTypeKind;583case Type::BFloatTyID:584return LLVMBFloatTypeKind;585case Type::FloatTyID:586return LLVMFloatTypeKind;587case Type::DoubleTyID:588return LLVMDoubleTypeKind;589case Type::X86_FP80TyID:590return LLVMX86_FP80TypeKind;591case Type::FP128TyID:592return LLVMFP128TypeKind;593case Type::PPC_FP128TyID:594return LLVMPPC_FP128TypeKind;595case Type::LabelTyID:596return LLVMLabelTypeKind;597case Type::MetadataTyID:598return LLVMMetadataTypeKind;599case Type::IntegerTyID:600return LLVMIntegerTypeKind;601case Type::FunctionTyID:602return LLVMFunctionTypeKind;603case Type::StructTyID:604return LLVMStructTypeKind;605case Type::ArrayTyID:606return LLVMArrayTypeKind;607case Type::PointerTyID:608return LLVMPointerTypeKind;609case Type::FixedVectorTyID:610return LLVMVectorTypeKind;611case Type::X86_MMXTyID:612return LLVMX86_MMXTypeKind;613case Type::X86_AMXTyID:614return LLVMX86_AMXTypeKind;615case Type::TokenTyID:616return LLVMTokenTypeKind;617case Type::ScalableVectorTyID:618return LLVMScalableVectorTypeKind;619case Type::TargetExtTyID:620return LLVMTargetExtTypeKind;621case Type::TypedPointerTyID:622llvm_unreachable("Typed pointers are unsupported via the C API");623}624llvm_unreachable("Unhandled TypeID.");625}626627LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)628{629return unwrap(Ty)->isSized();630}631632LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {633return wrap(&unwrap(Ty)->getContext());634}635636void LLVMDumpType(LLVMTypeRef Ty) {637return unwrap(Ty)->print(errs(), /*IsForDebug=*/true);638}639640char *LLVMPrintTypeToString(LLVMTypeRef Ty) {641std::string buf;642raw_string_ostream os(buf);643644if (unwrap(Ty))645unwrap(Ty)->print(os);646else647os << "Printing <null> Type";648649os.flush();650651return strdup(buf.c_str());652}653654/*--.. Operations on integer types .........................................--*/655656LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {657return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));658}659LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {660return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));661}662LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {663return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));664}665LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {666return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));667}668LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {669return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));670}671LLVMTypeRef LLVMInt128TypeInContext(LLVMContextRef C) {672return (LLVMTypeRef) Type::getInt128Ty(*unwrap(C));673}674LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {675return wrap(IntegerType::get(*unwrap(C), NumBits));676}677678LLVMTypeRef LLVMInt1Type(void) {679return LLVMInt1TypeInContext(LLVMGetGlobalContext());680}681LLVMTypeRef LLVMInt8Type(void) {682return LLVMInt8TypeInContext(LLVMGetGlobalContext());683}684LLVMTypeRef LLVMInt16Type(void) {685return LLVMInt16TypeInContext(LLVMGetGlobalContext());686}687LLVMTypeRef LLVMInt32Type(void) {688return LLVMInt32TypeInContext(LLVMGetGlobalContext());689}690LLVMTypeRef LLVMInt64Type(void) {691return LLVMInt64TypeInContext(LLVMGetGlobalContext());692}693LLVMTypeRef LLVMInt128Type(void) {694return LLVMInt128TypeInContext(LLVMGetGlobalContext());695}696LLVMTypeRef LLVMIntType(unsigned NumBits) {697return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);698}699700unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {701return unwrap<IntegerType>(IntegerTy)->getBitWidth();702}703704/*--.. Operations on real types ............................................--*/705706LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {707return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));708}709LLVMTypeRef LLVMBFloatTypeInContext(LLVMContextRef C) {710return (LLVMTypeRef) Type::getBFloatTy(*unwrap(C));711}712LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {713return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));714}715LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {716return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));717}718LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {719return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));720}721LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {722return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));723}724LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {725return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));726}727LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {728return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));729}730LLVMTypeRef LLVMX86AMXTypeInContext(LLVMContextRef C) {731return (LLVMTypeRef) Type::getX86_AMXTy(*unwrap(C));732}733734LLVMTypeRef LLVMHalfType(void) {735return LLVMHalfTypeInContext(LLVMGetGlobalContext());736}737LLVMTypeRef LLVMBFloatType(void) {738return LLVMBFloatTypeInContext(LLVMGetGlobalContext());739}740LLVMTypeRef LLVMFloatType(void) {741return LLVMFloatTypeInContext(LLVMGetGlobalContext());742}743LLVMTypeRef LLVMDoubleType(void) {744return LLVMDoubleTypeInContext(LLVMGetGlobalContext());745}746LLVMTypeRef LLVMX86FP80Type(void) {747return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());748}749LLVMTypeRef LLVMFP128Type(void) {750return LLVMFP128TypeInContext(LLVMGetGlobalContext());751}752LLVMTypeRef LLVMPPCFP128Type(void) {753return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());754}755LLVMTypeRef LLVMX86MMXType(void) {756return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());757}758LLVMTypeRef LLVMX86AMXType(void) {759return LLVMX86AMXTypeInContext(LLVMGetGlobalContext());760}761762/*--.. Operations on function types ........................................--*/763764LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,765LLVMTypeRef *ParamTypes, unsigned ParamCount,766LLVMBool IsVarArg) {767ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);768return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));769}770771LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {772return unwrap<FunctionType>(FunctionTy)->isVarArg();773}774775LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {776return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());777}778779unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {780return unwrap<FunctionType>(FunctionTy)->getNumParams();781}782783void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {784FunctionType *Ty = unwrap<FunctionType>(FunctionTy);785for (Type *T : Ty->params())786*Dest++ = wrap(T);787}788789/*--.. Operations on struct types ..........................................--*/790791LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,792unsigned ElementCount, LLVMBool Packed) {793ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);794return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));795}796797LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,798unsigned ElementCount, LLVMBool Packed) {799return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,800ElementCount, Packed);801}802803LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)804{805return wrap(StructType::create(*unwrap(C), Name));806}807808const char *LLVMGetStructName(LLVMTypeRef Ty)809{810StructType *Type = unwrap<StructType>(Ty);811if (!Type->hasName())812return nullptr;813return Type->getName().data();814}815816void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,817unsigned ElementCount, LLVMBool Packed) {818ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);819unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);820}821822unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {823return unwrap<StructType>(StructTy)->getNumElements();824}825826void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {827StructType *Ty = unwrap<StructType>(StructTy);828for (Type *T : Ty->elements())829*Dest++ = wrap(T);830}831832LLVMTypeRef LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy, unsigned i) {833StructType *Ty = unwrap<StructType>(StructTy);834return wrap(Ty->getTypeAtIndex(i));835}836837LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {838return unwrap<StructType>(StructTy)->isPacked();839}840841LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {842return unwrap<StructType>(StructTy)->isOpaque();843}844845LLVMBool LLVMIsLiteralStruct(LLVMTypeRef StructTy) {846return unwrap<StructType>(StructTy)->isLiteral();847}848849LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {850return wrap(StructType::getTypeByName(unwrap(M)->getContext(), Name));851}852853LLVMTypeRef LLVMGetTypeByName2(LLVMContextRef C, const char *Name) {854return wrap(StructType::getTypeByName(*unwrap(C), Name));855}856857/*--.. Operations on array, pointer, and vector types (sequence types) .....--*/858859void LLVMGetSubtypes(LLVMTypeRef Tp, LLVMTypeRef *Arr) {860int i = 0;861for (auto *T : unwrap(Tp)->subtypes()) {862Arr[i] = wrap(T);863i++;864}865}866867LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {868return wrap(ArrayType::get(unwrap(ElementType), ElementCount));869}870871LLVMTypeRef LLVMArrayType2(LLVMTypeRef ElementType, uint64_t ElementCount) {872return wrap(ArrayType::get(unwrap(ElementType), ElementCount));873}874875LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {876return wrap(PointerType::get(unwrap(ElementType), AddressSpace));877}878879LLVMBool LLVMPointerTypeIsOpaque(LLVMTypeRef Ty) {880return true;881}882883LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {884return wrap(FixedVectorType::get(unwrap(ElementType), ElementCount));885}886887LLVMTypeRef LLVMScalableVectorType(LLVMTypeRef ElementType,888unsigned ElementCount) {889return wrap(ScalableVectorType::get(unwrap(ElementType), ElementCount));890}891892LLVMTypeRef LLVMGetElementType(LLVMTypeRef WrappedTy) {893auto *Ty = unwrap(WrappedTy);894if (auto *ATy = dyn_cast<ArrayType>(Ty))895return wrap(ATy->getElementType());896return wrap(cast<VectorType>(Ty)->getElementType());897}898899unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp) {900return unwrap(Tp)->getNumContainedTypes();901}902903unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {904return unwrap<ArrayType>(ArrayTy)->getNumElements();905}906907uint64_t LLVMGetArrayLength2(LLVMTypeRef ArrayTy) {908return unwrap<ArrayType>(ArrayTy)->getNumElements();909}910911unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {912return unwrap<PointerType>(PointerTy)->getAddressSpace();913}914915unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {916return unwrap<VectorType>(VectorTy)->getElementCount().getKnownMinValue();917}918919LLVMValueRef LLVMGetConstantPtrAuthPointer(LLVMValueRef PtrAuth) {920return wrap(unwrap<ConstantPtrAuth>(PtrAuth)->getPointer());921}922923LLVMValueRef LLVMGetConstantPtrAuthKey(LLVMValueRef PtrAuth) {924return wrap(unwrap<ConstantPtrAuth>(PtrAuth)->getKey());925}926927LLVMValueRef LLVMGetConstantPtrAuthDiscriminator(LLVMValueRef PtrAuth) {928return wrap(unwrap<ConstantPtrAuth>(PtrAuth)->getDiscriminator());929}930931LLVMValueRef LLVMGetConstantPtrAuthAddrDiscriminator(LLVMValueRef PtrAuth) {932return wrap(unwrap<ConstantPtrAuth>(PtrAuth)->getAddrDiscriminator());933}934935/*--.. Operations on other types ...........................................--*/936937LLVMTypeRef LLVMPointerTypeInContext(LLVMContextRef C, unsigned AddressSpace) {938return wrap(PointerType::get(*unwrap(C), AddressSpace));939}940941LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {942return wrap(Type::getVoidTy(*unwrap(C)));943}944LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {945return wrap(Type::getLabelTy(*unwrap(C)));946}947LLVMTypeRef LLVMTokenTypeInContext(LLVMContextRef C) {948return wrap(Type::getTokenTy(*unwrap(C)));949}950LLVMTypeRef LLVMMetadataTypeInContext(LLVMContextRef C) {951return wrap(Type::getMetadataTy(*unwrap(C)));952}953954LLVMTypeRef LLVMVoidType(void) {955return LLVMVoidTypeInContext(LLVMGetGlobalContext());956}957LLVMTypeRef LLVMLabelType(void) {958return LLVMLabelTypeInContext(LLVMGetGlobalContext());959}960961LLVMTypeRef LLVMTargetExtTypeInContext(LLVMContextRef C, const char *Name,962LLVMTypeRef *TypeParams,963unsigned TypeParamCount,964unsigned *IntParams,965unsigned IntParamCount) {966ArrayRef<Type *> TypeParamArray(unwrap(TypeParams), TypeParamCount);967ArrayRef<unsigned> IntParamArray(IntParams, IntParamCount);968return wrap(969TargetExtType::get(*unwrap(C), Name, TypeParamArray, IntParamArray));970}971972const char *LLVMGetTargetExtTypeName(LLVMTypeRef TargetExtTy) {973TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);974return Type->getName().data();975}976977unsigned LLVMGetTargetExtTypeNumTypeParams(LLVMTypeRef TargetExtTy) {978TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);979return Type->getNumTypeParameters();980}981982LLVMTypeRef LLVMGetTargetExtTypeTypeParam(LLVMTypeRef TargetExtTy,983unsigned Idx) {984TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);985return wrap(Type->getTypeParameter(Idx));986}987988unsigned LLVMGetTargetExtTypeNumIntParams(LLVMTypeRef TargetExtTy) {989TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);990return Type->getNumIntParameters();991}992993unsigned LLVMGetTargetExtTypeIntParam(LLVMTypeRef TargetExtTy, unsigned Idx) {994TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);995return Type->getIntParameter(Idx);996}997998/*===-- Operations on values ----------------------------------------------===*/9991000/*--.. Operations on all values ............................................--*/10011002LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {1003return wrap(unwrap(Val)->getType());1004}10051006LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) {1007switch(unwrap(Val)->getValueID()) {1008#define LLVM_C_API 11009#define HANDLE_VALUE(Name) \1010case Value::Name##Val: \1011return LLVM##Name##ValueKind;1012#include "llvm/IR/Value.def"1013default:1014return LLVMInstructionValueKind;1015}1016}10171018const char *LLVMGetValueName2(LLVMValueRef Val, size_t *Length) {1019auto *V = unwrap(Val);1020*Length = V->getName().size();1021return V->getName().data();1022}10231024void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen) {1025unwrap(Val)->setName(StringRef(Name, NameLen));1026}10271028const char *LLVMGetValueName(LLVMValueRef Val) {1029return unwrap(Val)->getName().data();1030}10311032void LLVMSetValueName(LLVMValueRef Val, const char *Name) {1033unwrap(Val)->setName(Name);1034}10351036void LLVMDumpValue(LLVMValueRef Val) {1037unwrap(Val)->print(errs(), /*IsForDebug=*/true);1038}10391040char* LLVMPrintValueToString(LLVMValueRef Val) {1041std::string buf;1042raw_string_ostream os(buf);10431044if (unwrap(Val))1045unwrap(Val)->print(os);1046else1047os << "Printing <null> Value";10481049os.flush();10501051return strdup(buf.c_str());1052}10531054char *LLVMPrintDbgRecordToString(LLVMDbgRecordRef Record) {1055std::string buf;1056raw_string_ostream os(buf);10571058if (unwrap(Record))1059unwrap(Record)->print(os);1060else1061os << "Printing <null> DbgRecord";10621063os.flush();10641065return strdup(buf.c_str());1066}10671068void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {1069unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));1070}10711072int LLVMHasMetadata(LLVMValueRef Inst) {1073return unwrap<Instruction>(Inst)->hasMetadata();1074}10751076LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {1077auto *I = unwrap<Instruction>(Inst);1078assert(I && "Expected instruction");1079if (auto *MD = I->getMetadata(KindID))1080return wrap(MetadataAsValue::get(I->getContext(), MD));1081return nullptr;1082}10831084// MetadataAsValue uses a canonical format which strips the actual MDNode for1085// MDNode with just a single constant value, storing just a ConstantAsMetadata1086// This undoes this canonicalization, reconstructing the MDNode.1087static MDNode *extractMDNode(MetadataAsValue *MAV) {1088Metadata *MD = MAV->getMetadata();1089assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&1090"Expected a metadata node or a canonicalized constant");10911092if (MDNode *N = dyn_cast<MDNode>(MD))1093return N;10941095return MDNode::get(MAV->getContext(), MD);1096}10971098void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {1099MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;11001101unwrap<Instruction>(Inst)->setMetadata(KindID, N);1102}11031104struct LLVMOpaqueValueMetadataEntry {1105unsigned Kind;1106LLVMMetadataRef Metadata;1107};11081109using MetadataEntries = SmallVectorImpl<std::pair<unsigned, MDNode *>>;1110static LLVMValueMetadataEntry *1111llvm_getMetadata(size_t *NumEntries,1112llvm::function_ref<void(MetadataEntries &)> AccessMD) {1113SmallVector<std::pair<unsigned, MDNode *>, 8> MVEs;1114AccessMD(MVEs);11151116LLVMOpaqueValueMetadataEntry *Result =1117static_cast<LLVMOpaqueValueMetadataEntry *>(1118safe_malloc(MVEs.size() * sizeof(LLVMOpaqueValueMetadataEntry)));1119for (unsigned i = 0; i < MVEs.size(); ++i) {1120const auto &ModuleFlag = MVEs[i];1121Result[i].Kind = ModuleFlag.first;1122Result[i].Metadata = wrap(ModuleFlag.second);1123}1124*NumEntries = MVEs.size();1125return Result;1126}11271128LLVMValueMetadataEntry *1129LLVMInstructionGetAllMetadataOtherThanDebugLoc(LLVMValueRef Value,1130size_t *NumEntries) {1131return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {1132Entries.clear();1133unwrap<Instruction>(Value)->getAllMetadata(Entries);1134});1135}11361137/*--.. Conversion functions ................................................--*/11381139#define LLVM_DEFINE_VALUE_CAST(name) \1140LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \1141return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \1142}11431144LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)11451146LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) {1147if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))1148if (isa<MDNode>(MD->getMetadata()) ||1149isa<ValueAsMetadata>(MD->getMetadata()))1150return Val;1151return nullptr;1152}11531154LLVMValueRef LLVMIsAValueAsMetadata(LLVMValueRef Val) {1155if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))1156if (isa<ValueAsMetadata>(MD->getMetadata()))1157return Val;1158return nullptr;1159}11601161LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) {1162if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))1163if (isa<MDString>(MD->getMetadata()))1164return Val;1165return nullptr;1166}11671168/*--.. Operations on Uses ..................................................--*/1169LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {1170Value *V = unwrap(Val);1171Value::use_iterator I = V->use_begin();1172if (I == V->use_end())1173return nullptr;1174return wrap(&*I);1175}11761177LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {1178Use *Next = unwrap(U)->getNext();1179if (Next)1180return wrap(Next);1181return nullptr;1182}11831184LLVMValueRef LLVMGetUser(LLVMUseRef U) {1185return wrap(unwrap(U)->getUser());1186}11871188LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {1189return wrap(unwrap(U)->get());1190}11911192/*--.. Operations on Users .................................................--*/11931194static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N,1195unsigned Index) {1196Metadata *Op = N->getOperand(Index);1197if (!Op)1198return nullptr;1199if (auto *C = dyn_cast<ConstantAsMetadata>(Op))1200return wrap(C->getValue());1201return wrap(MetadataAsValue::get(Context, Op));1202}12031204LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {1205Value *V = unwrap(Val);1206if (auto *MD = dyn_cast<MetadataAsValue>(V)) {1207if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {1208assert(Index == 0 && "Function-local metadata can only have one operand");1209return wrap(L->getValue());1210}1211return getMDNodeOperandImpl(V->getContext(),1212cast<MDNode>(MD->getMetadata()), Index);1213}12141215return wrap(cast<User>(V)->getOperand(Index));1216}12171218LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) {1219Value *V = unwrap(Val);1220return wrap(&cast<User>(V)->getOperandUse(Index));1221}12221223void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {1224unwrap<User>(Val)->setOperand(Index, unwrap(Op));1225}12261227int LLVMGetNumOperands(LLVMValueRef Val) {1228Value *V = unwrap(Val);1229if (isa<MetadataAsValue>(V))1230return LLVMGetMDNodeNumOperands(Val);12311232return cast<User>(V)->getNumOperands();1233}12341235/*--.. Operations on constants of any type .................................--*/12361237LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {1238return wrap(Constant::getNullValue(unwrap(Ty)));1239}12401241LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {1242return wrap(Constant::getAllOnesValue(unwrap(Ty)));1243}12441245LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {1246return wrap(UndefValue::get(unwrap(Ty)));1247}12481249LLVMValueRef LLVMGetPoison(LLVMTypeRef Ty) {1250return wrap(PoisonValue::get(unwrap(Ty)));1251}12521253LLVMBool LLVMIsConstant(LLVMValueRef Ty) {1254return isa<Constant>(unwrap(Ty));1255}12561257LLVMBool LLVMIsNull(LLVMValueRef Val) {1258if (Constant *C = dyn_cast<Constant>(unwrap(Val)))1259return C->isNullValue();1260return false;1261}12621263LLVMBool LLVMIsUndef(LLVMValueRef Val) {1264return isa<UndefValue>(unwrap(Val));1265}12661267LLVMBool LLVMIsPoison(LLVMValueRef Val) {1268return isa<PoisonValue>(unwrap(Val));1269}12701271LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {1272return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));1273}12741275/*--.. Operations on metadata nodes ........................................--*/12761277LLVMMetadataRef LLVMMDStringInContext2(LLVMContextRef C, const char *Str,1278size_t SLen) {1279return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));1280}12811282LLVMMetadataRef LLVMMDNodeInContext2(LLVMContextRef C, LLVMMetadataRef *MDs,1283size_t Count) {1284return wrap(MDNode::get(*unwrap(C), ArrayRef<Metadata*>(unwrap(MDs), Count)));1285}12861287LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,1288unsigned SLen) {1289LLVMContext &Context = *unwrap(C);1290return wrap(MetadataAsValue::get(1291Context, MDString::get(Context, StringRef(Str, SLen))));1292}12931294LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {1295return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);1296}12971298LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,1299unsigned Count) {1300LLVMContext &Context = *unwrap(C);1301SmallVector<Metadata *, 8> MDs;1302for (auto *OV : ArrayRef(Vals, Count)) {1303Value *V = unwrap(OV);1304Metadata *MD;1305if (!V)1306MD = nullptr;1307else if (auto *C = dyn_cast<Constant>(V))1308MD = ConstantAsMetadata::get(C);1309else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {1310MD = MDV->getMetadata();1311assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "1312"outside of direct argument to call");1313} else {1314// This is function-local metadata. Pretend to make an MDNode.1315assert(Count == 1 &&1316"Expected only one operand to function-local metadata");1317return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V)));1318}13191320MDs.push_back(MD);1321}1322return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs)));1323}13241325LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {1326return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);1327}13281329LLVMValueRef LLVMMetadataAsValue(LLVMContextRef C, LLVMMetadataRef MD) {1330return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));1331}13321333LLVMMetadataRef LLVMValueAsMetadata(LLVMValueRef Val) {1334auto *V = unwrap(Val);1335if (auto *C = dyn_cast<Constant>(V))1336return wrap(ConstantAsMetadata::get(C));1337if (auto *MAV = dyn_cast<MetadataAsValue>(V))1338return wrap(MAV->getMetadata());1339return wrap(ValueAsMetadata::get(V));1340}13411342const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {1343if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))1344if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {1345*Length = S->getString().size();1346return S->getString().data();1347}1348*Length = 0;1349return nullptr;1350}13511352unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) {1353auto *MD = unwrap<MetadataAsValue>(V);1354if (isa<ValueAsMetadata>(MD->getMetadata()))1355return 1;1356return cast<MDNode>(MD->getMetadata())->getNumOperands();1357}13581359LLVMNamedMDNodeRef LLVMGetFirstNamedMetadata(LLVMModuleRef M) {1360Module *Mod = unwrap(M);1361Module::named_metadata_iterator I = Mod->named_metadata_begin();1362if (I == Mod->named_metadata_end())1363return nullptr;1364return wrap(&*I);1365}13661367LLVMNamedMDNodeRef LLVMGetLastNamedMetadata(LLVMModuleRef M) {1368Module *Mod = unwrap(M);1369Module::named_metadata_iterator I = Mod->named_metadata_end();1370if (I == Mod->named_metadata_begin())1371return nullptr;1372return wrap(&*--I);1373}13741375LLVMNamedMDNodeRef LLVMGetNextNamedMetadata(LLVMNamedMDNodeRef NMD) {1376NamedMDNode *NamedNode = unwrap(NMD);1377Module::named_metadata_iterator I(NamedNode);1378if (++I == NamedNode->getParent()->named_metadata_end())1379return nullptr;1380return wrap(&*I);1381}13821383LLVMNamedMDNodeRef LLVMGetPreviousNamedMetadata(LLVMNamedMDNodeRef NMD) {1384NamedMDNode *NamedNode = unwrap(NMD);1385Module::named_metadata_iterator I(NamedNode);1386if (I == NamedNode->getParent()->named_metadata_begin())1387return nullptr;1388return wrap(&*--I);1389}13901391LLVMNamedMDNodeRef LLVMGetNamedMetadata(LLVMModuleRef M,1392const char *Name, size_t NameLen) {1393return wrap(unwrap(M)->getNamedMetadata(StringRef(Name, NameLen)));1394}13951396LLVMNamedMDNodeRef LLVMGetOrInsertNamedMetadata(LLVMModuleRef M,1397const char *Name, size_t NameLen) {1398return wrap(unwrap(M)->getOrInsertNamedMetadata({Name, NameLen}));1399}14001401const char *LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NMD, size_t *NameLen) {1402NamedMDNode *NamedNode = unwrap(NMD);1403*NameLen = NamedNode->getName().size();1404return NamedNode->getName().data();1405}14061407void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) {1408auto *MD = unwrap<MetadataAsValue>(V);1409if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {1410*Dest = wrap(MDV->getValue());1411return;1412}1413const auto *N = cast<MDNode>(MD->getMetadata());1414const unsigned numOperands = N->getNumOperands();1415LLVMContext &Context = unwrap(V)->getContext();1416for (unsigned i = 0; i < numOperands; i++)1417Dest[i] = getMDNodeOperandImpl(Context, N, i);1418}14191420void LLVMReplaceMDNodeOperandWith(LLVMValueRef V, unsigned Index,1421LLVMMetadataRef Replacement) {1422auto *MD = cast<MetadataAsValue>(unwrap(V));1423auto *N = cast<MDNode>(MD->getMetadata());1424N->replaceOperandWith(Index, unwrap<Metadata>(Replacement));1425}14261427unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char *Name) {1428if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {1429return N->getNumOperands();1430}1431return 0;1432}14331434void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char *Name,1435LLVMValueRef *Dest) {1436NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);1437if (!N)1438return;1439LLVMContext &Context = unwrap(M)->getContext();1440for (unsigned i=0;i<N->getNumOperands();i++)1441Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));1442}14431444void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char *Name,1445LLVMValueRef Val) {1446NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);1447if (!N)1448return;1449if (!Val)1450return;1451N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));1452}14531454const char *LLVMGetDebugLocDirectory(LLVMValueRef Val, unsigned *Length) {1455if (!Length) return nullptr;1456StringRef S;1457if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {1458if (const auto &DL = I->getDebugLoc()) {1459S = DL->getDirectory();1460}1461} else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {1462SmallVector<DIGlobalVariableExpression *, 1> GVEs;1463GV->getDebugInfo(GVEs);1464if (GVEs.size())1465if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())1466S = DGV->getDirectory();1467} else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {1468if (const DISubprogram *DSP = F->getSubprogram())1469S = DSP->getDirectory();1470} else {1471assert(0 && "Expected Instruction, GlobalVariable or Function");1472return nullptr;1473}1474*Length = S.size();1475return S.data();1476}14771478const char *LLVMGetDebugLocFilename(LLVMValueRef Val, unsigned *Length) {1479if (!Length) return nullptr;1480StringRef S;1481if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {1482if (const auto &DL = I->getDebugLoc()) {1483S = DL->getFilename();1484}1485} else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {1486SmallVector<DIGlobalVariableExpression *, 1> GVEs;1487GV->getDebugInfo(GVEs);1488if (GVEs.size())1489if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())1490S = DGV->getFilename();1491} else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {1492if (const DISubprogram *DSP = F->getSubprogram())1493S = DSP->getFilename();1494} else {1495assert(0 && "Expected Instruction, GlobalVariable or Function");1496return nullptr;1497}1498*Length = S.size();1499return S.data();1500}15011502unsigned LLVMGetDebugLocLine(LLVMValueRef Val) {1503unsigned L = 0;1504if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {1505if (const auto &DL = I->getDebugLoc()) {1506L = DL->getLine();1507}1508} else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {1509SmallVector<DIGlobalVariableExpression *, 1> GVEs;1510GV->getDebugInfo(GVEs);1511if (GVEs.size())1512if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())1513L = DGV->getLine();1514} else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {1515if (const DISubprogram *DSP = F->getSubprogram())1516L = DSP->getLine();1517} else {1518assert(0 && "Expected Instruction, GlobalVariable or Function");1519return -1;1520}1521return L;1522}15231524unsigned LLVMGetDebugLocColumn(LLVMValueRef Val) {1525unsigned C = 0;1526if (const auto *I = dyn_cast<Instruction>(unwrap(Val)))1527if (const auto &DL = I->getDebugLoc())1528C = DL->getColumn();1529return C;1530}15311532/*--.. Operations on scalar constants ......................................--*/15331534LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,1535LLVMBool SignExtend) {1536return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));1537}15381539LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,1540unsigned NumWords,1541const uint64_t Words[]) {1542IntegerType *Ty = unwrap<IntegerType>(IntTy);1543return wrap(ConstantInt::get(1544Ty->getContext(), APInt(Ty->getBitWidth(), ArrayRef(Words, NumWords))));1545}15461547LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],1548uint8_t Radix) {1549return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),1550Radix));1551}15521553LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],1554unsigned SLen, uint8_t Radix) {1555return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),1556Radix));1557}15581559LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {1560return wrap(ConstantFP::get(unwrap(RealTy), N));1561}15621563LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {1564return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));1565}15661567LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],1568unsigned SLen) {1569return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));1570}15711572unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {1573return unwrap<ConstantInt>(ConstantVal)->getZExtValue();1574}15751576long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {1577return unwrap<ConstantInt>(ConstantVal)->getSExtValue();1578}15791580double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {1581ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;1582Type *Ty = cFP->getType();15831584if (Ty->isHalfTy() || Ty->isBFloatTy() || Ty->isFloatTy() ||1585Ty->isDoubleTy()) {1586*LosesInfo = false;1587return cFP->getValueAPF().convertToDouble();1588}15891590bool APFLosesInfo;1591APFloat APF = cFP->getValueAPF();1592APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo);1593*LosesInfo = APFLosesInfo;1594return APF.convertToDouble();1595}15961597/*--.. Operations on composite constants ...................................--*/15981599LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,1600unsigned Length,1601LLVMBool DontNullTerminate) {1602/* Inverted the sense of AddNull because ', 0)' is a1603better mnemonic for null termination than ', 1)'. */1604return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),1605DontNullTerminate == 0));1606}16071608LLVMValueRef LLVMConstStringInContext2(LLVMContextRef C, const char *Str,1609size_t Length,1610LLVMBool DontNullTerminate) {1611/* Inverted the sense of AddNull because ', 0)' is a1612better mnemonic for null termination than ', 1)'. */1613return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),1614DontNullTerminate == 0));1615}16161617LLVMValueRef LLVMConstString(const char *Str, unsigned Length,1618LLVMBool DontNullTerminate) {1619return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,1620DontNullTerminate);1621}16221623LLVMValueRef LLVMGetAggregateElement(LLVMValueRef C, unsigned Idx) {1624return wrap(unwrap<Constant>(C)->getAggregateElement(Idx));1625}16261627LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef C, unsigned idx) {1628return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));1629}16301631LLVMBool LLVMIsConstantString(LLVMValueRef C) {1632return unwrap<ConstantDataSequential>(C)->isString();1633}16341635const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {1636StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();1637*Length = Str.size();1638return Str.data();1639}16401641LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,1642LLVMValueRef *ConstantVals, unsigned Length) {1643ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);1644return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));1645}16461647LLVMValueRef LLVMConstArray2(LLVMTypeRef ElementTy, LLVMValueRef *ConstantVals,1648uint64_t Length) {1649ArrayRef<Constant *> V(unwrap<Constant>(ConstantVals, Length), Length);1650return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));1651}16521653LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,1654LLVMValueRef *ConstantVals,1655unsigned Count, LLVMBool Packed) {1656Constant **Elements = unwrap<Constant>(ConstantVals, Count);1657return wrap(ConstantStruct::getAnon(*unwrap(C), ArrayRef(Elements, Count),1658Packed != 0));1659}16601661LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,1662LLVMBool Packed) {1663return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,1664Packed);1665}16661667LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,1668LLVMValueRef *ConstantVals,1669unsigned Count) {1670Constant **Elements = unwrap<Constant>(ConstantVals, Count);1671StructType *Ty = unwrap<StructType>(StructTy);16721673return wrap(ConstantStruct::get(Ty, ArrayRef(Elements, Count)));1674}16751676LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {1677return wrap(ConstantVector::get(1678ArrayRef(unwrap<Constant>(ScalarConstantVals, Size), Size)));1679}16801681LLVMValueRef LLVMConstantPtrAuth(LLVMValueRef Ptr, LLVMValueRef Key,1682LLVMValueRef Disc, LLVMValueRef AddrDisc) {1683return wrap(ConstantPtrAuth::get(1684unwrap<Constant>(Ptr), unwrap<ConstantInt>(Key),1685unwrap<ConstantInt>(Disc), unwrap<Constant>(AddrDisc)));1686}16871688/*-- Opcode mapping */16891690static LLVMOpcode map_to_llvmopcode(int opcode)1691{1692switch (opcode) {1693default: llvm_unreachable("Unhandled Opcode.");1694#define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;1695#include "llvm/IR/Instruction.def"1696#undef HANDLE_INST1697}1698}16991700static int map_from_llvmopcode(LLVMOpcode code)1701{1702switch (code) {1703#define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;1704#include "llvm/IR/Instruction.def"1705#undef HANDLE_INST1706}1707llvm_unreachable("Unhandled Opcode.");1708}17091710/*-- GEP wrap flag conversions */17111712static GEPNoWrapFlags mapFromLLVMGEPNoWrapFlags(LLVMGEPNoWrapFlags GEPFlags) {1713GEPNoWrapFlags NewGEPFlags;1714if ((GEPFlags & LLVMGEPFlagInBounds) != 0)1715NewGEPFlags |= GEPNoWrapFlags::inBounds();1716if ((GEPFlags & LLVMGEPFlagNUSW) != 0)1717NewGEPFlags |= GEPNoWrapFlags::noUnsignedSignedWrap();1718if ((GEPFlags & LLVMGEPFlagNUW) != 0)1719NewGEPFlags |= GEPNoWrapFlags::noUnsignedWrap();17201721return NewGEPFlags;1722}17231724static LLVMGEPNoWrapFlags mapToLLVMGEPNoWrapFlags(GEPNoWrapFlags GEPFlags) {1725LLVMGEPNoWrapFlags NewGEPFlags = 0;1726if (GEPFlags.isInBounds())1727NewGEPFlags |= LLVMGEPFlagInBounds;1728if (GEPFlags.hasNoUnsignedSignedWrap())1729NewGEPFlags |= LLVMGEPFlagNUSW;1730if (GEPFlags.hasNoUnsignedWrap())1731NewGEPFlags |= LLVMGEPFlagNUW;17321733return NewGEPFlags;1734}17351736/*--.. Constant expressions ................................................--*/17371738LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {1739return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());1740}17411742LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {1743return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));1744}17451746LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {1747return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));1748}17491750LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {1751return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));1752}17531754LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {1755return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));1756}17571758LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {1759return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));1760}176117621763LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {1764return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));1765}17661767LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {1768return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),1769unwrap<Constant>(RHSConstant)));1770}17711772LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,1773LLVMValueRef RHSConstant) {1774return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),1775unwrap<Constant>(RHSConstant)));1776}17771778LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,1779LLVMValueRef RHSConstant) {1780return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),1781unwrap<Constant>(RHSConstant)));1782}17831784LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {1785return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),1786unwrap<Constant>(RHSConstant)));1787}17881789LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,1790LLVMValueRef RHSConstant) {1791return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),1792unwrap<Constant>(RHSConstant)));1793}17941795LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,1796LLVMValueRef RHSConstant) {1797return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),1798unwrap<Constant>(RHSConstant)));1799}18001801LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {1802return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),1803unwrap<Constant>(RHSConstant)));1804}18051806LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,1807LLVMValueRef RHSConstant) {1808return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),1809unwrap<Constant>(RHSConstant)));1810}18111812LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,1813LLVMValueRef RHSConstant) {1814return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),1815unwrap<Constant>(RHSConstant)));1816}18171818LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {1819return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),1820unwrap<Constant>(RHSConstant)));1821}18221823LLVMValueRef LLVMConstGEP2(LLVMTypeRef Ty, LLVMValueRef ConstantVal,1824LLVMValueRef *ConstantIndices, unsigned NumIndices) {1825ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),1826NumIndices);1827Constant *Val = unwrap<Constant>(ConstantVal);1828return wrap(ConstantExpr::getGetElementPtr(unwrap(Ty), Val, IdxList));1829}18301831LLVMValueRef LLVMConstInBoundsGEP2(LLVMTypeRef Ty, LLVMValueRef ConstantVal,1832LLVMValueRef *ConstantIndices,1833unsigned NumIndices) {1834ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),1835NumIndices);1836Constant *Val = unwrap<Constant>(ConstantVal);1837return wrap(ConstantExpr::getInBoundsGetElementPtr(unwrap(Ty), Val, IdxList));1838}18391840LLVMValueRef LLVMConstGEPWithNoWrapFlags(LLVMTypeRef Ty,1841LLVMValueRef ConstantVal,1842LLVMValueRef *ConstantIndices,1843unsigned NumIndices,1844LLVMGEPNoWrapFlags NoWrapFlags) {1845ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),1846NumIndices);1847Constant *Val = unwrap<Constant>(ConstantVal);1848return wrap(ConstantExpr::getGetElementPtr(1849unwrap(Ty), Val, IdxList, mapFromLLVMGEPNoWrapFlags(NoWrapFlags)));1850}18511852LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {1853return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),1854unwrap(ToType)));1855}18561857LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {1858return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),1859unwrap(ToType)));1860}18611862LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {1863return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),1864unwrap(ToType)));1865}18661867LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {1868return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),1869unwrap(ToType)));1870}18711872LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,1873LLVMTypeRef ToType) {1874return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),1875unwrap(ToType)));1876}18771878LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,1879LLVMTypeRef ToType) {1880return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),1881unwrap(ToType)));1882}18831884LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,1885LLVMTypeRef ToType) {1886return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),1887unwrap(ToType)));1888}18891890LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,1891LLVMValueRef IndexConstant) {1892return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),1893unwrap<Constant>(IndexConstant)));1894}18951896LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,1897LLVMValueRef ElementValueConstant,1898LLVMValueRef IndexConstant) {1899return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),1900unwrap<Constant>(ElementValueConstant),1901unwrap<Constant>(IndexConstant)));1902}19031904LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,1905LLVMValueRef VectorBConstant,1906LLVMValueRef MaskConstant) {1907SmallVector<int, 16> IntMask;1908ShuffleVectorInst::getShuffleMask(unwrap<Constant>(MaskConstant), IntMask);1909return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),1910unwrap<Constant>(VectorBConstant),1911IntMask));1912}19131914LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,1915const char *Constraints,1916LLVMBool HasSideEffects,1917LLVMBool IsAlignStack) {1918return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,1919Constraints, HasSideEffects, IsAlignStack));1920}19211922LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {1923return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));1924}19251926LLVMValueRef LLVMGetBlockAddressFunction(LLVMValueRef BlockAddr) {1927return wrap(unwrap<BlockAddress>(BlockAddr)->getFunction());1928}19291930LLVMBasicBlockRef LLVMGetBlockAddressBasicBlock(LLVMValueRef BlockAddr) {1931return wrap(unwrap<BlockAddress>(BlockAddr)->getBasicBlock());1932}19331934/*--.. Operations on global variables, functions, and aliases (globals) ....--*/19351936LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {1937return wrap(unwrap<GlobalValue>(Global)->getParent());1938}19391940LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {1941return unwrap<GlobalValue>(Global)->isDeclaration();1942}19431944LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {1945switch (unwrap<GlobalValue>(Global)->getLinkage()) {1946case GlobalValue::ExternalLinkage:1947return LLVMExternalLinkage;1948case GlobalValue::AvailableExternallyLinkage:1949return LLVMAvailableExternallyLinkage;1950case GlobalValue::LinkOnceAnyLinkage:1951return LLVMLinkOnceAnyLinkage;1952case GlobalValue::LinkOnceODRLinkage:1953return LLVMLinkOnceODRLinkage;1954case GlobalValue::WeakAnyLinkage:1955return LLVMWeakAnyLinkage;1956case GlobalValue::WeakODRLinkage:1957return LLVMWeakODRLinkage;1958case GlobalValue::AppendingLinkage:1959return LLVMAppendingLinkage;1960case GlobalValue::InternalLinkage:1961return LLVMInternalLinkage;1962case GlobalValue::PrivateLinkage:1963return LLVMPrivateLinkage;1964case GlobalValue::ExternalWeakLinkage:1965return LLVMExternalWeakLinkage;1966case GlobalValue::CommonLinkage:1967return LLVMCommonLinkage;1968}19691970llvm_unreachable("Invalid GlobalValue linkage!");1971}19721973void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {1974GlobalValue *GV = unwrap<GlobalValue>(Global);19751976switch (Linkage) {1977case LLVMExternalLinkage:1978GV->setLinkage(GlobalValue::ExternalLinkage);1979break;1980case LLVMAvailableExternallyLinkage:1981GV->setLinkage(GlobalValue::AvailableExternallyLinkage);1982break;1983case LLVMLinkOnceAnyLinkage:1984GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);1985break;1986case LLVMLinkOnceODRLinkage:1987GV->setLinkage(GlobalValue::LinkOnceODRLinkage);1988break;1989case LLVMLinkOnceODRAutoHideLinkage:1990LLVM_DEBUG(1991errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "1992"longer supported.");1993break;1994case LLVMWeakAnyLinkage:1995GV->setLinkage(GlobalValue::WeakAnyLinkage);1996break;1997case LLVMWeakODRLinkage:1998GV->setLinkage(GlobalValue::WeakODRLinkage);1999break;2000case LLVMAppendingLinkage:2001GV->setLinkage(GlobalValue::AppendingLinkage);2002break;2003case LLVMInternalLinkage:2004GV->setLinkage(GlobalValue::InternalLinkage);2005break;2006case LLVMPrivateLinkage:2007GV->setLinkage(GlobalValue::PrivateLinkage);2008break;2009case LLVMLinkerPrivateLinkage:2010GV->setLinkage(GlobalValue::PrivateLinkage);2011break;2012case LLVMLinkerPrivateWeakLinkage:2013GV->setLinkage(GlobalValue::PrivateLinkage);2014break;2015case LLVMDLLImportLinkage:2016LLVM_DEBUG(2017errs()2018<< "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");2019break;2020case LLVMDLLExportLinkage:2021LLVM_DEBUG(2022errs()2023<< "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");2024break;2025case LLVMExternalWeakLinkage:2026GV->setLinkage(GlobalValue::ExternalWeakLinkage);2027break;2028case LLVMGhostLinkage:2029LLVM_DEBUG(2030errs() << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");2031break;2032case LLVMCommonLinkage:2033GV->setLinkage(GlobalValue::CommonLinkage);2034break;2035}2036}20372038const char *LLVMGetSection(LLVMValueRef Global) {2039// Using .data() is safe because of how GlobalObject::setSection is2040// implemented.2041return unwrap<GlobalValue>(Global)->getSection().data();2042}20432044void LLVMSetSection(LLVMValueRef Global, const char *Section) {2045unwrap<GlobalObject>(Global)->setSection(Section);2046}20472048LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {2049return static_cast<LLVMVisibility>(2050unwrap<GlobalValue>(Global)->getVisibility());2051}20522053void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {2054unwrap<GlobalValue>(Global)2055->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));2056}20572058LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {2059return static_cast<LLVMDLLStorageClass>(2060unwrap<GlobalValue>(Global)->getDLLStorageClass());2061}20622063void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {2064unwrap<GlobalValue>(Global)->setDLLStorageClass(2065static_cast<GlobalValue::DLLStorageClassTypes>(Class));2066}20672068LLVMUnnamedAddr LLVMGetUnnamedAddress(LLVMValueRef Global) {2069switch (unwrap<GlobalValue>(Global)->getUnnamedAddr()) {2070case GlobalVariable::UnnamedAddr::None:2071return LLVMNoUnnamedAddr;2072case GlobalVariable::UnnamedAddr::Local:2073return LLVMLocalUnnamedAddr;2074case GlobalVariable::UnnamedAddr::Global:2075return LLVMGlobalUnnamedAddr;2076}2077llvm_unreachable("Unknown UnnamedAddr kind!");2078}20792080void LLVMSetUnnamedAddress(LLVMValueRef Global, LLVMUnnamedAddr UnnamedAddr) {2081GlobalValue *GV = unwrap<GlobalValue>(Global);20822083switch (UnnamedAddr) {2084case LLVMNoUnnamedAddr:2085return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::None);2086case LLVMLocalUnnamedAddr:2087return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Local);2088case LLVMGlobalUnnamedAddr:2089return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Global);2090}2091}20922093LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {2094return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();2095}20962097void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {2098unwrap<GlobalValue>(Global)->setUnnamedAddr(2099HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global2100: GlobalValue::UnnamedAddr::None);2101}21022103LLVMTypeRef LLVMGlobalGetValueType(LLVMValueRef Global) {2104return wrap(unwrap<GlobalValue>(Global)->getValueType());2105}21062107/*--.. Operations on global variables, load and store instructions .........--*/21082109unsigned LLVMGetAlignment(LLVMValueRef V) {2110Value *P = unwrap(V);2111if (GlobalObject *GV = dyn_cast<GlobalObject>(P))2112return GV->getAlign() ? GV->getAlign()->value() : 0;2113if (AllocaInst *AI = dyn_cast<AllocaInst>(P))2114return AI->getAlign().value();2115if (LoadInst *LI = dyn_cast<LoadInst>(P))2116return LI->getAlign().value();2117if (StoreInst *SI = dyn_cast<StoreInst>(P))2118return SI->getAlign().value();2119if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))2120return RMWI->getAlign().value();2121if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))2122return CXI->getAlign().value();21232124llvm_unreachable(2125"only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, "2126"and AtomicCmpXchgInst have alignment");2127}21282129void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {2130Value *P = unwrap(V);2131if (GlobalObject *GV = dyn_cast<GlobalObject>(P))2132GV->setAlignment(MaybeAlign(Bytes));2133else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))2134AI->setAlignment(Align(Bytes));2135else if (LoadInst *LI = dyn_cast<LoadInst>(P))2136LI->setAlignment(Align(Bytes));2137else if (StoreInst *SI = dyn_cast<StoreInst>(P))2138SI->setAlignment(Align(Bytes));2139else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))2140RMWI->setAlignment(Align(Bytes));2141else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))2142CXI->setAlignment(Align(Bytes));2143else2144llvm_unreachable(2145"only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, and "2146"and AtomicCmpXchgInst have alignment");2147}21482149LLVMValueMetadataEntry *LLVMGlobalCopyAllMetadata(LLVMValueRef Value,2150size_t *NumEntries) {2151return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {2152Entries.clear();2153if (Instruction *Instr = dyn_cast<Instruction>(unwrap(Value))) {2154Instr->getAllMetadata(Entries);2155} else {2156unwrap<GlobalObject>(Value)->getAllMetadata(Entries);2157}2158});2159}21602161unsigned LLVMValueMetadataEntriesGetKind(LLVMValueMetadataEntry *Entries,2162unsigned Index) {2163LLVMOpaqueValueMetadataEntry MVE =2164static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);2165return MVE.Kind;2166}21672168LLVMMetadataRef2169LLVMValueMetadataEntriesGetMetadata(LLVMValueMetadataEntry *Entries,2170unsigned Index) {2171LLVMOpaqueValueMetadataEntry MVE =2172static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);2173return MVE.Metadata;2174}21752176void LLVMDisposeValueMetadataEntries(LLVMValueMetadataEntry *Entries) {2177free(Entries);2178}21792180void LLVMGlobalSetMetadata(LLVMValueRef Global, unsigned Kind,2181LLVMMetadataRef MD) {2182unwrap<GlobalObject>(Global)->setMetadata(Kind, unwrap<MDNode>(MD));2183}21842185void LLVMGlobalEraseMetadata(LLVMValueRef Global, unsigned Kind) {2186unwrap<GlobalObject>(Global)->eraseMetadata(Kind);2187}21882189void LLVMGlobalClearMetadata(LLVMValueRef Global) {2190unwrap<GlobalObject>(Global)->clearMetadata();2191}21922193/*--.. Operations on global variables ......................................--*/21942195LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {2196return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,2197GlobalValue::ExternalLinkage, nullptr, Name));2198}21992200LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,2201const char *Name,2202unsigned AddressSpace) {2203return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,2204GlobalValue::ExternalLinkage, nullptr, Name,2205nullptr, GlobalVariable::NotThreadLocal,2206AddressSpace));2207}22082209LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {2210return wrap(unwrap(M)->getNamedGlobal(Name));2211}22122213LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {2214Module *Mod = unwrap(M);2215Module::global_iterator I = Mod->global_begin();2216if (I == Mod->global_end())2217return nullptr;2218return wrap(&*I);2219}22202221LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {2222Module *Mod = unwrap(M);2223Module::global_iterator I = Mod->global_end();2224if (I == Mod->global_begin())2225return nullptr;2226return wrap(&*--I);2227}22282229LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {2230GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);2231Module::global_iterator I(GV);2232if (++I == GV->getParent()->global_end())2233return nullptr;2234return wrap(&*I);2235}22362237LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {2238GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);2239Module::global_iterator I(GV);2240if (I == GV->getParent()->global_begin())2241return nullptr;2242return wrap(&*--I);2243}22442245void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {2246unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();2247}22482249LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {2250GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);2251if ( !GV->hasInitializer() )2252return nullptr;2253return wrap(GV->getInitializer());2254}22552256void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {2257unwrap<GlobalVariable>(GlobalVar)2258->setInitializer(unwrap<Constant>(ConstantVal));2259}22602261LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {2262return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();2263}22642265void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {2266unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);2267}22682269LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {2270return unwrap<GlobalVariable>(GlobalVar)->isConstant();2271}22722273void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {2274unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);2275}22762277LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {2278switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {2279case GlobalVariable::NotThreadLocal:2280return LLVMNotThreadLocal;2281case GlobalVariable::GeneralDynamicTLSModel:2282return LLVMGeneralDynamicTLSModel;2283case GlobalVariable::LocalDynamicTLSModel:2284return LLVMLocalDynamicTLSModel;2285case GlobalVariable::InitialExecTLSModel:2286return LLVMInitialExecTLSModel;2287case GlobalVariable::LocalExecTLSModel:2288return LLVMLocalExecTLSModel;2289}22902291llvm_unreachable("Invalid GlobalVariable thread local mode");2292}22932294void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {2295GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);22962297switch (Mode) {2298case LLVMNotThreadLocal:2299GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);2300break;2301case LLVMGeneralDynamicTLSModel:2302GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);2303break;2304case LLVMLocalDynamicTLSModel:2305GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);2306break;2307case LLVMInitialExecTLSModel:2308GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);2309break;2310case LLVMLocalExecTLSModel:2311GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);2312break;2313}2314}23152316LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {2317return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();2318}23192320void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {2321unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);2322}23232324/*--.. Operations on aliases ......................................--*/23252326LLVMValueRef LLVMAddAlias2(LLVMModuleRef M, LLVMTypeRef ValueTy,2327unsigned AddrSpace, LLVMValueRef Aliasee,2328const char *Name) {2329return wrap(GlobalAlias::create(unwrap(ValueTy), AddrSpace,2330GlobalValue::ExternalLinkage, Name,2331unwrap<Constant>(Aliasee), unwrap(M)));2332}23332334LLVMValueRef LLVMGetNamedGlobalAlias(LLVMModuleRef M,2335const char *Name, size_t NameLen) {2336return wrap(unwrap(M)->getNamedAlias(StringRef(Name, NameLen)));2337}23382339LLVMValueRef LLVMGetFirstGlobalAlias(LLVMModuleRef M) {2340Module *Mod = unwrap(M);2341Module::alias_iterator I = Mod->alias_begin();2342if (I == Mod->alias_end())2343return nullptr;2344return wrap(&*I);2345}23462347LLVMValueRef LLVMGetLastGlobalAlias(LLVMModuleRef M) {2348Module *Mod = unwrap(M);2349Module::alias_iterator I = Mod->alias_end();2350if (I == Mod->alias_begin())2351return nullptr;2352return wrap(&*--I);2353}23542355LLVMValueRef LLVMGetNextGlobalAlias(LLVMValueRef GA) {2356GlobalAlias *Alias = unwrap<GlobalAlias>(GA);2357Module::alias_iterator I(Alias);2358if (++I == Alias->getParent()->alias_end())2359return nullptr;2360return wrap(&*I);2361}23622363LLVMValueRef LLVMGetPreviousGlobalAlias(LLVMValueRef GA) {2364GlobalAlias *Alias = unwrap<GlobalAlias>(GA);2365Module::alias_iterator I(Alias);2366if (I == Alias->getParent()->alias_begin())2367return nullptr;2368return wrap(&*--I);2369}23702371LLVMValueRef LLVMAliasGetAliasee(LLVMValueRef Alias) {2372return wrap(unwrap<GlobalAlias>(Alias)->getAliasee());2373}23742375void LLVMAliasSetAliasee(LLVMValueRef Alias, LLVMValueRef Aliasee) {2376unwrap<GlobalAlias>(Alias)->setAliasee(unwrap<Constant>(Aliasee));2377}23782379/*--.. Operations on functions .............................................--*/23802381LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,2382LLVMTypeRef FunctionTy) {2383return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),2384GlobalValue::ExternalLinkage, Name, unwrap(M)));2385}23862387LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {2388return wrap(unwrap(M)->getFunction(Name));2389}23902391LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {2392Module *Mod = unwrap(M);2393Module::iterator I = Mod->begin();2394if (I == Mod->end())2395return nullptr;2396return wrap(&*I);2397}23982399LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {2400Module *Mod = unwrap(M);2401Module::iterator I = Mod->end();2402if (I == Mod->begin())2403return nullptr;2404return wrap(&*--I);2405}24062407LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {2408Function *Func = unwrap<Function>(Fn);2409Module::iterator I(Func);2410if (++I == Func->getParent()->end())2411return nullptr;2412return wrap(&*I);2413}24142415LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {2416Function *Func = unwrap<Function>(Fn);2417Module::iterator I(Func);2418if (I == Func->getParent()->begin())2419return nullptr;2420return wrap(&*--I);2421}24222423void LLVMDeleteFunction(LLVMValueRef Fn) {2424unwrap<Function>(Fn)->eraseFromParent();2425}24262427LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) {2428return unwrap<Function>(Fn)->hasPersonalityFn();2429}24302431LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) {2432return wrap(unwrap<Function>(Fn)->getPersonalityFn());2433}24342435void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) {2436unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));2437}24382439unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {2440if (Function *F = dyn_cast<Function>(unwrap(Fn)))2441return F->getIntrinsicID();2442return 0;2443}24442445static Intrinsic::ID llvm_map_to_intrinsic_id(unsigned ID) {2446assert(ID < llvm::Intrinsic::num_intrinsics && "Intrinsic ID out of range");2447return llvm::Intrinsic::ID(ID);2448}24492450LLVMValueRef LLVMGetIntrinsicDeclaration(LLVMModuleRef Mod,2451unsigned ID,2452LLVMTypeRef *ParamTypes,2453size_t ParamCount) {2454ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);2455auto IID = llvm_map_to_intrinsic_id(ID);2456return wrap(llvm::Intrinsic::getDeclaration(unwrap(Mod), IID, Tys));2457}24582459const char *LLVMIntrinsicGetName(unsigned ID, size_t *NameLength) {2460auto IID = llvm_map_to_intrinsic_id(ID);2461auto Str = llvm::Intrinsic::getName(IID);2462*NameLength = Str.size();2463return Str.data();2464}24652466LLVMTypeRef LLVMIntrinsicGetType(LLVMContextRef Ctx, unsigned ID,2467LLVMTypeRef *ParamTypes, size_t ParamCount) {2468auto IID = llvm_map_to_intrinsic_id(ID);2469ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);2470return wrap(llvm::Intrinsic::getType(*unwrap(Ctx), IID, Tys));2471}24722473const char *LLVMIntrinsicCopyOverloadedName(unsigned ID,2474LLVMTypeRef *ParamTypes,2475size_t ParamCount,2476size_t *NameLength) {2477auto IID = llvm_map_to_intrinsic_id(ID);2478ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);2479auto Str = llvm::Intrinsic::getNameNoUnnamedTypes(IID, Tys);2480*NameLength = Str.length();2481return strdup(Str.c_str());2482}24832484const char *LLVMIntrinsicCopyOverloadedName2(LLVMModuleRef Mod, unsigned ID,2485LLVMTypeRef *ParamTypes,2486size_t ParamCount,2487size_t *NameLength) {2488auto IID = llvm_map_to_intrinsic_id(ID);2489ArrayRef<Type *> Tys(unwrap(ParamTypes), ParamCount);2490auto Str = llvm::Intrinsic::getName(IID, Tys, unwrap(Mod));2491*NameLength = Str.length();2492return strdup(Str.c_str());2493}24942495unsigned LLVMLookupIntrinsicID(const char *Name, size_t NameLen) {2496return Function::lookupIntrinsicID({Name, NameLen});2497}24982499LLVMBool LLVMIntrinsicIsOverloaded(unsigned ID) {2500auto IID = llvm_map_to_intrinsic_id(ID);2501return llvm::Intrinsic::isOverloaded(IID);2502}25032504unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {2505return unwrap<Function>(Fn)->getCallingConv();2506}25072508void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {2509return unwrap<Function>(Fn)->setCallingConv(2510static_cast<CallingConv::ID>(CC));2511}25122513const char *LLVMGetGC(LLVMValueRef Fn) {2514Function *F = unwrap<Function>(Fn);2515return F->hasGC()? F->getGC().c_str() : nullptr;2516}25172518void LLVMSetGC(LLVMValueRef Fn, const char *GC) {2519Function *F = unwrap<Function>(Fn);2520if (GC)2521F->setGC(GC);2522else2523F->clearGC();2524}25252526LLVMValueRef LLVMGetPrefixData(LLVMValueRef Fn) {2527Function *F = unwrap<Function>(Fn);2528return wrap(F->getPrefixData());2529}25302531LLVMBool LLVMHasPrefixData(LLVMValueRef Fn) {2532Function *F = unwrap<Function>(Fn);2533return F->hasPrefixData();2534}25352536void LLVMSetPrefixData(LLVMValueRef Fn, LLVMValueRef prefixData) {2537Function *F = unwrap<Function>(Fn);2538Constant *prefix = unwrap<Constant>(prefixData);2539F->setPrefixData(prefix);2540}25412542LLVMValueRef LLVMGetPrologueData(LLVMValueRef Fn) {2543Function *F = unwrap<Function>(Fn);2544return wrap(F->getPrologueData());2545}25462547LLVMBool LLVMHasPrologueData(LLVMValueRef Fn) {2548Function *F = unwrap<Function>(Fn);2549return F->hasPrologueData();2550}25512552void LLVMSetPrologueData(LLVMValueRef Fn, LLVMValueRef prologueData) {2553Function *F = unwrap<Function>(Fn);2554Constant *prologue = unwrap<Constant>(prologueData);2555F->setPrologueData(prologue);2556}25572558void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,2559LLVMAttributeRef A) {2560unwrap<Function>(F)->addAttributeAtIndex(Idx, unwrap(A));2561}25622563unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) {2564auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);2565return AS.getNumAttributes();2566}25672568void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,2569LLVMAttributeRef *Attrs) {2570auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);2571for (auto A : AS)2572*Attrs++ = wrap(A);2573}25742575LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F,2576LLVMAttributeIndex Idx,2577unsigned KindID) {2578return wrap(unwrap<Function>(F)->getAttributeAtIndex(2579Idx, (Attribute::AttrKind)KindID));2580}25812582LLVMAttributeRef LLVMGetStringAttributeAtIndex(LLVMValueRef F,2583LLVMAttributeIndex Idx,2584const char *K, unsigned KLen) {2585return wrap(2586unwrap<Function>(F)->getAttributeAtIndex(Idx, StringRef(K, KLen)));2587}25882589void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,2590unsigned KindID) {2591unwrap<Function>(F)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);2592}25932594void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,2595const char *K, unsigned KLen) {2596unwrap<Function>(F)->removeAttributeAtIndex(Idx, StringRef(K, KLen));2597}25982599void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,2600const char *V) {2601Function *Func = unwrap<Function>(Fn);2602Attribute Attr = Attribute::get(Func->getContext(), A, V);2603Func->addFnAttr(Attr);2604}26052606/*--.. Operations on parameters ............................................--*/26072608unsigned LLVMCountParams(LLVMValueRef FnRef) {2609// This function is strictly redundant to2610// LLVMCountParamTypes(LLVMGlobalGetValueType(FnRef))2611return unwrap<Function>(FnRef)->arg_size();2612}26132614void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {2615Function *Fn = unwrap<Function>(FnRef);2616for (Argument &A : Fn->args())2617*ParamRefs++ = wrap(&A);2618}26192620LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {2621Function *Fn = unwrap<Function>(FnRef);2622return wrap(&Fn->arg_begin()[index]);2623}26242625LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {2626return wrap(unwrap<Argument>(V)->getParent());2627}26282629LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {2630Function *Func = unwrap<Function>(Fn);2631Function::arg_iterator I = Func->arg_begin();2632if (I == Func->arg_end())2633return nullptr;2634return wrap(&*I);2635}26362637LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {2638Function *Func = unwrap<Function>(Fn);2639Function::arg_iterator I = Func->arg_end();2640if (I == Func->arg_begin())2641return nullptr;2642return wrap(&*--I);2643}26442645LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {2646Argument *A = unwrap<Argument>(Arg);2647Function *Fn = A->getParent();2648if (A->getArgNo() + 1 >= Fn->arg_size())2649return nullptr;2650return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);2651}26522653LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {2654Argument *A = unwrap<Argument>(Arg);2655if (A->getArgNo() == 0)2656return nullptr;2657return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);2658}26592660void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {2661Argument *A = unwrap<Argument>(Arg);2662A->addAttr(Attribute::getWithAlignment(A->getContext(), Align(align)));2663}26642665/*--.. Operations on ifuncs ................................................--*/26662667LLVMValueRef LLVMAddGlobalIFunc(LLVMModuleRef M,2668const char *Name, size_t NameLen,2669LLVMTypeRef Ty, unsigned AddrSpace,2670LLVMValueRef Resolver) {2671return wrap(GlobalIFunc::create(unwrap(Ty), AddrSpace,2672GlobalValue::ExternalLinkage,2673StringRef(Name, NameLen),2674unwrap<Constant>(Resolver), unwrap(M)));2675}26762677LLVMValueRef LLVMGetNamedGlobalIFunc(LLVMModuleRef M,2678const char *Name, size_t NameLen) {2679return wrap(unwrap(M)->getNamedIFunc(StringRef(Name, NameLen)));2680}26812682LLVMValueRef LLVMGetFirstGlobalIFunc(LLVMModuleRef M) {2683Module *Mod = unwrap(M);2684Module::ifunc_iterator I = Mod->ifunc_begin();2685if (I == Mod->ifunc_end())2686return nullptr;2687return wrap(&*I);2688}26892690LLVMValueRef LLVMGetLastGlobalIFunc(LLVMModuleRef M) {2691Module *Mod = unwrap(M);2692Module::ifunc_iterator I = Mod->ifunc_end();2693if (I == Mod->ifunc_begin())2694return nullptr;2695return wrap(&*--I);2696}26972698LLVMValueRef LLVMGetNextGlobalIFunc(LLVMValueRef IFunc) {2699GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);2700Module::ifunc_iterator I(GIF);2701if (++I == GIF->getParent()->ifunc_end())2702return nullptr;2703return wrap(&*I);2704}27052706LLVMValueRef LLVMGetPreviousGlobalIFunc(LLVMValueRef IFunc) {2707GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);2708Module::ifunc_iterator I(GIF);2709if (I == GIF->getParent()->ifunc_begin())2710return nullptr;2711return wrap(&*--I);2712}27132714LLVMValueRef LLVMGetGlobalIFuncResolver(LLVMValueRef IFunc) {2715return wrap(unwrap<GlobalIFunc>(IFunc)->getResolver());2716}27172718void LLVMSetGlobalIFuncResolver(LLVMValueRef IFunc, LLVMValueRef Resolver) {2719unwrap<GlobalIFunc>(IFunc)->setResolver(unwrap<Constant>(Resolver));2720}27212722void LLVMEraseGlobalIFunc(LLVMValueRef IFunc) {2723unwrap<GlobalIFunc>(IFunc)->eraseFromParent();2724}27252726void LLVMRemoveGlobalIFunc(LLVMValueRef IFunc) {2727unwrap<GlobalIFunc>(IFunc)->removeFromParent();2728}27292730/*--.. Operations on operand bundles........................................--*/27312732LLVMOperandBundleRef LLVMCreateOperandBundle(const char *Tag, size_t TagLen,2733LLVMValueRef *Args,2734unsigned NumArgs) {2735return wrap(new OperandBundleDef(std::string(Tag, TagLen),2736ArrayRef(unwrap(Args), NumArgs)));2737}27382739void LLVMDisposeOperandBundle(LLVMOperandBundleRef Bundle) {2740delete unwrap(Bundle);2741}27422743const char *LLVMGetOperandBundleTag(LLVMOperandBundleRef Bundle, size_t *Len) {2744StringRef Str = unwrap(Bundle)->getTag();2745*Len = Str.size();2746return Str.data();2747}27482749unsigned LLVMGetNumOperandBundleArgs(LLVMOperandBundleRef Bundle) {2750return unwrap(Bundle)->inputs().size();2751}27522753LLVMValueRef LLVMGetOperandBundleArgAtIndex(LLVMOperandBundleRef Bundle,2754unsigned Index) {2755return wrap(unwrap(Bundle)->inputs()[Index]);2756}27572758/*--.. Operations on basic blocks ..........................................--*/27592760LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {2761return wrap(static_cast<Value*>(unwrap(BB)));2762}27632764LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {2765return isa<BasicBlock>(unwrap(Val));2766}27672768LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {2769return wrap(unwrap<BasicBlock>(Val));2770}27712772const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) {2773return unwrap(BB)->getName().data();2774}27752776LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {2777return wrap(unwrap(BB)->getParent());2778}27792780LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {2781return wrap(unwrap(BB)->getTerminator());2782}27832784unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {2785return unwrap<Function>(FnRef)->size();2786}27872788void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){2789Function *Fn = unwrap<Function>(FnRef);2790for (BasicBlock &BB : *Fn)2791*BasicBlocksRefs++ = wrap(&BB);2792}27932794LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {2795return wrap(&unwrap<Function>(Fn)->getEntryBlock());2796}27972798LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {2799Function *Func = unwrap<Function>(Fn);2800Function::iterator I = Func->begin();2801if (I == Func->end())2802return nullptr;2803return wrap(&*I);2804}28052806LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {2807Function *Func = unwrap<Function>(Fn);2808Function::iterator I = Func->end();2809if (I == Func->begin())2810return nullptr;2811return wrap(&*--I);2812}28132814LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {2815BasicBlock *Block = unwrap(BB);2816Function::iterator I(Block);2817if (++I == Block->getParent()->end())2818return nullptr;2819return wrap(&*I);2820}28212822LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {2823BasicBlock *Block = unwrap(BB);2824Function::iterator I(Block);2825if (I == Block->getParent()->begin())2826return nullptr;2827return wrap(&*--I);2828}28292830LLVMBasicBlockRef LLVMCreateBasicBlockInContext(LLVMContextRef C,2831const char *Name) {2832return wrap(llvm::BasicBlock::Create(*unwrap(C), Name));2833}28342835void LLVMInsertExistingBasicBlockAfterInsertBlock(LLVMBuilderRef Builder,2836LLVMBasicBlockRef BB) {2837BasicBlock *ToInsert = unwrap(BB);2838BasicBlock *CurBB = unwrap(Builder)->GetInsertBlock();2839assert(CurBB && "current insertion point is invalid!");2840CurBB->getParent()->insert(std::next(CurBB->getIterator()), ToInsert);2841}28422843void LLVMAppendExistingBasicBlock(LLVMValueRef Fn,2844LLVMBasicBlockRef BB) {2845unwrap<Function>(Fn)->insert(unwrap<Function>(Fn)->end(), unwrap(BB));2846}28472848LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,2849LLVMValueRef FnRef,2850const char *Name) {2851return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));2852}28532854LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {2855return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);2856}28572858LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,2859LLVMBasicBlockRef BBRef,2860const char *Name) {2861BasicBlock *BB = unwrap(BBRef);2862return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));2863}28642865LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,2866const char *Name) {2867return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);2868}28692870void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {2871unwrap(BBRef)->eraseFromParent();2872}28732874void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {2875unwrap(BBRef)->removeFromParent();2876}28772878void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {2879unwrap(BB)->moveBefore(unwrap(MovePos));2880}28812882void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {2883unwrap(BB)->moveAfter(unwrap(MovePos));2884}28852886/*--.. Operations on instructions ..........................................--*/28872888LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {2889return wrap(unwrap<Instruction>(Inst)->getParent());2890}28912892LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {2893BasicBlock *Block = unwrap(BB);2894BasicBlock::iterator I = Block->begin();2895if (I == Block->end())2896return nullptr;2897return wrap(&*I);2898}28992900LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {2901BasicBlock *Block = unwrap(BB);2902BasicBlock::iterator I = Block->end();2903if (I == Block->begin())2904return nullptr;2905return wrap(&*--I);2906}29072908LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {2909Instruction *Instr = unwrap<Instruction>(Inst);2910BasicBlock::iterator I(Instr);2911if (++I == Instr->getParent()->end())2912return nullptr;2913return wrap(&*I);2914}29152916LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {2917Instruction *Instr = unwrap<Instruction>(Inst);2918BasicBlock::iterator I(Instr);2919if (I == Instr->getParent()->begin())2920return nullptr;2921return wrap(&*--I);2922}29232924void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) {2925unwrap<Instruction>(Inst)->removeFromParent();2926}29272928void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {2929unwrap<Instruction>(Inst)->eraseFromParent();2930}29312932void LLVMDeleteInstruction(LLVMValueRef Inst) {2933unwrap<Instruction>(Inst)->deleteValue();2934}29352936LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {2937if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))2938return (LLVMIntPredicate)I->getPredicate();2939return (LLVMIntPredicate)0;2940}29412942LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {2943if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))2944return (LLVMRealPredicate)I->getPredicate();2945return (LLVMRealPredicate)0;2946}29472948LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {2949if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))2950return map_to_llvmopcode(C->getOpcode());2951return (LLVMOpcode)0;2952}29532954LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {2955if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))2956return wrap(C->clone());2957return nullptr;2958}29592960LLVMValueRef LLVMIsATerminatorInst(LLVMValueRef Inst) {2961Instruction *I = dyn_cast<Instruction>(unwrap(Inst));2962return (I && I->isTerminator()) ? wrap(I) : nullptr;2963}29642965unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) {2966if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) {2967return FPI->arg_size();2968}2969return unwrap<CallBase>(Instr)->arg_size();2970}29712972/*--.. Call and invoke instructions ........................................--*/29732974unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {2975return unwrap<CallBase>(Instr)->getCallingConv();2976}29772978void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {2979return unwrap<CallBase>(Instr)->setCallingConv(2980static_cast<CallingConv::ID>(CC));2981}29822983void LLVMSetInstrParamAlignment(LLVMValueRef Instr, LLVMAttributeIndex Idx,2984unsigned align) {2985auto *Call = unwrap<CallBase>(Instr);2986Attribute AlignAttr =2987Attribute::getWithAlignment(Call->getContext(), Align(align));2988Call->addAttributeAtIndex(Idx, AlignAttr);2989}29902991void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,2992LLVMAttributeRef A) {2993unwrap<CallBase>(C)->addAttributeAtIndex(Idx, unwrap(A));2994}29952996unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C,2997LLVMAttributeIndex Idx) {2998auto *Call = unwrap<CallBase>(C);2999auto AS = Call->getAttributes().getAttributes(Idx);3000return AS.getNumAttributes();3001}30023003void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx,3004LLVMAttributeRef *Attrs) {3005auto *Call = unwrap<CallBase>(C);3006auto AS = Call->getAttributes().getAttributes(Idx);3007for (auto A : AS)3008*Attrs++ = wrap(A);3009}30103011LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C,3012LLVMAttributeIndex Idx,3013unsigned KindID) {3014return wrap(unwrap<CallBase>(C)->getAttributeAtIndex(3015Idx, (Attribute::AttrKind)KindID));3016}30173018LLVMAttributeRef LLVMGetCallSiteStringAttribute(LLVMValueRef C,3019LLVMAttributeIndex Idx,3020const char *K, unsigned KLen) {3021return wrap(3022unwrap<CallBase>(C)->getAttributeAtIndex(Idx, StringRef(K, KLen)));3023}30243025void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,3026unsigned KindID) {3027unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);3028}30293030void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,3031const char *K, unsigned KLen) {3032unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, StringRef(K, KLen));3033}30343035LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) {3036return wrap(unwrap<CallBase>(Instr)->getCalledOperand());3037}30383039LLVMTypeRef LLVMGetCalledFunctionType(LLVMValueRef Instr) {3040return wrap(unwrap<CallBase>(Instr)->getFunctionType());3041}30423043unsigned LLVMGetNumOperandBundles(LLVMValueRef C) {3044return unwrap<CallBase>(C)->getNumOperandBundles();3045}30463047LLVMOperandBundleRef LLVMGetOperandBundleAtIndex(LLVMValueRef C,3048unsigned Index) {3049return wrap(3050new OperandBundleDef(unwrap<CallBase>(C)->getOperandBundleAt(Index)));3051}30523053/*--.. Operations on call instructions (only) ..............................--*/30543055LLVMBool LLVMIsTailCall(LLVMValueRef Call) {3056return unwrap<CallInst>(Call)->isTailCall();3057}30583059void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {3060unwrap<CallInst>(Call)->setTailCall(isTailCall);3061}30623063LLVMTailCallKind LLVMGetTailCallKind(LLVMValueRef Call) {3064return (LLVMTailCallKind)unwrap<CallInst>(Call)->getTailCallKind();3065}30663067void LLVMSetTailCallKind(LLVMValueRef Call, LLVMTailCallKind kind) {3068unwrap<CallInst>(Call)->setTailCallKind((CallInst::TailCallKind)kind);3069}30703071/*--.. Operations on invoke instructions (only) ............................--*/30723073LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) {3074return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());3075}30763077LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) {3078if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {3079return wrap(CRI->getUnwindDest());3080} else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {3081return wrap(CSI->getUnwindDest());3082}3083return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());3084}30853086void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {3087unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));3088}30893090void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {3091if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {3092return CRI->setUnwindDest(unwrap(B));3093} else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {3094return CSI->setUnwindDest(unwrap(B));3095}3096unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));3097}30983099LLVMBasicBlockRef LLVMGetCallBrDefaultDest(LLVMValueRef CallBr) {3100return wrap(unwrap<CallBrInst>(CallBr)->getDefaultDest());3101}31023103unsigned LLVMGetCallBrNumIndirectDests(LLVMValueRef CallBr) {3104return unwrap<CallBrInst>(CallBr)->getNumIndirectDests();3105}31063107LLVMBasicBlockRef LLVMGetCallBrIndirectDest(LLVMValueRef CallBr, unsigned Idx) {3108return wrap(unwrap<CallBrInst>(CallBr)->getIndirectDest(Idx));3109}31103111/*--.. Operations on terminators ...........................................--*/31123113unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {3114return unwrap<Instruction>(Term)->getNumSuccessors();3115}31163117LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) {3118return wrap(unwrap<Instruction>(Term)->getSuccessor(i));3119}31203121void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {3122return unwrap<Instruction>(Term)->setSuccessor(i, unwrap(block));3123}31243125/*--.. Operations on branch instructions (only) ............................--*/31263127LLVMBool LLVMIsConditional(LLVMValueRef Branch) {3128return unwrap<BranchInst>(Branch)->isConditional();3129}31303131LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) {3132return wrap(unwrap<BranchInst>(Branch)->getCondition());3133}31343135void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) {3136return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));3137}31383139/*--.. Operations on switch instructions (only) ............................--*/31403141LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {3142return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());3143}31443145/*--.. Operations on alloca instructions (only) ............................--*/31463147LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) {3148return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());3149}31503151/*--.. Operations on gep instructions (only) ...............................--*/31523153LLVMBool LLVMIsInBounds(LLVMValueRef GEP) {3154return unwrap<GEPOperator>(GEP)->isInBounds();3155}31563157void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) {3158return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);3159}31603161LLVMTypeRef LLVMGetGEPSourceElementType(LLVMValueRef GEP) {3162return wrap(unwrap<GEPOperator>(GEP)->getSourceElementType());3163}31643165LLVMGEPNoWrapFlags LLVMGEPGetNoWrapFlags(LLVMValueRef GEP) {3166GEPOperator *GEPOp = unwrap<GEPOperator>(GEP);3167return mapToLLVMGEPNoWrapFlags(GEPOp->getNoWrapFlags());3168}31693170void LLVMGEPSetNoWrapFlags(LLVMValueRef GEP, LLVMGEPNoWrapFlags NoWrapFlags) {3171GetElementPtrInst *GEPInst = unwrap<GetElementPtrInst>(GEP);3172GEPInst->setNoWrapFlags(mapFromLLVMGEPNoWrapFlags(NoWrapFlags));3173}31743175/*--.. Operations on phi nodes .............................................--*/31763177void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,3178LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {3179PHINode *PhiVal = unwrap<PHINode>(PhiNode);3180for (unsigned I = 0; I != Count; ++I)3181PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));3182}31833184unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {3185return unwrap<PHINode>(PhiNode)->getNumIncomingValues();3186}31873188LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {3189return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));3190}31913192LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {3193return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));3194}31953196/*--.. Operations on extractvalue and insertvalue nodes ....................--*/31973198unsigned LLVMGetNumIndices(LLVMValueRef Inst) {3199auto *I = unwrap(Inst);3200if (auto *GEP = dyn_cast<GEPOperator>(I))3201return GEP->getNumIndices();3202if (auto *EV = dyn_cast<ExtractValueInst>(I))3203return EV->getNumIndices();3204if (auto *IV = dyn_cast<InsertValueInst>(I))3205return IV->getNumIndices();3206llvm_unreachable(3207"LLVMGetNumIndices applies only to extractvalue and insertvalue!");3208}32093210const unsigned *LLVMGetIndices(LLVMValueRef Inst) {3211auto *I = unwrap(Inst);3212if (auto *EV = dyn_cast<ExtractValueInst>(I))3213return EV->getIndices().data();3214if (auto *IV = dyn_cast<InsertValueInst>(I))3215return IV->getIndices().data();3216llvm_unreachable(3217"LLVMGetIndices applies only to extractvalue and insertvalue!");3218}321932203221/*===-- Instruction builders ----------------------------------------------===*/32223223LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {3224return wrap(new IRBuilder<>(*unwrap(C)));3225}32263227LLVMBuilderRef LLVMCreateBuilder(void) {3228return LLVMCreateBuilderInContext(LLVMGetGlobalContext());3229}32303231static void LLVMPositionBuilderImpl(IRBuilder<> *Builder, BasicBlock *Block,3232Instruction *Instr, bool BeforeDbgRecords) {3233BasicBlock::iterator I = Instr ? Instr->getIterator() : Block->end();3234I.setHeadBit(BeforeDbgRecords);3235Builder->SetInsertPoint(Block, I);3236}32373238void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,3239LLVMValueRef Instr) {3240return LLVMPositionBuilderImpl(unwrap(Builder), unwrap(Block),3241unwrap<Instruction>(Instr), false);3242}32433244void LLVMPositionBuilderBeforeDbgRecords(LLVMBuilderRef Builder,3245LLVMBasicBlockRef Block,3246LLVMValueRef Instr) {3247return LLVMPositionBuilderImpl(unwrap(Builder), unwrap(Block),3248unwrap<Instruction>(Instr), true);3249}32503251void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {3252Instruction *I = unwrap<Instruction>(Instr);3253return LLVMPositionBuilderImpl(unwrap(Builder), I->getParent(), I, false);3254}32553256void LLVMPositionBuilderBeforeInstrAndDbgRecords(LLVMBuilderRef Builder,3257LLVMValueRef Instr) {3258Instruction *I = unwrap<Instruction>(Instr);3259return LLVMPositionBuilderImpl(unwrap(Builder), I->getParent(), I, true);3260}32613262void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {3263BasicBlock *BB = unwrap(Block);3264unwrap(Builder)->SetInsertPoint(BB);3265}32663267LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {3268return wrap(unwrap(Builder)->GetInsertBlock());3269}32703271void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {3272unwrap(Builder)->ClearInsertionPoint();3273}32743275void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {3276unwrap(Builder)->Insert(unwrap<Instruction>(Instr));3277}32783279void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,3280const char *Name) {3281unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);3282}32833284void LLVMDisposeBuilder(LLVMBuilderRef Builder) {3285delete unwrap(Builder);3286}32873288/*--.. Metadata builders ...................................................--*/32893290LLVMMetadataRef LLVMGetCurrentDebugLocation2(LLVMBuilderRef Builder) {3291return wrap(unwrap(Builder)->getCurrentDebugLocation().getAsMDNode());3292}32933294void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Builder, LLVMMetadataRef Loc) {3295if (Loc)3296unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(unwrap<MDNode>(Loc)));3297else3298unwrap(Builder)->SetCurrentDebugLocation(DebugLoc());3299}33003301void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {3302MDNode *Loc =3303L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;3304unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));3305}33063307LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {3308LLVMContext &Context = unwrap(Builder)->getContext();3309return wrap(MetadataAsValue::get(3310Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));3311}33123313void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {3314unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));3315}33163317void LLVMAddMetadataToInst(LLVMBuilderRef Builder, LLVMValueRef Inst) {3318unwrap(Builder)->AddMetadataToInst(unwrap<Instruction>(Inst));3319}33203321void LLVMBuilderSetDefaultFPMathTag(LLVMBuilderRef Builder,3322LLVMMetadataRef FPMathTag) {33233324unwrap(Builder)->setDefaultFPMathTag(FPMathTag3325? unwrap<MDNode>(FPMathTag)3326: nullptr);3327}33283329LLVMMetadataRef LLVMBuilderGetDefaultFPMathTag(LLVMBuilderRef Builder) {3330return wrap(unwrap(Builder)->getDefaultFPMathTag());3331}33323333/*--.. Instruction builders ................................................--*/33343335LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {3336return wrap(unwrap(B)->CreateRetVoid());3337}33383339LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {3340return wrap(unwrap(B)->CreateRet(unwrap(V)));3341}33423343LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,3344unsigned N) {3345return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));3346}33473348LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {3349return wrap(unwrap(B)->CreateBr(unwrap(Dest)));3350}33513352LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,3353LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {3354return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));3355}33563357LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,3358LLVMBasicBlockRef Else, unsigned NumCases) {3359return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));3360}33613362LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,3363unsigned NumDests) {3364return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));3365}33663367LLVMValueRef LLVMBuildCallBr(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn,3368LLVMBasicBlockRef DefaultDest,3369LLVMBasicBlockRef *IndirectDests,3370unsigned NumIndirectDests, LLVMValueRef *Args,3371unsigned NumArgs, LLVMOperandBundleRef *Bundles,3372unsigned NumBundles, const char *Name) {33733374SmallVector<OperandBundleDef, 8> OBs;3375for (auto *Bundle : ArrayRef(Bundles, NumBundles)) {3376OperandBundleDef *OB = unwrap(Bundle);3377OBs.push_back(*OB);3378}33793380return wrap(unwrap(B)->CreateCallBr(3381unwrap<FunctionType>(Ty), unwrap(Fn), unwrap(DefaultDest),3382ArrayRef(unwrap(IndirectDests), NumIndirectDests),3383ArrayRef<Value *>(unwrap(Args), NumArgs), OBs, Name));3384}33853386LLVMValueRef LLVMBuildInvoke2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn,3387LLVMValueRef *Args, unsigned NumArgs,3388LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,3389const char *Name) {3390return wrap(unwrap(B)->CreateInvoke(unwrap<FunctionType>(Ty), unwrap(Fn),3391unwrap(Then), unwrap(Catch),3392ArrayRef(unwrap(Args), NumArgs), Name));3393}33943395LLVMValueRef LLVMBuildInvokeWithOperandBundles(3396LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn, LLVMValueRef *Args,3397unsigned NumArgs, LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,3398LLVMOperandBundleRef *Bundles, unsigned NumBundles, const char *Name) {3399SmallVector<OperandBundleDef, 8> OBs;3400for (auto *Bundle : ArrayRef(Bundles, NumBundles)) {3401OperandBundleDef *OB = unwrap(Bundle);3402OBs.push_back(*OB);3403}3404return wrap(unwrap(B)->CreateInvoke(3405unwrap<FunctionType>(Ty), unwrap(Fn), unwrap(Then), unwrap(Catch),3406ArrayRef(unwrap(Args), NumArgs), OBs, Name));3407}34083409LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,3410LLVMValueRef PersFn, unsigned NumClauses,3411const char *Name) {3412// The personality used to live on the landingpad instruction, but now it3413// lives on the parent function. For compatibility, take the provided3414// personality and put it on the parent function.3415if (PersFn)3416unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(3417unwrap<Function>(PersFn));3418return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));3419}34203421LLVMValueRef LLVMBuildCatchPad(LLVMBuilderRef B, LLVMValueRef ParentPad,3422LLVMValueRef *Args, unsigned NumArgs,3423const char *Name) {3424return wrap(unwrap(B)->CreateCatchPad(unwrap(ParentPad),3425ArrayRef(unwrap(Args), NumArgs), Name));3426}34273428LLVMValueRef LLVMBuildCleanupPad(LLVMBuilderRef B, LLVMValueRef ParentPad,3429LLVMValueRef *Args, unsigned NumArgs,3430const char *Name) {3431if (ParentPad == nullptr) {3432Type *Ty = Type::getTokenTy(unwrap(B)->getContext());3433ParentPad = wrap(Constant::getNullValue(Ty));3434}3435return wrap(unwrap(B)->CreateCleanupPad(3436unwrap(ParentPad), ArrayRef(unwrap(Args), NumArgs), Name));3437}34383439LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {3440return wrap(unwrap(B)->CreateResume(unwrap(Exn)));3441}34423443LLVMValueRef LLVMBuildCatchSwitch(LLVMBuilderRef B, LLVMValueRef ParentPad,3444LLVMBasicBlockRef UnwindBB,3445unsigned NumHandlers, const char *Name) {3446if (ParentPad == nullptr) {3447Type *Ty = Type::getTokenTy(unwrap(B)->getContext());3448ParentPad = wrap(Constant::getNullValue(Ty));3449}3450return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(UnwindBB),3451NumHandlers, Name));3452}34533454LLVMValueRef LLVMBuildCatchRet(LLVMBuilderRef B, LLVMValueRef CatchPad,3455LLVMBasicBlockRef BB) {3456return wrap(unwrap(B)->CreateCatchRet(unwrap<CatchPadInst>(CatchPad),3457unwrap(BB)));3458}34593460LLVMValueRef LLVMBuildCleanupRet(LLVMBuilderRef B, LLVMValueRef CatchPad,3461LLVMBasicBlockRef BB) {3462return wrap(unwrap(B)->CreateCleanupRet(unwrap<CleanupPadInst>(CatchPad),3463unwrap(BB)));3464}34653466LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {3467return wrap(unwrap(B)->CreateUnreachable());3468}34693470void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,3471LLVMBasicBlockRef Dest) {3472unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));3473}34743475void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {3476unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));3477}34783479unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {3480return unwrap<LandingPadInst>(LandingPad)->getNumClauses();3481}34823483LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {3484return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));3485}34863487void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {3488unwrap<LandingPadInst>(LandingPad)->addClause(unwrap<Constant>(ClauseVal));3489}34903491LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) {3492return unwrap<LandingPadInst>(LandingPad)->isCleanup();3493}34943495void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {3496unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);3497}34983499void LLVMAddHandler(LLVMValueRef CatchSwitch, LLVMBasicBlockRef Dest) {3500unwrap<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Dest));3501}35023503unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) {3504return unwrap<CatchSwitchInst>(CatchSwitch)->getNumHandlers();3505}35063507void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) {3508CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(CatchSwitch);3509for (const BasicBlock *H : CSI->handlers())3510*Handlers++ = wrap(H);3511}35123513LLVMValueRef LLVMGetParentCatchSwitch(LLVMValueRef CatchPad) {3514return wrap(unwrap<CatchPadInst>(CatchPad)->getCatchSwitch());3515}35163517void LLVMSetParentCatchSwitch(LLVMValueRef CatchPad, LLVMValueRef CatchSwitch) {3518unwrap<CatchPadInst>(CatchPad)3519->setCatchSwitch(unwrap<CatchSwitchInst>(CatchSwitch));3520}35213522/*--.. Funclets ...........................................................--*/35233524LLVMValueRef LLVMGetArgOperand(LLVMValueRef Funclet, unsigned i) {3525return wrap(unwrap<FuncletPadInst>(Funclet)->getArgOperand(i));3526}35273528void LLVMSetArgOperand(LLVMValueRef Funclet, unsigned i, LLVMValueRef value) {3529unwrap<FuncletPadInst>(Funclet)->setArgOperand(i, unwrap(value));3530}35313532/*--.. Arithmetic ..........................................................--*/35333534static FastMathFlags mapFromLLVMFastMathFlags(LLVMFastMathFlags FMF) {3535FastMathFlags NewFMF;3536NewFMF.setAllowReassoc((FMF & LLVMFastMathAllowReassoc) != 0);3537NewFMF.setNoNaNs((FMF & LLVMFastMathNoNaNs) != 0);3538NewFMF.setNoInfs((FMF & LLVMFastMathNoInfs) != 0);3539NewFMF.setNoSignedZeros((FMF & LLVMFastMathNoSignedZeros) != 0);3540NewFMF.setAllowReciprocal((FMF & LLVMFastMathAllowReciprocal) != 0);3541NewFMF.setAllowContract((FMF & LLVMFastMathAllowContract) != 0);3542NewFMF.setApproxFunc((FMF & LLVMFastMathApproxFunc) != 0);35433544return NewFMF;3545}35463547static LLVMFastMathFlags mapToLLVMFastMathFlags(FastMathFlags FMF) {3548LLVMFastMathFlags NewFMF = LLVMFastMathNone;3549if (FMF.allowReassoc())3550NewFMF |= LLVMFastMathAllowReassoc;3551if (FMF.noNaNs())3552NewFMF |= LLVMFastMathNoNaNs;3553if (FMF.noInfs())3554NewFMF |= LLVMFastMathNoInfs;3555if (FMF.noSignedZeros())3556NewFMF |= LLVMFastMathNoSignedZeros;3557if (FMF.allowReciprocal())3558NewFMF |= LLVMFastMathAllowReciprocal;3559if (FMF.allowContract())3560NewFMF |= LLVMFastMathAllowContract;3561if (FMF.approxFunc())3562NewFMF |= LLVMFastMathApproxFunc;35633564return NewFMF;3565}35663567LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,3568const char *Name) {3569return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));3570}35713572LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,3573const char *Name) {3574return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));3575}35763577LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,3578const char *Name) {3579return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));3580}35813582LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,3583const char *Name) {3584return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));3585}35863587LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,3588const char *Name) {3589return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));3590}35913592LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,3593const char *Name) {3594return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));3595}35963597LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,3598const char *Name) {3599return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));3600}36013602LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,3603const char *Name) {3604return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));3605}36063607LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,3608const char *Name) {3609return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));3610}36113612LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,3613const char *Name) {3614return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));3615}36163617LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,3618const char *Name) {3619return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));3620}36213622LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,3623const char *Name) {3624return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));3625}36263627LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,3628const char *Name) {3629return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));3630}36313632LLVMValueRef LLVMBuildExactUDiv(LLVMBuilderRef B, LLVMValueRef LHS,3633LLVMValueRef RHS, const char *Name) {3634return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));3635}36363637LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,3638const char *Name) {3639return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));3640}36413642LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,3643LLVMValueRef RHS, const char *Name) {3644return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));3645}36463647LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,3648const char *Name) {3649return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));3650}36513652LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,3653const char *Name) {3654return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));3655}36563657LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,3658const char *Name) {3659return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));3660}36613662LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,3663const char *Name) {3664return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));3665}36663667LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,3668const char *Name) {3669return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));3670}36713672LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,3673const char *Name) {3674return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));3675}36763677LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,3678const char *Name) {3679return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));3680}36813682LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,3683const char *Name) {3684return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));3685}36863687LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,3688const char *Name) {3689return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));3690}36913692LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,3693const char *Name) {3694return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));3695}36963697LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,3698LLVMValueRef LHS, LLVMValueRef RHS,3699const char *Name) {3700return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),3701unwrap(RHS), Name));3702}37033704LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {3705return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));3706}37073708LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,3709const char *Name) {3710return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));3711}37123713LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,3714const char *Name) {3715Value *Neg = unwrap(B)->CreateNeg(unwrap(V), Name);3716if (auto *I = dyn_cast<BinaryOperator>(Neg))3717I->setHasNoUnsignedWrap();3718return wrap(Neg);3719}37203721LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {3722return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));3723}37243725LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {3726return wrap(unwrap(B)->CreateNot(unwrap(V), Name));3727}37283729LLVMBool LLVMGetNUW(LLVMValueRef ArithInst) {3730Value *P = unwrap<Value>(ArithInst);3731return cast<Instruction>(P)->hasNoUnsignedWrap();3732}37333734void LLVMSetNUW(LLVMValueRef ArithInst, LLVMBool HasNUW) {3735Value *P = unwrap<Value>(ArithInst);3736cast<Instruction>(P)->setHasNoUnsignedWrap(HasNUW);3737}37383739LLVMBool LLVMGetNSW(LLVMValueRef ArithInst) {3740Value *P = unwrap<Value>(ArithInst);3741return cast<Instruction>(P)->hasNoSignedWrap();3742}37433744void LLVMSetNSW(LLVMValueRef ArithInst, LLVMBool HasNSW) {3745Value *P = unwrap<Value>(ArithInst);3746cast<Instruction>(P)->setHasNoSignedWrap(HasNSW);3747}37483749LLVMBool LLVMGetExact(LLVMValueRef DivOrShrInst) {3750Value *P = unwrap<Value>(DivOrShrInst);3751return cast<Instruction>(P)->isExact();3752}37533754void LLVMSetExact(LLVMValueRef DivOrShrInst, LLVMBool IsExact) {3755Value *P = unwrap<Value>(DivOrShrInst);3756cast<Instruction>(P)->setIsExact(IsExact);3757}37583759LLVMBool LLVMGetNNeg(LLVMValueRef NonNegInst) {3760Value *P = unwrap<Value>(NonNegInst);3761return cast<Instruction>(P)->hasNonNeg();3762}37633764void LLVMSetNNeg(LLVMValueRef NonNegInst, LLVMBool IsNonNeg) {3765Value *P = unwrap<Value>(NonNegInst);3766cast<Instruction>(P)->setNonNeg(IsNonNeg);3767}37683769LLVMFastMathFlags LLVMGetFastMathFlags(LLVMValueRef FPMathInst) {3770Value *P = unwrap<Value>(FPMathInst);3771FastMathFlags FMF = cast<Instruction>(P)->getFastMathFlags();3772return mapToLLVMFastMathFlags(FMF);3773}37743775void LLVMSetFastMathFlags(LLVMValueRef FPMathInst, LLVMFastMathFlags FMF) {3776Value *P = unwrap<Value>(FPMathInst);3777cast<Instruction>(P)->setFastMathFlags(mapFromLLVMFastMathFlags(FMF));3778}37793780LLVMBool LLVMCanValueUseFastMathFlags(LLVMValueRef V) {3781Value *Val = unwrap<Value>(V);3782return isa<FPMathOperator>(Val);3783}37843785LLVMBool LLVMGetIsDisjoint(LLVMValueRef Inst) {3786Value *P = unwrap<Value>(Inst);3787return cast<PossiblyDisjointInst>(P)->isDisjoint();3788}37893790void LLVMSetIsDisjoint(LLVMValueRef Inst, LLVMBool IsDisjoint) {3791Value *P = unwrap<Value>(Inst);3792cast<PossiblyDisjointInst>(P)->setIsDisjoint(IsDisjoint);3793}37943795/*--.. Memory ..............................................................--*/37963797LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,3798const char *Name) {3799Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());3800Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));3801AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);3802return wrap(unwrap(B)->CreateMalloc(ITy, unwrap(Ty), AllocSize, nullptr,3803nullptr, Name));3804}38053806LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,3807LLVMValueRef Val, const char *Name) {3808Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());3809Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));3810AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);3811return wrap(unwrap(B)->CreateMalloc(ITy, unwrap(Ty), AllocSize, unwrap(Val),3812nullptr, Name));3813}38143815LLVMValueRef LLVMBuildMemSet(LLVMBuilderRef B, LLVMValueRef Ptr,3816LLVMValueRef Val, LLVMValueRef Len,3817unsigned Align) {3818return wrap(unwrap(B)->CreateMemSet(unwrap(Ptr), unwrap(Val), unwrap(Len),3819MaybeAlign(Align)));3820}38213822LLVMValueRef LLVMBuildMemCpy(LLVMBuilderRef B,3823LLVMValueRef Dst, unsigned DstAlign,3824LLVMValueRef Src, unsigned SrcAlign,3825LLVMValueRef Size) {3826return wrap(unwrap(B)->CreateMemCpy(unwrap(Dst), MaybeAlign(DstAlign),3827unwrap(Src), MaybeAlign(SrcAlign),3828unwrap(Size)));3829}38303831LLVMValueRef LLVMBuildMemMove(LLVMBuilderRef B,3832LLVMValueRef Dst, unsigned DstAlign,3833LLVMValueRef Src, unsigned SrcAlign,3834LLVMValueRef Size) {3835return wrap(unwrap(B)->CreateMemMove(unwrap(Dst), MaybeAlign(DstAlign),3836unwrap(Src), MaybeAlign(SrcAlign),3837unwrap(Size)));3838}38393840LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,3841const char *Name) {3842return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));3843}38443845LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,3846LLVMValueRef Val, const char *Name) {3847return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));3848}38493850LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {3851return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));3852}38533854LLVMValueRef LLVMBuildLoad2(LLVMBuilderRef B, LLVMTypeRef Ty,3855LLVMValueRef PointerVal, const char *Name) {3856return wrap(unwrap(B)->CreateLoad(unwrap(Ty), unwrap(PointerVal), Name));3857}38583859LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,3860LLVMValueRef PointerVal) {3861return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));3862}38633864static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {3865switch (Ordering) {3866case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic;3867case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered;3868case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic;3869case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire;3870case LLVMAtomicOrderingRelease: return AtomicOrdering::Release;3871case LLVMAtomicOrderingAcquireRelease:3872return AtomicOrdering::AcquireRelease;3873case LLVMAtomicOrderingSequentiallyConsistent:3874return AtomicOrdering::SequentiallyConsistent;3875}38763877llvm_unreachable("Invalid LLVMAtomicOrdering value!");3878}38793880static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) {3881switch (Ordering) {3882case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic;3883case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered;3884case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic;3885case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire;3886case AtomicOrdering::Release: return LLVMAtomicOrderingRelease;3887case AtomicOrdering::AcquireRelease:3888return LLVMAtomicOrderingAcquireRelease;3889case AtomicOrdering::SequentiallyConsistent:3890return LLVMAtomicOrderingSequentiallyConsistent;3891}38923893llvm_unreachable("Invalid AtomicOrdering value!");3894}38953896static AtomicRMWInst::BinOp mapFromLLVMRMWBinOp(LLVMAtomicRMWBinOp BinOp) {3897switch (BinOp) {3898case LLVMAtomicRMWBinOpXchg: return AtomicRMWInst::Xchg;3899case LLVMAtomicRMWBinOpAdd: return AtomicRMWInst::Add;3900case LLVMAtomicRMWBinOpSub: return AtomicRMWInst::Sub;3901case LLVMAtomicRMWBinOpAnd: return AtomicRMWInst::And;3902case LLVMAtomicRMWBinOpNand: return AtomicRMWInst::Nand;3903case LLVMAtomicRMWBinOpOr: return AtomicRMWInst::Or;3904case LLVMAtomicRMWBinOpXor: return AtomicRMWInst::Xor;3905case LLVMAtomicRMWBinOpMax: return AtomicRMWInst::Max;3906case LLVMAtomicRMWBinOpMin: return AtomicRMWInst::Min;3907case LLVMAtomicRMWBinOpUMax: return AtomicRMWInst::UMax;3908case LLVMAtomicRMWBinOpUMin: return AtomicRMWInst::UMin;3909case LLVMAtomicRMWBinOpFAdd: return AtomicRMWInst::FAdd;3910case LLVMAtomicRMWBinOpFSub: return AtomicRMWInst::FSub;3911case LLVMAtomicRMWBinOpFMax: return AtomicRMWInst::FMax;3912case LLVMAtomicRMWBinOpFMin: return AtomicRMWInst::FMin;3913case LLVMAtomicRMWBinOpUIncWrap:3914return AtomicRMWInst::UIncWrap;3915case LLVMAtomicRMWBinOpUDecWrap:3916return AtomicRMWInst::UDecWrap;3917}39183919llvm_unreachable("Invalid LLVMAtomicRMWBinOp value!");3920}39213922static LLVMAtomicRMWBinOp mapToLLVMRMWBinOp(AtomicRMWInst::BinOp BinOp) {3923switch (BinOp) {3924case AtomicRMWInst::Xchg: return LLVMAtomicRMWBinOpXchg;3925case AtomicRMWInst::Add: return LLVMAtomicRMWBinOpAdd;3926case AtomicRMWInst::Sub: return LLVMAtomicRMWBinOpSub;3927case AtomicRMWInst::And: return LLVMAtomicRMWBinOpAnd;3928case AtomicRMWInst::Nand: return LLVMAtomicRMWBinOpNand;3929case AtomicRMWInst::Or: return LLVMAtomicRMWBinOpOr;3930case AtomicRMWInst::Xor: return LLVMAtomicRMWBinOpXor;3931case AtomicRMWInst::Max: return LLVMAtomicRMWBinOpMax;3932case AtomicRMWInst::Min: return LLVMAtomicRMWBinOpMin;3933case AtomicRMWInst::UMax: return LLVMAtomicRMWBinOpUMax;3934case AtomicRMWInst::UMin: return LLVMAtomicRMWBinOpUMin;3935case AtomicRMWInst::FAdd: return LLVMAtomicRMWBinOpFAdd;3936case AtomicRMWInst::FSub: return LLVMAtomicRMWBinOpFSub;3937case AtomicRMWInst::FMax: return LLVMAtomicRMWBinOpFMax;3938case AtomicRMWInst::FMin: return LLVMAtomicRMWBinOpFMin;3939case AtomicRMWInst::UIncWrap:3940return LLVMAtomicRMWBinOpUIncWrap;3941case AtomicRMWInst::UDecWrap:3942return LLVMAtomicRMWBinOpUDecWrap;3943default: break;3944}39453946llvm_unreachable("Invalid AtomicRMWBinOp value!");3947}39483949// TODO: Should this and other atomic instructions support building with3950// "syncscope"?3951LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,3952LLVMBool isSingleThread, const char *Name) {3953return wrap(3954unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),3955isSingleThread ? SyncScope::SingleThread3956: SyncScope::System,3957Name));3958}39593960LLVMValueRef LLVMBuildGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,3961LLVMValueRef Pointer, LLVMValueRef *Indices,3962unsigned NumIndices, const char *Name) {3963ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);3964return wrap(unwrap(B)->CreateGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));3965}39663967LLVMValueRef LLVMBuildInBoundsGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,3968LLVMValueRef Pointer, LLVMValueRef *Indices,3969unsigned NumIndices, const char *Name) {3970ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);3971return wrap(3972unwrap(B)->CreateInBoundsGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));3973}39743975LLVMValueRef LLVMBuildGEPWithNoWrapFlags(LLVMBuilderRef B, LLVMTypeRef Ty,3976LLVMValueRef Pointer,3977LLVMValueRef *Indices,3978unsigned NumIndices, const char *Name,3979LLVMGEPNoWrapFlags NoWrapFlags) {3980ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);3981return wrap(unwrap(B)->CreateGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name,3982mapFromLLVMGEPNoWrapFlags(NoWrapFlags)));3983}39843985LLVMValueRef LLVMBuildStructGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,3986LLVMValueRef Pointer, unsigned Idx,3987const char *Name) {3988return wrap(3989unwrap(B)->CreateStructGEP(unwrap(Ty), unwrap(Pointer), Idx, Name));3990}39913992LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,3993const char *Name) {3994return wrap(unwrap(B)->CreateGlobalString(Str, Name));3995}39963997LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,3998const char *Name) {3999return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));4000}40014002LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {4003Value *P = unwrap(MemAccessInst);4004if (LoadInst *LI = dyn_cast<LoadInst>(P))4005return LI->isVolatile();4006if (StoreInst *SI = dyn_cast<StoreInst>(P))4007return SI->isVolatile();4008if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))4009return AI->isVolatile();4010return cast<AtomicCmpXchgInst>(P)->isVolatile();4011}40124013void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {4014Value *P = unwrap(MemAccessInst);4015if (LoadInst *LI = dyn_cast<LoadInst>(P))4016return LI->setVolatile(isVolatile);4017if (StoreInst *SI = dyn_cast<StoreInst>(P))4018return SI->setVolatile(isVolatile);4019if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))4020return AI->setVolatile(isVolatile);4021return cast<AtomicCmpXchgInst>(P)->setVolatile(isVolatile);4022}40234024LLVMBool LLVMGetWeak(LLVMValueRef CmpXchgInst) {4025return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->isWeak();4026}40274028void LLVMSetWeak(LLVMValueRef CmpXchgInst, LLVMBool isWeak) {4029return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->setWeak(isWeak);4030}40314032LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) {4033Value *P = unwrap(MemAccessInst);4034AtomicOrdering O;4035if (LoadInst *LI = dyn_cast<LoadInst>(P))4036O = LI->getOrdering();4037else if (StoreInst *SI = dyn_cast<StoreInst>(P))4038O = SI->getOrdering();4039else if (FenceInst *FI = dyn_cast<FenceInst>(P))4040O = FI->getOrdering();4041else4042O = cast<AtomicRMWInst>(P)->getOrdering();4043return mapToLLVMOrdering(O);4044}40454046void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {4047Value *P = unwrap(MemAccessInst);4048AtomicOrdering O = mapFromLLVMOrdering(Ordering);40494050if (LoadInst *LI = dyn_cast<LoadInst>(P))4051return LI->setOrdering(O);4052else if (FenceInst *FI = dyn_cast<FenceInst>(P))4053return FI->setOrdering(O);4054else if (AtomicRMWInst *ARWI = dyn_cast<AtomicRMWInst>(P))4055return ARWI->setOrdering(O);4056return cast<StoreInst>(P)->setOrdering(O);4057}40584059LLVMAtomicRMWBinOp LLVMGetAtomicRMWBinOp(LLVMValueRef Inst) {4060return mapToLLVMRMWBinOp(unwrap<AtomicRMWInst>(Inst)->getOperation());4061}40624063void LLVMSetAtomicRMWBinOp(LLVMValueRef Inst, LLVMAtomicRMWBinOp BinOp) {4064unwrap<AtomicRMWInst>(Inst)->setOperation(mapFromLLVMRMWBinOp(BinOp));4065}40664067/*--.. Casts ...............................................................--*/40684069LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,4070LLVMTypeRef DestTy, const char *Name) {4071return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));4072}40734074LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,4075LLVMTypeRef DestTy, const char *Name) {4076return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));4077}40784079LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,4080LLVMTypeRef DestTy, const char *Name) {4081return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));4082}40834084LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,4085LLVMTypeRef DestTy, const char *Name) {4086return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));4087}40884089LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,4090LLVMTypeRef DestTy, const char *Name) {4091return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));4092}40934094LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,4095LLVMTypeRef DestTy, const char *Name) {4096return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));4097}40984099LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,4100LLVMTypeRef DestTy, const char *Name) {4101return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));4102}41034104LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,4105LLVMTypeRef DestTy, const char *Name) {4106return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));4107}41084109LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,4110LLVMTypeRef DestTy, const char *Name) {4111return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));4112}41134114LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,4115LLVMTypeRef DestTy, const char *Name) {4116return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));4117}41184119LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,4120LLVMTypeRef DestTy, const char *Name) {4121return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));4122}41234124LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,4125LLVMTypeRef DestTy, const char *Name) {4126return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));4127}41284129LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,4130LLVMTypeRef DestTy, const char *Name) {4131return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));4132}41334134LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,4135LLVMTypeRef DestTy, const char *Name) {4136return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),4137Name));4138}41394140LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,4141LLVMTypeRef DestTy, const char *Name) {4142return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),4143Name));4144}41454146LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,4147LLVMTypeRef DestTy, const char *Name) {4148return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),4149Name));4150}41514152LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,4153LLVMTypeRef DestTy, const char *Name) {4154return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),4155unwrap(DestTy), Name));4156}41574158LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,4159LLVMTypeRef DestTy, const char *Name) {4160return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));4161}41624163LLVMValueRef LLVMBuildIntCast2(LLVMBuilderRef B, LLVMValueRef Val,4164LLVMTypeRef DestTy, LLVMBool IsSigned,4165const char *Name) {4166return wrap(4167unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy), IsSigned, Name));4168}41694170LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,4171LLVMTypeRef DestTy, const char *Name) {4172return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),4173/*isSigned*/true, Name));4174}41754176LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,4177LLVMTypeRef DestTy, const char *Name) {4178return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));4179}41804181LLVMOpcode LLVMGetCastOpcode(LLVMValueRef Src, LLVMBool SrcIsSigned,4182LLVMTypeRef DestTy, LLVMBool DestIsSigned) {4183return map_to_llvmopcode(CastInst::getCastOpcode(4184unwrap(Src), SrcIsSigned, unwrap(DestTy), DestIsSigned));4185}41864187/*--.. Comparisons .........................................................--*/41884189LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,4190LLVMValueRef LHS, LLVMValueRef RHS,4191const char *Name) {4192return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),4193unwrap(LHS), unwrap(RHS), Name));4194}41954196LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,4197LLVMValueRef LHS, LLVMValueRef RHS,4198const char *Name) {4199return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),4200unwrap(LHS), unwrap(RHS), Name));4201}42024203/*--.. Miscellaneous instructions ..........................................--*/42044205LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {4206return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));4207}42084209LLVMValueRef LLVMBuildCall2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn,4210LLVMValueRef *Args, unsigned NumArgs,4211const char *Name) {4212FunctionType *FTy = unwrap<FunctionType>(Ty);4213return wrap(unwrap(B)->CreateCall(FTy, unwrap(Fn),4214ArrayRef(unwrap(Args), NumArgs), Name));4215}42164217LLVMValueRef4218LLVMBuildCallWithOperandBundles(LLVMBuilderRef B, LLVMTypeRef Ty,4219LLVMValueRef Fn, LLVMValueRef *Args,4220unsigned NumArgs, LLVMOperandBundleRef *Bundles,4221unsigned NumBundles, const char *Name) {4222FunctionType *FTy = unwrap<FunctionType>(Ty);4223SmallVector<OperandBundleDef, 8> OBs;4224for (auto *Bundle : ArrayRef(Bundles, NumBundles)) {4225OperandBundleDef *OB = unwrap(Bundle);4226OBs.push_back(*OB);4227}4228return wrap(unwrap(B)->CreateCall(4229FTy, unwrap(Fn), ArrayRef(unwrap(Args), NumArgs), OBs, Name));4230}42314232LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,4233LLVMValueRef Then, LLVMValueRef Else,4234const char *Name) {4235return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),4236Name));4237}42384239LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,4240LLVMTypeRef Ty, const char *Name) {4241return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));4242}42434244LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,4245LLVMValueRef Index, const char *Name) {4246return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),4247Name));4248}42494250LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,4251LLVMValueRef EltVal, LLVMValueRef Index,4252const char *Name) {4253return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),4254unwrap(Index), Name));4255}42564257LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,4258LLVMValueRef V2, LLVMValueRef Mask,4259const char *Name) {4260return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),4261unwrap(Mask), Name));4262}42634264LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,4265unsigned Index, const char *Name) {4266return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));4267}42684269LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,4270LLVMValueRef EltVal, unsigned Index,4271const char *Name) {4272return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),4273Index, Name));4274}42754276LLVMValueRef LLVMBuildFreeze(LLVMBuilderRef B, LLVMValueRef Val,4277const char *Name) {4278return wrap(unwrap(B)->CreateFreeze(unwrap(Val), Name));4279}42804281LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,4282const char *Name) {4283return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));4284}42854286LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,4287const char *Name) {4288return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));4289}42904291LLVMValueRef LLVMBuildPtrDiff2(LLVMBuilderRef B, LLVMTypeRef ElemTy,4292LLVMValueRef LHS, LLVMValueRef RHS,4293const char *Name) {4294return wrap(unwrap(B)->CreatePtrDiff(unwrap(ElemTy), unwrap(LHS),4295unwrap(RHS), Name));4296}42974298LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,4299LLVMValueRef PTR, LLVMValueRef Val,4300LLVMAtomicOrdering ordering,4301LLVMBool singleThread) {4302AtomicRMWInst::BinOp intop = mapFromLLVMRMWBinOp(op);4303return wrap(unwrap(B)->CreateAtomicRMW(4304intop, unwrap(PTR), unwrap(Val), MaybeAlign(),4305mapFromLLVMOrdering(ordering),4306singleThread ? SyncScope::SingleThread : SyncScope::System));4307}43084309LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr,4310LLVMValueRef Cmp, LLVMValueRef New,4311LLVMAtomicOrdering SuccessOrdering,4312LLVMAtomicOrdering FailureOrdering,4313LLVMBool singleThread) {43144315return wrap(unwrap(B)->CreateAtomicCmpXchg(4316unwrap(Ptr), unwrap(Cmp), unwrap(New), MaybeAlign(),4317mapFromLLVMOrdering(SuccessOrdering),4318mapFromLLVMOrdering(FailureOrdering),4319singleThread ? SyncScope::SingleThread : SyncScope::System));4320}43214322unsigned LLVMGetNumMaskElements(LLVMValueRef SVInst) {4323Value *P = unwrap(SVInst);4324ShuffleVectorInst *I = cast<ShuffleVectorInst>(P);4325return I->getShuffleMask().size();4326}43274328int LLVMGetMaskValue(LLVMValueRef SVInst, unsigned Elt) {4329Value *P = unwrap(SVInst);4330ShuffleVectorInst *I = cast<ShuffleVectorInst>(P);4331return I->getMaskValue(Elt);4332}43334334int LLVMGetUndefMaskElem(void) { return PoisonMaskElem; }43354336LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) {4337Value *P = unwrap(AtomicInst);43384339if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))4340return I->getSyncScopeID() == SyncScope::SingleThread;4341else if (FenceInst *FI = dyn_cast<FenceInst>(P))4342return FI->getSyncScopeID() == SyncScope::SingleThread;4343else if (StoreInst *SI = dyn_cast<StoreInst>(P))4344return SI->getSyncScopeID() == SyncScope::SingleThread;4345else if (LoadInst *LI = dyn_cast<LoadInst>(P))4346return LI->getSyncScopeID() == SyncScope::SingleThread;4347return cast<AtomicCmpXchgInst>(P)->getSyncScopeID() ==4348SyncScope::SingleThread;4349}43504351void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) {4352Value *P = unwrap(AtomicInst);4353SyncScope::ID SSID = NewValue ? SyncScope::SingleThread : SyncScope::System;43544355if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))4356return I->setSyncScopeID(SSID);4357else if (FenceInst *FI = dyn_cast<FenceInst>(P))4358return FI->setSyncScopeID(SSID);4359else if (StoreInst *SI = dyn_cast<StoreInst>(P))4360return SI->setSyncScopeID(SSID);4361else if (LoadInst *LI = dyn_cast<LoadInst>(P))4362return LI->setSyncScopeID(SSID);4363return cast<AtomicCmpXchgInst>(P)->setSyncScopeID(SSID);4364}43654366LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) {4367Value *P = unwrap(CmpXchgInst);4368return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering());4369}43704371void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst,4372LLVMAtomicOrdering Ordering) {4373Value *P = unwrap(CmpXchgInst);4374AtomicOrdering O = mapFromLLVMOrdering(Ordering);43754376return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O);4377}43784379LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst) {4380Value *P = unwrap(CmpXchgInst);4381return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering());4382}43834384void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst,4385LLVMAtomicOrdering Ordering) {4386Value *P = unwrap(CmpXchgInst);4387AtomicOrdering O = mapFromLLVMOrdering(Ordering);43884389return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O);4390}43914392/*===-- Module providers --------------------------------------------------===*/43934394LLVMModuleProviderRef4395LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {4396return reinterpret_cast<LLVMModuleProviderRef>(M);4397}43984399void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {4400delete unwrap(MP);4401}440244034404/*===-- Memory buffers ----------------------------------------------------===*/44054406LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(4407const char *Path,4408LLVMMemoryBufferRef *OutMemBuf,4409char **OutMessage) {44104411ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);4412if (std::error_code EC = MBOrErr.getError()) {4413*OutMessage = strdup(EC.message().c_str());4414return 1;4415}4416*OutMemBuf = wrap(MBOrErr.get().release());4417return 0;4418}44194420LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,4421char **OutMessage) {4422ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();4423if (std::error_code EC = MBOrErr.getError()) {4424*OutMessage = strdup(EC.message().c_str());4425return 1;4426}4427*OutMemBuf = wrap(MBOrErr.get().release());4428return 0;4429}44304431LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(4432const char *InputData,4433size_t InputDataLength,4434const char *BufferName,4435LLVMBool RequiresNullTerminator) {44364437return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),4438StringRef(BufferName),4439RequiresNullTerminator).release());4440}44414442LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(4443const char *InputData,4444size_t InputDataLength,4445const char *BufferName) {44464447return wrap(4448MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),4449StringRef(BufferName)).release());4450}44514452const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {4453return unwrap(MemBuf)->getBufferStart();4454}44554456size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {4457return unwrap(MemBuf)->getBufferSize();4458}44594460void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {4461delete unwrap(MemBuf);4462}44634464/*===-- Pass Manager ------------------------------------------------------===*/44654466LLVMPassManagerRef LLVMCreatePassManager() {4467return wrap(new legacy::PassManager());4468}44694470LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {4471return wrap(new legacy::FunctionPassManager(unwrap(M)));4472}44734474LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {4475return LLVMCreateFunctionPassManagerForModule(4476reinterpret_cast<LLVMModuleRef>(P));4477}44784479LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {4480return unwrap<legacy::PassManager>(PM)->run(*unwrap(M));4481}44824483LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {4484return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization();4485}44864487LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {4488return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F));4489}44904491LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {4492return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization();4493}44944495void LLVMDisposePassManager(LLVMPassManagerRef PM) {4496delete unwrap(PM);4497}44984499/*===-- Threading ------------------------------------------------------===*/45004501LLVMBool LLVMStartMultithreaded() {4502return LLVMIsMultithreaded();4503}45044505void LLVMStopMultithreaded() {4506}45074508LLVMBool LLVMIsMultithreaded() {4509return llvm_is_multithreaded();4510}451145124513