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/DerivedTypes.h"
21 #include "llvm/IR/DiagnosticInfo.h"
22 #include "llvm/IR/DiagnosticPrinter.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"
41 #include <system_error>
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, EC, sys::fs::F_Text);
189 *ErrorMessage = strdup(EC.message().c_str());
193 unwrap(M)->print(dest, nullptr);
197 if (dest.has_error()) {
198 *ErrorMessage = strdup("Error printing to file");
205 char *LLVMPrintModuleToString(LLVMModuleRef M) {
207 raw_string_ostream os(buf);
209 unwrap(M)->print(os, nullptr);
212 return strdup(buf.c_str());
215 /*--.. Operations on inline assembler ......................................--*/
216 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
217 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
221 /*--.. Operations on module contexts ......................................--*/
222 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
223 return wrap(&unwrap(M)->getContext());
227 /*===-- Operations on types -----------------------------------------------===*/
229 /*--.. Operations on all types (mostly) ....................................--*/
231 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
232 switch (unwrap(Ty)->getTypeID()) {
234 return LLVMVoidTypeKind;
236 return LLVMHalfTypeKind;
237 case Type::FloatTyID:
238 return LLVMFloatTypeKind;
239 case Type::DoubleTyID:
240 return LLVMDoubleTypeKind;
241 case Type::X86_FP80TyID:
242 return LLVMX86_FP80TypeKind;
243 case Type::FP128TyID:
244 return LLVMFP128TypeKind;
245 case Type::PPC_FP128TyID:
246 return LLVMPPC_FP128TypeKind;
247 case Type::LabelTyID:
248 return LLVMLabelTypeKind;
249 case Type::MetadataTyID:
250 return LLVMMetadataTypeKind;
251 case Type::IntegerTyID:
252 return LLVMIntegerTypeKind;
253 case Type::FunctionTyID:
254 return LLVMFunctionTypeKind;
255 case Type::StructTyID:
256 return LLVMStructTypeKind;
257 case Type::ArrayTyID:
258 return LLVMArrayTypeKind;
259 case Type::PointerTyID:
260 return LLVMPointerTypeKind;
261 case Type::VectorTyID:
262 return LLVMVectorTypeKind;
263 case Type::X86_MMXTyID:
264 return LLVMX86_MMXTypeKind;
266 llvm_unreachable("Unhandled TypeID.");
269 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
271 return unwrap(Ty)->isSized();
274 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
275 return wrap(&unwrap(Ty)->getContext());
278 void LLVMDumpType(LLVMTypeRef Ty) {
279 return unwrap(Ty)->dump();
282 char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
284 raw_string_ostream os(buf);
287 unwrap(Ty)->print(os);
289 os << "Printing <null> Type";
293 return strdup(buf.c_str());
296 /*--.. Operations on integer types .........................................--*/
298 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
299 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
301 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
302 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
304 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
305 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
307 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
308 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
310 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
311 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
313 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
314 return wrap(IntegerType::get(*unwrap(C), NumBits));
317 LLVMTypeRef LLVMInt1Type(void) {
318 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
320 LLVMTypeRef LLVMInt8Type(void) {
321 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
323 LLVMTypeRef LLVMInt16Type(void) {
324 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
326 LLVMTypeRef LLVMInt32Type(void) {
327 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
329 LLVMTypeRef LLVMInt64Type(void) {
330 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
332 LLVMTypeRef LLVMIntType(unsigned NumBits) {
333 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
336 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
337 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
340 /*--.. Operations on real types ............................................--*/
342 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
343 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
345 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
346 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
348 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
349 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
351 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
352 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
354 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
355 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
357 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
358 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
360 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
361 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
364 LLVMTypeRef LLVMHalfType(void) {
365 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
367 LLVMTypeRef LLVMFloatType(void) {
368 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
370 LLVMTypeRef LLVMDoubleType(void) {
371 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
373 LLVMTypeRef LLVMX86FP80Type(void) {
374 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
376 LLVMTypeRef LLVMFP128Type(void) {
377 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
379 LLVMTypeRef LLVMPPCFP128Type(void) {
380 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
382 LLVMTypeRef LLVMX86MMXType(void) {
383 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
386 /*--.. Operations on function types ........................................--*/
388 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
389 LLVMTypeRef *ParamTypes, unsigned ParamCount,
391 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
392 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
395 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
396 return unwrap<FunctionType>(FunctionTy)->isVarArg();
399 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
400 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
403 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
404 return unwrap<FunctionType>(FunctionTy)->getNumParams();
407 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
408 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
409 for (FunctionType::param_iterator I = Ty->param_begin(),
410 E = Ty->param_end(); I != E; ++I)
414 /*--.. Operations on struct types ..........................................--*/
416 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
417 unsigned ElementCount, LLVMBool Packed) {
418 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
419 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
422 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
423 unsigned ElementCount, LLVMBool Packed) {
424 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
425 ElementCount, Packed);
428 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
430 return wrap(StructType::create(*unwrap(C), Name));
433 const char *LLVMGetStructName(LLVMTypeRef Ty)
435 StructType *Type = unwrap<StructType>(Ty);
436 if (!Type->hasName())
438 return Type->getName().data();
441 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
442 unsigned ElementCount, LLVMBool Packed) {
443 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
444 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
447 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
448 return unwrap<StructType>(StructTy)->getNumElements();
451 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
452 StructType *Ty = unwrap<StructType>(StructTy);
453 for (StructType::element_iterator I = Ty->element_begin(),
454 E = Ty->element_end(); I != E; ++I)
458 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
459 return unwrap<StructType>(StructTy)->isPacked();
462 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
463 return unwrap<StructType>(StructTy)->isOpaque();
466 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
467 return wrap(unwrap(M)->getTypeByName(Name));
470 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
472 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
473 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
476 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
477 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
480 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
481 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
484 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
485 return wrap(unwrap<SequentialType>(Ty)->getElementType());
488 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
489 return unwrap<ArrayType>(ArrayTy)->getNumElements();
492 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
493 return unwrap<PointerType>(PointerTy)->getAddressSpace();
496 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
497 return unwrap<VectorType>(VectorTy)->getNumElements();
500 /*--.. Operations on other types ...........................................--*/
502 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
503 return wrap(Type::getVoidTy(*unwrap(C)));
505 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
506 return wrap(Type::getLabelTy(*unwrap(C)));
509 LLVMTypeRef LLVMVoidType(void) {
510 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
512 LLVMTypeRef LLVMLabelType(void) {
513 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
516 /*===-- Operations on values ----------------------------------------------===*/
518 /*--.. Operations on all values ............................................--*/
520 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
521 return wrap(unwrap(Val)->getType());
524 const char *LLVMGetValueName(LLVMValueRef Val) {
525 return unwrap(Val)->getName().data();
528 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
529 unwrap(Val)->setName(Name);
532 void LLVMDumpValue(LLVMValueRef Val) {
536 char* LLVMPrintValueToString(LLVMValueRef Val) {
538 raw_string_ostream os(buf);
541 unwrap(Val)->print(os);
543 os << "Printing <null> Value";
547 return strdup(buf.c_str());
550 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
551 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
554 int LLVMHasMetadata(LLVMValueRef Inst) {
555 return unwrap<Instruction>(Inst)->hasMetadata();
558 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
559 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
562 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
563 unwrap<Instruction>(Inst)->setMetadata(KindID,
564 MD ? unwrap<MDNode>(MD) : nullptr);
567 /*--.. Conversion functions ................................................--*/
569 #define LLVM_DEFINE_VALUE_CAST(name) \
570 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
571 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
574 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
576 /*--.. Operations on Uses ..................................................--*/
577 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
578 Value *V = unwrap(Val);
579 Value::use_iterator I = V->use_begin();
580 if (I == V->use_end())
585 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
586 Use *Next = unwrap(U)->getNext();
592 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
593 return wrap(unwrap(U)->getUser());
596 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
597 return wrap(unwrap(U)->get());
600 /*--.. Operations on Users .................................................--*/
601 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
602 Value *V = unwrap(Val);
603 if (MDNode *MD = dyn_cast<MDNode>(V))
604 return wrap(MD->getOperand(Index));
605 return wrap(cast<User>(V)->getOperand(Index));
608 LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) {
609 Value *V = unwrap(Val);
610 return wrap(&cast<User>(V)->getOperandUse(Index));
613 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
614 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
617 int LLVMGetNumOperands(LLVMValueRef Val) {
618 Value *V = unwrap(Val);
619 if (MDNode *MD = dyn_cast<MDNode>(V))
620 return MD->getNumOperands();
621 return cast<User>(V)->getNumOperands();
624 /*--.. Operations on constants of any type .................................--*/
626 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
627 return wrap(Constant::getNullValue(unwrap(Ty)));
630 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
631 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
634 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
635 return wrap(UndefValue::get(unwrap(Ty)));
638 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
639 return isa<Constant>(unwrap(Ty));
642 LLVMBool LLVMIsNull(LLVMValueRef Val) {
643 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
644 return C->isNullValue();
648 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
649 return isa<UndefValue>(unwrap(Val));
652 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
654 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
657 /*--.. Operations on metadata nodes ........................................--*/
659 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
661 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
664 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
665 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
668 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
670 return wrap(MDNode::get(*unwrap(C),
671 makeArrayRef(unwrap<Value>(Vals, Count), Count)));
674 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
675 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
678 const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
679 if (const MDString *S = dyn_cast<MDString>(unwrap(V))) {
680 *Len = S->getString().size();
681 return S->getString().data();
687 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V)
689 return cast<MDNode>(unwrap(V))->getNumOperands();
692 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest)
694 const MDNode *N = cast<MDNode>(unwrap(V));
695 const unsigned numOperands = N->getNumOperands();
696 for (unsigned i = 0; i < numOperands; i++)
697 Dest[i] = wrap(N->getOperand(i));
700 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
702 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
703 return N->getNumOperands();
708 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
710 NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
713 for (unsigned i=0;i<N->getNumOperands();i++)
714 Dest[i] = wrap(N->getOperand(i));
717 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
720 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
723 MDNode *Op = Val ? unwrap<MDNode>(Val) : nullptr;
728 /*--.. Operations on scalar constants ......................................--*/
730 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
731 LLVMBool SignExtend) {
732 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
735 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
737 const uint64_t Words[]) {
738 IntegerType *Ty = unwrap<IntegerType>(IntTy);
739 return wrap(ConstantInt::get(Ty->getContext(),
740 APInt(Ty->getBitWidth(),
741 makeArrayRef(Words, NumWords))));
744 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
746 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
750 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
751 unsigned SLen, uint8_t Radix) {
752 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
756 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
757 return wrap(ConstantFP::get(unwrap(RealTy), N));
760 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
761 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
764 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
766 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
769 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
770 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
773 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
774 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
777 /*--.. Operations on composite constants ...................................--*/
779 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
781 LLVMBool DontNullTerminate) {
782 /* Inverted the sense of AddNull because ', 0)' is a
783 better mnemonic for null termination than ', 1)'. */
784 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
785 DontNullTerminate == 0));
787 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
788 LLVMValueRef *ConstantVals,
789 unsigned Count, LLVMBool Packed) {
790 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
791 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
795 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
796 LLVMBool DontNullTerminate) {
797 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
801 LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef c, unsigned idx) {
802 return wrap(static_cast<ConstantDataSequential*>(unwrap(c))->getElementAsConstant(idx));
805 LLVMBool LLVMIsConstantString(LLVMValueRef c) {
806 return static_cast<ConstantDataSequential*>(unwrap(c))->isString();
809 const char *LLVMGetAsString(LLVMValueRef c, size_t* Length) {
810 StringRef str = static_cast<ConstantDataSequential*>(unwrap(c))->getAsString();
811 *Length = str.size();
815 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
816 LLVMValueRef *ConstantVals, unsigned Length) {
817 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
818 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
821 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
823 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
827 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
828 LLVMValueRef *ConstantVals,
830 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
831 StructType *Ty = cast<StructType>(unwrap(StructTy));
833 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
836 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
837 return wrap(ConstantVector::get(makeArrayRef(
838 unwrap<Constant>(ScalarConstantVals, Size), Size)));
841 /*-- Opcode mapping */
843 static LLVMOpcode map_to_llvmopcode(int opcode)
846 default: llvm_unreachable("Unhandled Opcode.");
847 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
848 #include "llvm/IR/Instruction.def"
853 static int map_from_llvmopcode(LLVMOpcode code)
856 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
857 #include "llvm/IR/Instruction.def"
860 llvm_unreachable("Unhandled Opcode.");
863 /*--.. Constant expressions ................................................--*/
865 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
866 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
869 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
870 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
873 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
874 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
877 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
878 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
881 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
882 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
885 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
886 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
890 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
891 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
894 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
895 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
898 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
899 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
900 unwrap<Constant>(RHSConstant)));
903 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
904 LLVMValueRef RHSConstant) {
905 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
906 unwrap<Constant>(RHSConstant)));
909 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
910 LLVMValueRef RHSConstant) {
911 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
912 unwrap<Constant>(RHSConstant)));
915 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
916 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
917 unwrap<Constant>(RHSConstant)));
920 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
921 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
922 unwrap<Constant>(RHSConstant)));
925 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
926 LLVMValueRef RHSConstant) {
927 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
928 unwrap<Constant>(RHSConstant)));
931 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
932 LLVMValueRef RHSConstant) {
933 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
934 unwrap<Constant>(RHSConstant)));
937 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
938 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
939 unwrap<Constant>(RHSConstant)));
942 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
943 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
944 unwrap<Constant>(RHSConstant)));
947 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
948 LLVMValueRef RHSConstant) {
949 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
950 unwrap<Constant>(RHSConstant)));
953 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
954 LLVMValueRef RHSConstant) {
955 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
956 unwrap<Constant>(RHSConstant)));
959 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
960 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
961 unwrap<Constant>(RHSConstant)));
964 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
965 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
966 unwrap<Constant>(RHSConstant)));
969 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
970 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
971 unwrap<Constant>(RHSConstant)));
974 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
975 LLVMValueRef RHSConstant) {
976 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
977 unwrap<Constant>(RHSConstant)));
980 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
981 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
982 unwrap<Constant>(RHSConstant)));
985 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
986 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
987 unwrap<Constant>(RHSConstant)));
990 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
991 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
992 unwrap<Constant>(RHSConstant)));
995 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
996 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
997 unwrap<Constant>(RHSConstant)));
1000 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1001 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
1002 unwrap<Constant>(RHSConstant)));
1005 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1006 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
1007 unwrap<Constant>(RHSConstant)));
1010 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1011 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
1012 unwrap<Constant>(RHSConstant)));
1015 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
1016 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1017 return wrap(ConstantExpr::getICmp(Predicate,
1018 unwrap<Constant>(LHSConstant),
1019 unwrap<Constant>(RHSConstant)));
1022 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
1023 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1024 return wrap(ConstantExpr::getFCmp(Predicate,
1025 unwrap<Constant>(LHSConstant),
1026 unwrap<Constant>(RHSConstant)));
1029 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1030 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1031 unwrap<Constant>(RHSConstant)));
1034 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1035 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
1036 unwrap<Constant>(RHSConstant)));
1039 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1040 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1041 unwrap<Constant>(RHSConstant)));
1044 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
1045 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1046 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1048 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
1052 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
1053 LLVMValueRef *ConstantIndices,
1054 unsigned NumIndices) {
1055 Constant* Val = unwrap<Constant>(ConstantVal);
1056 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1058 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
1061 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1062 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1066 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1067 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1071 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1072 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1076 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1077 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1081 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1082 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1086 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1087 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1091 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1092 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1096 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1097 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1101 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1102 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1106 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1107 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1111 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1112 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1116 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1117 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1121 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1122 LLVMTypeRef ToType) {
1123 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1127 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1128 LLVMTypeRef ToType) {
1129 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1133 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1134 LLVMTypeRef ToType) {
1135 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1139 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1140 LLVMTypeRef ToType) {
1141 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1145 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1146 LLVMTypeRef ToType) {
1147 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1151 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1152 LLVMBool isSigned) {
1153 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1154 unwrap(ToType), isSigned));
1157 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1158 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1162 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1163 LLVMValueRef ConstantIfTrue,
1164 LLVMValueRef ConstantIfFalse) {
1165 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1166 unwrap<Constant>(ConstantIfTrue),
1167 unwrap<Constant>(ConstantIfFalse)));
1170 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1171 LLVMValueRef IndexConstant) {
1172 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1173 unwrap<Constant>(IndexConstant)));
1176 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1177 LLVMValueRef ElementValueConstant,
1178 LLVMValueRef IndexConstant) {
1179 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1180 unwrap<Constant>(ElementValueConstant),
1181 unwrap<Constant>(IndexConstant)));
1184 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1185 LLVMValueRef VectorBConstant,
1186 LLVMValueRef MaskConstant) {
1187 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1188 unwrap<Constant>(VectorBConstant),
1189 unwrap<Constant>(MaskConstant)));
1192 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1194 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1195 makeArrayRef(IdxList, NumIdx)));
1198 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1199 LLVMValueRef ElementValueConstant,
1200 unsigned *IdxList, unsigned NumIdx) {
1201 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1202 unwrap<Constant>(ElementValueConstant),
1203 makeArrayRef(IdxList, NumIdx)));
1206 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1207 const char *Constraints,
1208 LLVMBool HasSideEffects,
1209 LLVMBool IsAlignStack) {
1210 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1211 Constraints, HasSideEffects, IsAlignStack));
1214 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1215 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1218 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1220 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1221 return wrap(unwrap<GlobalValue>(Global)->getParent());
1224 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1225 return unwrap<GlobalValue>(Global)->isDeclaration();
1228 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1229 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1230 case GlobalValue::ExternalLinkage:
1231 return LLVMExternalLinkage;
1232 case GlobalValue::AvailableExternallyLinkage:
1233 return LLVMAvailableExternallyLinkage;
1234 case GlobalValue::LinkOnceAnyLinkage:
1235 return LLVMLinkOnceAnyLinkage;
1236 case GlobalValue::LinkOnceODRLinkage:
1237 return LLVMLinkOnceODRLinkage;
1238 case GlobalValue::WeakAnyLinkage:
1239 return LLVMWeakAnyLinkage;
1240 case GlobalValue::WeakODRLinkage:
1241 return LLVMWeakODRLinkage;
1242 case GlobalValue::AppendingLinkage:
1243 return LLVMAppendingLinkage;
1244 case GlobalValue::InternalLinkage:
1245 return LLVMInternalLinkage;
1246 case GlobalValue::PrivateLinkage:
1247 return LLVMPrivateLinkage;
1248 case GlobalValue::ExternalWeakLinkage:
1249 return LLVMExternalWeakLinkage;
1250 case GlobalValue::CommonLinkage:
1251 return LLVMCommonLinkage;
1254 llvm_unreachable("Invalid GlobalValue linkage!");
1257 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1258 GlobalValue *GV = unwrap<GlobalValue>(Global);
1261 case LLVMExternalLinkage:
1262 GV->setLinkage(GlobalValue::ExternalLinkage);
1264 case LLVMAvailableExternallyLinkage:
1265 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1267 case LLVMLinkOnceAnyLinkage:
1268 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1270 case LLVMLinkOnceODRLinkage:
1271 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1273 case LLVMLinkOnceODRAutoHideLinkage:
1274 DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1275 "longer supported.");
1277 case LLVMWeakAnyLinkage:
1278 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1280 case LLVMWeakODRLinkage:
1281 GV->setLinkage(GlobalValue::WeakODRLinkage);
1283 case LLVMAppendingLinkage:
1284 GV->setLinkage(GlobalValue::AppendingLinkage);
1286 case LLVMInternalLinkage:
1287 GV->setLinkage(GlobalValue::InternalLinkage);
1289 case LLVMPrivateLinkage:
1290 GV->setLinkage(GlobalValue::PrivateLinkage);
1292 case LLVMLinkerPrivateLinkage:
1293 GV->setLinkage(GlobalValue::PrivateLinkage);
1295 case LLVMLinkerPrivateWeakLinkage:
1296 GV->setLinkage(GlobalValue::PrivateLinkage);
1298 case LLVMDLLImportLinkage:
1300 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1302 case LLVMDLLExportLinkage:
1304 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1306 case LLVMExternalWeakLinkage:
1307 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1309 case LLVMGhostLinkage:
1311 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1313 case LLVMCommonLinkage:
1314 GV->setLinkage(GlobalValue::CommonLinkage);
1319 const char *LLVMGetSection(LLVMValueRef Global) {
1320 return unwrap<GlobalValue>(Global)->getSection();
1323 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1324 unwrap<GlobalObject>(Global)->setSection(Section);
1327 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1328 return static_cast<LLVMVisibility>(
1329 unwrap<GlobalValue>(Global)->getVisibility());
1332 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1333 unwrap<GlobalValue>(Global)
1334 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1337 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
1338 return static_cast<LLVMDLLStorageClass>(
1339 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1342 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
1343 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1344 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1347 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
1348 return unwrap<GlobalValue>(Global)->hasUnnamedAddr();
1351 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
1352 unwrap<GlobalValue>(Global)->setUnnamedAddr(HasUnnamedAddr);
1355 /*--.. Operations on global variables, load and store instructions .........--*/
1357 unsigned LLVMGetAlignment(LLVMValueRef V) {
1358 Value *P = unwrap<Value>(V);
1359 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1360 return GV->getAlignment();
1361 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1362 return AI->getAlignment();
1363 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1364 return LI->getAlignment();
1365 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1366 return SI->getAlignment();
1369 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1372 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1373 Value *P = unwrap<Value>(V);
1374 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
1375 GV->setAlignment(Bytes);
1376 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1377 AI->setAlignment(Bytes);
1378 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1379 LI->setAlignment(Bytes);
1380 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1381 SI->setAlignment(Bytes);
1384 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1387 /*--.. Operations on global variables ......................................--*/
1389 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1390 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1391 GlobalValue::ExternalLinkage, nullptr, Name));
1394 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1396 unsigned AddressSpace) {
1397 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1398 GlobalValue::ExternalLinkage, nullptr, Name,
1399 nullptr, GlobalVariable::NotThreadLocal,
1403 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1404 return wrap(unwrap(M)->getNamedGlobal(Name));
1407 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1408 Module *Mod = unwrap(M);
1409 Module::global_iterator I = Mod->global_begin();
1410 if (I == Mod->global_end())
1415 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1416 Module *Mod = unwrap(M);
1417 Module::global_iterator I = Mod->global_end();
1418 if (I == Mod->global_begin())
1423 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1424 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1425 Module::global_iterator I = GV;
1426 if (++I == GV->getParent()->global_end())
1431 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1432 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1433 Module::global_iterator I = GV;
1434 if (I == GV->getParent()->global_begin())
1439 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1440 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1443 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1444 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1445 if ( !GV->hasInitializer() )
1447 return wrap(GV->getInitializer());
1450 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1451 unwrap<GlobalVariable>(GlobalVar)
1452 ->setInitializer(unwrap<Constant>(ConstantVal));
1455 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1456 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1459 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1460 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1463 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1464 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1467 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1468 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1471 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1472 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1473 case GlobalVariable::NotThreadLocal:
1474 return LLVMNotThreadLocal;
1475 case GlobalVariable::GeneralDynamicTLSModel:
1476 return LLVMGeneralDynamicTLSModel;
1477 case GlobalVariable::LocalDynamicTLSModel:
1478 return LLVMLocalDynamicTLSModel;
1479 case GlobalVariable::InitialExecTLSModel:
1480 return LLVMInitialExecTLSModel;
1481 case GlobalVariable::LocalExecTLSModel:
1482 return LLVMLocalExecTLSModel;
1485 llvm_unreachable("Invalid GlobalVariable thread local mode");
1488 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1489 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1492 case LLVMNotThreadLocal:
1493 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1495 case LLVMGeneralDynamicTLSModel:
1496 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1498 case LLVMLocalDynamicTLSModel:
1499 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1501 case LLVMInitialExecTLSModel:
1502 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1504 case LLVMLocalExecTLSModel:
1505 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1510 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1511 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1514 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1515 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1518 /*--.. Operations on aliases ......................................--*/
1520 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1522 auto *PTy = cast<PointerType>(unwrap(Ty));
1523 return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
1524 GlobalValue::ExternalLinkage, Name,
1525 unwrap<GlobalObject>(Aliasee), unwrap(M)));
1528 /*--.. Operations on functions .............................................--*/
1530 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1531 LLVMTypeRef FunctionTy) {
1532 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1533 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1536 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1537 return wrap(unwrap(M)->getFunction(Name));
1540 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1541 Module *Mod = unwrap(M);
1542 Module::iterator I = Mod->begin();
1543 if (I == Mod->end())
1548 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1549 Module *Mod = unwrap(M);
1550 Module::iterator I = Mod->end();
1551 if (I == Mod->begin())
1556 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1557 Function *Func = unwrap<Function>(Fn);
1558 Module::iterator I = Func;
1559 if (++I == Func->getParent()->end())
1564 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1565 Function *Func = unwrap<Function>(Fn);
1566 Module::iterator I = Func;
1567 if (I == Func->getParent()->begin())
1572 void LLVMDeleteFunction(LLVMValueRef Fn) {
1573 unwrap<Function>(Fn)->eraseFromParent();
1576 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1577 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1578 return F->getIntrinsicID();
1582 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1583 return unwrap<Function>(Fn)->getCallingConv();
1586 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1587 return unwrap<Function>(Fn)->setCallingConv(
1588 static_cast<CallingConv::ID>(CC));
1591 const char *LLVMGetGC(LLVMValueRef Fn) {
1592 Function *F = unwrap<Function>(Fn);
1593 return F->hasGC()? F->getGC() : nullptr;
1596 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1597 Function *F = unwrap<Function>(Fn);
1604 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1605 Function *Func = unwrap<Function>(Fn);
1606 const AttributeSet PAL = Func->getAttributes();
1608 const AttributeSet PALnew =
1609 PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1610 AttributeSet::get(Func->getContext(),
1611 AttributeSet::FunctionIndex, B));
1612 Func->setAttributes(PALnew);
1615 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1617 Function *Func = unwrap<Function>(Fn);
1618 AttributeSet::AttrIndex Idx =
1619 AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
1622 B.addAttribute(A, V);
1623 AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
1624 Func->addAttributes(Idx, Set);
1627 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1628 Function *Func = unwrap<Function>(Fn);
1629 const AttributeSet PAL = Func->getAttributes();
1631 const AttributeSet PALnew =
1632 PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1633 AttributeSet::get(Func->getContext(),
1634 AttributeSet::FunctionIndex, B));
1635 Func->setAttributes(PALnew);
1638 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1639 Function *Func = unwrap<Function>(Fn);
1640 const AttributeSet PAL = Func->getAttributes();
1641 return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
1644 /*--.. Operations on parameters ............................................--*/
1646 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1647 // This function is strictly redundant to
1648 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1649 return unwrap<Function>(FnRef)->arg_size();
1652 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1653 Function *Fn = unwrap<Function>(FnRef);
1654 for (Function::arg_iterator I = Fn->arg_begin(),
1655 E = Fn->arg_end(); I != E; I++)
1656 *ParamRefs++ = wrap(I);
1659 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1660 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1666 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1667 return wrap(unwrap<Argument>(V)->getParent());
1670 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1671 Function *Func = unwrap<Function>(Fn);
1672 Function::arg_iterator I = Func->arg_begin();
1673 if (I == Func->arg_end())
1678 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1679 Function *Func = unwrap<Function>(Fn);
1680 Function::arg_iterator I = Func->arg_end();
1681 if (I == Func->arg_begin())
1686 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1687 Argument *A = unwrap<Argument>(Arg);
1688 Function::arg_iterator I = A;
1689 if (++I == A->getParent()->arg_end())
1694 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1695 Argument *A = unwrap<Argument>(Arg);
1696 Function::arg_iterator I = A;
1697 if (I == A->getParent()->arg_begin())
1702 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1703 Argument *A = unwrap<Argument>(Arg);
1705 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1708 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1709 Argument *A = unwrap<Argument>(Arg);
1711 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1714 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1715 Argument *A = unwrap<Argument>(Arg);
1716 return (LLVMAttribute)A->getParent()->getAttributes().
1717 Raw(A->getArgNo()+1);
1721 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1722 Argument *A = unwrap<Argument>(Arg);
1724 B.addAlignmentAttr(align);
1725 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
1728 /*--.. Operations on basic blocks ..........................................--*/
1730 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1731 return wrap(static_cast<Value*>(unwrap(BB)));
1734 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1735 return isa<BasicBlock>(unwrap(Val));
1738 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1739 return wrap(unwrap<BasicBlock>(Val));
1742 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1743 return wrap(unwrap(BB)->getParent());
1746 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1747 return wrap(unwrap(BB)->getTerminator());
1750 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1751 return unwrap<Function>(FnRef)->size();
1754 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1755 Function *Fn = unwrap<Function>(FnRef);
1756 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1757 *BasicBlocksRefs++ = wrap(I);
1760 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1761 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1764 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1765 Function *Func = unwrap<Function>(Fn);
1766 Function::iterator I = Func->begin();
1767 if (I == Func->end())
1772 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1773 Function *Func = unwrap<Function>(Fn);
1774 Function::iterator I = Func->end();
1775 if (I == Func->begin())
1780 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1781 BasicBlock *Block = unwrap(BB);
1782 Function::iterator I = Block;
1783 if (++I == Block->getParent()->end())
1788 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1789 BasicBlock *Block = unwrap(BB);
1790 Function::iterator I = Block;
1791 if (I == Block->getParent()->begin())
1796 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1799 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1802 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1803 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1806 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1807 LLVMBasicBlockRef BBRef,
1809 BasicBlock *BB = unwrap(BBRef);
1810 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1813 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1815 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1818 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1819 unwrap(BBRef)->eraseFromParent();
1822 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1823 unwrap(BBRef)->removeFromParent();
1826 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1827 unwrap(BB)->moveBefore(unwrap(MovePos));
1830 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1831 unwrap(BB)->moveAfter(unwrap(MovePos));
1834 /*--.. Operations on instructions ..........................................--*/
1836 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1837 return wrap(unwrap<Instruction>(Inst)->getParent());
1840 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1841 BasicBlock *Block = unwrap(BB);
1842 BasicBlock::iterator I = Block->begin();
1843 if (I == Block->end())
1848 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1849 BasicBlock *Block = unwrap(BB);
1850 BasicBlock::iterator I = Block->end();
1851 if (I == Block->begin())
1856 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1857 Instruction *Instr = unwrap<Instruction>(Inst);
1858 BasicBlock::iterator I = Instr;
1859 if (++I == Instr->getParent()->end())
1864 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1865 Instruction *Instr = unwrap<Instruction>(Inst);
1866 BasicBlock::iterator I = Instr;
1867 if (I == Instr->getParent()->begin())
1872 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1873 unwrap<Instruction>(Inst)->eraseFromParent();
1876 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
1877 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
1878 return (LLVMIntPredicate)I->getPredicate();
1879 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1880 if (CE->getOpcode() == Instruction::ICmp)
1881 return (LLVMIntPredicate)CE->getPredicate();
1882 return (LLVMIntPredicate)0;
1885 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
1886 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
1887 return map_to_llvmopcode(C->getOpcode());
1888 return (LLVMOpcode)0;
1891 /*--.. Call and invoke instructions ........................................--*/
1893 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1894 Value *V = unwrap(Instr);
1895 if (CallInst *CI = dyn_cast<CallInst>(V))
1896 return CI->getCallingConv();
1897 if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1898 return II->getCallingConv();
1899 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1902 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1903 Value *V = unwrap(Instr);
1904 if (CallInst *CI = dyn_cast<CallInst>(V))
1905 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1906 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1907 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1908 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1911 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1913 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1916 Call.getAttributes().addAttributes(Call->getContext(), index,
1917 AttributeSet::get(Call->getContext(),
1921 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1923 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1925 Call.setAttributes(Call.getAttributes()
1926 .removeAttributes(Call->getContext(), index,
1927 AttributeSet::get(Call->getContext(),
1931 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1933 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1935 B.addAlignmentAttr(align);
1936 Call.setAttributes(Call.getAttributes()
1937 .addAttributes(Call->getContext(), index,
1938 AttributeSet::get(Call->getContext(),
1942 /*--.. Operations on call instructions (only) ..............................--*/
1944 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1945 return unwrap<CallInst>(Call)->isTailCall();
1948 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1949 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1952 /*--.. Operations on switch instructions (only) ............................--*/
1954 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
1955 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
1958 /*--.. Operations on phi nodes .............................................--*/
1960 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1961 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1962 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1963 for (unsigned I = 0; I != Count; ++I)
1964 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1967 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1968 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1971 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1972 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1975 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1976 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1980 /*===-- Instruction builders ----------------------------------------------===*/
1982 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1983 return wrap(new IRBuilder<>(*unwrap(C)));
1986 LLVMBuilderRef LLVMCreateBuilder(void) {
1987 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1990 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1991 LLVMValueRef Instr) {
1992 BasicBlock *BB = unwrap(Block);
1993 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1994 unwrap(Builder)->SetInsertPoint(BB, I);
1997 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1998 Instruction *I = unwrap<Instruction>(Instr);
1999 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
2002 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
2003 BasicBlock *BB = unwrap(Block);
2004 unwrap(Builder)->SetInsertPoint(BB);
2007 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
2008 return wrap(unwrap(Builder)->GetInsertBlock());
2011 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
2012 unwrap(Builder)->ClearInsertionPoint();
2015 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2016 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
2019 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
2021 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
2024 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
2025 delete unwrap(Builder);
2028 /*--.. Metadata builders ...................................................--*/
2030 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
2031 MDNode *Loc = L ? unwrap<MDNode>(L) : nullptr;
2032 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
2035 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
2036 return wrap(unwrap(Builder)->getCurrentDebugLocation()
2037 .getAsMDNode(unwrap(Builder)->getContext()));
2040 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
2041 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
2045 /*--.. Instruction builders ................................................--*/
2047 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
2048 return wrap(unwrap(B)->CreateRetVoid());
2051 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
2052 return wrap(unwrap(B)->CreateRet(unwrap(V)));
2055 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
2057 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
2060 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
2061 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
2064 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
2065 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
2066 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
2069 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
2070 LLVMBasicBlockRef Else, unsigned NumCases) {
2071 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
2074 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
2075 unsigned NumDests) {
2076 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
2079 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
2080 LLVMValueRef *Args, unsigned NumArgs,
2081 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
2083 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
2084 makeArrayRef(unwrap(Args), NumArgs),
2088 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
2089 LLVMValueRef PersFn, unsigned NumClauses,
2091 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
2092 cast<Function>(unwrap(PersFn)),
2096 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
2097 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
2100 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
2101 return wrap(unwrap(B)->CreateUnreachable());
2104 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
2105 LLVMBasicBlockRef Dest) {
2106 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
2109 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
2110 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
2113 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
2114 unwrap<LandingPadInst>(LandingPad)->
2115 addClause(cast<Constant>(unwrap(ClauseVal)));
2118 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
2119 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2122 /*--.. Arithmetic ..........................................................--*/
2124 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2126 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2129 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2131 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2134 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2136 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2139 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2141 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2144 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2146 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2149 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2151 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2154 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2156 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2159 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2161 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2164 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2166 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2169 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2171 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2174 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2176 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2179 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2181 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2184 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2186 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2189 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2191 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2194 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2195 LLVMValueRef RHS, const char *Name) {
2196 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2199 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2201 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2204 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2206 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2209 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2211 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2214 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2216 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2219 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2221 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2224 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2226 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2229 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2231 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2234 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2236 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2239 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2241 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2244 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2246 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2249 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2250 LLVMValueRef LHS, LLVMValueRef RHS,
2252 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2253 unwrap(RHS), Name));
2256 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2257 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2260 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2262 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2265 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2267 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2270 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2271 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2274 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2275 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2278 /*--.. Memory ..............................................................--*/
2280 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2282 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2283 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2284 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2285 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2286 ITy, unwrap(Ty), AllocSize,
2287 nullptr, nullptr, "");
2288 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2291 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2292 LLVMValueRef Val, const char *Name) {
2293 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2294 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2295 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2296 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2297 ITy, unwrap(Ty), AllocSize,
2298 unwrap(Val), nullptr, "");
2299 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2302 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2304 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
2307 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2308 LLVMValueRef Val, const char *Name) {
2309 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2312 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2313 return wrap(unwrap(B)->Insert(
2314 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2318 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2320 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2323 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2324 LLVMValueRef PointerVal) {
2325 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2328 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
2330 case LLVMAtomicOrderingNotAtomic: return NotAtomic;
2331 case LLVMAtomicOrderingUnordered: return Unordered;
2332 case LLVMAtomicOrderingMonotonic: return Monotonic;
2333 case LLVMAtomicOrderingAcquire: return Acquire;
2334 case LLVMAtomicOrderingRelease: return Release;
2335 case LLVMAtomicOrderingAcquireRelease: return AcquireRelease;
2336 case LLVMAtomicOrderingSequentiallyConsistent:
2337 return SequentiallyConsistent;
2340 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
2343 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
2344 LLVMBool isSingleThread, const char *Name) {
2346 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
2347 isSingleThread ? SingleThread : CrossThread,
2351 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2352 LLVMValueRef *Indices, unsigned NumIndices,
2354 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2355 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
2358 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2359 LLVMValueRef *Indices, unsigned NumIndices,
2361 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2362 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
2365 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2366 unsigned Idx, const char *Name) {
2367 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2370 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2372 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2375 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2377 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2380 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2381 Value *P = unwrap<Value>(MemAccessInst);
2382 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2383 return LI->isVolatile();
2384 return cast<StoreInst>(P)->isVolatile();
2387 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2388 Value *P = unwrap<Value>(MemAccessInst);
2389 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2390 return LI->setVolatile(isVolatile);
2391 return cast<StoreInst>(P)->setVolatile(isVolatile);
2394 /*--.. Casts ...............................................................--*/
2396 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2397 LLVMTypeRef DestTy, const char *Name) {
2398 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2401 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2402 LLVMTypeRef DestTy, const char *Name) {
2403 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2406 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2407 LLVMTypeRef DestTy, const char *Name) {
2408 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2411 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2412 LLVMTypeRef DestTy, const char *Name) {
2413 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2416 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2417 LLVMTypeRef DestTy, const char *Name) {
2418 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2421 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2422 LLVMTypeRef DestTy, const char *Name) {
2423 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2426 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2427 LLVMTypeRef DestTy, const char *Name) {
2428 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2431 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2432 LLVMTypeRef DestTy, const char *Name) {
2433 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2436 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2437 LLVMTypeRef DestTy, const char *Name) {
2438 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2441 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2442 LLVMTypeRef DestTy, const char *Name) {
2443 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2446 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2447 LLVMTypeRef DestTy, const char *Name) {
2448 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2451 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2452 LLVMTypeRef DestTy, const char *Name) {
2453 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2456 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
2457 LLVMTypeRef DestTy, const char *Name) {
2458 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
2461 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2462 LLVMTypeRef DestTy, const char *Name) {
2463 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2467 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2468 LLVMTypeRef DestTy, const char *Name) {
2469 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2473 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2474 LLVMTypeRef DestTy, const char *Name) {
2475 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2479 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2480 LLVMTypeRef DestTy, const char *Name) {
2481 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2482 unwrap(DestTy), Name));
2485 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2486 LLVMTypeRef DestTy, const char *Name) {
2487 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2490 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2491 LLVMTypeRef DestTy, const char *Name) {
2492 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2493 /*isSigned*/true, Name));
2496 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2497 LLVMTypeRef DestTy, const char *Name) {
2498 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2501 /*--.. Comparisons .........................................................--*/
2503 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2504 LLVMValueRef LHS, LLVMValueRef RHS,
2506 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2507 unwrap(LHS), unwrap(RHS), Name));
2510 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2511 LLVMValueRef LHS, LLVMValueRef RHS,
2513 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2514 unwrap(LHS), unwrap(RHS), Name));
2517 /*--.. Miscellaneous instructions ..........................................--*/
2519 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2520 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2523 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2524 LLVMValueRef *Args, unsigned NumArgs,
2526 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2527 makeArrayRef(unwrap(Args), NumArgs),
2531 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2532 LLVMValueRef Then, LLVMValueRef Else,
2534 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2538 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2539 LLVMTypeRef Ty, const char *Name) {
2540 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2543 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2544 LLVMValueRef Index, const char *Name) {
2545 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2549 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2550 LLVMValueRef EltVal, LLVMValueRef Index,
2552 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2553 unwrap(Index), Name));
2556 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2557 LLVMValueRef V2, LLVMValueRef Mask,
2559 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2560 unwrap(Mask), Name));
2563 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2564 unsigned Index, const char *Name) {
2565 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2568 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2569 LLVMValueRef EltVal, unsigned Index,
2571 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2575 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2577 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2580 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2582 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2585 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2586 LLVMValueRef RHS, const char *Name) {
2587 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2590 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
2591 LLVMValueRef PTR, LLVMValueRef Val,
2592 LLVMAtomicOrdering ordering,
2593 LLVMBool singleThread) {
2594 AtomicRMWInst::BinOp intop;
2596 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
2597 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
2598 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
2599 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
2600 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
2601 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
2602 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
2603 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
2604 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
2605 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
2606 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
2608 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
2609 mapFromLLVMOrdering(ordering), singleThread ? SingleThread : CrossThread));
2613 /*===-- Module providers --------------------------------------------------===*/
2615 LLVMModuleProviderRef
2616 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2617 return reinterpret_cast<LLVMModuleProviderRef>(M);
2620 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2625 /*===-- Memory buffers ----------------------------------------------------===*/
2627 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2629 LLVMMemoryBufferRef *OutMemBuf,
2630 char **OutMessage) {
2632 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
2633 if (std::error_code EC = MBOrErr.getError()) {
2634 *OutMessage = strdup(EC.message().c_str());
2637 *OutMemBuf = wrap(MBOrErr.get().release());
2641 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2642 char **OutMessage) {
2643 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
2644 if (std::error_code EC = MBOrErr.getError()) {
2645 *OutMessage = strdup(EC.message().c_str());
2648 *OutMemBuf = wrap(MBOrErr.get().release());
2652 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
2653 const char *InputData,
2654 size_t InputDataLength,
2655 const char *BufferName,
2656 LLVMBool RequiresNullTerminator) {
2658 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
2659 StringRef(BufferName),
2660 RequiresNullTerminator).release());
2663 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
2664 const char *InputData,
2665 size_t InputDataLength,
2666 const char *BufferName) {
2669 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
2670 StringRef(BufferName)).release());
2673 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
2674 return unwrap(MemBuf)->getBufferStart();
2677 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
2678 return unwrap(MemBuf)->getBufferSize();
2681 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2682 delete unwrap(MemBuf);
2685 /*===-- Pass Registry -----------------------------------------------------===*/
2687 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2688 return wrap(PassRegistry::getPassRegistry());
2691 /*===-- Pass Manager ------------------------------------------------------===*/
2693 LLVMPassManagerRef LLVMCreatePassManager() {
2694 return wrap(new PassManager());
2697 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2698 return wrap(new FunctionPassManager(unwrap(M)));
2701 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2702 return LLVMCreateFunctionPassManagerForModule(
2703 reinterpret_cast<LLVMModuleRef>(P));
2706 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2707 return unwrap<PassManager>(PM)->run(*unwrap(M));
2710 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2711 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2714 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2715 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2718 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2719 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2722 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
2726 /*===-- Threading ------------------------------------------------------===*/
2728 LLVMBool LLVMStartMultithreaded() {
2729 return LLVMIsMultithreaded();
2732 void LLVMStopMultithreaded() {
2735 LLVMBool LLVMIsMultithreaded() {
2736 return llvm_is_multithreaded();