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/ADT/StringSwitch.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 initializeSafepointIRVerifierPass(Registry);
53 initializeVerifierLegacyPassPass(Registry);
56 void LLVMInitializeCore(LLVMPassRegistryRef R) {
57 initializeCore(*unwrap(R));
64 /*===-- Error handling ----------------------------------------------------===*/
66 char *LLVMCreateMessage(const char *Message) {
67 return strdup(Message);
70 void LLVMDisposeMessage(char *Message) {
75 /*===-- Operations on contexts --------------------------------------------===*/
77 static ManagedStatic<LLVMContext> GlobalContext;
79 LLVMContextRef LLVMContextCreate() {
80 return wrap(new LLVMContext());
83 LLVMContextRef LLVMGetGlobalContext() { return wrap(&*GlobalContext); }
85 void LLVMContextSetDiagnosticHandler(LLVMContextRef C,
86 LLVMDiagnosticHandler Handler,
87 void *DiagnosticContext) {
88 unwrap(C)->setDiagnosticHandlerCallBack(
89 LLVM_EXTENSION reinterpret_cast<DiagnosticHandler::DiagnosticHandlerTy>(
94 LLVMDiagnosticHandler LLVMContextGetDiagnosticHandler(LLVMContextRef C) {
95 return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>(
96 unwrap(C)->getDiagnosticHandlerCallBack());
99 void *LLVMContextGetDiagnosticContext(LLVMContextRef C) {
100 return unwrap(C)->getDiagnosticContext();
103 void LLVMContextSetYieldCallback(LLVMContextRef C, LLVMYieldCallback Callback,
104 void *OpaqueHandle) {
106 LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
107 unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
110 void LLVMContextDispose(LLVMContextRef C) {
114 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char *Name,
116 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
119 unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) {
120 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
123 #define GET_ATTR_KIND_FROM_NAME
124 #include "AttributesCompatFunc.inc"
126 unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) {
127 return getAttrKindFromName(StringRef(Name, SLen));
130 unsigned LLVMGetLastEnumAttributeKind(void) {
131 return Attribute::AttrKind::EndAttrKinds;
134 LLVMAttributeRef LLVMCreateEnumAttribute(LLVMContextRef C, unsigned KindID,
136 return wrap(Attribute::get(*unwrap(C), (Attribute::AttrKind)KindID, Val));
139 unsigned LLVMGetEnumAttributeKind(LLVMAttributeRef A) {
140 return unwrap(A).getKindAsEnum();
143 uint64_t LLVMGetEnumAttributeValue(LLVMAttributeRef A) {
144 auto Attr = unwrap(A);
145 if (Attr.isEnumAttribute())
147 return Attr.getValueAsInt();
150 LLVMAttributeRef LLVMCreateStringAttribute(LLVMContextRef C,
151 const char *K, unsigned KLength,
152 const char *V, unsigned VLength) {
153 return wrap(Attribute::get(*unwrap(C), StringRef(K, KLength),
154 StringRef(V, VLength)));
157 const char *LLVMGetStringAttributeKind(LLVMAttributeRef A,
159 auto S = unwrap(A).getKindAsString();
164 const char *LLVMGetStringAttributeValue(LLVMAttributeRef A,
166 auto S = unwrap(A).getValueAsString();
171 LLVMBool LLVMIsEnumAttribute(LLVMAttributeRef A) {
172 auto Attr = unwrap(A);
173 return Attr.isEnumAttribute() || Attr.isIntAttribute();
176 LLVMBool LLVMIsStringAttribute(LLVMAttributeRef A) {
177 return unwrap(A).isStringAttribute();
180 char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) {
181 std::string MsgStorage;
182 raw_string_ostream Stream(MsgStorage);
183 DiagnosticPrinterRawOStream DP(Stream);
185 unwrap(DI)->print(DP);
188 return LLVMCreateMessage(MsgStorage.c_str());
191 LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI) {
192 LLVMDiagnosticSeverity severity;
194 switch(unwrap(DI)->getSeverity()) {
196 severity = LLVMDSError;
199 severity = LLVMDSWarning;
202 severity = LLVMDSRemark;
205 severity = LLVMDSNote;
212 /*===-- Operations on modules ---------------------------------------------===*/
214 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
215 return wrap(new Module(ModuleID, *GlobalContext));
218 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
220 return wrap(new Module(ModuleID, *unwrap(C)));
223 void LLVMDisposeModule(LLVMModuleRef M) {
227 const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) {
228 auto &Str = unwrap(M)->getModuleIdentifier();
233 void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) {
234 unwrap(M)->setModuleIdentifier(StringRef(Ident, Len));
237 const char *LLVMGetSourceFileName(LLVMModuleRef M, size_t *Len) {
238 auto &Str = unwrap(M)->getSourceFileName();
243 void LLVMSetSourceFileName(LLVMModuleRef M, const char *Name, size_t Len) {
244 unwrap(M)->setSourceFileName(StringRef(Name, Len));
247 /*--.. Data layout .........................................................--*/
248 const char *LLVMGetDataLayoutStr(LLVMModuleRef M) {
249 return unwrap(M)->getDataLayoutStr().c_str();
252 const char *LLVMGetDataLayout(LLVMModuleRef M) {
253 return LLVMGetDataLayoutStr(M);
256 void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
257 unwrap(M)->setDataLayout(DataLayoutStr);
260 /*--.. Target triple .......................................................--*/
261 const char * LLVMGetTarget(LLVMModuleRef M) {
262 return unwrap(M)->getTargetTriple().c_str();
265 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
266 unwrap(M)->setTargetTriple(Triple);
269 void LLVMDumpModule(LLVMModuleRef M) {
270 unwrap(M)->print(errs(), nullptr,
271 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
274 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
275 char **ErrorMessage) {
277 raw_fd_ostream dest(Filename, EC, sys::fs::F_Text);
279 *ErrorMessage = strdup(EC.message().c_str());
283 unwrap(M)->print(dest, nullptr);
287 if (dest.has_error()) {
288 std::string E = "Error printing to file: " + dest.error().message();
289 *ErrorMessage = strdup(E.c_str());
296 char *LLVMPrintModuleToString(LLVMModuleRef M) {
298 raw_string_ostream os(buf);
300 unwrap(M)->print(os, nullptr);
303 return strdup(buf.c_str());
306 /*--.. Operations on inline assembler ......................................--*/
307 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
308 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
312 /*--.. Operations on module contexts ......................................--*/
313 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
314 return wrap(&unwrap(M)->getContext());
318 /*===-- Operations on types -----------------------------------------------===*/
320 /*--.. Operations on all types (mostly) ....................................--*/
322 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
323 switch (unwrap(Ty)->getTypeID()) {
325 return LLVMVoidTypeKind;
327 return LLVMHalfTypeKind;
328 case Type::FloatTyID:
329 return LLVMFloatTypeKind;
330 case Type::DoubleTyID:
331 return LLVMDoubleTypeKind;
332 case Type::X86_FP80TyID:
333 return LLVMX86_FP80TypeKind;
334 case Type::FP128TyID:
335 return LLVMFP128TypeKind;
336 case Type::PPC_FP128TyID:
337 return LLVMPPC_FP128TypeKind;
338 case Type::LabelTyID:
339 return LLVMLabelTypeKind;
340 case Type::MetadataTyID:
341 return LLVMMetadataTypeKind;
342 case Type::IntegerTyID:
343 return LLVMIntegerTypeKind;
344 case Type::FunctionTyID:
345 return LLVMFunctionTypeKind;
346 case Type::StructTyID:
347 return LLVMStructTypeKind;
348 case Type::ArrayTyID:
349 return LLVMArrayTypeKind;
350 case Type::PointerTyID:
351 return LLVMPointerTypeKind;
352 case Type::VectorTyID:
353 return LLVMVectorTypeKind;
354 case Type::X86_MMXTyID:
355 return LLVMX86_MMXTypeKind;
356 case Type::TokenTyID:
357 return LLVMTokenTypeKind;
359 llvm_unreachable("Unhandled TypeID.");
362 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
364 return unwrap(Ty)->isSized();
367 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
368 return wrap(&unwrap(Ty)->getContext());
371 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
372 LLVM_DUMP_METHOD void LLVMDumpType(LLVMTypeRef Ty) {
373 return unwrap(Ty)->dump();
377 char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
379 raw_string_ostream os(buf);
382 unwrap(Ty)->print(os);
384 os << "Printing <null> Type";
388 return strdup(buf.c_str());
391 /*--.. Operations on integer types .........................................--*/
393 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
394 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
396 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
397 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
399 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
400 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
402 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
403 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
405 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
406 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
408 LLVMTypeRef LLVMInt128TypeInContext(LLVMContextRef C) {
409 return (LLVMTypeRef) Type::getInt128Ty(*unwrap(C));
411 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
412 return wrap(IntegerType::get(*unwrap(C), NumBits));
415 LLVMTypeRef LLVMInt1Type(void) {
416 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
418 LLVMTypeRef LLVMInt8Type(void) {
419 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
421 LLVMTypeRef LLVMInt16Type(void) {
422 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
424 LLVMTypeRef LLVMInt32Type(void) {
425 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
427 LLVMTypeRef LLVMInt64Type(void) {
428 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
430 LLVMTypeRef LLVMInt128Type(void) {
431 return LLVMInt128TypeInContext(LLVMGetGlobalContext());
433 LLVMTypeRef LLVMIntType(unsigned NumBits) {
434 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
437 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
438 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
441 /*--.. Operations on real types ............................................--*/
443 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
444 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
446 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
447 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
449 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
450 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
452 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
453 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
455 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
456 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
458 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
459 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
461 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
462 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
465 LLVMTypeRef LLVMHalfType(void) {
466 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
468 LLVMTypeRef LLVMFloatType(void) {
469 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
471 LLVMTypeRef LLVMDoubleType(void) {
472 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
474 LLVMTypeRef LLVMX86FP80Type(void) {
475 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
477 LLVMTypeRef LLVMFP128Type(void) {
478 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
480 LLVMTypeRef LLVMPPCFP128Type(void) {
481 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
483 LLVMTypeRef LLVMX86MMXType(void) {
484 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
487 /*--.. Operations on function types ........................................--*/
489 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
490 LLVMTypeRef *ParamTypes, unsigned ParamCount,
492 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
493 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
496 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
497 return unwrap<FunctionType>(FunctionTy)->isVarArg();
500 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
501 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
504 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
505 return unwrap<FunctionType>(FunctionTy)->getNumParams();
508 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
509 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
510 for (FunctionType::param_iterator I = Ty->param_begin(),
511 E = Ty->param_end(); I != E; ++I)
515 /*--.. Operations on struct types ..........................................--*/
517 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
518 unsigned ElementCount, LLVMBool Packed) {
519 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
520 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
523 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
524 unsigned ElementCount, LLVMBool Packed) {
525 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
526 ElementCount, Packed);
529 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
531 return wrap(StructType::create(*unwrap(C), Name));
534 const char *LLVMGetStructName(LLVMTypeRef Ty)
536 StructType *Type = unwrap<StructType>(Ty);
537 if (!Type->hasName())
539 return Type->getName().data();
542 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
543 unsigned ElementCount, LLVMBool Packed) {
544 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
545 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
548 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
549 return unwrap<StructType>(StructTy)->getNumElements();
552 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
553 StructType *Ty = unwrap<StructType>(StructTy);
554 for (StructType::element_iterator I = Ty->element_begin(),
555 E = Ty->element_end(); I != E; ++I)
559 LLVMTypeRef LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy, unsigned i) {
560 StructType *Ty = unwrap<StructType>(StructTy);
561 return wrap(Ty->getTypeAtIndex(i));
564 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
565 return unwrap<StructType>(StructTy)->isPacked();
568 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
569 return unwrap<StructType>(StructTy)->isOpaque();
572 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
573 return wrap(unwrap(M)->getTypeByName(Name));
576 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
578 void LLVMGetSubtypes(LLVMTypeRef Tp, LLVMTypeRef *Arr) {
580 for (auto *T : unwrap(Tp)->subtypes()) {
586 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
587 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
590 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
591 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
594 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
595 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
598 LLVMTypeRef LLVMGetElementType(LLVMTypeRef WrappedTy) {
599 auto *Ty = unwrap<Type>(WrappedTy);
600 if (auto *PTy = dyn_cast<PointerType>(Ty))
601 return wrap(PTy->getElementType());
602 return wrap(cast<SequentialType>(Ty)->getElementType());
605 unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp) {
606 return unwrap(Tp)->getNumContainedTypes();
609 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
610 return unwrap<ArrayType>(ArrayTy)->getNumElements();
613 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
614 return unwrap<PointerType>(PointerTy)->getAddressSpace();
617 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
618 return unwrap<VectorType>(VectorTy)->getNumElements();
621 /*--.. Operations on other types ...........................................--*/
623 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
624 return wrap(Type::getVoidTy(*unwrap(C)));
626 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
627 return wrap(Type::getLabelTy(*unwrap(C)));
629 LLVMTypeRef LLVMTokenTypeInContext(LLVMContextRef C) {
630 return wrap(Type::getTokenTy(*unwrap(C)));
632 LLVMTypeRef LLVMMetadataTypeInContext(LLVMContextRef C) {
633 return wrap(Type::getMetadataTy(*unwrap(C)));
636 LLVMTypeRef LLVMVoidType(void) {
637 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
639 LLVMTypeRef LLVMLabelType(void) {
640 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
643 /*===-- Operations on values ----------------------------------------------===*/
645 /*--.. Operations on all values ............................................--*/
647 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
648 return wrap(unwrap(Val)->getType());
651 LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) {
652 switch(unwrap(Val)->getValueID()) {
653 #define HANDLE_VALUE(Name) \
654 case Value::Name##Val: \
655 return LLVM##Name##ValueKind;
656 #include "llvm/IR/Value.def"
658 return LLVMInstructionValueKind;
662 const char *LLVMGetValueName(LLVMValueRef Val) {
663 return unwrap(Val)->getName().data();
666 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
667 unwrap(Val)->setName(Name);
670 LLVM_DUMP_METHOD void LLVMDumpValue(LLVMValueRef Val) {
671 unwrap(Val)->print(errs(), /*IsForDebug=*/true);
674 char* LLVMPrintValueToString(LLVMValueRef Val) {
676 raw_string_ostream os(buf);
679 unwrap(Val)->print(os);
681 os << "Printing <null> Value";
685 return strdup(buf.c_str());
688 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
689 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
692 int LLVMHasMetadata(LLVMValueRef Inst) {
693 return unwrap<Instruction>(Inst)->hasMetadata();
696 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
697 auto *I = unwrap<Instruction>(Inst);
698 assert(I && "Expected instruction");
699 if (auto *MD = I->getMetadata(KindID))
700 return wrap(MetadataAsValue::get(I->getContext(), MD));
704 // MetadataAsValue uses a canonical format which strips the actual MDNode for
705 // MDNode with just a single constant value, storing just a ConstantAsMetadata
706 // This undoes this canonicalization, reconstructing the MDNode.
707 static MDNode *extractMDNode(MetadataAsValue *MAV) {
708 Metadata *MD = MAV->getMetadata();
709 assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
710 "Expected a metadata node or a canonicalized constant");
712 if (MDNode *N = dyn_cast<MDNode>(MD))
715 return MDNode::get(MAV->getContext(), MD);
718 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
719 MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
721 unwrap<Instruction>(Inst)->setMetadata(KindID, N);
724 /*--.. Conversion functions ................................................--*/
726 #define LLVM_DEFINE_VALUE_CAST(name) \
727 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
728 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
731 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
733 LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) {
734 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
735 if (isa<MDNode>(MD->getMetadata()) ||
736 isa<ValueAsMetadata>(MD->getMetadata()))
741 LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) {
742 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
743 if (isa<MDString>(MD->getMetadata()))
748 /*--.. Operations on Uses ..................................................--*/
749 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
750 Value *V = unwrap(Val);
751 Value::use_iterator I = V->use_begin();
752 if (I == V->use_end())
757 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
758 Use *Next = unwrap(U)->getNext();
764 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
765 return wrap(unwrap(U)->getUser());
768 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
769 return wrap(unwrap(U)->get());
772 /*--.. Operations on Users .................................................--*/
774 static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N,
776 Metadata *Op = N->getOperand(Index);
779 if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
780 return wrap(C->getValue());
781 return wrap(MetadataAsValue::get(Context, Op));
784 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
785 Value *V = unwrap(Val);
786 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
787 if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
788 assert(Index == 0 && "Function-local metadata can only have one operand");
789 return wrap(L->getValue());
791 return getMDNodeOperandImpl(V->getContext(),
792 cast<MDNode>(MD->getMetadata()), Index);
795 return wrap(cast<User>(V)->getOperand(Index));
798 LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) {
799 Value *V = unwrap(Val);
800 return wrap(&cast<User>(V)->getOperandUse(Index));
803 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
804 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
807 int LLVMGetNumOperands(LLVMValueRef Val) {
808 Value *V = unwrap(Val);
809 if (isa<MetadataAsValue>(V))
810 return LLVMGetMDNodeNumOperands(Val);
812 return cast<User>(V)->getNumOperands();
815 /*--.. Operations on constants of any type .................................--*/
817 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
818 return wrap(Constant::getNullValue(unwrap(Ty)));
821 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
822 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
825 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
826 return wrap(UndefValue::get(unwrap(Ty)));
829 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
830 return isa<Constant>(unwrap(Ty));
833 LLVMBool LLVMIsNull(LLVMValueRef Val) {
834 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
835 return C->isNullValue();
839 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
840 return isa<UndefValue>(unwrap(Val));
843 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
844 return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
847 /*--.. Operations on metadata nodes ........................................--*/
849 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
851 LLVMContext &Context = *unwrap(C);
852 return wrap(MetadataAsValue::get(
853 Context, MDString::get(Context, StringRef(Str, SLen))));
856 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
857 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
860 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
862 LLVMContext &Context = *unwrap(C);
863 SmallVector<Metadata *, 8> MDs;
864 for (auto *OV : makeArrayRef(Vals, Count)) {
865 Value *V = unwrap(OV);
869 else if (auto *C = dyn_cast<Constant>(V))
870 MD = ConstantAsMetadata::get(C);
871 else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
872 MD = MDV->getMetadata();
873 assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
874 "outside of direct argument to call");
876 // This is function-local metadata. Pretend to make an MDNode.
878 "Expected only one operand to function-local metadata");
879 return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V)));
884 return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs)));
887 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
888 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
891 LLVMValueRef LLVMMetadataAsValue(LLVMContextRef C, LLVMMetadataRef MD) {
892 return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
895 LLVMMetadataRef LLVMValueAsMetadata(LLVMValueRef Val) {
896 auto *V = unwrap(Val);
897 if (auto *C = dyn_cast<Constant>(V))
898 return wrap(ConstantAsMetadata::get(C));
899 if (auto *MAV = dyn_cast<MetadataAsValue>(V))
900 return wrap(MAV->getMetadata());
901 return wrap(ValueAsMetadata::get(V));
904 const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
905 if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
906 if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
907 *Length = S->getString().size();
908 return S->getString().data();
914 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) {
915 auto *MD = cast<MetadataAsValue>(unwrap(V));
916 if (isa<ValueAsMetadata>(MD->getMetadata()))
918 return cast<MDNode>(MD->getMetadata())->getNumOperands();
921 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) {
922 auto *MD = cast<MetadataAsValue>(unwrap(V));
923 if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
924 *Dest = wrap(MDV->getValue());
927 const auto *N = cast<MDNode>(MD->getMetadata());
928 const unsigned numOperands = N->getNumOperands();
929 LLVMContext &Context = unwrap(V)->getContext();
930 for (unsigned i = 0; i < numOperands; i++)
931 Dest[i] = getMDNodeOperandImpl(Context, N, i);
934 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char *Name) {
935 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
936 return N->getNumOperands();
941 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char *Name,
942 LLVMValueRef *Dest) {
943 NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
946 LLVMContext &Context = unwrap(M)->getContext();
947 for (unsigned i=0;i<N->getNumOperands();i++)
948 Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
951 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char *Name,
953 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
958 N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
961 /*--.. Operations on scalar constants ......................................--*/
963 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
964 LLVMBool SignExtend) {
965 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
968 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
970 const uint64_t Words[]) {
971 IntegerType *Ty = unwrap<IntegerType>(IntTy);
972 return wrap(ConstantInt::get(Ty->getContext(),
973 APInt(Ty->getBitWidth(),
974 makeArrayRef(Words, NumWords))));
977 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
979 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
983 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
984 unsigned SLen, uint8_t Radix) {
985 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
989 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
990 return wrap(ConstantFP::get(unwrap(RealTy), N));
993 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
994 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
997 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
999 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
1002 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
1003 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
1006 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
1007 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
1010 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
1011 ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
1012 Type *Ty = cFP->getType();
1014 if (Ty->isFloatTy()) {
1016 return cFP->getValueAPF().convertToFloat();
1019 if (Ty->isDoubleTy()) {
1021 return cFP->getValueAPF().convertToDouble();
1025 APFloat APF = cFP->getValueAPF();
1026 APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo);
1027 *LosesInfo = APFLosesInfo;
1028 return APF.convertToDouble();
1031 /*--.. Operations on composite constants ...................................--*/
1033 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
1035 LLVMBool DontNullTerminate) {
1036 /* Inverted the sense of AddNull because ', 0)' is a
1037 better mnemonic for null termination than ', 1)'. */
1038 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
1039 DontNullTerminate == 0));
1042 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
1043 LLVMBool DontNullTerminate) {
1044 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
1048 LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef C, unsigned idx) {
1049 return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
1052 LLVMBool LLVMIsConstantString(LLVMValueRef C) {
1053 return unwrap<ConstantDataSequential>(C)->isString();
1056 const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1057 StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1058 *Length = Str.size();
1062 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
1063 LLVMValueRef *ConstantVals, unsigned Length) {
1064 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1065 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1068 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
1069 LLVMValueRef *ConstantVals,
1070 unsigned Count, LLVMBool Packed) {
1071 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1072 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
1076 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
1078 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1082 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
1083 LLVMValueRef *ConstantVals,
1085 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1086 StructType *Ty = cast<StructType>(unwrap(StructTy));
1088 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
1091 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
1092 return wrap(ConstantVector::get(makeArrayRef(
1093 unwrap<Constant>(ScalarConstantVals, Size), Size)));
1096 /*-- Opcode mapping */
1098 static LLVMOpcode map_to_llvmopcode(int opcode)
1101 default: llvm_unreachable("Unhandled Opcode.");
1102 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
1103 #include "llvm/IR/Instruction.def"
1108 static int map_from_llvmopcode(LLVMOpcode code)
1111 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
1112 #include "llvm/IR/Instruction.def"
1115 llvm_unreachable("Unhandled Opcode.");
1118 /*--.. Constant expressions ................................................--*/
1120 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
1121 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
1124 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
1125 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
1128 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
1129 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
1132 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
1133 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1136 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
1137 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
1140 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
1141 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
1145 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
1146 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
1149 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
1150 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
1153 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1154 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
1155 unwrap<Constant>(RHSConstant)));
1158 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
1159 LLVMValueRef RHSConstant) {
1160 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
1161 unwrap<Constant>(RHSConstant)));
1164 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
1165 LLVMValueRef RHSConstant) {
1166 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
1167 unwrap<Constant>(RHSConstant)));
1170 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1171 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
1172 unwrap<Constant>(RHSConstant)));
1175 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1176 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
1177 unwrap<Constant>(RHSConstant)));
1180 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
1181 LLVMValueRef RHSConstant) {
1182 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
1183 unwrap<Constant>(RHSConstant)));
1186 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
1187 LLVMValueRef RHSConstant) {
1188 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
1189 unwrap<Constant>(RHSConstant)));
1192 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1193 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
1194 unwrap<Constant>(RHSConstant)));
1197 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1198 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
1199 unwrap<Constant>(RHSConstant)));
1202 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
1203 LLVMValueRef RHSConstant) {
1204 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
1205 unwrap<Constant>(RHSConstant)));
1208 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
1209 LLVMValueRef RHSConstant) {
1210 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
1211 unwrap<Constant>(RHSConstant)));
1214 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1215 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
1216 unwrap<Constant>(RHSConstant)));
1219 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1220 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
1221 unwrap<Constant>(RHSConstant)));
1224 LLVMValueRef LLVMConstExactUDiv(LLVMValueRef LHSConstant,
1225 LLVMValueRef RHSConstant) {
1226 return wrap(ConstantExpr::getExactUDiv(unwrap<Constant>(LHSConstant),
1227 unwrap<Constant>(RHSConstant)));
1230 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1231 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
1232 unwrap<Constant>(RHSConstant)));
1235 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
1236 LLVMValueRef RHSConstant) {
1237 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
1238 unwrap<Constant>(RHSConstant)));
1241 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1242 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
1243 unwrap<Constant>(RHSConstant)));
1246 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1247 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
1248 unwrap<Constant>(RHSConstant)));
1251 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1252 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
1253 unwrap<Constant>(RHSConstant)));
1256 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1257 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
1258 unwrap<Constant>(RHSConstant)));
1261 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1262 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
1263 unwrap<Constant>(RHSConstant)));
1266 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1267 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
1268 unwrap<Constant>(RHSConstant)));
1271 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1272 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
1273 unwrap<Constant>(RHSConstant)));
1276 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
1277 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1278 return wrap(ConstantExpr::getICmp(Predicate,
1279 unwrap<Constant>(LHSConstant),
1280 unwrap<Constant>(RHSConstant)));
1283 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
1284 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1285 return wrap(ConstantExpr::getFCmp(Predicate,
1286 unwrap<Constant>(LHSConstant),
1287 unwrap<Constant>(RHSConstant)));
1290 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1291 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1292 unwrap<Constant>(RHSConstant)));
1295 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1296 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
1297 unwrap<Constant>(RHSConstant)));
1300 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1301 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1302 unwrap<Constant>(RHSConstant)));
1305 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
1306 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1307 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1309 return wrap(ConstantExpr::getGetElementPtr(
1310 nullptr, unwrap<Constant>(ConstantVal), IdxList));
1313 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
1314 LLVMValueRef *ConstantIndices,
1315 unsigned NumIndices) {
1316 Constant* Val = unwrap<Constant>(ConstantVal);
1317 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1319 return wrap(ConstantExpr::getInBoundsGetElementPtr(nullptr, Val, IdxList));
1322 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1323 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1327 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1328 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1332 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1333 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1337 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1338 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1342 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1343 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1347 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1348 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1352 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1353 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1357 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1358 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1362 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1363 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1367 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1368 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1372 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1373 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1377 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1378 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1382 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1383 LLVMTypeRef ToType) {
1384 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1388 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1389 LLVMTypeRef ToType) {
1390 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1394 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1395 LLVMTypeRef ToType) {
1396 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1400 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1401 LLVMTypeRef ToType) {
1402 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1406 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1407 LLVMTypeRef ToType) {
1408 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1412 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1413 LLVMBool isSigned) {
1414 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1415 unwrap(ToType), isSigned));
1418 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1419 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1423 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1424 LLVMValueRef ConstantIfTrue,
1425 LLVMValueRef ConstantIfFalse) {
1426 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1427 unwrap<Constant>(ConstantIfTrue),
1428 unwrap<Constant>(ConstantIfFalse)));
1431 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1432 LLVMValueRef IndexConstant) {
1433 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1434 unwrap<Constant>(IndexConstant)));
1437 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1438 LLVMValueRef ElementValueConstant,
1439 LLVMValueRef IndexConstant) {
1440 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1441 unwrap<Constant>(ElementValueConstant),
1442 unwrap<Constant>(IndexConstant)));
1445 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1446 LLVMValueRef VectorBConstant,
1447 LLVMValueRef MaskConstant) {
1448 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1449 unwrap<Constant>(VectorBConstant),
1450 unwrap<Constant>(MaskConstant)));
1453 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1455 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1456 makeArrayRef(IdxList, NumIdx)));
1459 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1460 LLVMValueRef ElementValueConstant,
1461 unsigned *IdxList, unsigned NumIdx) {
1462 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1463 unwrap<Constant>(ElementValueConstant),
1464 makeArrayRef(IdxList, NumIdx)));
1467 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1468 const char *Constraints,
1469 LLVMBool HasSideEffects,
1470 LLVMBool IsAlignStack) {
1471 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1472 Constraints, HasSideEffects, IsAlignStack));
1475 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1476 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1479 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1481 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1482 return wrap(unwrap<GlobalValue>(Global)->getParent());
1485 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1486 return unwrap<GlobalValue>(Global)->isDeclaration();
1489 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1490 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1491 case GlobalValue::ExternalLinkage:
1492 return LLVMExternalLinkage;
1493 case GlobalValue::AvailableExternallyLinkage:
1494 return LLVMAvailableExternallyLinkage;
1495 case GlobalValue::LinkOnceAnyLinkage:
1496 return LLVMLinkOnceAnyLinkage;
1497 case GlobalValue::LinkOnceODRLinkage:
1498 return LLVMLinkOnceODRLinkage;
1499 case GlobalValue::WeakAnyLinkage:
1500 return LLVMWeakAnyLinkage;
1501 case GlobalValue::WeakODRLinkage:
1502 return LLVMWeakODRLinkage;
1503 case GlobalValue::AppendingLinkage:
1504 return LLVMAppendingLinkage;
1505 case GlobalValue::InternalLinkage:
1506 return LLVMInternalLinkage;
1507 case GlobalValue::PrivateLinkage:
1508 return LLVMPrivateLinkage;
1509 case GlobalValue::ExternalWeakLinkage:
1510 return LLVMExternalWeakLinkage;
1511 case GlobalValue::CommonLinkage:
1512 return LLVMCommonLinkage;
1515 llvm_unreachable("Invalid GlobalValue linkage!");
1518 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1519 GlobalValue *GV = unwrap<GlobalValue>(Global);
1522 case LLVMExternalLinkage:
1523 GV->setLinkage(GlobalValue::ExternalLinkage);
1525 case LLVMAvailableExternallyLinkage:
1526 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1528 case LLVMLinkOnceAnyLinkage:
1529 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1531 case LLVMLinkOnceODRLinkage:
1532 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1534 case LLVMLinkOnceODRAutoHideLinkage:
1535 DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1536 "longer supported.");
1538 case LLVMWeakAnyLinkage:
1539 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1541 case LLVMWeakODRLinkage:
1542 GV->setLinkage(GlobalValue::WeakODRLinkage);
1544 case LLVMAppendingLinkage:
1545 GV->setLinkage(GlobalValue::AppendingLinkage);
1547 case LLVMInternalLinkage:
1548 GV->setLinkage(GlobalValue::InternalLinkage);
1550 case LLVMPrivateLinkage:
1551 GV->setLinkage(GlobalValue::PrivateLinkage);
1553 case LLVMLinkerPrivateLinkage:
1554 GV->setLinkage(GlobalValue::PrivateLinkage);
1556 case LLVMLinkerPrivateWeakLinkage:
1557 GV->setLinkage(GlobalValue::PrivateLinkage);
1559 case LLVMDLLImportLinkage:
1561 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1563 case LLVMDLLExportLinkage:
1565 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1567 case LLVMExternalWeakLinkage:
1568 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1570 case LLVMGhostLinkage:
1572 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1574 case LLVMCommonLinkage:
1575 GV->setLinkage(GlobalValue::CommonLinkage);
1580 const char *LLVMGetSection(LLVMValueRef Global) {
1581 // Using .data() is safe because of how GlobalObject::setSection is
1583 return unwrap<GlobalValue>(Global)->getSection().data();
1586 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1587 unwrap<GlobalObject>(Global)->setSection(Section);
1590 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1591 return static_cast<LLVMVisibility>(
1592 unwrap<GlobalValue>(Global)->getVisibility());
1595 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1596 unwrap<GlobalValue>(Global)
1597 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1600 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
1601 return static_cast<LLVMDLLStorageClass>(
1602 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1605 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
1606 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1607 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1610 LLVMUnnamedAddr LLVMGetUnnamedAddress(LLVMValueRef Global) {
1611 switch (unwrap<GlobalValue>(Global)->getUnnamedAddr()) {
1612 case GlobalVariable::UnnamedAddr::None:
1613 return LLVMNoUnnamedAddr;
1614 case GlobalVariable::UnnamedAddr::Local:
1615 return LLVMLocalUnnamedAddr;
1616 case GlobalVariable::UnnamedAddr::Global:
1617 return LLVMGlobalUnnamedAddr;
1619 llvm_unreachable("Unknown UnnamedAddr kind!");
1622 void LLVMSetUnnamedAddress(LLVMValueRef Global, LLVMUnnamedAddr UnnamedAddr) {
1623 GlobalValue *GV = unwrap<GlobalValue>(Global);
1625 switch (UnnamedAddr) {
1626 case LLVMNoUnnamedAddr:
1627 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::None);
1628 case LLVMLocalUnnamedAddr:
1629 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Local);
1630 case LLVMGlobalUnnamedAddr:
1631 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Global);
1635 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
1636 return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
1639 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
1640 unwrap<GlobalValue>(Global)->setUnnamedAddr(
1641 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
1642 : GlobalValue::UnnamedAddr::None);
1645 /*--.. Operations on global variables, load and store instructions .........--*/
1647 unsigned LLVMGetAlignment(LLVMValueRef V) {
1648 Value *P = unwrap<Value>(V);
1649 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1650 return GV->getAlignment();
1651 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1652 return AI->getAlignment();
1653 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1654 return LI->getAlignment();
1655 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1656 return SI->getAlignment();
1659 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1662 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1663 Value *P = unwrap<Value>(V);
1664 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
1665 GV->setAlignment(Bytes);
1666 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1667 AI->setAlignment(Bytes);
1668 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1669 LI->setAlignment(Bytes);
1670 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1671 SI->setAlignment(Bytes);
1674 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1677 /*--.. Operations on global variables ......................................--*/
1679 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1680 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1681 GlobalValue::ExternalLinkage, nullptr, Name));
1684 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1686 unsigned AddressSpace) {
1687 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1688 GlobalValue::ExternalLinkage, nullptr, Name,
1689 nullptr, GlobalVariable::NotThreadLocal,
1693 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1694 return wrap(unwrap(M)->getNamedGlobal(Name));
1697 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1698 Module *Mod = unwrap(M);
1699 Module::global_iterator I = Mod->global_begin();
1700 if (I == Mod->global_end())
1705 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1706 Module *Mod = unwrap(M);
1707 Module::global_iterator I = Mod->global_end();
1708 if (I == Mod->global_begin())
1713 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1714 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1715 Module::global_iterator I(GV);
1716 if (++I == GV->getParent()->global_end())
1721 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1722 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1723 Module::global_iterator I(GV);
1724 if (I == GV->getParent()->global_begin())
1729 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1730 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1733 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1734 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1735 if ( !GV->hasInitializer() )
1737 return wrap(GV->getInitializer());
1740 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1741 unwrap<GlobalVariable>(GlobalVar)
1742 ->setInitializer(unwrap<Constant>(ConstantVal));
1745 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1746 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1749 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1750 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1753 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1754 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1757 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1758 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1761 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1762 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1763 case GlobalVariable::NotThreadLocal:
1764 return LLVMNotThreadLocal;
1765 case GlobalVariable::GeneralDynamicTLSModel:
1766 return LLVMGeneralDynamicTLSModel;
1767 case GlobalVariable::LocalDynamicTLSModel:
1768 return LLVMLocalDynamicTLSModel;
1769 case GlobalVariable::InitialExecTLSModel:
1770 return LLVMInitialExecTLSModel;
1771 case GlobalVariable::LocalExecTLSModel:
1772 return LLVMLocalExecTLSModel;
1775 llvm_unreachable("Invalid GlobalVariable thread local mode");
1778 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1779 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1782 case LLVMNotThreadLocal:
1783 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1785 case LLVMGeneralDynamicTLSModel:
1786 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1788 case LLVMLocalDynamicTLSModel:
1789 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1791 case LLVMInitialExecTLSModel:
1792 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1794 case LLVMLocalExecTLSModel:
1795 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1800 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1801 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1804 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1805 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1808 /*--.. Operations on aliases ......................................--*/
1810 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1812 auto *PTy = cast<PointerType>(unwrap(Ty));
1813 return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
1814 GlobalValue::ExternalLinkage, Name,
1815 unwrap<Constant>(Aliasee), unwrap(M)));
1818 /*--.. Operations on functions .............................................--*/
1820 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1821 LLVMTypeRef FunctionTy) {
1822 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1823 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1826 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1827 return wrap(unwrap(M)->getFunction(Name));
1830 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1831 Module *Mod = unwrap(M);
1832 Module::iterator I = Mod->begin();
1833 if (I == Mod->end())
1838 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1839 Module *Mod = unwrap(M);
1840 Module::iterator I = Mod->end();
1841 if (I == Mod->begin())
1846 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1847 Function *Func = unwrap<Function>(Fn);
1848 Module::iterator I(Func);
1849 if (++I == Func->getParent()->end())
1854 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1855 Function *Func = unwrap<Function>(Fn);
1856 Module::iterator I(Func);
1857 if (I == Func->getParent()->begin())
1862 void LLVMDeleteFunction(LLVMValueRef Fn) {
1863 unwrap<Function>(Fn)->eraseFromParent();
1866 LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) {
1867 return unwrap<Function>(Fn)->hasPersonalityFn();
1870 LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) {
1871 return wrap(unwrap<Function>(Fn)->getPersonalityFn());
1874 void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) {
1875 unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
1878 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1879 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1880 return F->getIntrinsicID();
1884 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1885 return unwrap<Function>(Fn)->getCallingConv();
1888 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1889 return unwrap<Function>(Fn)->setCallingConv(
1890 static_cast<CallingConv::ID>(CC));
1893 const char *LLVMGetGC(LLVMValueRef Fn) {
1894 Function *F = unwrap<Function>(Fn);
1895 return F->hasGC()? F->getGC().c_str() : nullptr;
1898 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1899 Function *F = unwrap<Function>(Fn);
1906 void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1907 LLVMAttributeRef A) {
1908 unwrap<Function>(F)->addAttribute(Idx, unwrap(A));
1911 unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) {
1912 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
1913 return AS.getNumAttributes();
1916 void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1917 LLVMAttributeRef *Attrs) {
1918 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
1923 LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F,
1924 LLVMAttributeIndex Idx,
1926 return wrap(unwrap<Function>(F)->getAttribute(Idx,
1927 (Attribute::AttrKind)KindID));
1930 LLVMAttributeRef LLVMGetStringAttributeAtIndex(LLVMValueRef F,
1931 LLVMAttributeIndex Idx,
1932 const char *K, unsigned KLen) {
1933 return wrap(unwrap<Function>(F)->getAttribute(Idx, StringRef(K, KLen)));
1936 void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1938 unwrap<Function>(F)->removeAttribute(Idx, (Attribute::AttrKind)KindID);
1941 void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1942 const char *K, unsigned KLen) {
1943 unwrap<Function>(F)->removeAttribute(Idx, StringRef(K, KLen));
1946 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1948 Function *Func = unwrap<Function>(Fn);
1949 Attribute Attr = Attribute::get(Func->getContext(), A, V);
1950 Func->addAttribute(AttributeList::FunctionIndex, Attr);
1953 /*--.. Operations on parameters ............................................--*/
1955 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1956 // This function is strictly redundant to
1957 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1958 return unwrap<Function>(FnRef)->arg_size();
1961 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1962 Function *Fn = unwrap<Function>(FnRef);
1963 for (Function::arg_iterator I = Fn->arg_begin(),
1964 E = Fn->arg_end(); I != E; I++)
1965 *ParamRefs++ = wrap(&*I);
1968 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1969 Function *Fn = unwrap<Function>(FnRef);
1970 return wrap(&Fn->arg_begin()[index]);
1973 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1974 return wrap(unwrap<Argument>(V)->getParent());
1977 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1978 Function *Func = unwrap<Function>(Fn);
1979 Function::arg_iterator I = Func->arg_begin();
1980 if (I == Func->arg_end())
1985 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1986 Function *Func = unwrap<Function>(Fn);
1987 Function::arg_iterator I = Func->arg_end();
1988 if (I == Func->arg_begin())
1993 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1994 Argument *A = unwrap<Argument>(Arg);
1995 Function *Fn = A->getParent();
1996 if (A->getArgNo() + 1 >= Fn->arg_size())
1998 return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
2001 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
2002 Argument *A = unwrap<Argument>(Arg);
2003 if (A->getArgNo() == 0)
2005 return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
2008 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
2009 Argument *A = unwrap<Argument>(Arg);
2010 A->addAttr(Attribute::getWithAlignment(A->getContext(), align));
2013 /*--.. Operations on basic blocks ..........................................--*/
2015 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
2016 return wrap(static_cast<Value*>(unwrap(BB)));
2019 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
2020 return isa<BasicBlock>(unwrap(Val));
2023 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
2024 return wrap(unwrap<BasicBlock>(Val));
2027 const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) {
2028 return unwrap(BB)->getName().data();
2031 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
2032 return wrap(unwrap(BB)->getParent());
2035 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
2036 return wrap(unwrap(BB)->getTerminator());
2039 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
2040 return unwrap<Function>(FnRef)->size();
2043 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
2044 Function *Fn = unwrap<Function>(FnRef);
2045 for (BasicBlock &BB : *Fn)
2046 *BasicBlocksRefs++ = wrap(&BB);
2049 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
2050 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2053 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
2054 Function *Func = unwrap<Function>(Fn);
2055 Function::iterator I = Func->begin();
2056 if (I == Func->end())
2061 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
2062 Function *Func = unwrap<Function>(Fn);
2063 Function::iterator I = Func->end();
2064 if (I == Func->begin())
2069 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
2070 BasicBlock *Block = unwrap(BB);
2071 Function::iterator I(Block);
2072 if (++I == Block->getParent()->end())
2077 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
2078 BasicBlock *Block = unwrap(BB);
2079 Function::iterator I(Block);
2080 if (I == Block->getParent()->begin())
2085 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
2088 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
2091 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
2092 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
2095 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
2096 LLVMBasicBlockRef BBRef,
2098 BasicBlock *BB = unwrap(BBRef);
2099 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2102 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
2104 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
2107 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
2108 unwrap(BBRef)->eraseFromParent();
2111 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
2112 unwrap(BBRef)->removeFromParent();
2115 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2116 unwrap(BB)->moveBefore(unwrap(MovePos));
2119 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2120 unwrap(BB)->moveAfter(unwrap(MovePos));
2123 /*--.. Operations on instructions ..........................................--*/
2125 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
2126 return wrap(unwrap<Instruction>(Inst)->getParent());
2129 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
2130 BasicBlock *Block = unwrap(BB);
2131 BasicBlock::iterator I = Block->begin();
2132 if (I == Block->end())
2137 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
2138 BasicBlock *Block = unwrap(BB);
2139 BasicBlock::iterator I = Block->end();
2140 if (I == Block->begin())
2145 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
2146 Instruction *Instr = unwrap<Instruction>(Inst);
2147 BasicBlock::iterator I(Instr);
2148 if (++I == Instr->getParent()->end())
2153 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
2154 Instruction *Instr = unwrap<Instruction>(Inst);
2155 BasicBlock::iterator I(Instr);
2156 if (I == Instr->getParent()->begin())
2161 void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) {
2162 unwrap<Instruction>(Inst)->removeFromParent();
2165 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
2166 unwrap<Instruction>(Inst)->eraseFromParent();
2169 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
2170 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2171 return (LLVMIntPredicate)I->getPredicate();
2172 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2173 if (CE->getOpcode() == Instruction::ICmp)
2174 return (LLVMIntPredicate)CE->getPredicate();
2175 return (LLVMIntPredicate)0;
2178 LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
2179 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2180 return (LLVMRealPredicate)I->getPredicate();
2181 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2182 if (CE->getOpcode() == Instruction::FCmp)
2183 return (LLVMRealPredicate)CE->getPredicate();
2184 return (LLVMRealPredicate)0;
2187 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
2188 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2189 return map_to_llvmopcode(C->getOpcode());
2190 return (LLVMOpcode)0;
2193 LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {
2194 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2195 return wrap(C->clone());
2199 unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) {
2200 if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) {
2201 return FPI->getNumArgOperands();
2203 return CallSite(unwrap<Instruction>(Instr)).getNumArgOperands();
2206 /*--.. Call and invoke instructions ........................................--*/
2208 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
2209 return CallSite(unwrap<Instruction>(Instr)).getCallingConv();
2212 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
2213 return CallSite(unwrap<Instruction>(Instr))
2214 .setCallingConv(static_cast<CallingConv::ID>(CC));
2217 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
2219 CallSite Call = CallSite(unwrap<Instruction>(Instr));
2220 Attribute AlignAttr = Attribute::getWithAlignment(Call->getContext(), align);
2221 Call.addAttribute(index, AlignAttr);
2224 void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2225 LLVMAttributeRef A) {
2226 CallSite(unwrap<Instruction>(C)).addAttribute(Idx, unwrap(A));
2229 unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C,
2230 LLVMAttributeIndex Idx) {
2231 auto CS = CallSite(unwrap<Instruction>(C));
2232 auto AS = CS.getAttributes().getAttributes(Idx);
2233 return AS.getNumAttributes();
2236 void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx,
2237 LLVMAttributeRef *Attrs) {
2238 auto CS = CallSite(unwrap<Instruction>(C));
2239 auto AS = CS.getAttributes().getAttributes(Idx);
2244 LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C,
2245 LLVMAttributeIndex Idx,
2247 return wrap(CallSite(unwrap<Instruction>(C))
2248 .getAttribute(Idx, (Attribute::AttrKind)KindID));
2251 LLVMAttributeRef LLVMGetCallSiteStringAttribute(LLVMValueRef C,
2252 LLVMAttributeIndex Idx,
2253 const char *K, unsigned KLen) {
2254 return wrap(CallSite(unwrap<Instruction>(C))
2255 .getAttribute(Idx, StringRef(K, KLen)));
2258 void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2260 CallSite(unwrap<Instruction>(C))
2261 .removeAttribute(Idx, (Attribute::AttrKind)KindID);
2264 void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2265 const char *K, unsigned KLen) {
2266 CallSite(unwrap<Instruction>(C)).removeAttribute(Idx, StringRef(K, KLen));
2269 LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) {
2270 return wrap(CallSite(unwrap<Instruction>(Instr)).getCalledValue());
2273 /*--.. Operations on call instructions (only) ..............................--*/
2275 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
2276 return unwrap<CallInst>(Call)->isTailCall();
2279 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
2280 unwrap<CallInst>(Call)->setTailCall(isTailCall);
2283 /*--.. Operations on invoke instructions (only) ............................--*/
2285 LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) {
2286 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2289 LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) {
2290 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2291 return wrap(CRI->getUnwindDest());
2292 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2293 return wrap(CSI->getUnwindDest());
2295 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2298 void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2299 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2302 void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2303 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2304 return CRI->setUnwindDest(unwrap(B));
2305 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2306 return CSI->setUnwindDest(unwrap(B));
2308 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2311 /*--.. Operations on terminators ...........................................--*/
2313 unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {
2314 return unwrap<TerminatorInst>(Term)->getNumSuccessors();
2317 LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) {
2318 return wrap(unwrap<TerminatorInst>(Term)->getSuccessor(i));
2321 void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
2322 return unwrap<TerminatorInst>(Term)->setSuccessor(i,unwrap(block));
2325 /*--.. Operations on branch instructions (only) ............................--*/
2327 LLVMBool LLVMIsConditional(LLVMValueRef Branch) {
2328 return unwrap<BranchInst>(Branch)->isConditional();
2331 LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) {
2332 return wrap(unwrap<BranchInst>(Branch)->getCondition());
2335 void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) {
2336 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2339 /*--.. Operations on switch instructions (only) ............................--*/
2341 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
2342 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2345 /*--.. Operations on alloca instructions (only) ............................--*/
2347 LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) {
2348 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
2351 /*--.. Operations on gep instructions (only) ...............................--*/
2353 LLVMBool LLVMIsInBounds(LLVMValueRef GEP) {
2354 return unwrap<GetElementPtrInst>(GEP)->isInBounds();
2357 void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) {
2358 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
2361 /*--.. Operations on phi nodes .............................................--*/
2363 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
2364 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
2365 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
2366 for (unsigned I = 0; I != Count; ++I)
2367 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
2370 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
2371 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
2374 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
2375 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
2378 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
2379 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
2382 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/
2384 unsigned LLVMGetNumIndices(LLVMValueRef Inst) {
2385 auto *I = unwrap(Inst);
2386 if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
2387 return GEP->getNumIndices();
2388 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2389 return EV->getNumIndices();
2390 if (auto *IV = dyn_cast<InsertValueInst>(I))
2391 return IV->getNumIndices();
2393 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
2396 const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
2397 auto *I = unwrap(Inst);
2398 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2399 return EV->getIndices().data();
2400 if (auto *IV = dyn_cast<InsertValueInst>(I))
2401 return IV->getIndices().data();
2403 "LLVMGetIndices applies only to extractvalue and insertvalue!");
2407 /*===-- Instruction builders ----------------------------------------------===*/
2409 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
2410 return wrap(new IRBuilder<>(*unwrap(C)));
2413 LLVMBuilderRef LLVMCreateBuilder(void) {
2414 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
2417 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
2418 LLVMValueRef Instr) {
2419 BasicBlock *BB = unwrap(Block);
2420 auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
2421 unwrap(Builder)->SetInsertPoint(BB, I);
2424 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2425 Instruction *I = unwrap<Instruction>(Instr);
2426 unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
2429 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
2430 BasicBlock *BB = unwrap(Block);
2431 unwrap(Builder)->SetInsertPoint(BB);
2434 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
2435 return wrap(unwrap(Builder)->GetInsertBlock());
2438 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
2439 unwrap(Builder)->ClearInsertionPoint();
2442 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2443 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
2446 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
2448 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
2451 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
2452 delete unwrap(Builder);
2455 /*--.. Metadata builders ...................................................--*/
2457 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
2459 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
2460 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
2463 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
2464 LLVMContext &Context = unwrap(Builder)->getContext();
2465 return wrap(MetadataAsValue::get(
2466 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
2469 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
2470 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
2474 /*--.. Instruction builders ................................................--*/
2476 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
2477 return wrap(unwrap(B)->CreateRetVoid());
2480 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
2481 return wrap(unwrap(B)->CreateRet(unwrap(V)));
2484 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
2486 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
2489 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
2490 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
2493 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
2494 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
2495 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
2498 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
2499 LLVMBasicBlockRef Else, unsigned NumCases) {
2500 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
2503 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
2504 unsigned NumDests) {
2505 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
2508 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
2509 LLVMValueRef *Args, unsigned NumArgs,
2510 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
2512 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
2513 makeArrayRef(unwrap(Args), NumArgs),
2517 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
2518 LLVMValueRef PersFn, unsigned NumClauses,
2520 // The personality used to live on the landingpad instruction, but now it
2521 // lives on the parent function. For compatibility, take the provided
2522 // personality and put it on the parent function.
2524 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
2525 cast<Function>(unwrap(PersFn)));
2526 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
2529 LLVMValueRef LLVMBuildCatchPad(LLVMBuilderRef B, LLVMValueRef ParentPad,
2530 LLVMValueRef *Args, unsigned NumArgs,
2532 return wrap(unwrap(B)->CreateCatchPad(unwrap(ParentPad),
2533 makeArrayRef(unwrap(Args), NumArgs),
2537 LLVMValueRef LLVMBuildCleanupPad(LLVMBuilderRef B, LLVMValueRef ParentPad,
2538 LLVMValueRef *Args, unsigned NumArgs,
2540 if (ParentPad == nullptr) {
2541 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
2542 ParentPad = wrap(Constant::getNullValue(Ty));
2544 return wrap(unwrap(B)->CreateCleanupPad(unwrap(ParentPad),
2545 makeArrayRef(unwrap(Args), NumArgs),
2549 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
2550 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
2553 LLVMValueRef LLVMBuildCatchSwitch(LLVMBuilderRef B, LLVMValueRef ParentPad,
2554 LLVMBasicBlockRef UnwindBB,
2555 unsigned NumHandlers, const char *Name) {
2556 if (ParentPad == nullptr) {
2557 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
2558 ParentPad = wrap(Constant::getNullValue(Ty));
2560 return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(UnwindBB),
2561 NumHandlers, Name));
2564 LLVMValueRef LLVMBuildCatchRet(LLVMBuilderRef B, LLVMValueRef CatchPad,
2565 LLVMBasicBlockRef BB) {
2566 return wrap(unwrap(B)->CreateCatchRet(unwrap<CatchPadInst>(CatchPad),
2570 LLVMValueRef LLVMBuildCleanupRet(LLVMBuilderRef B, LLVMValueRef CatchPad,
2571 LLVMBasicBlockRef BB) {
2572 return wrap(unwrap(B)->CreateCleanupRet(unwrap<CleanupPadInst>(CatchPad),
2576 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
2577 return wrap(unwrap(B)->CreateUnreachable());
2580 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
2581 LLVMBasicBlockRef Dest) {
2582 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
2585 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
2586 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
2589 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
2590 return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
2593 LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
2594 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
2597 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
2598 unwrap<LandingPadInst>(LandingPad)->
2599 addClause(cast<Constant>(unwrap(ClauseVal)));
2602 LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) {
2603 return unwrap<LandingPadInst>(LandingPad)->isCleanup();
2606 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
2607 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2610 void LLVMAddHandler(LLVMValueRef CatchSwitch, LLVMBasicBlockRef Dest) {
2611 unwrap<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Dest));
2614 unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) {
2615 return unwrap<CatchSwitchInst>(CatchSwitch)->getNumHandlers();
2618 void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) {
2619 CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(CatchSwitch);
2620 for (CatchSwitchInst::handler_iterator I = CSI->handler_begin(),
2621 E = CSI->handler_end(); I != E; ++I)
2622 *Handlers++ = wrap(*I);
2625 LLVMValueRef LLVMGetParentCatchSwitch(LLVMValueRef CatchPad) {
2626 return wrap(unwrap<CatchPadInst>(CatchPad)->getCatchSwitch());
2629 void LLVMSetParentCatchSwitch(LLVMValueRef CatchPad, LLVMValueRef CatchSwitch) {
2630 unwrap<CatchPadInst>(CatchPad)
2631 ->setCatchSwitch(unwrap<CatchSwitchInst>(CatchSwitch));
2634 /*--.. Funclets ...........................................................--*/
2636 LLVMValueRef LLVMGetArgOperand(LLVMValueRef Funclet, unsigned i) {
2637 return wrap(unwrap<FuncletPadInst>(Funclet)->getArgOperand(i));
2640 void LLVMSetArgOperand(LLVMValueRef Funclet, unsigned i, LLVMValueRef value) {
2641 unwrap<FuncletPadInst>(Funclet)->setArgOperand(i, unwrap(value));
2644 /*--.. Arithmetic ..........................................................--*/
2646 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2648 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2651 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2653 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2656 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2658 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2661 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2663 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2666 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2668 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2671 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2673 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2676 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2678 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2681 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2683 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2686 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2688 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2691 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2693 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2696 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2698 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2701 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2703 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2706 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2708 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2711 LLVMValueRef LLVMBuildExactUDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2712 LLVMValueRef RHS, const char *Name) {
2713 return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
2716 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2718 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2721 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2722 LLVMValueRef RHS, const char *Name) {
2723 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2726 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2728 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2731 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2733 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2736 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2738 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2741 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2743 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2746 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2748 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2751 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2753 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2756 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2758 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2761 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2763 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2766 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2768 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2771 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2773 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2776 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2777 LLVMValueRef LHS, LLVMValueRef RHS,
2779 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2780 unwrap(RHS), Name));
2783 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2784 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2787 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2789 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2792 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2794 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2797 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2798 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2801 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2802 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2805 /*--.. Memory ..............................................................--*/
2807 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2809 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2810 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2811 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2812 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2813 ITy, unwrap(Ty), AllocSize,
2814 nullptr, nullptr, "");
2815 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2818 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2819 LLVMValueRef Val, const char *Name) {
2820 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2821 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2822 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2823 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2824 ITy, unwrap(Ty), AllocSize,
2825 unwrap(Val), nullptr, "");
2826 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2829 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2831 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
2834 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2835 LLVMValueRef Val, const char *Name) {
2836 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2839 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2840 return wrap(unwrap(B)->Insert(
2841 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2844 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2846 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2849 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2850 LLVMValueRef PointerVal) {
2851 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2854 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
2856 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic;
2857 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered;
2858 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic;
2859 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire;
2860 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release;
2861 case LLVMAtomicOrderingAcquireRelease:
2862 return AtomicOrdering::AcquireRelease;
2863 case LLVMAtomicOrderingSequentiallyConsistent:
2864 return AtomicOrdering::SequentiallyConsistent;
2867 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
2870 static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) {
2872 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic;
2873 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered;
2874 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic;
2875 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire;
2876 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease;
2877 case AtomicOrdering::AcquireRelease:
2878 return LLVMAtomicOrderingAcquireRelease;
2879 case AtomicOrdering::SequentiallyConsistent:
2880 return LLVMAtomicOrderingSequentiallyConsistent;
2883 llvm_unreachable("Invalid AtomicOrdering value!");
2886 // TODO: Should this and other atomic instructions support building with
2888 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
2889 LLVMBool isSingleThread, const char *Name) {
2891 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
2892 isSingleThread ? SyncScope::SingleThread
2893 : SyncScope::System,
2897 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2898 LLVMValueRef *Indices, unsigned NumIndices,
2900 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2901 return wrap(unwrap(B)->CreateGEP(nullptr, unwrap(Pointer), IdxList, Name));
2904 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2905 LLVMValueRef *Indices, unsigned NumIndices,
2907 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2909 unwrap(B)->CreateInBoundsGEP(nullptr, unwrap(Pointer), IdxList, Name));
2912 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2913 unsigned Idx, const char *Name) {
2914 return wrap(unwrap(B)->CreateStructGEP(nullptr, unwrap(Pointer), Idx, Name));
2917 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2919 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2922 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2924 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2927 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2928 Value *P = unwrap<Value>(MemAccessInst);
2929 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2930 return LI->isVolatile();
2931 return cast<StoreInst>(P)->isVolatile();
2934 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2935 Value *P = unwrap<Value>(MemAccessInst);
2936 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2937 return LI->setVolatile(isVolatile);
2938 return cast<StoreInst>(P)->setVolatile(isVolatile);
2941 LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) {
2942 Value *P = unwrap<Value>(MemAccessInst);
2944 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2945 O = LI->getOrdering();
2947 O = cast<StoreInst>(P)->getOrdering();
2948 return mapToLLVMOrdering(O);
2951 void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
2952 Value *P = unwrap<Value>(MemAccessInst);
2953 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
2955 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2956 return LI->setOrdering(O);
2957 return cast<StoreInst>(P)->setOrdering(O);
2960 /*--.. Casts ...............................................................--*/
2962 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2963 LLVMTypeRef DestTy, const char *Name) {
2964 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2967 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2968 LLVMTypeRef DestTy, const char *Name) {
2969 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2972 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2973 LLVMTypeRef DestTy, const char *Name) {
2974 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2977 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2978 LLVMTypeRef DestTy, const char *Name) {
2979 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2982 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2983 LLVMTypeRef DestTy, const char *Name) {
2984 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2987 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2988 LLVMTypeRef DestTy, const char *Name) {
2989 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2992 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2993 LLVMTypeRef DestTy, const char *Name) {
2994 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2997 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2998 LLVMTypeRef DestTy, const char *Name) {
2999 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
3002 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
3003 LLVMTypeRef DestTy, const char *Name) {
3004 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
3007 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
3008 LLVMTypeRef DestTy, const char *Name) {
3009 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
3012 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
3013 LLVMTypeRef DestTy, const char *Name) {
3014 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
3017 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3018 LLVMTypeRef DestTy, const char *Name) {
3019 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
3022 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
3023 LLVMTypeRef DestTy, const char *Name) {
3024 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
3027 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3028 LLVMTypeRef DestTy, const char *Name) {
3029 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
3033 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3034 LLVMTypeRef DestTy, const char *Name) {
3035 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
3039 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3040 LLVMTypeRef DestTy, const char *Name) {
3041 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
3045 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
3046 LLVMTypeRef DestTy, const char *Name) {
3047 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
3048 unwrap(DestTy), Name));
3051 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
3052 LLVMTypeRef DestTy, const char *Name) {
3053 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
3056 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
3057 LLVMTypeRef DestTy, const char *Name) {
3058 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
3059 /*isSigned*/true, Name));
3062 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
3063 LLVMTypeRef DestTy, const char *Name) {
3064 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
3067 /*--.. Comparisons .........................................................--*/
3069 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
3070 LLVMValueRef LHS, LLVMValueRef RHS,
3072 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
3073 unwrap(LHS), unwrap(RHS), Name));
3076 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
3077 LLVMValueRef LHS, LLVMValueRef RHS,
3079 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
3080 unwrap(LHS), unwrap(RHS), Name));
3083 /*--.. Miscellaneous instructions ..........................................--*/
3085 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
3086 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
3089 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
3090 LLVMValueRef *Args, unsigned NumArgs,
3092 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
3093 makeArrayRef(unwrap(Args), NumArgs),
3097 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
3098 LLVMValueRef Then, LLVMValueRef Else,
3100 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
3104 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
3105 LLVMTypeRef Ty, const char *Name) {
3106 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
3109 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
3110 LLVMValueRef Index, const char *Name) {
3111 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
3115 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
3116 LLVMValueRef EltVal, LLVMValueRef Index,
3118 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
3119 unwrap(Index), Name));
3122 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
3123 LLVMValueRef V2, LLVMValueRef Mask,
3125 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
3126 unwrap(Mask), Name));
3129 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
3130 unsigned Index, const char *Name) {
3131 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
3134 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
3135 LLVMValueRef EltVal, unsigned Index,
3137 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
3141 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
3143 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
3146 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
3148 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
3151 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
3152 LLVMValueRef RHS, const char *Name) {
3153 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
3156 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
3157 LLVMValueRef PTR, LLVMValueRef Val,
3158 LLVMAtomicOrdering ordering,
3159 LLVMBool singleThread) {
3160 AtomicRMWInst::BinOp intop;
3162 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
3163 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
3164 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
3165 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
3166 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
3167 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
3168 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
3169 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
3170 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
3171 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
3172 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
3174 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
3175 mapFromLLVMOrdering(ordering), singleThread ? SyncScope::SingleThread
3176 : SyncScope::System));
3179 LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr,
3180 LLVMValueRef Cmp, LLVMValueRef New,
3181 LLVMAtomicOrdering SuccessOrdering,
3182 LLVMAtomicOrdering FailureOrdering,
3183 LLVMBool singleThread) {
3185 return wrap(unwrap(B)->CreateAtomicCmpXchg(unwrap(Ptr), unwrap(Cmp),
3186 unwrap(New), mapFromLLVMOrdering(SuccessOrdering),
3187 mapFromLLVMOrdering(FailureOrdering),
3188 singleThread ? SyncScope::SingleThread : SyncScope::System));
3192 LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) {
3193 Value *P = unwrap<Value>(AtomicInst);
3195 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
3196 return I->getSyncScopeID() == SyncScope::SingleThread;
3197 return cast<AtomicCmpXchgInst>(P)->getSyncScopeID() ==
3198 SyncScope::SingleThread;
3201 void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) {
3202 Value *P = unwrap<Value>(AtomicInst);
3203 SyncScope::ID SSID = NewValue ? SyncScope::SingleThread : SyncScope::System;
3205 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
3206 return I->setSyncScopeID(SSID);
3207 return cast<AtomicCmpXchgInst>(P)->setSyncScopeID(SSID);
3210 LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) {
3211 Value *P = unwrap<Value>(CmpXchgInst);
3212 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering());
3215 void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst,
3216 LLVMAtomicOrdering Ordering) {
3217 Value *P = unwrap<Value>(CmpXchgInst);
3218 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3220 return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O);
3223 LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst) {
3224 Value *P = unwrap<Value>(CmpXchgInst);
3225 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering());
3228 void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst,
3229 LLVMAtomicOrdering Ordering) {
3230 Value *P = unwrap<Value>(CmpXchgInst);
3231 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3233 return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O);
3236 /*===-- Module providers --------------------------------------------------===*/
3238 LLVMModuleProviderRef
3239 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
3240 return reinterpret_cast<LLVMModuleProviderRef>(M);
3243 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
3248 /*===-- Memory buffers ----------------------------------------------------===*/
3250 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
3252 LLVMMemoryBufferRef *OutMemBuf,
3253 char **OutMessage) {
3255 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
3256 if (std::error_code EC = MBOrErr.getError()) {
3257 *OutMessage = strdup(EC.message().c_str());
3260 *OutMemBuf = wrap(MBOrErr.get().release());
3264 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
3265 char **OutMessage) {
3266 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
3267 if (std::error_code EC = MBOrErr.getError()) {
3268 *OutMessage = strdup(EC.message().c_str());
3271 *OutMemBuf = wrap(MBOrErr.get().release());
3275 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
3276 const char *InputData,
3277 size_t InputDataLength,
3278 const char *BufferName,
3279 LLVMBool RequiresNullTerminator) {
3281 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
3282 StringRef(BufferName),
3283 RequiresNullTerminator).release());
3286 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
3287 const char *InputData,
3288 size_t InputDataLength,
3289 const char *BufferName) {
3292 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
3293 StringRef(BufferName)).release());
3296 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
3297 return unwrap(MemBuf)->getBufferStart();
3300 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
3301 return unwrap(MemBuf)->getBufferSize();
3304 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
3305 delete unwrap(MemBuf);
3308 /*===-- Pass Registry -----------------------------------------------------===*/
3310 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
3311 return wrap(PassRegistry::getPassRegistry());
3314 /*===-- Pass Manager ------------------------------------------------------===*/
3316 LLVMPassManagerRef LLVMCreatePassManager() {
3317 return wrap(new legacy::PassManager());
3320 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
3321 return wrap(new legacy::FunctionPassManager(unwrap(M)));
3324 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
3325 return LLVMCreateFunctionPassManagerForModule(
3326 reinterpret_cast<LLVMModuleRef>(P));
3329 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
3330 return unwrap<legacy::PassManager>(PM)->run(*unwrap(M));
3333 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
3334 return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization();
3337 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
3338 return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
3341 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
3342 return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization();
3345 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
3349 /*===-- Threading ------------------------------------------------------===*/
3351 LLVMBool LLVMStartMultithreaded() {
3352 return LLVMIsMultithreaded();
3355 void LLVMStopMultithreaded() {
3358 LLVMBool LLVMIsMultithreaded() {
3359 return llvm_is_multithreaded();