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/Bitcode/BitcodeReader.h"
18 #include "llvm/IR/Attributes.h"
19 #include "AttributeSetNode.h"
20 #include "llvm/IR/CallSite.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/DerivedTypes.h"
23 #include "llvm/IR/DiagnosticInfo.h"
24 #include "llvm/IR/DiagnosticPrinter.h"
25 #include "llvm/IR/GlobalAlias.h"
26 #include "llvm/IR/GlobalVariable.h"
27 #include "llvm/IR/IRBuilder.h"
28 #include "llvm/IR/InlineAsm.h"
29 #include "llvm/IR/IntrinsicInst.h"
30 #include "llvm/IR/LLVMContext.h"
31 #include "llvm/IR/LegacyPassManager.h"
32 #include "llvm/IR/Module.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/FileSystem.h"
36 #include "llvm/Support/ManagedStatic.h"
37 #include "llvm/Support/MemoryBuffer.h"
38 #include "llvm/Support/Threading.h"
39 #include "llvm/Support/raw_ostream.h"
43 #include <system_error>
47 #define DEBUG_TYPE "ir"
49 void llvm::initializeCore(PassRegistry &Registry) {
50 initializeDominatorTreeWrapperPassPass(Registry);
51 initializePrintModulePassWrapperPass(Registry);
52 initializePrintFunctionPassWrapperPass(Registry);
53 initializePrintBasicBlockPassPass(Registry);
54 initializeVerifierLegacyPassPass(Registry);
57 void LLVMInitializeCore(LLVMPassRegistryRef R) {
58 initializeCore(*unwrap(R));
65 /*===-- Error handling ----------------------------------------------------===*/
67 char *LLVMCreateMessage(const char *Message) {
68 return strdup(Message);
71 void LLVMDisposeMessage(char *Message) {
76 /*===-- Operations on contexts --------------------------------------------===*/
78 static ManagedStatic<LLVMContext> GlobalContext;
80 LLVMContextRef LLVMContextCreate() {
81 return wrap(new LLVMContext());
84 LLVMContextRef LLVMGetGlobalContext() { return wrap(&*GlobalContext); }
86 void LLVMContextSetDiagnosticHandler(LLVMContextRef C,
87 LLVMDiagnosticHandler Handler,
88 void *DiagnosticContext) {
89 unwrap(C)->setDiagnosticHandler(
90 LLVM_EXTENSION reinterpret_cast<LLVMContext::DiagnosticHandlerTy>(
95 LLVMDiagnosticHandler LLVMContextGetDiagnosticHandler(LLVMContextRef C) {
96 return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>(
97 unwrap(C)->getDiagnosticHandler());
100 void *LLVMContextGetDiagnosticContext(LLVMContextRef C) {
101 return unwrap(C)->getDiagnosticContext();
104 void LLVMContextSetYieldCallback(LLVMContextRef C, LLVMYieldCallback Callback,
105 void *OpaqueHandle) {
107 LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
108 unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
111 void LLVMContextDispose(LLVMContextRef C) {
115 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char *Name,
117 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
120 unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) {
121 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
124 #define GET_ATTR_KIND_FROM_NAME
125 #include "AttributesCompatFunc.inc"
127 unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) {
128 return getAttrKindFromName(StringRef(Name, SLen));
131 unsigned LLVMGetLastEnumAttributeKind(void) {
132 return Attribute::AttrKind::EndAttrKinds;
135 LLVMAttributeRef LLVMCreateEnumAttribute(LLVMContextRef C, unsigned KindID,
137 return wrap(Attribute::get(*unwrap(C), (Attribute::AttrKind)KindID, Val));
140 unsigned LLVMGetEnumAttributeKind(LLVMAttributeRef A) {
141 return unwrap(A).getKindAsEnum();
144 uint64_t LLVMGetEnumAttributeValue(LLVMAttributeRef A) {
145 auto Attr = unwrap(A);
146 if (Attr.isEnumAttribute())
148 return Attr.getValueAsInt();
151 LLVMAttributeRef LLVMCreateStringAttribute(LLVMContextRef C,
152 const char *K, unsigned KLength,
153 const char *V, unsigned VLength) {
154 return wrap(Attribute::get(*unwrap(C), StringRef(K, KLength),
155 StringRef(V, VLength)));
158 const char *LLVMGetStringAttributeKind(LLVMAttributeRef A,
160 auto S = unwrap(A).getKindAsString();
165 const char *LLVMGetStringAttributeValue(LLVMAttributeRef A,
167 auto S = unwrap(A).getValueAsString();
172 LLVMBool LLVMIsEnumAttribute(LLVMAttributeRef A) {
173 auto Attr = unwrap(A);
174 return Attr.isEnumAttribute() || Attr.isIntAttribute();
177 LLVMBool LLVMIsStringAttribute(LLVMAttributeRef A) {
178 return unwrap(A).isStringAttribute();
181 char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) {
182 std::string MsgStorage;
183 raw_string_ostream Stream(MsgStorage);
184 DiagnosticPrinterRawOStream DP(Stream);
186 unwrap(DI)->print(DP);
189 return LLVMCreateMessage(MsgStorage.c_str());
192 LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI) {
193 LLVMDiagnosticSeverity severity;
195 switch(unwrap(DI)->getSeverity()) {
197 severity = LLVMDSError;
200 severity = LLVMDSWarning;
203 severity = LLVMDSRemark;
206 severity = LLVMDSNote;
213 /*===-- Operations on modules ---------------------------------------------===*/
215 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
216 return wrap(new Module(ModuleID, *GlobalContext));
219 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
221 return wrap(new Module(ModuleID, *unwrap(C)));
224 void LLVMDisposeModule(LLVMModuleRef M) {
228 const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) {
229 auto &Str = unwrap(M)->getModuleIdentifier();
234 void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) {
235 unwrap(M)->setModuleIdentifier(StringRef(Ident, Len));
239 /*--.. Data layout .........................................................--*/
240 const char *LLVMGetDataLayoutStr(LLVMModuleRef M) {
241 return unwrap(M)->getDataLayoutStr().c_str();
244 const char *LLVMGetDataLayout(LLVMModuleRef M) {
245 return LLVMGetDataLayoutStr(M);
248 void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
249 unwrap(M)->setDataLayout(DataLayoutStr);
252 /*--.. Target triple .......................................................--*/
253 const char * LLVMGetTarget(LLVMModuleRef M) {
254 return unwrap(M)->getTargetTriple().c_str();
257 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
258 unwrap(M)->setTargetTriple(Triple);
261 void LLVMDumpModule(LLVMModuleRef M) {
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 *ErrorMessage = strdup("Error printing to file");
286 char *LLVMPrintModuleToString(LLVMModuleRef M) {
288 raw_string_ostream os(buf);
290 unwrap(M)->print(os, nullptr);
293 return strdup(buf.c_str());
296 /*--.. Operations on inline assembler ......................................--*/
297 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
298 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
302 /*--.. Operations on module contexts ......................................--*/
303 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
304 return wrap(&unwrap(M)->getContext());
308 /*===-- Operations on types -----------------------------------------------===*/
310 /*--.. Operations on all types (mostly) ....................................--*/
312 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
313 switch (unwrap(Ty)->getTypeID()) {
315 return LLVMVoidTypeKind;
317 return LLVMHalfTypeKind;
318 case Type::FloatTyID:
319 return LLVMFloatTypeKind;
320 case Type::DoubleTyID:
321 return LLVMDoubleTypeKind;
322 case Type::X86_FP80TyID:
323 return LLVMX86_FP80TypeKind;
324 case Type::FP128TyID:
325 return LLVMFP128TypeKind;
326 case Type::PPC_FP128TyID:
327 return LLVMPPC_FP128TypeKind;
328 case Type::LabelTyID:
329 return LLVMLabelTypeKind;
330 case Type::MetadataTyID:
331 return LLVMMetadataTypeKind;
332 case Type::IntegerTyID:
333 return LLVMIntegerTypeKind;
334 case Type::FunctionTyID:
335 return LLVMFunctionTypeKind;
336 case Type::StructTyID:
337 return LLVMStructTypeKind;
338 case Type::ArrayTyID:
339 return LLVMArrayTypeKind;
340 case Type::PointerTyID:
341 return LLVMPointerTypeKind;
342 case Type::VectorTyID:
343 return LLVMVectorTypeKind;
344 case Type::X86_MMXTyID:
345 return LLVMX86_MMXTypeKind;
346 case Type::TokenTyID:
347 return LLVMTokenTypeKind;
349 llvm_unreachable("Unhandled TypeID.");
352 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
354 return unwrap(Ty)->isSized();
357 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
358 return wrap(&unwrap(Ty)->getContext());
361 void LLVMDumpType(LLVMTypeRef Ty) {
362 return unwrap(Ty)->dump();
365 char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
367 raw_string_ostream os(buf);
370 unwrap(Ty)->print(os);
372 os << "Printing <null> Type";
376 return strdup(buf.c_str());
379 /*--.. Operations on integer types .........................................--*/
381 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
382 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
384 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
385 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
387 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
388 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
390 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
391 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
393 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
394 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
396 LLVMTypeRef LLVMInt128TypeInContext(LLVMContextRef C) {
397 return (LLVMTypeRef) Type::getInt128Ty(*unwrap(C));
399 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
400 return wrap(IntegerType::get(*unwrap(C), NumBits));
403 LLVMTypeRef LLVMInt1Type(void) {
404 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
406 LLVMTypeRef LLVMInt8Type(void) {
407 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
409 LLVMTypeRef LLVMInt16Type(void) {
410 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
412 LLVMTypeRef LLVMInt32Type(void) {
413 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
415 LLVMTypeRef LLVMInt64Type(void) {
416 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
418 LLVMTypeRef LLVMInt128Type(void) {
419 return LLVMInt128TypeInContext(LLVMGetGlobalContext());
421 LLVMTypeRef LLVMIntType(unsigned NumBits) {
422 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
425 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
426 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
429 /*--.. Operations on real types ............................................--*/
431 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
432 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
434 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
435 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
437 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
438 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
440 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
441 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
443 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
444 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
446 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
447 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
449 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
450 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
452 LLVMTypeRef LLVMTokenTypeInContext(LLVMContextRef C) {
453 return (LLVMTypeRef) Type::getTokenTy(*unwrap(C));
456 LLVMTypeRef LLVMHalfType(void) {
457 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
459 LLVMTypeRef LLVMFloatType(void) {
460 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
462 LLVMTypeRef LLVMDoubleType(void) {
463 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
465 LLVMTypeRef LLVMX86FP80Type(void) {
466 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
468 LLVMTypeRef LLVMFP128Type(void) {
469 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
471 LLVMTypeRef LLVMPPCFP128Type(void) {
472 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
474 LLVMTypeRef LLVMX86MMXType(void) {
475 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
478 /*--.. Operations on function types ........................................--*/
480 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
481 LLVMTypeRef *ParamTypes, unsigned ParamCount,
483 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
484 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
487 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
488 return unwrap<FunctionType>(FunctionTy)->isVarArg();
491 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
492 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
495 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
496 return unwrap<FunctionType>(FunctionTy)->getNumParams();
499 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
500 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
501 for (FunctionType::param_iterator I = Ty->param_begin(),
502 E = Ty->param_end(); I != E; ++I)
506 /*--.. Operations on struct types ..........................................--*/
508 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
509 unsigned ElementCount, LLVMBool Packed) {
510 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
511 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
514 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
515 unsigned ElementCount, LLVMBool Packed) {
516 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
517 ElementCount, Packed);
520 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
522 return wrap(StructType::create(*unwrap(C), Name));
525 const char *LLVMGetStructName(LLVMTypeRef Ty)
527 StructType *Type = unwrap<StructType>(Ty);
528 if (!Type->hasName())
530 return Type->getName().data();
533 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
534 unsigned ElementCount, LLVMBool Packed) {
535 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
536 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
539 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
540 return unwrap<StructType>(StructTy)->getNumElements();
543 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
544 StructType *Ty = unwrap<StructType>(StructTy);
545 for (StructType::element_iterator I = Ty->element_begin(),
546 E = Ty->element_end(); I != E; ++I)
550 LLVMTypeRef LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy, unsigned i) {
551 StructType *Ty = unwrap<StructType>(StructTy);
552 return wrap(Ty->getTypeAtIndex(i));
555 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
556 return unwrap<StructType>(StructTy)->isPacked();
559 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
560 return unwrap<StructType>(StructTy)->isOpaque();
563 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
564 return wrap(unwrap(M)->getTypeByName(Name));
567 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
569 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
570 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
573 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
574 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
577 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
578 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
581 LLVMTypeRef LLVMGetElementType(LLVMTypeRef WrappedTy) {
582 auto *Ty = unwrap<Type>(WrappedTy);
583 if (auto *PTy = dyn_cast<PointerType>(Ty))
584 return wrap(PTy->getElementType());
585 return wrap(cast<SequentialType>(Ty)->getElementType());
588 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
589 return unwrap<ArrayType>(ArrayTy)->getNumElements();
592 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
593 return unwrap<PointerType>(PointerTy)->getAddressSpace();
596 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
597 return unwrap<VectorType>(VectorTy)->getNumElements();
600 /*--.. Operations on other types ...........................................--*/
602 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
603 return wrap(Type::getVoidTy(*unwrap(C)));
605 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
606 return wrap(Type::getLabelTy(*unwrap(C)));
609 LLVMTypeRef LLVMVoidType(void) {
610 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
612 LLVMTypeRef LLVMLabelType(void) {
613 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
616 /*===-- Operations on values ----------------------------------------------===*/
618 /*--.. Operations on all values ............................................--*/
620 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
621 return wrap(unwrap(Val)->getType());
624 LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) {
625 switch(unwrap(Val)->getValueID()) {
626 #define HANDLE_VALUE(Name) \
627 case Value::Name##Val: \
628 return LLVM##Name##ValueKind;
629 #include "llvm/IR/Value.def"
631 return LLVMInstructionValueKind;
635 const char *LLVMGetValueName(LLVMValueRef Val) {
636 return unwrap(Val)->getName().data();
639 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
640 unwrap(Val)->setName(Name);
643 void LLVMDumpValue(LLVMValueRef Val) {
647 char* LLVMPrintValueToString(LLVMValueRef Val) {
649 raw_string_ostream os(buf);
652 unwrap(Val)->print(os);
654 os << "Printing <null> Value";
658 return strdup(buf.c_str());
661 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
662 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
665 int LLVMHasMetadata(LLVMValueRef Inst) {
666 return unwrap<Instruction>(Inst)->hasMetadata();
669 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
670 auto *I = unwrap<Instruction>(Inst);
671 assert(I && "Expected instruction");
672 if (auto *MD = I->getMetadata(KindID))
673 return wrap(MetadataAsValue::get(I->getContext(), MD));
677 // MetadataAsValue uses a canonical format which strips the actual MDNode for
678 // MDNode with just a single constant value, storing just a ConstantAsMetadata
679 // This undoes this canonicalization, reconstructing the MDNode.
680 static MDNode *extractMDNode(MetadataAsValue *MAV) {
681 Metadata *MD = MAV->getMetadata();
682 assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
683 "Expected a metadata node or a canonicalized constant");
685 if (MDNode *N = dyn_cast<MDNode>(MD))
688 return MDNode::get(MAV->getContext(), MD);
691 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
692 MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
694 unwrap<Instruction>(Inst)->setMetadata(KindID, N);
697 /*--.. Conversion functions ................................................--*/
699 #define LLVM_DEFINE_VALUE_CAST(name) \
700 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
701 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
704 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
706 LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) {
707 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
708 if (isa<MDNode>(MD->getMetadata()) ||
709 isa<ValueAsMetadata>(MD->getMetadata()))
714 LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) {
715 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
716 if (isa<MDString>(MD->getMetadata()))
721 /*--.. Operations on Uses ..................................................--*/
722 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
723 Value *V = unwrap(Val);
724 Value::use_iterator I = V->use_begin();
725 if (I == V->use_end())
730 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
731 Use *Next = unwrap(U)->getNext();
737 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
738 return wrap(unwrap(U)->getUser());
741 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
742 return wrap(unwrap(U)->get());
745 /*--.. Operations on Users .................................................--*/
747 static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N,
749 Metadata *Op = N->getOperand(Index);
752 if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
753 return wrap(C->getValue());
754 return wrap(MetadataAsValue::get(Context, Op));
757 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
758 Value *V = unwrap(Val);
759 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
760 if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
761 assert(Index == 0 && "Function-local metadata can only have one operand");
762 return wrap(L->getValue());
764 return getMDNodeOperandImpl(V->getContext(),
765 cast<MDNode>(MD->getMetadata()), Index);
768 return wrap(cast<User>(V)->getOperand(Index));
771 LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) {
772 Value *V = unwrap(Val);
773 return wrap(&cast<User>(V)->getOperandUse(Index));
776 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
777 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
780 int LLVMGetNumOperands(LLVMValueRef Val) {
781 Value *V = unwrap(Val);
782 if (isa<MetadataAsValue>(V))
783 return LLVMGetMDNodeNumOperands(Val);
785 return cast<User>(V)->getNumOperands();
788 /*--.. Operations on constants of any type .................................--*/
790 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
791 return wrap(Constant::getNullValue(unwrap(Ty)));
794 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
795 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
798 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
799 return wrap(UndefValue::get(unwrap(Ty)));
802 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
803 return isa<Constant>(unwrap(Ty));
806 LLVMBool LLVMIsNull(LLVMValueRef Val) {
807 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
808 return C->isNullValue();
812 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
813 return isa<UndefValue>(unwrap(Val));
816 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
817 return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
820 /*--.. Operations on metadata nodes ........................................--*/
822 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
824 LLVMContext &Context = *unwrap(C);
825 return wrap(MetadataAsValue::get(
826 Context, MDString::get(Context, StringRef(Str, SLen))));
829 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
830 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
833 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
835 LLVMContext &Context = *unwrap(C);
836 SmallVector<Metadata *, 8> MDs;
837 for (auto *OV : makeArrayRef(Vals, Count)) {
838 Value *V = unwrap(OV);
842 else if (auto *C = dyn_cast<Constant>(V))
843 MD = ConstantAsMetadata::get(C);
844 else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
845 MD = MDV->getMetadata();
846 assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
847 "outside of direct argument to call");
849 // This is function-local metadata. Pretend to make an MDNode.
851 "Expected only one operand to function-local metadata");
852 return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V)));
857 return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs)));
860 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
861 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
864 const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
865 if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
866 if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
867 *Length = S->getString().size();
868 return S->getString().data();
874 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) {
875 auto *MD = cast<MetadataAsValue>(unwrap(V));
876 if (isa<ValueAsMetadata>(MD->getMetadata()))
878 return cast<MDNode>(MD->getMetadata())->getNumOperands();
881 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) {
882 auto *MD = cast<MetadataAsValue>(unwrap(V));
883 if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
884 *Dest = wrap(MDV->getValue());
887 const auto *N = cast<MDNode>(MD->getMetadata());
888 const unsigned numOperands = N->getNumOperands();
889 LLVMContext &Context = unwrap(V)->getContext();
890 for (unsigned i = 0; i < numOperands; i++)
891 Dest[i] = getMDNodeOperandImpl(Context, N, i);
894 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char *Name) {
895 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
896 return N->getNumOperands();
901 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char *Name,
902 LLVMValueRef *Dest) {
903 NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
906 LLVMContext &Context = unwrap(M)->getContext();
907 for (unsigned i=0;i<N->getNumOperands();i++)
908 Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
911 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char *Name,
913 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
918 N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
921 /*--.. Operations on scalar constants ......................................--*/
923 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
924 LLVMBool SignExtend) {
925 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
928 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
930 const uint64_t Words[]) {
931 IntegerType *Ty = unwrap<IntegerType>(IntTy);
932 return wrap(ConstantInt::get(Ty->getContext(),
933 APInt(Ty->getBitWidth(),
934 makeArrayRef(Words, NumWords))));
937 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
939 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
943 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
944 unsigned SLen, uint8_t Radix) {
945 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
949 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
950 return wrap(ConstantFP::get(unwrap(RealTy), N));
953 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
954 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
957 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
959 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
962 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
963 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
966 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
967 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
970 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
971 ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
972 Type *Ty = cFP->getType();
974 if (Ty->isFloatTy()) {
976 return cFP->getValueAPF().convertToFloat();
979 if (Ty->isDoubleTy()) {
981 return cFP->getValueAPF().convertToDouble();
985 APFloat APF = cFP->getValueAPF();
986 APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo);
987 *LosesInfo = APFLosesInfo;
988 return APF.convertToDouble();
991 /*--.. Operations on composite constants ...................................--*/
993 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
995 LLVMBool DontNullTerminate) {
996 /* Inverted the sense of AddNull because ', 0)' is a
997 better mnemonic for null termination than ', 1)'. */
998 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
999 DontNullTerminate == 0));
1002 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
1003 LLVMBool DontNullTerminate) {
1004 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
1008 LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef C, unsigned idx) {
1009 return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
1012 LLVMBool LLVMIsConstantString(LLVMValueRef C) {
1013 return unwrap<ConstantDataSequential>(C)->isString();
1016 const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1017 StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1018 *Length = Str.size();
1022 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
1023 LLVMValueRef *ConstantVals, unsigned Length) {
1024 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1025 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1028 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
1029 LLVMValueRef *ConstantVals,
1030 unsigned Count, LLVMBool Packed) {
1031 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1032 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
1036 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
1038 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1042 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
1043 LLVMValueRef *ConstantVals,
1045 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1046 StructType *Ty = cast<StructType>(unwrap(StructTy));
1048 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
1051 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
1052 return wrap(ConstantVector::get(makeArrayRef(
1053 unwrap<Constant>(ScalarConstantVals, Size), Size)));
1056 /*-- Opcode mapping */
1058 static LLVMOpcode map_to_llvmopcode(int opcode)
1061 default: llvm_unreachable("Unhandled Opcode.");
1062 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
1063 #include "llvm/IR/Instruction.def"
1068 static int map_from_llvmopcode(LLVMOpcode code)
1071 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
1072 #include "llvm/IR/Instruction.def"
1075 llvm_unreachable("Unhandled Opcode.");
1078 /*--.. Constant expressions ................................................--*/
1080 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
1081 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
1084 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
1085 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
1088 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
1089 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
1092 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
1093 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1096 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
1097 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
1100 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
1101 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
1105 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
1106 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
1109 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
1110 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
1113 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1114 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
1115 unwrap<Constant>(RHSConstant)));
1118 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
1119 LLVMValueRef RHSConstant) {
1120 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
1121 unwrap<Constant>(RHSConstant)));
1124 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
1125 LLVMValueRef RHSConstant) {
1126 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
1127 unwrap<Constant>(RHSConstant)));
1130 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1131 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
1132 unwrap<Constant>(RHSConstant)));
1135 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1136 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
1137 unwrap<Constant>(RHSConstant)));
1140 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
1141 LLVMValueRef RHSConstant) {
1142 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
1143 unwrap<Constant>(RHSConstant)));
1146 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
1147 LLVMValueRef RHSConstant) {
1148 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
1149 unwrap<Constant>(RHSConstant)));
1152 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1153 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
1154 unwrap<Constant>(RHSConstant)));
1157 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1158 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
1159 unwrap<Constant>(RHSConstant)));
1162 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
1163 LLVMValueRef RHSConstant) {
1164 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
1165 unwrap<Constant>(RHSConstant)));
1168 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
1169 LLVMValueRef RHSConstant) {
1170 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
1171 unwrap<Constant>(RHSConstant)));
1174 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1175 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
1176 unwrap<Constant>(RHSConstant)));
1179 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1180 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
1181 unwrap<Constant>(RHSConstant)));
1184 LLVMValueRef LLVMConstExactUDiv(LLVMValueRef LHSConstant,
1185 LLVMValueRef RHSConstant) {
1186 return wrap(ConstantExpr::getExactUDiv(unwrap<Constant>(LHSConstant),
1187 unwrap<Constant>(RHSConstant)));
1190 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1191 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
1192 unwrap<Constant>(RHSConstant)));
1195 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
1196 LLVMValueRef RHSConstant) {
1197 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
1198 unwrap<Constant>(RHSConstant)));
1201 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1202 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
1203 unwrap<Constant>(RHSConstant)));
1206 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1207 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
1208 unwrap<Constant>(RHSConstant)));
1211 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1212 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
1213 unwrap<Constant>(RHSConstant)));
1216 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1217 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
1218 unwrap<Constant>(RHSConstant)));
1221 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1222 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
1223 unwrap<Constant>(RHSConstant)));
1226 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1227 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
1228 unwrap<Constant>(RHSConstant)));
1231 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1232 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
1233 unwrap<Constant>(RHSConstant)));
1236 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
1237 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1238 return wrap(ConstantExpr::getICmp(Predicate,
1239 unwrap<Constant>(LHSConstant),
1240 unwrap<Constant>(RHSConstant)));
1243 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
1244 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1245 return wrap(ConstantExpr::getFCmp(Predicate,
1246 unwrap<Constant>(LHSConstant),
1247 unwrap<Constant>(RHSConstant)));
1250 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1251 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1252 unwrap<Constant>(RHSConstant)));
1255 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1256 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
1257 unwrap<Constant>(RHSConstant)));
1260 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1261 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1262 unwrap<Constant>(RHSConstant)));
1265 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
1266 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1267 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1269 return wrap(ConstantExpr::getGetElementPtr(
1270 nullptr, unwrap<Constant>(ConstantVal), IdxList));
1273 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
1274 LLVMValueRef *ConstantIndices,
1275 unsigned NumIndices) {
1276 Constant* Val = unwrap<Constant>(ConstantVal);
1277 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1279 return wrap(ConstantExpr::getInBoundsGetElementPtr(nullptr, Val, IdxList));
1282 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1283 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1287 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1288 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1292 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1293 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1297 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1298 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1302 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1303 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1307 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1308 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1312 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1313 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1317 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1318 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1322 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1323 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1327 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1328 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1332 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1333 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1337 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1338 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1342 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1343 LLVMTypeRef ToType) {
1344 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1348 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1349 LLVMTypeRef ToType) {
1350 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1354 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1355 LLVMTypeRef ToType) {
1356 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1360 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1361 LLVMTypeRef ToType) {
1362 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1366 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1367 LLVMTypeRef ToType) {
1368 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1372 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1373 LLVMBool isSigned) {
1374 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1375 unwrap(ToType), isSigned));
1378 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1379 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1383 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1384 LLVMValueRef ConstantIfTrue,
1385 LLVMValueRef ConstantIfFalse) {
1386 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1387 unwrap<Constant>(ConstantIfTrue),
1388 unwrap<Constant>(ConstantIfFalse)));
1391 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1392 LLVMValueRef IndexConstant) {
1393 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1394 unwrap<Constant>(IndexConstant)));
1397 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1398 LLVMValueRef ElementValueConstant,
1399 LLVMValueRef IndexConstant) {
1400 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1401 unwrap<Constant>(ElementValueConstant),
1402 unwrap<Constant>(IndexConstant)));
1405 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1406 LLVMValueRef VectorBConstant,
1407 LLVMValueRef MaskConstant) {
1408 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1409 unwrap<Constant>(VectorBConstant),
1410 unwrap<Constant>(MaskConstant)));
1413 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1415 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1416 makeArrayRef(IdxList, NumIdx)));
1419 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1420 LLVMValueRef ElementValueConstant,
1421 unsigned *IdxList, unsigned NumIdx) {
1422 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1423 unwrap<Constant>(ElementValueConstant),
1424 makeArrayRef(IdxList, NumIdx)));
1427 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1428 const char *Constraints,
1429 LLVMBool HasSideEffects,
1430 LLVMBool IsAlignStack) {
1431 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1432 Constraints, HasSideEffects, IsAlignStack));
1435 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1436 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1439 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1441 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1442 return wrap(unwrap<GlobalValue>(Global)->getParent());
1445 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1446 return unwrap<GlobalValue>(Global)->isDeclaration();
1449 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1450 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1451 case GlobalValue::ExternalLinkage:
1452 return LLVMExternalLinkage;
1453 case GlobalValue::AvailableExternallyLinkage:
1454 return LLVMAvailableExternallyLinkage;
1455 case GlobalValue::LinkOnceAnyLinkage:
1456 return LLVMLinkOnceAnyLinkage;
1457 case GlobalValue::LinkOnceODRLinkage:
1458 return LLVMLinkOnceODRLinkage;
1459 case GlobalValue::WeakAnyLinkage:
1460 return LLVMWeakAnyLinkage;
1461 case GlobalValue::WeakODRLinkage:
1462 return LLVMWeakODRLinkage;
1463 case GlobalValue::AppendingLinkage:
1464 return LLVMAppendingLinkage;
1465 case GlobalValue::InternalLinkage:
1466 return LLVMInternalLinkage;
1467 case GlobalValue::PrivateLinkage:
1468 return LLVMPrivateLinkage;
1469 case GlobalValue::ExternalWeakLinkage:
1470 return LLVMExternalWeakLinkage;
1471 case GlobalValue::CommonLinkage:
1472 return LLVMCommonLinkage;
1475 llvm_unreachable("Invalid GlobalValue linkage!");
1478 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1479 GlobalValue *GV = unwrap<GlobalValue>(Global);
1482 case LLVMExternalLinkage:
1483 GV->setLinkage(GlobalValue::ExternalLinkage);
1485 case LLVMAvailableExternallyLinkage:
1486 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1488 case LLVMLinkOnceAnyLinkage:
1489 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1491 case LLVMLinkOnceODRLinkage:
1492 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1494 case LLVMLinkOnceODRAutoHideLinkage:
1495 DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1496 "longer supported.");
1498 case LLVMWeakAnyLinkage:
1499 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1501 case LLVMWeakODRLinkage:
1502 GV->setLinkage(GlobalValue::WeakODRLinkage);
1504 case LLVMAppendingLinkage:
1505 GV->setLinkage(GlobalValue::AppendingLinkage);
1507 case LLVMInternalLinkage:
1508 GV->setLinkage(GlobalValue::InternalLinkage);
1510 case LLVMPrivateLinkage:
1511 GV->setLinkage(GlobalValue::PrivateLinkage);
1513 case LLVMLinkerPrivateLinkage:
1514 GV->setLinkage(GlobalValue::PrivateLinkage);
1516 case LLVMLinkerPrivateWeakLinkage:
1517 GV->setLinkage(GlobalValue::PrivateLinkage);
1519 case LLVMDLLImportLinkage:
1521 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1523 case LLVMDLLExportLinkage:
1525 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1527 case LLVMExternalWeakLinkage:
1528 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1530 case LLVMGhostLinkage:
1532 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1534 case LLVMCommonLinkage:
1535 GV->setLinkage(GlobalValue::CommonLinkage);
1540 const char *LLVMGetSection(LLVMValueRef Global) {
1541 // Using .data() is safe because of how GlobalObject::setSection is
1543 return unwrap<GlobalValue>(Global)->getSection().data();
1546 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1547 unwrap<GlobalObject>(Global)->setSection(Section);
1550 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1551 return static_cast<LLVMVisibility>(
1552 unwrap<GlobalValue>(Global)->getVisibility());
1555 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1556 unwrap<GlobalValue>(Global)
1557 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1560 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
1561 return static_cast<LLVMDLLStorageClass>(
1562 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1565 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
1566 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1567 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1570 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
1571 return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
1574 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
1575 unwrap<GlobalValue>(Global)->setUnnamedAddr(
1576 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
1577 : GlobalValue::UnnamedAddr::None);
1580 /*--.. Operations on global variables, load and store instructions .........--*/
1582 unsigned LLVMGetAlignment(LLVMValueRef V) {
1583 Value *P = unwrap<Value>(V);
1584 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1585 return GV->getAlignment();
1586 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1587 return AI->getAlignment();
1588 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1589 return LI->getAlignment();
1590 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1591 return SI->getAlignment();
1594 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1597 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1598 Value *P = unwrap<Value>(V);
1599 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
1600 GV->setAlignment(Bytes);
1601 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1602 AI->setAlignment(Bytes);
1603 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1604 LI->setAlignment(Bytes);
1605 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1606 SI->setAlignment(Bytes);
1609 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1612 /*--.. Operations on global variables ......................................--*/
1614 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1615 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1616 GlobalValue::ExternalLinkage, nullptr, Name));
1619 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1621 unsigned AddressSpace) {
1622 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1623 GlobalValue::ExternalLinkage, nullptr, Name,
1624 nullptr, GlobalVariable::NotThreadLocal,
1628 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1629 return wrap(unwrap(M)->getNamedGlobal(Name));
1632 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1633 Module *Mod = unwrap(M);
1634 Module::global_iterator I = Mod->global_begin();
1635 if (I == Mod->global_end())
1640 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1641 Module *Mod = unwrap(M);
1642 Module::global_iterator I = Mod->global_end();
1643 if (I == Mod->global_begin())
1648 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1649 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1650 Module::global_iterator I(GV);
1651 if (++I == GV->getParent()->global_end())
1656 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1657 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1658 Module::global_iterator I(GV);
1659 if (I == GV->getParent()->global_begin())
1664 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1665 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1668 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1669 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1670 if ( !GV->hasInitializer() )
1672 return wrap(GV->getInitializer());
1675 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1676 unwrap<GlobalVariable>(GlobalVar)
1677 ->setInitializer(unwrap<Constant>(ConstantVal));
1680 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1681 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1684 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1685 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1688 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1689 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1692 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1693 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1696 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1697 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1698 case GlobalVariable::NotThreadLocal:
1699 return LLVMNotThreadLocal;
1700 case GlobalVariable::GeneralDynamicTLSModel:
1701 return LLVMGeneralDynamicTLSModel;
1702 case GlobalVariable::LocalDynamicTLSModel:
1703 return LLVMLocalDynamicTLSModel;
1704 case GlobalVariable::InitialExecTLSModel:
1705 return LLVMInitialExecTLSModel;
1706 case GlobalVariable::LocalExecTLSModel:
1707 return LLVMLocalExecTLSModel;
1710 llvm_unreachable("Invalid GlobalVariable thread local mode");
1713 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1714 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1717 case LLVMNotThreadLocal:
1718 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1720 case LLVMGeneralDynamicTLSModel:
1721 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1723 case LLVMLocalDynamicTLSModel:
1724 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1726 case LLVMInitialExecTLSModel:
1727 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1729 case LLVMLocalExecTLSModel:
1730 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1735 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1736 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1739 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1740 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1743 /*--.. Operations on aliases ......................................--*/
1745 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1747 auto *PTy = cast<PointerType>(unwrap(Ty));
1748 return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
1749 GlobalValue::ExternalLinkage, Name,
1750 unwrap<Constant>(Aliasee), unwrap(M)));
1753 /*--.. Operations on functions .............................................--*/
1755 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1756 LLVMTypeRef FunctionTy) {
1757 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1758 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1761 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1762 return wrap(unwrap(M)->getFunction(Name));
1765 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1766 Module *Mod = unwrap(M);
1767 Module::iterator I = Mod->begin();
1768 if (I == Mod->end())
1773 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1774 Module *Mod = unwrap(M);
1775 Module::iterator I = Mod->end();
1776 if (I == Mod->begin())
1781 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1782 Function *Func = unwrap<Function>(Fn);
1783 Module::iterator I(Func);
1784 if (++I == Func->getParent()->end())
1789 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1790 Function *Func = unwrap<Function>(Fn);
1791 Module::iterator I(Func);
1792 if (I == Func->getParent()->begin())
1797 void LLVMDeleteFunction(LLVMValueRef Fn) {
1798 unwrap<Function>(Fn)->eraseFromParent();
1801 LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) {
1802 return unwrap<Function>(Fn)->hasPersonalityFn();
1805 LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) {
1806 return wrap(unwrap<Function>(Fn)->getPersonalityFn());
1809 void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) {
1810 unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
1813 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1814 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1815 return F->getIntrinsicID();
1819 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1820 return unwrap<Function>(Fn)->getCallingConv();
1823 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1824 return unwrap<Function>(Fn)->setCallingConv(
1825 static_cast<CallingConv::ID>(CC));
1828 const char *LLVMGetGC(LLVMValueRef Fn) {
1829 Function *F = unwrap<Function>(Fn);
1830 return F->hasGC()? F->getGC().c_str() : nullptr;
1833 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1834 Function *F = unwrap<Function>(Fn);
1841 void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1842 LLVMAttributeRef A) {
1843 unwrap<Function>(F)->addAttribute(Idx, unwrap(A));
1846 unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) {
1847 auto *ASN = AttributeSetNode::get(unwrap<Function>(F)->getAttributes(), Idx);
1850 return ASN->getNumAttributes();
1853 void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1854 LLVMAttributeRef *Attrs) {
1855 auto *ASN = AttributeSetNode::get(unwrap<Function>(F)->getAttributes(), Idx);
1858 for (auto A: make_range(ASN->begin(), ASN->end()))
1862 LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F,
1863 LLVMAttributeIndex Idx,
1865 return wrap(unwrap<Function>(F)->getAttribute(Idx,
1866 (Attribute::AttrKind)KindID));
1869 LLVMAttributeRef LLVMGetStringAttributeAtIndex(LLVMValueRef F,
1870 LLVMAttributeIndex Idx,
1871 const char *K, unsigned KLen) {
1872 return wrap(unwrap<Function>(F)->getAttribute(Idx, StringRef(K, KLen)));
1875 void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1877 unwrap<Function>(F)->removeAttribute(Idx, (Attribute::AttrKind)KindID);
1880 void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1881 const char *K, unsigned KLen) {
1882 unwrap<Function>(F)->removeAttribute(Idx, StringRef(K, KLen));
1885 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1887 Function *Func = unwrap<Function>(Fn);
1888 AttributeSet::AttrIndex Idx =
1889 AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
1892 B.addAttribute(A, V);
1893 AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
1894 Func->addAttributes(Idx, Set);
1897 /*--.. Operations on parameters ............................................--*/
1899 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1900 // This function is strictly redundant to
1901 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1902 return unwrap<Function>(FnRef)->arg_size();
1905 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1906 Function *Fn = unwrap<Function>(FnRef);
1907 for (Function::arg_iterator I = Fn->arg_begin(),
1908 E = Fn->arg_end(); I != E; I++)
1909 *ParamRefs++ = wrap(&*I);
1912 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1913 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1919 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1920 return wrap(unwrap<Argument>(V)->getParent());
1923 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1924 Function *Func = unwrap<Function>(Fn);
1925 Function::arg_iterator I = Func->arg_begin();
1926 if (I == Func->arg_end())
1931 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1932 Function *Func = unwrap<Function>(Fn);
1933 Function::arg_iterator I = Func->arg_end();
1934 if (I == Func->arg_begin())
1939 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1940 Argument *A = unwrap<Argument>(Arg);
1941 Function::arg_iterator I(A);
1942 if (++I == A->getParent()->arg_end())
1947 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1948 Argument *A = unwrap<Argument>(Arg);
1949 Function::arg_iterator I(A);
1950 if (I == A->getParent()->arg_begin())
1955 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1956 Argument *A = unwrap<Argument>(Arg);
1958 B.addAlignmentAttr(align);
1959 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
1962 /*--.. Operations on basic blocks ..........................................--*/
1964 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1965 return wrap(static_cast<Value*>(unwrap(BB)));
1968 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1969 return isa<BasicBlock>(unwrap(Val));
1972 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1973 return wrap(unwrap<BasicBlock>(Val));
1976 const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) {
1977 return unwrap(BB)->getName().data();
1980 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1981 return wrap(unwrap(BB)->getParent());
1984 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1985 return wrap(unwrap(BB)->getTerminator());
1988 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1989 return unwrap<Function>(FnRef)->size();
1992 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1993 Function *Fn = unwrap<Function>(FnRef);
1994 for (BasicBlock &BB : *Fn)
1995 *BasicBlocksRefs++ = wrap(&BB);
1998 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1999 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2002 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
2003 Function *Func = unwrap<Function>(Fn);
2004 Function::iterator I = Func->begin();
2005 if (I == Func->end())
2010 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
2011 Function *Func = unwrap<Function>(Fn);
2012 Function::iterator I = Func->end();
2013 if (I == Func->begin())
2018 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
2019 BasicBlock *Block = unwrap(BB);
2020 Function::iterator I(Block);
2021 if (++I == Block->getParent()->end())
2026 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
2027 BasicBlock *Block = unwrap(BB);
2028 Function::iterator I(Block);
2029 if (I == Block->getParent()->begin())
2034 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
2037 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
2040 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
2041 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
2044 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
2045 LLVMBasicBlockRef BBRef,
2047 BasicBlock *BB = unwrap(BBRef);
2048 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2051 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
2053 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
2056 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
2057 unwrap(BBRef)->eraseFromParent();
2060 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
2061 unwrap(BBRef)->removeFromParent();
2064 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2065 unwrap(BB)->moveBefore(unwrap(MovePos));
2068 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2069 unwrap(BB)->moveAfter(unwrap(MovePos));
2072 /*--.. Operations on instructions ..........................................--*/
2074 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
2075 return wrap(unwrap<Instruction>(Inst)->getParent());
2078 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
2079 BasicBlock *Block = unwrap(BB);
2080 BasicBlock::iterator I = Block->begin();
2081 if (I == Block->end())
2086 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
2087 BasicBlock *Block = unwrap(BB);
2088 BasicBlock::iterator I = Block->end();
2089 if (I == Block->begin())
2094 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
2095 Instruction *Instr = unwrap<Instruction>(Inst);
2096 BasicBlock::iterator I(Instr);
2097 if (++I == Instr->getParent()->end())
2102 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
2103 Instruction *Instr = unwrap<Instruction>(Inst);
2104 BasicBlock::iterator I(Instr);
2105 if (I == Instr->getParent()->begin())
2110 void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) {
2111 unwrap<Instruction>(Inst)->removeFromParent();
2114 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
2115 unwrap<Instruction>(Inst)->eraseFromParent();
2118 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
2119 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2120 return (LLVMIntPredicate)I->getPredicate();
2121 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2122 if (CE->getOpcode() == Instruction::ICmp)
2123 return (LLVMIntPredicate)CE->getPredicate();
2124 return (LLVMIntPredicate)0;
2127 LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
2128 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2129 return (LLVMRealPredicate)I->getPredicate();
2130 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2131 if (CE->getOpcode() == Instruction::FCmp)
2132 return (LLVMRealPredicate)CE->getPredicate();
2133 return (LLVMRealPredicate)0;
2136 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
2137 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2138 return map_to_llvmopcode(C->getOpcode());
2139 return (LLVMOpcode)0;
2142 LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {
2143 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2144 return wrap(C->clone());
2148 /*--.. Call and invoke instructions ........................................--*/
2150 unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) {
2151 return CallSite(unwrap<Instruction>(Instr)).getNumArgOperands();
2154 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
2155 return CallSite(unwrap<Instruction>(Instr)).getCallingConv();
2158 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
2159 return CallSite(unwrap<Instruction>(Instr))
2160 .setCallingConv(static_cast<CallingConv::ID>(CC));
2163 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
2165 CallSite Call = CallSite(unwrap<Instruction>(Instr));
2167 B.addAlignmentAttr(align);
2168 Call.setAttributes(Call.getAttributes()
2169 .addAttributes(Call->getContext(), index,
2170 AttributeSet::get(Call->getContext(),
2174 void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2175 LLVMAttributeRef A) {
2176 CallSite(unwrap<Instruction>(C)).addAttribute(Idx, unwrap(A));
2179 unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C,
2180 LLVMAttributeIndex Idx) {
2181 auto CS = CallSite(unwrap<Instruction>(C));
2182 auto *ASN = AttributeSetNode::get(CS.getAttributes(), Idx);
2185 return ASN->getNumAttributes();
2188 void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx,
2189 LLVMAttributeRef *Attrs) {
2190 auto CS = CallSite(unwrap<Instruction>(C));
2191 auto *ASN = AttributeSetNode::get(CS.getAttributes(), Idx);
2194 for (auto A: make_range(ASN->begin(), ASN->end()))
2198 LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C,
2199 LLVMAttributeIndex Idx,
2201 return wrap(CallSite(unwrap<Instruction>(C))
2202 .getAttribute(Idx, (Attribute::AttrKind)KindID));
2205 LLVMAttributeRef LLVMGetCallSiteStringAttribute(LLVMValueRef C,
2206 LLVMAttributeIndex Idx,
2207 const char *K, unsigned KLen) {
2208 return wrap(CallSite(unwrap<Instruction>(C))
2209 .getAttribute(Idx, StringRef(K, KLen)));
2212 void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2214 CallSite(unwrap<Instruction>(C))
2215 .removeAttribute(Idx, (Attribute::AttrKind)KindID);
2218 void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2219 const char *K, unsigned KLen) {
2220 CallSite(unwrap<Instruction>(C)).removeAttribute(Idx, StringRef(K, KLen));
2223 LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) {
2224 return wrap(CallSite(unwrap<Instruction>(Instr)).getCalledValue());
2227 /*--.. Operations on call instructions (only) ..............................--*/
2229 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
2230 return unwrap<CallInst>(Call)->isTailCall();
2233 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
2234 unwrap<CallInst>(Call)->setTailCall(isTailCall);
2237 /*--.. Operations on invoke instructions (only) ............................--*/
2239 LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) {
2240 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2243 LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) {
2244 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2247 void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2248 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2251 void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2252 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2255 /*--.. Operations on terminators ...........................................--*/
2257 unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {
2258 return unwrap<TerminatorInst>(Term)->getNumSuccessors();
2261 LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) {
2262 return wrap(unwrap<TerminatorInst>(Term)->getSuccessor(i));
2265 void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
2266 return unwrap<TerminatorInst>(Term)->setSuccessor(i,unwrap(block));
2269 /*--.. Operations on branch instructions (only) ............................--*/
2271 LLVMBool LLVMIsConditional(LLVMValueRef Branch) {
2272 return unwrap<BranchInst>(Branch)->isConditional();
2275 LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) {
2276 return wrap(unwrap<BranchInst>(Branch)->getCondition());
2279 void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) {
2280 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2283 /*--.. Operations on switch instructions (only) ............................--*/
2285 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
2286 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2289 /*--.. Operations on alloca instructions (only) ............................--*/
2291 LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) {
2292 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
2295 /*--.. Operations on gep instructions (only) ...............................--*/
2297 LLVMBool LLVMIsInBounds(LLVMValueRef GEP) {
2298 return unwrap<GetElementPtrInst>(GEP)->isInBounds();
2301 void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) {
2302 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
2305 /*--.. Operations on phi nodes .............................................--*/
2307 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
2308 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
2309 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
2310 for (unsigned I = 0; I != Count; ++I)
2311 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
2314 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
2315 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
2318 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
2319 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
2322 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
2323 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
2326 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/
2328 unsigned LLVMGetNumIndices(LLVMValueRef Inst) {
2329 auto *I = unwrap(Inst);
2330 if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
2331 return GEP->getNumIndices();
2332 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2333 return EV->getNumIndices();
2334 if (auto *IV = dyn_cast<InsertValueInst>(I))
2335 return IV->getNumIndices();
2337 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
2340 const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
2341 auto *I = unwrap(Inst);
2342 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2343 return EV->getIndices().data();
2344 if (auto *IV = dyn_cast<InsertValueInst>(I))
2345 return IV->getIndices().data();
2347 "LLVMGetIndices applies only to extractvalue and insertvalue!");
2351 /*===-- Instruction builders ----------------------------------------------===*/
2353 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
2354 return wrap(new IRBuilder<>(*unwrap(C)));
2357 LLVMBuilderRef LLVMCreateBuilder(void) {
2358 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
2361 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
2362 LLVMValueRef Instr) {
2363 BasicBlock *BB = unwrap(Block);
2364 auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
2365 unwrap(Builder)->SetInsertPoint(BB, I);
2368 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2369 Instruction *I = unwrap<Instruction>(Instr);
2370 unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
2373 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
2374 BasicBlock *BB = unwrap(Block);
2375 unwrap(Builder)->SetInsertPoint(BB);
2378 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
2379 return wrap(unwrap(Builder)->GetInsertBlock());
2382 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
2383 unwrap(Builder)->ClearInsertionPoint();
2386 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2387 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
2390 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
2392 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
2395 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
2396 delete unwrap(Builder);
2399 /*--.. Metadata builders ...................................................--*/
2401 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
2403 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
2404 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
2407 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
2408 LLVMContext &Context = unwrap(Builder)->getContext();
2409 return wrap(MetadataAsValue::get(
2410 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
2413 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
2414 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
2418 /*--.. Instruction builders ................................................--*/
2420 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
2421 return wrap(unwrap(B)->CreateRetVoid());
2424 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
2425 return wrap(unwrap(B)->CreateRet(unwrap(V)));
2428 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
2430 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
2433 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
2434 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
2437 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
2438 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
2439 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
2442 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
2443 LLVMBasicBlockRef Else, unsigned NumCases) {
2444 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
2447 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
2448 unsigned NumDests) {
2449 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
2452 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
2453 LLVMValueRef *Args, unsigned NumArgs,
2454 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
2456 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
2457 makeArrayRef(unwrap(Args), NumArgs),
2461 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
2462 LLVMValueRef PersFn, unsigned NumClauses,
2464 // The personality used to live on the landingpad instruction, but now it
2465 // lives on the parent function. For compatibility, take the provided
2466 // personality and put it on the parent function.
2468 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
2469 cast<Function>(unwrap(PersFn)));
2470 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
2473 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
2474 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
2477 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
2478 return wrap(unwrap(B)->CreateUnreachable());
2481 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
2482 LLVMBasicBlockRef Dest) {
2483 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
2486 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
2487 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
2490 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
2491 return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
2494 LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
2495 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
2498 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
2499 unwrap<LandingPadInst>(LandingPad)->
2500 addClause(cast<Constant>(unwrap(ClauseVal)));
2503 LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) {
2504 return unwrap<LandingPadInst>(LandingPad)->isCleanup();
2507 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
2508 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2511 /*--.. Arithmetic ..........................................................--*/
2513 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2515 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2518 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2520 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2523 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2525 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2528 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2530 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2533 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2535 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2538 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2540 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2543 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2545 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2548 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2550 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2553 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2555 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2558 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2560 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2563 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2565 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2568 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2570 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2573 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2575 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2578 LLVMValueRef LLVMBuildExactUDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2579 LLVMValueRef RHS, const char *Name) {
2580 return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
2583 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2585 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2588 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2589 LLVMValueRef RHS, const char *Name) {
2590 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2593 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2595 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2598 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2600 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2603 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2605 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2608 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2610 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2613 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2615 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2618 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2620 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2623 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2625 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2628 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2630 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2633 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2635 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2638 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2640 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2643 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2644 LLVMValueRef LHS, LLVMValueRef RHS,
2646 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2647 unwrap(RHS), Name));
2650 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2651 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2654 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2656 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2659 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2661 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2664 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2665 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2668 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2669 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2672 /*--.. Memory ..............................................................--*/
2674 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2676 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2677 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2678 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2679 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2680 ITy, unwrap(Ty), AllocSize,
2681 nullptr, nullptr, "");
2682 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2685 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2686 LLVMValueRef Val, const char *Name) {
2687 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2688 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2689 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2690 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2691 ITy, unwrap(Ty), AllocSize,
2692 unwrap(Val), nullptr, "");
2693 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2696 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2698 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
2701 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2702 LLVMValueRef Val, const char *Name) {
2703 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2706 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2707 return wrap(unwrap(B)->Insert(
2708 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2711 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2713 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2716 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2717 LLVMValueRef PointerVal) {
2718 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2721 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
2723 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic;
2724 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered;
2725 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic;
2726 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire;
2727 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release;
2728 case LLVMAtomicOrderingAcquireRelease:
2729 return AtomicOrdering::AcquireRelease;
2730 case LLVMAtomicOrderingSequentiallyConsistent:
2731 return AtomicOrdering::SequentiallyConsistent;
2734 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
2737 static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) {
2739 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic;
2740 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered;
2741 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic;
2742 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire;
2743 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease;
2744 case AtomicOrdering::AcquireRelease:
2745 return LLVMAtomicOrderingAcquireRelease;
2746 case AtomicOrdering::SequentiallyConsistent:
2747 return LLVMAtomicOrderingSequentiallyConsistent;
2750 llvm_unreachable("Invalid AtomicOrdering value!");
2753 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
2754 LLVMBool isSingleThread, const char *Name) {
2756 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
2757 isSingleThread ? SingleThread : CrossThread,
2761 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2762 LLVMValueRef *Indices, unsigned NumIndices,
2764 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2765 return wrap(unwrap(B)->CreateGEP(nullptr, unwrap(Pointer), IdxList, Name));
2768 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2769 LLVMValueRef *Indices, unsigned NumIndices,
2771 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2773 unwrap(B)->CreateInBoundsGEP(nullptr, unwrap(Pointer), IdxList, Name));
2776 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2777 unsigned Idx, const char *Name) {
2778 return wrap(unwrap(B)->CreateStructGEP(nullptr, unwrap(Pointer), Idx, Name));
2781 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2783 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2786 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2788 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2791 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2792 Value *P = unwrap<Value>(MemAccessInst);
2793 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2794 return LI->isVolatile();
2795 return cast<StoreInst>(P)->isVolatile();
2798 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2799 Value *P = unwrap<Value>(MemAccessInst);
2800 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2801 return LI->setVolatile(isVolatile);
2802 return cast<StoreInst>(P)->setVolatile(isVolatile);
2805 LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) {
2806 Value *P = unwrap<Value>(MemAccessInst);
2808 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2809 O = LI->getOrdering();
2811 O = cast<StoreInst>(P)->getOrdering();
2812 return mapToLLVMOrdering(O);
2815 void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
2816 Value *P = unwrap<Value>(MemAccessInst);
2817 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
2819 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2820 return LI->setOrdering(O);
2821 return cast<StoreInst>(P)->setOrdering(O);
2824 /*--.. Casts ...............................................................--*/
2826 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2827 LLVMTypeRef DestTy, const char *Name) {
2828 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2831 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2832 LLVMTypeRef DestTy, const char *Name) {
2833 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2836 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2837 LLVMTypeRef DestTy, const char *Name) {
2838 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2841 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2842 LLVMTypeRef DestTy, const char *Name) {
2843 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2846 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2847 LLVMTypeRef DestTy, const char *Name) {
2848 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2851 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2852 LLVMTypeRef DestTy, const char *Name) {
2853 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2856 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2857 LLVMTypeRef DestTy, const char *Name) {
2858 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2861 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2862 LLVMTypeRef DestTy, const char *Name) {
2863 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2866 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2867 LLVMTypeRef DestTy, const char *Name) {
2868 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2871 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2872 LLVMTypeRef DestTy, const char *Name) {
2873 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2876 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2877 LLVMTypeRef DestTy, const char *Name) {
2878 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2881 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2882 LLVMTypeRef DestTy, const char *Name) {
2883 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2886 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
2887 LLVMTypeRef DestTy, const char *Name) {
2888 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
2891 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2892 LLVMTypeRef DestTy, const char *Name) {
2893 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2897 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2898 LLVMTypeRef DestTy, const char *Name) {
2899 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2903 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2904 LLVMTypeRef DestTy, const char *Name) {
2905 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2909 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2910 LLVMTypeRef DestTy, const char *Name) {
2911 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2912 unwrap(DestTy), Name));
2915 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2916 LLVMTypeRef DestTy, const char *Name) {
2917 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2920 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2921 LLVMTypeRef DestTy, const char *Name) {
2922 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2923 /*isSigned*/true, Name));
2926 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2927 LLVMTypeRef DestTy, const char *Name) {
2928 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2931 /*--.. Comparisons .........................................................--*/
2933 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2934 LLVMValueRef LHS, LLVMValueRef RHS,
2936 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2937 unwrap(LHS), unwrap(RHS), Name));
2940 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2941 LLVMValueRef LHS, LLVMValueRef RHS,
2943 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2944 unwrap(LHS), unwrap(RHS), Name));
2947 /*--.. Miscellaneous instructions ..........................................--*/
2949 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2950 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2953 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2954 LLVMValueRef *Args, unsigned NumArgs,
2956 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2957 makeArrayRef(unwrap(Args), NumArgs),
2961 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2962 LLVMValueRef Then, LLVMValueRef Else,
2964 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2968 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2969 LLVMTypeRef Ty, const char *Name) {
2970 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2973 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2974 LLVMValueRef Index, const char *Name) {
2975 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2979 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2980 LLVMValueRef EltVal, LLVMValueRef Index,
2982 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2983 unwrap(Index), Name));
2986 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2987 LLVMValueRef V2, LLVMValueRef Mask,
2989 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2990 unwrap(Mask), Name));
2993 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2994 unsigned Index, const char *Name) {
2995 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2998 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2999 LLVMValueRef EltVal, unsigned Index,
3001 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
3005 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
3007 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
3010 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
3012 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
3015 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
3016 LLVMValueRef RHS, const char *Name) {
3017 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
3020 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
3021 LLVMValueRef PTR, LLVMValueRef Val,
3022 LLVMAtomicOrdering ordering,
3023 LLVMBool singleThread) {
3024 AtomicRMWInst::BinOp intop;
3026 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
3027 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
3028 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
3029 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
3030 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
3031 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
3032 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
3033 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
3034 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
3035 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
3036 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
3038 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
3039 mapFromLLVMOrdering(ordering), singleThread ? SingleThread : CrossThread));
3042 LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr,
3043 LLVMValueRef Cmp, LLVMValueRef New,
3044 LLVMAtomicOrdering SuccessOrdering,
3045 LLVMAtomicOrdering FailureOrdering,
3046 LLVMBool singleThread) {
3048 return wrap(unwrap(B)->CreateAtomicCmpXchg(unwrap(Ptr), unwrap(Cmp),
3049 unwrap(New), mapFromLLVMOrdering(SuccessOrdering),
3050 mapFromLLVMOrdering(FailureOrdering),
3051 singleThread ? SingleThread : CrossThread));
3055 LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) {
3056 Value *P = unwrap<Value>(AtomicInst);
3058 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
3059 return I->getSynchScope() == SingleThread;
3060 return cast<AtomicCmpXchgInst>(P)->getSynchScope() == SingleThread;
3063 void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) {
3064 Value *P = unwrap<Value>(AtomicInst);
3065 SynchronizationScope Sync = NewValue ? SingleThread : CrossThread;
3067 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
3068 return I->setSynchScope(Sync);
3069 return cast<AtomicCmpXchgInst>(P)->setSynchScope(Sync);
3072 LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) {
3073 Value *P = unwrap<Value>(CmpXchgInst);
3074 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering());
3077 void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst,
3078 LLVMAtomicOrdering Ordering) {
3079 Value *P = unwrap<Value>(CmpXchgInst);
3080 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3082 return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O);
3085 LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst) {
3086 Value *P = unwrap<Value>(CmpXchgInst);
3087 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering());
3090 void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst,
3091 LLVMAtomicOrdering Ordering) {
3092 Value *P = unwrap<Value>(CmpXchgInst);
3093 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3095 return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O);
3098 /*===-- Module providers --------------------------------------------------===*/
3100 LLVMModuleProviderRef
3101 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
3102 return reinterpret_cast<LLVMModuleProviderRef>(M);
3105 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
3110 /*===-- Memory buffers ----------------------------------------------------===*/
3112 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
3114 LLVMMemoryBufferRef *OutMemBuf,
3115 char **OutMessage) {
3117 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
3118 if (std::error_code EC = MBOrErr.getError()) {
3119 *OutMessage = strdup(EC.message().c_str());
3122 *OutMemBuf = wrap(MBOrErr.get().release());
3126 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
3127 char **OutMessage) {
3128 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
3129 if (std::error_code EC = MBOrErr.getError()) {
3130 *OutMessage = strdup(EC.message().c_str());
3133 *OutMemBuf = wrap(MBOrErr.get().release());
3137 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
3138 const char *InputData,
3139 size_t InputDataLength,
3140 const char *BufferName,
3141 LLVMBool RequiresNullTerminator) {
3143 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
3144 StringRef(BufferName),
3145 RequiresNullTerminator).release());
3148 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
3149 const char *InputData,
3150 size_t InputDataLength,
3151 const char *BufferName) {
3154 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
3155 StringRef(BufferName)).release());
3158 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
3159 return unwrap(MemBuf)->getBufferStart();
3162 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
3163 return unwrap(MemBuf)->getBufferSize();
3166 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
3167 delete unwrap(MemBuf);
3170 /*===-- Pass Registry -----------------------------------------------------===*/
3172 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
3173 return wrap(PassRegistry::getPassRegistry());
3176 /*===-- Pass Manager ------------------------------------------------------===*/
3178 LLVMPassManagerRef LLVMCreatePassManager() {
3179 return wrap(new legacy::PassManager());
3182 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
3183 return wrap(new legacy::FunctionPassManager(unwrap(M)));
3186 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
3187 return LLVMCreateFunctionPassManagerForModule(
3188 reinterpret_cast<LLVMModuleRef>(P));
3191 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
3192 return unwrap<legacy::PassManager>(PM)->run(*unwrap(M));
3195 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
3196 return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization();
3199 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
3200 return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
3203 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
3204 return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization();
3207 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
3211 /*===-- Threading ------------------------------------------------------===*/
3213 LLVMBool LLVMStartMultithreaded() {
3214 return LLVMIsMultithreaded();
3217 void LLVMStopMultithreaded() {
3220 LLVMBool LLVMIsMultithreaded() {
3221 return llvm_is_multithreaded();