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));
238 /*--.. Data layout .........................................................--*/
239 const char *LLVMGetDataLayoutStr(LLVMModuleRef M) {
240 return unwrap(M)->getDataLayoutStr().c_str();
243 const char *LLVMGetDataLayout(LLVMModuleRef M) {
244 return LLVMGetDataLayoutStr(M);
247 void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
248 unwrap(M)->setDataLayout(DataLayoutStr);
251 /*--.. Target triple .......................................................--*/
252 const char * LLVMGetTarget(LLVMModuleRef M) {
253 return unwrap(M)->getTargetTriple().c_str();
256 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
257 unwrap(M)->setTargetTriple(Triple);
260 void LLVMDumpModule(LLVMModuleRef M) {
261 unwrap(M)->print(errs(), nullptr,
262 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
265 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
266 char **ErrorMessage) {
268 raw_fd_ostream dest(Filename, EC, sys::fs::F_Text);
270 *ErrorMessage = strdup(EC.message().c_str());
274 unwrap(M)->print(dest, nullptr);
278 if (dest.has_error()) {
279 std::string E = "Error printing to file: " + dest.error().message();
280 *ErrorMessage = strdup(E.c_str());
287 char *LLVMPrintModuleToString(LLVMModuleRef M) {
289 raw_string_ostream os(buf);
291 unwrap(M)->print(os, nullptr);
294 return strdup(buf.c_str());
297 /*--.. Operations on inline assembler ......................................--*/
298 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
299 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
303 /*--.. Operations on module contexts ......................................--*/
304 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
305 return wrap(&unwrap(M)->getContext());
309 /*===-- Operations on types -----------------------------------------------===*/
311 /*--.. Operations on all types (mostly) ....................................--*/
313 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
314 switch (unwrap(Ty)->getTypeID()) {
316 return LLVMVoidTypeKind;
318 return LLVMHalfTypeKind;
319 case Type::FloatTyID:
320 return LLVMFloatTypeKind;
321 case Type::DoubleTyID:
322 return LLVMDoubleTypeKind;
323 case Type::X86_FP80TyID:
324 return LLVMX86_FP80TypeKind;
325 case Type::FP128TyID:
326 return LLVMFP128TypeKind;
327 case Type::PPC_FP128TyID:
328 return LLVMPPC_FP128TypeKind;
329 case Type::LabelTyID:
330 return LLVMLabelTypeKind;
331 case Type::MetadataTyID:
332 return LLVMMetadataTypeKind;
333 case Type::IntegerTyID:
334 return LLVMIntegerTypeKind;
335 case Type::FunctionTyID:
336 return LLVMFunctionTypeKind;
337 case Type::StructTyID:
338 return LLVMStructTypeKind;
339 case Type::ArrayTyID:
340 return LLVMArrayTypeKind;
341 case Type::PointerTyID:
342 return LLVMPointerTypeKind;
343 case Type::VectorTyID:
344 return LLVMVectorTypeKind;
345 case Type::X86_MMXTyID:
346 return LLVMX86_MMXTypeKind;
347 case Type::TokenTyID:
348 return LLVMTokenTypeKind;
350 llvm_unreachable("Unhandled TypeID.");
353 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
355 return unwrap(Ty)->isSized();
358 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
359 return wrap(&unwrap(Ty)->getContext());
362 void LLVMDumpType(LLVMTypeRef Ty) {
363 return unwrap(Ty)->print(errs(), /*IsForDebug=*/true);
366 char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
368 raw_string_ostream os(buf);
371 unwrap(Ty)->print(os);
373 os << "Printing <null> Type";
377 return strdup(buf.c_str());
380 /*--.. Operations on integer types .........................................--*/
382 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
383 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
385 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
386 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
388 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
389 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
391 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
392 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
394 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
395 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
397 LLVMTypeRef LLVMInt128TypeInContext(LLVMContextRef C) {
398 return (LLVMTypeRef) Type::getInt128Ty(*unwrap(C));
400 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
401 return wrap(IntegerType::get(*unwrap(C), NumBits));
404 LLVMTypeRef LLVMInt1Type(void) {
405 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
407 LLVMTypeRef LLVMInt8Type(void) {
408 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
410 LLVMTypeRef LLVMInt16Type(void) {
411 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
413 LLVMTypeRef LLVMInt32Type(void) {
414 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
416 LLVMTypeRef LLVMInt64Type(void) {
417 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
419 LLVMTypeRef LLVMInt128Type(void) {
420 return LLVMInt128TypeInContext(LLVMGetGlobalContext());
422 LLVMTypeRef LLVMIntType(unsigned NumBits) {
423 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
426 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
427 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
430 /*--.. Operations on real types ............................................--*/
432 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
433 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
435 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
436 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
438 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
439 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
441 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
442 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
444 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
445 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
447 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
448 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
450 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
451 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
454 LLVMTypeRef LLVMHalfType(void) {
455 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
457 LLVMTypeRef LLVMFloatType(void) {
458 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
460 LLVMTypeRef LLVMDoubleType(void) {
461 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
463 LLVMTypeRef LLVMX86FP80Type(void) {
464 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
466 LLVMTypeRef LLVMFP128Type(void) {
467 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
469 LLVMTypeRef LLVMPPCFP128Type(void) {
470 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
472 LLVMTypeRef LLVMX86MMXType(void) {
473 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
476 /*--.. Operations on function types ........................................--*/
478 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
479 LLVMTypeRef *ParamTypes, unsigned ParamCount,
481 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
482 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
485 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
486 return unwrap<FunctionType>(FunctionTy)->isVarArg();
489 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
490 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
493 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
494 return unwrap<FunctionType>(FunctionTy)->getNumParams();
497 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
498 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
499 for (FunctionType::param_iterator I = Ty->param_begin(),
500 E = Ty->param_end(); I != E; ++I)
504 /*--.. Operations on struct types ..........................................--*/
506 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
507 unsigned ElementCount, LLVMBool Packed) {
508 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
509 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
512 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
513 unsigned ElementCount, LLVMBool Packed) {
514 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
515 ElementCount, Packed);
518 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
520 return wrap(StructType::create(*unwrap(C), Name));
523 const char *LLVMGetStructName(LLVMTypeRef Ty)
525 StructType *Type = unwrap<StructType>(Ty);
526 if (!Type->hasName())
528 return Type->getName().data();
531 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
532 unsigned ElementCount, LLVMBool Packed) {
533 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
534 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
537 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
538 return unwrap<StructType>(StructTy)->getNumElements();
541 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
542 StructType *Ty = unwrap<StructType>(StructTy);
543 for (StructType::element_iterator I = Ty->element_begin(),
544 E = Ty->element_end(); I != E; ++I)
548 LLVMTypeRef LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy, unsigned i) {
549 StructType *Ty = unwrap<StructType>(StructTy);
550 return wrap(Ty->getTypeAtIndex(i));
553 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
554 return unwrap<StructType>(StructTy)->isPacked();
557 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
558 return unwrap<StructType>(StructTy)->isOpaque();
561 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
562 return wrap(unwrap(M)->getTypeByName(Name));
565 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
567 void LLVMGetSubtypes(LLVMTypeRef Tp, LLVMTypeRef *Arr) {
569 for (auto *T : unwrap(Tp)->subtypes()) {
575 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
576 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
579 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
580 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
583 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
584 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
587 LLVMTypeRef LLVMGetElementType(LLVMTypeRef WrappedTy) {
588 auto *Ty = unwrap<Type>(WrappedTy);
589 if (auto *PTy = dyn_cast<PointerType>(Ty))
590 return wrap(PTy->getElementType());
591 return wrap(cast<SequentialType>(Ty)->getElementType());
594 unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp) {
595 return unwrap(Tp)->getNumContainedTypes();
598 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
599 return unwrap<ArrayType>(ArrayTy)->getNumElements();
602 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
603 return unwrap<PointerType>(PointerTy)->getAddressSpace();
606 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
607 return unwrap<VectorType>(VectorTy)->getNumElements();
610 /*--.. Operations on other types ...........................................--*/
612 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
613 return wrap(Type::getVoidTy(*unwrap(C)));
615 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
616 return wrap(Type::getLabelTy(*unwrap(C)));
618 LLVMTypeRef LLVMTokenTypeInContext(LLVMContextRef C) {
619 return wrap(Type::getTokenTy(*unwrap(C)));
621 LLVMTypeRef LLVMMetadataTypeInContext(LLVMContextRef C) {
622 return wrap(Type::getMetadataTy(*unwrap(C)));
625 LLVMTypeRef LLVMVoidType(void) {
626 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
628 LLVMTypeRef LLVMLabelType(void) {
629 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
632 /*===-- Operations on values ----------------------------------------------===*/
634 /*--.. Operations on all values ............................................--*/
636 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
637 return wrap(unwrap(Val)->getType());
640 LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) {
641 switch(unwrap(Val)->getValueID()) {
642 #define HANDLE_VALUE(Name) \
643 case Value::Name##Val: \
644 return LLVM##Name##ValueKind;
645 #include "llvm/IR/Value.def"
647 return LLVMInstructionValueKind;
651 const char *LLVMGetValueName(LLVMValueRef Val) {
652 return unwrap(Val)->getName().data();
655 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
656 unwrap(Val)->setName(Name);
659 void LLVMDumpValue(LLVMValueRef Val) {
660 unwrap(Val)->print(errs(), /*IsForDebug=*/true);
663 char* LLVMPrintValueToString(LLVMValueRef Val) {
665 raw_string_ostream os(buf);
668 unwrap(Val)->print(os);
670 os << "Printing <null> Value";
674 return strdup(buf.c_str());
677 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
678 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
681 int LLVMHasMetadata(LLVMValueRef Inst) {
682 return unwrap<Instruction>(Inst)->hasMetadata();
685 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
686 auto *I = unwrap<Instruction>(Inst);
687 assert(I && "Expected instruction");
688 if (auto *MD = I->getMetadata(KindID))
689 return wrap(MetadataAsValue::get(I->getContext(), MD));
693 // MetadataAsValue uses a canonical format which strips the actual MDNode for
694 // MDNode with just a single constant value, storing just a ConstantAsMetadata
695 // This undoes this canonicalization, reconstructing the MDNode.
696 static MDNode *extractMDNode(MetadataAsValue *MAV) {
697 Metadata *MD = MAV->getMetadata();
698 assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
699 "Expected a metadata node or a canonicalized constant");
701 if (MDNode *N = dyn_cast<MDNode>(MD))
704 return MDNode::get(MAV->getContext(), MD);
707 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
708 MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
710 unwrap<Instruction>(Inst)->setMetadata(KindID, N);
713 /*--.. Conversion functions ................................................--*/
715 #define LLVM_DEFINE_VALUE_CAST(name) \
716 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
717 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
720 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
722 LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) {
723 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
724 if (isa<MDNode>(MD->getMetadata()) ||
725 isa<ValueAsMetadata>(MD->getMetadata()))
730 LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) {
731 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
732 if (isa<MDString>(MD->getMetadata()))
737 /*--.. Operations on Uses ..................................................--*/
738 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
739 Value *V = unwrap(Val);
740 Value::use_iterator I = V->use_begin();
741 if (I == V->use_end())
746 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
747 Use *Next = unwrap(U)->getNext();
753 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
754 return wrap(unwrap(U)->getUser());
757 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
758 return wrap(unwrap(U)->get());
761 /*--.. Operations on Users .................................................--*/
763 static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N,
765 Metadata *Op = N->getOperand(Index);
768 if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
769 return wrap(C->getValue());
770 return wrap(MetadataAsValue::get(Context, Op));
773 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
774 Value *V = unwrap(Val);
775 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
776 if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
777 assert(Index == 0 && "Function-local metadata can only have one operand");
778 return wrap(L->getValue());
780 return getMDNodeOperandImpl(V->getContext(),
781 cast<MDNode>(MD->getMetadata()), Index);
784 return wrap(cast<User>(V)->getOperand(Index));
787 LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) {
788 Value *V = unwrap(Val);
789 return wrap(&cast<User>(V)->getOperandUse(Index));
792 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
793 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
796 int LLVMGetNumOperands(LLVMValueRef Val) {
797 Value *V = unwrap(Val);
798 if (isa<MetadataAsValue>(V))
799 return LLVMGetMDNodeNumOperands(Val);
801 return cast<User>(V)->getNumOperands();
804 /*--.. Operations on constants of any type .................................--*/
806 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
807 return wrap(Constant::getNullValue(unwrap(Ty)));
810 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
811 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
814 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
815 return wrap(UndefValue::get(unwrap(Ty)));
818 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
819 return isa<Constant>(unwrap(Ty));
822 LLVMBool LLVMIsNull(LLVMValueRef Val) {
823 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
824 return C->isNullValue();
828 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
829 return isa<UndefValue>(unwrap(Val));
832 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
833 return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
836 /*--.. Operations on metadata nodes ........................................--*/
838 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
840 LLVMContext &Context = *unwrap(C);
841 return wrap(MetadataAsValue::get(
842 Context, MDString::get(Context, StringRef(Str, SLen))));
845 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
846 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
849 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
851 LLVMContext &Context = *unwrap(C);
852 SmallVector<Metadata *, 8> MDs;
853 for (auto *OV : makeArrayRef(Vals, Count)) {
854 Value *V = unwrap(OV);
858 else if (auto *C = dyn_cast<Constant>(V))
859 MD = ConstantAsMetadata::get(C);
860 else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
861 MD = MDV->getMetadata();
862 assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
863 "outside of direct argument to call");
865 // This is function-local metadata. Pretend to make an MDNode.
867 "Expected only one operand to function-local metadata");
868 return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V)));
873 return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs)));
876 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
877 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
880 LLVMValueRef LLVMMetadataAsValue(LLVMContextRef C, LLVMMetadataRef MD) {
881 return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
884 LLVMMetadataRef LLVMValueAsMetadata(LLVMValueRef Val) {
885 auto *V = unwrap(Val);
886 if (auto *C = dyn_cast<Constant>(V))
887 return wrap(ConstantAsMetadata::get(C));
888 if (auto *MAV = dyn_cast<MetadataAsValue>(V))
889 return wrap(MAV->getMetadata());
890 return wrap(ValueAsMetadata::get(V));
893 const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
894 if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
895 if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
896 *Length = S->getString().size();
897 return S->getString().data();
903 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) {
904 auto *MD = cast<MetadataAsValue>(unwrap(V));
905 if (isa<ValueAsMetadata>(MD->getMetadata()))
907 return cast<MDNode>(MD->getMetadata())->getNumOperands();
910 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) {
911 auto *MD = cast<MetadataAsValue>(unwrap(V));
912 if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
913 *Dest = wrap(MDV->getValue());
916 const auto *N = cast<MDNode>(MD->getMetadata());
917 const unsigned numOperands = N->getNumOperands();
918 LLVMContext &Context = unwrap(V)->getContext();
919 for (unsigned i = 0; i < numOperands; i++)
920 Dest[i] = getMDNodeOperandImpl(Context, N, i);
923 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char *Name) {
924 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
925 return N->getNumOperands();
930 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char *Name,
931 LLVMValueRef *Dest) {
932 NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
935 LLVMContext &Context = unwrap(M)->getContext();
936 for (unsigned i=0;i<N->getNumOperands();i++)
937 Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
940 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char *Name,
942 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
947 N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
950 /*--.. Operations on scalar constants ......................................--*/
952 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
953 LLVMBool SignExtend) {
954 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
957 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
959 const uint64_t Words[]) {
960 IntegerType *Ty = unwrap<IntegerType>(IntTy);
961 return wrap(ConstantInt::get(Ty->getContext(),
962 APInt(Ty->getBitWidth(),
963 makeArrayRef(Words, NumWords))));
966 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
968 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
972 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
973 unsigned SLen, uint8_t Radix) {
974 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
978 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
979 return wrap(ConstantFP::get(unwrap(RealTy), N));
982 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
983 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
986 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
988 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
991 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
992 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
995 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
996 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
999 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
1000 ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
1001 Type *Ty = cFP->getType();
1003 if (Ty->isFloatTy()) {
1005 return cFP->getValueAPF().convertToFloat();
1008 if (Ty->isDoubleTy()) {
1010 return cFP->getValueAPF().convertToDouble();
1014 APFloat APF = cFP->getValueAPF();
1015 APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo);
1016 *LosesInfo = APFLosesInfo;
1017 return APF.convertToDouble();
1020 /*--.. Operations on composite constants ...................................--*/
1022 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
1024 LLVMBool DontNullTerminate) {
1025 /* Inverted the sense of AddNull because ', 0)' is a
1026 better mnemonic for null termination than ', 1)'. */
1027 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
1028 DontNullTerminate == 0));
1031 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
1032 LLVMBool DontNullTerminate) {
1033 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
1037 LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef C, unsigned idx) {
1038 return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
1041 LLVMBool LLVMIsConstantString(LLVMValueRef C) {
1042 return unwrap<ConstantDataSequential>(C)->isString();
1045 const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1046 StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1047 *Length = Str.size();
1051 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
1052 LLVMValueRef *ConstantVals, unsigned Length) {
1053 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1054 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1057 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
1058 LLVMValueRef *ConstantVals,
1059 unsigned Count, LLVMBool Packed) {
1060 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1061 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
1065 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
1067 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1071 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
1072 LLVMValueRef *ConstantVals,
1074 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1075 StructType *Ty = cast<StructType>(unwrap(StructTy));
1077 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
1080 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
1081 return wrap(ConstantVector::get(makeArrayRef(
1082 unwrap<Constant>(ScalarConstantVals, Size), Size)));
1085 /*-- Opcode mapping */
1087 static LLVMOpcode map_to_llvmopcode(int opcode)
1090 default: llvm_unreachable("Unhandled Opcode.");
1091 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
1092 #include "llvm/IR/Instruction.def"
1097 static int map_from_llvmopcode(LLVMOpcode code)
1100 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
1101 #include "llvm/IR/Instruction.def"
1104 llvm_unreachable("Unhandled Opcode.");
1107 /*--.. Constant expressions ................................................--*/
1109 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
1110 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
1113 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
1114 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
1117 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
1118 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
1121 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
1122 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1125 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
1126 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
1129 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
1130 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
1134 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
1135 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
1138 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
1139 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
1142 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1143 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
1144 unwrap<Constant>(RHSConstant)));
1147 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
1148 LLVMValueRef RHSConstant) {
1149 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
1150 unwrap<Constant>(RHSConstant)));
1153 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
1154 LLVMValueRef RHSConstant) {
1155 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
1156 unwrap<Constant>(RHSConstant)));
1159 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1160 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
1161 unwrap<Constant>(RHSConstant)));
1164 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1165 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
1166 unwrap<Constant>(RHSConstant)));
1169 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
1170 LLVMValueRef RHSConstant) {
1171 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
1172 unwrap<Constant>(RHSConstant)));
1175 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
1176 LLVMValueRef RHSConstant) {
1177 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
1178 unwrap<Constant>(RHSConstant)));
1181 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1182 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
1183 unwrap<Constant>(RHSConstant)));
1186 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1187 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
1188 unwrap<Constant>(RHSConstant)));
1191 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
1192 LLVMValueRef RHSConstant) {
1193 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
1194 unwrap<Constant>(RHSConstant)));
1197 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
1198 LLVMValueRef RHSConstant) {
1199 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
1200 unwrap<Constant>(RHSConstant)));
1203 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1204 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
1205 unwrap<Constant>(RHSConstant)));
1208 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1209 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
1210 unwrap<Constant>(RHSConstant)));
1213 LLVMValueRef LLVMConstExactUDiv(LLVMValueRef LHSConstant,
1214 LLVMValueRef RHSConstant) {
1215 return wrap(ConstantExpr::getExactUDiv(unwrap<Constant>(LHSConstant),
1216 unwrap<Constant>(RHSConstant)));
1219 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1220 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
1221 unwrap<Constant>(RHSConstant)));
1224 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
1225 LLVMValueRef RHSConstant) {
1226 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
1227 unwrap<Constant>(RHSConstant)));
1230 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1231 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
1232 unwrap<Constant>(RHSConstant)));
1235 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1236 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
1237 unwrap<Constant>(RHSConstant)));
1240 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1241 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
1242 unwrap<Constant>(RHSConstant)));
1245 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1246 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
1247 unwrap<Constant>(RHSConstant)));
1250 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1251 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
1252 unwrap<Constant>(RHSConstant)));
1255 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1256 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
1257 unwrap<Constant>(RHSConstant)));
1260 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1261 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
1262 unwrap<Constant>(RHSConstant)));
1265 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
1266 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1267 return wrap(ConstantExpr::getICmp(Predicate,
1268 unwrap<Constant>(LHSConstant),
1269 unwrap<Constant>(RHSConstant)));
1272 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
1273 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1274 return wrap(ConstantExpr::getFCmp(Predicate,
1275 unwrap<Constant>(LHSConstant),
1276 unwrap<Constant>(RHSConstant)));
1279 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1280 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1281 unwrap<Constant>(RHSConstant)));
1284 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1285 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
1286 unwrap<Constant>(RHSConstant)));
1289 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1290 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1291 unwrap<Constant>(RHSConstant)));
1294 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
1295 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1296 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1298 return wrap(ConstantExpr::getGetElementPtr(
1299 nullptr, unwrap<Constant>(ConstantVal), IdxList));
1302 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
1303 LLVMValueRef *ConstantIndices,
1304 unsigned NumIndices) {
1305 Constant* Val = unwrap<Constant>(ConstantVal);
1306 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1308 return wrap(ConstantExpr::getInBoundsGetElementPtr(nullptr, Val, IdxList));
1311 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1312 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1316 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1317 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1321 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1322 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1326 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1327 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1331 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1332 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1336 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1337 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1341 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1342 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1346 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1347 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1351 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1352 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1356 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1357 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1361 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1362 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1366 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1367 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1371 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1372 LLVMTypeRef ToType) {
1373 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1377 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1378 LLVMTypeRef ToType) {
1379 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1383 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1384 LLVMTypeRef ToType) {
1385 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1389 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1390 LLVMTypeRef ToType) {
1391 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1395 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1396 LLVMTypeRef ToType) {
1397 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1401 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1402 LLVMBool isSigned) {
1403 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1404 unwrap(ToType), isSigned));
1407 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1408 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1412 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1413 LLVMValueRef ConstantIfTrue,
1414 LLVMValueRef ConstantIfFalse) {
1415 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1416 unwrap<Constant>(ConstantIfTrue),
1417 unwrap<Constant>(ConstantIfFalse)));
1420 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1421 LLVMValueRef IndexConstant) {
1422 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1423 unwrap<Constant>(IndexConstant)));
1426 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1427 LLVMValueRef ElementValueConstant,
1428 LLVMValueRef IndexConstant) {
1429 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1430 unwrap<Constant>(ElementValueConstant),
1431 unwrap<Constant>(IndexConstant)));
1434 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1435 LLVMValueRef VectorBConstant,
1436 LLVMValueRef MaskConstant) {
1437 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1438 unwrap<Constant>(VectorBConstant),
1439 unwrap<Constant>(MaskConstant)));
1442 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1444 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1445 makeArrayRef(IdxList, NumIdx)));
1448 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1449 LLVMValueRef ElementValueConstant,
1450 unsigned *IdxList, unsigned NumIdx) {
1451 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1452 unwrap<Constant>(ElementValueConstant),
1453 makeArrayRef(IdxList, NumIdx)));
1456 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1457 const char *Constraints,
1458 LLVMBool HasSideEffects,
1459 LLVMBool IsAlignStack) {
1460 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1461 Constraints, HasSideEffects, IsAlignStack));
1464 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1465 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1468 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1470 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1471 return wrap(unwrap<GlobalValue>(Global)->getParent());
1474 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1475 return unwrap<GlobalValue>(Global)->isDeclaration();
1478 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1479 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1480 case GlobalValue::ExternalLinkage:
1481 return LLVMExternalLinkage;
1482 case GlobalValue::AvailableExternallyLinkage:
1483 return LLVMAvailableExternallyLinkage;
1484 case GlobalValue::LinkOnceAnyLinkage:
1485 return LLVMLinkOnceAnyLinkage;
1486 case GlobalValue::LinkOnceODRLinkage:
1487 return LLVMLinkOnceODRLinkage;
1488 case GlobalValue::WeakAnyLinkage:
1489 return LLVMWeakAnyLinkage;
1490 case GlobalValue::WeakODRLinkage:
1491 return LLVMWeakODRLinkage;
1492 case GlobalValue::AppendingLinkage:
1493 return LLVMAppendingLinkage;
1494 case GlobalValue::InternalLinkage:
1495 return LLVMInternalLinkage;
1496 case GlobalValue::PrivateLinkage:
1497 return LLVMPrivateLinkage;
1498 case GlobalValue::ExternalWeakLinkage:
1499 return LLVMExternalWeakLinkage;
1500 case GlobalValue::CommonLinkage:
1501 return LLVMCommonLinkage;
1504 llvm_unreachable("Invalid GlobalValue linkage!");
1507 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1508 GlobalValue *GV = unwrap<GlobalValue>(Global);
1511 case LLVMExternalLinkage:
1512 GV->setLinkage(GlobalValue::ExternalLinkage);
1514 case LLVMAvailableExternallyLinkage:
1515 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1517 case LLVMLinkOnceAnyLinkage:
1518 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1520 case LLVMLinkOnceODRLinkage:
1521 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1523 case LLVMLinkOnceODRAutoHideLinkage:
1524 DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1525 "longer supported.");
1527 case LLVMWeakAnyLinkage:
1528 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1530 case LLVMWeakODRLinkage:
1531 GV->setLinkage(GlobalValue::WeakODRLinkage);
1533 case LLVMAppendingLinkage:
1534 GV->setLinkage(GlobalValue::AppendingLinkage);
1536 case LLVMInternalLinkage:
1537 GV->setLinkage(GlobalValue::InternalLinkage);
1539 case LLVMPrivateLinkage:
1540 GV->setLinkage(GlobalValue::PrivateLinkage);
1542 case LLVMLinkerPrivateLinkage:
1543 GV->setLinkage(GlobalValue::PrivateLinkage);
1545 case LLVMLinkerPrivateWeakLinkage:
1546 GV->setLinkage(GlobalValue::PrivateLinkage);
1548 case LLVMDLLImportLinkage:
1550 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1552 case LLVMDLLExportLinkage:
1554 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1556 case LLVMExternalWeakLinkage:
1557 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1559 case LLVMGhostLinkage:
1561 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1563 case LLVMCommonLinkage:
1564 GV->setLinkage(GlobalValue::CommonLinkage);
1569 const char *LLVMGetSection(LLVMValueRef Global) {
1570 // Using .data() is safe because of how GlobalObject::setSection is
1572 return unwrap<GlobalValue>(Global)->getSection().data();
1575 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1576 unwrap<GlobalObject>(Global)->setSection(Section);
1579 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1580 return static_cast<LLVMVisibility>(
1581 unwrap<GlobalValue>(Global)->getVisibility());
1584 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1585 unwrap<GlobalValue>(Global)
1586 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1589 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
1590 return static_cast<LLVMDLLStorageClass>(
1591 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1594 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
1595 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1596 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1599 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
1600 return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
1603 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
1604 unwrap<GlobalValue>(Global)->setUnnamedAddr(
1605 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
1606 : GlobalValue::UnnamedAddr::None);
1609 /*--.. Operations on global variables, load and store instructions .........--*/
1611 unsigned LLVMGetAlignment(LLVMValueRef V) {
1612 Value *P = unwrap<Value>(V);
1613 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1614 return GV->getAlignment();
1615 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1616 return AI->getAlignment();
1617 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1618 return LI->getAlignment();
1619 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1620 return SI->getAlignment();
1623 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1626 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1627 Value *P = unwrap<Value>(V);
1628 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
1629 GV->setAlignment(Bytes);
1630 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1631 AI->setAlignment(Bytes);
1632 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1633 LI->setAlignment(Bytes);
1634 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1635 SI->setAlignment(Bytes);
1638 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1641 /*--.. Operations on global variables ......................................--*/
1643 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1644 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1645 GlobalValue::ExternalLinkage, nullptr, Name));
1648 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1650 unsigned AddressSpace) {
1651 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1652 GlobalValue::ExternalLinkage, nullptr, Name,
1653 nullptr, GlobalVariable::NotThreadLocal,
1657 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1658 return wrap(unwrap(M)->getNamedGlobal(Name));
1661 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1662 Module *Mod = unwrap(M);
1663 Module::global_iterator I = Mod->global_begin();
1664 if (I == Mod->global_end())
1669 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1670 Module *Mod = unwrap(M);
1671 Module::global_iterator I = Mod->global_end();
1672 if (I == Mod->global_begin())
1677 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1678 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1679 Module::global_iterator I(GV);
1680 if (++I == GV->getParent()->global_end())
1685 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1686 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1687 Module::global_iterator I(GV);
1688 if (I == GV->getParent()->global_begin())
1693 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1694 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1697 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1698 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1699 if ( !GV->hasInitializer() )
1701 return wrap(GV->getInitializer());
1704 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1705 unwrap<GlobalVariable>(GlobalVar)
1706 ->setInitializer(unwrap<Constant>(ConstantVal));
1709 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1710 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1713 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1714 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1717 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1718 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1721 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1722 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1725 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1726 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1727 case GlobalVariable::NotThreadLocal:
1728 return LLVMNotThreadLocal;
1729 case GlobalVariable::GeneralDynamicTLSModel:
1730 return LLVMGeneralDynamicTLSModel;
1731 case GlobalVariable::LocalDynamicTLSModel:
1732 return LLVMLocalDynamicTLSModel;
1733 case GlobalVariable::InitialExecTLSModel:
1734 return LLVMInitialExecTLSModel;
1735 case GlobalVariable::LocalExecTLSModel:
1736 return LLVMLocalExecTLSModel;
1739 llvm_unreachable("Invalid GlobalVariable thread local mode");
1742 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1743 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1746 case LLVMNotThreadLocal:
1747 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1749 case LLVMGeneralDynamicTLSModel:
1750 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1752 case LLVMLocalDynamicTLSModel:
1753 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1755 case LLVMInitialExecTLSModel:
1756 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1758 case LLVMLocalExecTLSModel:
1759 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1764 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1765 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1768 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1769 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1772 /*--.. Operations on aliases ......................................--*/
1774 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1776 auto *PTy = cast<PointerType>(unwrap(Ty));
1777 return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
1778 GlobalValue::ExternalLinkage, Name,
1779 unwrap<Constant>(Aliasee), unwrap(M)));
1782 /*--.. Operations on functions .............................................--*/
1784 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1785 LLVMTypeRef FunctionTy) {
1786 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1787 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1790 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1791 return wrap(unwrap(M)->getFunction(Name));
1794 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1795 Module *Mod = unwrap(M);
1796 Module::iterator I = Mod->begin();
1797 if (I == Mod->end())
1802 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1803 Module *Mod = unwrap(M);
1804 Module::iterator I = Mod->end();
1805 if (I == Mod->begin())
1810 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1811 Function *Func = unwrap<Function>(Fn);
1812 Module::iterator I(Func);
1813 if (++I == Func->getParent()->end())
1818 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1819 Function *Func = unwrap<Function>(Fn);
1820 Module::iterator I(Func);
1821 if (I == Func->getParent()->begin())
1826 void LLVMDeleteFunction(LLVMValueRef Fn) {
1827 unwrap<Function>(Fn)->eraseFromParent();
1830 LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) {
1831 return unwrap<Function>(Fn)->hasPersonalityFn();
1834 LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) {
1835 return wrap(unwrap<Function>(Fn)->getPersonalityFn());
1838 void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) {
1839 unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
1842 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1843 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1844 return F->getIntrinsicID();
1848 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1849 return unwrap<Function>(Fn)->getCallingConv();
1852 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1853 return unwrap<Function>(Fn)->setCallingConv(
1854 static_cast<CallingConv::ID>(CC));
1857 const char *LLVMGetGC(LLVMValueRef Fn) {
1858 Function *F = unwrap<Function>(Fn);
1859 return F->hasGC()? F->getGC().c_str() : nullptr;
1862 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1863 Function *F = unwrap<Function>(Fn);
1870 void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1871 LLVMAttributeRef A) {
1872 unwrap<Function>(F)->addAttribute(Idx, unwrap(A));
1875 unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) {
1876 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
1877 return AS.getNumAttributes();
1880 void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1881 LLVMAttributeRef *Attrs) {
1882 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
1887 LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F,
1888 LLVMAttributeIndex Idx,
1890 return wrap(unwrap<Function>(F)->getAttribute(Idx,
1891 (Attribute::AttrKind)KindID));
1894 LLVMAttributeRef LLVMGetStringAttributeAtIndex(LLVMValueRef F,
1895 LLVMAttributeIndex Idx,
1896 const char *K, unsigned KLen) {
1897 return wrap(unwrap<Function>(F)->getAttribute(Idx, StringRef(K, KLen)));
1900 void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1902 unwrap<Function>(F)->removeAttribute(Idx, (Attribute::AttrKind)KindID);
1905 void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1906 const char *K, unsigned KLen) {
1907 unwrap<Function>(F)->removeAttribute(Idx, StringRef(K, KLen));
1910 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1912 Function *Func = unwrap<Function>(Fn);
1913 Attribute Attr = Attribute::get(Func->getContext(), A, V);
1914 Func->addAttribute(AttributeList::FunctionIndex, Attr);
1917 /*--.. Operations on parameters ............................................--*/
1919 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1920 // This function is strictly redundant to
1921 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1922 return unwrap<Function>(FnRef)->arg_size();
1925 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1926 Function *Fn = unwrap<Function>(FnRef);
1927 for (Function::arg_iterator I = Fn->arg_begin(),
1928 E = Fn->arg_end(); I != E; I++)
1929 *ParamRefs++ = wrap(&*I);
1932 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1933 Function *Fn = unwrap<Function>(FnRef);
1934 return wrap(&Fn->arg_begin()[index]);
1937 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1938 return wrap(unwrap<Argument>(V)->getParent());
1941 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1942 Function *Func = unwrap<Function>(Fn);
1943 Function::arg_iterator I = Func->arg_begin();
1944 if (I == Func->arg_end())
1949 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1950 Function *Func = unwrap<Function>(Fn);
1951 Function::arg_iterator I = Func->arg_end();
1952 if (I == Func->arg_begin())
1957 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1958 Argument *A = unwrap<Argument>(Arg);
1959 Function *Fn = A->getParent();
1960 if (A->getArgNo() + 1 >= Fn->arg_size())
1962 return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
1965 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1966 Argument *A = unwrap<Argument>(Arg);
1967 if (A->getArgNo() == 0)
1969 return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
1972 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1973 Argument *A = unwrap<Argument>(Arg);
1974 A->addAttr(Attribute::getWithAlignment(A->getContext(), align));
1977 /*--.. Operations on basic blocks ..........................................--*/
1979 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1980 return wrap(static_cast<Value*>(unwrap(BB)));
1983 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1984 return isa<BasicBlock>(unwrap(Val));
1987 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1988 return wrap(unwrap<BasicBlock>(Val));
1991 const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) {
1992 return unwrap(BB)->getName().data();
1995 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1996 return wrap(unwrap(BB)->getParent());
1999 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
2000 return wrap(unwrap(BB)->getTerminator());
2003 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
2004 return unwrap<Function>(FnRef)->size();
2007 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
2008 Function *Fn = unwrap<Function>(FnRef);
2009 for (BasicBlock &BB : *Fn)
2010 *BasicBlocksRefs++ = wrap(&BB);
2013 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
2014 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2017 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
2018 Function *Func = unwrap<Function>(Fn);
2019 Function::iterator I = Func->begin();
2020 if (I == Func->end())
2025 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
2026 Function *Func = unwrap<Function>(Fn);
2027 Function::iterator I = Func->end();
2028 if (I == Func->begin())
2033 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
2034 BasicBlock *Block = unwrap(BB);
2035 Function::iterator I(Block);
2036 if (++I == Block->getParent()->end())
2041 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
2042 BasicBlock *Block = unwrap(BB);
2043 Function::iterator I(Block);
2044 if (I == Block->getParent()->begin())
2049 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
2052 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
2055 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
2056 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
2059 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
2060 LLVMBasicBlockRef BBRef,
2062 BasicBlock *BB = unwrap(BBRef);
2063 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2066 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
2068 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
2071 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
2072 unwrap(BBRef)->eraseFromParent();
2075 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
2076 unwrap(BBRef)->removeFromParent();
2079 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2080 unwrap(BB)->moveBefore(unwrap(MovePos));
2083 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2084 unwrap(BB)->moveAfter(unwrap(MovePos));
2087 /*--.. Operations on instructions ..........................................--*/
2089 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
2090 return wrap(unwrap<Instruction>(Inst)->getParent());
2093 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
2094 BasicBlock *Block = unwrap(BB);
2095 BasicBlock::iterator I = Block->begin();
2096 if (I == Block->end())
2101 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
2102 BasicBlock *Block = unwrap(BB);
2103 BasicBlock::iterator I = Block->end();
2104 if (I == Block->begin())
2109 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
2110 Instruction *Instr = unwrap<Instruction>(Inst);
2111 BasicBlock::iterator I(Instr);
2112 if (++I == Instr->getParent()->end())
2117 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
2118 Instruction *Instr = unwrap<Instruction>(Inst);
2119 BasicBlock::iterator I(Instr);
2120 if (I == Instr->getParent()->begin())
2125 void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) {
2126 unwrap<Instruction>(Inst)->removeFromParent();
2129 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
2130 unwrap<Instruction>(Inst)->eraseFromParent();
2133 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
2134 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2135 return (LLVMIntPredicate)I->getPredicate();
2136 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2137 if (CE->getOpcode() == Instruction::ICmp)
2138 return (LLVMIntPredicate)CE->getPredicate();
2139 return (LLVMIntPredicate)0;
2142 LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
2143 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2144 return (LLVMRealPredicate)I->getPredicate();
2145 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2146 if (CE->getOpcode() == Instruction::FCmp)
2147 return (LLVMRealPredicate)CE->getPredicate();
2148 return (LLVMRealPredicate)0;
2151 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
2152 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2153 return map_to_llvmopcode(C->getOpcode());
2154 return (LLVMOpcode)0;
2157 LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {
2158 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2159 return wrap(C->clone());
2163 /*--.. Call and invoke instructions ........................................--*/
2165 unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) {
2166 return CallSite(unwrap<Instruction>(Instr)).getNumArgOperands();
2169 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
2170 return CallSite(unwrap<Instruction>(Instr)).getCallingConv();
2173 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
2174 return CallSite(unwrap<Instruction>(Instr))
2175 .setCallingConv(static_cast<CallingConv::ID>(CC));
2178 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
2180 CallSite Call = CallSite(unwrap<Instruction>(Instr));
2181 Attribute AlignAttr = Attribute::getWithAlignment(Call->getContext(), align);
2182 Call.addAttribute(index, AlignAttr);
2185 void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2186 LLVMAttributeRef A) {
2187 CallSite(unwrap<Instruction>(C)).addAttribute(Idx, unwrap(A));
2190 unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C,
2191 LLVMAttributeIndex Idx) {
2192 auto CS = CallSite(unwrap<Instruction>(C));
2193 auto AS = CS.getAttributes().getAttributes(Idx);
2194 return AS.getNumAttributes();
2197 void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx,
2198 LLVMAttributeRef *Attrs) {
2199 auto CS = CallSite(unwrap<Instruction>(C));
2200 auto AS = CS.getAttributes().getAttributes(Idx);
2205 LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C,
2206 LLVMAttributeIndex Idx,
2208 return wrap(CallSite(unwrap<Instruction>(C))
2209 .getAttribute(Idx, (Attribute::AttrKind)KindID));
2212 LLVMAttributeRef LLVMGetCallSiteStringAttribute(LLVMValueRef C,
2213 LLVMAttributeIndex Idx,
2214 const char *K, unsigned KLen) {
2215 return wrap(CallSite(unwrap<Instruction>(C))
2216 .getAttribute(Idx, StringRef(K, KLen)));
2219 void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2221 CallSite(unwrap<Instruction>(C))
2222 .removeAttribute(Idx, (Attribute::AttrKind)KindID);
2225 void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2226 const char *K, unsigned KLen) {
2227 CallSite(unwrap<Instruction>(C)).removeAttribute(Idx, StringRef(K, KLen));
2230 LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) {
2231 return wrap(CallSite(unwrap<Instruction>(Instr)).getCalledValue());
2234 /*--.. Operations on call instructions (only) ..............................--*/
2236 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
2237 return unwrap<CallInst>(Call)->isTailCall();
2240 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
2241 unwrap<CallInst>(Call)->setTailCall(isTailCall);
2244 /*--.. Operations on invoke instructions (only) ............................--*/
2246 LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) {
2247 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2250 LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) {
2251 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2254 void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2255 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2258 void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2259 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2262 /*--.. Operations on terminators ...........................................--*/
2264 unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {
2265 return unwrap<TerminatorInst>(Term)->getNumSuccessors();
2268 LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) {
2269 return wrap(unwrap<TerminatorInst>(Term)->getSuccessor(i));
2272 void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
2273 return unwrap<TerminatorInst>(Term)->setSuccessor(i,unwrap(block));
2276 /*--.. Operations on branch instructions (only) ............................--*/
2278 LLVMBool LLVMIsConditional(LLVMValueRef Branch) {
2279 return unwrap<BranchInst>(Branch)->isConditional();
2282 LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) {
2283 return wrap(unwrap<BranchInst>(Branch)->getCondition());
2286 void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) {
2287 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2290 /*--.. Operations on switch instructions (only) ............................--*/
2292 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
2293 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2296 /*--.. Operations on alloca instructions (only) ............................--*/
2298 LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) {
2299 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
2302 /*--.. Operations on gep instructions (only) ...............................--*/
2304 LLVMBool LLVMIsInBounds(LLVMValueRef GEP) {
2305 return unwrap<GetElementPtrInst>(GEP)->isInBounds();
2308 void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) {
2309 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
2312 /*--.. Operations on phi nodes .............................................--*/
2314 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
2315 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
2316 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
2317 for (unsigned I = 0; I != Count; ++I)
2318 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
2321 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
2322 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
2325 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
2326 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
2329 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
2330 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
2333 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/
2335 unsigned LLVMGetNumIndices(LLVMValueRef Inst) {
2336 auto *I = unwrap(Inst);
2337 if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
2338 return GEP->getNumIndices();
2339 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2340 return EV->getNumIndices();
2341 if (auto *IV = dyn_cast<InsertValueInst>(I))
2342 return IV->getNumIndices();
2344 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
2347 const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
2348 auto *I = unwrap(Inst);
2349 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2350 return EV->getIndices().data();
2351 if (auto *IV = dyn_cast<InsertValueInst>(I))
2352 return IV->getIndices().data();
2354 "LLVMGetIndices applies only to extractvalue and insertvalue!");
2358 /*===-- Instruction builders ----------------------------------------------===*/
2360 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
2361 return wrap(new IRBuilder<>(*unwrap(C)));
2364 LLVMBuilderRef LLVMCreateBuilder(void) {
2365 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
2368 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
2369 LLVMValueRef Instr) {
2370 BasicBlock *BB = unwrap(Block);
2371 auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
2372 unwrap(Builder)->SetInsertPoint(BB, I);
2375 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2376 Instruction *I = unwrap<Instruction>(Instr);
2377 unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
2380 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
2381 BasicBlock *BB = unwrap(Block);
2382 unwrap(Builder)->SetInsertPoint(BB);
2385 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
2386 return wrap(unwrap(Builder)->GetInsertBlock());
2389 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
2390 unwrap(Builder)->ClearInsertionPoint();
2393 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2394 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
2397 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
2399 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
2402 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
2403 delete unwrap(Builder);
2406 /*--.. Metadata builders ...................................................--*/
2408 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
2410 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
2411 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
2414 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
2415 LLVMContext &Context = unwrap(Builder)->getContext();
2416 return wrap(MetadataAsValue::get(
2417 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
2420 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
2421 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
2425 /*--.. Instruction builders ................................................--*/
2427 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
2428 return wrap(unwrap(B)->CreateRetVoid());
2431 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
2432 return wrap(unwrap(B)->CreateRet(unwrap(V)));
2435 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
2437 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
2440 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
2441 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
2444 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
2445 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
2446 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
2449 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
2450 LLVMBasicBlockRef Else, unsigned NumCases) {
2451 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
2454 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
2455 unsigned NumDests) {
2456 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
2459 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
2460 LLVMValueRef *Args, unsigned NumArgs,
2461 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
2463 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
2464 makeArrayRef(unwrap(Args), NumArgs),
2468 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
2469 LLVMValueRef PersFn, unsigned NumClauses,
2471 // The personality used to live on the landingpad instruction, but now it
2472 // lives on the parent function. For compatibility, take the provided
2473 // personality and put it on the parent function.
2475 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
2476 cast<Function>(unwrap(PersFn)));
2477 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
2480 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
2481 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
2484 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
2485 return wrap(unwrap(B)->CreateUnreachable());
2488 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
2489 LLVMBasicBlockRef Dest) {
2490 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
2493 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
2494 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
2497 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
2498 return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
2501 LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
2502 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
2505 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
2506 unwrap<LandingPadInst>(LandingPad)->
2507 addClause(cast<Constant>(unwrap(ClauseVal)));
2510 LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) {
2511 return unwrap<LandingPadInst>(LandingPad)->isCleanup();
2514 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
2515 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2518 /*--.. Arithmetic ..........................................................--*/
2520 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2522 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2525 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2527 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2530 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2532 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2535 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2537 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2540 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2542 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2545 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2547 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2550 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2552 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2555 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2557 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2560 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2562 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2565 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2567 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2570 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2572 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2575 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2577 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2580 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2582 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2585 LLVMValueRef LLVMBuildExactUDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2586 LLVMValueRef RHS, const char *Name) {
2587 return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
2590 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2592 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2595 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2596 LLVMValueRef RHS, const char *Name) {
2597 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2600 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2602 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2605 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2607 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2610 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2612 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2615 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2617 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2620 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2622 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2625 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2627 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2630 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2632 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2635 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2637 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2640 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2642 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2645 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2647 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2650 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2651 LLVMValueRef LHS, LLVMValueRef RHS,
2653 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2654 unwrap(RHS), Name));
2657 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2658 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2661 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2663 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2666 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2668 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2671 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2672 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2675 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2676 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2679 /*--.. Memory ..............................................................--*/
2681 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2683 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2684 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2685 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2686 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2687 ITy, unwrap(Ty), AllocSize,
2688 nullptr, nullptr, "");
2689 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2692 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2693 LLVMValueRef Val, const char *Name) {
2694 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2695 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2696 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2697 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2698 ITy, unwrap(Ty), AllocSize,
2699 unwrap(Val), nullptr, "");
2700 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2703 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2705 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
2708 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2709 LLVMValueRef Val, const char *Name) {
2710 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2713 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2714 return wrap(unwrap(B)->Insert(
2715 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2718 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2720 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2723 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2724 LLVMValueRef PointerVal) {
2725 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2728 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
2730 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic;
2731 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered;
2732 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic;
2733 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire;
2734 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release;
2735 case LLVMAtomicOrderingAcquireRelease:
2736 return AtomicOrdering::AcquireRelease;
2737 case LLVMAtomicOrderingSequentiallyConsistent:
2738 return AtomicOrdering::SequentiallyConsistent;
2741 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
2744 static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) {
2746 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic;
2747 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered;
2748 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic;
2749 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire;
2750 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease;
2751 case AtomicOrdering::AcquireRelease:
2752 return LLVMAtomicOrderingAcquireRelease;
2753 case AtomicOrdering::SequentiallyConsistent:
2754 return LLVMAtomicOrderingSequentiallyConsistent;
2757 llvm_unreachable("Invalid AtomicOrdering value!");
2760 // TODO: Should this and other atomic instructions support building with
2762 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
2763 LLVMBool isSingleThread, const char *Name) {
2765 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
2766 isSingleThread ? SyncScope::SingleThread
2767 : SyncScope::System,
2771 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2772 LLVMValueRef *Indices, unsigned NumIndices,
2774 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2775 return wrap(unwrap(B)->CreateGEP(nullptr, unwrap(Pointer), IdxList, Name));
2778 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2779 LLVMValueRef *Indices, unsigned NumIndices,
2781 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2783 unwrap(B)->CreateInBoundsGEP(nullptr, unwrap(Pointer), IdxList, Name));
2786 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2787 unsigned Idx, const char *Name) {
2788 return wrap(unwrap(B)->CreateStructGEP(nullptr, unwrap(Pointer), Idx, Name));
2791 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2793 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2796 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2798 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2801 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2802 Value *P = unwrap<Value>(MemAccessInst);
2803 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2804 return LI->isVolatile();
2805 return cast<StoreInst>(P)->isVolatile();
2808 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2809 Value *P = unwrap<Value>(MemAccessInst);
2810 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2811 return LI->setVolatile(isVolatile);
2812 return cast<StoreInst>(P)->setVolatile(isVolatile);
2815 LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) {
2816 Value *P = unwrap<Value>(MemAccessInst);
2818 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2819 O = LI->getOrdering();
2821 O = cast<StoreInst>(P)->getOrdering();
2822 return mapToLLVMOrdering(O);
2825 void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
2826 Value *P = unwrap<Value>(MemAccessInst);
2827 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
2829 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2830 return LI->setOrdering(O);
2831 return cast<StoreInst>(P)->setOrdering(O);
2834 /*--.. Casts ...............................................................--*/
2836 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2837 LLVMTypeRef DestTy, const char *Name) {
2838 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2841 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2842 LLVMTypeRef DestTy, const char *Name) {
2843 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2846 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2847 LLVMTypeRef DestTy, const char *Name) {
2848 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2851 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2852 LLVMTypeRef DestTy, const char *Name) {
2853 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2856 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2857 LLVMTypeRef DestTy, const char *Name) {
2858 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2861 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2862 LLVMTypeRef DestTy, const char *Name) {
2863 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2866 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2867 LLVMTypeRef DestTy, const char *Name) {
2868 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2871 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2872 LLVMTypeRef DestTy, const char *Name) {
2873 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2876 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2877 LLVMTypeRef DestTy, const char *Name) {
2878 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2881 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2882 LLVMTypeRef DestTy, const char *Name) {
2883 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2886 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2887 LLVMTypeRef DestTy, const char *Name) {
2888 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2891 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2892 LLVMTypeRef DestTy, const char *Name) {
2893 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2896 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
2897 LLVMTypeRef DestTy, const char *Name) {
2898 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
2901 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2902 LLVMTypeRef DestTy, const char *Name) {
2903 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2907 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2908 LLVMTypeRef DestTy, const char *Name) {
2909 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2913 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2914 LLVMTypeRef DestTy, const char *Name) {
2915 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2919 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2920 LLVMTypeRef DestTy, const char *Name) {
2921 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2922 unwrap(DestTy), Name));
2925 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2926 LLVMTypeRef DestTy, const char *Name) {
2927 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2930 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2931 LLVMTypeRef DestTy, const char *Name) {
2932 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2933 /*isSigned*/true, Name));
2936 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2937 LLVMTypeRef DestTy, const char *Name) {
2938 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2941 /*--.. Comparisons .........................................................--*/
2943 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2944 LLVMValueRef LHS, LLVMValueRef RHS,
2946 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2947 unwrap(LHS), unwrap(RHS), Name));
2950 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2951 LLVMValueRef LHS, LLVMValueRef RHS,
2953 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2954 unwrap(LHS), unwrap(RHS), Name));
2957 /*--.. Miscellaneous instructions ..........................................--*/
2959 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2960 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2963 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2964 LLVMValueRef *Args, unsigned NumArgs,
2966 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2967 makeArrayRef(unwrap(Args), NumArgs),
2971 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2972 LLVMValueRef Then, LLVMValueRef Else,
2974 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2978 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2979 LLVMTypeRef Ty, const char *Name) {
2980 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2983 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2984 LLVMValueRef Index, const char *Name) {
2985 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2989 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2990 LLVMValueRef EltVal, LLVMValueRef Index,
2992 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2993 unwrap(Index), Name));
2996 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2997 LLVMValueRef V2, LLVMValueRef Mask,
2999 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
3000 unwrap(Mask), Name));
3003 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
3004 unsigned Index, const char *Name) {
3005 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
3008 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
3009 LLVMValueRef EltVal, unsigned Index,
3011 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
3015 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
3017 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
3020 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
3022 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
3025 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
3026 LLVMValueRef RHS, const char *Name) {
3027 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
3030 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
3031 LLVMValueRef PTR, LLVMValueRef Val,
3032 LLVMAtomicOrdering ordering,
3033 LLVMBool singleThread) {
3034 AtomicRMWInst::BinOp intop;
3036 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
3037 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
3038 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
3039 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
3040 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
3041 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
3042 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
3043 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
3044 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
3045 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
3046 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
3048 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
3049 mapFromLLVMOrdering(ordering), singleThread ? SyncScope::SingleThread
3050 : SyncScope::System));
3053 LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr,
3054 LLVMValueRef Cmp, LLVMValueRef New,
3055 LLVMAtomicOrdering SuccessOrdering,
3056 LLVMAtomicOrdering FailureOrdering,
3057 LLVMBool singleThread) {
3059 return wrap(unwrap(B)->CreateAtomicCmpXchg(unwrap(Ptr), unwrap(Cmp),
3060 unwrap(New), mapFromLLVMOrdering(SuccessOrdering),
3061 mapFromLLVMOrdering(FailureOrdering),
3062 singleThread ? SyncScope::SingleThread : SyncScope::System));
3066 LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) {
3067 Value *P = unwrap<Value>(AtomicInst);
3069 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
3070 return I->getSyncScopeID() == SyncScope::SingleThread;
3071 return cast<AtomicCmpXchgInst>(P)->getSyncScopeID() ==
3072 SyncScope::SingleThread;
3075 void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) {
3076 Value *P = unwrap<Value>(AtomicInst);
3077 SyncScope::ID SSID = NewValue ? SyncScope::SingleThread : SyncScope::System;
3079 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
3080 return I->setSyncScopeID(SSID);
3081 return cast<AtomicCmpXchgInst>(P)->setSyncScopeID(SSID);
3084 LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) {
3085 Value *P = unwrap<Value>(CmpXchgInst);
3086 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering());
3089 void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst,
3090 LLVMAtomicOrdering Ordering) {
3091 Value *P = unwrap<Value>(CmpXchgInst);
3092 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3094 return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O);
3097 LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst) {
3098 Value *P = unwrap<Value>(CmpXchgInst);
3099 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering());
3102 void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst,
3103 LLVMAtomicOrdering Ordering) {
3104 Value *P = unwrap<Value>(CmpXchgInst);
3105 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3107 return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O);
3110 /*===-- Module providers --------------------------------------------------===*/
3112 LLVMModuleProviderRef
3113 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
3114 return reinterpret_cast<LLVMModuleProviderRef>(M);
3117 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
3122 /*===-- Memory buffers ----------------------------------------------------===*/
3124 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
3126 LLVMMemoryBufferRef *OutMemBuf,
3127 char **OutMessage) {
3129 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
3130 if (std::error_code EC = MBOrErr.getError()) {
3131 *OutMessage = strdup(EC.message().c_str());
3134 *OutMemBuf = wrap(MBOrErr.get().release());
3138 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
3139 char **OutMessage) {
3140 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
3141 if (std::error_code EC = MBOrErr.getError()) {
3142 *OutMessage = strdup(EC.message().c_str());
3145 *OutMemBuf = wrap(MBOrErr.get().release());
3149 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
3150 const char *InputData,
3151 size_t InputDataLength,
3152 const char *BufferName,
3153 LLVMBool RequiresNullTerminator) {
3155 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
3156 StringRef(BufferName),
3157 RequiresNullTerminator).release());
3160 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
3161 const char *InputData,
3162 size_t InputDataLength,
3163 const char *BufferName) {
3166 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
3167 StringRef(BufferName)).release());
3170 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
3171 return unwrap(MemBuf)->getBufferStart();
3174 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
3175 return unwrap(MemBuf)->getBufferSize();
3178 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
3179 delete unwrap(MemBuf);
3182 /*===-- Pass Registry -----------------------------------------------------===*/
3184 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
3185 return wrap(PassRegistry::getPassRegistry());
3188 /*===-- Pass Manager ------------------------------------------------------===*/
3190 LLVMPassManagerRef LLVMCreatePassManager() {
3191 return wrap(new legacy::PassManager());
3194 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
3195 return wrap(new legacy::FunctionPassManager(unwrap(M)));
3198 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
3199 return LLVMCreateFunctionPassManagerForModule(
3200 reinterpret_cast<LLVMModuleRef>(P));
3203 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
3204 return unwrap<legacy::PassManager>(PM)->run(*unwrap(M));
3207 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
3208 return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization();
3211 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
3212 return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
3215 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
3216 return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization();
3219 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
3223 /*===-- Threading ------------------------------------------------------===*/
3225 LLVMBool LLVMStartMultithreaded() {
3226 return LLVMIsMultithreaded();
3229 void LLVMStopMultithreaded() {
3232 LLVMBool LLVMIsMultithreaded() {
3233 return llvm_is_multithreaded();