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 auto *I = unwrap<Instruction>(Inst);
560 assert(I && "Expected instruction");
561 if (auto *MD = I->getMetadata(KindID))
562 return wrap(MetadataAsValue::get(I->getContext(), MD));
566 // MetadataAsValue uses a canonical format which strips the actual MDNode for
567 // MDNode with just a single constant value, storing just a ConstantAsMetadata
568 // This undoes this canonicalization, reconstructing the MDNode.
569 static MDNode *extractMDNode(MetadataAsValue *MAV) {
570 Metadata *MD = MAV->getMetadata();
571 assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
572 "Expected a metadata node or a canonicalized constant");
574 if (MDNode *N = dyn_cast<MDNode>(MD))
577 return MDNode::get(MAV->getContext(), MD);
580 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
581 MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
583 unwrap<Instruction>(Inst)->setMetadata(KindID, N);
586 /*--.. Conversion functions ................................................--*/
588 #define LLVM_DEFINE_VALUE_CAST(name) \
589 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
590 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
593 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
595 LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) {
596 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
597 if (isa<MDNode>(MD->getMetadata()) ||
598 isa<ValueAsMetadata>(MD->getMetadata()))
603 LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) {
604 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
605 if (isa<MDString>(MD->getMetadata()))
610 /*--.. Operations on Uses ..................................................--*/
611 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
612 Value *V = unwrap(Val);
613 Value::use_iterator I = V->use_begin();
614 if (I == V->use_end())
619 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
620 Use *Next = unwrap(U)->getNext();
626 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
627 return wrap(unwrap(U)->getUser());
630 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
631 return wrap(unwrap(U)->get());
634 /*--.. Operations on Users .................................................--*/
636 static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N,
638 Metadata *Op = N->getOperand(Index);
641 if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
642 return wrap(C->getValue());
643 return wrap(MetadataAsValue::get(Context, Op));
646 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
647 Value *V = unwrap(Val);
648 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
649 if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
650 assert(Index == 0 && "Function-local metadata can only have one operand");
651 return wrap(L->getValue());
653 return getMDNodeOperandImpl(V->getContext(),
654 cast<MDNode>(MD->getMetadata()), Index);
657 return wrap(cast<User>(V)->getOperand(Index));
660 LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) {
661 Value *V = unwrap(Val);
662 return wrap(&cast<User>(V)->getOperandUse(Index));
665 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
666 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
669 int LLVMGetNumOperands(LLVMValueRef Val) {
670 Value *V = unwrap(Val);
671 if (isa<MetadataAsValue>(V))
672 return LLVMGetMDNodeNumOperands(Val);
674 return cast<User>(V)->getNumOperands();
677 /*--.. Operations on constants of any type .................................--*/
679 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
680 return wrap(Constant::getNullValue(unwrap(Ty)));
683 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
684 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
687 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
688 return wrap(UndefValue::get(unwrap(Ty)));
691 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
692 return isa<Constant>(unwrap(Ty));
695 LLVMBool LLVMIsNull(LLVMValueRef Val) {
696 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
697 return C->isNullValue();
701 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
702 return isa<UndefValue>(unwrap(Val));
705 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
707 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
710 /*--.. Operations on metadata nodes ........................................--*/
712 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
714 LLVMContext &Context = *unwrap(C);
715 return wrap(MetadataAsValue::get(
716 Context, MDString::get(Context, StringRef(Str, SLen))));
719 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
720 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
723 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
725 LLVMContext &Context = *unwrap(C);
726 SmallVector<Metadata *, 8> MDs;
727 for (auto *OV : makeArrayRef(Vals, Count)) {
728 Value *V = unwrap(OV);
732 else if (auto *C = dyn_cast<Constant>(V))
733 MD = ConstantAsMetadata::get(C);
734 else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
735 MD = MDV->getMetadata();
736 assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
737 "outside of direct argument to call");
739 // This is function-local metadata. Pretend to make an MDNode.
741 "Expected only one operand to function-local metadata");
742 return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V)));
747 return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs)));
750 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
751 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
754 const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
755 if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
756 if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
757 *Len = S->getString().size();
758 return S->getString().data();
764 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V)
766 auto *MD = cast<MetadataAsValue>(unwrap(V));
767 if (isa<ValueAsMetadata>(MD->getMetadata()))
769 return cast<MDNode>(MD->getMetadata())->getNumOperands();
772 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest)
774 auto *MD = cast<MetadataAsValue>(unwrap(V));
775 if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
776 *Dest = wrap(MDV->getValue());
779 const auto *N = cast<MDNode>(MD->getMetadata());
780 const unsigned numOperands = N->getNumOperands();
781 LLVMContext &Context = unwrap(V)->getContext();
782 for (unsigned i = 0; i < numOperands; i++)
783 Dest[i] = getMDNodeOperandImpl(Context, N, i);
786 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
788 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
789 return N->getNumOperands();
794 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
796 NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
799 LLVMContext &Context = unwrap(M)->getContext();
800 for (unsigned i=0;i<N->getNumOperands();i++)
801 Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
804 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
807 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
812 N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
815 /*--.. Operations on scalar constants ......................................--*/
817 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
818 LLVMBool SignExtend) {
819 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
822 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
824 const uint64_t Words[]) {
825 IntegerType *Ty = unwrap<IntegerType>(IntTy);
826 return wrap(ConstantInt::get(Ty->getContext(),
827 APInt(Ty->getBitWidth(),
828 makeArrayRef(Words, NumWords))));
831 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
833 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
837 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
838 unsigned SLen, uint8_t Radix) {
839 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
843 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
844 return wrap(ConstantFP::get(unwrap(RealTy), N));
847 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
848 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
851 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
853 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
856 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
857 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
860 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
861 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
864 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
865 ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
866 Type *Ty = cFP->getType();
868 if (Ty->isFloatTy()) {
870 return cFP->getValueAPF().convertToFloat();
873 if (Ty->isDoubleTy()) {
875 return cFP->getValueAPF().convertToDouble();
879 APFloat APF = cFP->getValueAPF();
880 APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &APFLosesInfo);
881 *LosesInfo = APFLosesInfo;
882 return APF.convertToDouble();
885 /*--.. Operations on composite constants ...................................--*/
887 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
889 LLVMBool DontNullTerminate) {
890 /* Inverted the sense of AddNull because ', 0)' is a
891 better mnemonic for null termination than ', 1)'. */
892 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
893 DontNullTerminate == 0));
895 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
896 LLVMValueRef *ConstantVals,
897 unsigned Count, LLVMBool Packed) {
898 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
899 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
903 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
904 LLVMBool DontNullTerminate) {
905 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
909 LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef c, unsigned idx) {
910 return wrap(static_cast<ConstantDataSequential*>(unwrap(c))->getElementAsConstant(idx));
913 LLVMBool LLVMIsConstantString(LLVMValueRef c) {
914 return static_cast<ConstantDataSequential*>(unwrap(c))->isString();
917 const char *LLVMGetAsString(LLVMValueRef c, size_t* Length) {
918 StringRef str = static_cast<ConstantDataSequential*>(unwrap(c))->getAsString();
919 *Length = str.size();
923 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
924 LLVMValueRef *ConstantVals, unsigned Length) {
925 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
926 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
929 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
931 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
935 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
936 LLVMValueRef *ConstantVals,
938 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
939 StructType *Ty = cast<StructType>(unwrap(StructTy));
941 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
944 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
945 return wrap(ConstantVector::get(makeArrayRef(
946 unwrap<Constant>(ScalarConstantVals, Size), Size)));
949 /*-- Opcode mapping */
951 static LLVMOpcode map_to_llvmopcode(int opcode)
954 default: llvm_unreachable("Unhandled Opcode.");
955 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
956 #include "llvm/IR/Instruction.def"
961 static int map_from_llvmopcode(LLVMOpcode code)
964 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
965 #include "llvm/IR/Instruction.def"
968 llvm_unreachable("Unhandled Opcode.");
971 /*--.. Constant expressions ................................................--*/
973 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
974 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
977 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
978 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
981 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
982 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
985 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
986 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
989 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
990 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
993 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
994 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
998 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
999 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
1002 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
1003 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
1006 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1007 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
1008 unwrap<Constant>(RHSConstant)));
1011 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
1012 LLVMValueRef RHSConstant) {
1013 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
1014 unwrap<Constant>(RHSConstant)));
1017 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
1018 LLVMValueRef RHSConstant) {
1019 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
1020 unwrap<Constant>(RHSConstant)));
1023 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1024 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
1025 unwrap<Constant>(RHSConstant)));
1028 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1029 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
1030 unwrap<Constant>(RHSConstant)));
1033 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
1034 LLVMValueRef RHSConstant) {
1035 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
1036 unwrap<Constant>(RHSConstant)));
1039 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
1040 LLVMValueRef RHSConstant) {
1041 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
1042 unwrap<Constant>(RHSConstant)));
1045 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1046 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
1047 unwrap<Constant>(RHSConstant)));
1050 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1051 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
1052 unwrap<Constant>(RHSConstant)));
1055 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
1056 LLVMValueRef RHSConstant) {
1057 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
1058 unwrap<Constant>(RHSConstant)));
1061 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
1062 LLVMValueRef RHSConstant) {
1063 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
1064 unwrap<Constant>(RHSConstant)));
1067 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1068 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
1069 unwrap<Constant>(RHSConstant)));
1072 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1073 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
1074 unwrap<Constant>(RHSConstant)));
1077 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1078 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
1079 unwrap<Constant>(RHSConstant)));
1082 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
1083 LLVMValueRef RHSConstant) {
1084 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
1085 unwrap<Constant>(RHSConstant)));
1088 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1089 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
1090 unwrap<Constant>(RHSConstant)));
1093 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1094 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
1095 unwrap<Constant>(RHSConstant)));
1098 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1099 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
1100 unwrap<Constant>(RHSConstant)));
1103 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1104 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
1105 unwrap<Constant>(RHSConstant)));
1108 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1109 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
1110 unwrap<Constant>(RHSConstant)));
1113 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1114 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
1115 unwrap<Constant>(RHSConstant)));
1118 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1119 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
1120 unwrap<Constant>(RHSConstant)));
1123 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
1124 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1125 return wrap(ConstantExpr::getICmp(Predicate,
1126 unwrap<Constant>(LHSConstant),
1127 unwrap<Constant>(RHSConstant)));
1130 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
1131 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1132 return wrap(ConstantExpr::getFCmp(Predicate,
1133 unwrap<Constant>(LHSConstant),
1134 unwrap<Constant>(RHSConstant)));
1137 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1138 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1139 unwrap<Constant>(RHSConstant)));
1142 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1143 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
1144 unwrap<Constant>(RHSConstant)));
1147 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1148 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1149 unwrap<Constant>(RHSConstant)));
1152 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
1153 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1154 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1156 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
1160 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
1161 LLVMValueRef *ConstantIndices,
1162 unsigned NumIndices) {
1163 Constant* Val = unwrap<Constant>(ConstantVal);
1164 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1166 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
1169 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1170 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1174 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1175 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1179 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1180 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1184 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1185 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1189 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1190 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1194 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1195 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1199 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1200 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1204 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1205 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1209 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1210 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1214 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1215 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1219 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1220 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1224 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1225 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1229 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1230 LLVMTypeRef ToType) {
1231 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1235 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1236 LLVMTypeRef ToType) {
1237 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1241 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1242 LLVMTypeRef ToType) {
1243 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1247 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1248 LLVMTypeRef ToType) {
1249 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1253 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1254 LLVMTypeRef ToType) {
1255 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1259 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1260 LLVMBool isSigned) {
1261 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1262 unwrap(ToType), isSigned));
1265 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1266 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1270 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1271 LLVMValueRef ConstantIfTrue,
1272 LLVMValueRef ConstantIfFalse) {
1273 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1274 unwrap<Constant>(ConstantIfTrue),
1275 unwrap<Constant>(ConstantIfFalse)));
1278 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1279 LLVMValueRef IndexConstant) {
1280 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1281 unwrap<Constant>(IndexConstant)));
1284 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1285 LLVMValueRef ElementValueConstant,
1286 LLVMValueRef IndexConstant) {
1287 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1288 unwrap<Constant>(ElementValueConstant),
1289 unwrap<Constant>(IndexConstant)));
1292 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1293 LLVMValueRef VectorBConstant,
1294 LLVMValueRef MaskConstant) {
1295 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1296 unwrap<Constant>(VectorBConstant),
1297 unwrap<Constant>(MaskConstant)));
1300 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1302 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1303 makeArrayRef(IdxList, NumIdx)));
1306 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1307 LLVMValueRef ElementValueConstant,
1308 unsigned *IdxList, unsigned NumIdx) {
1309 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1310 unwrap<Constant>(ElementValueConstant),
1311 makeArrayRef(IdxList, NumIdx)));
1314 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1315 const char *Constraints,
1316 LLVMBool HasSideEffects,
1317 LLVMBool IsAlignStack) {
1318 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1319 Constraints, HasSideEffects, IsAlignStack));
1322 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1323 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1326 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1328 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1329 return wrap(unwrap<GlobalValue>(Global)->getParent());
1332 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1333 return unwrap<GlobalValue>(Global)->isDeclaration();
1336 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1337 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1338 case GlobalValue::ExternalLinkage:
1339 return LLVMExternalLinkage;
1340 case GlobalValue::AvailableExternallyLinkage:
1341 return LLVMAvailableExternallyLinkage;
1342 case GlobalValue::LinkOnceAnyLinkage:
1343 return LLVMLinkOnceAnyLinkage;
1344 case GlobalValue::LinkOnceODRLinkage:
1345 return LLVMLinkOnceODRLinkage;
1346 case GlobalValue::WeakAnyLinkage:
1347 return LLVMWeakAnyLinkage;
1348 case GlobalValue::WeakODRLinkage:
1349 return LLVMWeakODRLinkage;
1350 case GlobalValue::AppendingLinkage:
1351 return LLVMAppendingLinkage;
1352 case GlobalValue::InternalLinkage:
1353 return LLVMInternalLinkage;
1354 case GlobalValue::PrivateLinkage:
1355 return LLVMPrivateLinkage;
1356 case GlobalValue::ExternalWeakLinkage:
1357 return LLVMExternalWeakLinkage;
1358 case GlobalValue::CommonLinkage:
1359 return LLVMCommonLinkage;
1362 llvm_unreachable("Invalid GlobalValue linkage!");
1365 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1366 GlobalValue *GV = unwrap<GlobalValue>(Global);
1369 case LLVMExternalLinkage:
1370 GV->setLinkage(GlobalValue::ExternalLinkage);
1372 case LLVMAvailableExternallyLinkage:
1373 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1375 case LLVMLinkOnceAnyLinkage:
1376 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1378 case LLVMLinkOnceODRLinkage:
1379 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1381 case LLVMLinkOnceODRAutoHideLinkage:
1382 DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1383 "longer supported.");
1385 case LLVMWeakAnyLinkage:
1386 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1388 case LLVMWeakODRLinkage:
1389 GV->setLinkage(GlobalValue::WeakODRLinkage);
1391 case LLVMAppendingLinkage:
1392 GV->setLinkage(GlobalValue::AppendingLinkage);
1394 case LLVMInternalLinkage:
1395 GV->setLinkage(GlobalValue::InternalLinkage);
1397 case LLVMPrivateLinkage:
1398 GV->setLinkage(GlobalValue::PrivateLinkage);
1400 case LLVMLinkerPrivateLinkage:
1401 GV->setLinkage(GlobalValue::PrivateLinkage);
1403 case LLVMLinkerPrivateWeakLinkage:
1404 GV->setLinkage(GlobalValue::PrivateLinkage);
1406 case LLVMDLLImportLinkage:
1408 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1410 case LLVMDLLExportLinkage:
1412 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1414 case LLVMExternalWeakLinkage:
1415 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1417 case LLVMGhostLinkage:
1419 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1421 case LLVMCommonLinkage:
1422 GV->setLinkage(GlobalValue::CommonLinkage);
1427 const char *LLVMGetSection(LLVMValueRef Global) {
1428 return unwrap<GlobalValue>(Global)->getSection();
1431 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1432 unwrap<GlobalObject>(Global)->setSection(Section);
1435 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1436 return static_cast<LLVMVisibility>(
1437 unwrap<GlobalValue>(Global)->getVisibility());
1440 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1441 unwrap<GlobalValue>(Global)
1442 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1445 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
1446 return static_cast<LLVMDLLStorageClass>(
1447 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1450 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
1451 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1452 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1455 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
1456 return unwrap<GlobalValue>(Global)->hasUnnamedAddr();
1459 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
1460 unwrap<GlobalValue>(Global)->setUnnamedAddr(HasUnnamedAddr);
1463 /*--.. Operations on global variables, load and store instructions .........--*/
1465 unsigned LLVMGetAlignment(LLVMValueRef V) {
1466 Value *P = unwrap<Value>(V);
1467 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1468 return GV->getAlignment();
1469 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1470 return AI->getAlignment();
1471 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1472 return LI->getAlignment();
1473 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1474 return SI->getAlignment();
1477 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1480 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1481 Value *P = unwrap<Value>(V);
1482 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
1483 GV->setAlignment(Bytes);
1484 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1485 AI->setAlignment(Bytes);
1486 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1487 LI->setAlignment(Bytes);
1488 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1489 SI->setAlignment(Bytes);
1492 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1495 /*--.. Operations on global variables ......................................--*/
1497 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1498 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1499 GlobalValue::ExternalLinkage, nullptr, Name));
1502 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1504 unsigned AddressSpace) {
1505 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1506 GlobalValue::ExternalLinkage, nullptr, Name,
1507 nullptr, GlobalVariable::NotThreadLocal,
1511 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1512 return wrap(unwrap(M)->getNamedGlobal(Name));
1515 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1516 Module *Mod = unwrap(M);
1517 Module::global_iterator I = Mod->global_begin();
1518 if (I == Mod->global_end())
1523 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1524 Module *Mod = unwrap(M);
1525 Module::global_iterator I = Mod->global_end();
1526 if (I == Mod->global_begin())
1531 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1532 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1533 Module::global_iterator I = GV;
1534 if (++I == GV->getParent()->global_end())
1539 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1540 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1541 Module::global_iterator I = GV;
1542 if (I == GV->getParent()->global_begin())
1547 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1548 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1551 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1552 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1553 if ( !GV->hasInitializer() )
1555 return wrap(GV->getInitializer());
1558 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1559 unwrap<GlobalVariable>(GlobalVar)
1560 ->setInitializer(unwrap<Constant>(ConstantVal));
1563 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1564 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1567 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1568 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1571 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1572 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1575 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1576 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1579 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1580 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1581 case GlobalVariable::NotThreadLocal:
1582 return LLVMNotThreadLocal;
1583 case GlobalVariable::GeneralDynamicTLSModel:
1584 return LLVMGeneralDynamicTLSModel;
1585 case GlobalVariable::LocalDynamicTLSModel:
1586 return LLVMLocalDynamicTLSModel;
1587 case GlobalVariable::InitialExecTLSModel:
1588 return LLVMInitialExecTLSModel;
1589 case GlobalVariable::LocalExecTLSModel:
1590 return LLVMLocalExecTLSModel;
1593 llvm_unreachable("Invalid GlobalVariable thread local mode");
1596 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1597 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1600 case LLVMNotThreadLocal:
1601 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1603 case LLVMGeneralDynamicTLSModel:
1604 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1606 case LLVMLocalDynamicTLSModel:
1607 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1609 case LLVMInitialExecTLSModel:
1610 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1612 case LLVMLocalExecTLSModel:
1613 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1618 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1619 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1622 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1623 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1626 /*--.. Operations on aliases ......................................--*/
1628 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1630 auto *PTy = cast<PointerType>(unwrap(Ty));
1631 return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
1632 GlobalValue::ExternalLinkage, Name,
1633 unwrap<GlobalObject>(Aliasee), unwrap(M)));
1636 /*--.. Operations on functions .............................................--*/
1638 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1639 LLVMTypeRef FunctionTy) {
1640 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1641 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1644 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1645 return wrap(unwrap(M)->getFunction(Name));
1648 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1649 Module *Mod = unwrap(M);
1650 Module::iterator I = Mod->begin();
1651 if (I == Mod->end())
1656 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1657 Module *Mod = unwrap(M);
1658 Module::iterator I = Mod->end();
1659 if (I == Mod->begin())
1664 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1665 Function *Func = unwrap<Function>(Fn);
1666 Module::iterator I = Func;
1667 if (++I == Func->getParent()->end())
1672 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1673 Function *Func = unwrap<Function>(Fn);
1674 Module::iterator I = Func;
1675 if (I == Func->getParent()->begin())
1680 void LLVMDeleteFunction(LLVMValueRef Fn) {
1681 unwrap<Function>(Fn)->eraseFromParent();
1684 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1685 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1686 return F->getIntrinsicID();
1690 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1691 return unwrap<Function>(Fn)->getCallingConv();
1694 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1695 return unwrap<Function>(Fn)->setCallingConv(
1696 static_cast<CallingConv::ID>(CC));
1699 const char *LLVMGetGC(LLVMValueRef Fn) {
1700 Function *F = unwrap<Function>(Fn);
1701 return F->hasGC()? F->getGC() : nullptr;
1704 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1705 Function *F = unwrap<Function>(Fn);
1712 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1713 Function *Func = unwrap<Function>(Fn);
1714 const AttributeSet PAL = Func->getAttributes();
1716 const AttributeSet PALnew =
1717 PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1718 AttributeSet::get(Func->getContext(),
1719 AttributeSet::FunctionIndex, B));
1720 Func->setAttributes(PALnew);
1723 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1725 Function *Func = unwrap<Function>(Fn);
1726 AttributeSet::AttrIndex Idx =
1727 AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
1730 B.addAttribute(A, V);
1731 AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
1732 Func->addAttributes(Idx, Set);
1735 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1736 Function *Func = unwrap<Function>(Fn);
1737 const AttributeSet PAL = Func->getAttributes();
1739 const AttributeSet PALnew =
1740 PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1741 AttributeSet::get(Func->getContext(),
1742 AttributeSet::FunctionIndex, B));
1743 Func->setAttributes(PALnew);
1746 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1747 Function *Func = unwrap<Function>(Fn);
1748 const AttributeSet PAL = Func->getAttributes();
1749 return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
1752 /*--.. Operations on parameters ............................................--*/
1754 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1755 // This function is strictly redundant to
1756 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1757 return unwrap<Function>(FnRef)->arg_size();
1760 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1761 Function *Fn = unwrap<Function>(FnRef);
1762 for (Function::arg_iterator I = Fn->arg_begin(),
1763 E = Fn->arg_end(); I != E; I++)
1764 *ParamRefs++ = wrap(I);
1767 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1768 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1774 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1775 return wrap(unwrap<Argument>(V)->getParent());
1778 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1779 Function *Func = unwrap<Function>(Fn);
1780 Function::arg_iterator I = Func->arg_begin();
1781 if (I == Func->arg_end())
1786 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1787 Function *Func = unwrap<Function>(Fn);
1788 Function::arg_iterator I = Func->arg_end();
1789 if (I == Func->arg_begin())
1794 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1795 Argument *A = unwrap<Argument>(Arg);
1796 Function::arg_iterator I = A;
1797 if (++I == A->getParent()->arg_end())
1802 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1803 Argument *A = unwrap<Argument>(Arg);
1804 Function::arg_iterator I = A;
1805 if (I == A->getParent()->arg_begin())
1810 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1811 Argument *A = unwrap<Argument>(Arg);
1813 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1816 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1817 Argument *A = unwrap<Argument>(Arg);
1819 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1822 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1823 Argument *A = unwrap<Argument>(Arg);
1824 return (LLVMAttribute)A->getParent()->getAttributes().
1825 Raw(A->getArgNo()+1);
1829 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1830 Argument *A = unwrap<Argument>(Arg);
1832 B.addAlignmentAttr(align);
1833 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
1836 /*--.. Operations on basic blocks ..........................................--*/
1838 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1839 return wrap(static_cast<Value*>(unwrap(BB)));
1842 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1843 return isa<BasicBlock>(unwrap(Val));
1846 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1847 return wrap(unwrap<BasicBlock>(Val));
1850 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1851 return wrap(unwrap(BB)->getParent());
1854 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1855 return wrap(unwrap(BB)->getTerminator());
1858 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1859 return unwrap<Function>(FnRef)->size();
1862 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1863 Function *Fn = unwrap<Function>(FnRef);
1864 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1865 *BasicBlocksRefs++ = wrap(I);
1868 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1869 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1872 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1873 Function *Func = unwrap<Function>(Fn);
1874 Function::iterator I = Func->begin();
1875 if (I == Func->end())
1880 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1881 Function *Func = unwrap<Function>(Fn);
1882 Function::iterator I = Func->end();
1883 if (I == Func->begin())
1888 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1889 BasicBlock *Block = unwrap(BB);
1890 Function::iterator I = Block;
1891 if (++I == Block->getParent()->end())
1896 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1897 BasicBlock *Block = unwrap(BB);
1898 Function::iterator I = Block;
1899 if (I == Block->getParent()->begin())
1904 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1907 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1910 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1911 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1914 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1915 LLVMBasicBlockRef BBRef,
1917 BasicBlock *BB = unwrap(BBRef);
1918 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1921 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1923 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1926 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1927 unwrap(BBRef)->eraseFromParent();
1930 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1931 unwrap(BBRef)->removeFromParent();
1934 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1935 unwrap(BB)->moveBefore(unwrap(MovePos));
1938 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1939 unwrap(BB)->moveAfter(unwrap(MovePos));
1942 /*--.. Operations on instructions ..........................................--*/
1944 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1945 return wrap(unwrap<Instruction>(Inst)->getParent());
1948 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1949 BasicBlock *Block = unwrap(BB);
1950 BasicBlock::iterator I = Block->begin();
1951 if (I == Block->end())
1956 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1957 BasicBlock *Block = unwrap(BB);
1958 BasicBlock::iterator I = Block->end();
1959 if (I == Block->begin())
1964 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1965 Instruction *Instr = unwrap<Instruction>(Inst);
1966 BasicBlock::iterator I = Instr;
1967 if (++I == Instr->getParent()->end())
1972 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1973 Instruction *Instr = unwrap<Instruction>(Inst);
1974 BasicBlock::iterator I = Instr;
1975 if (I == Instr->getParent()->begin())
1980 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1981 unwrap<Instruction>(Inst)->eraseFromParent();
1984 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
1985 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
1986 return (LLVMIntPredicate)I->getPredicate();
1987 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1988 if (CE->getOpcode() == Instruction::ICmp)
1989 return (LLVMIntPredicate)CE->getPredicate();
1990 return (LLVMIntPredicate)0;
1993 LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
1994 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
1995 return (LLVMRealPredicate)I->getPredicate();
1996 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1997 if (CE->getOpcode() == Instruction::FCmp)
1998 return (LLVMRealPredicate)CE->getPredicate();
1999 return (LLVMRealPredicate)0;
2002 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
2003 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2004 return map_to_llvmopcode(C->getOpcode());
2005 return (LLVMOpcode)0;
2008 LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {
2009 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2010 return wrap(C->clone());
2014 /*--.. Call and invoke instructions ........................................--*/
2016 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
2017 Value *V = unwrap(Instr);
2018 if (CallInst *CI = dyn_cast<CallInst>(V))
2019 return CI->getCallingConv();
2020 if (InvokeInst *II = dyn_cast<InvokeInst>(V))
2021 return II->getCallingConv();
2022 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
2025 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
2026 Value *V = unwrap(Instr);
2027 if (CallInst *CI = dyn_cast<CallInst>(V))
2028 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
2029 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
2030 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
2031 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
2034 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
2036 CallSite Call = CallSite(unwrap<Instruction>(Instr));
2039 Call.getAttributes().addAttributes(Call->getContext(), index,
2040 AttributeSet::get(Call->getContext(),
2044 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
2046 CallSite Call = CallSite(unwrap<Instruction>(Instr));
2048 Call.setAttributes(Call.getAttributes()
2049 .removeAttributes(Call->getContext(), index,
2050 AttributeSet::get(Call->getContext(),
2054 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
2056 CallSite Call = CallSite(unwrap<Instruction>(Instr));
2058 B.addAlignmentAttr(align);
2059 Call.setAttributes(Call.getAttributes()
2060 .addAttributes(Call->getContext(), index,
2061 AttributeSet::get(Call->getContext(),
2065 /*--.. Operations on call instructions (only) ..............................--*/
2067 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
2068 return unwrap<CallInst>(Call)->isTailCall();
2071 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
2072 unwrap<CallInst>(Call)->setTailCall(isTailCall);
2075 /*--.. Operations on terminators ...........................................--*/
2077 unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {
2078 return unwrap<TerminatorInst>(Term)->getNumSuccessors();
2081 LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) {
2082 return wrap(unwrap<TerminatorInst>(Term)->getSuccessor(i));
2085 void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
2086 return unwrap<TerminatorInst>(Term)->setSuccessor(i,unwrap(block));
2089 /*--.. Operations on branch instructions (only) ............................--*/
2091 LLVMBool LLVMIsConditional(LLVMValueRef Branch) {
2092 return unwrap<BranchInst>(Branch)->isConditional();
2095 LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) {
2096 return wrap(unwrap<BranchInst>(Branch)->getCondition());
2099 void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) {
2100 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2103 /*--.. Operations on switch instructions (only) ............................--*/
2105 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
2106 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2109 /*--.. Operations on phi nodes .............................................--*/
2111 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
2112 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
2113 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
2114 for (unsigned I = 0; I != Count; ++I)
2115 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
2118 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
2119 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
2122 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
2123 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
2126 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
2127 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
2131 /*===-- Instruction builders ----------------------------------------------===*/
2133 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
2134 return wrap(new IRBuilder<>(*unwrap(C)));
2137 LLVMBuilderRef LLVMCreateBuilder(void) {
2138 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
2141 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
2142 LLVMValueRef Instr) {
2143 BasicBlock *BB = unwrap(Block);
2144 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
2145 unwrap(Builder)->SetInsertPoint(BB, I);
2148 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2149 Instruction *I = unwrap<Instruction>(Instr);
2150 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
2153 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
2154 BasicBlock *BB = unwrap(Block);
2155 unwrap(Builder)->SetInsertPoint(BB);
2158 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
2159 return wrap(unwrap(Builder)->GetInsertBlock());
2162 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
2163 unwrap(Builder)->ClearInsertionPoint();
2166 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2167 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
2170 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
2172 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
2175 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
2176 delete unwrap(Builder);
2179 /*--.. Metadata builders ...................................................--*/
2181 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
2183 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
2184 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
2187 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
2188 LLVMContext &Context = unwrap(Builder)->getContext();
2189 return wrap(MetadataAsValue::get(
2191 unwrap(Builder)->getCurrentDebugLocation().getAsMDNode(Context)));
2194 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
2195 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
2199 /*--.. Instruction builders ................................................--*/
2201 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
2202 return wrap(unwrap(B)->CreateRetVoid());
2205 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
2206 return wrap(unwrap(B)->CreateRet(unwrap(V)));
2209 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
2211 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
2214 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
2215 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
2218 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
2219 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
2220 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
2223 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
2224 LLVMBasicBlockRef Else, unsigned NumCases) {
2225 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
2228 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
2229 unsigned NumDests) {
2230 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
2233 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
2234 LLVMValueRef *Args, unsigned NumArgs,
2235 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
2237 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
2238 makeArrayRef(unwrap(Args), NumArgs),
2242 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
2243 LLVMValueRef PersFn, unsigned NumClauses,
2245 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
2246 cast<Function>(unwrap(PersFn)),
2250 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
2251 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
2254 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
2255 return wrap(unwrap(B)->CreateUnreachable());
2258 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
2259 LLVMBasicBlockRef Dest) {
2260 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
2263 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
2264 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
2267 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
2268 unwrap<LandingPadInst>(LandingPad)->
2269 addClause(cast<Constant>(unwrap(ClauseVal)));
2272 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
2273 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2276 /*--.. Arithmetic ..........................................................--*/
2278 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2280 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2283 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2285 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2288 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2290 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2293 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2295 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2298 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2300 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2303 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2305 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2308 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2310 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2313 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2315 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2318 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2320 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2323 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2325 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2328 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2330 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2333 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2335 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2338 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2340 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2343 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2345 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2348 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2349 LLVMValueRef RHS, const char *Name) {
2350 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2353 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2355 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2358 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2360 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2363 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2365 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2368 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2370 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2373 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2375 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2378 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2380 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2383 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2385 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2388 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2390 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2393 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2395 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2398 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2400 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2403 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2404 LLVMValueRef LHS, LLVMValueRef RHS,
2406 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2407 unwrap(RHS), Name));
2410 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2411 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2414 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2416 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2419 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2421 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2424 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2425 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2428 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2429 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2432 /*--.. Memory ..............................................................--*/
2434 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2436 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2437 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2438 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2439 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2440 ITy, unwrap(Ty), AllocSize,
2441 nullptr, nullptr, "");
2442 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2445 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2446 LLVMValueRef Val, const char *Name) {
2447 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2448 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2449 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2450 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2451 ITy, unwrap(Ty), AllocSize,
2452 unwrap(Val), nullptr, "");
2453 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2456 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2458 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
2461 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2462 LLVMValueRef Val, const char *Name) {
2463 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2466 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2467 return wrap(unwrap(B)->Insert(
2468 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2472 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2474 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2477 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2478 LLVMValueRef PointerVal) {
2479 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2482 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
2484 case LLVMAtomicOrderingNotAtomic: return NotAtomic;
2485 case LLVMAtomicOrderingUnordered: return Unordered;
2486 case LLVMAtomicOrderingMonotonic: return Monotonic;
2487 case LLVMAtomicOrderingAcquire: return Acquire;
2488 case LLVMAtomicOrderingRelease: return Release;
2489 case LLVMAtomicOrderingAcquireRelease: return AcquireRelease;
2490 case LLVMAtomicOrderingSequentiallyConsistent:
2491 return SequentiallyConsistent;
2494 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
2497 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
2498 LLVMBool isSingleThread, const char *Name) {
2500 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
2501 isSingleThread ? SingleThread : CrossThread,
2505 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2506 LLVMValueRef *Indices, unsigned NumIndices,
2508 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2509 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
2512 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2513 LLVMValueRef *Indices, unsigned NumIndices,
2515 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2516 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
2519 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2520 unsigned Idx, const char *Name) {
2521 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2524 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2526 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2529 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2531 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2534 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2535 Value *P = unwrap<Value>(MemAccessInst);
2536 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2537 return LI->isVolatile();
2538 return cast<StoreInst>(P)->isVolatile();
2541 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2542 Value *P = unwrap<Value>(MemAccessInst);
2543 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2544 return LI->setVolatile(isVolatile);
2545 return cast<StoreInst>(P)->setVolatile(isVolatile);
2548 /*--.. Casts ...............................................................--*/
2550 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2551 LLVMTypeRef DestTy, const char *Name) {
2552 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2555 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2556 LLVMTypeRef DestTy, const char *Name) {
2557 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2560 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2561 LLVMTypeRef DestTy, const char *Name) {
2562 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2565 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2566 LLVMTypeRef DestTy, const char *Name) {
2567 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2570 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2571 LLVMTypeRef DestTy, const char *Name) {
2572 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2575 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2576 LLVMTypeRef DestTy, const char *Name) {
2577 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2580 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2581 LLVMTypeRef DestTy, const char *Name) {
2582 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2585 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2586 LLVMTypeRef DestTy, const char *Name) {
2587 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2590 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2591 LLVMTypeRef DestTy, const char *Name) {
2592 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2595 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2596 LLVMTypeRef DestTy, const char *Name) {
2597 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2600 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2601 LLVMTypeRef DestTy, const char *Name) {
2602 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2605 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2606 LLVMTypeRef DestTy, const char *Name) {
2607 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2610 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
2611 LLVMTypeRef DestTy, const char *Name) {
2612 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
2615 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2616 LLVMTypeRef DestTy, const char *Name) {
2617 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2621 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2622 LLVMTypeRef DestTy, const char *Name) {
2623 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2627 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2628 LLVMTypeRef DestTy, const char *Name) {
2629 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2633 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2634 LLVMTypeRef DestTy, const char *Name) {
2635 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2636 unwrap(DestTy), Name));
2639 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2640 LLVMTypeRef DestTy, const char *Name) {
2641 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2644 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2645 LLVMTypeRef DestTy, const char *Name) {
2646 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2647 /*isSigned*/true, Name));
2650 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2651 LLVMTypeRef DestTy, const char *Name) {
2652 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2655 /*--.. Comparisons .........................................................--*/
2657 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2658 LLVMValueRef LHS, LLVMValueRef RHS,
2660 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2661 unwrap(LHS), unwrap(RHS), Name));
2664 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2665 LLVMValueRef LHS, LLVMValueRef RHS,
2667 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2668 unwrap(LHS), unwrap(RHS), Name));
2671 /*--.. Miscellaneous instructions ..........................................--*/
2673 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2674 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2677 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2678 LLVMValueRef *Args, unsigned NumArgs,
2680 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2681 makeArrayRef(unwrap(Args), NumArgs),
2685 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2686 LLVMValueRef Then, LLVMValueRef Else,
2688 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2692 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2693 LLVMTypeRef Ty, const char *Name) {
2694 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2697 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2698 LLVMValueRef Index, const char *Name) {
2699 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2703 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2704 LLVMValueRef EltVal, LLVMValueRef Index,
2706 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2707 unwrap(Index), Name));
2710 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2711 LLVMValueRef V2, LLVMValueRef Mask,
2713 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2714 unwrap(Mask), Name));
2717 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2718 unsigned Index, const char *Name) {
2719 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2722 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2723 LLVMValueRef EltVal, unsigned Index,
2725 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2729 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2731 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2734 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2736 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2739 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2740 LLVMValueRef RHS, const char *Name) {
2741 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2744 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
2745 LLVMValueRef PTR, LLVMValueRef Val,
2746 LLVMAtomicOrdering ordering,
2747 LLVMBool singleThread) {
2748 AtomicRMWInst::BinOp intop;
2750 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
2751 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
2752 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
2753 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
2754 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
2755 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
2756 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
2757 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
2758 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
2759 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
2760 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
2762 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
2763 mapFromLLVMOrdering(ordering), singleThread ? SingleThread : CrossThread));
2767 /*===-- Module providers --------------------------------------------------===*/
2769 LLVMModuleProviderRef
2770 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2771 return reinterpret_cast<LLVMModuleProviderRef>(M);
2774 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2779 /*===-- Memory buffers ----------------------------------------------------===*/
2781 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2783 LLVMMemoryBufferRef *OutMemBuf,
2784 char **OutMessage) {
2786 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
2787 if (std::error_code EC = MBOrErr.getError()) {
2788 *OutMessage = strdup(EC.message().c_str());
2791 *OutMemBuf = wrap(MBOrErr.get().release());
2795 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2796 char **OutMessage) {
2797 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
2798 if (std::error_code EC = MBOrErr.getError()) {
2799 *OutMessage = strdup(EC.message().c_str());
2802 *OutMemBuf = wrap(MBOrErr.get().release());
2806 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
2807 const char *InputData,
2808 size_t InputDataLength,
2809 const char *BufferName,
2810 LLVMBool RequiresNullTerminator) {
2812 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
2813 StringRef(BufferName),
2814 RequiresNullTerminator).release());
2817 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
2818 const char *InputData,
2819 size_t InputDataLength,
2820 const char *BufferName) {
2823 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
2824 StringRef(BufferName)).release());
2827 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
2828 return unwrap(MemBuf)->getBufferStart();
2831 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
2832 return unwrap(MemBuf)->getBufferSize();
2835 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2836 delete unwrap(MemBuf);
2839 /*===-- Pass Registry -----------------------------------------------------===*/
2841 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2842 return wrap(PassRegistry::getPassRegistry());
2845 /*===-- Pass Manager ------------------------------------------------------===*/
2847 LLVMPassManagerRef LLVMCreatePassManager() {
2848 return wrap(new PassManager());
2851 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2852 return wrap(new FunctionPassManager(unwrap(M)));
2855 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2856 return LLVMCreateFunctionPassManagerForModule(
2857 reinterpret_cast<LLVMModuleRef>(P));
2860 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2861 return unwrap<PassManager>(PM)->run(*unwrap(M));
2864 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2865 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2868 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2869 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2872 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2873 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2876 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
2880 /*===-- Threading ------------------------------------------------------===*/
2882 LLVMBool LLVMStartMultithreaded() {
2883 return LLVMIsMultithreaded();
2886 void LLVMStopMultithreaded() {
2889 LLVMBool LLVMIsMultithreaded() {
2890 return llvm_is_multithreaded();