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/LegacyPassManager.h"
30 #include "llvm/IR/Module.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 LLVMInt128TypeInContext(LLVMContextRef C) {
314 return (LLVMTypeRef) Type::getInt128Ty(*unwrap(C));
316 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
317 return wrap(IntegerType::get(*unwrap(C), NumBits));
320 LLVMTypeRef LLVMInt1Type(void) {
321 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
323 LLVMTypeRef LLVMInt8Type(void) {
324 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
326 LLVMTypeRef LLVMInt16Type(void) {
327 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
329 LLVMTypeRef LLVMInt32Type(void) {
330 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
332 LLVMTypeRef LLVMInt64Type(void) {
333 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
335 LLVMTypeRef LLVMInt128Type(void) {
336 return LLVMInt128TypeInContext(LLVMGetGlobalContext());
338 LLVMTypeRef LLVMIntType(unsigned NumBits) {
339 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
342 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
343 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
346 /*--.. Operations on real types ............................................--*/
348 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
349 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
351 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
352 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
354 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
355 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
357 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
358 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
360 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
361 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
363 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
364 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
366 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
367 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
370 LLVMTypeRef LLVMHalfType(void) {
371 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
373 LLVMTypeRef LLVMFloatType(void) {
374 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
376 LLVMTypeRef LLVMDoubleType(void) {
377 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
379 LLVMTypeRef LLVMX86FP80Type(void) {
380 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
382 LLVMTypeRef LLVMFP128Type(void) {
383 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
385 LLVMTypeRef LLVMPPCFP128Type(void) {
386 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
388 LLVMTypeRef LLVMX86MMXType(void) {
389 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
392 /*--.. Operations on function types ........................................--*/
394 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
395 LLVMTypeRef *ParamTypes, unsigned ParamCount,
397 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
398 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
401 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
402 return unwrap<FunctionType>(FunctionTy)->isVarArg();
405 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
406 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
409 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
410 return unwrap<FunctionType>(FunctionTy)->getNumParams();
413 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
414 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
415 for (FunctionType::param_iterator I = Ty->param_begin(),
416 E = Ty->param_end(); I != E; ++I)
420 /*--.. Operations on struct types ..........................................--*/
422 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
423 unsigned ElementCount, LLVMBool Packed) {
424 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
425 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
428 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
429 unsigned ElementCount, LLVMBool Packed) {
430 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
431 ElementCount, Packed);
434 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
436 return wrap(StructType::create(*unwrap(C), Name));
439 const char *LLVMGetStructName(LLVMTypeRef Ty)
441 StructType *Type = unwrap<StructType>(Ty);
442 if (!Type->hasName())
444 return Type->getName().data();
447 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
448 unsigned ElementCount, LLVMBool Packed) {
449 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
450 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
453 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
454 return unwrap<StructType>(StructTy)->getNumElements();
457 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
458 StructType *Ty = unwrap<StructType>(StructTy);
459 for (StructType::element_iterator I = Ty->element_begin(),
460 E = Ty->element_end(); I != E; ++I)
464 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
465 return unwrap<StructType>(StructTy)->isPacked();
468 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
469 return unwrap<StructType>(StructTy)->isOpaque();
472 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
473 return wrap(unwrap(M)->getTypeByName(Name));
476 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
478 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
479 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
482 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
483 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
486 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
487 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
490 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
491 return wrap(unwrap<SequentialType>(Ty)->getElementType());
494 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
495 return unwrap<ArrayType>(ArrayTy)->getNumElements();
498 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
499 return unwrap<PointerType>(PointerTy)->getAddressSpace();
502 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
503 return unwrap<VectorType>(VectorTy)->getNumElements();
506 /*--.. Operations on other types ...........................................--*/
508 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
509 return wrap(Type::getVoidTy(*unwrap(C)));
511 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
512 return wrap(Type::getLabelTy(*unwrap(C)));
515 LLVMTypeRef LLVMVoidType(void) {
516 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
518 LLVMTypeRef LLVMLabelType(void) {
519 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
522 /*===-- Operations on values ----------------------------------------------===*/
524 /*--.. Operations on all values ............................................--*/
526 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
527 return wrap(unwrap(Val)->getType());
530 const char *LLVMGetValueName(LLVMValueRef Val) {
531 return unwrap(Val)->getName().data();
534 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
535 unwrap(Val)->setName(Name);
538 void LLVMDumpValue(LLVMValueRef Val) {
542 char* LLVMPrintValueToString(LLVMValueRef Val) {
544 raw_string_ostream os(buf);
547 unwrap(Val)->print(os);
549 os << "Printing <null> Value";
553 return strdup(buf.c_str());
556 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
557 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
560 int LLVMHasMetadata(LLVMValueRef Inst) {
561 return unwrap<Instruction>(Inst)->hasMetadata();
564 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
565 auto *I = unwrap<Instruction>(Inst);
566 assert(I && "Expected instruction");
567 if (auto *MD = I->getMetadata(KindID))
568 return wrap(MetadataAsValue::get(I->getContext(), MD));
572 // MetadataAsValue uses a canonical format which strips the actual MDNode for
573 // MDNode with just a single constant value, storing just a ConstantAsMetadata
574 // This undoes this canonicalization, reconstructing the MDNode.
575 static MDNode *extractMDNode(MetadataAsValue *MAV) {
576 Metadata *MD = MAV->getMetadata();
577 assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
578 "Expected a metadata node or a canonicalized constant");
580 if (MDNode *N = dyn_cast<MDNode>(MD))
583 return MDNode::get(MAV->getContext(), MD);
586 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
587 MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
589 unwrap<Instruction>(Inst)->setMetadata(KindID, N);
592 /*--.. Conversion functions ................................................--*/
594 #define LLVM_DEFINE_VALUE_CAST(name) \
595 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
596 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
599 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
601 LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) {
602 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
603 if (isa<MDNode>(MD->getMetadata()) ||
604 isa<ValueAsMetadata>(MD->getMetadata()))
609 LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) {
610 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
611 if (isa<MDString>(MD->getMetadata()))
616 /*--.. Operations on Uses ..................................................--*/
617 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
618 Value *V = unwrap(Val);
619 Value::use_iterator I = V->use_begin();
620 if (I == V->use_end())
625 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
626 Use *Next = unwrap(U)->getNext();
632 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
633 return wrap(unwrap(U)->getUser());
636 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
637 return wrap(unwrap(U)->get());
640 /*--.. Operations on Users .................................................--*/
642 static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N,
644 Metadata *Op = N->getOperand(Index);
647 if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
648 return wrap(C->getValue());
649 return wrap(MetadataAsValue::get(Context, Op));
652 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
653 Value *V = unwrap(Val);
654 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
655 if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
656 assert(Index == 0 && "Function-local metadata can only have one operand");
657 return wrap(L->getValue());
659 return getMDNodeOperandImpl(V->getContext(),
660 cast<MDNode>(MD->getMetadata()), Index);
663 return wrap(cast<User>(V)->getOperand(Index));
666 LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) {
667 Value *V = unwrap(Val);
668 return wrap(&cast<User>(V)->getOperandUse(Index));
671 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
672 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
675 int LLVMGetNumOperands(LLVMValueRef Val) {
676 Value *V = unwrap(Val);
677 if (isa<MetadataAsValue>(V))
678 return LLVMGetMDNodeNumOperands(Val);
680 return cast<User>(V)->getNumOperands();
683 /*--.. Operations on constants of any type .................................--*/
685 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
686 return wrap(Constant::getNullValue(unwrap(Ty)));
689 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
690 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
693 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
694 return wrap(UndefValue::get(unwrap(Ty)));
697 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
698 return isa<Constant>(unwrap(Ty));
701 LLVMBool LLVMIsNull(LLVMValueRef Val) {
702 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
703 return C->isNullValue();
707 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
708 return isa<UndefValue>(unwrap(Val));
711 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
713 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
716 /*--.. Operations on metadata nodes ........................................--*/
718 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
720 LLVMContext &Context = *unwrap(C);
721 return wrap(MetadataAsValue::get(
722 Context, MDString::get(Context, StringRef(Str, SLen))));
725 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
726 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
729 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
731 LLVMContext &Context = *unwrap(C);
732 SmallVector<Metadata *, 8> MDs;
733 for (auto *OV : makeArrayRef(Vals, Count)) {
734 Value *V = unwrap(OV);
738 else if (auto *C = dyn_cast<Constant>(V))
739 MD = ConstantAsMetadata::get(C);
740 else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
741 MD = MDV->getMetadata();
742 assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
743 "outside of direct argument to call");
745 // This is function-local metadata. Pretend to make an MDNode.
747 "Expected only one operand to function-local metadata");
748 return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V)));
753 return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs)));
756 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
757 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
760 const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
761 if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
762 if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
763 *Len = S->getString().size();
764 return S->getString().data();
770 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V)
772 auto *MD = cast<MetadataAsValue>(unwrap(V));
773 if (isa<ValueAsMetadata>(MD->getMetadata()))
775 return cast<MDNode>(MD->getMetadata())->getNumOperands();
778 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest)
780 auto *MD = cast<MetadataAsValue>(unwrap(V));
781 if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
782 *Dest = wrap(MDV->getValue());
785 const auto *N = cast<MDNode>(MD->getMetadata());
786 const unsigned numOperands = N->getNumOperands();
787 LLVMContext &Context = unwrap(V)->getContext();
788 for (unsigned i = 0; i < numOperands; i++)
789 Dest[i] = getMDNodeOperandImpl(Context, N, i);
792 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
794 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
795 return N->getNumOperands();
800 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
802 NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
805 LLVMContext &Context = unwrap(M)->getContext();
806 for (unsigned i=0;i<N->getNumOperands();i++)
807 Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
810 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
813 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
818 N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
821 /*--.. Operations on scalar constants ......................................--*/
823 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
824 LLVMBool SignExtend) {
825 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
828 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
830 const uint64_t Words[]) {
831 IntegerType *Ty = unwrap<IntegerType>(IntTy);
832 return wrap(ConstantInt::get(Ty->getContext(),
833 APInt(Ty->getBitWidth(),
834 makeArrayRef(Words, NumWords))));
837 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
839 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
843 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
844 unsigned SLen, uint8_t Radix) {
845 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
849 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
850 return wrap(ConstantFP::get(unwrap(RealTy), N));
853 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
854 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
857 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
859 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
862 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
863 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
866 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
867 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
870 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
871 ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
872 Type *Ty = cFP->getType();
874 if (Ty->isFloatTy()) {
876 return cFP->getValueAPF().convertToFloat();
879 if (Ty->isDoubleTy()) {
881 return cFP->getValueAPF().convertToDouble();
885 APFloat APF = cFP->getValueAPF();
886 APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &APFLosesInfo);
887 *LosesInfo = APFLosesInfo;
888 return APF.convertToDouble();
891 /*--.. Operations on composite constants ...................................--*/
893 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
895 LLVMBool DontNullTerminate) {
896 /* Inverted the sense of AddNull because ', 0)' is a
897 better mnemonic for null termination than ', 1)'. */
898 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
899 DontNullTerminate == 0));
901 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
902 LLVMValueRef *ConstantVals,
903 unsigned Count, LLVMBool Packed) {
904 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
905 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
909 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
910 LLVMBool DontNullTerminate) {
911 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
915 LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef c, unsigned idx) {
916 return wrap(static_cast<ConstantDataSequential*>(unwrap(c))->getElementAsConstant(idx));
919 LLVMBool LLVMIsConstantString(LLVMValueRef c) {
920 return static_cast<ConstantDataSequential*>(unwrap(c))->isString();
923 const char *LLVMGetAsString(LLVMValueRef c, size_t* Length) {
924 StringRef str = static_cast<ConstantDataSequential*>(unwrap(c))->getAsString();
925 *Length = str.size();
929 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
930 LLVMValueRef *ConstantVals, unsigned Length) {
931 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
932 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
935 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
937 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
941 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
942 LLVMValueRef *ConstantVals,
944 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
945 StructType *Ty = cast<StructType>(unwrap(StructTy));
947 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
950 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
951 return wrap(ConstantVector::get(makeArrayRef(
952 unwrap<Constant>(ScalarConstantVals, Size), Size)));
955 /*-- Opcode mapping */
957 static LLVMOpcode map_to_llvmopcode(int opcode)
960 default: llvm_unreachable("Unhandled Opcode.");
961 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
962 #include "llvm/IR/Instruction.def"
967 static int map_from_llvmopcode(LLVMOpcode code)
970 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
971 #include "llvm/IR/Instruction.def"
974 llvm_unreachable("Unhandled Opcode.");
977 /*--.. Constant expressions ................................................--*/
979 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
980 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
983 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
984 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
987 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
988 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
991 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
992 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
995 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
996 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
999 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
1000 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
1004 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
1005 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
1008 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
1009 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
1012 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1013 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
1014 unwrap<Constant>(RHSConstant)));
1017 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
1018 LLVMValueRef RHSConstant) {
1019 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
1020 unwrap<Constant>(RHSConstant)));
1023 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
1024 LLVMValueRef RHSConstant) {
1025 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
1026 unwrap<Constant>(RHSConstant)));
1029 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1030 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
1031 unwrap<Constant>(RHSConstant)));
1034 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1035 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
1036 unwrap<Constant>(RHSConstant)));
1039 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
1040 LLVMValueRef RHSConstant) {
1041 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
1042 unwrap<Constant>(RHSConstant)));
1045 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
1046 LLVMValueRef RHSConstant) {
1047 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
1048 unwrap<Constant>(RHSConstant)));
1051 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1052 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
1053 unwrap<Constant>(RHSConstant)));
1056 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1057 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
1058 unwrap<Constant>(RHSConstant)));
1061 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
1062 LLVMValueRef RHSConstant) {
1063 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
1064 unwrap<Constant>(RHSConstant)));
1067 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
1068 LLVMValueRef RHSConstant) {
1069 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
1070 unwrap<Constant>(RHSConstant)));
1073 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1074 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
1075 unwrap<Constant>(RHSConstant)));
1078 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1079 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
1080 unwrap<Constant>(RHSConstant)));
1083 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1084 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
1085 unwrap<Constant>(RHSConstant)));
1088 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
1089 LLVMValueRef RHSConstant) {
1090 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
1091 unwrap<Constant>(RHSConstant)));
1094 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1095 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
1096 unwrap<Constant>(RHSConstant)));
1099 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1100 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
1101 unwrap<Constant>(RHSConstant)));
1104 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1105 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
1106 unwrap<Constant>(RHSConstant)));
1109 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1110 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
1111 unwrap<Constant>(RHSConstant)));
1114 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1115 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
1116 unwrap<Constant>(RHSConstant)));
1119 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1120 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
1121 unwrap<Constant>(RHSConstant)));
1124 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1125 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
1126 unwrap<Constant>(RHSConstant)));
1129 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
1130 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1131 return wrap(ConstantExpr::getICmp(Predicate,
1132 unwrap<Constant>(LHSConstant),
1133 unwrap<Constant>(RHSConstant)));
1136 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
1137 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1138 return wrap(ConstantExpr::getFCmp(Predicate,
1139 unwrap<Constant>(LHSConstant),
1140 unwrap<Constant>(RHSConstant)));
1143 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1144 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1145 unwrap<Constant>(RHSConstant)));
1148 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1149 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
1150 unwrap<Constant>(RHSConstant)));
1153 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1154 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1155 unwrap<Constant>(RHSConstant)));
1158 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
1159 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1160 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1162 return wrap(ConstantExpr::getGetElementPtr(
1163 nullptr, unwrap<Constant>(ConstantVal), IdxList));
1166 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
1167 LLVMValueRef *ConstantIndices,
1168 unsigned NumIndices) {
1169 Constant* Val = unwrap<Constant>(ConstantVal);
1170 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1172 return wrap(ConstantExpr::getInBoundsGetElementPtr(nullptr, Val, IdxList));
1175 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1176 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1180 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1181 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1185 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1186 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1190 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1191 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1195 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1196 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1200 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1201 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1205 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1206 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1210 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1211 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1215 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1216 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1220 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1221 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1225 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1226 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1230 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1231 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1235 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1236 LLVMTypeRef ToType) {
1237 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1241 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1242 LLVMTypeRef ToType) {
1243 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1247 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1248 LLVMTypeRef ToType) {
1249 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1253 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1254 LLVMTypeRef ToType) {
1255 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1259 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1260 LLVMTypeRef ToType) {
1261 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1265 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1266 LLVMBool isSigned) {
1267 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1268 unwrap(ToType), isSigned));
1271 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1272 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1276 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1277 LLVMValueRef ConstantIfTrue,
1278 LLVMValueRef ConstantIfFalse) {
1279 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1280 unwrap<Constant>(ConstantIfTrue),
1281 unwrap<Constant>(ConstantIfFalse)));
1284 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1285 LLVMValueRef IndexConstant) {
1286 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1287 unwrap<Constant>(IndexConstant)));
1290 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1291 LLVMValueRef ElementValueConstant,
1292 LLVMValueRef IndexConstant) {
1293 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1294 unwrap<Constant>(ElementValueConstant),
1295 unwrap<Constant>(IndexConstant)));
1298 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1299 LLVMValueRef VectorBConstant,
1300 LLVMValueRef MaskConstant) {
1301 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1302 unwrap<Constant>(VectorBConstant),
1303 unwrap<Constant>(MaskConstant)));
1306 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1308 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1309 makeArrayRef(IdxList, NumIdx)));
1312 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1313 LLVMValueRef ElementValueConstant,
1314 unsigned *IdxList, unsigned NumIdx) {
1315 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1316 unwrap<Constant>(ElementValueConstant),
1317 makeArrayRef(IdxList, NumIdx)));
1320 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1321 const char *Constraints,
1322 LLVMBool HasSideEffects,
1323 LLVMBool IsAlignStack) {
1324 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1325 Constraints, HasSideEffects, IsAlignStack));
1328 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1329 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1332 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1334 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1335 return wrap(unwrap<GlobalValue>(Global)->getParent());
1338 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1339 return unwrap<GlobalValue>(Global)->isDeclaration();
1342 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1343 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1344 case GlobalValue::ExternalLinkage:
1345 return LLVMExternalLinkage;
1346 case GlobalValue::AvailableExternallyLinkage:
1347 return LLVMAvailableExternallyLinkage;
1348 case GlobalValue::LinkOnceAnyLinkage:
1349 return LLVMLinkOnceAnyLinkage;
1350 case GlobalValue::LinkOnceODRLinkage:
1351 return LLVMLinkOnceODRLinkage;
1352 case GlobalValue::WeakAnyLinkage:
1353 return LLVMWeakAnyLinkage;
1354 case GlobalValue::WeakODRLinkage:
1355 return LLVMWeakODRLinkage;
1356 case GlobalValue::AppendingLinkage:
1357 return LLVMAppendingLinkage;
1358 case GlobalValue::InternalLinkage:
1359 return LLVMInternalLinkage;
1360 case GlobalValue::PrivateLinkage:
1361 return LLVMPrivateLinkage;
1362 case GlobalValue::ExternalWeakLinkage:
1363 return LLVMExternalWeakLinkage;
1364 case GlobalValue::CommonLinkage:
1365 return LLVMCommonLinkage;
1368 llvm_unreachable("Invalid GlobalValue linkage!");
1371 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1372 GlobalValue *GV = unwrap<GlobalValue>(Global);
1375 case LLVMExternalLinkage:
1376 GV->setLinkage(GlobalValue::ExternalLinkage);
1378 case LLVMAvailableExternallyLinkage:
1379 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1381 case LLVMLinkOnceAnyLinkage:
1382 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1384 case LLVMLinkOnceODRLinkage:
1385 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1387 case LLVMLinkOnceODRAutoHideLinkage:
1388 DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1389 "longer supported.");
1391 case LLVMWeakAnyLinkage:
1392 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1394 case LLVMWeakODRLinkage:
1395 GV->setLinkage(GlobalValue::WeakODRLinkage);
1397 case LLVMAppendingLinkage:
1398 GV->setLinkage(GlobalValue::AppendingLinkage);
1400 case LLVMInternalLinkage:
1401 GV->setLinkage(GlobalValue::InternalLinkage);
1403 case LLVMPrivateLinkage:
1404 GV->setLinkage(GlobalValue::PrivateLinkage);
1406 case LLVMLinkerPrivateLinkage:
1407 GV->setLinkage(GlobalValue::PrivateLinkage);
1409 case LLVMLinkerPrivateWeakLinkage:
1410 GV->setLinkage(GlobalValue::PrivateLinkage);
1412 case LLVMDLLImportLinkage:
1414 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1416 case LLVMDLLExportLinkage:
1418 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1420 case LLVMExternalWeakLinkage:
1421 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1423 case LLVMGhostLinkage:
1425 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1427 case LLVMCommonLinkage:
1428 GV->setLinkage(GlobalValue::CommonLinkage);
1433 const char *LLVMGetSection(LLVMValueRef Global) {
1434 return unwrap<GlobalValue>(Global)->getSection();
1437 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1438 unwrap<GlobalObject>(Global)->setSection(Section);
1441 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1442 return static_cast<LLVMVisibility>(
1443 unwrap<GlobalValue>(Global)->getVisibility());
1446 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1447 unwrap<GlobalValue>(Global)
1448 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1451 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
1452 return static_cast<LLVMDLLStorageClass>(
1453 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1456 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
1457 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1458 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1461 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
1462 return unwrap<GlobalValue>(Global)->hasUnnamedAddr();
1465 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
1466 unwrap<GlobalValue>(Global)->setUnnamedAddr(HasUnnamedAddr);
1469 /*--.. Operations on global variables, load and store instructions .........--*/
1471 unsigned LLVMGetAlignment(LLVMValueRef V) {
1472 Value *P = unwrap<Value>(V);
1473 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1474 return GV->getAlignment();
1475 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1476 return AI->getAlignment();
1477 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1478 return LI->getAlignment();
1479 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1480 return SI->getAlignment();
1483 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1486 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1487 Value *P = unwrap<Value>(V);
1488 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
1489 GV->setAlignment(Bytes);
1490 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1491 AI->setAlignment(Bytes);
1492 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1493 LI->setAlignment(Bytes);
1494 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1495 SI->setAlignment(Bytes);
1498 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1501 /*--.. Operations on global variables ......................................--*/
1503 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1504 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1505 GlobalValue::ExternalLinkage, nullptr, Name));
1508 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1510 unsigned AddressSpace) {
1511 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1512 GlobalValue::ExternalLinkage, nullptr, Name,
1513 nullptr, GlobalVariable::NotThreadLocal,
1517 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1518 return wrap(unwrap(M)->getNamedGlobal(Name));
1521 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1522 Module *Mod = unwrap(M);
1523 Module::global_iterator I = Mod->global_begin();
1524 if (I == Mod->global_end())
1529 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1530 Module *Mod = unwrap(M);
1531 Module::global_iterator I = Mod->global_end();
1532 if (I == Mod->global_begin())
1537 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1538 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1539 Module::global_iterator I = GV;
1540 if (++I == GV->getParent()->global_end())
1545 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1546 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1547 Module::global_iterator I = GV;
1548 if (I == GV->getParent()->global_begin())
1553 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1554 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1557 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1558 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1559 if ( !GV->hasInitializer() )
1561 return wrap(GV->getInitializer());
1564 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1565 unwrap<GlobalVariable>(GlobalVar)
1566 ->setInitializer(unwrap<Constant>(ConstantVal));
1569 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1570 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1573 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1574 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1577 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1578 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1581 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1582 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1585 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1586 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1587 case GlobalVariable::NotThreadLocal:
1588 return LLVMNotThreadLocal;
1589 case GlobalVariable::GeneralDynamicTLSModel:
1590 return LLVMGeneralDynamicTLSModel;
1591 case GlobalVariable::LocalDynamicTLSModel:
1592 return LLVMLocalDynamicTLSModel;
1593 case GlobalVariable::InitialExecTLSModel:
1594 return LLVMInitialExecTLSModel;
1595 case GlobalVariable::LocalExecTLSModel:
1596 return LLVMLocalExecTLSModel;
1599 llvm_unreachable("Invalid GlobalVariable thread local mode");
1602 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1603 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1606 case LLVMNotThreadLocal:
1607 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1609 case LLVMGeneralDynamicTLSModel:
1610 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1612 case LLVMLocalDynamicTLSModel:
1613 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1615 case LLVMInitialExecTLSModel:
1616 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1618 case LLVMLocalExecTLSModel:
1619 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1624 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1625 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1628 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1629 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1632 /*--.. Operations on aliases ......................................--*/
1634 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1636 auto *PTy = cast<PointerType>(unwrap(Ty));
1637 return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
1638 GlobalValue::ExternalLinkage, Name,
1639 unwrap<Constant>(Aliasee), unwrap(M)));
1642 /*--.. Operations on functions .............................................--*/
1644 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1645 LLVMTypeRef FunctionTy) {
1646 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1647 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1650 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1651 return wrap(unwrap(M)->getFunction(Name));
1654 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1655 Module *Mod = unwrap(M);
1656 Module::iterator I = Mod->begin();
1657 if (I == Mod->end())
1662 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1663 Module *Mod = unwrap(M);
1664 Module::iterator I = Mod->end();
1665 if (I == Mod->begin())
1670 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1671 Function *Func = unwrap<Function>(Fn);
1672 Module::iterator I = Func;
1673 if (++I == Func->getParent()->end())
1678 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1679 Function *Func = unwrap<Function>(Fn);
1680 Module::iterator I = Func;
1681 if (I == Func->getParent()->begin())
1686 void LLVMDeleteFunction(LLVMValueRef Fn) {
1687 unwrap<Function>(Fn)->eraseFromParent();
1690 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1691 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1692 return F->getIntrinsicID();
1696 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1697 return unwrap<Function>(Fn)->getCallingConv();
1700 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1701 return unwrap<Function>(Fn)->setCallingConv(
1702 static_cast<CallingConv::ID>(CC));
1705 const char *LLVMGetGC(LLVMValueRef Fn) {
1706 Function *F = unwrap<Function>(Fn);
1707 return F->hasGC()? F->getGC() : nullptr;
1710 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1711 Function *F = unwrap<Function>(Fn);
1718 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1719 Function *Func = unwrap<Function>(Fn);
1720 const AttributeSet PAL = Func->getAttributes();
1722 const AttributeSet PALnew =
1723 PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1724 AttributeSet::get(Func->getContext(),
1725 AttributeSet::FunctionIndex, B));
1726 Func->setAttributes(PALnew);
1729 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1731 Function *Func = unwrap<Function>(Fn);
1732 AttributeSet::AttrIndex Idx =
1733 AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
1736 B.addAttribute(A, V);
1737 AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
1738 Func->addAttributes(Idx, Set);
1741 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1742 Function *Func = unwrap<Function>(Fn);
1743 const AttributeSet PAL = Func->getAttributes();
1745 const AttributeSet PALnew =
1746 PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1747 AttributeSet::get(Func->getContext(),
1748 AttributeSet::FunctionIndex, B));
1749 Func->setAttributes(PALnew);
1752 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1753 Function *Func = unwrap<Function>(Fn);
1754 const AttributeSet PAL = Func->getAttributes();
1755 return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
1758 /*--.. Operations on parameters ............................................--*/
1760 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1761 // This function is strictly redundant to
1762 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1763 return unwrap<Function>(FnRef)->arg_size();
1766 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1767 Function *Fn = unwrap<Function>(FnRef);
1768 for (Function::arg_iterator I = Fn->arg_begin(),
1769 E = Fn->arg_end(); I != E; I++)
1770 *ParamRefs++ = wrap(I);
1773 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1774 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1780 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1781 return wrap(unwrap<Argument>(V)->getParent());
1784 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1785 Function *Func = unwrap<Function>(Fn);
1786 Function::arg_iterator I = Func->arg_begin();
1787 if (I == Func->arg_end())
1792 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1793 Function *Func = unwrap<Function>(Fn);
1794 Function::arg_iterator I = Func->arg_end();
1795 if (I == Func->arg_begin())
1800 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1801 Argument *A = unwrap<Argument>(Arg);
1802 Function::arg_iterator I = A;
1803 if (++I == A->getParent()->arg_end())
1808 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1809 Argument *A = unwrap<Argument>(Arg);
1810 Function::arg_iterator I = A;
1811 if (I == A->getParent()->arg_begin())
1816 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1817 Argument *A = unwrap<Argument>(Arg);
1819 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1822 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1823 Argument *A = unwrap<Argument>(Arg);
1825 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1828 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1829 Argument *A = unwrap<Argument>(Arg);
1830 return (LLVMAttribute)A->getParent()->getAttributes().
1831 Raw(A->getArgNo()+1);
1835 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1836 Argument *A = unwrap<Argument>(Arg);
1838 B.addAlignmentAttr(align);
1839 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
1842 /*--.. Operations on basic blocks ..........................................--*/
1844 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1845 return wrap(static_cast<Value*>(unwrap(BB)));
1848 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1849 return isa<BasicBlock>(unwrap(Val));
1852 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1853 return wrap(unwrap<BasicBlock>(Val));
1856 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1857 return wrap(unwrap(BB)->getParent());
1860 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1861 return wrap(unwrap(BB)->getTerminator());
1864 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1865 return unwrap<Function>(FnRef)->size();
1868 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1869 Function *Fn = unwrap<Function>(FnRef);
1870 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1871 *BasicBlocksRefs++ = wrap(I);
1874 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1875 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1878 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1879 Function *Func = unwrap<Function>(Fn);
1880 Function::iterator I = Func->begin();
1881 if (I == Func->end())
1886 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1887 Function *Func = unwrap<Function>(Fn);
1888 Function::iterator I = Func->end();
1889 if (I == Func->begin())
1894 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1895 BasicBlock *Block = unwrap(BB);
1896 Function::iterator I = Block;
1897 if (++I == Block->getParent()->end())
1902 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1903 BasicBlock *Block = unwrap(BB);
1904 Function::iterator I = Block;
1905 if (I == Block->getParent()->begin())
1910 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1913 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1916 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1917 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1920 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1921 LLVMBasicBlockRef BBRef,
1923 BasicBlock *BB = unwrap(BBRef);
1924 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1927 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1929 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1932 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1933 unwrap(BBRef)->eraseFromParent();
1936 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1937 unwrap(BBRef)->removeFromParent();
1940 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1941 unwrap(BB)->moveBefore(unwrap(MovePos));
1944 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1945 unwrap(BB)->moveAfter(unwrap(MovePos));
1948 /*--.. Operations on instructions ..........................................--*/
1950 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1951 return wrap(unwrap<Instruction>(Inst)->getParent());
1954 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1955 BasicBlock *Block = unwrap(BB);
1956 BasicBlock::iterator I = Block->begin();
1957 if (I == Block->end())
1962 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1963 BasicBlock *Block = unwrap(BB);
1964 BasicBlock::iterator I = Block->end();
1965 if (I == Block->begin())
1970 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1971 Instruction *Instr = unwrap<Instruction>(Inst);
1972 BasicBlock::iterator I = Instr;
1973 if (++I == Instr->getParent()->end())
1978 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1979 Instruction *Instr = unwrap<Instruction>(Inst);
1980 BasicBlock::iterator I = Instr;
1981 if (I == Instr->getParent()->begin())
1986 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1987 unwrap<Instruction>(Inst)->eraseFromParent();
1990 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
1991 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
1992 return (LLVMIntPredicate)I->getPredicate();
1993 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1994 if (CE->getOpcode() == Instruction::ICmp)
1995 return (LLVMIntPredicate)CE->getPredicate();
1996 return (LLVMIntPredicate)0;
1999 LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
2000 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2001 return (LLVMRealPredicate)I->getPredicate();
2002 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2003 if (CE->getOpcode() == Instruction::FCmp)
2004 return (LLVMRealPredicate)CE->getPredicate();
2005 return (LLVMRealPredicate)0;
2008 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
2009 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2010 return map_to_llvmopcode(C->getOpcode());
2011 return (LLVMOpcode)0;
2014 LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {
2015 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2016 return wrap(C->clone());
2020 /*--.. Call and invoke instructions ........................................--*/
2022 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
2023 Value *V = unwrap(Instr);
2024 if (CallInst *CI = dyn_cast<CallInst>(V))
2025 return CI->getCallingConv();
2026 if (InvokeInst *II = dyn_cast<InvokeInst>(V))
2027 return II->getCallingConv();
2028 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
2031 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
2032 Value *V = unwrap(Instr);
2033 if (CallInst *CI = dyn_cast<CallInst>(V))
2034 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
2035 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
2036 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
2037 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
2040 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
2042 CallSite Call = CallSite(unwrap<Instruction>(Instr));
2045 Call.getAttributes().addAttributes(Call->getContext(), index,
2046 AttributeSet::get(Call->getContext(),
2050 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
2052 CallSite Call = CallSite(unwrap<Instruction>(Instr));
2054 Call.setAttributes(Call.getAttributes()
2055 .removeAttributes(Call->getContext(), index,
2056 AttributeSet::get(Call->getContext(),
2060 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
2062 CallSite Call = CallSite(unwrap<Instruction>(Instr));
2064 B.addAlignmentAttr(align);
2065 Call.setAttributes(Call.getAttributes()
2066 .addAttributes(Call->getContext(), index,
2067 AttributeSet::get(Call->getContext(),
2071 /*--.. Operations on call instructions (only) ..............................--*/
2073 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
2074 return unwrap<CallInst>(Call)->isTailCall();
2077 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
2078 unwrap<CallInst>(Call)->setTailCall(isTailCall);
2081 /*--.. Operations on terminators ...........................................--*/
2083 unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {
2084 return unwrap<TerminatorInst>(Term)->getNumSuccessors();
2087 LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) {
2088 return wrap(unwrap<TerminatorInst>(Term)->getSuccessor(i));
2091 void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
2092 return unwrap<TerminatorInst>(Term)->setSuccessor(i,unwrap(block));
2095 /*--.. Operations on branch instructions (only) ............................--*/
2097 LLVMBool LLVMIsConditional(LLVMValueRef Branch) {
2098 return unwrap<BranchInst>(Branch)->isConditional();
2101 LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) {
2102 return wrap(unwrap<BranchInst>(Branch)->getCondition());
2105 void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) {
2106 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2109 /*--.. Operations on switch instructions (only) ............................--*/
2111 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
2112 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2115 /*--.. Operations on phi nodes .............................................--*/
2117 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
2118 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
2119 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
2120 for (unsigned I = 0; I != Count; ++I)
2121 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
2124 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
2125 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
2128 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
2129 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
2132 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
2133 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
2137 /*===-- Instruction builders ----------------------------------------------===*/
2139 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
2140 return wrap(new IRBuilder<>(*unwrap(C)));
2143 LLVMBuilderRef LLVMCreateBuilder(void) {
2144 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
2147 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
2148 LLVMValueRef Instr) {
2149 BasicBlock *BB = unwrap(Block);
2150 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
2151 unwrap(Builder)->SetInsertPoint(BB, I);
2154 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2155 Instruction *I = unwrap<Instruction>(Instr);
2156 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
2159 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
2160 BasicBlock *BB = unwrap(Block);
2161 unwrap(Builder)->SetInsertPoint(BB);
2164 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
2165 return wrap(unwrap(Builder)->GetInsertBlock());
2168 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
2169 unwrap(Builder)->ClearInsertionPoint();
2172 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2173 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
2176 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
2178 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
2181 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
2182 delete unwrap(Builder);
2185 /*--.. Metadata builders ...................................................--*/
2187 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
2189 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
2190 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
2193 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
2194 LLVMContext &Context = unwrap(Builder)->getContext();
2195 return wrap(MetadataAsValue::get(
2196 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
2199 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
2200 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
2204 /*--.. Instruction builders ................................................--*/
2206 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
2207 return wrap(unwrap(B)->CreateRetVoid());
2210 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
2211 return wrap(unwrap(B)->CreateRet(unwrap(V)));
2214 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
2216 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
2219 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
2220 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
2223 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
2224 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
2225 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
2228 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
2229 LLVMBasicBlockRef Else, unsigned NumCases) {
2230 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
2233 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
2234 unsigned NumDests) {
2235 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
2238 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
2239 LLVMValueRef *Args, unsigned NumArgs,
2240 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
2242 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
2243 makeArrayRef(unwrap(Args), NumArgs),
2247 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
2248 LLVMValueRef PersFn, unsigned NumClauses,
2250 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
2251 cast<Function>(unwrap(PersFn)),
2255 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
2256 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
2259 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
2260 return wrap(unwrap(B)->CreateUnreachable());
2263 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
2264 LLVMBasicBlockRef Dest) {
2265 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
2268 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
2269 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
2272 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
2273 unwrap<LandingPadInst>(LandingPad)->
2274 addClause(cast<Constant>(unwrap(ClauseVal)));
2277 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
2278 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2281 /*--.. Arithmetic ..........................................................--*/
2283 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2285 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2288 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2290 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2293 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2295 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2298 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2300 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2303 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2305 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2308 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2310 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2313 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2315 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2318 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2320 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2323 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2325 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2328 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2330 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2333 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2335 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2338 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2340 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2343 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2345 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2348 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2350 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2353 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2354 LLVMValueRef RHS, const char *Name) {
2355 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2358 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2360 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2363 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2365 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2368 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2370 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2373 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2375 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2378 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2380 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2383 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2385 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2388 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2390 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2393 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2395 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2398 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2400 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2403 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2405 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2408 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2409 LLVMValueRef LHS, LLVMValueRef RHS,
2411 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2412 unwrap(RHS), Name));
2415 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2416 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2419 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2421 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2424 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2426 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2429 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2430 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2433 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2434 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2437 /*--.. Memory ..............................................................--*/
2439 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2441 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2442 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2443 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2444 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2445 ITy, unwrap(Ty), AllocSize,
2446 nullptr, nullptr, "");
2447 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2450 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2451 LLVMValueRef Val, const char *Name) {
2452 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2453 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2454 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2455 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2456 ITy, unwrap(Ty), AllocSize,
2457 unwrap(Val), nullptr, "");
2458 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2461 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2463 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
2466 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2467 LLVMValueRef Val, const char *Name) {
2468 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2471 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2472 return wrap(unwrap(B)->Insert(
2473 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2477 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2479 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2482 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2483 LLVMValueRef PointerVal) {
2484 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2487 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
2489 case LLVMAtomicOrderingNotAtomic: return NotAtomic;
2490 case LLVMAtomicOrderingUnordered: return Unordered;
2491 case LLVMAtomicOrderingMonotonic: return Monotonic;
2492 case LLVMAtomicOrderingAcquire: return Acquire;
2493 case LLVMAtomicOrderingRelease: return Release;
2494 case LLVMAtomicOrderingAcquireRelease: return AcquireRelease;
2495 case LLVMAtomicOrderingSequentiallyConsistent:
2496 return SequentiallyConsistent;
2499 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
2502 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
2503 LLVMBool isSingleThread, const char *Name) {
2505 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
2506 isSingleThread ? SingleThread : CrossThread,
2510 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2511 LLVMValueRef *Indices, unsigned NumIndices,
2513 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2514 return wrap(unwrap(B)->CreateGEP(nullptr, unwrap(Pointer), IdxList, Name));
2517 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2518 LLVMValueRef *Indices, unsigned NumIndices,
2520 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2522 unwrap(B)->CreateInBoundsGEP(nullptr, unwrap(Pointer), IdxList, Name));
2525 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2526 unsigned Idx, const char *Name) {
2527 return wrap(unwrap(B)->CreateStructGEP(nullptr, unwrap(Pointer), Idx, Name));
2530 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2532 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2535 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2537 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2540 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2541 Value *P = unwrap<Value>(MemAccessInst);
2542 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2543 return LI->isVolatile();
2544 return cast<StoreInst>(P)->isVolatile();
2547 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2548 Value *P = unwrap<Value>(MemAccessInst);
2549 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2550 return LI->setVolatile(isVolatile);
2551 return cast<StoreInst>(P)->setVolatile(isVolatile);
2554 /*--.. Casts ...............................................................--*/
2556 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2557 LLVMTypeRef DestTy, const char *Name) {
2558 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2561 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2562 LLVMTypeRef DestTy, const char *Name) {
2563 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2566 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2567 LLVMTypeRef DestTy, const char *Name) {
2568 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2571 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2572 LLVMTypeRef DestTy, const char *Name) {
2573 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2576 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2577 LLVMTypeRef DestTy, const char *Name) {
2578 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2581 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2582 LLVMTypeRef DestTy, const char *Name) {
2583 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2586 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2587 LLVMTypeRef DestTy, const char *Name) {
2588 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2591 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2592 LLVMTypeRef DestTy, const char *Name) {
2593 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2596 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2597 LLVMTypeRef DestTy, const char *Name) {
2598 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2601 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2602 LLVMTypeRef DestTy, const char *Name) {
2603 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2606 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2607 LLVMTypeRef DestTy, const char *Name) {
2608 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2611 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2612 LLVMTypeRef DestTy, const char *Name) {
2613 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2616 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
2617 LLVMTypeRef DestTy, const char *Name) {
2618 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
2621 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2622 LLVMTypeRef DestTy, const char *Name) {
2623 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2627 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2628 LLVMTypeRef DestTy, const char *Name) {
2629 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2633 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2634 LLVMTypeRef DestTy, const char *Name) {
2635 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2639 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2640 LLVMTypeRef DestTy, const char *Name) {
2641 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2642 unwrap(DestTy), Name));
2645 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2646 LLVMTypeRef DestTy, const char *Name) {
2647 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2650 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2651 LLVMTypeRef DestTy, const char *Name) {
2652 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2653 /*isSigned*/true, Name));
2656 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2657 LLVMTypeRef DestTy, const char *Name) {
2658 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2661 /*--.. Comparisons .........................................................--*/
2663 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2664 LLVMValueRef LHS, LLVMValueRef RHS,
2666 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2667 unwrap(LHS), unwrap(RHS), Name));
2670 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2671 LLVMValueRef LHS, LLVMValueRef RHS,
2673 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2674 unwrap(LHS), unwrap(RHS), Name));
2677 /*--.. Miscellaneous instructions ..........................................--*/
2679 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2680 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2683 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2684 LLVMValueRef *Args, unsigned NumArgs,
2686 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2687 makeArrayRef(unwrap(Args), NumArgs),
2691 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2692 LLVMValueRef Then, LLVMValueRef Else,
2694 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2698 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2699 LLVMTypeRef Ty, const char *Name) {
2700 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2703 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2704 LLVMValueRef Index, const char *Name) {
2705 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2709 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2710 LLVMValueRef EltVal, LLVMValueRef Index,
2712 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2713 unwrap(Index), Name));
2716 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2717 LLVMValueRef V2, LLVMValueRef Mask,
2719 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2720 unwrap(Mask), Name));
2723 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2724 unsigned Index, const char *Name) {
2725 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2728 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2729 LLVMValueRef EltVal, unsigned Index,
2731 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2735 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2737 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2740 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2742 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2745 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2746 LLVMValueRef RHS, const char *Name) {
2747 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2750 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
2751 LLVMValueRef PTR, LLVMValueRef Val,
2752 LLVMAtomicOrdering ordering,
2753 LLVMBool singleThread) {
2754 AtomicRMWInst::BinOp intop;
2756 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
2757 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
2758 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
2759 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
2760 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
2761 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
2762 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
2763 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
2764 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
2765 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
2766 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
2768 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
2769 mapFromLLVMOrdering(ordering), singleThread ? SingleThread : CrossThread));
2773 /*===-- Module providers --------------------------------------------------===*/
2775 LLVMModuleProviderRef
2776 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2777 return reinterpret_cast<LLVMModuleProviderRef>(M);
2780 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2785 /*===-- Memory buffers ----------------------------------------------------===*/
2787 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2789 LLVMMemoryBufferRef *OutMemBuf,
2790 char **OutMessage) {
2792 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
2793 if (std::error_code EC = MBOrErr.getError()) {
2794 *OutMessage = strdup(EC.message().c_str());
2797 *OutMemBuf = wrap(MBOrErr.get().release());
2801 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2802 char **OutMessage) {
2803 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
2804 if (std::error_code EC = MBOrErr.getError()) {
2805 *OutMessage = strdup(EC.message().c_str());
2808 *OutMemBuf = wrap(MBOrErr.get().release());
2812 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
2813 const char *InputData,
2814 size_t InputDataLength,
2815 const char *BufferName,
2816 LLVMBool RequiresNullTerminator) {
2818 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
2819 StringRef(BufferName),
2820 RequiresNullTerminator).release());
2823 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
2824 const char *InputData,
2825 size_t InputDataLength,
2826 const char *BufferName) {
2829 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
2830 StringRef(BufferName)).release());
2833 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
2834 return unwrap(MemBuf)->getBufferStart();
2837 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
2838 return unwrap(MemBuf)->getBufferSize();
2841 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2842 delete unwrap(MemBuf);
2845 /*===-- Pass Registry -----------------------------------------------------===*/
2847 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2848 return wrap(PassRegistry::getPassRegistry());
2851 /*===-- Pass Manager ------------------------------------------------------===*/
2853 LLVMPassManagerRef LLVMCreatePassManager() {
2854 return wrap(new legacy::PassManager());
2857 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2858 return wrap(new legacy::FunctionPassManager(unwrap(M)));
2861 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2862 return LLVMCreateFunctionPassManagerForModule(
2863 reinterpret_cast<LLVMModuleRef>(P));
2866 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2867 return unwrap<legacy::PassManager>(PM)->run(*unwrap(M));
2870 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2871 return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization();
2874 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2875 return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2878 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2879 return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization();
2882 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
2886 /*===-- Threading ------------------------------------------------------===*/
2888 LLVMBool LLVMStartMultithreaded() {
2889 return LLVMIsMultithreaded();
2892 void LLVMStopMultithreaded() {
2895 LLVMBool LLVMIsMultithreaded() {
2896 return llvm_is_multithreaded();