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/FileSystem.h"
34 #include "llvm/Support/ManagedStatic.h"
35 #include "llvm/Support/MemoryBuffer.h"
36 #include "llvm/Support/Threading.h"
37 #include "llvm/Support/raw_ostream.h"
38 #include "llvm/Support/system_error.h"
45 #define DEBUG_TYPE "ir"
47 void llvm::initializeCore(PassRegistry &Registry) {
48 initializeDominatorTreeWrapperPassPass(Registry);
49 initializePrintModulePassWrapperPass(Registry);
50 initializePrintFunctionPassWrapperPass(Registry);
51 initializePrintBasicBlockPassPass(Registry);
52 initializeVerifierLegacyPassPass(Registry);
55 void LLVMInitializeCore(LLVMPassRegistryRef R) {
56 initializeCore(*unwrap(R));
63 /*===-- Error handling ----------------------------------------------------===*/
65 char *LLVMCreateMessage(const char *Message) {
66 return strdup(Message);
69 void LLVMDisposeMessage(char *Message) {
74 /*===-- Operations on contexts --------------------------------------------===*/
76 LLVMContextRef LLVMContextCreate() {
77 return wrap(new LLVMContext());
80 LLVMContextRef LLVMGetGlobalContext() {
81 return wrap(&getGlobalContext());
84 void LLVMContextSetDiagnosticHandler(LLVMContextRef C,
85 LLVMDiagnosticHandler Handler,
86 void *DiagnosticContext) {
87 unwrap(C)->setDiagnosticHandler(
88 LLVM_EXTENSION reinterpret_cast<LLVMContext::DiagnosticHandlerTy>(Handler),
92 void LLVMContextDispose(LLVMContextRef C) {
96 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name,
98 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
101 unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) {
102 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
105 char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) {
106 std::string MsgStorage;
107 raw_string_ostream Stream(MsgStorage);
108 DiagnosticPrinterRawOStream DP(Stream);
110 unwrap(DI)->print(DP);
113 return LLVMCreateMessage(MsgStorage.c_str());
116 LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI){
117 LLVMDiagnosticSeverity severity;
119 switch(unwrap(DI)->getSeverity()) {
121 severity = LLVMDSError;
124 severity = LLVMDSWarning;
127 severity = LLVMDSRemark;
130 severity = LLVMDSNote;
140 /*===-- Operations on modules ---------------------------------------------===*/
142 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
143 return wrap(new Module(ModuleID, getGlobalContext()));
146 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
148 return wrap(new Module(ModuleID, *unwrap(C)));
151 void LLVMDisposeModule(LLVMModuleRef M) {
155 /*--.. Data layout .........................................................--*/
156 const char * LLVMGetDataLayout(LLVMModuleRef M) {
157 return unwrap(M)->getDataLayoutStr().c_str();
160 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
161 unwrap(M)->setDataLayout(Triple);
164 /*--.. Target triple .......................................................--*/
165 const char * LLVMGetTarget(LLVMModuleRef M) {
166 return unwrap(M)->getTargetTriple().c_str();
169 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
170 unwrap(M)->setTargetTriple(Triple);
173 void LLVMDumpModule(LLVMModuleRef M) {
177 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
178 char **ErrorMessage) {
180 raw_fd_ostream dest(Filename, error, sys::fs::F_Text);
181 if (!error.empty()) {
182 *ErrorMessage = strdup(error.c_str());
186 unwrap(M)->print(dest, nullptr);
188 if (!error.empty()) {
189 *ErrorMessage = strdup(error.c_str());
196 char *LLVMPrintModuleToString(LLVMModuleRef M) {
198 raw_string_ostream os(buf);
200 unwrap(M)->print(os, nullptr);
203 return strdup(buf.c_str());
206 /*--.. Operations on inline assembler ......................................--*/
207 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
208 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
212 /*--.. Operations on module contexts ......................................--*/
213 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
214 return wrap(&unwrap(M)->getContext());
218 /*===-- Operations on types -----------------------------------------------===*/
220 /*--.. Operations on all types (mostly) ....................................--*/
222 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
223 switch (unwrap(Ty)->getTypeID()) {
225 return LLVMVoidTypeKind;
227 return LLVMHalfTypeKind;
228 case Type::FloatTyID:
229 return LLVMFloatTypeKind;
230 case Type::DoubleTyID:
231 return LLVMDoubleTypeKind;
232 case Type::X86_FP80TyID:
233 return LLVMX86_FP80TypeKind;
234 case Type::FP128TyID:
235 return LLVMFP128TypeKind;
236 case Type::PPC_FP128TyID:
237 return LLVMPPC_FP128TypeKind;
238 case Type::LabelTyID:
239 return LLVMLabelTypeKind;
240 case Type::MetadataTyID:
241 return LLVMMetadataTypeKind;
242 case Type::IntegerTyID:
243 return LLVMIntegerTypeKind;
244 case Type::FunctionTyID:
245 return LLVMFunctionTypeKind;
246 case Type::StructTyID:
247 return LLVMStructTypeKind;
248 case Type::ArrayTyID:
249 return LLVMArrayTypeKind;
250 case Type::PointerTyID:
251 return LLVMPointerTypeKind;
252 case Type::VectorTyID:
253 return LLVMVectorTypeKind;
254 case Type::X86_MMXTyID:
255 return LLVMX86_MMXTypeKind;
257 llvm_unreachable("Unhandled TypeID.");
260 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
262 return unwrap(Ty)->isSized();
265 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
266 return wrap(&unwrap(Ty)->getContext());
269 void LLVMDumpType(LLVMTypeRef Ty) {
270 return unwrap(Ty)->dump();
273 char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
275 raw_string_ostream os(buf);
277 unwrap(Ty)->print(os);
280 return strdup(buf.c_str());
283 /*--.. Operations on integer types .........................................--*/
285 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
286 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
288 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
289 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
291 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
292 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
294 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
295 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
297 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
298 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
300 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
301 return wrap(IntegerType::get(*unwrap(C), NumBits));
304 LLVMTypeRef LLVMInt1Type(void) {
305 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
307 LLVMTypeRef LLVMInt8Type(void) {
308 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
310 LLVMTypeRef LLVMInt16Type(void) {
311 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
313 LLVMTypeRef LLVMInt32Type(void) {
314 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
316 LLVMTypeRef LLVMInt64Type(void) {
317 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
319 LLVMTypeRef LLVMIntType(unsigned NumBits) {
320 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
323 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
324 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
327 /*--.. Operations on real types ............................................--*/
329 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
330 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
332 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
333 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
335 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
336 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
338 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
339 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
341 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
342 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
344 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
345 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
347 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
348 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
351 LLVMTypeRef LLVMHalfType(void) {
352 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
354 LLVMTypeRef LLVMFloatType(void) {
355 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
357 LLVMTypeRef LLVMDoubleType(void) {
358 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
360 LLVMTypeRef LLVMX86FP80Type(void) {
361 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
363 LLVMTypeRef LLVMFP128Type(void) {
364 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
366 LLVMTypeRef LLVMPPCFP128Type(void) {
367 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
369 LLVMTypeRef LLVMX86MMXType(void) {
370 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
373 /*--.. Operations on function types ........................................--*/
375 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
376 LLVMTypeRef *ParamTypes, unsigned ParamCount,
378 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
379 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
382 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
383 return unwrap<FunctionType>(FunctionTy)->isVarArg();
386 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
387 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
390 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
391 return unwrap<FunctionType>(FunctionTy)->getNumParams();
394 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
395 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
396 for (FunctionType::param_iterator I = Ty->param_begin(),
397 E = Ty->param_end(); I != E; ++I)
401 /*--.. Operations on struct types ..........................................--*/
403 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
404 unsigned ElementCount, LLVMBool Packed) {
405 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
406 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
409 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
410 unsigned ElementCount, LLVMBool Packed) {
411 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
412 ElementCount, Packed);
415 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
417 return wrap(StructType::create(*unwrap(C), Name));
420 const char *LLVMGetStructName(LLVMTypeRef Ty)
422 StructType *Type = unwrap<StructType>(Ty);
423 if (!Type->hasName())
425 return Type->getName().data();
428 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
429 unsigned ElementCount, LLVMBool Packed) {
430 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
431 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
434 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
435 return unwrap<StructType>(StructTy)->getNumElements();
438 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
439 StructType *Ty = unwrap<StructType>(StructTy);
440 for (StructType::element_iterator I = Ty->element_begin(),
441 E = Ty->element_end(); I != E; ++I)
445 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
446 return unwrap<StructType>(StructTy)->isPacked();
449 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
450 return unwrap<StructType>(StructTy)->isOpaque();
453 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
454 return wrap(unwrap(M)->getTypeByName(Name));
457 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
459 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
460 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
463 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
464 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
467 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
468 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
471 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
472 return wrap(unwrap<SequentialType>(Ty)->getElementType());
475 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
476 return unwrap<ArrayType>(ArrayTy)->getNumElements();
479 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
480 return unwrap<PointerType>(PointerTy)->getAddressSpace();
483 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
484 return unwrap<VectorType>(VectorTy)->getNumElements();
487 /*--.. Operations on other types ...........................................--*/
489 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
490 return wrap(Type::getVoidTy(*unwrap(C)));
492 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
493 return wrap(Type::getLabelTy(*unwrap(C)));
496 LLVMTypeRef LLVMVoidType(void) {
497 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
499 LLVMTypeRef LLVMLabelType(void) {
500 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
503 /*===-- Operations on values ----------------------------------------------===*/
505 /*--.. Operations on all values ............................................--*/
507 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
508 return wrap(unwrap(Val)->getType());
511 const char *LLVMGetValueName(LLVMValueRef Val) {
512 return unwrap(Val)->getName().data();
515 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
516 unwrap(Val)->setName(Name);
519 void LLVMDumpValue(LLVMValueRef Val) {
523 char* LLVMPrintValueToString(LLVMValueRef Val) {
525 raw_string_ostream os(buf);
527 unwrap(Val)->print(os);
530 return strdup(buf.c_str());
533 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
534 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
537 int LLVMHasMetadata(LLVMValueRef Inst) {
538 return unwrap<Instruction>(Inst)->hasMetadata();
541 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
542 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
545 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
546 unwrap<Instruction>(Inst)->setMetadata(KindID,
547 MD ? unwrap<MDNode>(MD) : nullptr);
550 /*--.. Conversion functions ................................................--*/
552 #define LLVM_DEFINE_VALUE_CAST(name) \
553 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
554 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
557 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
559 /*--.. Operations on Uses ..................................................--*/
560 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
561 Value *V = unwrap(Val);
562 Value::use_iterator I = V->use_begin();
563 if (I == V->use_end())
568 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
569 Use *Next = unwrap(U)->getNext();
575 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
576 return wrap(unwrap(U)->getUser());
579 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
580 return wrap(unwrap(U)->get());
583 /*--.. Operations on Users .................................................--*/
584 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
585 Value *V = unwrap(Val);
586 if (MDNode *MD = dyn_cast<MDNode>(V))
587 return wrap(MD->getOperand(Index));
588 return wrap(cast<User>(V)->getOperand(Index));
591 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
592 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
595 int LLVMGetNumOperands(LLVMValueRef Val) {
596 Value *V = unwrap(Val);
597 if (MDNode *MD = dyn_cast<MDNode>(V))
598 return MD->getNumOperands();
599 return cast<User>(V)->getNumOperands();
602 /*--.. Operations on constants of any type .................................--*/
604 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
605 return wrap(Constant::getNullValue(unwrap(Ty)));
608 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
609 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
612 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
613 return wrap(UndefValue::get(unwrap(Ty)));
616 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
617 return isa<Constant>(unwrap(Ty));
620 LLVMBool LLVMIsNull(LLVMValueRef Val) {
621 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
622 return C->isNullValue();
626 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
627 return isa<UndefValue>(unwrap(Val));
630 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
632 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
635 /*--.. Operations on metadata nodes ........................................--*/
637 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
639 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
642 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
643 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
646 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
648 return wrap(MDNode::get(*unwrap(C),
649 makeArrayRef(unwrap<Value>(Vals, Count), Count)));
652 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
653 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
656 const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
657 if (const MDString *S = dyn_cast<MDString>(unwrap(V))) {
658 *Len = S->getString().size();
659 return S->getString().data();
665 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V)
667 return cast<MDNode>(unwrap(V))->getNumOperands();
670 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest)
672 const MDNode *N = cast<MDNode>(unwrap(V));
673 const unsigned numOperands = N->getNumOperands();
674 for (unsigned i = 0; i < numOperands; i++)
675 Dest[i] = wrap(N->getOperand(i));
678 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
680 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
681 return N->getNumOperands();
686 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
688 NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
691 for (unsigned i=0;i<N->getNumOperands();i++)
692 Dest[i] = wrap(N->getOperand(i));
695 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
698 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
701 MDNode *Op = Val ? unwrap<MDNode>(Val) : nullptr;
706 /*--.. Operations on scalar constants ......................................--*/
708 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
709 LLVMBool SignExtend) {
710 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
713 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
715 const uint64_t Words[]) {
716 IntegerType *Ty = unwrap<IntegerType>(IntTy);
717 return wrap(ConstantInt::get(Ty->getContext(),
718 APInt(Ty->getBitWidth(),
719 makeArrayRef(Words, NumWords))));
722 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
724 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
728 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
729 unsigned SLen, uint8_t Radix) {
730 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
734 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
735 return wrap(ConstantFP::get(unwrap(RealTy), N));
738 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
739 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
742 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
744 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
747 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
748 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
751 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
752 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
755 /*--.. Operations on composite constants ...................................--*/
757 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
759 LLVMBool DontNullTerminate) {
760 /* Inverted the sense of AddNull because ', 0)' is a
761 better mnemonic for null termination than ', 1)'. */
762 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
763 DontNullTerminate == 0));
765 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
766 LLVMValueRef *ConstantVals,
767 unsigned Count, LLVMBool Packed) {
768 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
769 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
773 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
774 LLVMBool DontNullTerminate) {
775 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
778 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
779 LLVMValueRef *ConstantVals, unsigned Length) {
780 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
781 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
783 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
785 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
789 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
790 LLVMValueRef *ConstantVals,
792 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
793 StructType *Ty = cast<StructType>(unwrap(StructTy));
795 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
798 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
799 return wrap(ConstantVector::get(makeArrayRef(
800 unwrap<Constant>(ScalarConstantVals, Size), Size)));
803 /*-- Opcode mapping */
805 static LLVMOpcode map_to_llvmopcode(int opcode)
808 default: llvm_unreachable("Unhandled Opcode.");
809 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
810 #include "llvm/IR/Instruction.def"
815 static int map_from_llvmopcode(LLVMOpcode code)
818 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
819 #include "llvm/IR/Instruction.def"
822 llvm_unreachable("Unhandled Opcode.");
825 /*--.. Constant expressions ................................................--*/
827 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
828 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
831 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
832 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
835 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
836 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
839 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
840 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
843 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
844 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
847 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
848 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
852 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
853 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
856 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
857 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
860 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
861 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
862 unwrap<Constant>(RHSConstant)));
865 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
866 LLVMValueRef RHSConstant) {
867 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
868 unwrap<Constant>(RHSConstant)));
871 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
872 LLVMValueRef RHSConstant) {
873 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
874 unwrap<Constant>(RHSConstant)));
877 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
878 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
879 unwrap<Constant>(RHSConstant)));
882 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
883 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
884 unwrap<Constant>(RHSConstant)));
887 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
888 LLVMValueRef RHSConstant) {
889 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
890 unwrap<Constant>(RHSConstant)));
893 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
894 LLVMValueRef RHSConstant) {
895 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
896 unwrap<Constant>(RHSConstant)));
899 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
900 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
901 unwrap<Constant>(RHSConstant)));
904 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
905 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
906 unwrap<Constant>(RHSConstant)));
909 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
910 LLVMValueRef RHSConstant) {
911 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
912 unwrap<Constant>(RHSConstant)));
915 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
916 LLVMValueRef RHSConstant) {
917 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
918 unwrap<Constant>(RHSConstant)));
921 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
922 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
923 unwrap<Constant>(RHSConstant)));
926 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
927 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
928 unwrap<Constant>(RHSConstant)));
931 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
932 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
933 unwrap<Constant>(RHSConstant)));
936 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
937 LLVMValueRef RHSConstant) {
938 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
939 unwrap<Constant>(RHSConstant)));
942 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
943 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
944 unwrap<Constant>(RHSConstant)));
947 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
948 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
949 unwrap<Constant>(RHSConstant)));
952 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
953 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
954 unwrap<Constant>(RHSConstant)));
957 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
958 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
959 unwrap<Constant>(RHSConstant)));
962 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
963 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
964 unwrap<Constant>(RHSConstant)));
967 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
968 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
969 unwrap<Constant>(RHSConstant)));
972 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
973 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
974 unwrap<Constant>(RHSConstant)));
977 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
978 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
979 return wrap(ConstantExpr::getICmp(Predicate,
980 unwrap<Constant>(LHSConstant),
981 unwrap<Constant>(RHSConstant)));
984 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
985 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
986 return wrap(ConstantExpr::getFCmp(Predicate,
987 unwrap<Constant>(LHSConstant),
988 unwrap<Constant>(RHSConstant)));
991 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
992 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
993 unwrap<Constant>(RHSConstant)));
996 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
997 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
998 unwrap<Constant>(RHSConstant)));
1001 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1002 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1003 unwrap<Constant>(RHSConstant)));
1006 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
1007 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1008 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1010 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
1014 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
1015 LLVMValueRef *ConstantIndices,
1016 unsigned NumIndices) {
1017 Constant* Val = unwrap<Constant>(ConstantVal);
1018 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1020 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
1023 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1024 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1028 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1029 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1033 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1034 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1038 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1039 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1043 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1044 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1048 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1049 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1053 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1054 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1058 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1059 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1063 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1064 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1068 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1069 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1073 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1074 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1078 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1079 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1083 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1084 LLVMTypeRef ToType) {
1085 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1089 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1090 LLVMTypeRef ToType) {
1091 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1095 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1096 LLVMTypeRef ToType) {
1097 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1101 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1102 LLVMTypeRef ToType) {
1103 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1107 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1108 LLVMTypeRef ToType) {
1109 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1113 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1114 LLVMBool isSigned) {
1115 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1116 unwrap(ToType), isSigned));
1119 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1120 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1124 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1125 LLVMValueRef ConstantIfTrue,
1126 LLVMValueRef ConstantIfFalse) {
1127 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1128 unwrap<Constant>(ConstantIfTrue),
1129 unwrap<Constant>(ConstantIfFalse)));
1132 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1133 LLVMValueRef IndexConstant) {
1134 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1135 unwrap<Constant>(IndexConstant)));
1138 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1139 LLVMValueRef ElementValueConstant,
1140 LLVMValueRef IndexConstant) {
1141 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1142 unwrap<Constant>(ElementValueConstant),
1143 unwrap<Constant>(IndexConstant)));
1146 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1147 LLVMValueRef VectorBConstant,
1148 LLVMValueRef MaskConstant) {
1149 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1150 unwrap<Constant>(VectorBConstant),
1151 unwrap<Constant>(MaskConstant)));
1154 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1156 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1157 makeArrayRef(IdxList, NumIdx)));
1160 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1161 LLVMValueRef ElementValueConstant,
1162 unsigned *IdxList, unsigned NumIdx) {
1163 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1164 unwrap<Constant>(ElementValueConstant),
1165 makeArrayRef(IdxList, NumIdx)));
1168 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1169 const char *Constraints,
1170 LLVMBool HasSideEffects,
1171 LLVMBool IsAlignStack) {
1172 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1173 Constraints, HasSideEffects, IsAlignStack));
1176 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1177 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1180 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1182 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1183 return wrap(unwrap<GlobalValue>(Global)->getParent());
1186 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1187 return unwrap<GlobalValue>(Global)->isDeclaration();
1190 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1191 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1192 case GlobalValue::ExternalLinkage:
1193 return LLVMExternalLinkage;
1194 case GlobalValue::AvailableExternallyLinkage:
1195 return LLVMAvailableExternallyLinkage;
1196 case GlobalValue::LinkOnceAnyLinkage:
1197 return LLVMLinkOnceAnyLinkage;
1198 case GlobalValue::LinkOnceODRLinkage:
1199 return LLVMLinkOnceODRLinkage;
1200 case GlobalValue::WeakAnyLinkage:
1201 return LLVMWeakAnyLinkage;
1202 case GlobalValue::WeakODRLinkage:
1203 return LLVMWeakODRLinkage;
1204 case GlobalValue::AppendingLinkage:
1205 return LLVMAppendingLinkage;
1206 case GlobalValue::InternalLinkage:
1207 return LLVMInternalLinkage;
1208 case GlobalValue::PrivateLinkage:
1209 return LLVMPrivateLinkage;
1210 case GlobalValue::ExternalWeakLinkage:
1211 return LLVMExternalWeakLinkage;
1212 case GlobalValue::CommonLinkage:
1213 return LLVMCommonLinkage;
1216 llvm_unreachable("Invalid GlobalValue linkage!");
1219 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1220 GlobalValue *GV = unwrap<GlobalValue>(Global);
1223 case LLVMExternalLinkage:
1224 GV->setLinkage(GlobalValue::ExternalLinkage);
1226 case LLVMAvailableExternallyLinkage:
1227 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1229 case LLVMLinkOnceAnyLinkage:
1230 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1232 case LLVMLinkOnceODRLinkage:
1233 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1235 case LLVMLinkOnceODRAutoHideLinkage:
1236 DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1237 "longer supported.");
1239 case LLVMWeakAnyLinkage:
1240 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1242 case LLVMWeakODRLinkage:
1243 GV->setLinkage(GlobalValue::WeakODRLinkage);
1245 case LLVMAppendingLinkage:
1246 GV->setLinkage(GlobalValue::AppendingLinkage);
1248 case LLVMInternalLinkage:
1249 GV->setLinkage(GlobalValue::InternalLinkage);
1251 case LLVMPrivateLinkage:
1252 GV->setLinkage(GlobalValue::PrivateLinkage);
1254 case LLVMLinkerPrivateLinkage:
1255 GV->setLinkage(GlobalValue::PrivateLinkage);
1257 case LLVMLinkerPrivateWeakLinkage:
1258 GV->setLinkage(GlobalValue::PrivateLinkage);
1260 case LLVMDLLImportLinkage:
1262 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1264 case LLVMDLLExportLinkage:
1266 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1268 case LLVMExternalWeakLinkage:
1269 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1271 case LLVMGhostLinkage:
1273 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1275 case LLVMCommonLinkage:
1276 GV->setLinkage(GlobalValue::CommonLinkage);
1281 const char *LLVMGetSection(LLVMValueRef Global) {
1282 return unwrap<GlobalValue>(Global)->getSection().c_str();
1285 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1286 unwrap<GlobalObject>(Global)->setSection(Section);
1289 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1290 return static_cast<LLVMVisibility>(
1291 unwrap<GlobalValue>(Global)->getVisibility());
1294 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1295 unwrap<GlobalValue>(Global)
1296 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1299 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
1300 return static_cast<LLVMDLLStorageClass>(
1301 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1304 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
1305 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1306 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1309 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
1310 return unwrap<GlobalValue>(Global)->hasUnnamedAddr();
1313 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
1314 unwrap<GlobalValue>(Global)->setUnnamedAddr(HasUnnamedAddr);
1317 /*--.. Operations on global variables, load and store instructions .........--*/
1319 unsigned LLVMGetAlignment(LLVMValueRef V) {
1320 Value *P = unwrap<Value>(V);
1321 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1322 return GV->getAlignment();
1323 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1324 return AI->getAlignment();
1325 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1326 return LI->getAlignment();
1327 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1328 return SI->getAlignment();
1331 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1334 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1335 Value *P = unwrap<Value>(V);
1336 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
1337 GV->setAlignment(Bytes);
1338 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1339 AI->setAlignment(Bytes);
1340 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1341 LI->setAlignment(Bytes);
1342 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1343 SI->setAlignment(Bytes);
1346 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1349 /*--.. Operations on global variables ......................................--*/
1351 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1352 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1353 GlobalValue::ExternalLinkage, nullptr, Name));
1356 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1358 unsigned AddressSpace) {
1359 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1360 GlobalValue::ExternalLinkage, nullptr, Name,
1361 nullptr, GlobalVariable::NotThreadLocal,
1365 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1366 return wrap(unwrap(M)->getNamedGlobal(Name));
1369 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1370 Module *Mod = unwrap(M);
1371 Module::global_iterator I = Mod->global_begin();
1372 if (I == Mod->global_end())
1377 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1378 Module *Mod = unwrap(M);
1379 Module::global_iterator I = Mod->global_end();
1380 if (I == Mod->global_begin())
1385 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1386 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1387 Module::global_iterator I = GV;
1388 if (++I == GV->getParent()->global_end())
1393 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1394 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1395 Module::global_iterator I = GV;
1396 if (I == GV->getParent()->global_begin())
1401 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1402 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1405 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1406 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1407 if ( !GV->hasInitializer() )
1409 return wrap(GV->getInitializer());
1412 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1413 unwrap<GlobalVariable>(GlobalVar)
1414 ->setInitializer(unwrap<Constant>(ConstantVal));
1417 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1418 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1421 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1422 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1425 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1426 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1429 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1430 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1433 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1434 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1435 case GlobalVariable::NotThreadLocal:
1436 return LLVMNotThreadLocal;
1437 case GlobalVariable::GeneralDynamicTLSModel:
1438 return LLVMGeneralDynamicTLSModel;
1439 case GlobalVariable::LocalDynamicTLSModel:
1440 return LLVMLocalDynamicTLSModel;
1441 case GlobalVariable::InitialExecTLSModel:
1442 return LLVMInitialExecTLSModel;
1443 case GlobalVariable::LocalExecTLSModel:
1444 return LLVMLocalExecTLSModel;
1447 llvm_unreachable("Invalid GlobalVariable thread local mode");
1450 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1451 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1454 case LLVMNotThreadLocal:
1455 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1457 case LLVMGeneralDynamicTLSModel:
1458 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1460 case LLVMLocalDynamicTLSModel:
1461 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1463 case LLVMInitialExecTLSModel:
1464 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1466 case LLVMLocalExecTLSModel:
1467 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1472 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1473 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1476 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1477 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1480 /*--.. Operations on aliases ......................................--*/
1482 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1484 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1485 unwrap<Constant>(Aliasee), unwrap (M)));
1488 /*--.. Operations on functions .............................................--*/
1490 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1491 LLVMTypeRef FunctionTy) {
1492 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1493 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1496 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1497 return wrap(unwrap(M)->getFunction(Name));
1500 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1501 Module *Mod = unwrap(M);
1502 Module::iterator I = Mod->begin();
1503 if (I == Mod->end())
1508 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1509 Module *Mod = unwrap(M);
1510 Module::iterator I = Mod->end();
1511 if (I == Mod->begin())
1516 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1517 Function *Func = unwrap<Function>(Fn);
1518 Module::iterator I = Func;
1519 if (++I == Func->getParent()->end())
1524 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1525 Function *Func = unwrap<Function>(Fn);
1526 Module::iterator I = Func;
1527 if (I == Func->getParent()->begin())
1532 void LLVMDeleteFunction(LLVMValueRef Fn) {
1533 unwrap<Function>(Fn)->eraseFromParent();
1536 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1537 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1538 return F->getIntrinsicID();
1542 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1543 return unwrap<Function>(Fn)->getCallingConv();
1546 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1547 return unwrap<Function>(Fn)->setCallingConv(
1548 static_cast<CallingConv::ID>(CC));
1551 const char *LLVMGetGC(LLVMValueRef Fn) {
1552 Function *F = unwrap<Function>(Fn);
1553 return F->hasGC()? F->getGC() : nullptr;
1556 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1557 Function *F = unwrap<Function>(Fn);
1564 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1565 Function *Func = unwrap<Function>(Fn);
1566 const AttributeSet PAL = Func->getAttributes();
1568 const AttributeSet PALnew =
1569 PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1570 AttributeSet::get(Func->getContext(),
1571 AttributeSet::FunctionIndex, B));
1572 Func->setAttributes(PALnew);
1575 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1577 Function *Func = unwrap<Function>(Fn);
1578 AttributeSet::AttrIndex Idx =
1579 AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
1582 B.addAttribute(A, V);
1583 AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
1584 Func->addAttributes(Idx, Set);
1587 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1588 Function *Func = unwrap<Function>(Fn);
1589 const AttributeSet PAL = Func->getAttributes();
1591 const AttributeSet PALnew =
1592 PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1593 AttributeSet::get(Func->getContext(),
1594 AttributeSet::FunctionIndex, B));
1595 Func->setAttributes(PALnew);
1598 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1599 Function *Func = unwrap<Function>(Fn);
1600 const AttributeSet PAL = Func->getAttributes();
1601 return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
1604 /*--.. Operations on parameters ............................................--*/
1606 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1607 // This function is strictly redundant to
1608 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1609 return unwrap<Function>(FnRef)->arg_size();
1612 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1613 Function *Fn = unwrap<Function>(FnRef);
1614 for (Function::arg_iterator I = Fn->arg_begin(),
1615 E = Fn->arg_end(); I != E; I++)
1616 *ParamRefs++ = wrap(I);
1619 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1620 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1626 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1627 return wrap(unwrap<Argument>(V)->getParent());
1630 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1631 Function *Func = unwrap<Function>(Fn);
1632 Function::arg_iterator I = Func->arg_begin();
1633 if (I == Func->arg_end())
1638 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1639 Function *Func = unwrap<Function>(Fn);
1640 Function::arg_iterator I = Func->arg_end();
1641 if (I == Func->arg_begin())
1646 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1647 Argument *A = unwrap<Argument>(Arg);
1648 Function::arg_iterator I = A;
1649 if (++I == A->getParent()->arg_end())
1654 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1655 Argument *A = unwrap<Argument>(Arg);
1656 Function::arg_iterator I = A;
1657 if (I == A->getParent()->arg_begin())
1662 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1663 Argument *A = unwrap<Argument>(Arg);
1665 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1668 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1669 Argument *A = unwrap<Argument>(Arg);
1671 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1674 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1675 Argument *A = unwrap<Argument>(Arg);
1676 return (LLVMAttribute)A->getParent()->getAttributes().
1677 Raw(A->getArgNo()+1);
1681 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1682 Argument *A = unwrap<Argument>(Arg);
1684 B.addAlignmentAttr(align);
1685 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
1688 /*--.. Operations on basic blocks ..........................................--*/
1690 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1691 return wrap(static_cast<Value*>(unwrap(BB)));
1694 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1695 return isa<BasicBlock>(unwrap(Val));
1698 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1699 return wrap(unwrap<BasicBlock>(Val));
1702 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1703 return wrap(unwrap(BB)->getParent());
1706 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1707 return wrap(unwrap(BB)->getTerminator());
1710 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1711 return unwrap<Function>(FnRef)->size();
1714 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1715 Function *Fn = unwrap<Function>(FnRef);
1716 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1717 *BasicBlocksRefs++ = wrap(I);
1720 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1721 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1724 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1725 Function *Func = unwrap<Function>(Fn);
1726 Function::iterator I = Func->begin();
1727 if (I == Func->end())
1732 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1733 Function *Func = unwrap<Function>(Fn);
1734 Function::iterator I = Func->end();
1735 if (I == Func->begin())
1740 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1741 BasicBlock *Block = unwrap(BB);
1742 Function::iterator I = Block;
1743 if (++I == Block->getParent()->end())
1748 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1749 BasicBlock *Block = unwrap(BB);
1750 Function::iterator I = Block;
1751 if (I == Block->getParent()->begin())
1756 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1759 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1762 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1763 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1766 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1767 LLVMBasicBlockRef BBRef,
1769 BasicBlock *BB = unwrap(BBRef);
1770 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1773 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1775 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1778 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1779 unwrap(BBRef)->eraseFromParent();
1782 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1783 unwrap(BBRef)->removeFromParent();
1786 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1787 unwrap(BB)->moveBefore(unwrap(MovePos));
1790 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1791 unwrap(BB)->moveAfter(unwrap(MovePos));
1794 /*--.. Operations on instructions ..........................................--*/
1796 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1797 return wrap(unwrap<Instruction>(Inst)->getParent());
1800 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1801 BasicBlock *Block = unwrap(BB);
1802 BasicBlock::iterator I = Block->begin();
1803 if (I == Block->end())
1808 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1809 BasicBlock *Block = unwrap(BB);
1810 BasicBlock::iterator I = Block->end();
1811 if (I == Block->begin())
1816 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1817 Instruction *Instr = unwrap<Instruction>(Inst);
1818 BasicBlock::iterator I = Instr;
1819 if (++I == Instr->getParent()->end())
1824 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1825 Instruction *Instr = unwrap<Instruction>(Inst);
1826 BasicBlock::iterator I = Instr;
1827 if (I == Instr->getParent()->begin())
1832 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1833 unwrap<Instruction>(Inst)->eraseFromParent();
1836 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
1837 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
1838 return (LLVMIntPredicate)I->getPredicate();
1839 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1840 if (CE->getOpcode() == Instruction::ICmp)
1841 return (LLVMIntPredicate)CE->getPredicate();
1842 return (LLVMIntPredicate)0;
1845 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
1846 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
1847 return map_to_llvmopcode(C->getOpcode());
1848 return (LLVMOpcode)0;
1851 /*--.. Call and invoke instructions ........................................--*/
1853 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1854 Value *V = unwrap(Instr);
1855 if (CallInst *CI = dyn_cast<CallInst>(V))
1856 return CI->getCallingConv();
1857 if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1858 return II->getCallingConv();
1859 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1862 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1863 Value *V = unwrap(Instr);
1864 if (CallInst *CI = dyn_cast<CallInst>(V))
1865 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1866 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1867 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1868 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1871 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1873 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1876 Call.getAttributes().addAttributes(Call->getContext(), index,
1877 AttributeSet::get(Call->getContext(),
1881 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1883 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1885 Call.setAttributes(Call.getAttributes()
1886 .removeAttributes(Call->getContext(), index,
1887 AttributeSet::get(Call->getContext(),
1891 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1893 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1895 B.addAlignmentAttr(align);
1896 Call.setAttributes(Call.getAttributes()
1897 .addAttributes(Call->getContext(), index,
1898 AttributeSet::get(Call->getContext(),
1902 /*--.. Operations on call instructions (only) ..............................--*/
1904 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1905 return unwrap<CallInst>(Call)->isTailCall();
1908 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1909 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1912 /*--.. Operations on switch instructions (only) ............................--*/
1914 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
1915 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
1918 /*--.. Operations on phi nodes .............................................--*/
1920 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1921 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1922 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1923 for (unsigned I = 0; I != Count; ++I)
1924 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1927 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1928 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1931 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1932 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1935 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1936 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1940 /*===-- Instruction builders ----------------------------------------------===*/
1942 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1943 return wrap(new IRBuilder<>(*unwrap(C)));
1946 LLVMBuilderRef LLVMCreateBuilder(void) {
1947 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1950 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1951 LLVMValueRef Instr) {
1952 BasicBlock *BB = unwrap(Block);
1953 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1954 unwrap(Builder)->SetInsertPoint(BB, I);
1957 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1958 Instruction *I = unwrap<Instruction>(Instr);
1959 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1962 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1963 BasicBlock *BB = unwrap(Block);
1964 unwrap(Builder)->SetInsertPoint(BB);
1967 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1968 return wrap(unwrap(Builder)->GetInsertBlock());
1971 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1972 unwrap(Builder)->ClearInsertionPoint();
1975 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1976 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1979 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1981 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1984 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1985 delete unwrap(Builder);
1988 /*--.. Metadata builders ...................................................--*/
1990 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1991 MDNode *Loc = L ? unwrap<MDNode>(L) : nullptr;
1992 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1995 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1996 return wrap(unwrap(Builder)->getCurrentDebugLocation()
1997 .getAsMDNode(unwrap(Builder)->getContext()));
2000 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
2001 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
2005 /*--.. Instruction builders ................................................--*/
2007 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
2008 return wrap(unwrap(B)->CreateRetVoid());
2011 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
2012 return wrap(unwrap(B)->CreateRet(unwrap(V)));
2015 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
2017 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
2020 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
2021 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
2024 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
2025 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
2026 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
2029 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
2030 LLVMBasicBlockRef Else, unsigned NumCases) {
2031 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
2034 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
2035 unsigned NumDests) {
2036 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
2039 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
2040 LLVMValueRef *Args, unsigned NumArgs,
2041 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
2043 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
2044 makeArrayRef(unwrap(Args), NumArgs),
2048 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
2049 LLVMValueRef PersFn, unsigned NumClauses,
2051 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
2052 cast<Function>(unwrap(PersFn)),
2056 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
2057 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
2060 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
2061 return wrap(unwrap(B)->CreateUnreachable());
2064 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
2065 LLVMBasicBlockRef Dest) {
2066 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
2069 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
2070 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
2073 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
2074 unwrap<LandingPadInst>(LandingPad)->
2075 addClause(cast<Constant>(unwrap(ClauseVal)));
2078 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
2079 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2082 /*--.. Arithmetic ..........................................................--*/
2084 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2086 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2089 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2091 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2094 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2096 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2099 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2101 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2104 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2106 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2109 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2111 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2114 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2116 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2119 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2121 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2124 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2126 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2129 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2131 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2134 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2136 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2139 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2141 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2144 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2146 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2149 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2151 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2154 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2155 LLVMValueRef RHS, const char *Name) {
2156 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2159 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2161 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2164 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2166 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2169 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2171 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2174 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2176 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2179 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2181 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2184 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2186 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2189 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2191 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2194 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2196 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2199 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2201 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2204 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2206 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2209 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2210 LLVMValueRef LHS, LLVMValueRef RHS,
2212 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2213 unwrap(RHS), Name));
2216 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2217 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2220 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2222 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2225 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2227 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2230 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2231 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2234 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2235 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2238 /*--.. Memory ..............................................................--*/
2240 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2242 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2243 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2244 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2245 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2246 ITy, unwrap(Ty), AllocSize,
2247 nullptr, nullptr, "");
2248 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2251 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2252 LLVMValueRef Val, const char *Name) {
2253 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2254 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2255 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2256 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2257 ITy, unwrap(Ty), AllocSize,
2258 unwrap(Val), nullptr, "");
2259 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2262 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2264 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
2267 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2268 LLVMValueRef Val, const char *Name) {
2269 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2272 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2273 return wrap(unwrap(B)->Insert(
2274 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2278 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2280 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2283 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2284 LLVMValueRef PointerVal) {
2285 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2288 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
2290 case LLVMAtomicOrderingNotAtomic: return NotAtomic;
2291 case LLVMAtomicOrderingUnordered: return Unordered;
2292 case LLVMAtomicOrderingMonotonic: return Monotonic;
2293 case LLVMAtomicOrderingAcquire: return Acquire;
2294 case LLVMAtomicOrderingRelease: return Release;
2295 case LLVMAtomicOrderingAcquireRelease: return AcquireRelease;
2296 case LLVMAtomicOrderingSequentiallyConsistent:
2297 return SequentiallyConsistent;
2300 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
2303 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
2304 LLVMBool isSingleThread, const char *Name) {
2306 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
2307 isSingleThread ? SingleThread : CrossThread,
2311 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2312 LLVMValueRef *Indices, unsigned NumIndices,
2314 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2315 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
2318 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2319 LLVMValueRef *Indices, unsigned NumIndices,
2321 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2322 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
2325 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2326 unsigned Idx, const char *Name) {
2327 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2330 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2332 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2335 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2337 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2340 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2341 Value *P = unwrap<Value>(MemAccessInst);
2342 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2343 return LI->isVolatile();
2344 return cast<StoreInst>(P)->isVolatile();
2347 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2348 Value *P = unwrap<Value>(MemAccessInst);
2349 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2350 return LI->setVolatile(isVolatile);
2351 return cast<StoreInst>(P)->setVolatile(isVolatile);
2354 /*--.. Casts ...............................................................--*/
2356 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2357 LLVMTypeRef DestTy, const char *Name) {
2358 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2361 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2362 LLVMTypeRef DestTy, const char *Name) {
2363 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2366 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2367 LLVMTypeRef DestTy, const char *Name) {
2368 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2371 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2372 LLVMTypeRef DestTy, const char *Name) {
2373 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2376 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2377 LLVMTypeRef DestTy, const char *Name) {
2378 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2381 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2382 LLVMTypeRef DestTy, const char *Name) {
2383 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2386 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2387 LLVMTypeRef DestTy, const char *Name) {
2388 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2391 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2392 LLVMTypeRef DestTy, const char *Name) {
2393 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2396 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2397 LLVMTypeRef DestTy, const char *Name) {
2398 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2401 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2402 LLVMTypeRef DestTy, const char *Name) {
2403 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2406 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2407 LLVMTypeRef DestTy, const char *Name) {
2408 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2411 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2412 LLVMTypeRef DestTy, const char *Name) {
2413 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2416 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
2417 LLVMTypeRef DestTy, const char *Name) {
2418 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
2421 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2422 LLVMTypeRef DestTy, const char *Name) {
2423 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2427 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2428 LLVMTypeRef DestTy, const char *Name) {
2429 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2433 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2434 LLVMTypeRef DestTy, const char *Name) {
2435 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2439 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2440 LLVMTypeRef DestTy, const char *Name) {
2441 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2442 unwrap(DestTy), Name));
2445 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2446 LLVMTypeRef DestTy, const char *Name) {
2447 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2450 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2451 LLVMTypeRef DestTy, const char *Name) {
2452 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2453 /*isSigned*/true, Name));
2456 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2457 LLVMTypeRef DestTy, const char *Name) {
2458 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2461 /*--.. Comparisons .........................................................--*/
2463 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2464 LLVMValueRef LHS, LLVMValueRef RHS,
2466 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2467 unwrap(LHS), unwrap(RHS), Name));
2470 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2471 LLVMValueRef LHS, LLVMValueRef RHS,
2473 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2474 unwrap(LHS), unwrap(RHS), Name));
2477 /*--.. Miscellaneous instructions ..........................................--*/
2479 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2480 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2483 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2484 LLVMValueRef *Args, unsigned NumArgs,
2486 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2487 makeArrayRef(unwrap(Args), NumArgs),
2491 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2492 LLVMValueRef Then, LLVMValueRef Else,
2494 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2498 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2499 LLVMTypeRef Ty, const char *Name) {
2500 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2503 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2504 LLVMValueRef Index, const char *Name) {
2505 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2509 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2510 LLVMValueRef EltVal, LLVMValueRef Index,
2512 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2513 unwrap(Index), Name));
2516 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2517 LLVMValueRef V2, LLVMValueRef Mask,
2519 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2520 unwrap(Mask), Name));
2523 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2524 unsigned Index, const char *Name) {
2525 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2528 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2529 LLVMValueRef EltVal, unsigned Index,
2531 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2535 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2537 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2540 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2542 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2545 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2546 LLVMValueRef RHS, const char *Name) {
2547 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2550 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
2551 LLVMValueRef PTR, LLVMValueRef Val,
2552 LLVMAtomicOrdering ordering,
2553 LLVMBool singleThread) {
2554 AtomicRMWInst::BinOp intop;
2556 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
2557 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
2558 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
2559 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
2560 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
2561 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
2562 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
2563 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
2564 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
2565 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
2566 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
2568 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
2569 mapFromLLVMOrdering(ordering), singleThread ? SingleThread : CrossThread));
2573 /*===-- Module providers --------------------------------------------------===*/
2575 LLVMModuleProviderRef
2576 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2577 return reinterpret_cast<LLVMModuleProviderRef>(M);
2580 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2585 /*===-- Memory buffers ----------------------------------------------------===*/
2587 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2589 LLVMMemoryBufferRef *OutMemBuf,
2590 char **OutMessage) {
2592 std::unique_ptr<MemoryBuffer> MB;
2594 if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2595 *OutMemBuf = wrap(MB.release());
2599 *OutMessage = strdup(ec.message().c_str());
2603 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2604 char **OutMessage) {
2605 std::unique_ptr<MemoryBuffer> MB;
2607 if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2608 *OutMemBuf = wrap(MB.release());
2612 *OutMessage = strdup(ec.message().c_str());
2616 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
2617 const char *InputData,
2618 size_t InputDataLength,
2619 const char *BufferName,
2620 LLVMBool RequiresNullTerminator) {
2622 return wrap(MemoryBuffer::getMemBuffer(
2623 StringRef(InputData, InputDataLength),
2624 StringRef(BufferName),
2625 RequiresNullTerminator));
2628 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
2629 const char *InputData,
2630 size_t InputDataLength,
2631 const char *BufferName) {
2633 return wrap(MemoryBuffer::getMemBufferCopy(
2634 StringRef(InputData, InputDataLength),
2635 StringRef(BufferName)));
2638 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
2639 return unwrap(MemBuf)->getBufferStart();
2642 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
2643 return unwrap(MemBuf)->getBufferSize();
2646 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2647 delete unwrap(MemBuf);
2650 /*===-- Pass Registry -----------------------------------------------------===*/
2652 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2653 return wrap(PassRegistry::getPassRegistry());
2656 /*===-- Pass Manager ------------------------------------------------------===*/
2658 LLVMPassManagerRef LLVMCreatePassManager() {
2659 return wrap(new PassManager());
2662 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2663 return wrap(new FunctionPassManager(unwrap(M)));
2666 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2667 return LLVMCreateFunctionPassManagerForModule(
2668 reinterpret_cast<LLVMModuleRef>(P));
2671 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2672 return unwrap<PassManager>(PM)->run(*unwrap(M));
2675 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2676 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2679 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2680 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2683 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2684 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2687 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
2691 /*===-- Threading ------------------------------------------------------===*/
2693 LLVMBool LLVMStartMultithreaded() {
2694 return llvm_start_multithreaded();
2697 void LLVMStopMultithreaded() {
2698 llvm_stop_multithreaded();
2701 LLVMBool LLVMIsMultithreaded() {
2702 return llvm_is_multithreaded();