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 LLVMContextSetYieldCallback(LLVMContextRef C, LLVMYieldCallback Callback,
95 LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
96 unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
99 void LLVMContextDispose(LLVMContextRef C) {
103 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name,
105 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
108 unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) {
109 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
112 char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) {
113 std::string MsgStorage;
114 raw_string_ostream Stream(MsgStorage);
115 DiagnosticPrinterRawOStream DP(Stream);
117 unwrap(DI)->print(DP);
120 return LLVMCreateMessage(MsgStorage.c_str());
123 LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI){
124 LLVMDiagnosticSeverity severity;
126 switch(unwrap(DI)->getSeverity()) {
128 severity = LLVMDSError;
131 severity = LLVMDSWarning;
134 severity = LLVMDSRemark;
137 severity = LLVMDSNote;
147 /*===-- Operations on modules ---------------------------------------------===*/
149 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
150 return wrap(new Module(ModuleID, getGlobalContext()));
153 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
155 return wrap(new Module(ModuleID, *unwrap(C)));
158 void LLVMDisposeModule(LLVMModuleRef M) {
162 /*--.. Data layout .........................................................--*/
163 const char * LLVMGetDataLayout(LLVMModuleRef M) {
164 return unwrap(M)->getDataLayoutStr().c_str();
167 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
168 unwrap(M)->setDataLayout(Triple);
171 /*--.. Target triple .......................................................--*/
172 const char * LLVMGetTarget(LLVMModuleRef M) {
173 return unwrap(M)->getTargetTriple().c_str();
176 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
177 unwrap(M)->setTargetTriple(Triple);
180 void LLVMDumpModule(LLVMModuleRef M) {
184 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
185 char **ErrorMessage) {
187 raw_fd_ostream dest(Filename, error, sys::fs::F_Text);
188 if (!error.empty()) {
189 *ErrorMessage = strdup(error.c_str());
193 unwrap(M)->print(dest, nullptr);
195 if (!error.empty()) {
196 *ErrorMessage = strdup(error.c_str());
203 char *LLVMPrintModuleToString(LLVMModuleRef M) {
205 raw_string_ostream os(buf);
207 unwrap(M)->print(os, nullptr);
210 return strdup(buf.c_str());
213 /*--.. Operations on inline assembler ......................................--*/
214 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
215 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
219 /*--.. Operations on module contexts ......................................--*/
220 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
221 return wrap(&unwrap(M)->getContext());
225 /*===-- Operations on types -----------------------------------------------===*/
227 /*--.. Operations on all types (mostly) ....................................--*/
229 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
230 switch (unwrap(Ty)->getTypeID()) {
232 return LLVMVoidTypeKind;
234 return LLVMHalfTypeKind;
235 case Type::FloatTyID:
236 return LLVMFloatTypeKind;
237 case Type::DoubleTyID:
238 return LLVMDoubleTypeKind;
239 case Type::X86_FP80TyID:
240 return LLVMX86_FP80TypeKind;
241 case Type::FP128TyID:
242 return LLVMFP128TypeKind;
243 case Type::PPC_FP128TyID:
244 return LLVMPPC_FP128TypeKind;
245 case Type::LabelTyID:
246 return LLVMLabelTypeKind;
247 case Type::MetadataTyID:
248 return LLVMMetadataTypeKind;
249 case Type::IntegerTyID:
250 return LLVMIntegerTypeKind;
251 case Type::FunctionTyID:
252 return LLVMFunctionTypeKind;
253 case Type::StructTyID:
254 return LLVMStructTypeKind;
255 case Type::ArrayTyID:
256 return LLVMArrayTypeKind;
257 case Type::PointerTyID:
258 return LLVMPointerTypeKind;
259 case Type::VectorTyID:
260 return LLVMVectorTypeKind;
261 case Type::X86_MMXTyID:
262 return LLVMX86_MMXTypeKind;
264 llvm_unreachable("Unhandled TypeID.");
267 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
269 return unwrap(Ty)->isSized();
272 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
273 return wrap(&unwrap(Ty)->getContext());
276 void LLVMDumpType(LLVMTypeRef Ty) {
277 return unwrap(Ty)->dump();
280 char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
282 raw_string_ostream os(buf);
284 unwrap(Ty)->print(os);
287 return strdup(buf.c_str());
290 /*--.. Operations on integer types .........................................--*/
292 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
293 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
295 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
296 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
298 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
299 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
301 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
302 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
304 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
305 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
307 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
308 return wrap(IntegerType::get(*unwrap(C), NumBits));
311 LLVMTypeRef LLVMInt1Type(void) {
312 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
314 LLVMTypeRef LLVMInt8Type(void) {
315 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
317 LLVMTypeRef LLVMInt16Type(void) {
318 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
320 LLVMTypeRef LLVMInt32Type(void) {
321 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
323 LLVMTypeRef LLVMInt64Type(void) {
324 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
326 LLVMTypeRef LLVMIntType(unsigned NumBits) {
327 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
330 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
331 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
334 /*--.. Operations on real types ............................................--*/
336 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
337 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
339 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
340 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
342 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
343 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
345 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
346 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
348 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
349 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
351 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
352 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
354 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
355 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
358 LLVMTypeRef LLVMHalfType(void) {
359 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
361 LLVMTypeRef LLVMFloatType(void) {
362 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
364 LLVMTypeRef LLVMDoubleType(void) {
365 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
367 LLVMTypeRef LLVMX86FP80Type(void) {
368 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
370 LLVMTypeRef LLVMFP128Type(void) {
371 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
373 LLVMTypeRef LLVMPPCFP128Type(void) {
374 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
376 LLVMTypeRef LLVMX86MMXType(void) {
377 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
380 /*--.. Operations on function types ........................................--*/
382 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
383 LLVMTypeRef *ParamTypes, unsigned ParamCount,
385 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
386 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
389 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
390 return unwrap<FunctionType>(FunctionTy)->isVarArg();
393 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
394 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
397 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
398 return unwrap<FunctionType>(FunctionTy)->getNumParams();
401 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
402 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
403 for (FunctionType::param_iterator I = Ty->param_begin(),
404 E = Ty->param_end(); I != E; ++I)
408 /*--.. Operations on struct types ..........................................--*/
410 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
411 unsigned ElementCount, LLVMBool Packed) {
412 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
413 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
416 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
417 unsigned ElementCount, LLVMBool Packed) {
418 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
419 ElementCount, Packed);
422 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
424 return wrap(StructType::create(*unwrap(C), Name));
427 const char *LLVMGetStructName(LLVMTypeRef Ty)
429 StructType *Type = unwrap<StructType>(Ty);
430 if (!Type->hasName())
432 return Type->getName().data();
435 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
436 unsigned ElementCount, LLVMBool Packed) {
437 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
438 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
441 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
442 return unwrap<StructType>(StructTy)->getNumElements();
445 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
446 StructType *Ty = unwrap<StructType>(StructTy);
447 for (StructType::element_iterator I = Ty->element_begin(),
448 E = Ty->element_end(); I != E; ++I)
452 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
453 return unwrap<StructType>(StructTy)->isPacked();
456 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
457 return unwrap<StructType>(StructTy)->isOpaque();
460 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
461 return wrap(unwrap(M)->getTypeByName(Name));
464 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
466 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
467 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
470 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
471 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
474 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
475 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
478 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
479 return wrap(unwrap<SequentialType>(Ty)->getElementType());
482 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
483 return unwrap<ArrayType>(ArrayTy)->getNumElements();
486 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
487 return unwrap<PointerType>(PointerTy)->getAddressSpace();
490 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
491 return unwrap<VectorType>(VectorTy)->getNumElements();
494 /*--.. Operations on other types ...........................................--*/
496 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
497 return wrap(Type::getVoidTy(*unwrap(C)));
499 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
500 return wrap(Type::getLabelTy(*unwrap(C)));
503 LLVMTypeRef LLVMVoidType(void) {
504 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
506 LLVMTypeRef LLVMLabelType(void) {
507 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
510 /*===-- Operations on values ----------------------------------------------===*/
512 /*--.. Operations on all values ............................................--*/
514 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
515 return wrap(unwrap(Val)->getType());
518 const char *LLVMGetValueName(LLVMValueRef Val) {
519 return unwrap(Val)->getName().data();
522 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
523 unwrap(Val)->setName(Name);
526 void LLVMDumpValue(LLVMValueRef Val) {
530 char* LLVMPrintValueToString(LLVMValueRef Val) {
532 raw_string_ostream os(buf);
534 unwrap(Val)->print(os);
537 return strdup(buf.c_str());
540 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
541 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
544 int LLVMHasMetadata(LLVMValueRef Inst) {
545 return unwrap<Instruction>(Inst)->hasMetadata();
548 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
549 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
552 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
553 unwrap<Instruction>(Inst)->setMetadata(KindID,
554 MD ? unwrap<MDNode>(MD) : nullptr);
557 /*--.. Conversion functions ................................................--*/
559 #define LLVM_DEFINE_VALUE_CAST(name) \
560 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
561 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
564 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
566 /*--.. Operations on Uses ..................................................--*/
567 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
568 Value *V = unwrap(Val);
569 Value::use_iterator I = V->use_begin();
570 if (I == V->use_end())
575 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
576 Use *Next = unwrap(U)->getNext();
582 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
583 return wrap(unwrap(U)->getUser());
586 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
587 return wrap(unwrap(U)->get());
590 /*--.. Operations on Users .................................................--*/
591 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
592 Value *V = unwrap(Val);
593 if (MDNode *MD = dyn_cast<MDNode>(V))
594 return wrap(MD->getOperand(Index));
595 return wrap(cast<User>(V)->getOperand(Index));
598 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
599 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
602 int LLVMGetNumOperands(LLVMValueRef Val) {
603 Value *V = unwrap(Val);
604 if (MDNode *MD = dyn_cast<MDNode>(V))
605 return MD->getNumOperands();
606 return cast<User>(V)->getNumOperands();
609 /*--.. Operations on constants of any type .................................--*/
611 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
612 return wrap(Constant::getNullValue(unwrap(Ty)));
615 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
616 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
619 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
620 return wrap(UndefValue::get(unwrap(Ty)));
623 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
624 return isa<Constant>(unwrap(Ty));
627 LLVMBool LLVMIsNull(LLVMValueRef Val) {
628 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
629 return C->isNullValue();
633 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
634 return isa<UndefValue>(unwrap(Val));
637 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
639 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
642 /*--.. Operations on metadata nodes ........................................--*/
644 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
646 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
649 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
650 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
653 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
655 return wrap(MDNode::get(*unwrap(C),
656 makeArrayRef(unwrap<Value>(Vals, Count), Count)));
659 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
660 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
663 const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
664 if (const MDString *S = dyn_cast<MDString>(unwrap(V))) {
665 *Len = S->getString().size();
666 return S->getString().data();
672 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V)
674 return cast<MDNode>(unwrap(V))->getNumOperands();
677 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest)
679 const MDNode *N = cast<MDNode>(unwrap(V));
680 const unsigned numOperands = N->getNumOperands();
681 for (unsigned i = 0; i < numOperands; i++)
682 Dest[i] = wrap(N->getOperand(i));
685 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
687 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
688 return N->getNumOperands();
693 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
695 NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
698 for (unsigned i=0;i<N->getNumOperands();i++)
699 Dest[i] = wrap(N->getOperand(i));
702 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
705 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
708 MDNode *Op = Val ? unwrap<MDNode>(Val) : nullptr;
713 /*--.. Operations on scalar constants ......................................--*/
715 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
716 LLVMBool SignExtend) {
717 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
720 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
722 const uint64_t Words[]) {
723 IntegerType *Ty = unwrap<IntegerType>(IntTy);
724 return wrap(ConstantInt::get(Ty->getContext(),
725 APInt(Ty->getBitWidth(),
726 makeArrayRef(Words, NumWords))));
729 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
731 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
735 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
736 unsigned SLen, uint8_t Radix) {
737 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
741 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
742 return wrap(ConstantFP::get(unwrap(RealTy), N));
745 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
746 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
749 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
751 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
754 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
755 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
758 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
759 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
762 /*--.. Operations on composite constants ...................................--*/
764 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
766 LLVMBool DontNullTerminate) {
767 /* Inverted the sense of AddNull because ', 0)' is a
768 better mnemonic for null termination than ', 1)'. */
769 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
770 DontNullTerminate == 0));
772 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
773 LLVMValueRef *ConstantVals,
774 unsigned Count, LLVMBool Packed) {
775 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
776 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
780 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
781 LLVMBool DontNullTerminate) {
782 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
785 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
786 LLVMValueRef *ConstantVals, unsigned Length) {
787 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
788 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
790 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
792 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
796 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
797 LLVMValueRef *ConstantVals,
799 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
800 StructType *Ty = cast<StructType>(unwrap(StructTy));
802 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
805 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
806 return wrap(ConstantVector::get(makeArrayRef(
807 unwrap<Constant>(ScalarConstantVals, Size), Size)));
810 /*-- Opcode mapping */
812 static LLVMOpcode map_to_llvmopcode(int opcode)
815 default: llvm_unreachable("Unhandled Opcode.");
816 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
817 #include "llvm/IR/Instruction.def"
822 static int map_from_llvmopcode(LLVMOpcode code)
825 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
826 #include "llvm/IR/Instruction.def"
829 llvm_unreachable("Unhandled Opcode.");
832 /*--.. Constant expressions ................................................--*/
834 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
835 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
838 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
839 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
842 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
843 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
846 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
847 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
850 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
851 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
854 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
855 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
859 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
860 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
863 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
864 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
867 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
868 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
869 unwrap<Constant>(RHSConstant)));
872 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
873 LLVMValueRef RHSConstant) {
874 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
875 unwrap<Constant>(RHSConstant)));
878 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
879 LLVMValueRef RHSConstant) {
880 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
881 unwrap<Constant>(RHSConstant)));
884 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
885 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
886 unwrap<Constant>(RHSConstant)));
889 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
890 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
891 unwrap<Constant>(RHSConstant)));
894 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
895 LLVMValueRef RHSConstant) {
896 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
897 unwrap<Constant>(RHSConstant)));
900 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
901 LLVMValueRef RHSConstant) {
902 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
903 unwrap<Constant>(RHSConstant)));
906 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
907 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
908 unwrap<Constant>(RHSConstant)));
911 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
912 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
913 unwrap<Constant>(RHSConstant)));
916 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
917 LLVMValueRef RHSConstant) {
918 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
919 unwrap<Constant>(RHSConstant)));
922 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
923 LLVMValueRef RHSConstant) {
924 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
925 unwrap<Constant>(RHSConstant)));
928 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
929 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
930 unwrap<Constant>(RHSConstant)));
933 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
934 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
935 unwrap<Constant>(RHSConstant)));
938 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
939 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
940 unwrap<Constant>(RHSConstant)));
943 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
944 LLVMValueRef RHSConstant) {
945 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
946 unwrap<Constant>(RHSConstant)));
949 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
950 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
951 unwrap<Constant>(RHSConstant)));
954 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
955 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
956 unwrap<Constant>(RHSConstant)));
959 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
960 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
961 unwrap<Constant>(RHSConstant)));
964 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
965 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
966 unwrap<Constant>(RHSConstant)));
969 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
970 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
971 unwrap<Constant>(RHSConstant)));
974 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
975 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
976 unwrap<Constant>(RHSConstant)));
979 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
980 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
981 unwrap<Constant>(RHSConstant)));
984 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
985 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
986 return wrap(ConstantExpr::getICmp(Predicate,
987 unwrap<Constant>(LHSConstant),
988 unwrap<Constant>(RHSConstant)));
991 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
992 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
993 return wrap(ConstantExpr::getFCmp(Predicate,
994 unwrap<Constant>(LHSConstant),
995 unwrap<Constant>(RHSConstant)));
998 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
999 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1000 unwrap<Constant>(RHSConstant)));
1003 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1004 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
1005 unwrap<Constant>(RHSConstant)));
1008 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1009 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1010 unwrap<Constant>(RHSConstant)));
1013 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
1014 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1015 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1017 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
1021 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
1022 LLVMValueRef *ConstantIndices,
1023 unsigned NumIndices) {
1024 Constant* Val = unwrap<Constant>(ConstantVal);
1025 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1027 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
1030 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1031 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1035 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1036 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1040 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1041 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1045 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1046 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1050 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1051 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1055 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1056 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1060 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1061 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1065 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1066 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1070 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1071 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1075 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1076 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1080 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1081 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1085 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1086 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1090 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1091 LLVMTypeRef ToType) {
1092 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1096 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1097 LLVMTypeRef ToType) {
1098 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1102 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1103 LLVMTypeRef ToType) {
1104 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1108 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1109 LLVMTypeRef ToType) {
1110 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1114 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1115 LLVMTypeRef ToType) {
1116 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1120 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1121 LLVMBool isSigned) {
1122 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1123 unwrap(ToType), isSigned));
1126 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1127 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1131 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1132 LLVMValueRef ConstantIfTrue,
1133 LLVMValueRef ConstantIfFalse) {
1134 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1135 unwrap<Constant>(ConstantIfTrue),
1136 unwrap<Constant>(ConstantIfFalse)));
1139 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1140 LLVMValueRef IndexConstant) {
1141 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1142 unwrap<Constant>(IndexConstant)));
1145 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1146 LLVMValueRef ElementValueConstant,
1147 LLVMValueRef IndexConstant) {
1148 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1149 unwrap<Constant>(ElementValueConstant),
1150 unwrap<Constant>(IndexConstant)));
1153 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1154 LLVMValueRef VectorBConstant,
1155 LLVMValueRef MaskConstant) {
1156 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1157 unwrap<Constant>(VectorBConstant),
1158 unwrap<Constant>(MaskConstant)));
1161 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1163 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1164 makeArrayRef(IdxList, NumIdx)));
1167 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1168 LLVMValueRef ElementValueConstant,
1169 unsigned *IdxList, unsigned NumIdx) {
1170 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1171 unwrap<Constant>(ElementValueConstant),
1172 makeArrayRef(IdxList, NumIdx)));
1175 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1176 const char *Constraints,
1177 LLVMBool HasSideEffects,
1178 LLVMBool IsAlignStack) {
1179 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1180 Constraints, HasSideEffects, IsAlignStack));
1183 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1184 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1187 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1189 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1190 return wrap(unwrap<GlobalValue>(Global)->getParent());
1193 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1194 return unwrap<GlobalValue>(Global)->isDeclaration();
1197 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1198 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1199 case GlobalValue::ExternalLinkage:
1200 return LLVMExternalLinkage;
1201 case GlobalValue::AvailableExternallyLinkage:
1202 return LLVMAvailableExternallyLinkage;
1203 case GlobalValue::LinkOnceAnyLinkage:
1204 return LLVMLinkOnceAnyLinkage;
1205 case GlobalValue::LinkOnceODRLinkage:
1206 return LLVMLinkOnceODRLinkage;
1207 case GlobalValue::WeakAnyLinkage:
1208 return LLVMWeakAnyLinkage;
1209 case GlobalValue::WeakODRLinkage:
1210 return LLVMWeakODRLinkage;
1211 case GlobalValue::AppendingLinkage:
1212 return LLVMAppendingLinkage;
1213 case GlobalValue::InternalLinkage:
1214 return LLVMInternalLinkage;
1215 case GlobalValue::PrivateLinkage:
1216 return LLVMPrivateLinkage;
1217 case GlobalValue::ExternalWeakLinkage:
1218 return LLVMExternalWeakLinkage;
1219 case GlobalValue::CommonLinkage:
1220 return LLVMCommonLinkage;
1223 llvm_unreachable("Invalid GlobalValue linkage!");
1226 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1227 GlobalValue *GV = unwrap<GlobalValue>(Global);
1230 case LLVMExternalLinkage:
1231 GV->setLinkage(GlobalValue::ExternalLinkage);
1233 case LLVMAvailableExternallyLinkage:
1234 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1236 case LLVMLinkOnceAnyLinkage:
1237 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1239 case LLVMLinkOnceODRLinkage:
1240 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1242 case LLVMLinkOnceODRAutoHideLinkage:
1243 DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1244 "longer supported.");
1246 case LLVMWeakAnyLinkage:
1247 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1249 case LLVMWeakODRLinkage:
1250 GV->setLinkage(GlobalValue::WeakODRLinkage);
1252 case LLVMAppendingLinkage:
1253 GV->setLinkage(GlobalValue::AppendingLinkage);
1255 case LLVMInternalLinkage:
1256 GV->setLinkage(GlobalValue::InternalLinkage);
1258 case LLVMPrivateLinkage:
1259 GV->setLinkage(GlobalValue::PrivateLinkage);
1261 case LLVMLinkerPrivateLinkage:
1262 GV->setLinkage(GlobalValue::PrivateLinkage);
1264 case LLVMLinkerPrivateWeakLinkage:
1265 GV->setLinkage(GlobalValue::PrivateLinkage);
1267 case LLVMDLLImportLinkage:
1269 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1271 case LLVMDLLExportLinkage:
1273 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1275 case LLVMExternalWeakLinkage:
1276 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1278 case LLVMGhostLinkage:
1280 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1282 case LLVMCommonLinkage:
1283 GV->setLinkage(GlobalValue::CommonLinkage);
1288 const char *LLVMGetSection(LLVMValueRef Global) {
1289 return unwrap<GlobalValue>(Global)->getSection().c_str();
1292 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1293 unwrap<GlobalObject>(Global)->setSection(Section);
1296 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1297 return static_cast<LLVMVisibility>(
1298 unwrap<GlobalValue>(Global)->getVisibility());
1301 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1302 unwrap<GlobalValue>(Global)
1303 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1306 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
1307 return static_cast<LLVMDLLStorageClass>(
1308 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1311 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
1312 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1313 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1316 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
1317 return unwrap<GlobalValue>(Global)->hasUnnamedAddr();
1320 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
1321 unwrap<GlobalValue>(Global)->setUnnamedAddr(HasUnnamedAddr);
1324 /*--.. Operations on global variables, load and store instructions .........--*/
1326 unsigned LLVMGetAlignment(LLVMValueRef V) {
1327 Value *P = unwrap<Value>(V);
1328 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1329 return GV->getAlignment();
1330 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1331 return AI->getAlignment();
1332 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1333 return LI->getAlignment();
1334 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1335 return SI->getAlignment();
1338 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1341 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1342 Value *P = unwrap<Value>(V);
1343 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
1344 GV->setAlignment(Bytes);
1345 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1346 AI->setAlignment(Bytes);
1347 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1348 LI->setAlignment(Bytes);
1349 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1350 SI->setAlignment(Bytes);
1353 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1356 /*--.. Operations on global variables ......................................--*/
1358 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1359 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1360 GlobalValue::ExternalLinkage, nullptr, Name));
1363 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1365 unsigned AddressSpace) {
1366 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1367 GlobalValue::ExternalLinkage, nullptr, Name,
1368 nullptr, GlobalVariable::NotThreadLocal,
1372 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1373 return wrap(unwrap(M)->getNamedGlobal(Name));
1376 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1377 Module *Mod = unwrap(M);
1378 Module::global_iterator I = Mod->global_begin();
1379 if (I == Mod->global_end())
1384 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1385 Module *Mod = unwrap(M);
1386 Module::global_iterator I = Mod->global_end();
1387 if (I == Mod->global_begin())
1392 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1393 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1394 Module::global_iterator I = GV;
1395 if (++I == GV->getParent()->global_end())
1400 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1401 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1402 Module::global_iterator I = GV;
1403 if (I == GV->getParent()->global_begin())
1408 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1409 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1412 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1413 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1414 if ( !GV->hasInitializer() )
1416 return wrap(GV->getInitializer());
1419 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1420 unwrap<GlobalVariable>(GlobalVar)
1421 ->setInitializer(unwrap<Constant>(ConstantVal));
1424 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1425 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1428 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1429 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1432 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1433 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1436 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1437 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1440 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1441 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1442 case GlobalVariable::NotThreadLocal:
1443 return LLVMNotThreadLocal;
1444 case GlobalVariable::GeneralDynamicTLSModel:
1445 return LLVMGeneralDynamicTLSModel;
1446 case GlobalVariable::LocalDynamicTLSModel:
1447 return LLVMLocalDynamicTLSModel;
1448 case GlobalVariable::InitialExecTLSModel:
1449 return LLVMInitialExecTLSModel;
1450 case GlobalVariable::LocalExecTLSModel:
1451 return LLVMLocalExecTLSModel;
1454 llvm_unreachable("Invalid GlobalVariable thread local mode");
1457 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1458 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1461 case LLVMNotThreadLocal:
1462 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1464 case LLVMGeneralDynamicTLSModel:
1465 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1467 case LLVMLocalDynamicTLSModel:
1468 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1470 case LLVMInitialExecTLSModel:
1471 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1473 case LLVMLocalExecTLSModel:
1474 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1479 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1480 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1483 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1484 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1487 /*--.. Operations on aliases ......................................--*/
1489 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1491 auto *PTy = cast<PointerType>(unwrap(Ty));
1492 return wrap(new GlobalAlias(
1493 PTy->getElementType(), GlobalValue::ExternalLinkage, Name,
1494 unwrap<GlobalObject>(Aliasee), unwrap(M), PTy->getAddressSpace()));
1497 /*--.. Operations on functions .............................................--*/
1499 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1500 LLVMTypeRef FunctionTy) {
1501 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1502 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1505 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1506 return wrap(unwrap(M)->getFunction(Name));
1509 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1510 Module *Mod = unwrap(M);
1511 Module::iterator I = Mod->begin();
1512 if (I == Mod->end())
1517 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1518 Module *Mod = unwrap(M);
1519 Module::iterator I = Mod->end();
1520 if (I == Mod->begin())
1525 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1526 Function *Func = unwrap<Function>(Fn);
1527 Module::iterator I = Func;
1528 if (++I == Func->getParent()->end())
1533 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1534 Function *Func = unwrap<Function>(Fn);
1535 Module::iterator I = Func;
1536 if (I == Func->getParent()->begin())
1541 void LLVMDeleteFunction(LLVMValueRef Fn) {
1542 unwrap<Function>(Fn)->eraseFromParent();
1545 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1546 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1547 return F->getIntrinsicID();
1551 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1552 return unwrap<Function>(Fn)->getCallingConv();
1555 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1556 return unwrap<Function>(Fn)->setCallingConv(
1557 static_cast<CallingConv::ID>(CC));
1560 const char *LLVMGetGC(LLVMValueRef Fn) {
1561 Function *F = unwrap<Function>(Fn);
1562 return F->hasGC()? F->getGC() : nullptr;
1565 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1566 Function *F = unwrap<Function>(Fn);
1573 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1574 Function *Func = unwrap<Function>(Fn);
1575 const AttributeSet PAL = Func->getAttributes();
1577 const AttributeSet PALnew =
1578 PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1579 AttributeSet::get(Func->getContext(),
1580 AttributeSet::FunctionIndex, B));
1581 Func->setAttributes(PALnew);
1584 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1586 Function *Func = unwrap<Function>(Fn);
1587 AttributeSet::AttrIndex Idx =
1588 AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
1591 B.addAttribute(A, V);
1592 AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
1593 Func->addAttributes(Idx, Set);
1596 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1597 Function *Func = unwrap<Function>(Fn);
1598 const AttributeSet PAL = Func->getAttributes();
1600 const AttributeSet PALnew =
1601 PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1602 AttributeSet::get(Func->getContext(),
1603 AttributeSet::FunctionIndex, B));
1604 Func->setAttributes(PALnew);
1607 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1608 Function *Func = unwrap<Function>(Fn);
1609 const AttributeSet PAL = Func->getAttributes();
1610 return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
1613 /*--.. Operations on parameters ............................................--*/
1615 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1616 // This function is strictly redundant to
1617 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1618 return unwrap<Function>(FnRef)->arg_size();
1621 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1622 Function *Fn = unwrap<Function>(FnRef);
1623 for (Function::arg_iterator I = Fn->arg_begin(),
1624 E = Fn->arg_end(); I != E; I++)
1625 *ParamRefs++ = wrap(I);
1628 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1629 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1635 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1636 return wrap(unwrap<Argument>(V)->getParent());
1639 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1640 Function *Func = unwrap<Function>(Fn);
1641 Function::arg_iterator I = Func->arg_begin();
1642 if (I == Func->arg_end())
1647 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1648 Function *Func = unwrap<Function>(Fn);
1649 Function::arg_iterator I = Func->arg_end();
1650 if (I == Func->arg_begin())
1655 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1656 Argument *A = unwrap<Argument>(Arg);
1657 Function::arg_iterator I = A;
1658 if (++I == A->getParent()->arg_end())
1663 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1664 Argument *A = unwrap<Argument>(Arg);
1665 Function::arg_iterator I = A;
1666 if (I == A->getParent()->arg_begin())
1671 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1672 Argument *A = unwrap<Argument>(Arg);
1674 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1677 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1678 Argument *A = unwrap<Argument>(Arg);
1680 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1683 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1684 Argument *A = unwrap<Argument>(Arg);
1685 return (LLVMAttribute)A->getParent()->getAttributes().
1686 Raw(A->getArgNo()+1);
1690 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1691 Argument *A = unwrap<Argument>(Arg);
1693 B.addAlignmentAttr(align);
1694 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
1697 /*--.. Operations on basic blocks ..........................................--*/
1699 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1700 return wrap(static_cast<Value*>(unwrap(BB)));
1703 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1704 return isa<BasicBlock>(unwrap(Val));
1707 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1708 return wrap(unwrap<BasicBlock>(Val));
1711 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1712 return wrap(unwrap(BB)->getParent());
1715 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1716 return wrap(unwrap(BB)->getTerminator());
1719 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1720 return unwrap<Function>(FnRef)->size();
1723 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1724 Function *Fn = unwrap<Function>(FnRef);
1725 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1726 *BasicBlocksRefs++ = wrap(I);
1729 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1730 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1733 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1734 Function *Func = unwrap<Function>(Fn);
1735 Function::iterator I = Func->begin();
1736 if (I == Func->end())
1741 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1742 Function *Func = unwrap<Function>(Fn);
1743 Function::iterator I = Func->end();
1744 if (I == Func->begin())
1749 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1750 BasicBlock *Block = unwrap(BB);
1751 Function::iterator I = Block;
1752 if (++I == Block->getParent()->end())
1757 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1758 BasicBlock *Block = unwrap(BB);
1759 Function::iterator I = Block;
1760 if (I == Block->getParent()->begin())
1765 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1768 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1771 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1772 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1775 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1776 LLVMBasicBlockRef BBRef,
1778 BasicBlock *BB = unwrap(BBRef);
1779 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1782 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1784 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1787 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1788 unwrap(BBRef)->eraseFromParent();
1791 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1792 unwrap(BBRef)->removeFromParent();
1795 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1796 unwrap(BB)->moveBefore(unwrap(MovePos));
1799 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1800 unwrap(BB)->moveAfter(unwrap(MovePos));
1803 /*--.. Operations on instructions ..........................................--*/
1805 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1806 return wrap(unwrap<Instruction>(Inst)->getParent());
1809 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1810 BasicBlock *Block = unwrap(BB);
1811 BasicBlock::iterator I = Block->begin();
1812 if (I == Block->end())
1817 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1818 BasicBlock *Block = unwrap(BB);
1819 BasicBlock::iterator I = Block->end();
1820 if (I == Block->begin())
1825 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1826 Instruction *Instr = unwrap<Instruction>(Inst);
1827 BasicBlock::iterator I = Instr;
1828 if (++I == Instr->getParent()->end())
1833 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1834 Instruction *Instr = unwrap<Instruction>(Inst);
1835 BasicBlock::iterator I = Instr;
1836 if (I == Instr->getParent()->begin())
1841 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1842 unwrap<Instruction>(Inst)->eraseFromParent();
1845 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
1846 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
1847 return (LLVMIntPredicate)I->getPredicate();
1848 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1849 if (CE->getOpcode() == Instruction::ICmp)
1850 return (LLVMIntPredicate)CE->getPredicate();
1851 return (LLVMIntPredicate)0;
1854 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
1855 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
1856 return map_to_llvmopcode(C->getOpcode());
1857 return (LLVMOpcode)0;
1860 /*--.. Call and invoke instructions ........................................--*/
1862 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1863 Value *V = unwrap(Instr);
1864 if (CallInst *CI = dyn_cast<CallInst>(V))
1865 return CI->getCallingConv();
1866 if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1867 return II->getCallingConv();
1868 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1871 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1872 Value *V = unwrap(Instr);
1873 if (CallInst *CI = dyn_cast<CallInst>(V))
1874 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1875 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1876 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1877 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1880 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1882 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1885 Call.getAttributes().addAttributes(Call->getContext(), index,
1886 AttributeSet::get(Call->getContext(),
1890 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1892 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1894 Call.setAttributes(Call.getAttributes()
1895 .removeAttributes(Call->getContext(), index,
1896 AttributeSet::get(Call->getContext(),
1900 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1902 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1904 B.addAlignmentAttr(align);
1905 Call.setAttributes(Call.getAttributes()
1906 .addAttributes(Call->getContext(), index,
1907 AttributeSet::get(Call->getContext(),
1911 /*--.. Operations on call instructions (only) ..............................--*/
1913 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1914 return unwrap<CallInst>(Call)->isTailCall();
1917 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1918 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1921 /*--.. Operations on switch instructions (only) ............................--*/
1923 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
1924 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
1927 /*--.. Operations on phi nodes .............................................--*/
1929 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1930 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1931 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1932 for (unsigned I = 0; I != Count; ++I)
1933 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1936 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1937 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1940 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1941 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1944 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1945 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1949 /*===-- Instruction builders ----------------------------------------------===*/
1951 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1952 return wrap(new IRBuilder<>(*unwrap(C)));
1955 LLVMBuilderRef LLVMCreateBuilder(void) {
1956 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1959 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1960 LLVMValueRef Instr) {
1961 BasicBlock *BB = unwrap(Block);
1962 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1963 unwrap(Builder)->SetInsertPoint(BB, I);
1966 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1967 Instruction *I = unwrap<Instruction>(Instr);
1968 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1971 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1972 BasicBlock *BB = unwrap(Block);
1973 unwrap(Builder)->SetInsertPoint(BB);
1976 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1977 return wrap(unwrap(Builder)->GetInsertBlock());
1980 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1981 unwrap(Builder)->ClearInsertionPoint();
1984 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1985 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1988 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1990 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1993 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1994 delete unwrap(Builder);
1997 /*--.. Metadata builders ...................................................--*/
1999 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
2000 MDNode *Loc = L ? unwrap<MDNode>(L) : nullptr;
2001 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
2004 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
2005 return wrap(unwrap(Builder)->getCurrentDebugLocation()
2006 .getAsMDNode(unwrap(Builder)->getContext()));
2009 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
2010 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
2014 /*--.. Instruction builders ................................................--*/
2016 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
2017 return wrap(unwrap(B)->CreateRetVoid());
2020 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
2021 return wrap(unwrap(B)->CreateRet(unwrap(V)));
2024 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
2026 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
2029 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
2030 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
2033 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
2034 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
2035 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
2038 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
2039 LLVMBasicBlockRef Else, unsigned NumCases) {
2040 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
2043 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
2044 unsigned NumDests) {
2045 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
2048 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
2049 LLVMValueRef *Args, unsigned NumArgs,
2050 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
2052 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
2053 makeArrayRef(unwrap(Args), NumArgs),
2057 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
2058 LLVMValueRef PersFn, unsigned NumClauses,
2060 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
2061 cast<Function>(unwrap(PersFn)),
2065 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
2066 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
2069 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
2070 return wrap(unwrap(B)->CreateUnreachable());
2073 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
2074 LLVMBasicBlockRef Dest) {
2075 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
2078 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
2079 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
2082 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
2083 unwrap<LandingPadInst>(LandingPad)->
2084 addClause(cast<Constant>(unwrap(ClauseVal)));
2087 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
2088 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2091 /*--.. Arithmetic ..........................................................--*/
2093 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2095 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2098 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2100 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2103 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2105 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2108 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2110 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2113 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2115 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2118 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2120 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2123 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2125 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2128 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2130 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2133 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2135 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2138 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2140 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2143 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2145 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2148 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2150 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2153 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2155 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2158 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2160 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2163 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2164 LLVMValueRef RHS, const char *Name) {
2165 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2168 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2170 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2173 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2175 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2178 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2180 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2183 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2185 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2188 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2190 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2193 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2195 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2198 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2200 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2203 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2205 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2208 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2210 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2213 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2215 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2218 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2219 LLVMValueRef LHS, LLVMValueRef RHS,
2221 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2222 unwrap(RHS), Name));
2225 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2226 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2229 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2231 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2234 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2236 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2239 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2240 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2243 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2244 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2247 /*--.. Memory ..............................................................--*/
2249 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2251 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2252 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2253 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2254 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2255 ITy, unwrap(Ty), AllocSize,
2256 nullptr, nullptr, "");
2257 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2260 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2261 LLVMValueRef Val, const char *Name) {
2262 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2263 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2264 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2265 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2266 ITy, unwrap(Ty), AllocSize,
2267 unwrap(Val), nullptr, "");
2268 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2271 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2273 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
2276 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2277 LLVMValueRef Val, const char *Name) {
2278 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2281 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2282 return wrap(unwrap(B)->Insert(
2283 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2287 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2289 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2292 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2293 LLVMValueRef PointerVal) {
2294 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2297 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
2299 case LLVMAtomicOrderingNotAtomic: return NotAtomic;
2300 case LLVMAtomicOrderingUnordered: return Unordered;
2301 case LLVMAtomicOrderingMonotonic: return Monotonic;
2302 case LLVMAtomicOrderingAcquire: return Acquire;
2303 case LLVMAtomicOrderingRelease: return Release;
2304 case LLVMAtomicOrderingAcquireRelease: return AcquireRelease;
2305 case LLVMAtomicOrderingSequentiallyConsistent:
2306 return SequentiallyConsistent;
2309 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
2312 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
2313 LLVMBool isSingleThread, const char *Name) {
2315 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
2316 isSingleThread ? SingleThread : CrossThread,
2320 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2321 LLVMValueRef *Indices, unsigned NumIndices,
2323 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2324 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
2327 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2328 LLVMValueRef *Indices, unsigned NumIndices,
2330 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2331 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
2334 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2335 unsigned Idx, const char *Name) {
2336 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2339 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2341 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2344 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2346 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2349 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2350 Value *P = unwrap<Value>(MemAccessInst);
2351 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2352 return LI->isVolatile();
2353 return cast<StoreInst>(P)->isVolatile();
2356 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2357 Value *P = unwrap<Value>(MemAccessInst);
2358 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2359 return LI->setVolatile(isVolatile);
2360 return cast<StoreInst>(P)->setVolatile(isVolatile);
2363 /*--.. Casts ...............................................................--*/
2365 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2366 LLVMTypeRef DestTy, const char *Name) {
2367 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2370 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2371 LLVMTypeRef DestTy, const char *Name) {
2372 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2375 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2376 LLVMTypeRef DestTy, const char *Name) {
2377 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2380 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2381 LLVMTypeRef DestTy, const char *Name) {
2382 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2385 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2386 LLVMTypeRef DestTy, const char *Name) {
2387 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2390 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2391 LLVMTypeRef DestTy, const char *Name) {
2392 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2395 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2396 LLVMTypeRef DestTy, const char *Name) {
2397 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2400 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2401 LLVMTypeRef DestTy, const char *Name) {
2402 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2405 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2406 LLVMTypeRef DestTy, const char *Name) {
2407 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2410 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2411 LLVMTypeRef DestTy, const char *Name) {
2412 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2415 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2416 LLVMTypeRef DestTy, const char *Name) {
2417 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2420 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2421 LLVMTypeRef DestTy, const char *Name) {
2422 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2425 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
2426 LLVMTypeRef DestTy, const char *Name) {
2427 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
2430 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2431 LLVMTypeRef DestTy, const char *Name) {
2432 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2436 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2437 LLVMTypeRef DestTy, const char *Name) {
2438 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2442 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2443 LLVMTypeRef DestTy, const char *Name) {
2444 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2448 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2449 LLVMTypeRef DestTy, const char *Name) {
2450 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2451 unwrap(DestTy), Name));
2454 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2455 LLVMTypeRef DestTy, const char *Name) {
2456 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2459 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2460 LLVMTypeRef DestTy, const char *Name) {
2461 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2462 /*isSigned*/true, Name));
2465 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2466 LLVMTypeRef DestTy, const char *Name) {
2467 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2470 /*--.. Comparisons .........................................................--*/
2472 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2473 LLVMValueRef LHS, LLVMValueRef RHS,
2475 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2476 unwrap(LHS), unwrap(RHS), Name));
2479 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2480 LLVMValueRef LHS, LLVMValueRef RHS,
2482 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2483 unwrap(LHS), unwrap(RHS), Name));
2486 /*--.. Miscellaneous instructions ..........................................--*/
2488 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2489 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2492 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2493 LLVMValueRef *Args, unsigned NumArgs,
2495 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2496 makeArrayRef(unwrap(Args), NumArgs),
2500 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2501 LLVMValueRef Then, LLVMValueRef Else,
2503 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2507 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2508 LLVMTypeRef Ty, const char *Name) {
2509 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2512 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2513 LLVMValueRef Index, const char *Name) {
2514 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2518 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2519 LLVMValueRef EltVal, LLVMValueRef Index,
2521 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2522 unwrap(Index), Name));
2525 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2526 LLVMValueRef V2, LLVMValueRef Mask,
2528 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2529 unwrap(Mask), Name));
2532 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2533 unsigned Index, const char *Name) {
2534 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2537 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2538 LLVMValueRef EltVal, unsigned Index,
2540 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2544 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2546 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2549 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2551 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2554 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2555 LLVMValueRef RHS, const char *Name) {
2556 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2559 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
2560 LLVMValueRef PTR, LLVMValueRef Val,
2561 LLVMAtomicOrdering ordering,
2562 LLVMBool singleThread) {
2563 AtomicRMWInst::BinOp intop;
2565 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
2566 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
2567 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
2568 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
2569 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
2570 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
2571 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
2572 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
2573 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
2574 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
2575 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
2577 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
2578 mapFromLLVMOrdering(ordering), singleThread ? SingleThread : CrossThread));
2582 /*===-- Module providers --------------------------------------------------===*/
2584 LLVMModuleProviderRef
2585 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2586 return reinterpret_cast<LLVMModuleProviderRef>(M);
2589 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2594 /*===-- Memory buffers ----------------------------------------------------===*/
2596 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2598 LLVMMemoryBufferRef *OutMemBuf,
2599 char **OutMessage) {
2601 std::unique_ptr<MemoryBuffer> MB;
2603 if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2604 *OutMemBuf = wrap(MB.release());
2608 *OutMessage = strdup(ec.message().c_str());
2612 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2613 char **OutMessage) {
2614 std::unique_ptr<MemoryBuffer> MB;
2616 if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2617 *OutMemBuf = wrap(MB.release());
2621 *OutMessage = strdup(ec.message().c_str());
2625 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
2626 const char *InputData,
2627 size_t InputDataLength,
2628 const char *BufferName,
2629 LLVMBool RequiresNullTerminator) {
2631 return wrap(MemoryBuffer::getMemBuffer(
2632 StringRef(InputData, InputDataLength),
2633 StringRef(BufferName),
2634 RequiresNullTerminator));
2637 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
2638 const char *InputData,
2639 size_t InputDataLength,
2640 const char *BufferName) {
2642 return wrap(MemoryBuffer::getMemBufferCopy(
2643 StringRef(InputData, InputDataLength),
2644 StringRef(BufferName)));
2647 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
2648 return unwrap(MemBuf)->getBufferStart();
2651 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
2652 return unwrap(MemBuf)->getBufferSize();
2655 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2656 delete unwrap(MemBuf);
2659 /*===-- Pass Registry -----------------------------------------------------===*/
2661 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2662 return wrap(PassRegistry::getPassRegistry());
2665 /*===-- Pass Manager ------------------------------------------------------===*/
2667 LLVMPassManagerRef LLVMCreatePassManager() {
2668 return wrap(new PassManager());
2671 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2672 return wrap(new FunctionPassManager(unwrap(M)));
2675 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2676 return LLVMCreateFunctionPassManagerForModule(
2677 reinterpret_cast<LLVMModuleRef>(P));
2680 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2681 return unwrap<PassManager>(PM)->run(*unwrap(M));
2684 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2685 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2688 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2689 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2692 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2693 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2696 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
2700 /*===-- Threading ------------------------------------------------------===*/
2702 LLVMBool LLVMStartMultithreaded() {
2703 return llvm_start_multithreaded();
2706 void LLVMStopMultithreaded() {
2707 llvm_stop_multithreaded();
2710 LLVMBool LLVMIsMultithreaded() {
2711 return llvm_is_multithreaded();