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