LLVM API Documentation

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