1 //===-- Core.cpp ----------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the common infrastructure (including the C bindings)
11 // for libLLVMCore.a, which implements the LLVM intermediate representation.
13 //===----------------------------------------------------------------------===//
15 #include "llvm-c/Core.h"
16 #include "llvm/Bitcode/ReaderWriter.h"
17 #include "llvm/IR/Attributes.h"
18 #include "llvm/IR/CallSite.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DiagnosticInfo.h"
21 #include "llvm/IR/DiagnosticPrinter.h"
22 #include "llvm/IR/DerivedTypes.h"
23 #include "llvm/IR/GlobalAlias.h"
24 #include "llvm/IR/GlobalVariable.h"
25 #include "llvm/IR/IRBuilder.h"
26 #include "llvm/IR/InlineAsm.h"
27 #include "llvm/IR/IntrinsicInst.h"
28 #include "llvm/IR/LLVMContext.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/PassManager.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/ManagedStatic.h"
34 #include "llvm/Support/MemoryBuffer.h"
35 #include "llvm/Support/Threading.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/Support/system_error.h"
44 #define DEBUG_TYPE "ir"
46 void llvm::initializeCore(PassRegistry &Registry) {
47 initializeDominatorTreeWrapperPassPass(Registry);
48 initializePrintModulePassWrapperPass(Registry);
49 initializePrintFunctionPassWrapperPass(Registry);
50 initializePrintBasicBlockPassPass(Registry);
51 initializeVerifierLegacyPassPass(Registry);
54 void LLVMInitializeCore(LLVMPassRegistryRef R) {
55 initializeCore(*unwrap(R));
62 /*===-- Error handling ----------------------------------------------------===*/
64 char *LLVMCreateMessage(const char *Message) {
65 return strdup(Message);
68 void LLVMDisposeMessage(char *Message) {
73 /*===-- Operations on contexts --------------------------------------------===*/
75 LLVMContextRef LLVMContextCreate() {
76 return wrap(new LLVMContext());
79 LLVMContextRef LLVMGetGlobalContext() {
80 return wrap(&getGlobalContext());
83 void LLVMContextSetDiagnosticHandler(LLVMContextRef C,
84 LLVMDiagnosticHandler Handler,
85 void *DiagnosticContext) {
86 unwrap(C)->setDiagnosticHandler(
87 LLVM_EXTENSION reinterpret_cast<LLVMContext::DiagnosticHandlerTy>(Handler),
91 void LLVMContextDispose(LLVMContextRef C) {
95 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name,
97 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
100 unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) {
101 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
104 char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) {
105 std::string MsgStorage;
106 raw_string_ostream Stream(MsgStorage);
107 DiagnosticPrinterRawOStream DP(Stream);
109 unwrap(DI)->print(DP);
112 return LLVMCreateMessage(MsgStorage.c_str());
115 LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI){
116 LLVMDiagnosticSeverity severity;
118 switch(unwrap(DI)->getSeverity()) {
120 severity = LLVMDSError;
123 severity = LLVMDSWarning;
126 severity = LLVMDSRemark;
129 severity = LLVMDSNote;
139 /*===-- Operations on modules ---------------------------------------------===*/
141 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
142 return wrap(new Module(ModuleID, getGlobalContext()));
145 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
147 return wrap(new Module(ModuleID, *unwrap(C)));
150 void LLVMDisposeModule(LLVMModuleRef M) {
154 /*--.. Data layout .........................................................--*/
155 const char * LLVMGetDataLayout(LLVMModuleRef M) {
156 return unwrap(M)->getDataLayoutStr().c_str();
159 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
160 unwrap(M)->setDataLayout(Triple);
163 /*--.. Target triple .......................................................--*/
164 const char * LLVMGetTarget(LLVMModuleRef M) {
165 return unwrap(M)->getTargetTriple().c_str();
168 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
169 unwrap(M)->setTargetTriple(Triple);
172 void LLVMDumpModule(LLVMModuleRef M) {
176 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
177 char **ErrorMessage) {
179 raw_fd_ostream dest(Filename, error, sys::fs::F_Text);
180 if (!error.empty()) {
181 *ErrorMessage = strdup(error.c_str());
185 unwrap(M)->print(dest, nullptr);
187 if (!error.empty()) {
188 *ErrorMessage = strdup(error.c_str());
195 char *LLVMPrintModuleToString(LLVMModuleRef M) {
197 raw_string_ostream os(buf);
199 unwrap(M)->print(os, nullptr);
202 return strdup(buf.c_str());
205 /*--.. Operations on inline assembler ......................................--*/
206 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
207 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
211 /*--.. Operations on module contexts ......................................--*/
212 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
213 return wrap(&unwrap(M)->getContext());
217 /*===-- Operations on types -----------------------------------------------===*/
219 /*--.. Operations on all types (mostly) ....................................--*/
221 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
222 switch (unwrap(Ty)->getTypeID()) {
224 return LLVMVoidTypeKind;
226 return LLVMHalfTypeKind;
227 case Type::FloatTyID:
228 return LLVMFloatTypeKind;
229 case Type::DoubleTyID:
230 return LLVMDoubleTypeKind;
231 case Type::X86_FP80TyID:
232 return LLVMX86_FP80TypeKind;
233 case Type::FP128TyID:
234 return LLVMFP128TypeKind;
235 case Type::PPC_FP128TyID:
236 return LLVMPPC_FP128TypeKind;
237 case Type::LabelTyID:
238 return LLVMLabelTypeKind;
239 case Type::MetadataTyID:
240 return LLVMMetadataTypeKind;
241 case Type::IntegerTyID:
242 return LLVMIntegerTypeKind;
243 case Type::FunctionTyID:
244 return LLVMFunctionTypeKind;
245 case Type::StructTyID:
246 return LLVMStructTypeKind;
247 case Type::ArrayTyID:
248 return LLVMArrayTypeKind;
249 case Type::PointerTyID:
250 return LLVMPointerTypeKind;
251 case Type::VectorTyID:
252 return LLVMVectorTypeKind;
253 case Type::X86_MMXTyID:
254 return LLVMX86_MMXTypeKind;
256 llvm_unreachable("Unhandled TypeID.");
259 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
261 return unwrap(Ty)->isSized();
264 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
265 return wrap(&unwrap(Ty)->getContext());
268 void LLVMDumpType(LLVMTypeRef Ty) {
269 return unwrap(Ty)->dump();
272 char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
274 raw_string_ostream os(buf);
276 unwrap(Ty)->print(os);
279 return strdup(buf.c_str());
282 /*--.. Operations on integer types .........................................--*/
284 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
285 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
287 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
288 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
290 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
291 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
293 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
294 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
296 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
297 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
299 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
300 return wrap(IntegerType::get(*unwrap(C), NumBits));
303 LLVMTypeRef LLVMInt1Type(void) {
304 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
306 LLVMTypeRef LLVMInt8Type(void) {
307 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
309 LLVMTypeRef LLVMInt16Type(void) {
310 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
312 LLVMTypeRef LLVMInt32Type(void) {
313 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
315 LLVMTypeRef LLVMInt64Type(void) {
316 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
318 LLVMTypeRef LLVMIntType(unsigned NumBits) {
319 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
322 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
323 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
326 /*--.. Operations on real types ............................................--*/
328 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
329 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
331 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
332 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
334 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
335 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
337 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
338 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
340 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
341 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
343 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
344 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
346 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
347 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
350 LLVMTypeRef LLVMHalfType(void) {
351 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
353 LLVMTypeRef LLVMFloatType(void) {
354 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
356 LLVMTypeRef LLVMDoubleType(void) {
357 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
359 LLVMTypeRef LLVMX86FP80Type(void) {
360 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
362 LLVMTypeRef LLVMFP128Type(void) {
363 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
365 LLVMTypeRef LLVMPPCFP128Type(void) {
366 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
368 LLVMTypeRef LLVMX86MMXType(void) {
369 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
372 /*--.. Operations on function types ........................................--*/
374 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
375 LLVMTypeRef *ParamTypes, unsigned ParamCount,
377 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
378 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
381 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
382 return unwrap<FunctionType>(FunctionTy)->isVarArg();
385 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
386 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
389 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
390 return unwrap<FunctionType>(FunctionTy)->getNumParams();
393 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
394 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
395 for (FunctionType::param_iterator I = Ty->param_begin(),
396 E = Ty->param_end(); I != E; ++I)
400 /*--.. Operations on struct types ..........................................--*/
402 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
403 unsigned ElementCount, LLVMBool Packed) {
404 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
405 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
408 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
409 unsigned ElementCount, LLVMBool Packed) {
410 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
411 ElementCount, Packed);
414 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
416 return wrap(StructType::create(*unwrap(C), Name));
419 const char *LLVMGetStructName(LLVMTypeRef Ty)
421 StructType *Type = unwrap<StructType>(Ty);
422 if (!Type->hasName())
424 return Type->getName().data();
427 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
428 unsigned ElementCount, LLVMBool Packed) {
429 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
430 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
433 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
434 return unwrap<StructType>(StructTy)->getNumElements();
437 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
438 StructType *Ty = unwrap<StructType>(StructTy);
439 for (StructType::element_iterator I = Ty->element_begin(),
440 E = Ty->element_end(); I != E; ++I)
444 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
445 return unwrap<StructType>(StructTy)->isPacked();
448 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
449 return unwrap<StructType>(StructTy)->isOpaque();
452 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
453 return wrap(unwrap(M)->getTypeByName(Name));
456 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
458 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
459 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
462 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
463 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
466 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
467 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
470 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
471 return wrap(unwrap<SequentialType>(Ty)->getElementType());
474 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
475 return unwrap<ArrayType>(ArrayTy)->getNumElements();
478 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
479 return unwrap<PointerType>(PointerTy)->getAddressSpace();
482 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
483 return unwrap<VectorType>(VectorTy)->getNumElements();
486 /*--.. Operations on other types ...........................................--*/
488 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
489 return wrap(Type::getVoidTy(*unwrap(C)));
491 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
492 return wrap(Type::getLabelTy(*unwrap(C)));
495 LLVMTypeRef LLVMVoidType(void) {
496 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
498 LLVMTypeRef LLVMLabelType(void) {
499 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
502 /*===-- Operations on values ----------------------------------------------===*/
504 /*--.. Operations on all values ............................................--*/
506 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
507 return wrap(unwrap(Val)->getType());
510 const char *LLVMGetValueName(LLVMValueRef Val) {
511 return unwrap(Val)->getName().data();
514 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
515 unwrap(Val)->setName(Name);
518 void LLVMDumpValue(LLVMValueRef Val) {
522 char* LLVMPrintValueToString(LLVMValueRef Val) {
524 raw_string_ostream os(buf);
526 unwrap(Val)->print(os);
529 return strdup(buf.c_str());
532 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
533 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
536 int LLVMHasMetadata(LLVMValueRef Inst) {
537 return unwrap<Instruction>(Inst)->hasMetadata();
540 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
541 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
544 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
545 unwrap<Instruction>(Inst)->setMetadata(KindID,
546 MD ? unwrap<MDNode>(MD) : nullptr);
549 /*--.. Conversion functions ................................................--*/
551 #define LLVM_DEFINE_VALUE_CAST(name) \
552 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
553 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
556 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
558 /*--.. Operations on Uses ..................................................--*/
559 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
560 Value *V = unwrap(Val);
561 Value::use_iterator I = V->use_begin();
562 if (I == V->use_end())
567 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
568 Use *Next = unwrap(U)->getNext();
574 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
575 return wrap(unwrap(U)->getUser());
578 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
579 return wrap(unwrap(U)->get());
582 /*--.. Operations on Users .................................................--*/
583 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
584 Value *V = unwrap(Val);
585 if (MDNode *MD = dyn_cast<MDNode>(V))
586 return wrap(MD->getOperand(Index));
587 return wrap(cast<User>(V)->getOperand(Index));
590 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
591 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
594 int LLVMGetNumOperands(LLVMValueRef Val) {
595 Value *V = unwrap(Val);
596 if (MDNode *MD = dyn_cast<MDNode>(V))
597 return MD->getNumOperands();
598 return cast<User>(V)->getNumOperands();
601 /*--.. Operations on constants of any type .................................--*/
603 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
604 return wrap(Constant::getNullValue(unwrap(Ty)));
607 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
608 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
611 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
612 return wrap(UndefValue::get(unwrap(Ty)));
615 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
616 return isa<Constant>(unwrap(Ty));
619 LLVMBool LLVMIsNull(LLVMValueRef Val) {
620 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
621 return C->isNullValue();
625 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
626 return isa<UndefValue>(unwrap(Val));
629 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
631 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
634 /*--.. Operations on metadata nodes ........................................--*/
636 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
638 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
641 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
642 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
645 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
647 return wrap(MDNode::get(*unwrap(C),
648 makeArrayRef(unwrap<Value>(Vals, Count), Count)));
651 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
652 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
655 const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
656 if (const MDString *S = dyn_cast<MDString>(unwrap(V))) {
657 *Len = S->getString().size();
658 return S->getString().data();
664 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V)
666 return cast<MDNode>(unwrap(V))->getNumOperands();
669 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest)
671 const MDNode *N = cast<MDNode>(unwrap(V));
672 const unsigned numOperands = N->getNumOperands();
673 for (unsigned i = 0; i < numOperands; i++)
674 Dest[i] = wrap(N->getOperand(i));
677 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
679 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
680 return N->getNumOperands();
685 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
687 NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
690 for (unsigned i=0;i<N->getNumOperands();i++)
691 Dest[i] = wrap(N->getOperand(i));
694 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
697 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
700 MDNode *Op = Val ? unwrap<MDNode>(Val) : nullptr;
705 /*--.. Operations on scalar constants ......................................--*/
707 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
708 LLVMBool SignExtend) {
709 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
712 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
714 const uint64_t Words[]) {
715 IntegerType *Ty = unwrap<IntegerType>(IntTy);
716 return wrap(ConstantInt::get(Ty->getContext(),
717 APInt(Ty->getBitWidth(),
718 makeArrayRef(Words, NumWords))));
721 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
723 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
727 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
728 unsigned SLen, uint8_t Radix) {
729 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
733 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
734 return wrap(ConstantFP::get(unwrap(RealTy), N));
737 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
738 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
741 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
743 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
746 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
747 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
750 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
751 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
754 /*--.. Operations on composite constants ...................................--*/
756 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
758 LLVMBool DontNullTerminate) {
759 /* Inverted the sense of AddNull because ', 0)' is a
760 better mnemonic for null termination than ', 1)'. */
761 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
762 DontNullTerminate == 0));
764 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
765 LLVMValueRef *ConstantVals,
766 unsigned Count, LLVMBool Packed) {
767 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
768 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
772 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
773 LLVMBool DontNullTerminate) {
774 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
777 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
778 LLVMValueRef *ConstantVals, unsigned Length) {
779 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
780 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
782 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
784 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
788 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
789 LLVMValueRef *ConstantVals,
791 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
792 StructType *Ty = cast<StructType>(unwrap(StructTy));
794 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
797 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
798 return wrap(ConstantVector::get(makeArrayRef(
799 unwrap<Constant>(ScalarConstantVals, Size), Size)));
802 /*-- Opcode mapping */
804 static LLVMOpcode map_to_llvmopcode(int opcode)
807 default: llvm_unreachable("Unhandled Opcode.");
808 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
809 #include "llvm/IR/Instruction.def"
814 static int map_from_llvmopcode(LLVMOpcode code)
817 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
818 #include "llvm/IR/Instruction.def"
821 llvm_unreachable("Unhandled Opcode.");
824 /*--.. Constant expressions ................................................--*/
826 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
827 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
830 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
831 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
834 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
835 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
838 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
839 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
842 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
843 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
846 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
847 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
851 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
852 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
855 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
856 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
859 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
860 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
861 unwrap<Constant>(RHSConstant)));
864 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
865 LLVMValueRef RHSConstant) {
866 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
867 unwrap<Constant>(RHSConstant)));
870 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
871 LLVMValueRef RHSConstant) {
872 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
873 unwrap<Constant>(RHSConstant)));
876 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
877 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
878 unwrap<Constant>(RHSConstant)));
881 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
882 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
883 unwrap<Constant>(RHSConstant)));
886 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
887 LLVMValueRef RHSConstant) {
888 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
889 unwrap<Constant>(RHSConstant)));
892 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
893 LLVMValueRef RHSConstant) {
894 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
895 unwrap<Constant>(RHSConstant)));
898 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
899 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
900 unwrap<Constant>(RHSConstant)));
903 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
904 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
905 unwrap<Constant>(RHSConstant)));
908 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
909 LLVMValueRef RHSConstant) {
910 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
911 unwrap<Constant>(RHSConstant)));
914 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
915 LLVMValueRef RHSConstant) {
916 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
917 unwrap<Constant>(RHSConstant)));
920 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
921 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
922 unwrap<Constant>(RHSConstant)));
925 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
926 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
927 unwrap<Constant>(RHSConstant)));
930 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
931 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
932 unwrap<Constant>(RHSConstant)));
935 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
936 LLVMValueRef RHSConstant) {
937 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
938 unwrap<Constant>(RHSConstant)));
941 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
942 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
943 unwrap<Constant>(RHSConstant)));
946 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
947 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
948 unwrap<Constant>(RHSConstant)));
951 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
952 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
953 unwrap<Constant>(RHSConstant)));
956 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
957 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
958 unwrap<Constant>(RHSConstant)));
961 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
962 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
963 unwrap<Constant>(RHSConstant)));
966 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
967 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
968 unwrap<Constant>(RHSConstant)));
971 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
972 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
973 unwrap<Constant>(RHSConstant)));
976 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
977 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
978 return wrap(ConstantExpr::getICmp(Predicate,
979 unwrap<Constant>(LHSConstant),
980 unwrap<Constant>(RHSConstant)));
983 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
984 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
985 return wrap(ConstantExpr::getFCmp(Predicate,
986 unwrap<Constant>(LHSConstant),
987 unwrap<Constant>(RHSConstant)));
990 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
991 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
992 unwrap<Constant>(RHSConstant)));
995 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
996 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
997 unwrap<Constant>(RHSConstant)));
1000 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1001 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1002 unwrap<Constant>(RHSConstant)));
1005 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
1006 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1007 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1009 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
1013 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
1014 LLVMValueRef *ConstantIndices,
1015 unsigned NumIndices) {
1016 Constant* Val = unwrap<Constant>(ConstantVal);
1017 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1019 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
1022 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1023 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1027 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1028 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1032 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1033 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1037 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1038 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1042 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1043 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1047 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1048 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1052 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1053 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1057 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1058 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1062 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1063 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1067 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1068 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1072 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1073 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1077 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1078 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1082 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1083 LLVMTypeRef ToType) {
1084 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1088 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1089 LLVMTypeRef ToType) {
1090 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1094 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1095 LLVMTypeRef ToType) {
1096 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1100 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1101 LLVMTypeRef ToType) {
1102 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1106 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1107 LLVMTypeRef ToType) {
1108 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1112 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1113 LLVMBool isSigned) {
1114 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1115 unwrap(ToType), isSigned));
1118 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1119 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1123 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1124 LLVMValueRef ConstantIfTrue,
1125 LLVMValueRef ConstantIfFalse) {
1126 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1127 unwrap<Constant>(ConstantIfTrue),
1128 unwrap<Constant>(ConstantIfFalse)));
1131 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1132 LLVMValueRef IndexConstant) {
1133 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1134 unwrap<Constant>(IndexConstant)));
1137 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1138 LLVMValueRef ElementValueConstant,
1139 LLVMValueRef IndexConstant) {
1140 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1141 unwrap<Constant>(ElementValueConstant),
1142 unwrap<Constant>(IndexConstant)));
1145 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1146 LLVMValueRef VectorBConstant,
1147 LLVMValueRef MaskConstant) {
1148 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1149 unwrap<Constant>(VectorBConstant),
1150 unwrap<Constant>(MaskConstant)));
1153 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1155 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1156 makeArrayRef(IdxList, NumIdx)));
1159 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1160 LLVMValueRef ElementValueConstant,
1161 unsigned *IdxList, unsigned NumIdx) {
1162 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1163 unwrap<Constant>(ElementValueConstant),
1164 makeArrayRef(IdxList, NumIdx)));
1167 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1168 const char *Constraints,
1169 LLVMBool HasSideEffects,
1170 LLVMBool IsAlignStack) {
1171 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1172 Constraints, HasSideEffects, IsAlignStack));
1175 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1176 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1179 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1181 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1182 return wrap(unwrap<GlobalValue>(Global)->getParent());
1185 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1186 return unwrap<GlobalValue>(Global)->isDeclaration();
1189 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1190 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1191 case GlobalValue::ExternalLinkage:
1192 return LLVMExternalLinkage;
1193 case GlobalValue::AvailableExternallyLinkage:
1194 return LLVMAvailableExternallyLinkage;
1195 case GlobalValue::LinkOnceAnyLinkage:
1196 return LLVMLinkOnceAnyLinkage;
1197 case GlobalValue::LinkOnceODRLinkage:
1198 return LLVMLinkOnceODRLinkage;
1199 case GlobalValue::WeakAnyLinkage:
1200 return LLVMWeakAnyLinkage;
1201 case GlobalValue::WeakODRLinkage:
1202 return LLVMWeakODRLinkage;
1203 case GlobalValue::AppendingLinkage:
1204 return LLVMAppendingLinkage;
1205 case GlobalValue::InternalLinkage:
1206 return LLVMInternalLinkage;
1207 case GlobalValue::PrivateLinkage:
1208 return LLVMPrivateLinkage;
1209 case GlobalValue::ExternalWeakLinkage:
1210 return LLVMExternalWeakLinkage;
1211 case GlobalValue::CommonLinkage:
1212 return LLVMCommonLinkage;
1215 llvm_unreachable("Invalid GlobalValue linkage!");
1218 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1219 GlobalValue *GV = unwrap<GlobalValue>(Global);
1222 case LLVMExternalLinkage:
1223 GV->setLinkage(GlobalValue::ExternalLinkage);
1225 case LLVMAvailableExternallyLinkage:
1226 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1228 case LLVMLinkOnceAnyLinkage:
1229 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1231 case LLVMLinkOnceODRLinkage:
1232 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1234 case LLVMLinkOnceODRAutoHideLinkage:
1235 DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1236 "longer supported.");
1238 case LLVMWeakAnyLinkage:
1239 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1241 case LLVMWeakODRLinkage:
1242 GV->setLinkage(GlobalValue::WeakODRLinkage);
1244 case LLVMAppendingLinkage:
1245 GV->setLinkage(GlobalValue::AppendingLinkage);
1247 case LLVMInternalLinkage:
1248 GV->setLinkage(GlobalValue::InternalLinkage);
1250 case LLVMPrivateLinkage:
1251 GV->setLinkage(GlobalValue::PrivateLinkage);
1253 case LLVMLinkerPrivateLinkage:
1254 GV->setLinkage(GlobalValue::PrivateLinkage);
1256 case LLVMLinkerPrivateWeakLinkage:
1257 GV->setLinkage(GlobalValue::PrivateLinkage);
1259 case LLVMDLLImportLinkage:
1261 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1263 case LLVMDLLExportLinkage:
1265 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1267 case LLVMExternalWeakLinkage:
1268 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1270 case LLVMGhostLinkage:
1272 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1274 case LLVMCommonLinkage:
1275 GV->setLinkage(GlobalValue::CommonLinkage);
1280 const char *LLVMGetSection(LLVMValueRef Global) {
1281 return unwrap<GlobalValue>(Global)->getSection().c_str();
1284 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1285 unwrap<GlobalValue>(Global)->setSection(Section);
1288 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1289 return static_cast<LLVMVisibility>(
1290 unwrap<GlobalValue>(Global)->getVisibility());
1293 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1294 unwrap<GlobalValue>(Global)
1295 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1298 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
1299 return static_cast<LLVMDLLStorageClass>(
1300 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1303 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
1304 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1305 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1308 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
1309 return unwrap<GlobalValue>(Global)->hasUnnamedAddr();
1312 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
1313 unwrap<GlobalValue>(Global)->setUnnamedAddr(HasUnnamedAddr);
1316 /*--.. Operations on global variables, load and store instructions .........--*/
1318 unsigned LLVMGetAlignment(LLVMValueRef V) {
1319 Value *P = unwrap<Value>(V);
1320 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1321 return GV->getAlignment();
1322 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1323 return AI->getAlignment();
1324 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1325 return LI->getAlignment();
1326 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1327 return SI->getAlignment();
1330 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1333 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1334 Value *P = unwrap<Value>(V);
1335 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1336 GV->setAlignment(Bytes);
1337 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1338 AI->setAlignment(Bytes);
1339 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1340 LI->setAlignment(Bytes);
1341 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1342 SI->setAlignment(Bytes);
1345 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1348 /*--.. Operations on global variables ......................................--*/
1350 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1351 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1352 GlobalValue::ExternalLinkage, nullptr, Name));
1355 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1357 unsigned AddressSpace) {
1358 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1359 GlobalValue::ExternalLinkage, nullptr, Name,
1360 nullptr, GlobalVariable::NotThreadLocal,
1364 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1365 return wrap(unwrap(M)->getNamedGlobal(Name));
1368 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1369 Module *Mod = unwrap(M);
1370 Module::global_iterator I = Mod->global_begin();
1371 if (I == Mod->global_end())
1376 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1377 Module *Mod = unwrap(M);
1378 Module::global_iterator I = Mod->global_end();
1379 if (I == Mod->global_begin())
1384 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1385 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1386 Module::global_iterator I = GV;
1387 if (++I == GV->getParent()->global_end())
1392 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1393 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1394 Module::global_iterator I = GV;
1395 if (I == GV->getParent()->global_begin())
1400 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1401 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1404 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1405 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1406 if ( !GV->hasInitializer() )
1408 return wrap(GV->getInitializer());
1411 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1412 unwrap<GlobalVariable>(GlobalVar)
1413 ->setInitializer(unwrap<Constant>(ConstantVal));
1416 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1417 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1420 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1421 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1424 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1425 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1428 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1429 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1432 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1433 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1434 case GlobalVariable::NotThreadLocal:
1435 return LLVMNotThreadLocal;
1436 case GlobalVariable::GeneralDynamicTLSModel:
1437 return LLVMGeneralDynamicTLSModel;
1438 case GlobalVariable::LocalDynamicTLSModel:
1439 return LLVMLocalDynamicTLSModel;
1440 case GlobalVariable::InitialExecTLSModel:
1441 return LLVMInitialExecTLSModel;
1442 case GlobalVariable::LocalExecTLSModel:
1443 return LLVMLocalExecTLSModel;
1446 llvm_unreachable("Invalid GlobalVariable thread local mode");
1449 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1450 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1453 case LLVMNotThreadLocal:
1454 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1456 case LLVMGeneralDynamicTLSModel:
1457 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1459 case LLVMLocalDynamicTLSModel:
1460 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1462 case LLVMInitialExecTLSModel:
1463 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1465 case LLVMLocalExecTLSModel:
1466 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1471 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1472 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1475 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1476 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1479 /*--.. Operations on aliases ......................................--*/
1481 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1483 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1484 unwrap<Constant>(Aliasee), unwrap (M)));
1487 /*--.. Operations on functions .............................................--*/
1489 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1490 LLVMTypeRef FunctionTy) {
1491 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1492 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1495 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1496 return wrap(unwrap(M)->getFunction(Name));
1499 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1500 Module *Mod = unwrap(M);
1501 Module::iterator I = Mod->begin();
1502 if (I == Mod->end())
1507 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1508 Module *Mod = unwrap(M);
1509 Module::iterator I = Mod->end();
1510 if (I == Mod->begin())
1515 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1516 Function *Func = unwrap<Function>(Fn);
1517 Module::iterator I = Func;
1518 if (++I == Func->getParent()->end())
1523 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1524 Function *Func = unwrap<Function>(Fn);
1525 Module::iterator I = Func;
1526 if (I == Func->getParent()->begin())
1531 void LLVMDeleteFunction(LLVMValueRef Fn) {
1532 unwrap<Function>(Fn)->eraseFromParent();
1535 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1536 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1537 return F->getIntrinsicID();
1541 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1542 return unwrap<Function>(Fn)->getCallingConv();
1545 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1546 return unwrap<Function>(Fn)->setCallingConv(
1547 static_cast<CallingConv::ID>(CC));
1550 const char *LLVMGetGC(LLVMValueRef Fn) {
1551 Function *F = unwrap<Function>(Fn);
1552 return F->hasGC()? F->getGC() : nullptr;
1555 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1556 Function *F = unwrap<Function>(Fn);
1563 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1564 Function *Func = unwrap<Function>(Fn);
1565 const AttributeSet PAL = Func->getAttributes();
1567 const AttributeSet PALnew =
1568 PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1569 AttributeSet::get(Func->getContext(),
1570 AttributeSet::FunctionIndex, B));
1571 Func->setAttributes(PALnew);
1574 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1576 Function *Func = unwrap<Function>(Fn);
1577 AttributeSet::AttrIndex Idx =
1578 AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
1581 B.addAttribute(A, V);
1582 AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
1583 Func->addAttributes(Idx, Set);
1586 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1587 Function *Func = unwrap<Function>(Fn);
1588 const AttributeSet PAL = Func->getAttributes();
1590 const AttributeSet PALnew =
1591 PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1592 AttributeSet::get(Func->getContext(),
1593 AttributeSet::FunctionIndex, B));
1594 Func->setAttributes(PALnew);
1597 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1598 Function *Func = unwrap<Function>(Fn);
1599 const AttributeSet PAL = Func->getAttributes();
1600 return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
1603 /*--.. Operations on parameters ............................................--*/
1605 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1606 // This function is strictly redundant to
1607 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1608 return unwrap<Function>(FnRef)->arg_size();
1611 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1612 Function *Fn = unwrap<Function>(FnRef);
1613 for (Function::arg_iterator I = Fn->arg_begin(),
1614 E = Fn->arg_end(); I != E; I++)
1615 *ParamRefs++ = wrap(I);
1618 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1619 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1625 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1626 return wrap(unwrap<Argument>(V)->getParent());
1629 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1630 Function *Func = unwrap<Function>(Fn);
1631 Function::arg_iterator I = Func->arg_begin();
1632 if (I == Func->arg_end())
1637 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1638 Function *Func = unwrap<Function>(Fn);
1639 Function::arg_iterator I = Func->arg_end();
1640 if (I == Func->arg_begin())
1645 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1646 Argument *A = unwrap<Argument>(Arg);
1647 Function::arg_iterator I = A;
1648 if (++I == A->getParent()->arg_end())
1653 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1654 Argument *A = unwrap<Argument>(Arg);
1655 Function::arg_iterator I = A;
1656 if (I == A->getParent()->arg_begin())
1661 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1662 Argument *A = unwrap<Argument>(Arg);
1664 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1667 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1668 Argument *A = unwrap<Argument>(Arg);
1670 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1673 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1674 Argument *A = unwrap<Argument>(Arg);
1675 return (LLVMAttribute)A->getParent()->getAttributes().
1676 Raw(A->getArgNo()+1);
1680 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1681 Argument *A = unwrap<Argument>(Arg);
1683 B.addAlignmentAttr(align);
1684 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
1687 /*--.. Operations on basic blocks ..........................................--*/
1689 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1690 return wrap(static_cast<Value*>(unwrap(BB)));
1693 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1694 return isa<BasicBlock>(unwrap(Val));
1697 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1698 return wrap(unwrap<BasicBlock>(Val));
1701 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1702 return wrap(unwrap(BB)->getParent());
1705 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1706 return wrap(unwrap(BB)->getTerminator());
1709 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1710 return unwrap<Function>(FnRef)->size();
1713 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1714 Function *Fn = unwrap<Function>(FnRef);
1715 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1716 *BasicBlocksRefs++ = wrap(I);
1719 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1720 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1723 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1724 Function *Func = unwrap<Function>(Fn);
1725 Function::iterator I = Func->begin();
1726 if (I == Func->end())
1731 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1732 Function *Func = unwrap<Function>(Fn);
1733 Function::iterator I = Func->end();
1734 if (I == Func->begin())
1739 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1740 BasicBlock *Block = unwrap(BB);
1741 Function::iterator I = Block;
1742 if (++I == Block->getParent()->end())
1747 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1748 BasicBlock *Block = unwrap(BB);
1749 Function::iterator I = Block;
1750 if (I == Block->getParent()->begin())
1755 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1758 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1761 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1762 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1765 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1766 LLVMBasicBlockRef BBRef,
1768 BasicBlock *BB = unwrap(BBRef);
1769 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1772 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1774 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1777 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1778 unwrap(BBRef)->eraseFromParent();
1781 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1782 unwrap(BBRef)->removeFromParent();
1785 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1786 unwrap(BB)->moveBefore(unwrap(MovePos));
1789 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1790 unwrap(BB)->moveAfter(unwrap(MovePos));
1793 /*--.. Operations on instructions ..........................................--*/
1795 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1796 return wrap(unwrap<Instruction>(Inst)->getParent());
1799 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1800 BasicBlock *Block = unwrap(BB);
1801 BasicBlock::iterator I = Block->begin();
1802 if (I == Block->end())
1807 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1808 BasicBlock *Block = unwrap(BB);
1809 BasicBlock::iterator I = Block->end();
1810 if (I == Block->begin())
1815 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1816 Instruction *Instr = unwrap<Instruction>(Inst);
1817 BasicBlock::iterator I = Instr;
1818 if (++I == Instr->getParent()->end())
1823 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1824 Instruction *Instr = unwrap<Instruction>(Inst);
1825 BasicBlock::iterator I = Instr;
1826 if (I == Instr->getParent()->begin())
1831 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1832 unwrap<Instruction>(Inst)->eraseFromParent();
1835 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
1836 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
1837 return (LLVMIntPredicate)I->getPredicate();
1838 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1839 if (CE->getOpcode() == Instruction::ICmp)
1840 return (LLVMIntPredicate)CE->getPredicate();
1841 return (LLVMIntPredicate)0;
1844 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
1845 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
1846 return map_to_llvmopcode(C->getOpcode());
1847 return (LLVMOpcode)0;
1850 /*--.. Call and invoke instructions ........................................--*/
1852 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1853 Value *V = unwrap(Instr);
1854 if (CallInst *CI = dyn_cast<CallInst>(V))
1855 return CI->getCallingConv();
1856 if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1857 return II->getCallingConv();
1858 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1861 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1862 Value *V = unwrap(Instr);
1863 if (CallInst *CI = dyn_cast<CallInst>(V))
1864 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1865 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1866 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1867 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1870 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1872 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1875 Call.getAttributes().addAttributes(Call->getContext(), index,
1876 AttributeSet::get(Call->getContext(),
1880 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1882 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1884 Call.setAttributes(Call.getAttributes()
1885 .removeAttributes(Call->getContext(), index,
1886 AttributeSet::get(Call->getContext(),
1890 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1892 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1894 B.addAlignmentAttr(align);
1895 Call.setAttributes(Call.getAttributes()
1896 .addAttributes(Call->getContext(), index,
1897 AttributeSet::get(Call->getContext(),
1901 /*--.. Operations on call instructions (only) ..............................--*/
1903 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1904 return unwrap<CallInst>(Call)->isTailCall();
1907 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1908 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1911 /*--.. Operations on switch instructions (only) ............................--*/
1913 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
1914 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
1917 /*--.. Operations on phi nodes .............................................--*/
1919 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1920 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1921 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1922 for (unsigned I = 0; I != Count; ++I)
1923 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1926 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1927 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1930 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1931 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1934 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1935 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1939 /*===-- Instruction builders ----------------------------------------------===*/
1941 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1942 return wrap(new IRBuilder<>(*unwrap(C)));
1945 LLVMBuilderRef LLVMCreateBuilder(void) {
1946 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1949 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1950 LLVMValueRef Instr) {
1951 BasicBlock *BB = unwrap(Block);
1952 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1953 unwrap(Builder)->SetInsertPoint(BB, I);
1956 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1957 Instruction *I = unwrap<Instruction>(Instr);
1958 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1961 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1962 BasicBlock *BB = unwrap(Block);
1963 unwrap(Builder)->SetInsertPoint(BB);
1966 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1967 return wrap(unwrap(Builder)->GetInsertBlock());
1970 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1971 unwrap(Builder)->ClearInsertionPoint();
1974 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1975 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1978 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1980 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1983 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1984 delete unwrap(Builder);
1987 /*--.. Metadata builders ...................................................--*/
1989 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1990 MDNode *Loc = L ? unwrap<MDNode>(L) : nullptr;
1991 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1994 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1995 return wrap(unwrap(Builder)->getCurrentDebugLocation()
1996 .getAsMDNode(unwrap(Builder)->getContext()));
1999 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
2000 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
2004 /*--.. Instruction builders ................................................--*/
2006 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
2007 return wrap(unwrap(B)->CreateRetVoid());
2010 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
2011 return wrap(unwrap(B)->CreateRet(unwrap(V)));
2014 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
2016 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
2019 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
2020 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
2023 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
2024 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
2025 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
2028 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
2029 LLVMBasicBlockRef Else, unsigned NumCases) {
2030 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
2033 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
2034 unsigned NumDests) {
2035 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
2038 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
2039 LLVMValueRef *Args, unsigned NumArgs,
2040 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
2042 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
2043 makeArrayRef(unwrap(Args), NumArgs),
2047 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
2048 LLVMValueRef PersFn, unsigned NumClauses,
2050 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
2051 cast<Function>(unwrap(PersFn)),
2055 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
2056 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
2059 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
2060 return wrap(unwrap(B)->CreateUnreachable());
2063 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
2064 LLVMBasicBlockRef Dest) {
2065 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
2068 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
2069 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
2072 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
2073 unwrap<LandingPadInst>(LandingPad)->
2074 addClause(cast<Constant>(unwrap(ClauseVal)));
2077 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
2078 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2081 /*--.. Arithmetic ..........................................................--*/
2083 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2085 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2088 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2090 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2093 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2095 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2098 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2100 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2103 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2105 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2108 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2110 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2113 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2115 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2118 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2120 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2123 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2125 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2128 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2130 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2133 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2135 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2138 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2140 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2143 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2145 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2148 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2150 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2153 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2154 LLVMValueRef RHS, const char *Name) {
2155 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2158 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2160 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2163 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2165 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2168 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2170 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2173 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2175 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2178 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2180 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2183 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2185 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2188 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2190 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2193 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2195 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2198 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2200 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2203 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2205 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2208 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2209 LLVMValueRef LHS, LLVMValueRef RHS,
2211 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2212 unwrap(RHS), Name));
2215 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2216 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2219 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2221 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2224 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2226 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2229 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2230 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2233 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2234 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2237 /*--.. Memory ..............................................................--*/
2239 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2241 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2242 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2243 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2244 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2245 ITy, unwrap(Ty), AllocSize,
2246 nullptr, nullptr, "");
2247 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2250 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2251 LLVMValueRef Val, const char *Name) {
2252 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2253 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2254 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2255 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2256 ITy, unwrap(Ty), AllocSize,
2257 unwrap(Val), nullptr, "");
2258 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2261 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2263 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
2266 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2267 LLVMValueRef Val, const char *Name) {
2268 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2271 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2272 return wrap(unwrap(B)->Insert(
2273 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2277 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2279 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2282 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2283 LLVMValueRef PointerVal) {
2284 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2287 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
2289 case LLVMAtomicOrderingNotAtomic: return NotAtomic;
2290 case LLVMAtomicOrderingUnordered: return Unordered;
2291 case LLVMAtomicOrderingMonotonic: return Monotonic;
2292 case LLVMAtomicOrderingAcquire: return Acquire;
2293 case LLVMAtomicOrderingRelease: return Release;
2294 case LLVMAtomicOrderingAcquireRelease: return AcquireRelease;
2295 case LLVMAtomicOrderingSequentiallyConsistent:
2296 return SequentiallyConsistent;
2299 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
2302 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
2303 LLVMBool isSingleThread, const char *Name) {
2305 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
2306 isSingleThread ? SingleThread : CrossThread,
2310 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2311 LLVMValueRef *Indices, unsigned NumIndices,
2313 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2314 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
2317 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2318 LLVMValueRef *Indices, unsigned NumIndices,
2320 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2321 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
2324 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2325 unsigned Idx, const char *Name) {
2326 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2329 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2331 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2334 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2336 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2339 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2340 Value *P = unwrap<Value>(MemAccessInst);
2341 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2342 return LI->isVolatile();
2343 return cast<StoreInst>(P)->isVolatile();
2346 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2347 Value *P = unwrap<Value>(MemAccessInst);
2348 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2349 return LI->setVolatile(isVolatile);
2350 return cast<StoreInst>(P)->setVolatile(isVolatile);
2353 /*--.. Casts ...............................................................--*/
2355 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2356 LLVMTypeRef DestTy, const char *Name) {
2357 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2360 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2361 LLVMTypeRef DestTy, const char *Name) {
2362 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2365 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2366 LLVMTypeRef DestTy, const char *Name) {
2367 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2370 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2371 LLVMTypeRef DestTy, const char *Name) {
2372 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2375 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2376 LLVMTypeRef DestTy, const char *Name) {
2377 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2380 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2381 LLVMTypeRef DestTy, const char *Name) {
2382 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2385 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2386 LLVMTypeRef DestTy, const char *Name) {
2387 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2390 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2391 LLVMTypeRef DestTy, const char *Name) {
2392 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2395 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2396 LLVMTypeRef DestTy, const char *Name) {
2397 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2400 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2401 LLVMTypeRef DestTy, const char *Name) {
2402 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2405 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2406 LLVMTypeRef DestTy, const char *Name) {
2407 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2410 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2411 LLVMTypeRef DestTy, const char *Name) {
2412 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2415 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
2416 LLVMTypeRef DestTy, const char *Name) {
2417 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
2420 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2421 LLVMTypeRef DestTy, const char *Name) {
2422 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2426 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2427 LLVMTypeRef DestTy, const char *Name) {
2428 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2432 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2433 LLVMTypeRef DestTy, const char *Name) {
2434 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2438 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2439 LLVMTypeRef DestTy, const char *Name) {
2440 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2441 unwrap(DestTy), Name));
2444 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2445 LLVMTypeRef DestTy, const char *Name) {
2446 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2449 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2450 LLVMTypeRef DestTy, const char *Name) {
2451 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2452 /*isSigned*/true, Name));
2455 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2456 LLVMTypeRef DestTy, const char *Name) {
2457 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2460 /*--.. Comparisons .........................................................--*/
2462 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2463 LLVMValueRef LHS, LLVMValueRef RHS,
2465 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2466 unwrap(LHS), unwrap(RHS), Name));
2469 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2470 LLVMValueRef LHS, LLVMValueRef RHS,
2472 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2473 unwrap(LHS), unwrap(RHS), Name));
2476 /*--.. Miscellaneous instructions ..........................................--*/
2478 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2479 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2482 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2483 LLVMValueRef *Args, unsigned NumArgs,
2485 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2486 makeArrayRef(unwrap(Args), NumArgs),
2490 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2491 LLVMValueRef Then, LLVMValueRef Else,
2493 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2497 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2498 LLVMTypeRef Ty, const char *Name) {
2499 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2502 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2503 LLVMValueRef Index, const char *Name) {
2504 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2508 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2509 LLVMValueRef EltVal, LLVMValueRef Index,
2511 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2512 unwrap(Index), Name));
2515 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2516 LLVMValueRef V2, LLVMValueRef Mask,
2518 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2519 unwrap(Mask), Name));
2522 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2523 unsigned Index, const char *Name) {
2524 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2527 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2528 LLVMValueRef EltVal, unsigned Index,
2530 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2534 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2536 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2539 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2541 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2544 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2545 LLVMValueRef RHS, const char *Name) {
2546 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2549 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
2550 LLVMValueRef PTR, LLVMValueRef Val,
2551 LLVMAtomicOrdering ordering,
2552 LLVMBool singleThread) {
2553 AtomicRMWInst::BinOp intop;
2555 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
2556 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
2557 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
2558 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
2559 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
2560 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
2561 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
2562 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
2563 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
2564 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
2565 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
2567 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
2568 mapFromLLVMOrdering(ordering), singleThread ? SingleThread : CrossThread));
2572 /*===-- Module providers --------------------------------------------------===*/
2574 LLVMModuleProviderRef
2575 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2576 return reinterpret_cast<LLVMModuleProviderRef>(M);
2579 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2584 /*===-- Memory buffers ----------------------------------------------------===*/
2586 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2588 LLVMMemoryBufferRef *OutMemBuf,
2589 char **OutMessage) {
2591 std::unique_ptr<MemoryBuffer> MB;
2593 if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2594 *OutMemBuf = wrap(MB.release());
2598 *OutMessage = strdup(ec.message().c_str());
2602 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2603 char **OutMessage) {
2604 std::unique_ptr<MemoryBuffer> MB;
2606 if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2607 *OutMemBuf = wrap(MB.release());
2611 *OutMessage = strdup(ec.message().c_str());
2615 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
2616 const char *InputData,
2617 size_t InputDataLength,
2618 const char *BufferName,
2619 LLVMBool RequiresNullTerminator) {
2621 return wrap(MemoryBuffer::getMemBuffer(
2622 StringRef(InputData, InputDataLength),
2623 StringRef(BufferName),
2624 RequiresNullTerminator));
2627 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
2628 const char *InputData,
2629 size_t InputDataLength,
2630 const char *BufferName) {
2632 return wrap(MemoryBuffer::getMemBufferCopy(
2633 StringRef(InputData, InputDataLength),
2634 StringRef(BufferName)));
2637 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
2638 return unwrap(MemBuf)->getBufferStart();
2641 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
2642 return unwrap(MemBuf)->getBufferSize();
2645 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2646 delete unwrap(MemBuf);
2649 /*===-- Pass Registry -----------------------------------------------------===*/
2651 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2652 return wrap(PassRegistry::getPassRegistry());
2655 /*===-- Pass Manager ------------------------------------------------------===*/
2657 LLVMPassManagerRef LLVMCreatePassManager() {
2658 return wrap(new PassManager());
2661 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2662 return wrap(new FunctionPassManager(unwrap(M)));
2665 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2666 return LLVMCreateFunctionPassManagerForModule(
2667 reinterpret_cast<LLVMModuleRef>(P));
2670 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2671 return unwrap<PassManager>(PM)->run(*unwrap(M));
2674 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2675 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2678 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2679 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2682 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2683 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2686 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
2690 /*===-- Threading ------------------------------------------------------===*/
2692 LLVMBool LLVMStartMultithreaded() {
2693 return llvm_start_multithreaded();
2696 void LLVMStopMultithreaded() {
2697 llvm_stop_multithreaded();
2700 LLVMBool LLVMIsMultithreaded() {
2701 return llvm_is_multithreaded();