Home | History | Annotate | Download | only in IR
      1 //===-- Core.cpp ----------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file implements the common infrastructure (including the C bindings)
     11 // for libLLVMCore.a, which implements the LLVM intermediate representation.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #include "llvm-c/Core.h"
     16 #include "llvm/Bitcode/ReaderWriter.h"
     17 #include "llvm/IR/Attributes.h"
     18 #include "llvm/IR/Constants.h"
     19 #include "llvm/IR/DerivedTypes.h"
     20 #include "llvm/IR/GlobalAlias.h"
     21 #include "llvm/IR/GlobalVariable.h"
     22 #include "llvm/IR/InlineAsm.h"
     23 #include "llvm/IR/IntrinsicInst.h"
     24 #include "llvm/IR/IRBuilder.h"
     25 #include "llvm/IR/LLVMContext.h"
     26 #include "llvm/IR/Module.h"
     27 #include "llvm/PassManager.h"
     28 #include "llvm/Support/CallSite.h"
     29 #include "llvm/Support/Debug.h"
     30 #include "llvm/Support/ErrorHandling.h"
     31 #include "llvm/Support/ManagedStatic.h"
     32 #include "llvm/Support/MemoryBuffer.h"
     33 #include "llvm/Support/raw_ostream.h"
     34 #include "llvm/Support/system_error.h"
     35 #include "llvm/Support/Threading.h"
     36 #include <cassert>
     37 #include <cstdlib>
     38 #include <cstring>
     39 
     40 using namespace llvm;
     41 
     42 void llvm::initializeCore(PassRegistry &Registry) {
     43   initializeDominatorTreePass(Registry);
     44   initializePrintModulePassPass(Registry);
     45   initializePrintFunctionPassPass(Registry);
     46   initializePrintBasicBlockPassPass(Registry);
     47   initializeVerifierPass(Registry);
     48   initializePreVerifierPass(Registry);
     49 }
     50 
     51 void LLVMInitializeCore(LLVMPassRegistryRef R) {
     52   initializeCore(*unwrap(R));
     53 }
     54 
     55 void LLVMShutdown() {
     56   llvm_shutdown();
     57 }
     58 
     59 /*===-- Error handling ----------------------------------------------------===*/
     60 
     61 char *LLVMCreateMessage(const char *Message) {
     62   return strdup(Message);
     63 }
     64 
     65 void LLVMDisposeMessage(char *Message) {
     66   free(Message);
     67 }
     68 
     69 
     70 /*===-- Operations on contexts --------------------------------------------===*/
     71 
     72 LLVMContextRef LLVMContextCreate() {
     73   return wrap(new LLVMContext());
     74 }
     75 
     76 LLVMContextRef LLVMGetGlobalContext() {
     77   return wrap(&getGlobalContext());
     78 }
     79 
     80 void LLVMContextDispose(LLVMContextRef C) {
     81   delete unwrap(C);
     82 }
     83 
     84 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name,
     85                                   unsigned SLen) {
     86   return unwrap(C)->getMDKindID(StringRef(Name, SLen));
     87 }
     88 
     89 unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) {
     90   return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
     91 }
     92 
     93 
     94 /*===-- Operations on modules ---------------------------------------------===*/
     95 
     96 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
     97   return wrap(new Module(ModuleID, getGlobalContext()));
     98 }
     99 
    100 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
    101                                                 LLVMContextRef C) {
    102   return wrap(new Module(ModuleID, *unwrap(C)));
    103 }
    104 
    105 void LLVMDisposeModule(LLVMModuleRef M) {
    106   delete unwrap(M);
    107 }
    108 
    109 /*--.. Data layout .........................................................--*/
    110 const char * LLVMGetDataLayout(LLVMModuleRef M) {
    111   return unwrap(M)->getDataLayout().c_str();
    112 }
    113 
    114 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
    115   unwrap(M)->setDataLayout(Triple);
    116 }
    117 
    118 /*--.. Target triple .......................................................--*/
    119 const char * LLVMGetTarget(LLVMModuleRef M) {
    120   return unwrap(M)->getTargetTriple().c_str();
    121 }
    122 
    123 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
    124   unwrap(M)->setTargetTriple(Triple);
    125 }
    126 
    127 void LLVMDumpModule(LLVMModuleRef M) {
    128   unwrap(M)->dump();
    129 }
    130 
    131 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
    132                                char **ErrorMessage) {
    133   std::string error;
    134   raw_fd_ostream dest(Filename, error);
    135   if (!error.empty()) {
    136     *ErrorMessage = strdup(error.c_str());
    137     return true;
    138   }
    139 
    140   unwrap(M)->print(dest, NULL);
    141 
    142   if (!error.empty()) {
    143     *ErrorMessage = strdup(error.c_str());
    144     return true;
    145   }
    146   dest.flush();
    147   return false;
    148 }
    149 
    150 /*--.. Operations on inline assembler ......................................--*/
    151 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
    152   unwrap(M)->setModuleInlineAsm(StringRef(Asm));
    153 }
    154 
    155 
    156 /*--.. Operations on module contexts ......................................--*/
    157 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
    158   return wrap(&unwrap(M)->getContext());
    159 }
    160 
    161 
    162 /*===-- Operations on types -----------------------------------------------===*/
    163 
    164 /*--.. Operations on all types (mostly) ....................................--*/
    165 
    166 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
    167   switch (unwrap(Ty)->getTypeID()) {
    168   default: llvm_unreachable("Unhandled TypeID.");
    169   case Type::VoidTyID:
    170     return LLVMVoidTypeKind;
    171   case Type::HalfTyID:
    172     return LLVMHalfTypeKind;
    173   case Type::FloatTyID:
    174     return LLVMFloatTypeKind;
    175   case Type::DoubleTyID:
    176     return LLVMDoubleTypeKind;
    177   case Type::X86_FP80TyID:
    178     return LLVMX86_FP80TypeKind;
    179   case Type::FP128TyID:
    180     return LLVMFP128TypeKind;
    181   case Type::PPC_FP128TyID:
    182     return LLVMPPC_FP128TypeKind;
    183   case Type::LabelTyID:
    184     return LLVMLabelTypeKind;
    185   case Type::MetadataTyID:
    186     return LLVMMetadataTypeKind;
    187   case Type::IntegerTyID:
    188     return LLVMIntegerTypeKind;
    189   case Type::FunctionTyID:
    190     return LLVMFunctionTypeKind;
    191   case Type::StructTyID:
    192     return LLVMStructTypeKind;
    193   case Type::ArrayTyID:
    194     return LLVMArrayTypeKind;
    195   case Type::PointerTyID:
    196     return LLVMPointerTypeKind;
    197   case Type::VectorTyID:
    198     return LLVMVectorTypeKind;
    199   case Type::X86_MMXTyID:
    200     return LLVMX86_MMXTypeKind;
    201   }
    202 }
    203 
    204 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
    205 {
    206     return unwrap(Ty)->isSized();
    207 }
    208 
    209 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
    210   return wrap(&unwrap(Ty)->getContext());
    211 }
    212 
    213 /*--.. Operations on integer types .........................................--*/
    214 
    215 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C)  {
    216   return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
    217 }
    218 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C)  {
    219   return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
    220 }
    221 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
    222   return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
    223 }
    224 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
    225   return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
    226 }
    227 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
    228   return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
    229 }
    230 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
    231   return wrap(IntegerType::get(*unwrap(C), NumBits));
    232 }
    233 
    234 LLVMTypeRef LLVMInt1Type(void)  {
    235   return LLVMInt1TypeInContext(LLVMGetGlobalContext());
    236 }
    237 LLVMTypeRef LLVMInt8Type(void)  {
    238   return LLVMInt8TypeInContext(LLVMGetGlobalContext());
    239 }
    240 LLVMTypeRef LLVMInt16Type(void) {
    241   return LLVMInt16TypeInContext(LLVMGetGlobalContext());
    242 }
    243 LLVMTypeRef LLVMInt32Type(void) {
    244   return LLVMInt32TypeInContext(LLVMGetGlobalContext());
    245 }
    246 LLVMTypeRef LLVMInt64Type(void) {
    247   return LLVMInt64TypeInContext(LLVMGetGlobalContext());
    248 }
    249 LLVMTypeRef LLVMIntType(unsigned NumBits) {
    250   return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
    251 }
    252 
    253 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
    254   return unwrap<IntegerType>(IntegerTy)->getBitWidth();
    255 }
    256 
    257 /*--.. Operations on real types ............................................--*/
    258 
    259 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
    260   return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
    261 }
    262 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
    263   return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
    264 }
    265 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
    266   return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
    267 }
    268 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
    269   return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
    270 }
    271 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
    272   return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
    273 }
    274 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
    275   return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
    276 }
    277 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
    278   return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
    279 }
    280 
    281 LLVMTypeRef LLVMHalfType(void) {
    282   return LLVMHalfTypeInContext(LLVMGetGlobalContext());
    283 }
    284 LLVMTypeRef LLVMFloatType(void) {
    285   return LLVMFloatTypeInContext(LLVMGetGlobalContext());
    286 }
    287 LLVMTypeRef LLVMDoubleType(void) {
    288   return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
    289 }
    290 LLVMTypeRef LLVMX86FP80Type(void) {
    291   return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
    292 }
    293 LLVMTypeRef LLVMFP128Type(void) {
    294   return LLVMFP128TypeInContext(LLVMGetGlobalContext());
    295 }
    296 LLVMTypeRef LLVMPPCFP128Type(void) {
    297   return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
    298 }
    299 LLVMTypeRef LLVMX86MMXType(void) {
    300   return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
    301 }
    302 
    303 /*--.. Operations on function types ........................................--*/
    304 
    305 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
    306                              LLVMTypeRef *ParamTypes, unsigned ParamCount,
    307                              LLVMBool IsVarArg) {
    308   ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
    309   return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
    310 }
    311 
    312 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
    313   return unwrap<FunctionType>(FunctionTy)->isVarArg();
    314 }
    315 
    316 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
    317   return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
    318 }
    319 
    320 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
    321   return unwrap<FunctionType>(FunctionTy)->getNumParams();
    322 }
    323 
    324 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
    325   FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
    326   for (FunctionType::param_iterator I = Ty->param_begin(),
    327                                     E = Ty->param_end(); I != E; ++I)
    328     *Dest++ = wrap(*I);
    329 }
    330 
    331 /*--.. Operations on struct types ..........................................--*/
    332 
    333 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
    334                            unsigned ElementCount, LLVMBool Packed) {
    335   ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
    336   return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
    337 }
    338 
    339 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
    340                            unsigned ElementCount, LLVMBool Packed) {
    341   return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
    342                                  ElementCount, Packed);
    343 }
    344 
    345 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
    346 {
    347   return wrap(StructType::create(*unwrap(C), Name));
    348 }
    349 
    350 const char *LLVMGetStructName(LLVMTypeRef Ty)
    351 {
    352   StructType *Type = unwrap<StructType>(Ty);
    353   if (!Type->hasName())
    354     return 0;
    355   return Type->getName().data();
    356 }
    357 
    358 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
    359                        unsigned ElementCount, LLVMBool Packed) {
    360   ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
    361   unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
    362 }
    363 
    364 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
    365   return unwrap<StructType>(StructTy)->getNumElements();
    366 }
    367 
    368 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
    369   StructType *Ty = unwrap<StructType>(StructTy);
    370   for (StructType::element_iterator I = Ty->element_begin(),
    371                                     E = Ty->element_end(); I != E; ++I)
    372     *Dest++ = wrap(*I);
    373 }
    374 
    375 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
    376   return unwrap<StructType>(StructTy)->isPacked();
    377 }
    378 
    379 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
    380   return unwrap<StructType>(StructTy)->isOpaque();
    381 }
    382 
    383 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
    384   return wrap(unwrap(M)->getTypeByName(Name));
    385 }
    386 
    387 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
    388 
    389 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
    390   return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
    391 }
    392 
    393 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
    394   return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
    395 }
    396 
    397 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
    398   return wrap(VectorType::get(unwrap(ElementType), ElementCount));
    399 }
    400 
    401 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
    402   return wrap(unwrap<SequentialType>(Ty)->getElementType());
    403 }
    404 
    405 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
    406   return unwrap<ArrayType>(ArrayTy)->getNumElements();
    407 }
    408 
    409 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
    410   return unwrap<PointerType>(PointerTy)->getAddressSpace();
    411 }
    412 
    413 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
    414   return unwrap<VectorType>(VectorTy)->getNumElements();
    415 }
    416 
    417 /*--.. Operations on other types ...........................................--*/
    418 
    419 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C)  {
    420   return wrap(Type::getVoidTy(*unwrap(C)));
    421 }
    422 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
    423   return wrap(Type::getLabelTy(*unwrap(C)));
    424 }
    425 
    426 LLVMTypeRef LLVMVoidType(void)  {
    427   return LLVMVoidTypeInContext(LLVMGetGlobalContext());
    428 }
    429 LLVMTypeRef LLVMLabelType(void) {
    430   return LLVMLabelTypeInContext(LLVMGetGlobalContext());
    431 }
    432 
    433 /*===-- Operations on values ----------------------------------------------===*/
    434 
    435 /*--.. Operations on all values ............................................--*/
    436 
    437 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
    438   return wrap(unwrap(Val)->getType());
    439 }
    440 
    441 const char *LLVMGetValueName(LLVMValueRef Val) {
    442   return unwrap(Val)->getName().data();
    443 }
    444 
    445 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
    446   unwrap(Val)->setName(Name);
    447 }
    448 
    449 void LLVMDumpValue(LLVMValueRef Val) {
    450   unwrap(Val)->dump();
    451 }
    452 
    453 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
    454   unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
    455 }
    456 
    457 int LLVMHasMetadata(LLVMValueRef Inst) {
    458   return unwrap<Instruction>(Inst)->hasMetadata();
    459 }
    460 
    461 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
    462   return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
    463 }
    464 
    465 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
    466   unwrap<Instruction>(Inst)->setMetadata(KindID, MD? unwrap<MDNode>(MD) : NULL);
    467 }
    468 
    469 /*--.. Conversion functions ................................................--*/
    470 
    471 #define LLVM_DEFINE_VALUE_CAST(name)                                       \
    472   LLVMValueRef LLVMIsA##name(LLVMValueRef Val) {                           \
    473     return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
    474   }
    475 
    476 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
    477 
    478 /*--.. Operations on Uses ..................................................--*/
    479 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
    480   Value *V = unwrap(Val);
    481   Value::use_iterator I = V->use_begin();
    482   if (I == V->use_end())
    483     return 0;
    484   return wrap(&(I.getUse()));
    485 }
    486 
    487 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
    488   Use *Next = unwrap(U)->getNext();
    489   if (Next)
    490     return wrap(Next);
    491   return 0;
    492 }
    493 
    494 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
    495   return wrap(unwrap(U)->getUser());
    496 }
    497 
    498 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
    499   return wrap(unwrap(U)->get());
    500 }
    501 
    502 /*--.. Operations on Users .................................................--*/
    503 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
    504   Value *V = unwrap(Val);
    505   if (MDNode *MD = dyn_cast<MDNode>(V))
    506       return wrap(MD->getOperand(Index));
    507   return wrap(cast<User>(V)->getOperand(Index));
    508 }
    509 
    510 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
    511   unwrap<User>(Val)->setOperand(Index, unwrap(Op));
    512 }
    513 
    514 int LLVMGetNumOperands(LLVMValueRef Val) {
    515   Value *V = unwrap(Val);
    516   if (MDNode *MD = dyn_cast<MDNode>(V))
    517       return MD->getNumOperands();
    518   return cast<User>(V)->getNumOperands();
    519 }
    520 
    521 /*--.. Operations on constants of any type .................................--*/
    522 
    523 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
    524   return wrap(Constant::getNullValue(unwrap(Ty)));
    525 }
    526 
    527 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
    528   return wrap(Constant::getAllOnesValue(unwrap(Ty)));
    529 }
    530 
    531 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
    532   return wrap(UndefValue::get(unwrap(Ty)));
    533 }
    534 
    535 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
    536   return isa<Constant>(unwrap(Ty));
    537 }
    538 
    539 LLVMBool LLVMIsNull(LLVMValueRef Val) {
    540   if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
    541     return C->isNullValue();
    542   return false;
    543 }
    544 
    545 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
    546   return isa<UndefValue>(unwrap(Val));
    547 }
    548 
    549 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
    550   return
    551       wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
    552 }
    553 
    554 /*--.. Operations on metadata nodes ........................................--*/
    555 
    556 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
    557                                    unsigned SLen) {
    558   return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
    559 }
    560 
    561 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
    562   return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
    563 }
    564 
    565 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
    566                                  unsigned Count) {
    567   return wrap(MDNode::get(*unwrap(C),
    568                           makeArrayRef(unwrap<Value>(Vals, Count), Count)));
    569 }
    570 
    571 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
    572   return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
    573 }
    574 
    575 const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
    576   if (const MDString *S = dyn_cast<MDString>(unwrap(V))) {
    577     *Len = S->getString().size();
    578     return S->getString().data();
    579   }
    580   *Len = 0;
    581   return 0;
    582 }
    583 
    584 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V)
    585 {
    586   return cast<MDNode>(unwrap(V))->getNumOperands();
    587 }
    588 
    589 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest)
    590 {
    591   const MDNode *N = cast<MDNode>(unwrap(V));
    592   const unsigned numOperands = N->getNumOperands();
    593   for (unsigned i = 0; i < numOperands; i++)
    594     Dest[i] = wrap(N->getOperand(i));
    595 }
    596 
    597 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
    598 {
    599   if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
    600     return N->getNumOperands();
    601   }
    602   return 0;
    603 }
    604 
    605 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
    606 {
    607   NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
    608   if (!N)
    609     return;
    610   for (unsigned i=0;i<N->getNumOperands();i++)
    611     Dest[i] = wrap(N->getOperand(i));
    612 }
    613 
    614 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
    615                                  LLVMValueRef Val)
    616 {
    617   NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
    618   if (!N)
    619     return;
    620   MDNode *Op = Val ? unwrap<MDNode>(Val) : NULL;
    621   if (Op)
    622     N->addOperand(Op);
    623 }
    624 
    625 /*--.. Operations on scalar constants ......................................--*/
    626 
    627 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
    628                           LLVMBool SignExtend) {
    629   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
    630 }
    631 
    632 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
    633                                               unsigned NumWords,
    634                                               const uint64_t Words[]) {
    635     IntegerType *Ty = unwrap<IntegerType>(IntTy);
    636     return wrap(ConstantInt::get(Ty->getContext(),
    637                                  APInt(Ty->getBitWidth(),
    638                                        makeArrayRef(Words, NumWords))));
    639 }
    640 
    641 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
    642                                   uint8_t Radix) {
    643   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
    644                                Radix));
    645 }
    646 
    647 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
    648                                          unsigned SLen, uint8_t Radix) {
    649   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
    650                                Radix));
    651 }
    652 
    653 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
    654   return wrap(ConstantFP::get(unwrap(RealTy), N));
    655 }
    656 
    657 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
    658   return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
    659 }
    660 
    661 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
    662                                           unsigned SLen) {
    663   return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
    664 }
    665 
    666 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
    667   return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
    668 }
    669 
    670 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
    671   return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
    672 }
    673 
    674 /*--.. Operations on composite constants ...................................--*/
    675 
    676 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
    677                                       unsigned Length,
    678                                       LLVMBool DontNullTerminate) {
    679   /* Inverted the sense of AddNull because ', 0)' is a
    680      better mnemonic for null termination than ', 1)'. */
    681   return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
    682                                            DontNullTerminate == 0));
    683 }
    684 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
    685                                       LLVMValueRef *ConstantVals,
    686                                       unsigned Count, LLVMBool Packed) {
    687   Constant **Elements = unwrap<Constant>(ConstantVals, Count);
    688   return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
    689                                       Packed != 0));
    690 }
    691 
    692 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
    693                              LLVMBool DontNullTerminate) {
    694   return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
    695                                   DontNullTerminate);
    696 }
    697 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
    698                             LLVMValueRef *ConstantVals, unsigned Length) {
    699   ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
    700   return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
    701 }
    702 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
    703                              LLVMBool Packed) {
    704   return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
    705                                   Packed);
    706 }
    707 
    708 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
    709                                   LLVMValueRef *ConstantVals,
    710                                   unsigned Count) {
    711   Constant **Elements = unwrap<Constant>(ConstantVals, Count);
    712   StructType *Ty = cast<StructType>(unwrap(StructTy));
    713 
    714   return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
    715 }
    716 
    717 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
    718   return wrap(ConstantVector::get(makeArrayRef(
    719                             unwrap<Constant>(ScalarConstantVals, Size), Size)));
    720 }
    721 
    722 /*-- Opcode mapping */
    723 
    724 static LLVMOpcode map_to_llvmopcode(int opcode)
    725 {
    726     switch (opcode) {
    727       default: llvm_unreachable("Unhandled Opcode.");
    728 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
    729 #include "llvm/IR/Instruction.def"
    730 #undef HANDLE_INST
    731     }
    732 }
    733 
    734 static int map_from_llvmopcode(LLVMOpcode code)
    735 {
    736     switch (code) {
    737 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
    738 #include "llvm/IR/Instruction.def"
    739 #undef HANDLE_INST
    740     }
    741     llvm_unreachable("Unhandled Opcode.");
    742 }
    743 
    744 /*--.. Constant expressions ................................................--*/
    745 
    746 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
    747   return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
    748 }
    749 
    750 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
    751   return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
    752 }
    753 
    754 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
    755   return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
    756 }
    757 
    758 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
    759   return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
    760 }
    761 
    762 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
    763   return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
    764 }
    765 
    766 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
    767   return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
    768 }
    769 
    770 
    771 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
    772   return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
    773 }
    774 
    775 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
    776   return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
    777 }
    778 
    779 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
    780   return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
    781                                    unwrap<Constant>(RHSConstant)));
    782 }
    783 
    784 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
    785                              LLVMValueRef RHSConstant) {
    786   return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
    787                                       unwrap<Constant>(RHSConstant)));
    788 }
    789 
    790 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
    791                              LLVMValueRef RHSConstant) {
    792   return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
    793                                       unwrap<Constant>(RHSConstant)));
    794 }
    795 
    796 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
    797   return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
    798                                     unwrap<Constant>(RHSConstant)));
    799 }
    800 
    801 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
    802   return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
    803                                    unwrap<Constant>(RHSConstant)));
    804 }
    805 
    806 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
    807                              LLVMValueRef RHSConstant) {
    808   return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
    809                                       unwrap<Constant>(RHSConstant)));
    810 }
    811 
    812 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
    813                              LLVMValueRef RHSConstant) {
    814   return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
    815                                       unwrap<Constant>(RHSConstant)));
    816 }
    817 
    818 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
    819   return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
    820                                     unwrap<Constant>(RHSConstant)));
    821 }
    822 
    823 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
    824   return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
    825                                    unwrap<Constant>(RHSConstant)));
    826 }
    827 
    828 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
    829                              LLVMValueRef RHSConstant) {
    830   return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
    831                                       unwrap<Constant>(RHSConstant)));
    832 }
    833 
    834 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
    835                              LLVMValueRef RHSConstant) {
    836   return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
    837                                       unwrap<Constant>(RHSConstant)));
    838 }
    839 
    840 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
    841   return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
    842                                     unwrap<Constant>(RHSConstant)));
    843 }
    844 
    845 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
    846   return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
    847                                     unwrap<Constant>(RHSConstant)));
    848 }
    849 
    850 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
    851   return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
    852                                     unwrap<Constant>(RHSConstant)));
    853 }
    854 
    855 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
    856                                 LLVMValueRef RHSConstant) {
    857   return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
    858                                          unwrap<Constant>(RHSConstant)));
    859 }
    860 
    861 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
    862   return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
    863                                     unwrap<Constant>(RHSConstant)));
    864 }
    865 
    866 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
    867   return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
    868                                     unwrap<Constant>(RHSConstant)));
    869 }
    870 
    871 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
    872   return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
    873                                     unwrap<Constant>(RHSConstant)));
    874 }
    875 
    876 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
    877   return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
    878                                     unwrap<Constant>(RHSConstant)));
    879 }
    880 
    881 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
    882   return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
    883                                    unwrap<Constant>(RHSConstant)));
    884 }
    885 
    886 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
    887   return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
    888                                   unwrap<Constant>(RHSConstant)));
    889 }
    890 
    891 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
    892   return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
    893                                    unwrap<Constant>(RHSConstant)));
    894 }
    895 
    896 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
    897                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
    898   return wrap(ConstantExpr::getICmp(Predicate,
    899                                     unwrap<Constant>(LHSConstant),
    900                                     unwrap<Constant>(RHSConstant)));
    901 }
    902 
    903 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
    904                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
    905   return wrap(ConstantExpr::getFCmp(Predicate,
    906                                     unwrap<Constant>(LHSConstant),
    907                                     unwrap<Constant>(RHSConstant)));
    908 }
    909 
    910 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
    911   return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
    912                                    unwrap<Constant>(RHSConstant)));
    913 }
    914 
    915 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
    916   return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
    917                                     unwrap<Constant>(RHSConstant)));
    918 }
    919 
    920 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
    921   return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
    922                                     unwrap<Constant>(RHSConstant)));
    923 }
    924 
    925 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
    926                           LLVMValueRef *ConstantIndices, unsigned NumIndices) {
    927   ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
    928                                NumIndices);
    929   return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
    930                                              IdxList));
    931 }
    932 
    933 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
    934                                   LLVMValueRef *ConstantIndices,
    935                                   unsigned NumIndices) {
    936   Constant* Val = unwrap<Constant>(ConstantVal);
    937   ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
    938                                NumIndices);
    939   return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
    940 }
    941 
    942 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
    943   return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
    944                                      unwrap(ToType)));
    945 }
    946 
    947 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
    948   return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
    949                                     unwrap(ToType)));
    950 }
    951 
    952 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
    953   return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
    954                                     unwrap(ToType)));
    955 }
    956 
    957 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
    958   return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
    959                                        unwrap(ToType)));
    960 }
    961 
    962 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
    963   return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
    964                                         unwrap(ToType)));
    965 }
    966 
    967 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
    968   return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
    969                                       unwrap(ToType)));
    970 }
    971 
    972 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
    973   return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
    974                                       unwrap(ToType)));
    975 }
    976 
    977 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
    978   return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
    979                                       unwrap(ToType)));
    980 }
    981 
    982 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
    983   return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
    984                                       unwrap(ToType)));
    985 }
    986 
    987 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
    988   return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
    989                                         unwrap(ToType)));
    990 }
    991 
    992 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
    993   return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
    994                                         unwrap(ToType)));
    995 }
    996 
    997 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
    998   return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
    999                                        unwrap(ToType)));
   1000 }
   1001 
   1002 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
   1003                                     LLVMTypeRef ToType) {
   1004   return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
   1005                                              unwrap(ToType)));
   1006 }
   1007 
   1008 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
   1009                                     LLVMTypeRef ToType) {
   1010   return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
   1011                                              unwrap(ToType)));
   1012 }
   1013 
   1014 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
   1015                                      LLVMTypeRef ToType) {
   1016   return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
   1017                                               unwrap(ToType)));
   1018 }
   1019 
   1020 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
   1021                                   LLVMTypeRef ToType) {
   1022   return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
   1023                                            unwrap(ToType)));
   1024 }
   1025 
   1026 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
   1027                               LLVMBool isSigned) {
   1028   return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
   1029                                            unwrap(ToType), isSigned));
   1030 }
   1031 
   1032 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
   1033   return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
   1034                                       unwrap(ToType)));
   1035 }
   1036 
   1037 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
   1038                              LLVMValueRef ConstantIfTrue,
   1039                              LLVMValueRef ConstantIfFalse) {
   1040   return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
   1041                                       unwrap<Constant>(ConstantIfTrue),
   1042                                       unwrap<Constant>(ConstantIfFalse)));
   1043 }
   1044 
   1045 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
   1046                                      LLVMValueRef IndexConstant) {
   1047   return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
   1048                                               unwrap<Constant>(IndexConstant)));
   1049 }
   1050 
   1051 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
   1052                                     LLVMValueRef ElementValueConstant,
   1053                                     LLVMValueRef IndexConstant) {
   1054   return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
   1055                                          unwrap<Constant>(ElementValueConstant),
   1056                                              unwrap<Constant>(IndexConstant)));
   1057 }
   1058 
   1059 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
   1060                                     LLVMValueRef VectorBConstant,
   1061                                     LLVMValueRef MaskConstant) {
   1062   return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
   1063                                              unwrap<Constant>(VectorBConstant),
   1064                                              unwrap<Constant>(MaskConstant)));
   1065 }
   1066 
   1067 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
   1068                                    unsigned NumIdx) {
   1069   return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
   1070                                             makeArrayRef(IdxList, NumIdx)));
   1071 }
   1072 
   1073 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
   1074                                   LLVMValueRef ElementValueConstant,
   1075                                   unsigned *IdxList, unsigned NumIdx) {
   1076   return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
   1077                                          unwrap<Constant>(ElementValueConstant),
   1078                                            makeArrayRef(IdxList, NumIdx)));
   1079 }
   1080 
   1081 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
   1082                                 const char *Constraints,
   1083                                 LLVMBool HasSideEffects,
   1084                                 LLVMBool IsAlignStack) {
   1085   return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
   1086                              Constraints, HasSideEffects, IsAlignStack));
   1087 }
   1088 
   1089 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
   1090   return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
   1091 }
   1092 
   1093 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
   1094 
   1095 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
   1096   return wrap(unwrap<GlobalValue>(Global)->getParent());
   1097 }
   1098 
   1099 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
   1100   return unwrap<GlobalValue>(Global)->isDeclaration();
   1101 }
   1102 
   1103 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
   1104   switch (unwrap<GlobalValue>(Global)->getLinkage()) {
   1105   case GlobalValue::ExternalLinkage:
   1106     return LLVMExternalLinkage;
   1107   case GlobalValue::AvailableExternallyLinkage:
   1108     return LLVMAvailableExternallyLinkage;
   1109   case GlobalValue::LinkOnceAnyLinkage:
   1110     return LLVMLinkOnceAnyLinkage;
   1111   case GlobalValue::LinkOnceODRLinkage:
   1112     return LLVMLinkOnceODRLinkage;
   1113   case GlobalValue::LinkOnceODRAutoHideLinkage:
   1114     return LLVMLinkOnceODRAutoHideLinkage;
   1115   case GlobalValue::WeakAnyLinkage:
   1116     return LLVMWeakAnyLinkage;
   1117   case GlobalValue::WeakODRLinkage:
   1118     return LLVMWeakODRLinkage;
   1119   case GlobalValue::AppendingLinkage:
   1120     return LLVMAppendingLinkage;
   1121   case GlobalValue::InternalLinkage:
   1122     return LLVMInternalLinkage;
   1123   case GlobalValue::PrivateLinkage:
   1124     return LLVMPrivateLinkage;
   1125   case GlobalValue::LinkerPrivateLinkage:
   1126     return LLVMLinkerPrivateLinkage;
   1127   case GlobalValue::LinkerPrivateWeakLinkage:
   1128     return LLVMLinkerPrivateWeakLinkage;
   1129   case GlobalValue::DLLImportLinkage:
   1130     return LLVMDLLImportLinkage;
   1131   case GlobalValue::DLLExportLinkage:
   1132     return LLVMDLLExportLinkage;
   1133   case GlobalValue::ExternalWeakLinkage:
   1134     return LLVMExternalWeakLinkage;
   1135   case GlobalValue::CommonLinkage:
   1136     return LLVMCommonLinkage;
   1137   }
   1138 
   1139   llvm_unreachable("Invalid GlobalValue linkage!");
   1140 }
   1141 
   1142 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
   1143   GlobalValue *GV = unwrap<GlobalValue>(Global);
   1144 
   1145   switch (Linkage) {
   1146   case LLVMExternalLinkage:
   1147     GV->setLinkage(GlobalValue::ExternalLinkage);
   1148     break;
   1149   case LLVMAvailableExternallyLinkage:
   1150     GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
   1151     break;
   1152   case LLVMLinkOnceAnyLinkage:
   1153     GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
   1154     break;
   1155   case LLVMLinkOnceODRLinkage:
   1156     GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
   1157     break;
   1158   case LLVMLinkOnceODRAutoHideLinkage:
   1159     GV->setLinkage(GlobalValue::LinkOnceODRAutoHideLinkage);
   1160     break;
   1161   case LLVMWeakAnyLinkage:
   1162     GV->setLinkage(GlobalValue::WeakAnyLinkage);
   1163     break;
   1164   case LLVMWeakODRLinkage:
   1165     GV->setLinkage(GlobalValue::WeakODRLinkage);
   1166     break;
   1167   case LLVMAppendingLinkage:
   1168     GV->setLinkage(GlobalValue::AppendingLinkage);
   1169     break;
   1170   case LLVMInternalLinkage:
   1171     GV->setLinkage(GlobalValue::InternalLinkage);
   1172     break;
   1173   case LLVMPrivateLinkage:
   1174     GV->setLinkage(GlobalValue::PrivateLinkage);
   1175     break;
   1176   case LLVMLinkerPrivateLinkage:
   1177     GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
   1178     break;
   1179   case LLVMLinkerPrivateWeakLinkage:
   1180     GV->setLinkage(GlobalValue::LinkerPrivateWeakLinkage);
   1181     break;
   1182   case LLVMDLLImportLinkage:
   1183     GV->setLinkage(GlobalValue::DLLImportLinkage);
   1184     break;
   1185   case LLVMDLLExportLinkage:
   1186     GV->setLinkage(GlobalValue::DLLExportLinkage);
   1187     break;
   1188   case LLVMExternalWeakLinkage:
   1189     GV->setLinkage(GlobalValue::ExternalWeakLinkage);
   1190     break;
   1191   case LLVMGhostLinkage:
   1192     DEBUG(errs()
   1193           << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
   1194     break;
   1195   case LLVMCommonLinkage:
   1196     GV->setLinkage(GlobalValue::CommonLinkage);
   1197     break;
   1198   }
   1199 }
   1200 
   1201 const char *LLVMGetSection(LLVMValueRef Global) {
   1202   return unwrap<GlobalValue>(Global)->getSection().c_str();
   1203 }
   1204 
   1205 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
   1206   unwrap<GlobalValue>(Global)->setSection(Section);
   1207 }
   1208 
   1209 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
   1210   return static_cast<LLVMVisibility>(
   1211     unwrap<GlobalValue>(Global)->getVisibility());
   1212 }
   1213 
   1214 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
   1215   unwrap<GlobalValue>(Global)
   1216     ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
   1217 }
   1218 
   1219 unsigned LLVMGetAlignment(LLVMValueRef Global) {
   1220   return unwrap<GlobalValue>(Global)->getAlignment();
   1221 }
   1222 
   1223 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
   1224   unwrap<GlobalValue>(Global)->setAlignment(Bytes);
   1225 }
   1226 
   1227 /*--.. Operations on global variables ......................................--*/
   1228 
   1229 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
   1230   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
   1231                                  GlobalValue::ExternalLinkage, 0, Name));
   1232 }
   1233 
   1234 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
   1235                                          const char *Name,
   1236                                          unsigned AddressSpace) {
   1237   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
   1238                                  GlobalValue::ExternalLinkage, 0, Name, 0,
   1239                                  GlobalVariable::NotThreadLocal, AddressSpace));
   1240 }
   1241 
   1242 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
   1243   return wrap(unwrap(M)->getNamedGlobal(Name));
   1244 }
   1245 
   1246 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
   1247   Module *Mod = unwrap(M);
   1248   Module::global_iterator I = Mod->global_begin();
   1249   if (I == Mod->global_end())
   1250     return 0;
   1251   return wrap(I);
   1252 }
   1253 
   1254 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
   1255   Module *Mod = unwrap(M);
   1256   Module::global_iterator I = Mod->global_end();
   1257   if (I == Mod->global_begin())
   1258     return 0;
   1259   return wrap(--I);
   1260 }
   1261 
   1262 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
   1263   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
   1264   Module::global_iterator I = GV;
   1265   if (++I == GV->getParent()->global_end())
   1266     return 0;
   1267   return wrap(I);
   1268 }
   1269 
   1270 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
   1271   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
   1272   Module::global_iterator I = GV;
   1273   if (I == GV->getParent()->global_begin())
   1274     return 0;
   1275   return wrap(--I);
   1276 }
   1277 
   1278 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
   1279   unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
   1280 }
   1281 
   1282 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
   1283   GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
   1284   if ( !GV->hasInitializer() )
   1285     return 0;
   1286   return wrap(GV->getInitializer());
   1287 }
   1288 
   1289 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
   1290   unwrap<GlobalVariable>(GlobalVar)
   1291     ->setInitializer(unwrap<Constant>(ConstantVal));
   1292 }
   1293 
   1294 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
   1295   return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
   1296 }
   1297 
   1298 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
   1299   unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
   1300 }
   1301 
   1302 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
   1303   return unwrap<GlobalVariable>(GlobalVar)->isConstant();
   1304 }
   1305 
   1306 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
   1307   unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
   1308 }
   1309 
   1310 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
   1311   switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
   1312   case GlobalVariable::NotThreadLocal:
   1313     return LLVMNotThreadLocal;
   1314   case GlobalVariable::GeneralDynamicTLSModel:
   1315     return LLVMGeneralDynamicTLSModel;
   1316   case GlobalVariable::LocalDynamicTLSModel:
   1317     return LLVMLocalDynamicTLSModel;
   1318   case GlobalVariable::InitialExecTLSModel:
   1319     return LLVMInitialExecTLSModel;
   1320   case GlobalVariable::LocalExecTLSModel:
   1321     return LLVMLocalExecTLSModel;
   1322   }
   1323 
   1324   llvm_unreachable("Invalid GlobalVariable thread local mode");
   1325 }
   1326 
   1327 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
   1328   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
   1329 
   1330   switch (Mode) {
   1331   case LLVMNotThreadLocal:
   1332     GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
   1333     break;
   1334   case LLVMGeneralDynamicTLSModel:
   1335     GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
   1336     break;
   1337   case LLVMLocalDynamicTLSModel:
   1338     GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
   1339     break;
   1340   case LLVMInitialExecTLSModel:
   1341     GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
   1342     break;
   1343   case LLVMLocalExecTLSModel:
   1344     GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
   1345     break;
   1346   }
   1347 }
   1348 
   1349 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
   1350   return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
   1351 }
   1352 
   1353 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
   1354   unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
   1355 }
   1356 
   1357 /*--.. Operations on aliases ......................................--*/
   1358 
   1359 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
   1360                           const char *Name) {
   1361   return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
   1362                               unwrap<Constant>(Aliasee), unwrap (M)));
   1363 }
   1364 
   1365 /*--.. Operations on functions .............................................--*/
   1366 
   1367 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
   1368                              LLVMTypeRef FunctionTy) {
   1369   return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
   1370                                GlobalValue::ExternalLinkage, Name, unwrap(M)));
   1371 }
   1372 
   1373 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
   1374   return wrap(unwrap(M)->getFunction(Name));
   1375 }
   1376 
   1377 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
   1378   Module *Mod = unwrap(M);
   1379   Module::iterator I = Mod->begin();
   1380   if (I == Mod->end())
   1381     return 0;
   1382   return wrap(I);
   1383 }
   1384 
   1385 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
   1386   Module *Mod = unwrap(M);
   1387   Module::iterator I = Mod->end();
   1388   if (I == Mod->begin())
   1389     return 0;
   1390   return wrap(--I);
   1391 }
   1392 
   1393 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
   1394   Function *Func = unwrap<Function>(Fn);
   1395   Module::iterator I = Func;
   1396   if (++I == Func->getParent()->end())
   1397     return 0;
   1398   return wrap(I);
   1399 }
   1400 
   1401 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
   1402   Function *Func = unwrap<Function>(Fn);
   1403   Module::iterator I = Func;
   1404   if (I == Func->getParent()->begin())
   1405     return 0;
   1406   return wrap(--I);
   1407 }
   1408 
   1409 void LLVMDeleteFunction(LLVMValueRef Fn) {
   1410   unwrap<Function>(Fn)->eraseFromParent();
   1411 }
   1412 
   1413 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
   1414   if (Function *F = dyn_cast<Function>(unwrap(Fn)))
   1415     return F->getIntrinsicID();
   1416   return 0;
   1417 }
   1418 
   1419 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
   1420   return unwrap<Function>(Fn)->getCallingConv();
   1421 }
   1422 
   1423 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
   1424   return unwrap<Function>(Fn)->setCallingConv(
   1425     static_cast<CallingConv::ID>(CC));
   1426 }
   1427 
   1428 const char *LLVMGetGC(LLVMValueRef Fn) {
   1429   Function *F = unwrap<Function>(Fn);
   1430   return F->hasGC()? F->getGC() : 0;
   1431 }
   1432 
   1433 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
   1434   Function *F = unwrap<Function>(Fn);
   1435   if (GC)
   1436     F->setGC(GC);
   1437   else
   1438     F->clearGC();
   1439 }
   1440 
   1441 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
   1442   Function *Func = unwrap<Function>(Fn);
   1443   const AttributeSet PAL = Func->getAttributes();
   1444   AttrBuilder B(PA);
   1445   const AttributeSet PALnew =
   1446     PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
   1447                       AttributeSet::get(Func->getContext(),
   1448                                         AttributeSet::FunctionIndex, B));
   1449   Func->setAttributes(PALnew);
   1450 }
   1451 
   1452 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
   1453                                         const char *V) {
   1454   Function *Func = unwrap<Function>(Fn);
   1455   AttributeSet::AttrIndex Idx =
   1456     AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
   1457   AttrBuilder B;
   1458 
   1459   B.addAttribute(A, V);
   1460   AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
   1461   Func->addAttributes(Idx, Set);
   1462 }
   1463 
   1464 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
   1465   Function *Func = unwrap<Function>(Fn);
   1466   const AttributeSet PAL = Func->getAttributes();
   1467   AttrBuilder B(PA);
   1468   const AttributeSet PALnew =
   1469     PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
   1470                          AttributeSet::get(Func->getContext(),
   1471                                            AttributeSet::FunctionIndex, B));
   1472   Func->setAttributes(PALnew);
   1473 }
   1474 
   1475 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
   1476   Function *Func = unwrap<Function>(Fn);
   1477   const AttributeSet PAL = Func->getAttributes();
   1478   return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
   1479 }
   1480 
   1481 /*--.. Operations on parameters ............................................--*/
   1482 
   1483 unsigned LLVMCountParams(LLVMValueRef FnRef) {
   1484   // This function is strictly redundant to
   1485   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
   1486   return unwrap<Function>(FnRef)->arg_size();
   1487 }
   1488 
   1489 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
   1490   Function *Fn = unwrap<Function>(FnRef);
   1491   for (Function::arg_iterator I = Fn->arg_begin(),
   1492                               E = Fn->arg_end(); I != E; I++)
   1493     *ParamRefs++ = wrap(I);
   1494 }
   1495 
   1496 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
   1497   Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
   1498   while (index --> 0)
   1499     AI++;
   1500   return wrap(AI);
   1501 }
   1502 
   1503 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
   1504   return wrap(unwrap<Argument>(V)->getParent());
   1505 }
   1506 
   1507 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
   1508   Function *Func = unwrap<Function>(Fn);
   1509   Function::arg_iterator I = Func->arg_begin();
   1510   if (I == Func->arg_end())
   1511     return 0;
   1512   return wrap(I);
   1513 }
   1514 
   1515 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
   1516   Function *Func = unwrap<Function>(Fn);
   1517   Function::arg_iterator I = Func->arg_end();
   1518   if (I == Func->arg_begin())
   1519     return 0;
   1520   return wrap(--I);
   1521 }
   1522 
   1523 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
   1524   Argument *A = unwrap<Argument>(Arg);
   1525   Function::arg_iterator I = A;
   1526   if (++I == A->getParent()->arg_end())
   1527     return 0;
   1528   return wrap(I);
   1529 }
   1530 
   1531 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
   1532   Argument *A = unwrap<Argument>(Arg);
   1533   Function::arg_iterator I = A;
   1534   if (I == A->getParent()->arg_begin())
   1535     return 0;
   1536   return wrap(--I);
   1537 }
   1538 
   1539 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
   1540   Argument *A = unwrap<Argument>(Arg);
   1541   AttrBuilder B(PA);
   1542   A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1,  B));
   1543 }
   1544 
   1545 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
   1546   Argument *A = unwrap<Argument>(Arg);
   1547   AttrBuilder B(PA);
   1548   A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1,  B));
   1549 }
   1550 
   1551 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
   1552   Argument *A = unwrap<Argument>(Arg);
   1553   return (LLVMAttribute)A->getParent()->getAttributes().
   1554     Raw(A->getArgNo()+1);
   1555 }
   1556 
   1557 
   1558 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
   1559   Argument *A = unwrap<Argument>(Arg);
   1560   AttrBuilder B;
   1561   B.addAlignmentAttr(align);
   1562   A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
   1563 }
   1564 
   1565 /*--.. Operations on basic blocks ..........................................--*/
   1566 
   1567 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
   1568   return wrap(static_cast<Value*>(unwrap(BB)));
   1569 }
   1570 
   1571 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
   1572   return isa<BasicBlock>(unwrap(Val));
   1573 }
   1574 
   1575 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
   1576   return wrap(unwrap<BasicBlock>(Val));
   1577 }
   1578 
   1579 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
   1580   return wrap(unwrap(BB)->getParent());
   1581 }
   1582 
   1583 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
   1584   return wrap(unwrap(BB)->getTerminator());
   1585 }
   1586 
   1587 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
   1588   return unwrap<Function>(FnRef)->size();
   1589 }
   1590 
   1591 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
   1592   Function *Fn = unwrap<Function>(FnRef);
   1593   for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
   1594     *BasicBlocksRefs++ = wrap(I);
   1595 }
   1596 
   1597 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
   1598   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
   1599 }
   1600 
   1601 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
   1602   Function *Func = unwrap<Function>(Fn);
   1603   Function::iterator I = Func->begin();
   1604   if (I == Func->end())
   1605     return 0;
   1606   return wrap(I);
   1607 }
   1608 
   1609 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
   1610   Function *Func = unwrap<Function>(Fn);
   1611   Function::iterator I = Func->end();
   1612   if (I == Func->begin())
   1613     return 0;
   1614   return wrap(--I);
   1615 }
   1616 
   1617 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
   1618   BasicBlock *Block = unwrap(BB);
   1619   Function::iterator I = Block;
   1620   if (++I == Block->getParent()->end())
   1621     return 0;
   1622   return wrap(I);
   1623 }
   1624 
   1625 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
   1626   BasicBlock *Block = unwrap(BB);
   1627   Function::iterator I = Block;
   1628   if (I == Block->getParent()->begin())
   1629     return 0;
   1630   return wrap(--I);
   1631 }
   1632 
   1633 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
   1634                                                 LLVMValueRef FnRef,
   1635                                                 const char *Name) {
   1636   return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
   1637 }
   1638 
   1639 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
   1640   return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
   1641 }
   1642 
   1643 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
   1644                                                 LLVMBasicBlockRef BBRef,
   1645                                                 const char *Name) {
   1646   BasicBlock *BB = unwrap(BBRef);
   1647   return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
   1648 }
   1649 
   1650 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
   1651                                        const char *Name) {
   1652   return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
   1653 }
   1654 
   1655 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
   1656   unwrap(BBRef)->eraseFromParent();
   1657 }
   1658 
   1659 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
   1660   unwrap(BBRef)->removeFromParent();
   1661 }
   1662 
   1663 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
   1664   unwrap(BB)->moveBefore(unwrap(MovePos));
   1665 }
   1666 
   1667 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
   1668   unwrap(BB)->moveAfter(unwrap(MovePos));
   1669 }
   1670 
   1671 /*--.. Operations on instructions ..........................................--*/
   1672 
   1673 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
   1674   return wrap(unwrap<Instruction>(Inst)->getParent());
   1675 }
   1676 
   1677 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
   1678   BasicBlock *Block = unwrap(BB);
   1679   BasicBlock::iterator I = Block->begin();
   1680   if (I == Block->end())
   1681     return 0;
   1682   return wrap(I);
   1683 }
   1684 
   1685 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
   1686   BasicBlock *Block = unwrap(BB);
   1687   BasicBlock::iterator I = Block->end();
   1688   if (I == Block->begin())
   1689     return 0;
   1690   return wrap(--I);
   1691 }
   1692 
   1693 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
   1694   Instruction *Instr = unwrap<Instruction>(Inst);
   1695   BasicBlock::iterator I = Instr;
   1696   if (++I == Instr->getParent()->end())
   1697     return 0;
   1698   return wrap(I);
   1699 }
   1700 
   1701 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
   1702   Instruction *Instr = unwrap<Instruction>(Inst);
   1703   BasicBlock::iterator I = Instr;
   1704   if (I == Instr->getParent()->begin())
   1705     return 0;
   1706   return wrap(--I);
   1707 }
   1708 
   1709 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
   1710   unwrap<Instruction>(Inst)->eraseFromParent();
   1711 }
   1712 
   1713 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
   1714   if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
   1715     return (LLVMIntPredicate)I->getPredicate();
   1716   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
   1717     if (CE->getOpcode() == Instruction::ICmp)
   1718       return (LLVMIntPredicate)CE->getPredicate();
   1719   return (LLVMIntPredicate)0;
   1720 }
   1721 
   1722 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
   1723   if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
   1724     return map_to_llvmopcode(C->getOpcode());
   1725   return (LLVMOpcode)0;
   1726 }
   1727 
   1728 /*--.. Call and invoke instructions ........................................--*/
   1729 
   1730 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
   1731   Value *V = unwrap(Instr);
   1732   if (CallInst *CI = dyn_cast<CallInst>(V))
   1733     return CI->getCallingConv();
   1734   if (InvokeInst *II = dyn_cast<InvokeInst>(V))
   1735     return II->getCallingConv();
   1736   llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
   1737 }
   1738 
   1739 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
   1740   Value *V = unwrap(Instr);
   1741   if (CallInst *CI = dyn_cast<CallInst>(V))
   1742     return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
   1743   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
   1744     return II->setCallingConv(static_cast<CallingConv::ID>(CC));
   1745   llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
   1746 }
   1747 
   1748 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
   1749                            LLVMAttribute PA) {
   1750   CallSite Call = CallSite(unwrap<Instruction>(Instr));
   1751   AttrBuilder B(PA);
   1752   Call.setAttributes(
   1753     Call.getAttributes().addAttributes(Call->getContext(), index,
   1754                                        AttributeSet::get(Call->getContext(),
   1755                                                          index, B)));
   1756 }
   1757 
   1758 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
   1759                               LLVMAttribute PA) {
   1760   CallSite Call = CallSite(unwrap<Instruction>(Instr));
   1761   AttrBuilder B(PA);
   1762   Call.setAttributes(Call.getAttributes()
   1763                        .removeAttributes(Call->getContext(), index,
   1764                                          AttributeSet::get(Call->getContext(),
   1765                                                            index, B)));
   1766 }
   1767 
   1768 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
   1769                                 unsigned align) {
   1770   CallSite Call = CallSite(unwrap<Instruction>(Instr));
   1771   AttrBuilder B;
   1772   B.addAlignmentAttr(align);
   1773   Call.setAttributes(Call.getAttributes()
   1774                        .addAttributes(Call->getContext(), index,
   1775                                       AttributeSet::get(Call->getContext(),
   1776                                                         index, B)));
   1777 }
   1778 
   1779 /*--.. Operations on call instructions (only) ..............................--*/
   1780 
   1781 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
   1782   return unwrap<CallInst>(Call)->isTailCall();
   1783 }
   1784 
   1785 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
   1786   unwrap<CallInst>(Call)->setTailCall(isTailCall);
   1787 }
   1788 
   1789 /*--.. Operations on switch instructions (only) ............................--*/
   1790 
   1791 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
   1792   return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
   1793 }
   1794 
   1795 /*--.. Operations on phi nodes .............................................--*/
   1796 
   1797 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
   1798                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
   1799   PHINode *PhiVal = unwrap<PHINode>(PhiNode);
   1800   for (unsigned I = 0; I != Count; ++I)
   1801     PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
   1802 }
   1803 
   1804 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
   1805   return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
   1806 }
   1807 
   1808 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
   1809   return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
   1810 }
   1811 
   1812 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
   1813   return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
   1814 }
   1815 
   1816 
   1817 /*===-- Instruction builders ----------------------------------------------===*/
   1818 
   1819 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
   1820   return wrap(new IRBuilder<>(*unwrap(C)));
   1821 }
   1822 
   1823 LLVMBuilderRef LLVMCreateBuilder(void) {
   1824   return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
   1825 }
   1826 
   1827 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
   1828                          LLVMValueRef Instr) {
   1829   BasicBlock *BB = unwrap(Block);
   1830   Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
   1831   unwrap(Builder)->SetInsertPoint(BB, I);
   1832 }
   1833 
   1834 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
   1835   Instruction *I = unwrap<Instruction>(Instr);
   1836   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
   1837 }
   1838 
   1839 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
   1840   BasicBlock *BB = unwrap(Block);
   1841   unwrap(Builder)->SetInsertPoint(BB);
   1842 }
   1843 
   1844 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
   1845    return wrap(unwrap(Builder)->GetInsertBlock());
   1846 }
   1847 
   1848 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
   1849   unwrap(Builder)->ClearInsertionPoint();
   1850 }
   1851 
   1852 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
   1853   unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
   1854 }
   1855 
   1856 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
   1857                                    const char *Name) {
   1858   unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
   1859 }
   1860 
   1861 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
   1862   delete unwrap(Builder);
   1863 }
   1864 
   1865 /*--.. Metadata builders ...................................................--*/
   1866 
   1867 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
   1868   MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
   1869   unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
   1870 }
   1871 
   1872 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
   1873   return wrap(unwrap(Builder)->getCurrentDebugLocation()
   1874               .getAsMDNode(unwrap(Builder)->getContext()));
   1875 }
   1876 
   1877 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
   1878   unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
   1879 }
   1880 
   1881 
   1882 /*--.. Instruction builders ................................................--*/
   1883 
   1884 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
   1885   return wrap(unwrap(B)->CreateRetVoid());
   1886 }
   1887 
   1888 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
   1889   return wrap(unwrap(B)->CreateRet(unwrap(V)));
   1890 }
   1891 
   1892 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
   1893                                    unsigned N) {
   1894   return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
   1895 }
   1896 
   1897 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
   1898   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
   1899 }
   1900 
   1901 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
   1902                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
   1903   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
   1904 }
   1905 
   1906 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
   1907                              LLVMBasicBlockRef Else, unsigned NumCases) {
   1908   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
   1909 }
   1910 
   1911 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
   1912                                  unsigned NumDests) {
   1913   return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
   1914 }
   1915 
   1916 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
   1917                              LLVMValueRef *Args, unsigned NumArgs,
   1918                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
   1919                              const char *Name) {
   1920   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
   1921                                       makeArrayRef(unwrap(Args), NumArgs),
   1922                                       Name));
   1923 }
   1924 
   1925 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
   1926                                  LLVMValueRef PersFn, unsigned NumClauses,
   1927                                  const char *Name) {
   1928   return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
   1929                                           cast<Function>(unwrap(PersFn)),
   1930                                           NumClauses, Name));
   1931 }
   1932 
   1933 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
   1934   return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
   1935 }
   1936 
   1937 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
   1938   return wrap(unwrap(B)->CreateUnreachable());
   1939 }
   1940 
   1941 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
   1942                  LLVMBasicBlockRef Dest) {
   1943   unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
   1944 }
   1945 
   1946 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
   1947   unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
   1948 }
   1949 
   1950 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
   1951   unwrap<LandingPadInst>(LandingPad)->
   1952     addClause(cast<Constant>(unwrap(ClauseVal)));
   1953 }
   1954 
   1955 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
   1956   unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
   1957 }
   1958 
   1959 /*--.. Arithmetic ..........................................................--*/
   1960 
   1961 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
   1962                           const char *Name) {
   1963   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
   1964 }
   1965 
   1966 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
   1967                           const char *Name) {
   1968   return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
   1969 }
   1970 
   1971 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
   1972                           const char *Name) {
   1973   return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
   1974 }
   1975 
   1976 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
   1977                           const char *Name) {
   1978   return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
   1979 }
   1980 
   1981 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
   1982                           const char *Name) {
   1983   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
   1984 }
   1985 
   1986 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
   1987                           const char *Name) {
   1988   return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
   1989 }
   1990 
   1991 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
   1992                           const char *Name) {
   1993   return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
   1994 }
   1995 
   1996 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
   1997                           const char *Name) {
   1998   return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
   1999 }
   2000 
   2001 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
   2002                           const char *Name) {
   2003   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
   2004 }
   2005 
   2006 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
   2007                           const char *Name) {
   2008   return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
   2009 }
   2010 
   2011 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
   2012                           const char *Name) {
   2013   return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
   2014 }
   2015 
   2016 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
   2017                           const char *Name) {
   2018   return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
   2019 }
   2020 
   2021 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
   2022                            const char *Name) {
   2023   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
   2024 }
   2025 
   2026 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
   2027                            const char *Name) {
   2028   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
   2029 }
   2030 
   2031 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
   2032                                 LLVMValueRef RHS, const char *Name) {
   2033   return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
   2034 }
   2035 
   2036 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
   2037                            const char *Name) {
   2038   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
   2039 }
   2040 
   2041 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
   2042                            const char *Name) {
   2043   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
   2044 }
   2045 
   2046 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
   2047                            const char *Name) {
   2048   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
   2049 }
   2050 
   2051 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
   2052                            const char *Name) {
   2053   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
   2054 }
   2055 
   2056 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
   2057                           const char *Name) {
   2058   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
   2059 }
   2060 
   2061 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
   2062                            const char *Name) {
   2063   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
   2064 }
   2065 
   2066 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
   2067                            const char *Name) {
   2068   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
   2069 }
   2070 
   2071 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
   2072                           const char *Name) {
   2073   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
   2074 }
   2075 
   2076 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
   2077                          const char *Name) {
   2078   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
   2079 }
   2080 
   2081 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
   2082                           const char *Name) {
   2083   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
   2084 }
   2085 
   2086 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
   2087                             LLVMValueRef LHS, LLVMValueRef RHS,
   2088                             const char *Name) {
   2089   return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
   2090                                      unwrap(RHS), Name));
   2091 }
   2092 
   2093 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
   2094   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
   2095 }
   2096 
   2097 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
   2098                              const char *Name) {
   2099   return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
   2100 }
   2101 
   2102 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
   2103                              const char *Name) {
   2104   return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
   2105 }
   2106 
   2107 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
   2108   return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
   2109 }
   2110 
   2111 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
   2112   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
   2113 }
   2114 
   2115 /*--.. Memory ..............................................................--*/
   2116 
   2117 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
   2118                              const char *Name) {
   2119   Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
   2120   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
   2121   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
   2122   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
   2123                                                ITy, unwrap(Ty), AllocSize,
   2124                                                0, 0, "");
   2125   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
   2126 }
   2127 
   2128 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
   2129                                   LLVMValueRef Val, const char *Name) {
   2130   Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
   2131   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
   2132   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
   2133   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
   2134                                                ITy, unwrap(Ty), AllocSize,
   2135                                                unwrap(Val), 0, "");
   2136   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
   2137 }
   2138 
   2139 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
   2140                              const char *Name) {
   2141   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
   2142 }
   2143 
   2144 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
   2145                                   LLVMValueRef Val, const char *Name) {
   2146   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
   2147 }
   2148 
   2149 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
   2150   return wrap(unwrap(B)->Insert(
   2151      CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
   2152 }
   2153 
   2154 
   2155 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
   2156                            const char *Name) {
   2157   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
   2158 }
   2159 
   2160 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
   2161                             LLVMValueRef PointerVal) {
   2162   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
   2163 }
   2164 
   2165 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
   2166                           LLVMValueRef *Indices, unsigned NumIndices,
   2167                           const char *Name) {
   2168   ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
   2169   return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
   2170 }
   2171 
   2172 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
   2173                                   LLVMValueRef *Indices, unsigned NumIndices,
   2174                                   const char *Name) {
   2175   ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
   2176   return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
   2177 }
   2178 
   2179 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
   2180                                 unsigned Idx, const char *Name) {
   2181   return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
   2182 }
   2183 
   2184 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
   2185                                    const char *Name) {
   2186   return wrap(unwrap(B)->CreateGlobalString(Str, Name));
   2187 }
   2188 
   2189 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
   2190                                       const char *Name) {
   2191   return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
   2192 }
   2193 
   2194 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
   2195   Value *P = unwrap<Value>(MemAccessInst);
   2196   if (LoadInst *LI = dyn_cast<LoadInst>(P))
   2197     return LI->isVolatile();
   2198   return cast<StoreInst>(P)->isVolatile();
   2199 }
   2200 
   2201 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
   2202   Value *P = unwrap<Value>(MemAccessInst);
   2203   if (LoadInst *LI = dyn_cast<LoadInst>(P))
   2204     return LI->setVolatile(isVolatile);
   2205   return cast<StoreInst>(P)->setVolatile(isVolatile);
   2206 }
   2207 
   2208 /*--.. Casts ...............................................................--*/
   2209 
   2210 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
   2211                             LLVMTypeRef DestTy, const char *Name) {
   2212   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
   2213 }
   2214 
   2215 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
   2216                            LLVMTypeRef DestTy, const char *Name) {
   2217   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
   2218 }
   2219 
   2220 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
   2221                            LLVMTypeRef DestTy, const char *Name) {
   2222   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
   2223 }
   2224 
   2225 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
   2226                              LLVMTypeRef DestTy, const char *Name) {
   2227   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
   2228 }
   2229 
   2230 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
   2231                              LLVMTypeRef DestTy, const char *Name) {
   2232   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
   2233 }
   2234 
   2235 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
   2236                              LLVMTypeRef DestTy, const char *Name) {
   2237   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
   2238 }
   2239 
   2240 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
   2241                              LLVMTypeRef DestTy, const char *Name) {
   2242   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
   2243 }
   2244 
   2245 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
   2246                               LLVMTypeRef DestTy, const char *Name) {
   2247   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
   2248 }
   2249 
   2250 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
   2251                             LLVMTypeRef DestTy, const char *Name) {
   2252   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
   2253 }
   2254 
   2255 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
   2256                                LLVMTypeRef DestTy, const char *Name) {
   2257   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
   2258 }
   2259 
   2260 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
   2261                                LLVMTypeRef DestTy, const char *Name) {
   2262   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
   2263 }
   2264 
   2265 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
   2266                               LLVMTypeRef DestTy, const char *Name) {
   2267   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
   2268 }
   2269 
   2270 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
   2271                                     LLVMTypeRef DestTy, const char *Name) {
   2272   return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
   2273                                              Name));
   2274 }
   2275 
   2276 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
   2277                                     LLVMTypeRef DestTy, const char *Name) {
   2278   return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
   2279                                              Name));
   2280 }
   2281 
   2282 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
   2283                                      LLVMTypeRef DestTy, const char *Name) {
   2284   return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
   2285                                               Name));
   2286 }
   2287 
   2288 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
   2289                            LLVMTypeRef DestTy, const char *Name) {
   2290   return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
   2291                                     unwrap(DestTy), Name));
   2292 }
   2293 
   2294 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
   2295                                   LLVMTypeRef DestTy, const char *Name) {
   2296   return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
   2297 }
   2298 
   2299 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
   2300                               LLVMTypeRef DestTy, const char *Name) {
   2301   return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
   2302                                        /*isSigned*/true, Name));
   2303 }
   2304 
   2305 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
   2306                              LLVMTypeRef DestTy, const char *Name) {
   2307   return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
   2308 }
   2309 
   2310 /*--.. Comparisons .........................................................--*/
   2311 
   2312 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
   2313                            LLVMValueRef LHS, LLVMValueRef RHS,
   2314                            const char *Name) {
   2315   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
   2316                                     unwrap(LHS), unwrap(RHS), Name));
   2317 }
   2318 
   2319 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
   2320                            LLVMValueRef LHS, LLVMValueRef RHS,
   2321                            const char *Name) {
   2322   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
   2323                                     unwrap(LHS), unwrap(RHS), Name));
   2324 }
   2325 
   2326 /*--.. Miscellaneous instructions ..........................................--*/
   2327 
   2328 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
   2329   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
   2330 }
   2331 
   2332 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
   2333                            LLVMValueRef *Args, unsigned NumArgs,
   2334                            const char *Name) {
   2335   return wrap(unwrap(B)->CreateCall(unwrap(Fn),
   2336                                     makeArrayRef(unwrap(Args), NumArgs),
   2337                                     Name));
   2338 }
   2339 
   2340 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
   2341                              LLVMValueRef Then, LLVMValueRef Else,
   2342                              const char *Name) {
   2343   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
   2344                                       Name));
   2345 }
   2346 
   2347 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
   2348                             LLVMTypeRef Ty, const char *Name) {
   2349   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
   2350 }
   2351 
   2352 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
   2353                                       LLVMValueRef Index, const char *Name) {
   2354   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
   2355                                               Name));
   2356 }
   2357 
   2358 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
   2359                                     LLVMValueRef EltVal, LLVMValueRef Index,
   2360                                     const char *Name) {
   2361   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
   2362                                              unwrap(Index), Name));
   2363 }
   2364 
   2365 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
   2366                                     LLVMValueRef V2, LLVMValueRef Mask,
   2367                                     const char *Name) {
   2368   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
   2369                                              unwrap(Mask), Name));
   2370 }
   2371 
   2372 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
   2373                                    unsigned Index, const char *Name) {
   2374   return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
   2375 }
   2376 
   2377 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
   2378                                   LLVMValueRef EltVal, unsigned Index,
   2379                                   const char *Name) {
   2380   return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
   2381                                            Index, Name));
   2382 }
   2383 
   2384 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
   2385                              const char *Name) {
   2386   return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
   2387 }
   2388 
   2389 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
   2390                                 const char *Name) {
   2391   return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
   2392 }
   2393 
   2394 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
   2395                               LLVMValueRef RHS, const char *Name) {
   2396   return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
   2397 }
   2398 
   2399 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
   2400                                LLVMValueRef PTR, LLVMValueRef Val,
   2401                                LLVMAtomicOrdering ordering,
   2402                                LLVMBool singleThread) {
   2403   AtomicRMWInst::BinOp intop;
   2404   switch (op) {
   2405     case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
   2406     case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
   2407     case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
   2408     case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
   2409     case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
   2410     case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
   2411     case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
   2412     case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
   2413     case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
   2414     case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
   2415     case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
   2416   }
   2417   AtomicOrdering intordering;
   2418   switch (ordering) {
   2419     case LLVMAtomicOrderingNotAtomic: intordering = NotAtomic; break;
   2420     case LLVMAtomicOrderingUnordered: intordering = Unordered; break;
   2421     case LLVMAtomicOrderingMonotonic: intordering = Monotonic; break;
   2422     case LLVMAtomicOrderingAcquire: intordering = Acquire; break;
   2423     case LLVMAtomicOrderingRelease: intordering = Release; break;
   2424     case LLVMAtomicOrderingAcquireRelease:
   2425       intordering = AcquireRelease;
   2426       break;
   2427     case LLVMAtomicOrderingSequentiallyConsistent:
   2428       intordering = SequentiallyConsistent;
   2429       break;
   2430   }
   2431   return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
   2432     intordering, singleThread ? SingleThread : CrossThread));
   2433 }
   2434 
   2435 
   2436 /*===-- Module providers --------------------------------------------------===*/
   2437 
   2438 LLVMModuleProviderRef
   2439 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
   2440   return reinterpret_cast<LLVMModuleProviderRef>(M);
   2441 }
   2442 
   2443 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
   2444   delete unwrap(MP);
   2445 }
   2446 
   2447 
   2448 /*===-- Memory buffers ----------------------------------------------------===*/
   2449 
   2450 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
   2451     const char *Path,
   2452     LLVMMemoryBufferRef *OutMemBuf,
   2453     char **OutMessage) {
   2454 
   2455   OwningPtr<MemoryBuffer> MB;
   2456   error_code ec;
   2457   if (!(ec = MemoryBuffer::getFile(Path, MB))) {
   2458     *OutMemBuf = wrap(MB.take());
   2459     return 0;
   2460   }
   2461 
   2462   *OutMessage = strdup(ec.message().c_str());
   2463   return 1;
   2464 }
   2465 
   2466 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
   2467                                          char **OutMessage) {
   2468   OwningPtr<MemoryBuffer> MB;
   2469   error_code ec;
   2470   if (!(ec = MemoryBuffer::getSTDIN(MB))) {
   2471     *OutMemBuf = wrap(MB.take());
   2472     return 0;
   2473   }
   2474 
   2475   *OutMessage = strdup(ec.message().c_str());
   2476   return 1;
   2477 }
   2478 
   2479 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
   2480     const char *InputData,
   2481     size_t InputDataLength,
   2482     const char *BufferName,
   2483     LLVMBool RequiresNullTerminator) {
   2484 
   2485   return wrap(MemoryBuffer::getMemBuffer(
   2486       StringRef(InputData, InputDataLength),
   2487       StringRef(BufferName),
   2488       RequiresNullTerminator));
   2489 }
   2490 
   2491 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
   2492     const char *InputData,
   2493     size_t InputDataLength,
   2494     const char *BufferName) {
   2495 
   2496   return wrap(MemoryBuffer::getMemBufferCopy(
   2497       StringRef(InputData, InputDataLength),
   2498       StringRef(BufferName)));
   2499 }
   2500 
   2501 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
   2502   return unwrap(MemBuf)->getBufferStart();
   2503 }
   2504 
   2505 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
   2506   return unwrap(MemBuf)->getBufferSize();
   2507 }
   2508 
   2509 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
   2510   delete unwrap(MemBuf);
   2511 }
   2512 
   2513 /*===-- Pass Registry -----------------------------------------------------===*/
   2514 
   2515 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
   2516   return wrap(PassRegistry::getPassRegistry());
   2517 }
   2518 
   2519 /*===-- Pass Manager ------------------------------------------------------===*/
   2520 
   2521 LLVMPassManagerRef LLVMCreatePassManager() {
   2522   return wrap(new PassManager());
   2523 }
   2524 
   2525 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
   2526   return wrap(new FunctionPassManager(unwrap(M)));
   2527 }
   2528 
   2529 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
   2530   return LLVMCreateFunctionPassManagerForModule(
   2531                                             reinterpret_cast<LLVMModuleRef>(P));
   2532 }
   2533 
   2534 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
   2535   return unwrap<PassManager>(PM)->run(*unwrap(M));
   2536 }
   2537 
   2538 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
   2539   return unwrap<FunctionPassManager>(FPM)->doInitialization();
   2540 }
   2541 
   2542 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
   2543   return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
   2544 }
   2545 
   2546 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
   2547   return unwrap<FunctionPassManager>(FPM)->doFinalization();
   2548 }
   2549 
   2550 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
   2551   delete unwrap(PM);
   2552 }
   2553 
   2554 /*===-- Threading ------------------------------------------------------===*/
   2555 
   2556 LLVMBool LLVMStartMultithreaded() {
   2557   return llvm_start_multithreaded();
   2558 }
   2559 
   2560 void LLVMStopMultithreaded() {
   2561   llvm_stop_multithreaded();
   2562 }
   2563 
   2564 LLVMBool LLVMIsMultithreaded() {
   2565   return llvm_is_multithreaded();
   2566 }
   2567