LLVM API Documentation

Core.cpp
Go to the documentation of this file.
00001 //===-- Core.cpp ----------------------------------------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the common infrastructure (including the C bindings)
00011 // for libLLVMCore.a, which implements the LLVM intermediate representation.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm-c/Core.h"
00016 #include "llvm/Bitcode/ReaderWriter.h"
00017 #include "llvm/IR/Attributes.h"
00018 #include "llvm/IR/CallSite.h"
00019 #include "llvm/IR/Constants.h"
00020 #include "llvm/IR/DerivedTypes.h"
00021 #include "llvm/IR/DiagnosticInfo.h"
00022 #include "llvm/IR/DiagnosticPrinter.h"
00023 #include "llvm/IR/GlobalAlias.h"
00024 #include "llvm/IR/GlobalVariable.h"
00025 #include "llvm/IR/IRBuilder.h"
00026 #include "llvm/IR/InlineAsm.h"
00027 #include "llvm/IR/IntrinsicInst.h"
00028 #include "llvm/IR/LLVMContext.h"
00029 #include "llvm/IR/Module.h"
00030 #include "llvm/PassManager.h"
00031 #include "llvm/Support/Debug.h"
00032 #include "llvm/Support/ErrorHandling.h"
00033 #include "llvm/Support/FileSystem.h"
00034 #include "llvm/Support/ManagedStatic.h"
00035 #include "llvm/Support/MemoryBuffer.h"
00036 #include "llvm/Support/Threading.h"
00037 #include "llvm/Support/raw_ostream.h"
00038 #include <cassert>
00039 #include <cstdlib>
00040 #include <cstring>
00041 #include <system_error>
00042 
00043 using namespace llvm;
00044 
00045 #define DEBUG_TYPE "ir"
00046 
00047 void llvm::initializeCore(PassRegistry &Registry) {
00048   initializeDominatorTreeWrapperPassPass(Registry);
00049   initializePrintModulePassWrapperPass(Registry);
00050   initializePrintFunctionPassWrapperPass(Registry);
00051   initializePrintBasicBlockPassPass(Registry);
00052   initializeVerifierLegacyPassPass(Registry);
00053 }
00054 
00055 void LLVMInitializeCore(LLVMPassRegistryRef R) {
00056   initializeCore(*unwrap(R));
00057 }
00058 
00059 void LLVMShutdown() {
00060   llvm_shutdown();
00061 }
00062 
00063 /*===-- Error handling ----------------------------------------------------===*/
00064 
00065 char *LLVMCreateMessage(const char *Message) {
00066   return strdup(Message);
00067 }
00068 
00069 void LLVMDisposeMessage(char *Message) {
00070   free(Message);
00071 }
00072 
00073 
00074 /*===-- Operations on contexts --------------------------------------------===*/
00075 
00076 LLVMContextRef LLVMContextCreate() {
00077   return wrap(new LLVMContext());
00078 }
00079 
00080 LLVMContextRef LLVMGetGlobalContext() {
00081   return wrap(&getGlobalContext());
00082 }
00083 
00084 void LLVMContextSetDiagnosticHandler(LLVMContextRef C,
00085                                      LLVMDiagnosticHandler Handler,
00086                                      void *DiagnosticContext) {
00087   unwrap(C)->setDiagnosticHandler(
00088       LLVM_EXTENSION reinterpret_cast<LLVMContext::DiagnosticHandlerTy>(Handler),
00089       DiagnosticContext);
00090 }
00091 
00092 void LLVMContextSetYieldCallback(LLVMContextRef C, LLVMYieldCallback Callback,
00093                                  void *OpaqueHandle) {
00094   auto YieldCallback =
00095     LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
00096   unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
00097 }
00098 
00099 void LLVMContextDispose(LLVMContextRef C) {
00100   delete unwrap(C);
00101 }
00102 
00103 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name,
00104                                   unsigned SLen) {
00105   return unwrap(C)->getMDKindID(StringRef(Name, SLen));
00106 }
00107 
00108 unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) {
00109   return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
00110 }
00111 
00112 char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) {
00113   std::string MsgStorage;
00114   raw_string_ostream Stream(MsgStorage);
00115   DiagnosticPrinterRawOStream DP(Stream);
00116 
00117   unwrap(DI)->print(DP);
00118   Stream.flush();
00119 
00120   return LLVMCreateMessage(MsgStorage.c_str());
00121 }
00122 
00123 LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI){
00124     LLVMDiagnosticSeverity severity;
00125 
00126     switch(unwrap(DI)->getSeverity()) {
00127     default:
00128       severity = LLVMDSError;
00129       break;
00130     case DS_Warning:
00131       severity = LLVMDSWarning;
00132       break;
00133     case DS_Remark:
00134       severity = LLVMDSRemark;
00135       break;
00136     case DS_Note:
00137       severity = LLVMDSNote;
00138       break;
00139     }
00140 
00141     return severity;
00142 }
00143 
00144 
00145 
00146 
00147 /*===-- Operations on modules ---------------------------------------------===*/
00148 
00149 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
00150   return wrap(new Module(ModuleID, getGlobalContext()));
00151 }
00152 
00153 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
00154                                                 LLVMContextRef C) {
00155   return wrap(new Module(ModuleID, *unwrap(C)));
00156 }
00157 
00158 void LLVMDisposeModule(LLVMModuleRef M) {
00159   delete unwrap(M);
00160 }
00161 
00162 /*--.. Data layout .........................................................--*/
00163 const char * LLVMGetDataLayout(LLVMModuleRef M) {
00164   return unwrap(M)->getDataLayoutStr().c_str();
00165 }
00166 
00167 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
00168   unwrap(M)->setDataLayout(Triple);
00169 }
00170 
00171 /*--.. Target triple .......................................................--*/
00172 const char * LLVMGetTarget(LLVMModuleRef M) {
00173   return unwrap(M)->getTargetTriple().c_str();
00174 }
00175 
00176 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
00177   unwrap(M)->setTargetTriple(Triple);
00178 }
00179 
00180 void LLVMDumpModule(LLVMModuleRef M) {
00181   unwrap(M)->dump();
00182 }
00183 
00184 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
00185                                char **ErrorMessage) {
00186   std::error_code EC;
00187   raw_fd_ostream dest(Filename, EC, sys::fs::F_Text);
00188   if (EC) {
00189     *ErrorMessage = strdup(EC.message().c_str());
00190     return true;
00191   }
00192 
00193   unwrap(M)->print(dest, nullptr);
00194 
00195   dest.close();
00196 
00197   if (dest.has_error()) {
00198     *ErrorMessage = strdup("Error printing to file");
00199     return true;
00200   }
00201 
00202   return false;
00203 }
00204 
00205 char *LLVMPrintModuleToString(LLVMModuleRef M) {
00206   std::string buf;
00207   raw_string_ostream os(buf);
00208 
00209   unwrap(M)->print(os, nullptr);
00210   os.flush();
00211 
00212   return strdup(buf.c_str());
00213 }
00214 
00215 /*--.. Operations on inline assembler ......................................--*/
00216 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
00217   unwrap(M)->setModuleInlineAsm(StringRef(Asm));
00218 }
00219 
00220 
00221 /*--.. Operations on module contexts ......................................--*/
00222 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
00223   return wrap(&unwrap(M)->getContext());
00224 }
00225 
00226 
00227 /*===-- Operations on types -----------------------------------------------===*/
00228 
00229 /*--.. Operations on all types (mostly) ....................................--*/
00230 
00231 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
00232   switch (unwrap(Ty)->getTypeID()) {
00233   case Type::VoidTyID:
00234     return LLVMVoidTypeKind;
00235   case Type::HalfTyID:
00236     return LLVMHalfTypeKind;
00237   case Type::FloatTyID:
00238     return LLVMFloatTypeKind;
00239   case Type::DoubleTyID:
00240     return LLVMDoubleTypeKind;
00241   case Type::X86_FP80TyID:
00242     return LLVMX86_FP80TypeKind;
00243   case Type::FP128TyID:
00244     return LLVMFP128TypeKind;
00245   case Type::PPC_FP128TyID:
00246     return LLVMPPC_FP128TypeKind;
00247   case Type::LabelTyID:
00248     return LLVMLabelTypeKind;
00249   case Type::MetadataTyID:
00250     return LLVMMetadataTypeKind;
00251   case Type::IntegerTyID:
00252     return LLVMIntegerTypeKind;
00253   case Type::FunctionTyID:
00254     return LLVMFunctionTypeKind;
00255   case Type::StructTyID:
00256     return LLVMStructTypeKind;
00257   case Type::ArrayTyID:
00258     return LLVMArrayTypeKind;
00259   case Type::PointerTyID:
00260     return LLVMPointerTypeKind;
00261   case Type::VectorTyID:
00262     return LLVMVectorTypeKind;
00263   case Type::X86_MMXTyID:
00264     return LLVMX86_MMXTypeKind;
00265   }
00266   llvm_unreachable("Unhandled TypeID.");
00267 }
00268 
00269 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
00270 {
00271     return unwrap(Ty)->isSized();
00272 }
00273 
00274 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
00275   return wrap(&unwrap(Ty)->getContext());
00276 }
00277 
00278 void LLVMDumpType(LLVMTypeRef Ty) {
00279   return unwrap(Ty)->dump();
00280 }
00281 
00282 char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
00283   std::string buf;
00284   raw_string_ostream os(buf);
00285 
00286   if (unwrap(Ty))
00287     unwrap(Ty)->print(os);
00288   else
00289     os << "Printing <null> Type";
00290 
00291   os.flush();
00292 
00293   return strdup(buf.c_str());
00294 }
00295 
00296 /*--.. Operations on integer types .........................................--*/
00297 
00298 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C)  {
00299   return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
00300 }
00301 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C)  {
00302   return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
00303 }
00304 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
00305   return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
00306 }
00307 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
00308   return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
00309 }
00310 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
00311   return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
00312 }
00313 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
00314   return wrap(IntegerType::get(*unwrap(C), NumBits));
00315 }
00316 
00317 LLVMTypeRef LLVMInt1Type(void)  {
00318   return LLVMInt1TypeInContext(LLVMGetGlobalContext());
00319 }
00320 LLVMTypeRef LLVMInt8Type(void)  {
00321   return LLVMInt8TypeInContext(LLVMGetGlobalContext());
00322 }
00323 LLVMTypeRef LLVMInt16Type(void) {
00324   return LLVMInt16TypeInContext(LLVMGetGlobalContext());
00325 }
00326 LLVMTypeRef LLVMInt32Type(void) {
00327   return LLVMInt32TypeInContext(LLVMGetGlobalContext());
00328 }
00329 LLVMTypeRef LLVMInt64Type(void) {
00330   return LLVMInt64TypeInContext(LLVMGetGlobalContext());
00331 }
00332 LLVMTypeRef LLVMIntType(unsigned NumBits) {
00333   return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
00334 }
00335 
00336 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
00337   return unwrap<IntegerType>(IntegerTy)->getBitWidth();
00338 }
00339 
00340 /*--.. Operations on real types ............................................--*/
00341 
00342 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
00343   return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
00344 }
00345 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
00346   return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
00347 }
00348 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
00349   return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
00350 }
00351 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
00352   return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
00353 }
00354 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
00355   return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
00356 }
00357 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
00358   return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
00359 }
00360 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
00361   return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
00362 }
00363 
00364 LLVMTypeRef LLVMHalfType(void) {
00365   return LLVMHalfTypeInContext(LLVMGetGlobalContext());
00366 }
00367 LLVMTypeRef LLVMFloatType(void) {
00368   return LLVMFloatTypeInContext(LLVMGetGlobalContext());
00369 }
00370 LLVMTypeRef LLVMDoubleType(void) {
00371   return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
00372 }
00373 LLVMTypeRef LLVMX86FP80Type(void) {
00374   return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
00375 }
00376 LLVMTypeRef LLVMFP128Type(void) {
00377   return LLVMFP128TypeInContext(LLVMGetGlobalContext());
00378 }
00379 LLVMTypeRef LLVMPPCFP128Type(void) {
00380   return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
00381 }
00382 LLVMTypeRef LLVMX86MMXType(void) {
00383   return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
00384 }
00385 
00386 /*--.. Operations on function types ........................................--*/
00387 
00388 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
00389                              LLVMTypeRef *ParamTypes, unsigned ParamCount,
00390                              LLVMBool IsVarArg) {
00391   ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
00392   return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
00393 }
00394 
00395 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
00396   return unwrap<FunctionType>(FunctionTy)->isVarArg();
00397 }
00398 
00399 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
00400   return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
00401 }
00402 
00403 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
00404   return unwrap<FunctionType>(FunctionTy)->getNumParams();
00405 }
00406 
00407 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
00408   FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
00409   for (FunctionType::param_iterator I = Ty->param_begin(),
00410                                     E = Ty->param_end(); I != E; ++I)
00411     *Dest++ = wrap(*I);
00412 }
00413 
00414 /*--.. Operations on struct types ..........................................--*/
00415 
00416 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
00417                            unsigned ElementCount, LLVMBool Packed) {
00418   ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
00419   return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
00420 }
00421 
00422 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
00423                            unsigned ElementCount, LLVMBool Packed) {
00424   return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
00425                                  ElementCount, Packed);
00426 }
00427 
00428 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
00429 {
00430   return wrap(StructType::create(*unwrap(C), Name));
00431 }
00432 
00433 const char *LLVMGetStructName(LLVMTypeRef Ty)
00434 {
00435   StructType *Type = unwrap<StructType>(Ty);
00436   if (!Type->hasName())
00437     return nullptr;
00438   return Type->getName().data();
00439 }
00440 
00441 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
00442                        unsigned ElementCount, LLVMBool Packed) {
00443   ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
00444   unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
00445 }
00446 
00447 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
00448   return unwrap<StructType>(StructTy)->getNumElements();
00449 }
00450 
00451 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
00452   StructType *Ty = unwrap<StructType>(StructTy);
00453   for (StructType::element_iterator I = Ty->element_begin(),
00454                                     E = Ty->element_end(); I != E; ++I)
00455     *Dest++ = wrap(*I);
00456 }
00457 
00458 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
00459   return unwrap<StructType>(StructTy)->isPacked();
00460 }
00461 
00462 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
00463   return unwrap<StructType>(StructTy)->isOpaque();
00464 }
00465 
00466 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
00467   return wrap(unwrap(M)->getTypeByName(Name));
00468 }
00469 
00470 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
00471 
00472 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
00473   return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
00474 }
00475 
00476 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
00477   return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
00478 }
00479 
00480 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
00481   return wrap(VectorType::get(unwrap(ElementType), ElementCount));
00482 }
00483 
00484 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
00485   return wrap(unwrap<SequentialType>(Ty)->getElementType());
00486 }
00487 
00488 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
00489   return unwrap<ArrayType>(ArrayTy)->getNumElements();
00490 }
00491 
00492 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
00493   return unwrap<PointerType>(PointerTy)->getAddressSpace();
00494 }
00495 
00496 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
00497   return unwrap<VectorType>(VectorTy)->getNumElements();
00498 }
00499 
00500 /*--.. Operations on other types ...........................................--*/
00501 
00502 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C)  {
00503   return wrap(Type::getVoidTy(*unwrap(C)));
00504 }
00505 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
00506   return wrap(Type::getLabelTy(*unwrap(C)));
00507 }
00508 
00509 LLVMTypeRef LLVMVoidType(void)  {
00510   return LLVMVoidTypeInContext(LLVMGetGlobalContext());
00511 }
00512 LLVMTypeRef LLVMLabelType(void) {
00513   return LLVMLabelTypeInContext(LLVMGetGlobalContext());
00514 }
00515 
00516 /*===-- Operations on values ----------------------------------------------===*/
00517 
00518 /*--.. Operations on all values ............................................--*/
00519 
00520 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
00521   return wrap(unwrap(Val)->getType());
00522 }
00523 
00524 const char *LLVMGetValueName(LLVMValueRef Val) {
00525   return unwrap(Val)->getName().data();
00526 }
00527 
00528 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
00529   unwrap(Val)->setName(Name);
00530 }
00531 
00532 void LLVMDumpValue(LLVMValueRef Val) {
00533   unwrap(Val)->dump();
00534 }
00535 
00536 char* LLVMPrintValueToString(LLVMValueRef Val) {
00537   std::string buf;
00538   raw_string_ostream os(buf);
00539 
00540   if (unwrap(Val))
00541     unwrap(Val)->print(os);
00542   else
00543     os << "Printing <null> Value";
00544 
00545   os.flush();
00546 
00547   return strdup(buf.c_str());
00548 }
00549 
00550 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
00551   unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
00552 }
00553 
00554 int LLVMHasMetadata(LLVMValueRef Inst) {
00555   return unwrap<Instruction>(Inst)->hasMetadata();
00556 }
00557 
00558 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
00559   return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
00560 }
00561 
00562 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
00563   unwrap<Instruction>(Inst)->setMetadata(KindID,
00564                                          MD ? unwrap<MDNode>(MD) : nullptr);
00565 }
00566 
00567 /*--.. Conversion functions ................................................--*/
00568 
00569 #define LLVM_DEFINE_VALUE_CAST(name)                                       \
00570   LLVMValueRef LLVMIsA##name(LLVMValueRef Val) {                           \
00571     return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
00572   }
00573 
00574 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
00575 
00576 /*--.. Operations on Uses ..................................................--*/
00577 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
00578   Value *V = unwrap(Val);
00579   Value::use_iterator I = V->use_begin();
00580   if (I == V->use_end())
00581     return nullptr;
00582   return wrap(&*I);
00583 }
00584 
00585 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
00586   Use *Next = unwrap(U)->getNext();
00587   if (Next)
00588     return wrap(Next);
00589   return nullptr;
00590 }
00591 
00592 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
00593   return wrap(unwrap(U)->getUser());
00594 }
00595 
00596 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
00597   return wrap(unwrap(U)->get());
00598 }
00599 
00600 /*--.. Operations on Users .................................................--*/
00601 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
00602   Value *V = unwrap(Val);
00603   if (MDNode *MD = dyn_cast<MDNode>(V))
00604       return wrap(MD->getOperand(Index));
00605   return wrap(cast<User>(V)->getOperand(Index));
00606 }
00607 
00608 LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) {
00609   Value *V = unwrap(Val);
00610   return wrap(&cast<User>(V)->getOperandUse(Index));
00611 }
00612 
00613 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
00614   unwrap<User>(Val)->setOperand(Index, unwrap(Op));
00615 }
00616 
00617 int LLVMGetNumOperands(LLVMValueRef Val) {
00618   Value *V = unwrap(Val);
00619   if (MDNode *MD = dyn_cast<MDNode>(V))
00620       return MD->getNumOperands();
00621   return cast<User>(V)->getNumOperands();
00622 }
00623 
00624 /*--.. Operations on constants of any type .................................--*/
00625 
00626 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
00627   return wrap(Constant::getNullValue(unwrap(Ty)));
00628 }
00629 
00630 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
00631   return wrap(Constant::getAllOnesValue(unwrap(Ty)));
00632 }
00633 
00634 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
00635   return wrap(UndefValue::get(unwrap(Ty)));
00636 }
00637 
00638 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
00639   return isa<Constant>(unwrap(Ty));
00640 }
00641 
00642 LLVMBool LLVMIsNull(LLVMValueRef Val) {
00643   if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
00644     return C->isNullValue();
00645   return false;
00646 }
00647 
00648 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
00649   return isa<UndefValue>(unwrap(Val));
00650 }
00651 
00652 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
00653   return
00654       wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
00655 }
00656 
00657 /*--.. Operations on metadata nodes ........................................--*/
00658 
00659 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
00660                                    unsigned SLen) {
00661   return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
00662 }
00663 
00664 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
00665   return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
00666 }
00667 
00668 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
00669                                  unsigned Count) {
00670   return wrap(MDNode::get(*unwrap(C),
00671                           makeArrayRef(unwrap<Value>(Vals, Count), Count)));
00672 }
00673 
00674 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
00675   return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
00676 }
00677 
00678 const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
00679   if (const MDString *S = dyn_cast<MDString>(unwrap(V))) {
00680     *Len = S->getString().size();
00681     return S->getString().data();
00682   }
00683   *Len = 0;
00684   return nullptr;
00685 }
00686 
00687 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V)
00688 {
00689   return cast<MDNode>(unwrap(V))->getNumOperands();
00690 }
00691 
00692 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest)
00693 {
00694   const MDNode *N = cast<MDNode>(unwrap(V));
00695   const unsigned numOperands = N->getNumOperands();
00696   for (unsigned i = 0; i < numOperands; i++)
00697     Dest[i] = wrap(N->getOperand(i));
00698 }
00699 
00700 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
00701 {
00702   if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
00703     return N->getNumOperands();
00704   }
00705   return 0;
00706 }
00707 
00708 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
00709 {
00710   NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
00711   if (!N)
00712     return;
00713   for (unsigned i=0;i<N->getNumOperands();i++)
00714     Dest[i] = wrap(N->getOperand(i));
00715 }
00716 
00717 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
00718                                  LLVMValueRef Val)
00719 {
00720   NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
00721   if (!N)
00722     return;
00723   MDNode *Op = Val ? unwrap<MDNode>(Val) : nullptr;
00724   if (Op)
00725     N->addOperand(Op);
00726 }
00727 
00728 /*--.. Operations on scalar constants ......................................--*/
00729 
00730 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
00731                           LLVMBool SignExtend) {
00732   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
00733 }
00734 
00735 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
00736                                               unsigned NumWords,
00737                                               const uint64_t Words[]) {
00738     IntegerType *Ty = unwrap<IntegerType>(IntTy);
00739     return wrap(ConstantInt::get(Ty->getContext(),
00740                                  APInt(Ty->getBitWidth(),
00741                                        makeArrayRef(Words, NumWords))));
00742 }
00743 
00744 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
00745                                   uint8_t Radix) {
00746   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
00747                                Radix));
00748 }
00749 
00750 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
00751                                          unsigned SLen, uint8_t Radix) {
00752   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
00753                                Radix));
00754 }
00755 
00756 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
00757   return wrap(ConstantFP::get(unwrap(RealTy), N));
00758 }
00759 
00760 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
00761   return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
00762 }
00763 
00764 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
00765                                           unsigned SLen) {
00766   return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
00767 }
00768 
00769 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
00770   return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
00771 }
00772 
00773 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
00774   return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
00775 }
00776 
00777 /*--.. Operations on composite constants ...................................--*/
00778 
00779 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
00780                                       unsigned Length,
00781                                       LLVMBool DontNullTerminate) {
00782   /* Inverted the sense of AddNull because ', 0)' is a
00783      better mnemonic for null termination than ', 1)'. */
00784   return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
00785                                            DontNullTerminate == 0));
00786 }
00787 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
00788                                       LLVMValueRef *ConstantVals,
00789                                       unsigned Count, LLVMBool Packed) {
00790   Constant **Elements = unwrap<Constant>(ConstantVals, Count);
00791   return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
00792                                       Packed != 0));
00793 }
00794 
00795 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
00796                              LLVMBool DontNullTerminate) {
00797   return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
00798                                   DontNullTerminate);
00799 }
00800 
00801 LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef c, unsigned idx) {
00802   return wrap(static_cast<ConstantDataSequential*>(unwrap(c))->getElementAsConstant(idx));
00803 }
00804 
00805 LLVMBool LLVMIsConstantString(LLVMValueRef c) {
00806   return static_cast<ConstantDataSequential*>(unwrap(c))->isString();
00807 }
00808 
00809 const char *LLVMGetAsString(LLVMValueRef c, size_t* Length) {
00810   StringRef str = static_cast<ConstantDataSequential*>(unwrap(c))->getAsString();
00811   *Length = str.size();
00812   return str.data();
00813 }
00814 
00815 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
00816                             LLVMValueRef *ConstantVals, unsigned Length) {
00817   ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
00818   return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
00819 }
00820 
00821 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
00822                              LLVMBool Packed) {
00823   return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
00824                                   Packed);
00825 }
00826 
00827 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
00828                                   LLVMValueRef *ConstantVals,
00829                                   unsigned Count) {
00830   Constant **Elements = unwrap<Constant>(ConstantVals, Count);
00831   StructType *Ty = cast<StructType>(unwrap(StructTy));
00832 
00833   return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
00834 }
00835 
00836 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
00837   return wrap(ConstantVector::get(makeArrayRef(
00838                             unwrap<Constant>(ScalarConstantVals, Size), Size)));
00839 }
00840 
00841 /*-- Opcode mapping */
00842 
00843 static LLVMOpcode map_to_llvmopcode(int opcode)
00844 {
00845     switch (opcode) {
00846       default: llvm_unreachable("Unhandled Opcode.");
00847 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
00848 #include "llvm/IR/Instruction.def"
00849 #undef HANDLE_INST
00850     }
00851 }
00852 
00853 static int map_from_llvmopcode(LLVMOpcode code)
00854 {
00855     switch (code) {
00856 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
00857 #include "llvm/IR/Instruction.def"
00858 #undef HANDLE_INST
00859     }
00860     llvm_unreachable("Unhandled Opcode.");
00861 }
00862 
00863 /*--.. Constant expressions ................................................--*/
00864 
00865 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
00866   return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
00867 }
00868 
00869 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
00870   return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
00871 }
00872 
00873 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
00874   return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
00875 }
00876 
00877 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
00878   return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
00879 }
00880 
00881 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
00882   return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
00883 }
00884 
00885 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
00886   return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
00887 }
00888 
00889 
00890 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
00891   return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
00892 }
00893 
00894 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
00895   return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
00896 }
00897 
00898 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00899   return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
00900                                    unwrap<Constant>(RHSConstant)));
00901 }
00902 
00903 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
00904                              LLVMValueRef RHSConstant) {
00905   return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
00906                                       unwrap<Constant>(RHSConstant)));
00907 }
00908 
00909 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
00910                              LLVMValueRef RHSConstant) {
00911   return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
00912                                       unwrap<Constant>(RHSConstant)));
00913 }
00914 
00915 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00916   return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
00917                                     unwrap<Constant>(RHSConstant)));
00918 }
00919 
00920 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00921   return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
00922                                    unwrap<Constant>(RHSConstant)));
00923 }
00924 
00925 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
00926                              LLVMValueRef RHSConstant) {
00927   return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
00928                                       unwrap<Constant>(RHSConstant)));
00929 }
00930 
00931 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
00932                              LLVMValueRef RHSConstant) {
00933   return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
00934                                       unwrap<Constant>(RHSConstant)));
00935 }
00936 
00937 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00938   return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
00939                                     unwrap<Constant>(RHSConstant)));
00940 }
00941 
00942 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00943   return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
00944                                    unwrap<Constant>(RHSConstant)));
00945 }
00946 
00947 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
00948                              LLVMValueRef RHSConstant) {
00949   return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
00950                                       unwrap<Constant>(RHSConstant)));
00951 }
00952 
00953 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
00954                              LLVMValueRef RHSConstant) {
00955   return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
00956                                       unwrap<Constant>(RHSConstant)));
00957 }
00958 
00959 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00960   return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
00961                                     unwrap<Constant>(RHSConstant)));
00962 }
00963 
00964 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00965   return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
00966                                     unwrap<Constant>(RHSConstant)));
00967 }
00968 
00969 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00970   return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
00971                                     unwrap<Constant>(RHSConstant)));
00972 }
00973 
00974 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
00975                                 LLVMValueRef RHSConstant) {
00976   return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
00977                                          unwrap<Constant>(RHSConstant)));
00978 }
00979 
00980 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00981   return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
00982                                     unwrap<Constant>(RHSConstant)));
00983 }
00984 
00985 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00986   return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
00987                                     unwrap<Constant>(RHSConstant)));
00988 }
00989 
00990 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00991   return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
00992                                     unwrap<Constant>(RHSConstant)));
00993 }
00994 
00995 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00996   return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
00997                                     unwrap<Constant>(RHSConstant)));
00998 }
00999 
01000 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
01001   return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
01002                                    unwrap<Constant>(RHSConstant)));
01003 }
01004 
01005 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
01006   return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
01007                                   unwrap<Constant>(RHSConstant)));
01008 }
01009 
01010 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
01011   return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
01012                                    unwrap<Constant>(RHSConstant)));
01013 }
01014 
01015 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
01016                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
01017   return wrap(ConstantExpr::getICmp(Predicate,
01018                                     unwrap<Constant>(LHSConstant),
01019                                     unwrap<Constant>(RHSConstant)));
01020 }
01021 
01022 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
01023                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
01024   return wrap(ConstantExpr::getFCmp(Predicate,
01025                                     unwrap<Constant>(LHSConstant),
01026                                     unwrap<Constant>(RHSConstant)));
01027 }
01028 
01029 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
01030   return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
01031                                    unwrap<Constant>(RHSConstant)));
01032 }
01033 
01034 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
01035   return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
01036                                     unwrap<Constant>(RHSConstant)));
01037 }
01038 
01039 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
01040   return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
01041                                     unwrap<Constant>(RHSConstant)));
01042 }
01043 
01044 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
01045                           LLVMValueRef *ConstantIndices, unsigned NumIndices) {
01046   ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
01047                                NumIndices);
01048   return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
01049                                              IdxList));
01050 }
01051 
01052 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
01053                                   LLVMValueRef *ConstantIndices,
01054                                   unsigned NumIndices) {
01055   Constant* Val = unwrap<Constant>(ConstantVal);
01056   ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
01057                                NumIndices);
01058   return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
01059 }
01060 
01061 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01062   return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
01063                                      unwrap(ToType)));
01064 }
01065 
01066 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01067   return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
01068                                     unwrap(ToType)));
01069 }
01070 
01071 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01072   return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
01073                                     unwrap(ToType)));
01074 }
01075 
01076 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01077   return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
01078                                        unwrap(ToType)));
01079 }
01080 
01081 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01082   return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
01083                                         unwrap(ToType)));
01084 }
01085 
01086 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01087   return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
01088                                       unwrap(ToType)));
01089 }
01090 
01091 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01092   return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
01093                                       unwrap(ToType)));
01094 }
01095 
01096 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01097   return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
01098                                       unwrap(ToType)));
01099 }
01100 
01101 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01102   return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
01103                                       unwrap(ToType)));
01104 }
01105 
01106 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01107   return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
01108                                         unwrap(ToType)));
01109 }
01110 
01111 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01112   return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
01113                                         unwrap(ToType)));
01114 }
01115 
01116 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01117   return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
01118                                        unwrap(ToType)));
01119 }
01120 
01121 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
01122                                     LLVMTypeRef ToType) {
01123   return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
01124                                              unwrap(ToType)));
01125 }
01126 
01127 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
01128                                     LLVMTypeRef ToType) {
01129   return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
01130                                              unwrap(ToType)));
01131 }
01132 
01133 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
01134                                     LLVMTypeRef ToType) {
01135   return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
01136                                              unwrap(ToType)));
01137 }
01138 
01139 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
01140                                      LLVMTypeRef ToType) {
01141   return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
01142                                               unwrap(ToType)));
01143 }
01144 
01145 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
01146                                   LLVMTypeRef ToType) {
01147   return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
01148                                            unwrap(ToType)));
01149 }
01150 
01151 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
01152                               LLVMBool isSigned) {
01153   return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
01154                                            unwrap(ToType), isSigned));
01155 }
01156 
01157 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01158   return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
01159                                       unwrap(ToType)));
01160 }
01161 
01162 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
01163                              LLVMValueRef ConstantIfTrue,
01164                              LLVMValueRef ConstantIfFalse) {
01165   return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
01166                                       unwrap<Constant>(ConstantIfTrue),
01167                                       unwrap<Constant>(ConstantIfFalse)));
01168 }
01169 
01170 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
01171                                      LLVMValueRef IndexConstant) {
01172   return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
01173                                               unwrap<Constant>(IndexConstant)));
01174 }
01175 
01176 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
01177                                     LLVMValueRef ElementValueConstant,
01178                                     LLVMValueRef IndexConstant) {
01179   return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
01180                                          unwrap<Constant>(ElementValueConstant),
01181                                              unwrap<Constant>(IndexConstant)));
01182 }
01183 
01184 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
01185                                     LLVMValueRef VectorBConstant,
01186                                     LLVMValueRef MaskConstant) {
01187   return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
01188                                              unwrap<Constant>(VectorBConstant),
01189                                              unwrap<Constant>(MaskConstant)));
01190 }
01191 
01192 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
01193                                    unsigned NumIdx) {
01194   return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
01195                                             makeArrayRef(IdxList, NumIdx)));
01196 }
01197 
01198 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
01199                                   LLVMValueRef ElementValueConstant,
01200                                   unsigned *IdxList, unsigned NumIdx) {
01201   return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
01202                                          unwrap<Constant>(ElementValueConstant),
01203                                            makeArrayRef(IdxList, NumIdx)));
01204 }
01205 
01206 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
01207                                 const char *Constraints,
01208                                 LLVMBool HasSideEffects,
01209                                 LLVMBool IsAlignStack) {
01210   return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
01211                              Constraints, HasSideEffects, IsAlignStack));
01212 }
01213 
01214 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
01215   return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
01216 }
01217 
01218 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
01219 
01220 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
01221   return wrap(unwrap<GlobalValue>(Global)->getParent());
01222 }
01223 
01224 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
01225   return unwrap<GlobalValue>(Global)->isDeclaration();
01226 }
01227 
01228 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
01229   switch (unwrap<GlobalValue>(Global)->getLinkage()) {
01230   case GlobalValue::ExternalLinkage:
01231     return LLVMExternalLinkage;
01232   case GlobalValue::AvailableExternallyLinkage:
01233     return LLVMAvailableExternallyLinkage;
01234   case GlobalValue::LinkOnceAnyLinkage:
01235     return LLVMLinkOnceAnyLinkage;
01236   case GlobalValue::LinkOnceODRLinkage:
01237     return LLVMLinkOnceODRLinkage;
01238   case GlobalValue::WeakAnyLinkage:
01239     return LLVMWeakAnyLinkage;
01240   case GlobalValue::WeakODRLinkage:
01241     return LLVMWeakODRLinkage;
01242   case GlobalValue::AppendingLinkage:
01243     return LLVMAppendingLinkage;
01244   case GlobalValue::InternalLinkage:
01245     return LLVMInternalLinkage;
01246   case GlobalValue::PrivateLinkage:
01247     return LLVMPrivateLinkage;
01248   case GlobalValue::ExternalWeakLinkage:
01249     return LLVMExternalWeakLinkage;
01250   case GlobalValue::CommonLinkage:
01251     return LLVMCommonLinkage;
01252   }
01253 
01254   llvm_unreachable("Invalid GlobalValue linkage!");
01255 }
01256 
01257 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
01258   GlobalValue *GV = unwrap<GlobalValue>(Global);
01259 
01260   switch (Linkage) {
01261   case LLVMExternalLinkage:
01262     GV->setLinkage(GlobalValue::ExternalLinkage);
01263     break;
01264   case LLVMAvailableExternallyLinkage:
01265     GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
01266     break;
01267   case LLVMLinkOnceAnyLinkage:
01268     GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
01269     break;
01270   case LLVMLinkOnceODRLinkage:
01271     GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
01272     break;
01273   case LLVMLinkOnceODRAutoHideLinkage:
01274     DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
01275                     "longer supported.");
01276     break;
01277   case LLVMWeakAnyLinkage:
01278     GV->setLinkage(GlobalValue::WeakAnyLinkage);
01279     break;
01280   case LLVMWeakODRLinkage:
01281     GV->setLinkage(GlobalValue::WeakODRLinkage);
01282     break;
01283   case LLVMAppendingLinkage:
01284     GV->setLinkage(GlobalValue::AppendingLinkage);
01285     break;
01286   case LLVMInternalLinkage:
01287     GV->setLinkage(GlobalValue::InternalLinkage);
01288     break;
01289   case LLVMPrivateLinkage:
01290     GV->setLinkage(GlobalValue::PrivateLinkage);
01291     break;
01292   case LLVMLinkerPrivateLinkage:
01293     GV->setLinkage(GlobalValue::PrivateLinkage);
01294     break;
01295   case LLVMLinkerPrivateWeakLinkage:
01296     GV->setLinkage(GlobalValue::PrivateLinkage);
01297     break;
01298   case LLVMDLLImportLinkage:
01299     DEBUG(errs()
01300           << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
01301     break;
01302   case LLVMDLLExportLinkage:
01303     DEBUG(errs()
01304           << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
01305     break;
01306   case LLVMExternalWeakLinkage:
01307     GV->setLinkage(GlobalValue::ExternalWeakLinkage);
01308     break;
01309   case LLVMGhostLinkage:
01310     DEBUG(errs()
01311           << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
01312     break;
01313   case LLVMCommonLinkage:
01314     GV->setLinkage(GlobalValue::CommonLinkage);
01315     break;
01316   }
01317 }
01318 
01319 const char *LLVMGetSection(LLVMValueRef Global) {
01320   return unwrap<GlobalValue>(Global)->getSection();
01321 }
01322 
01323 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
01324   unwrap<GlobalObject>(Global)->setSection(Section);
01325 }
01326 
01327 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
01328   return static_cast<LLVMVisibility>(
01329     unwrap<GlobalValue>(Global)->getVisibility());
01330 }
01331 
01332 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
01333   unwrap<GlobalValue>(Global)
01334     ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
01335 }
01336 
01337 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
01338   return static_cast<LLVMDLLStorageClass>(
01339       unwrap<GlobalValue>(Global)->getDLLStorageClass());
01340 }
01341 
01342 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
01343   unwrap<GlobalValue>(Global)->setDLLStorageClass(
01344       static_cast<GlobalValue::DLLStorageClassTypes>(Class));
01345 }
01346 
01347 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
01348   return unwrap<GlobalValue>(Global)->hasUnnamedAddr();
01349 }
01350 
01351 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
01352   unwrap<GlobalValue>(Global)->setUnnamedAddr(HasUnnamedAddr);
01353 }
01354 
01355 /*--.. Operations on global variables, load and store instructions .........--*/
01356 
01357 unsigned LLVMGetAlignment(LLVMValueRef V) {
01358   Value *P = unwrap<Value>(V);
01359   if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
01360     return GV->getAlignment();
01361   if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
01362     return AI->getAlignment();
01363   if (LoadInst *LI = dyn_cast<LoadInst>(P))
01364     return LI->getAlignment();
01365   if (StoreInst *SI = dyn_cast<StoreInst>(P))
01366     return SI->getAlignment();
01367 
01368   llvm_unreachable(
01369       "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
01370 }
01371 
01372 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
01373   Value *P = unwrap<Value>(V);
01374   if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
01375     GV->setAlignment(Bytes);
01376   else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
01377     AI->setAlignment(Bytes);
01378   else if (LoadInst *LI = dyn_cast<LoadInst>(P))
01379     LI->setAlignment(Bytes);
01380   else if (StoreInst *SI = dyn_cast<StoreInst>(P))
01381     SI->setAlignment(Bytes);
01382   else
01383     llvm_unreachable(
01384         "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
01385 }
01386 
01387 /*--.. Operations on global variables ......................................--*/
01388 
01389 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
01390   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
01391                                  GlobalValue::ExternalLinkage, nullptr, Name));
01392 }
01393 
01394 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
01395                                          const char *Name,
01396                                          unsigned AddressSpace) {
01397   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
01398                                  GlobalValue::ExternalLinkage, nullptr, Name,
01399                                  nullptr, GlobalVariable::NotThreadLocal,
01400                                  AddressSpace));
01401 }
01402 
01403 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
01404   return wrap(unwrap(M)->getNamedGlobal(Name));
01405 }
01406 
01407 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
01408   Module *Mod = unwrap(M);
01409   Module::global_iterator I = Mod->global_begin();
01410   if (I == Mod->global_end())
01411     return nullptr;
01412   return wrap(I);
01413 }
01414 
01415 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
01416   Module *Mod = unwrap(M);
01417   Module::global_iterator I = Mod->global_end();
01418   if (I == Mod->global_begin())
01419     return nullptr;
01420   return wrap(--I);
01421 }
01422 
01423 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
01424   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
01425   Module::global_iterator I = GV;
01426   if (++I == GV->getParent()->global_end())
01427     return nullptr;
01428   return wrap(I);
01429 }
01430 
01431 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
01432   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
01433   Module::global_iterator I = GV;
01434   if (I == GV->getParent()->global_begin())
01435     return nullptr;
01436   return wrap(--I);
01437 }
01438 
01439 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
01440   unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
01441 }
01442 
01443 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
01444   GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
01445   if ( !GV->hasInitializer() )
01446     return nullptr;
01447   return wrap(GV->getInitializer());
01448 }
01449 
01450 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
01451   unwrap<GlobalVariable>(GlobalVar)
01452     ->setInitializer(unwrap<Constant>(ConstantVal));
01453 }
01454 
01455 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
01456   return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
01457 }
01458 
01459 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
01460   unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
01461 }
01462 
01463 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
01464   return unwrap<GlobalVariable>(GlobalVar)->isConstant();
01465 }
01466 
01467 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
01468   unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
01469 }
01470 
01471 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
01472   switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
01473   case GlobalVariable::NotThreadLocal:
01474     return LLVMNotThreadLocal;
01475   case GlobalVariable::GeneralDynamicTLSModel:
01476     return LLVMGeneralDynamicTLSModel;
01477   case GlobalVariable::LocalDynamicTLSModel:
01478     return LLVMLocalDynamicTLSModel;
01479   case GlobalVariable::InitialExecTLSModel:
01480     return LLVMInitialExecTLSModel;
01481   case GlobalVariable::LocalExecTLSModel:
01482     return LLVMLocalExecTLSModel;
01483   }
01484 
01485   llvm_unreachable("Invalid GlobalVariable thread local mode");
01486 }
01487 
01488 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
01489   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
01490 
01491   switch (Mode) {
01492   case LLVMNotThreadLocal:
01493     GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
01494     break;
01495   case LLVMGeneralDynamicTLSModel:
01496     GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
01497     break;
01498   case LLVMLocalDynamicTLSModel:
01499     GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
01500     break;
01501   case LLVMInitialExecTLSModel:
01502     GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
01503     break;
01504   case LLVMLocalExecTLSModel:
01505     GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
01506     break;
01507   }
01508 }
01509 
01510 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
01511   return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
01512 }
01513 
01514 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
01515   unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
01516 }
01517 
01518 /*--.. Operations on aliases ......................................--*/
01519 
01520 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
01521                           const char *Name) {
01522   auto *PTy = cast<PointerType>(unwrap(Ty));
01523   return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
01524                                   GlobalValue::ExternalLinkage, Name,
01525                                   unwrap<GlobalObject>(Aliasee), unwrap(M)));
01526 }
01527 
01528 /*--.. Operations on functions .............................................--*/
01529 
01530 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
01531                              LLVMTypeRef FunctionTy) {
01532   return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
01533                                GlobalValue::ExternalLinkage, Name, unwrap(M)));
01534 }
01535 
01536 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
01537   return wrap(unwrap(M)->getFunction(Name));
01538 }
01539 
01540 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
01541   Module *Mod = unwrap(M);
01542   Module::iterator I = Mod->begin();
01543   if (I == Mod->end())
01544     return nullptr;
01545   return wrap(I);
01546 }
01547 
01548 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
01549   Module *Mod = unwrap(M);
01550   Module::iterator I = Mod->end();
01551   if (I == Mod->begin())
01552     return nullptr;
01553   return wrap(--I);
01554 }
01555 
01556 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
01557   Function *Func = unwrap<Function>(Fn);
01558   Module::iterator I = Func;
01559   if (++I == Func->getParent()->end())
01560     return nullptr;
01561   return wrap(I);
01562 }
01563 
01564 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
01565   Function *Func = unwrap<Function>(Fn);
01566   Module::iterator I = Func;
01567   if (I == Func->getParent()->begin())
01568     return nullptr;
01569   return wrap(--I);
01570 }
01571 
01572 void LLVMDeleteFunction(LLVMValueRef Fn) {
01573   unwrap<Function>(Fn)->eraseFromParent();
01574 }
01575 
01576 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
01577   if (Function *F = dyn_cast<Function>(unwrap(Fn)))
01578     return F->getIntrinsicID();
01579   return 0;
01580 }
01581 
01582 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
01583   return unwrap<Function>(Fn)->getCallingConv();
01584 }
01585 
01586 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
01587   return unwrap<Function>(Fn)->setCallingConv(
01588     static_cast<CallingConv::ID>(CC));
01589 }
01590 
01591 const char *LLVMGetGC(LLVMValueRef Fn) {
01592   Function *F = unwrap<Function>(Fn);
01593   return F->hasGC()? F->getGC() : nullptr;
01594 }
01595 
01596 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
01597   Function *F = unwrap<Function>(Fn);
01598   if (GC)
01599     F->setGC(GC);
01600   else
01601     F->clearGC();
01602 }
01603 
01604 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
01605   Function *Func = unwrap<Function>(Fn);
01606   const AttributeSet PAL = Func->getAttributes();
01607   AttrBuilder B(PA);
01608   const AttributeSet PALnew =
01609     PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
01610                       AttributeSet::get(Func->getContext(),
01611                                         AttributeSet::FunctionIndex, B));
01612   Func->setAttributes(PALnew);
01613 }
01614 
01615 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
01616                                         const char *V) {
01617   Function *Func = unwrap<Function>(Fn);
01618   AttributeSet::AttrIndex Idx =
01619     AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
01620   AttrBuilder B;
01621 
01622   B.addAttribute(A, V);
01623   AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
01624   Func->addAttributes(Idx, Set);
01625 }
01626 
01627 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
01628   Function *Func = unwrap<Function>(Fn);
01629   const AttributeSet PAL = Func->getAttributes();
01630   AttrBuilder B(PA);
01631   const AttributeSet PALnew =
01632     PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
01633                          AttributeSet::get(Func->getContext(),
01634                                            AttributeSet::FunctionIndex, B));
01635   Func->setAttributes(PALnew);
01636 }
01637 
01638 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
01639   Function *Func = unwrap<Function>(Fn);
01640   const AttributeSet PAL = Func->getAttributes();
01641   return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
01642 }
01643 
01644 /*--.. Operations on parameters ............................................--*/
01645 
01646 unsigned LLVMCountParams(LLVMValueRef FnRef) {
01647   // This function is strictly redundant to
01648   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
01649   return unwrap<Function>(FnRef)->arg_size();
01650 }
01651 
01652 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
01653   Function *Fn = unwrap<Function>(FnRef);
01654   for (Function::arg_iterator I = Fn->arg_begin(),
01655                               E = Fn->arg_end(); I != E; I++)
01656     *ParamRefs++ = wrap(I);
01657 }
01658 
01659 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
01660   Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
01661   while (index --> 0)
01662     AI++;
01663   return wrap(AI);
01664 }
01665 
01666 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
01667   return wrap(unwrap<Argument>(V)->getParent());
01668 }
01669 
01670 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
01671   Function *Func = unwrap<Function>(Fn);
01672   Function::arg_iterator I = Func->arg_begin();
01673   if (I == Func->arg_end())
01674     return nullptr;
01675   return wrap(I);
01676 }
01677 
01678 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
01679   Function *Func = unwrap<Function>(Fn);
01680   Function::arg_iterator I = Func->arg_end();
01681   if (I == Func->arg_begin())
01682     return nullptr;
01683   return wrap(--I);
01684 }
01685 
01686 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
01687   Argument *A = unwrap<Argument>(Arg);
01688   Function::arg_iterator I = A;
01689   if (++I == A->getParent()->arg_end())
01690     return nullptr;
01691   return wrap(I);
01692 }
01693 
01694 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
01695   Argument *A = unwrap<Argument>(Arg);
01696   Function::arg_iterator I = A;
01697   if (I == A->getParent()->arg_begin())
01698     return nullptr;
01699   return wrap(--I);
01700 }
01701 
01702 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
01703   Argument *A = unwrap<Argument>(Arg);
01704   AttrBuilder B(PA);
01705   A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1,  B));
01706 }
01707 
01708 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
01709   Argument *A = unwrap<Argument>(Arg);
01710   AttrBuilder B(PA);
01711   A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1,  B));
01712 }
01713 
01714 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
01715   Argument *A = unwrap<Argument>(Arg);
01716   return (LLVMAttribute)A->getParent()->getAttributes().
01717     Raw(A->getArgNo()+1);
01718 }
01719 
01720 
01721 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
01722   Argument *A = unwrap<Argument>(Arg);
01723   AttrBuilder B;
01724   B.addAlignmentAttr(align);
01725   A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
01726 }
01727 
01728 /*--.. Operations on basic blocks ..........................................--*/
01729 
01730 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
01731   return wrap(static_cast<Value*>(unwrap(BB)));
01732 }
01733 
01734 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
01735   return isa<BasicBlock>(unwrap(Val));
01736 }
01737 
01738 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
01739   return wrap(unwrap<BasicBlock>(Val));
01740 }
01741 
01742 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
01743   return wrap(unwrap(BB)->getParent());
01744 }
01745 
01746 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
01747   return wrap(unwrap(BB)->getTerminator());
01748 }
01749 
01750 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
01751   return unwrap<Function>(FnRef)->size();
01752 }
01753 
01754 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
01755   Function *Fn = unwrap<Function>(FnRef);
01756   for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
01757     *BasicBlocksRefs++ = wrap(I);
01758 }
01759 
01760 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
01761   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
01762 }
01763 
01764 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
01765   Function *Func = unwrap<Function>(Fn);
01766   Function::iterator I = Func->begin();
01767   if (I == Func->end())
01768     return nullptr;
01769   return wrap(I);
01770 }
01771 
01772 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
01773   Function *Func = unwrap<Function>(Fn);
01774   Function::iterator I = Func->end();
01775   if (I == Func->begin())
01776     return nullptr;
01777   return wrap(--I);
01778 }
01779 
01780 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
01781   BasicBlock *Block = unwrap(BB);
01782   Function::iterator I = Block;
01783   if (++I == Block->getParent()->end())
01784     return nullptr;
01785   return wrap(I);
01786 }
01787 
01788 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
01789   BasicBlock *Block = unwrap(BB);
01790   Function::iterator I = Block;
01791   if (I == Block->getParent()->begin())
01792     return nullptr;
01793   return wrap(--I);
01794 }
01795 
01796 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
01797                                                 LLVMValueRef FnRef,
01798                                                 const char *Name) {
01799   return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
01800 }
01801 
01802 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
01803   return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
01804 }
01805 
01806 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
01807                                                 LLVMBasicBlockRef BBRef,
01808                                                 const char *Name) {
01809   BasicBlock *BB = unwrap(BBRef);
01810   return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
01811 }
01812 
01813 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
01814                                        const char *Name) {
01815   return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
01816 }
01817 
01818 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
01819   unwrap(BBRef)->eraseFromParent();
01820 }
01821 
01822 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
01823   unwrap(BBRef)->removeFromParent();
01824 }
01825 
01826 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
01827   unwrap(BB)->moveBefore(unwrap(MovePos));
01828 }
01829 
01830 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
01831   unwrap(BB)->moveAfter(unwrap(MovePos));
01832 }
01833 
01834 /*--.. Operations on instructions ..........................................--*/
01835 
01836 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
01837   return wrap(unwrap<Instruction>(Inst)->getParent());
01838 }
01839 
01840 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
01841   BasicBlock *Block = unwrap(BB);
01842   BasicBlock::iterator I = Block->begin();
01843   if (I == Block->end())
01844     return nullptr;
01845   return wrap(I);
01846 }
01847 
01848 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
01849   BasicBlock *Block = unwrap(BB);
01850   BasicBlock::iterator I = Block->end();
01851   if (I == Block->begin())
01852     return nullptr;
01853   return wrap(--I);
01854 }
01855 
01856 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
01857   Instruction *Instr = unwrap<Instruction>(Inst);
01858   BasicBlock::iterator I = Instr;
01859   if (++I == Instr->getParent()->end())
01860     return nullptr;
01861   return wrap(I);
01862 }
01863 
01864 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
01865   Instruction *Instr = unwrap<Instruction>(Inst);
01866   BasicBlock::iterator I = Instr;
01867   if (I == Instr->getParent()->begin())
01868     return nullptr;
01869   return wrap(--I);
01870 }
01871 
01872 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
01873   unwrap<Instruction>(Inst)->eraseFromParent();
01874 }
01875 
01876 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
01877   if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
01878     return (LLVMIntPredicate)I->getPredicate();
01879   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
01880     if (CE->getOpcode() == Instruction::ICmp)
01881       return (LLVMIntPredicate)CE->getPredicate();
01882   return (LLVMIntPredicate)0;
01883 }
01884 
01885 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
01886   if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
01887     return map_to_llvmopcode(C->getOpcode());
01888   return (LLVMOpcode)0;
01889 }
01890 
01891 /*--.. Call and invoke instructions ........................................--*/
01892 
01893 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
01894   Value *V = unwrap(Instr);
01895   if (CallInst *CI = dyn_cast<CallInst>(V))
01896     return CI->getCallingConv();
01897   if (InvokeInst *II = dyn_cast<InvokeInst>(V))
01898     return II->getCallingConv();
01899   llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
01900 }
01901 
01902 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
01903   Value *V = unwrap(Instr);
01904   if (CallInst *CI = dyn_cast<CallInst>(V))
01905     return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
01906   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
01907     return II->setCallingConv(static_cast<CallingConv::ID>(CC));
01908   llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
01909 }
01910 
01911 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
01912                            LLVMAttribute PA) {
01913   CallSite Call = CallSite(unwrap<Instruction>(Instr));
01914   AttrBuilder B(PA);
01915   Call.setAttributes(
01916     Call.getAttributes().addAttributes(Call->getContext(), index,
01917                                        AttributeSet::get(Call->getContext(),
01918                                                          index, B)));
01919 }
01920 
01921 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
01922                               LLVMAttribute PA) {
01923   CallSite Call = CallSite(unwrap<Instruction>(Instr));
01924   AttrBuilder B(PA);
01925   Call.setAttributes(Call.getAttributes()
01926                        .removeAttributes(Call->getContext(), index,
01927                                          AttributeSet::get(Call->getContext(),
01928                                                            index, B)));
01929 }
01930 
01931 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
01932                                 unsigned align) {
01933   CallSite Call = CallSite(unwrap<Instruction>(Instr));
01934   AttrBuilder B;
01935   B.addAlignmentAttr(align);
01936   Call.setAttributes(Call.getAttributes()
01937                        .addAttributes(Call->getContext(), index,
01938                                       AttributeSet::get(Call->getContext(),
01939                                                         index, B)));
01940 }
01941 
01942 /*--.. Operations on call instructions (only) ..............................--*/
01943 
01944 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
01945   return unwrap<CallInst>(Call)->isTailCall();
01946 }
01947 
01948 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
01949   unwrap<CallInst>(Call)->setTailCall(isTailCall);
01950 }
01951 
01952 /*--.. Operations on switch instructions (only) ............................--*/
01953 
01954 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
01955   return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
01956 }
01957 
01958 /*--.. Operations on phi nodes .............................................--*/
01959 
01960 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
01961                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
01962   PHINode *PhiVal = unwrap<PHINode>(PhiNode);
01963   for (unsigned I = 0; I != Count; ++I)
01964     PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
01965 }
01966 
01967 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
01968   return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
01969 }
01970 
01971 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
01972   return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
01973 }
01974 
01975 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
01976   return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
01977 }
01978 
01979 
01980 /*===-- Instruction builders ----------------------------------------------===*/
01981 
01982 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
01983   return wrap(new IRBuilder<>(*unwrap(C)));
01984 }
01985 
01986 LLVMBuilderRef LLVMCreateBuilder(void) {
01987   return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
01988 }
01989 
01990 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
01991                          LLVMValueRef Instr) {
01992   BasicBlock *BB = unwrap(Block);
01993   Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
01994   unwrap(Builder)->SetInsertPoint(BB, I);
01995 }
01996 
01997 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
01998   Instruction *I = unwrap<Instruction>(Instr);
01999   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
02000 }
02001 
02002 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
02003   BasicBlock *BB = unwrap(Block);
02004   unwrap(Builder)->SetInsertPoint(BB);
02005 }
02006 
02007 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
02008    return wrap(unwrap(Builder)->GetInsertBlock());
02009 }
02010 
02011 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
02012   unwrap(Builder)->ClearInsertionPoint();
02013 }
02014 
02015 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
02016   unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
02017 }
02018 
02019 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
02020                                    const char *Name) {
02021   unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
02022 }
02023 
02024 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
02025   delete unwrap(Builder);
02026 }
02027 
02028 /*--.. Metadata builders ...................................................--*/
02029 
02030 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
02031   MDNode *Loc = L ? unwrap<MDNode>(L) : nullptr;
02032   unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
02033 }
02034 
02035 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
02036   return wrap(unwrap(Builder)->getCurrentDebugLocation()
02037               .getAsMDNode(unwrap(Builder)->getContext()));
02038 }
02039 
02040 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
02041   unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
02042 }
02043 
02044 
02045 /*--.. Instruction builders ................................................--*/
02046 
02047 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
02048   return wrap(unwrap(B)->CreateRetVoid());
02049 }
02050 
02051 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
02052   return wrap(unwrap(B)->CreateRet(unwrap(V)));
02053 }
02054 
02055 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
02056                                    unsigned N) {
02057   return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
02058 }
02059 
02060 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
02061   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
02062 }
02063 
02064 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
02065                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
02066   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
02067 }
02068 
02069 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
02070                              LLVMBasicBlockRef Else, unsigned NumCases) {
02071   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
02072 }
02073 
02074 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
02075                                  unsigned NumDests) {
02076   return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
02077 }
02078 
02079 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
02080                              LLVMValueRef *Args, unsigned NumArgs,
02081                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
02082                              const char *Name) {
02083   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
02084                                       makeArrayRef(unwrap(Args), NumArgs),
02085                                       Name));
02086 }
02087 
02088 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
02089                                  LLVMValueRef PersFn, unsigned NumClauses,
02090                                  const char *Name) {
02091   return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
02092                                           cast<Function>(unwrap(PersFn)),
02093                                           NumClauses, Name));
02094 }
02095 
02096 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
02097   return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
02098 }
02099 
02100 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
02101   return wrap(unwrap(B)->CreateUnreachable());
02102 }
02103 
02104 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
02105                  LLVMBasicBlockRef Dest) {
02106   unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
02107 }
02108 
02109 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
02110   unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
02111 }
02112 
02113 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
02114   unwrap<LandingPadInst>(LandingPad)->
02115     addClause(cast<Constant>(unwrap(ClauseVal)));
02116 }
02117 
02118 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
02119   unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
02120 }
02121 
02122 /*--.. Arithmetic ..........................................................--*/
02123 
02124 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02125                           const char *Name) {
02126   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
02127 }
02128 
02129 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02130                           const char *Name) {
02131   return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
02132 }
02133 
02134 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02135                           const char *Name) {
02136   return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
02137 }
02138 
02139 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02140                           const char *Name) {
02141   return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
02142 }
02143 
02144 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02145                           const char *Name) {
02146   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
02147 }
02148 
02149 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02150                           const char *Name) {
02151   return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
02152 }
02153 
02154 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02155                           const char *Name) {
02156   return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
02157 }
02158 
02159 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02160                           const char *Name) {
02161   return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
02162 }
02163 
02164 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02165                           const char *Name) {
02166   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
02167 }
02168 
02169 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02170                           const char *Name) {
02171   return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
02172 }
02173 
02174 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02175                           const char *Name) {
02176   return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
02177 }
02178 
02179 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02180                           const char *Name) {
02181   return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
02182 }
02183 
02184 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02185                            const char *Name) {
02186   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
02187 }
02188 
02189 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02190                            const char *Name) {
02191   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
02192 }
02193 
02194 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
02195                                 LLVMValueRef RHS, const char *Name) {
02196   return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
02197 }
02198 
02199 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02200                            const char *Name) {
02201   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
02202 }
02203 
02204 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02205                            const char *Name) {
02206   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
02207 }
02208 
02209 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02210                            const char *Name) {
02211   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
02212 }
02213 
02214 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02215                            const char *Name) {
02216   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
02217 }
02218 
02219 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02220                           const char *Name) {
02221   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
02222 }
02223 
02224 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02225                            const char *Name) {
02226   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
02227 }
02228 
02229 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02230                            const char *Name) {
02231   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
02232 }
02233 
02234 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02235                           const char *Name) {
02236   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
02237 }
02238 
02239 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02240                          const char *Name) {
02241   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
02242 }
02243 
02244 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02245                           const char *Name) {
02246   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
02247 }
02248 
02249 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
02250                             LLVMValueRef LHS, LLVMValueRef RHS,
02251                             const char *Name) {
02252   return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
02253                                      unwrap(RHS), Name));
02254 }
02255 
02256 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
02257   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
02258 }
02259 
02260 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
02261                              const char *Name) {
02262   return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
02263 }
02264 
02265 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
02266                              const char *Name) {
02267   return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
02268 }
02269 
02270 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
02271   return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
02272 }
02273 
02274 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
02275   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
02276 }
02277 
02278 /*--.. Memory ..............................................................--*/
02279 
02280 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
02281                              const char *Name) {
02282   Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
02283   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
02284   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
02285   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
02286                                                ITy, unwrap(Ty), AllocSize,
02287                                                nullptr, nullptr, "");
02288   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
02289 }
02290 
02291 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
02292                                   LLVMValueRef Val, const char *Name) {
02293   Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
02294   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
02295   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
02296   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
02297                                                ITy, unwrap(Ty), AllocSize,
02298                                                unwrap(Val), nullptr, "");
02299   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
02300 }
02301 
02302 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
02303                              const char *Name) {
02304   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
02305 }
02306 
02307 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
02308                                   LLVMValueRef Val, const char *Name) {
02309   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
02310 }
02311 
02312 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
02313   return wrap(unwrap(B)->Insert(
02314      CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
02315 }
02316 
02317 
02318 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
02319                            const char *Name) {
02320   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
02321 }
02322 
02323 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
02324                             LLVMValueRef PointerVal) {
02325   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
02326 }
02327 
02328 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
02329   switch (Ordering) {
02330     case LLVMAtomicOrderingNotAtomic: return NotAtomic;
02331     case LLVMAtomicOrderingUnordered: return Unordered;
02332     case LLVMAtomicOrderingMonotonic: return Monotonic;
02333     case LLVMAtomicOrderingAcquire: return Acquire;
02334     case LLVMAtomicOrderingRelease: return Release;
02335     case LLVMAtomicOrderingAcquireRelease: return AcquireRelease;
02336     case LLVMAtomicOrderingSequentiallyConsistent:
02337       return SequentiallyConsistent;
02338   }
02339   
02340   llvm_unreachable("Invalid LLVMAtomicOrdering value!");
02341 }
02342 
02343 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
02344                             LLVMBool isSingleThread, const char *Name) {
02345   return wrap(
02346     unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
02347                            isSingleThread ? SingleThread : CrossThread,
02348                            Name));
02349 }
02350 
02351 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
02352                           LLVMValueRef *Indices, unsigned NumIndices,
02353                           const char *Name) {
02354   ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
02355   return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
02356 }
02357 
02358 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
02359                                   LLVMValueRef *Indices, unsigned NumIndices,
02360                                   const char *Name) {
02361   ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
02362   return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
02363 }
02364 
02365 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
02366                                 unsigned Idx, const char *Name) {
02367   return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
02368 }
02369 
02370 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
02371                                    const char *Name) {
02372   return wrap(unwrap(B)->CreateGlobalString(Str, Name));
02373 }
02374 
02375 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
02376                                       const char *Name) {
02377   return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
02378 }
02379 
02380 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
02381   Value *P = unwrap<Value>(MemAccessInst);
02382   if (LoadInst *LI = dyn_cast<LoadInst>(P))
02383     return LI->isVolatile();
02384   return cast<StoreInst>(P)->isVolatile();
02385 }
02386 
02387 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
02388   Value *P = unwrap<Value>(MemAccessInst);
02389   if (LoadInst *LI = dyn_cast<LoadInst>(P))
02390     return LI->setVolatile(isVolatile);
02391   return cast<StoreInst>(P)->setVolatile(isVolatile);
02392 }
02393 
02394 /*--.. Casts ...............................................................--*/
02395 
02396 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
02397                             LLVMTypeRef DestTy, const char *Name) {
02398   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
02399 }
02400 
02401 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
02402                            LLVMTypeRef DestTy, const char *Name) {
02403   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
02404 }
02405 
02406 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
02407                            LLVMTypeRef DestTy, const char *Name) {
02408   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
02409 }
02410 
02411 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
02412                              LLVMTypeRef DestTy, const char *Name) {
02413   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
02414 }
02415 
02416 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
02417                              LLVMTypeRef DestTy, const char *Name) {
02418   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
02419 }
02420 
02421 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
02422                              LLVMTypeRef DestTy, const char *Name) {
02423   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
02424 }
02425 
02426 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
02427                              LLVMTypeRef DestTy, const char *Name) {
02428   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
02429 }
02430 
02431 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
02432                               LLVMTypeRef DestTy, const char *Name) {
02433   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
02434 }
02435 
02436 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
02437                             LLVMTypeRef DestTy, const char *Name) {
02438   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
02439 }
02440 
02441 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
02442                                LLVMTypeRef DestTy, const char *Name) {
02443   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
02444 }
02445 
02446 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
02447                                LLVMTypeRef DestTy, const char *Name) {
02448   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
02449 }
02450 
02451 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
02452                               LLVMTypeRef DestTy, const char *Name) {
02453   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
02454 }
02455 
02456 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
02457                                     LLVMTypeRef DestTy, const char *Name) {
02458   return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
02459 }
02460 
02461 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
02462                                     LLVMTypeRef DestTy, const char *Name) {
02463   return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
02464                                              Name));
02465 }
02466 
02467 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
02468                                     LLVMTypeRef DestTy, const char *Name) {
02469   return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
02470                                              Name));
02471 }
02472 
02473 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
02474                                      LLVMTypeRef DestTy, const char *Name) {
02475   return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
02476                                               Name));
02477 }
02478 
02479 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
02480                            LLVMTypeRef DestTy, const char *Name) {
02481   return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
02482                                     unwrap(DestTy), Name));
02483 }
02484 
02485 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
02486                                   LLVMTypeRef DestTy, const char *Name) {
02487   return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
02488 }
02489 
02490 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
02491                               LLVMTypeRef DestTy, const char *Name) {
02492   return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
02493                                        /*isSigned*/true, Name));
02494 }
02495 
02496 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
02497                              LLVMTypeRef DestTy, const char *Name) {
02498   return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
02499 }
02500 
02501 /*--.. Comparisons .........................................................--*/
02502 
02503 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
02504                            LLVMValueRef LHS, LLVMValueRef RHS,
02505                            const char *Name) {
02506   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
02507                                     unwrap(LHS), unwrap(RHS), Name));
02508 }
02509 
02510 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
02511                            LLVMValueRef LHS, LLVMValueRef RHS,
02512                            const char *Name) {
02513   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
02514                                     unwrap(LHS), unwrap(RHS), Name));
02515 }
02516 
02517 /*--.. Miscellaneous instructions ..........................................--*/
02518 
02519 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
02520   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
02521 }
02522 
02523 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
02524                            LLVMValueRef *Args, unsigned NumArgs,
02525                            const char *Name) {
02526   return wrap(unwrap(B)->CreateCall(unwrap(Fn),
02527                                     makeArrayRef(unwrap(Args), NumArgs),
02528                                     Name));
02529 }
02530 
02531 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
02532                              LLVMValueRef Then, LLVMValueRef Else,
02533                              const char *Name) {
02534   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
02535                                       Name));
02536 }
02537 
02538 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
02539                             LLVMTypeRef Ty, const char *Name) {
02540   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
02541 }
02542 
02543 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
02544                                       LLVMValueRef Index, const char *Name) {
02545   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
02546                                               Name));
02547 }
02548 
02549 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
02550                                     LLVMValueRef EltVal, LLVMValueRef Index,
02551                                     const char *Name) {
02552   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
02553                                              unwrap(Index), Name));
02554 }
02555 
02556 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
02557                                     LLVMValueRef V2, LLVMValueRef Mask,
02558                                     const char *Name) {
02559   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
02560                                              unwrap(Mask), Name));
02561 }
02562 
02563 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
02564                                    unsigned Index, const char *Name) {
02565   return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
02566 }
02567 
02568 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
02569                                   LLVMValueRef EltVal, unsigned Index,
02570                                   const char *Name) {
02571   return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
02572                                            Index, Name));
02573 }
02574 
02575 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
02576                              const char *Name) {
02577   return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
02578 }
02579 
02580 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
02581                                 const char *Name) {
02582   return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
02583 }
02584 
02585 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
02586                               LLVMValueRef RHS, const char *Name) {
02587   return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
02588 }
02589 
02590 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
02591                                LLVMValueRef PTR, LLVMValueRef Val,
02592                                LLVMAtomicOrdering ordering,
02593                                LLVMBool singleThread) {
02594   AtomicRMWInst::BinOp intop;
02595   switch (op) {
02596     case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
02597     case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
02598     case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
02599     case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
02600     case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
02601     case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
02602     case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
02603     case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
02604     case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
02605     case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
02606     case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
02607   }
02608   return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
02609     mapFromLLVMOrdering(ordering), singleThread ? SingleThread : CrossThread));
02610 }
02611 
02612 
02613 /*===-- Module providers --------------------------------------------------===*/
02614 
02615 LLVMModuleProviderRef
02616 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
02617   return reinterpret_cast<LLVMModuleProviderRef>(M);
02618 }
02619 
02620 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
02621   delete unwrap(MP);
02622 }
02623 
02624 
02625 /*===-- Memory buffers ----------------------------------------------------===*/
02626 
02627 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
02628     const char *Path,
02629     LLVMMemoryBufferRef *OutMemBuf,
02630     char **OutMessage) {
02631 
02632   ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
02633   if (std::error_code EC = MBOrErr.getError()) {
02634     *OutMessage = strdup(EC.message().c_str());
02635     return 1;
02636   }
02637   *OutMemBuf = wrap(MBOrErr.get().release());
02638   return 0;
02639 }
02640 
02641 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
02642                                          char **OutMessage) {
02643   ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
02644   if (std::error_code EC = MBOrErr.getError()) {
02645     *OutMessage = strdup(EC.message().c_str());
02646     return 1;
02647   }
02648   *OutMemBuf = wrap(MBOrErr.get().release());
02649   return 0;
02650 }
02651 
02652 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
02653     const char *InputData,
02654     size_t InputDataLength,
02655     const char *BufferName,
02656     LLVMBool RequiresNullTerminator) {
02657 
02658   return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
02659                                          StringRef(BufferName),
02660                                          RequiresNullTerminator).release());
02661 }
02662 
02663 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
02664     const char *InputData,
02665     size_t InputDataLength,
02666     const char *BufferName) {
02667 
02668   return wrap(
02669       MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
02670                                      StringRef(BufferName)).release());
02671 }
02672 
02673 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
02674   return unwrap(MemBuf)->getBufferStart();
02675 }
02676 
02677 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
02678   return unwrap(MemBuf)->getBufferSize();
02679 }
02680 
02681 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
02682   delete unwrap(MemBuf);
02683 }
02684 
02685 /*===-- Pass Registry -----------------------------------------------------===*/
02686 
02687 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
02688   return wrap(PassRegistry::getPassRegistry());
02689 }
02690 
02691 /*===-- Pass Manager ------------------------------------------------------===*/
02692 
02693 LLVMPassManagerRef LLVMCreatePassManager() {
02694   return wrap(new PassManager());
02695 }
02696 
02697 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
02698   return wrap(new FunctionPassManager(unwrap(M)));
02699 }
02700 
02701 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
02702   return LLVMCreateFunctionPassManagerForModule(
02703                                             reinterpret_cast<LLVMModuleRef>(P));
02704 }
02705 
02706 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
02707   return unwrap<PassManager>(PM)->run(*unwrap(M));
02708 }
02709 
02710 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
02711   return unwrap<FunctionPassManager>(FPM)->doInitialization();
02712 }
02713 
02714 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
02715   return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
02716 }
02717 
02718 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
02719   return unwrap<FunctionPassManager>(FPM)->doFinalization();
02720 }
02721 
02722 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
02723   delete unwrap(PM);
02724 }
02725 
02726 /*===-- Threading ------------------------------------------------------===*/
02727 
02728 LLVMBool LLVMStartMultithreaded() {
02729   return LLVMIsMultithreaded();
02730 }
02731 
02732 void LLVMStopMultithreaded() {
02733 }
02734 
02735 LLVMBool LLVMIsMultithreaded() {
02736   return llvm_is_multithreaded();
02737 }