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)
564 ->setMetadata(KindID, MD ? unwrap<Value>(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 Value *Op = Val ? unwrap<Value>(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 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
778 ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
779 Type *Ty = cFP->getType();
781 if (Ty->isFloatTy()) {
783 return cFP->getValueAPF().convertToFloat();
786 if (Ty->isDoubleTy()) {
788 return cFP->getValueAPF().convertToDouble();
792 APFloat APF = cFP->getValueAPF();
793 APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &APFLosesInfo);
794 *LosesInfo = APFLosesInfo;
795 return APF.convertToDouble();
798 /*--.. Operations on composite constants ...................................--*/
800 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
802 LLVMBool DontNullTerminate) {
803 /* Inverted the sense of AddNull because ', 0)' is a
804 better mnemonic for null termination than ', 1)'. */
805 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
806 DontNullTerminate == 0));
808 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
809 LLVMValueRef *ConstantVals,
810 unsigned Count, LLVMBool Packed) {
811 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
812 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
816 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
817 LLVMBool DontNullTerminate) {
818 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
822 LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef c, unsigned idx) {
823 return wrap(static_cast<ConstantDataSequential*>(unwrap(c))->getElementAsConstant(idx));
826 LLVMBool LLVMIsConstantString(LLVMValueRef c) {
827 return static_cast<ConstantDataSequential*>(unwrap(c))->isString();
830 const char *LLVMGetAsString(LLVMValueRef c, size_t* Length) {
831 StringRef str = static_cast<ConstantDataSequential*>(unwrap(c))->getAsString();
832 *Length = str.size();
836 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
837 LLVMValueRef *ConstantVals, unsigned Length) {
838 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
839 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
842 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
844 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
848 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
849 LLVMValueRef *ConstantVals,
851 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
852 StructType *Ty = cast<StructType>(unwrap(StructTy));
854 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
857 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
858 return wrap(ConstantVector::get(makeArrayRef(
859 unwrap<Constant>(ScalarConstantVals, Size), Size)));
862 /*-- Opcode mapping */
864 static LLVMOpcode map_to_llvmopcode(int opcode)
867 default: llvm_unreachable("Unhandled Opcode.");
868 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
869 #include "llvm/IR/Instruction.def"
874 static int map_from_llvmopcode(LLVMOpcode code)
877 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
878 #include "llvm/IR/Instruction.def"
881 llvm_unreachable("Unhandled Opcode.");
884 /*--.. Constant expressions ................................................--*/
886 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
887 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
890 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
891 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
894 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
895 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
898 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
899 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
902 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
903 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
906 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
907 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
911 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
912 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
915 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
916 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
919 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
920 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
921 unwrap<Constant>(RHSConstant)));
924 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
925 LLVMValueRef RHSConstant) {
926 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
927 unwrap<Constant>(RHSConstant)));
930 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
931 LLVMValueRef RHSConstant) {
932 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
933 unwrap<Constant>(RHSConstant)));
936 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
937 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
938 unwrap<Constant>(RHSConstant)));
941 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
942 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
943 unwrap<Constant>(RHSConstant)));
946 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
947 LLVMValueRef RHSConstant) {
948 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
949 unwrap<Constant>(RHSConstant)));
952 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
953 LLVMValueRef RHSConstant) {
954 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
955 unwrap<Constant>(RHSConstant)));
958 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
959 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
960 unwrap<Constant>(RHSConstant)));
963 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
964 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
965 unwrap<Constant>(RHSConstant)));
968 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
969 LLVMValueRef RHSConstant) {
970 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
971 unwrap<Constant>(RHSConstant)));
974 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
975 LLVMValueRef RHSConstant) {
976 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
977 unwrap<Constant>(RHSConstant)));
980 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
981 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
982 unwrap<Constant>(RHSConstant)));
985 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
986 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
987 unwrap<Constant>(RHSConstant)));
990 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
991 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
992 unwrap<Constant>(RHSConstant)));
995 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
996 LLVMValueRef RHSConstant) {
997 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
998 unwrap<Constant>(RHSConstant)));
1001 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1002 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
1003 unwrap<Constant>(RHSConstant)));
1006 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1007 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
1008 unwrap<Constant>(RHSConstant)));
1011 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1012 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
1013 unwrap<Constant>(RHSConstant)));
1016 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1017 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
1018 unwrap<Constant>(RHSConstant)));
1021 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1022 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
1023 unwrap<Constant>(RHSConstant)));
1026 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1027 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
1028 unwrap<Constant>(RHSConstant)));
1031 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1032 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
1033 unwrap<Constant>(RHSConstant)));
1036 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
1037 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1038 return wrap(ConstantExpr::getICmp(Predicate,
1039 unwrap<Constant>(LHSConstant),
1040 unwrap<Constant>(RHSConstant)));
1043 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
1044 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1045 return wrap(ConstantExpr::getFCmp(Predicate,
1046 unwrap<Constant>(LHSConstant),
1047 unwrap<Constant>(RHSConstant)));
1050 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1051 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1052 unwrap<Constant>(RHSConstant)));
1055 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1056 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
1057 unwrap<Constant>(RHSConstant)));
1060 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1061 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1062 unwrap<Constant>(RHSConstant)));
1065 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
1066 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1067 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1069 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
1073 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
1074 LLVMValueRef *ConstantIndices,
1075 unsigned NumIndices) {
1076 Constant* Val = unwrap<Constant>(ConstantVal);
1077 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1079 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
1082 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1083 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1087 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1088 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1092 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1093 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1097 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1098 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1102 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1103 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1107 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1108 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1112 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1113 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1117 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1118 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1122 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1123 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1127 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1128 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1132 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1133 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1137 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1138 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1142 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1143 LLVMTypeRef ToType) {
1144 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1148 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1149 LLVMTypeRef ToType) {
1150 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1154 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1155 LLVMTypeRef ToType) {
1156 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1160 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1161 LLVMTypeRef ToType) {
1162 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1166 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1167 LLVMTypeRef ToType) {
1168 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1172 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1173 LLVMBool isSigned) {
1174 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1175 unwrap(ToType), isSigned));
1178 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1179 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1183 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1184 LLVMValueRef ConstantIfTrue,
1185 LLVMValueRef ConstantIfFalse) {
1186 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1187 unwrap<Constant>(ConstantIfTrue),
1188 unwrap<Constant>(ConstantIfFalse)));
1191 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1192 LLVMValueRef IndexConstant) {
1193 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1194 unwrap<Constant>(IndexConstant)));
1197 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1198 LLVMValueRef ElementValueConstant,
1199 LLVMValueRef IndexConstant) {
1200 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1201 unwrap<Constant>(ElementValueConstant),
1202 unwrap<Constant>(IndexConstant)));
1205 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1206 LLVMValueRef VectorBConstant,
1207 LLVMValueRef MaskConstant) {
1208 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1209 unwrap<Constant>(VectorBConstant),
1210 unwrap<Constant>(MaskConstant)));
1213 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1215 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1216 makeArrayRef(IdxList, NumIdx)));
1219 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1220 LLVMValueRef ElementValueConstant,
1221 unsigned *IdxList, unsigned NumIdx) {
1222 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1223 unwrap<Constant>(ElementValueConstant),
1224 makeArrayRef(IdxList, NumIdx)));
1227 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1228 const char *Constraints,
1229 LLVMBool HasSideEffects,
1230 LLVMBool IsAlignStack) {
1231 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1232 Constraints, HasSideEffects, IsAlignStack));
1235 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1236 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1239 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1241 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1242 return wrap(unwrap<GlobalValue>(Global)->getParent());
1245 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1246 return unwrap<GlobalValue>(Global)->isDeclaration();
1249 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1250 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1251 case GlobalValue::ExternalLinkage:
1252 return LLVMExternalLinkage;
1253 case GlobalValue::AvailableExternallyLinkage:
1254 return LLVMAvailableExternallyLinkage;
1255 case GlobalValue::LinkOnceAnyLinkage:
1256 return LLVMLinkOnceAnyLinkage;
1257 case GlobalValue::LinkOnceODRLinkage:
1258 return LLVMLinkOnceODRLinkage;
1259 case GlobalValue::WeakAnyLinkage:
1260 return LLVMWeakAnyLinkage;
1261 case GlobalValue::WeakODRLinkage:
1262 return LLVMWeakODRLinkage;
1263 case GlobalValue::AppendingLinkage:
1264 return LLVMAppendingLinkage;
1265 case GlobalValue::InternalLinkage:
1266 return LLVMInternalLinkage;
1267 case GlobalValue::PrivateLinkage:
1268 return LLVMPrivateLinkage;
1269 case GlobalValue::ExternalWeakLinkage:
1270 return LLVMExternalWeakLinkage;
1271 case GlobalValue::CommonLinkage:
1272 return LLVMCommonLinkage;
1275 llvm_unreachable("Invalid GlobalValue linkage!");
1278 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1279 GlobalValue *GV = unwrap<GlobalValue>(Global);
1282 case LLVMExternalLinkage:
1283 GV->setLinkage(GlobalValue::ExternalLinkage);
1285 case LLVMAvailableExternallyLinkage:
1286 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1288 case LLVMLinkOnceAnyLinkage:
1289 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1291 case LLVMLinkOnceODRLinkage:
1292 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1294 case LLVMLinkOnceODRAutoHideLinkage:
1295 DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1296 "longer supported.");
1298 case LLVMWeakAnyLinkage:
1299 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1301 case LLVMWeakODRLinkage:
1302 GV->setLinkage(GlobalValue::WeakODRLinkage);
1304 case LLVMAppendingLinkage:
1305 GV->setLinkage(GlobalValue::AppendingLinkage);
1307 case LLVMInternalLinkage:
1308 GV->setLinkage(GlobalValue::InternalLinkage);
1310 case LLVMPrivateLinkage:
1311 GV->setLinkage(GlobalValue::PrivateLinkage);
1313 case LLVMLinkerPrivateLinkage:
1314 GV->setLinkage(GlobalValue::PrivateLinkage);
1316 case LLVMLinkerPrivateWeakLinkage:
1317 GV->setLinkage(GlobalValue::PrivateLinkage);
1319 case LLVMDLLImportLinkage:
1321 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1323 case LLVMDLLExportLinkage:
1325 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1327 case LLVMExternalWeakLinkage:
1328 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1330 case LLVMGhostLinkage:
1332 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1334 case LLVMCommonLinkage:
1335 GV->setLinkage(GlobalValue::CommonLinkage);
1340 const char *LLVMGetSection(LLVMValueRef Global) {
1341 return unwrap<GlobalValue>(Global)->getSection();
1344 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1345 unwrap<GlobalObject>(Global)->setSection(Section);
1348 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1349 return static_cast<LLVMVisibility>(
1350 unwrap<GlobalValue>(Global)->getVisibility());
1353 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1354 unwrap<GlobalValue>(Global)
1355 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1358 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
1359 return static_cast<LLVMDLLStorageClass>(
1360 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1363 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
1364 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1365 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1368 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
1369 return unwrap<GlobalValue>(Global)->hasUnnamedAddr();
1372 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
1373 unwrap<GlobalValue>(Global)->setUnnamedAddr(HasUnnamedAddr);
1376 /*--.. Operations on global variables, load and store instructions .........--*/
1378 unsigned LLVMGetAlignment(LLVMValueRef V) {
1379 Value *P = unwrap<Value>(V);
1380 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1381 return GV->getAlignment();
1382 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1383 return AI->getAlignment();
1384 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1385 return LI->getAlignment();
1386 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1387 return SI->getAlignment();
1390 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1393 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1394 Value *P = unwrap<Value>(V);
1395 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
1396 GV->setAlignment(Bytes);
1397 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1398 AI->setAlignment(Bytes);
1399 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1400 LI->setAlignment(Bytes);
1401 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1402 SI->setAlignment(Bytes);
1405 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1408 /*--.. Operations on global variables ......................................--*/
1410 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1411 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1412 GlobalValue::ExternalLinkage, nullptr, Name));
1415 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1417 unsigned AddressSpace) {
1418 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1419 GlobalValue::ExternalLinkage, nullptr, Name,
1420 nullptr, GlobalVariable::NotThreadLocal,
1424 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1425 return wrap(unwrap(M)->getNamedGlobal(Name));
1428 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1429 Module *Mod = unwrap(M);
1430 Module::global_iterator I = Mod->global_begin();
1431 if (I == Mod->global_end())
1436 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1437 Module *Mod = unwrap(M);
1438 Module::global_iterator I = Mod->global_end();
1439 if (I == Mod->global_begin())
1444 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1445 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1446 Module::global_iterator I = GV;
1447 if (++I == GV->getParent()->global_end())
1452 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1453 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1454 Module::global_iterator I = GV;
1455 if (I == GV->getParent()->global_begin())
1460 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1461 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1464 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1465 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1466 if ( !GV->hasInitializer() )
1468 return wrap(GV->getInitializer());
1471 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1472 unwrap<GlobalVariable>(GlobalVar)
1473 ->setInitializer(unwrap<Constant>(ConstantVal));
1476 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1477 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1480 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1481 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1484 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1485 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1488 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1489 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1492 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1493 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1494 case GlobalVariable::NotThreadLocal:
1495 return LLVMNotThreadLocal;
1496 case GlobalVariable::GeneralDynamicTLSModel:
1497 return LLVMGeneralDynamicTLSModel;
1498 case GlobalVariable::LocalDynamicTLSModel:
1499 return LLVMLocalDynamicTLSModel;
1500 case GlobalVariable::InitialExecTLSModel:
1501 return LLVMInitialExecTLSModel;
1502 case GlobalVariable::LocalExecTLSModel:
1503 return LLVMLocalExecTLSModel;
1506 llvm_unreachable("Invalid GlobalVariable thread local mode");
1509 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1510 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1513 case LLVMNotThreadLocal:
1514 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1516 case LLVMGeneralDynamicTLSModel:
1517 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1519 case LLVMLocalDynamicTLSModel:
1520 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1522 case LLVMInitialExecTLSModel:
1523 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1525 case LLVMLocalExecTLSModel:
1526 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1531 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1532 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1535 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1536 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1539 /*--.. Operations on aliases ......................................--*/
1541 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1543 auto *PTy = cast<PointerType>(unwrap(Ty));
1544 return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
1545 GlobalValue::ExternalLinkage, Name,
1546 unwrap<GlobalObject>(Aliasee), unwrap(M)));
1549 /*--.. Operations on functions .............................................--*/
1551 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1552 LLVMTypeRef FunctionTy) {
1553 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1554 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1557 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1558 return wrap(unwrap(M)->getFunction(Name));
1561 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1562 Module *Mod = unwrap(M);
1563 Module::iterator I = Mod->begin();
1564 if (I == Mod->end())
1569 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1570 Module *Mod = unwrap(M);
1571 Module::iterator I = Mod->end();
1572 if (I == Mod->begin())
1577 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1578 Function *Func = unwrap<Function>(Fn);
1579 Module::iterator I = Func;
1580 if (++I == Func->getParent()->end())
1585 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1586 Function *Func = unwrap<Function>(Fn);
1587 Module::iterator I = Func;
1588 if (I == Func->getParent()->begin())
1593 void LLVMDeleteFunction(LLVMValueRef Fn) {
1594 unwrap<Function>(Fn)->eraseFromParent();
1597 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1598 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1599 return F->getIntrinsicID();
1603 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1604 return unwrap<Function>(Fn)->getCallingConv();
1607 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1608 return unwrap<Function>(Fn)->setCallingConv(
1609 static_cast<CallingConv::ID>(CC));
1612 const char *LLVMGetGC(LLVMValueRef Fn) {
1613 Function *F = unwrap<Function>(Fn);
1614 return F->hasGC()? F->getGC() : nullptr;
1617 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1618 Function *F = unwrap<Function>(Fn);
1625 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1626 Function *Func = unwrap<Function>(Fn);
1627 const AttributeSet PAL = Func->getAttributes();
1629 const AttributeSet PALnew =
1630 PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1631 AttributeSet::get(Func->getContext(),
1632 AttributeSet::FunctionIndex, B));
1633 Func->setAttributes(PALnew);
1636 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1638 Function *Func = unwrap<Function>(Fn);
1639 AttributeSet::AttrIndex Idx =
1640 AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
1643 B.addAttribute(A, V);
1644 AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
1645 Func->addAttributes(Idx, Set);
1648 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1649 Function *Func = unwrap<Function>(Fn);
1650 const AttributeSet PAL = Func->getAttributes();
1652 const AttributeSet PALnew =
1653 PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1654 AttributeSet::get(Func->getContext(),
1655 AttributeSet::FunctionIndex, B));
1656 Func->setAttributes(PALnew);
1659 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1660 Function *Func = unwrap<Function>(Fn);
1661 const AttributeSet PAL = Func->getAttributes();
1662 return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
1665 /*--.. Operations on parameters ............................................--*/
1667 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1668 // This function is strictly redundant to
1669 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1670 return unwrap<Function>(FnRef)->arg_size();
1673 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1674 Function *Fn = unwrap<Function>(FnRef);
1675 for (Function::arg_iterator I = Fn->arg_begin(),
1676 E = Fn->arg_end(); I != E; I++)
1677 *ParamRefs++ = wrap(I);
1680 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1681 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1687 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1688 return wrap(unwrap<Argument>(V)->getParent());
1691 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1692 Function *Func = unwrap<Function>(Fn);
1693 Function::arg_iterator I = Func->arg_begin();
1694 if (I == Func->arg_end())
1699 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1700 Function *Func = unwrap<Function>(Fn);
1701 Function::arg_iterator I = Func->arg_end();
1702 if (I == Func->arg_begin())
1707 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1708 Argument *A = unwrap<Argument>(Arg);
1709 Function::arg_iterator I = A;
1710 if (++I == A->getParent()->arg_end())
1715 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1716 Argument *A = unwrap<Argument>(Arg);
1717 Function::arg_iterator I = A;
1718 if (I == A->getParent()->arg_begin())
1723 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1724 Argument *A = unwrap<Argument>(Arg);
1726 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1729 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1730 Argument *A = unwrap<Argument>(Arg);
1732 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1735 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1736 Argument *A = unwrap<Argument>(Arg);
1737 return (LLVMAttribute)A->getParent()->getAttributes().
1738 Raw(A->getArgNo()+1);
1742 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1743 Argument *A = unwrap<Argument>(Arg);
1745 B.addAlignmentAttr(align);
1746 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
1749 /*--.. Operations on basic blocks ..........................................--*/
1751 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1752 return wrap(static_cast<Value*>(unwrap(BB)));
1755 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1756 return isa<BasicBlock>(unwrap(Val));
1759 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1760 return wrap(unwrap<BasicBlock>(Val));
1763 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1764 return wrap(unwrap(BB)->getParent());
1767 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1768 return wrap(unwrap(BB)->getTerminator());
1771 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1772 return unwrap<Function>(FnRef)->size();
1775 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1776 Function *Fn = unwrap<Function>(FnRef);
1777 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1778 *BasicBlocksRefs++ = wrap(I);
1781 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1782 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1785 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1786 Function *Func = unwrap<Function>(Fn);
1787 Function::iterator I = Func->begin();
1788 if (I == Func->end())
1793 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1794 Function *Func = unwrap<Function>(Fn);
1795 Function::iterator I = Func->end();
1796 if (I == Func->begin())
1801 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1802 BasicBlock *Block = unwrap(BB);
1803 Function::iterator I = Block;
1804 if (++I == Block->getParent()->end())
1809 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1810 BasicBlock *Block = unwrap(BB);
1811 Function::iterator I = Block;
1812 if (I == Block->getParent()->begin())
1817 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1820 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1823 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1824 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1827 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1828 LLVMBasicBlockRef BBRef,
1830 BasicBlock *BB = unwrap(BBRef);
1831 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1834 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1836 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1839 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1840 unwrap(BBRef)->eraseFromParent();
1843 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1844 unwrap(BBRef)->removeFromParent();
1847 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1848 unwrap(BB)->moveBefore(unwrap(MovePos));
1851 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1852 unwrap(BB)->moveAfter(unwrap(MovePos));
1855 /*--.. Operations on instructions ..........................................--*/
1857 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1858 return wrap(unwrap<Instruction>(Inst)->getParent());
1861 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1862 BasicBlock *Block = unwrap(BB);
1863 BasicBlock::iterator I = Block->begin();
1864 if (I == Block->end())
1869 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1870 BasicBlock *Block = unwrap(BB);
1871 BasicBlock::iterator I = Block->end();
1872 if (I == Block->begin())
1877 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1878 Instruction *Instr = unwrap<Instruction>(Inst);
1879 BasicBlock::iterator I = Instr;
1880 if (++I == Instr->getParent()->end())
1885 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1886 Instruction *Instr = unwrap<Instruction>(Inst);
1887 BasicBlock::iterator I = Instr;
1888 if (I == Instr->getParent()->begin())
1893 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1894 unwrap<Instruction>(Inst)->eraseFromParent();
1897 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
1898 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
1899 return (LLVMIntPredicate)I->getPredicate();
1900 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1901 if (CE->getOpcode() == Instruction::ICmp)
1902 return (LLVMIntPredicate)CE->getPredicate();
1903 return (LLVMIntPredicate)0;
1906 LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
1907 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
1908 return (LLVMRealPredicate)I->getPredicate();
1909 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1910 if (CE->getOpcode() == Instruction::FCmp)
1911 return (LLVMRealPredicate)CE->getPredicate();
1912 return (LLVMRealPredicate)0;
1915 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
1916 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
1917 return map_to_llvmopcode(C->getOpcode());
1918 return (LLVMOpcode)0;
1921 LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {
1922 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
1923 return wrap(C->clone());
1927 /*--.. Call and invoke instructions ........................................--*/
1929 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1930 Value *V = unwrap(Instr);
1931 if (CallInst *CI = dyn_cast<CallInst>(V))
1932 return CI->getCallingConv();
1933 if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1934 return II->getCallingConv();
1935 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1938 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1939 Value *V = unwrap(Instr);
1940 if (CallInst *CI = dyn_cast<CallInst>(V))
1941 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1942 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1943 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1944 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1947 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1949 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1952 Call.getAttributes().addAttributes(Call->getContext(), index,
1953 AttributeSet::get(Call->getContext(),
1957 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1959 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1961 Call.setAttributes(Call.getAttributes()
1962 .removeAttributes(Call->getContext(), index,
1963 AttributeSet::get(Call->getContext(),
1967 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1969 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1971 B.addAlignmentAttr(align);
1972 Call.setAttributes(Call.getAttributes()
1973 .addAttributes(Call->getContext(), index,
1974 AttributeSet::get(Call->getContext(),
1978 /*--.. Operations on call instructions (only) ..............................--*/
1980 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1981 return unwrap<CallInst>(Call)->isTailCall();
1984 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1985 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1988 /*--.. Operations on terminators ...........................................--*/
1990 unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {
1991 return unwrap<TerminatorInst>(Term)->getNumSuccessors();
1994 LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) {
1995 return wrap(unwrap<TerminatorInst>(Term)->getSuccessor(i));
1998 void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
1999 return unwrap<TerminatorInst>(Term)->setSuccessor(i,unwrap(block));
2002 /*--.. Operations on branch instructions (only) ............................--*/
2004 LLVMBool LLVMIsConditional(LLVMValueRef Branch) {
2005 return unwrap<BranchInst>(Branch)->isConditional();
2008 LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) {
2009 return wrap(unwrap<BranchInst>(Branch)->getCondition());
2012 void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) {
2013 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2016 /*--.. Operations on switch instructions (only) ............................--*/
2018 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
2019 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2022 /*--.. Operations on phi nodes .............................................--*/
2024 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
2025 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
2026 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
2027 for (unsigned I = 0; I != Count; ++I)
2028 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
2031 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
2032 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
2035 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
2036 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
2039 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
2040 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
2044 /*===-- Instruction builders ----------------------------------------------===*/
2046 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
2047 return wrap(new IRBuilder<>(*unwrap(C)));
2050 LLVMBuilderRef LLVMCreateBuilder(void) {
2051 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
2054 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
2055 LLVMValueRef Instr) {
2056 BasicBlock *BB = unwrap(Block);
2057 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
2058 unwrap(Builder)->SetInsertPoint(BB, I);
2061 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2062 Instruction *I = unwrap<Instruction>(Instr);
2063 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
2066 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
2067 BasicBlock *BB = unwrap(Block);
2068 unwrap(Builder)->SetInsertPoint(BB);
2071 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
2072 return wrap(unwrap(Builder)->GetInsertBlock());
2075 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
2076 unwrap(Builder)->ClearInsertionPoint();
2079 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2080 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
2083 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
2085 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
2088 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
2089 delete unwrap(Builder);
2092 /*--.. Metadata builders ...................................................--*/
2094 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
2095 MDNode *Loc = L ? unwrap<MDNode>(L) : nullptr;
2096 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
2099 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
2100 return wrap(unwrap(Builder)->getCurrentDebugLocation()
2101 .getAsMDNode(unwrap(Builder)->getContext()));
2104 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
2105 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
2109 /*--.. Instruction builders ................................................--*/
2111 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
2112 return wrap(unwrap(B)->CreateRetVoid());
2115 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
2116 return wrap(unwrap(B)->CreateRet(unwrap(V)));
2119 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
2121 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
2124 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
2125 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
2128 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
2129 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
2130 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
2133 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
2134 LLVMBasicBlockRef Else, unsigned NumCases) {
2135 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
2138 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
2139 unsigned NumDests) {
2140 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
2143 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
2144 LLVMValueRef *Args, unsigned NumArgs,
2145 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
2147 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
2148 makeArrayRef(unwrap(Args), NumArgs),
2152 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
2153 LLVMValueRef PersFn, unsigned NumClauses,
2155 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
2156 cast<Function>(unwrap(PersFn)),
2160 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
2161 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
2164 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
2165 return wrap(unwrap(B)->CreateUnreachable());
2168 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
2169 LLVMBasicBlockRef Dest) {
2170 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
2173 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
2174 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
2177 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
2178 unwrap<LandingPadInst>(LandingPad)->
2179 addClause(cast<Constant>(unwrap(ClauseVal)));
2182 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
2183 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2186 /*--.. Arithmetic ..........................................................--*/
2188 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2190 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2193 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2195 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2198 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2200 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2203 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2205 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2208 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2210 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2213 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2215 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2218 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2220 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2223 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2225 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2228 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2230 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2233 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2235 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2238 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2240 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2243 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2245 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2248 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2250 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2253 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2255 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2258 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2259 LLVMValueRef RHS, const char *Name) {
2260 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2263 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2265 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2268 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2270 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2273 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2275 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2278 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2280 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2283 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2285 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2288 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2290 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2293 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2295 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2298 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2300 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2303 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2305 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2308 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2310 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2313 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2314 LLVMValueRef LHS, LLVMValueRef RHS,
2316 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2317 unwrap(RHS), Name));
2320 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2321 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2324 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2326 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2329 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2331 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2334 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2335 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2338 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2339 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2342 /*--.. Memory ..............................................................--*/
2344 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2346 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2347 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2348 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2349 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2350 ITy, unwrap(Ty), AllocSize,
2351 nullptr, nullptr, "");
2352 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2355 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2356 LLVMValueRef Val, const char *Name) {
2357 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2358 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2359 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2360 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2361 ITy, unwrap(Ty), AllocSize,
2362 unwrap(Val), nullptr, "");
2363 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2366 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2368 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
2371 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2372 LLVMValueRef Val, const char *Name) {
2373 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2376 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2377 return wrap(unwrap(B)->Insert(
2378 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2382 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2384 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2387 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2388 LLVMValueRef PointerVal) {
2389 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2392 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
2394 case LLVMAtomicOrderingNotAtomic: return NotAtomic;
2395 case LLVMAtomicOrderingUnordered: return Unordered;
2396 case LLVMAtomicOrderingMonotonic: return Monotonic;
2397 case LLVMAtomicOrderingAcquire: return Acquire;
2398 case LLVMAtomicOrderingRelease: return Release;
2399 case LLVMAtomicOrderingAcquireRelease: return AcquireRelease;
2400 case LLVMAtomicOrderingSequentiallyConsistent:
2401 return SequentiallyConsistent;
2404 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
2407 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
2408 LLVMBool isSingleThread, const char *Name) {
2410 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
2411 isSingleThread ? SingleThread : CrossThread,
2415 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2416 LLVMValueRef *Indices, unsigned NumIndices,
2418 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2419 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
2422 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2423 LLVMValueRef *Indices, unsigned NumIndices,
2425 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2426 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
2429 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2430 unsigned Idx, const char *Name) {
2431 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2434 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2436 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2439 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2441 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2444 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2445 Value *P = unwrap<Value>(MemAccessInst);
2446 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2447 return LI->isVolatile();
2448 return cast<StoreInst>(P)->isVolatile();
2451 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2452 Value *P = unwrap<Value>(MemAccessInst);
2453 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2454 return LI->setVolatile(isVolatile);
2455 return cast<StoreInst>(P)->setVolatile(isVolatile);
2458 /*--.. Casts ...............................................................--*/
2460 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2461 LLVMTypeRef DestTy, const char *Name) {
2462 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2465 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2466 LLVMTypeRef DestTy, const char *Name) {
2467 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2470 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2471 LLVMTypeRef DestTy, const char *Name) {
2472 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2475 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2476 LLVMTypeRef DestTy, const char *Name) {
2477 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2480 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2481 LLVMTypeRef DestTy, const char *Name) {
2482 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2485 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2486 LLVMTypeRef DestTy, const char *Name) {
2487 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2490 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2491 LLVMTypeRef DestTy, const char *Name) {
2492 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2495 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2496 LLVMTypeRef DestTy, const char *Name) {
2497 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2500 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2501 LLVMTypeRef DestTy, const char *Name) {
2502 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2505 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2506 LLVMTypeRef DestTy, const char *Name) {
2507 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2510 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2511 LLVMTypeRef DestTy, const char *Name) {
2512 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2515 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2516 LLVMTypeRef DestTy, const char *Name) {
2517 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2520 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
2521 LLVMTypeRef DestTy, const char *Name) {
2522 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
2525 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2526 LLVMTypeRef DestTy, const char *Name) {
2527 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2531 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2532 LLVMTypeRef DestTy, const char *Name) {
2533 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2537 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2538 LLVMTypeRef DestTy, const char *Name) {
2539 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2543 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2544 LLVMTypeRef DestTy, const char *Name) {
2545 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2546 unwrap(DestTy), Name));
2549 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2550 LLVMTypeRef DestTy, const char *Name) {
2551 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2554 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2555 LLVMTypeRef DestTy, const char *Name) {
2556 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2557 /*isSigned*/true, Name));
2560 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2561 LLVMTypeRef DestTy, const char *Name) {
2562 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2565 /*--.. Comparisons .........................................................--*/
2567 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2568 LLVMValueRef LHS, LLVMValueRef RHS,
2570 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2571 unwrap(LHS), unwrap(RHS), Name));
2574 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2575 LLVMValueRef LHS, LLVMValueRef RHS,
2577 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2578 unwrap(LHS), unwrap(RHS), Name));
2581 /*--.. Miscellaneous instructions ..........................................--*/
2583 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2584 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2587 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2588 LLVMValueRef *Args, unsigned NumArgs,
2590 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2591 makeArrayRef(unwrap(Args), NumArgs),
2595 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2596 LLVMValueRef Then, LLVMValueRef Else,
2598 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2602 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2603 LLVMTypeRef Ty, const char *Name) {
2604 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2607 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2608 LLVMValueRef Index, const char *Name) {
2609 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2613 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2614 LLVMValueRef EltVal, LLVMValueRef Index,
2616 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2617 unwrap(Index), Name));
2620 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2621 LLVMValueRef V2, LLVMValueRef Mask,
2623 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2624 unwrap(Mask), Name));
2627 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2628 unsigned Index, const char *Name) {
2629 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2632 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2633 LLVMValueRef EltVal, unsigned Index,
2635 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2639 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2641 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2644 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2646 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2649 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2650 LLVMValueRef RHS, const char *Name) {
2651 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2654 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
2655 LLVMValueRef PTR, LLVMValueRef Val,
2656 LLVMAtomicOrdering ordering,
2657 LLVMBool singleThread) {
2658 AtomicRMWInst::BinOp intop;
2660 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
2661 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
2662 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
2663 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
2664 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
2665 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
2666 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
2667 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
2668 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
2669 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
2670 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
2672 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
2673 mapFromLLVMOrdering(ordering), singleThread ? SingleThread : CrossThread));
2677 /*===-- Module providers --------------------------------------------------===*/
2679 LLVMModuleProviderRef
2680 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2681 return reinterpret_cast<LLVMModuleProviderRef>(M);
2684 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2689 /*===-- Memory buffers ----------------------------------------------------===*/
2691 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2693 LLVMMemoryBufferRef *OutMemBuf,
2694 char **OutMessage) {
2696 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
2697 if (std::error_code EC = MBOrErr.getError()) {
2698 *OutMessage = strdup(EC.message().c_str());
2701 *OutMemBuf = wrap(MBOrErr.get().release());
2705 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2706 char **OutMessage) {
2707 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
2708 if (std::error_code EC = MBOrErr.getError()) {
2709 *OutMessage = strdup(EC.message().c_str());
2712 *OutMemBuf = wrap(MBOrErr.get().release());
2716 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
2717 const char *InputData,
2718 size_t InputDataLength,
2719 const char *BufferName,
2720 LLVMBool RequiresNullTerminator) {
2722 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
2723 StringRef(BufferName),
2724 RequiresNullTerminator).release());
2727 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
2728 const char *InputData,
2729 size_t InputDataLength,
2730 const char *BufferName) {
2733 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
2734 StringRef(BufferName)).release());
2737 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
2738 return unwrap(MemBuf)->getBufferStart();
2741 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
2742 return unwrap(MemBuf)->getBufferSize();
2745 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2746 delete unwrap(MemBuf);
2749 /*===-- Pass Registry -----------------------------------------------------===*/
2751 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2752 return wrap(PassRegistry::getPassRegistry());
2755 /*===-- Pass Manager ------------------------------------------------------===*/
2757 LLVMPassManagerRef LLVMCreatePassManager() {
2758 return wrap(new PassManager());
2761 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2762 return wrap(new FunctionPassManager(unwrap(M)));
2765 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2766 return LLVMCreateFunctionPassManagerForModule(
2767 reinterpret_cast<LLVMModuleRef>(P));
2770 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2771 return unwrap<PassManager>(PM)->run(*unwrap(M));
2774 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2775 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2778 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2779 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2782 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2783 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2786 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
2790 /*===-- Threading ------------------------------------------------------===*/
2792 LLVMBool LLVMStartMultithreaded() {
2793 return LLVMIsMultithreaded();
2796 void LLVMStopMultithreaded() {
2799 LLVMBool LLVMIsMultithreaded() {
2800 return llvm_is_multithreaded();