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/ReaderWriter.h"
18 #include "llvm/IR/Attributes.h"
19 #include "llvm/IR/CallSite.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/DerivedTypes.h"
22 #include "llvm/IR/DiagnosticInfo.h"
23 #include "llvm/IR/DiagnosticPrinter.h"
24 #include "llvm/IR/GlobalAlias.h"
25 #include "llvm/IR/GlobalVariable.h"
26 #include "llvm/IR/IRBuilder.h"
27 #include "llvm/IR/InlineAsm.h"
28 #include "llvm/IR/IntrinsicInst.h"
29 #include "llvm/IR/LLVMContext.h"
30 #include "llvm/IR/LegacyPassManager.h"
31 #include "llvm/IR/Module.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/FileSystem.h"
35 #include "llvm/Support/ManagedStatic.h"
36 #include "llvm/Support/MemoryBuffer.h"
37 #include "llvm/Support/Threading.h"
38 #include "llvm/Support/raw_ostream.h"
42 #include <system_error>
46 #define DEBUG_TYPE "ir"
48 void llvm::initializeCore(PassRegistry &Registry) {
49 initializeDominatorTreeWrapperPassPass(Registry);
50 initializePrintModulePassWrapperPass(Registry);
51 initializePrintFunctionPassWrapperPass(Registry);
52 initializePrintBasicBlockPassPass(Registry);
53 initializeVerifierLegacyPassPass(Registry);
56 void LLVMInitializeCore(LLVMPassRegistryRef R) {
57 initializeCore(*unwrap(R));
64 /*===-- Error handling ----------------------------------------------------===*/
66 char *LLVMCreateMessage(const char *Message) {
67 return strdup(Message);
70 void LLVMDisposeMessage(char *Message) {
75 /*===-- Operations on contexts --------------------------------------------===*/
77 static ManagedStatic<LLVMContext> GlobalContext;
79 LLVMContextRef LLVMContextCreate() {
80 return wrap(new LLVMContext());
83 LLVMContextRef LLVMGetGlobalContext() { return wrap(&*GlobalContext); }
85 void LLVMContextSetDiagnosticHandler(LLVMContextRef C,
86 LLVMDiagnosticHandler Handler,
87 void *DiagnosticContext) {
88 unwrap(C)->setDiagnosticHandler(
89 LLVM_EXTENSION reinterpret_cast<LLVMContext::DiagnosticHandlerTy>(
94 LLVMDiagnosticHandler LLVMContextGetDiagnosticHandler(LLVMContextRef C) {
95 return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>(
96 unwrap(C)->getDiagnosticHandler());
99 void *LLVMContextGetDiagnosticContext(LLVMContextRef C) {
100 return unwrap(C)->getDiagnosticContext();
103 void LLVMContextSetYieldCallback(LLVMContextRef C, LLVMYieldCallback Callback,
104 void *OpaqueHandle) {
106 LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
107 unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
110 void LLVMContextDispose(LLVMContextRef C) {
114 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char *Name,
116 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
119 unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) {
120 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
123 #define GET_ATTR_KIND_FROM_NAME
124 #include "AttributesCompatFunc.inc"
126 unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) {
127 return getAttrKindFromName(StringRef(Name, SLen));
130 unsigned LLVMGetLastEnumAttributeKind(void) {
131 return Attribute::AttrKind::EndAttrKinds;
134 LLVMAttributeRef LLVMCreateEnumAttribute(LLVMContextRef C, unsigned KindID,
136 return wrap(Attribute::get(*unwrap(C), (Attribute::AttrKind)KindID, Val));
139 unsigned LLVMGetEnumAttributeKind(LLVMAttributeRef A) {
140 return unwrap(A).getKindAsEnum();
143 uint64_t LLVMGetEnumAttributeValue(LLVMAttributeRef A) {
144 auto Attr = unwrap(A);
145 if (Attr.isEnumAttribute())
147 return Attr.getValueAsInt();
150 LLVMAttributeRef LLVMCreateStringAttribute(LLVMContextRef C,
151 const char *K, unsigned KLength,
152 const char *V, unsigned VLength) {
153 return wrap(Attribute::get(*unwrap(C), StringRef(K, KLength),
154 StringRef(V, VLength)));
157 const char *LLVMGetStringAttributeKind(LLVMAttributeRef A,
159 auto S = unwrap(A).getKindAsString();
164 const char *LLVMGetStringAttributeValue(LLVMAttributeRef A,
166 auto S = unwrap(A).getValueAsString();
171 LLVMBool LLVMIsEnumAttribute(LLVMAttributeRef A) {
172 auto Attr = unwrap(A);
173 return Attr.isEnumAttribute() || Attr.isIntAttribute();
176 LLVMBool LLVMIsStringAttribute(LLVMAttributeRef A) {
177 return unwrap(A).isStringAttribute();
180 char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) {
181 std::string MsgStorage;
182 raw_string_ostream Stream(MsgStorage);
183 DiagnosticPrinterRawOStream DP(Stream);
185 unwrap(DI)->print(DP);
188 return LLVMCreateMessage(MsgStorage.c_str());
191 LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI) {
192 LLVMDiagnosticSeverity severity;
194 switch(unwrap(DI)->getSeverity()) {
196 severity = LLVMDSError;
199 severity = LLVMDSWarning;
202 severity = LLVMDSRemark;
205 severity = LLVMDSNote;
212 /*===-- Operations on modules ---------------------------------------------===*/
214 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
215 return wrap(new Module(ModuleID, *GlobalContext));
218 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
220 return wrap(new Module(ModuleID, *unwrap(C)));
223 void LLVMDisposeModule(LLVMModuleRef M) {
227 const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) {
228 auto &Str = unwrap(M)->getModuleIdentifier();
233 void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) {
234 unwrap(M)->setModuleIdentifier(StringRef(Ident, Len));
238 /*--.. Data layout .........................................................--*/
239 const char *LLVMGetDataLayoutStr(LLVMModuleRef M) {
240 return unwrap(M)->getDataLayoutStr().c_str();
243 const char *LLVMGetDataLayout(LLVMModuleRef M) {
244 return LLVMGetDataLayoutStr(M);
247 void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
248 unwrap(M)->setDataLayout(DataLayoutStr);
251 /*--.. Target triple .......................................................--*/
252 const char * LLVMGetTarget(LLVMModuleRef M) {
253 return unwrap(M)->getTargetTriple().c_str();
256 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
257 unwrap(M)->setTargetTriple(Triple);
260 void LLVMDumpModule(LLVMModuleRef M) {
264 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
265 char **ErrorMessage) {
267 raw_fd_ostream dest(Filename, EC, sys::fs::F_Text);
269 *ErrorMessage = strdup(EC.message().c_str());
273 unwrap(M)->print(dest, nullptr);
277 if (dest.has_error()) {
278 *ErrorMessage = strdup("Error printing to file");
285 char *LLVMPrintModuleToString(LLVMModuleRef M) {
287 raw_string_ostream os(buf);
289 unwrap(M)->print(os, nullptr);
292 return strdup(buf.c_str());
295 /*--.. Operations on inline assembler ......................................--*/
296 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
297 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
301 /*--.. Operations on module contexts ......................................--*/
302 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
303 return wrap(&unwrap(M)->getContext());
307 /*===-- Operations on types -----------------------------------------------===*/
309 /*--.. Operations on all types (mostly) ....................................--*/
311 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
312 switch (unwrap(Ty)->getTypeID()) {
314 return LLVMVoidTypeKind;
316 return LLVMHalfTypeKind;
317 case Type::FloatTyID:
318 return LLVMFloatTypeKind;
319 case Type::DoubleTyID:
320 return LLVMDoubleTypeKind;
321 case Type::X86_FP80TyID:
322 return LLVMX86_FP80TypeKind;
323 case Type::FP128TyID:
324 return LLVMFP128TypeKind;
325 case Type::PPC_FP128TyID:
326 return LLVMPPC_FP128TypeKind;
327 case Type::LabelTyID:
328 return LLVMLabelTypeKind;
329 case Type::MetadataTyID:
330 return LLVMMetadataTypeKind;
331 case Type::IntegerTyID:
332 return LLVMIntegerTypeKind;
333 case Type::FunctionTyID:
334 return LLVMFunctionTypeKind;
335 case Type::StructTyID:
336 return LLVMStructTypeKind;
337 case Type::ArrayTyID:
338 return LLVMArrayTypeKind;
339 case Type::PointerTyID:
340 return LLVMPointerTypeKind;
341 case Type::VectorTyID:
342 return LLVMVectorTypeKind;
343 case Type::X86_MMXTyID:
344 return LLVMX86_MMXTypeKind;
345 case Type::TokenTyID:
346 return LLVMTokenTypeKind;
348 llvm_unreachable("Unhandled TypeID.");
351 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
353 return unwrap(Ty)->isSized();
356 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
357 return wrap(&unwrap(Ty)->getContext());
360 void LLVMDumpType(LLVMTypeRef Ty) {
361 return unwrap(Ty)->dump();
364 char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
366 raw_string_ostream os(buf);
369 unwrap(Ty)->print(os);
371 os << "Printing <null> Type";
375 return strdup(buf.c_str());
378 /*--.. Operations on integer types .........................................--*/
380 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
381 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
383 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
384 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
386 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
387 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
389 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
390 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
392 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
393 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
395 LLVMTypeRef LLVMInt128TypeInContext(LLVMContextRef C) {
396 return (LLVMTypeRef) Type::getInt128Ty(*unwrap(C));
398 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
399 return wrap(IntegerType::get(*unwrap(C), NumBits));
402 LLVMTypeRef LLVMInt1Type(void) {
403 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
405 LLVMTypeRef LLVMInt8Type(void) {
406 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
408 LLVMTypeRef LLVMInt16Type(void) {
409 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
411 LLVMTypeRef LLVMInt32Type(void) {
412 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
414 LLVMTypeRef LLVMInt64Type(void) {
415 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
417 LLVMTypeRef LLVMInt128Type(void) {
418 return LLVMInt128TypeInContext(LLVMGetGlobalContext());
420 LLVMTypeRef LLVMIntType(unsigned NumBits) {
421 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
424 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
425 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
428 /*--.. Operations on real types ............................................--*/
430 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
431 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
433 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
434 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
436 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
437 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
439 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
440 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
442 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
443 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
445 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
446 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
448 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
449 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
451 LLVMTypeRef LLVMTokenTypeInContext(LLVMContextRef C) {
452 return (LLVMTypeRef) Type::getTokenTy(*unwrap(C));
455 LLVMTypeRef LLVMHalfType(void) {
456 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
458 LLVMTypeRef LLVMFloatType(void) {
459 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
461 LLVMTypeRef LLVMDoubleType(void) {
462 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
464 LLVMTypeRef LLVMX86FP80Type(void) {
465 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
467 LLVMTypeRef LLVMFP128Type(void) {
468 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
470 LLVMTypeRef LLVMPPCFP128Type(void) {
471 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
473 LLVMTypeRef LLVMX86MMXType(void) {
474 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
477 /*--.. Operations on function types ........................................--*/
479 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
480 LLVMTypeRef *ParamTypes, unsigned ParamCount,
482 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
483 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
486 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
487 return unwrap<FunctionType>(FunctionTy)->isVarArg();
490 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
491 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
494 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
495 return unwrap<FunctionType>(FunctionTy)->getNumParams();
498 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
499 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
500 for (FunctionType::param_iterator I = Ty->param_begin(),
501 E = Ty->param_end(); I != E; ++I)
505 /*--.. Operations on struct types ..........................................--*/
507 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
508 unsigned ElementCount, LLVMBool Packed) {
509 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
510 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
513 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
514 unsigned ElementCount, LLVMBool Packed) {
515 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
516 ElementCount, Packed);
519 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
521 return wrap(StructType::create(*unwrap(C), Name));
524 const char *LLVMGetStructName(LLVMTypeRef Ty)
526 StructType *Type = unwrap<StructType>(Ty);
527 if (!Type->hasName())
529 return Type->getName().data();
532 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
533 unsigned ElementCount, LLVMBool Packed) {
534 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
535 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
538 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
539 return unwrap<StructType>(StructTy)->getNumElements();
542 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
543 StructType *Ty = unwrap<StructType>(StructTy);
544 for (StructType::element_iterator I = Ty->element_begin(),
545 E = Ty->element_end(); I != E; ++I)
549 LLVMTypeRef LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy, unsigned i) {
550 StructType *Ty = unwrap<StructType>(StructTy);
551 return wrap(Ty->getTypeAtIndex(i));
554 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
555 return unwrap<StructType>(StructTy)->isPacked();
558 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
559 return unwrap<StructType>(StructTy)->isOpaque();
562 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
563 return wrap(unwrap(M)->getTypeByName(Name));
566 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
568 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
569 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
572 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
573 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
576 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
577 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
580 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
581 return wrap(unwrap<SequentialType>(Ty)->getElementType());
584 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
585 return unwrap<ArrayType>(ArrayTy)->getNumElements();
588 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
589 return unwrap<PointerType>(PointerTy)->getAddressSpace();
592 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
593 return unwrap<VectorType>(VectorTy)->getNumElements();
596 /*--.. Operations on other types ...........................................--*/
598 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
599 return wrap(Type::getVoidTy(*unwrap(C)));
601 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
602 return wrap(Type::getLabelTy(*unwrap(C)));
605 LLVMTypeRef LLVMVoidType(void) {
606 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
608 LLVMTypeRef LLVMLabelType(void) {
609 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
612 /*===-- Operations on values ----------------------------------------------===*/
614 /*--.. Operations on all values ............................................--*/
616 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
617 return wrap(unwrap(Val)->getType());
620 LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) {
621 switch(unwrap(Val)->getValueID()) {
622 #define HANDLE_VALUE(Name) \
623 case Value::Name##Val: \
624 return LLVM##Name##ValueKind;
625 #include "llvm/IR/Value.def"
627 return LLVMInstructionValueKind;
631 const char *LLVMGetValueName(LLVMValueRef Val) {
632 return unwrap(Val)->getName().data();
635 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
636 unwrap(Val)->setName(Name);
639 void LLVMDumpValue(LLVMValueRef Val) {
643 char* LLVMPrintValueToString(LLVMValueRef Val) {
645 raw_string_ostream os(buf);
648 unwrap(Val)->print(os);
650 os << "Printing <null> Value";
654 return strdup(buf.c_str());
657 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
658 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
661 int LLVMHasMetadata(LLVMValueRef Inst) {
662 return unwrap<Instruction>(Inst)->hasMetadata();
665 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
666 auto *I = unwrap<Instruction>(Inst);
667 assert(I && "Expected instruction");
668 if (auto *MD = I->getMetadata(KindID))
669 return wrap(MetadataAsValue::get(I->getContext(), MD));
673 // MetadataAsValue uses a canonical format which strips the actual MDNode for
674 // MDNode with just a single constant value, storing just a ConstantAsMetadata
675 // This undoes this canonicalization, reconstructing the MDNode.
676 static MDNode *extractMDNode(MetadataAsValue *MAV) {
677 Metadata *MD = MAV->getMetadata();
678 assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
679 "Expected a metadata node or a canonicalized constant");
681 if (MDNode *N = dyn_cast<MDNode>(MD))
684 return MDNode::get(MAV->getContext(), MD);
687 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
688 MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
690 unwrap<Instruction>(Inst)->setMetadata(KindID, N);
693 /*--.. Conversion functions ................................................--*/
695 #define LLVM_DEFINE_VALUE_CAST(name) \
696 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
697 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
700 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
702 LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) {
703 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
704 if (isa<MDNode>(MD->getMetadata()) ||
705 isa<ValueAsMetadata>(MD->getMetadata()))
710 LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) {
711 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
712 if (isa<MDString>(MD->getMetadata()))
717 /*--.. Operations on Uses ..................................................--*/
718 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
719 Value *V = unwrap(Val);
720 Value::use_iterator I = V->use_begin();
721 if (I == V->use_end())
726 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
727 Use *Next = unwrap(U)->getNext();
733 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
734 return wrap(unwrap(U)->getUser());
737 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
738 return wrap(unwrap(U)->get());
741 /*--.. Operations on Users .................................................--*/
743 static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N,
745 Metadata *Op = N->getOperand(Index);
748 if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
749 return wrap(C->getValue());
750 return wrap(MetadataAsValue::get(Context, Op));
753 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
754 Value *V = unwrap(Val);
755 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
756 if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
757 assert(Index == 0 && "Function-local metadata can only have one operand");
758 return wrap(L->getValue());
760 return getMDNodeOperandImpl(V->getContext(),
761 cast<MDNode>(MD->getMetadata()), Index);
764 return wrap(cast<User>(V)->getOperand(Index));
767 LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) {
768 Value *V = unwrap(Val);
769 return wrap(&cast<User>(V)->getOperandUse(Index));
772 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
773 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
776 int LLVMGetNumOperands(LLVMValueRef Val) {
777 Value *V = unwrap(Val);
778 if (isa<MetadataAsValue>(V))
779 return LLVMGetMDNodeNumOperands(Val);
781 return cast<User>(V)->getNumOperands();
784 /*--.. Operations on constants of any type .................................--*/
786 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
787 return wrap(Constant::getNullValue(unwrap(Ty)));
790 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
791 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
794 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
795 return wrap(UndefValue::get(unwrap(Ty)));
798 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
799 return isa<Constant>(unwrap(Ty));
802 LLVMBool LLVMIsNull(LLVMValueRef Val) {
803 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
804 return C->isNullValue();
808 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
809 return isa<UndefValue>(unwrap(Val));
812 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
813 return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
816 /*--.. Operations on metadata nodes ........................................--*/
818 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
820 LLVMContext &Context = *unwrap(C);
821 return wrap(MetadataAsValue::get(
822 Context, MDString::get(Context, StringRef(Str, SLen))));
825 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
826 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
829 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
831 LLVMContext &Context = *unwrap(C);
832 SmallVector<Metadata *, 8> MDs;
833 for (auto *OV : makeArrayRef(Vals, Count)) {
834 Value *V = unwrap(OV);
838 else if (auto *C = dyn_cast<Constant>(V))
839 MD = ConstantAsMetadata::get(C);
840 else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
841 MD = MDV->getMetadata();
842 assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
843 "outside of direct argument to call");
845 // This is function-local metadata. Pretend to make an MDNode.
847 "Expected only one operand to function-local metadata");
848 return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V)));
853 return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs)));
856 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
857 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
860 const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
861 if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
862 if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
863 *Length = S->getString().size();
864 return S->getString().data();
870 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) {
871 auto *MD = cast<MetadataAsValue>(unwrap(V));
872 if (isa<ValueAsMetadata>(MD->getMetadata()))
874 return cast<MDNode>(MD->getMetadata())->getNumOperands();
877 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) {
878 auto *MD = cast<MetadataAsValue>(unwrap(V));
879 if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
880 *Dest = wrap(MDV->getValue());
883 const auto *N = cast<MDNode>(MD->getMetadata());
884 const unsigned numOperands = N->getNumOperands();
885 LLVMContext &Context = unwrap(V)->getContext();
886 for (unsigned i = 0; i < numOperands; i++)
887 Dest[i] = getMDNodeOperandImpl(Context, N, i);
890 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char *Name) {
891 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
892 return N->getNumOperands();
897 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char *Name,
898 LLVMValueRef *Dest) {
899 NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
902 LLVMContext &Context = unwrap(M)->getContext();
903 for (unsigned i=0;i<N->getNumOperands();i++)
904 Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
907 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char *Name,
909 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
914 N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
917 /*--.. Operations on scalar constants ......................................--*/
919 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
920 LLVMBool SignExtend) {
921 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
924 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
926 const uint64_t Words[]) {
927 IntegerType *Ty = unwrap<IntegerType>(IntTy);
928 return wrap(ConstantInt::get(Ty->getContext(),
929 APInt(Ty->getBitWidth(),
930 makeArrayRef(Words, NumWords))));
933 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
935 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
939 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
940 unsigned SLen, uint8_t Radix) {
941 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
945 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
946 return wrap(ConstantFP::get(unwrap(RealTy), N));
949 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
950 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
953 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
955 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
958 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
959 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
962 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
963 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
966 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
967 ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
968 Type *Ty = cFP->getType();
970 if (Ty->isFloatTy()) {
972 return cFP->getValueAPF().convertToFloat();
975 if (Ty->isDoubleTy()) {
977 return cFP->getValueAPF().convertToDouble();
981 APFloat APF = cFP->getValueAPF();
982 APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &APFLosesInfo);
983 *LosesInfo = APFLosesInfo;
984 return APF.convertToDouble();
987 /*--.. Operations on composite constants ...................................--*/
989 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
991 LLVMBool DontNullTerminate) {
992 /* Inverted the sense of AddNull because ', 0)' is a
993 better mnemonic for null termination than ', 1)'. */
994 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
995 DontNullTerminate == 0));
998 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
999 LLVMBool DontNullTerminate) {
1000 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
1004 LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef C, unsigned idx) {
1005 return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
1008 LLVMBool LLVMIsConstantString(LLVMValueRef C) {
1009 return unwrap<ConstantDataSequential>(C)->isString();
1012 const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1013 StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1014 *Length = Str.size();
1018 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
1019 LLVMValueRef *ConstantVals, unsigned Length) {
1020 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1021 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1024 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
1025 LLVMValueRef *ConstantVals,
1026 unsigned Count, LLVMBool Packed) {
1027 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1028 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
1032 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
1034 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1038 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
1039 LLVMValueRef *ConstantVals,
1041 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1042 StructType *Ty = cast<StructType>(unwrap(StructTy));
1044 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
1047 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
1048 return wrap(ConstantVector::get(makeArrayRef(
1049 unwrap<Constant>(ScalarConstantVals, Size), Size)));
1052 /*-- Opcode mapping */
1054 static LLVMOpcode map_to_llvmopcode(int opcode)
1057 default: llvm_unreachable("Unhandled Opcode.");
1058 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
1059 #include "llvm/IR/Instruction.def"
1064 static int map_from_llvmopcode(LLVMOpcode code)
1067 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
1068 #include "llvm/IR/Instruction.def"
1071 llvm_unreachable("Unhandled Opcode.");
1074 /*--.. Constant expressions ................................................--*/
1076 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
1077 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
1080 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
1081 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
1084 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
1085 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
1088 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
1089 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1092 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
1093 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
1096 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
1097 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
1101 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
1102 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
1105 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
1106 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
1109 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1110 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
1111 unwrap<Constant>(RHSConstant)));
1114 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
1115 LLVMValueRef RHSConstant) {
1116 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
1117 unwrap<Constant>(RHSConstant)));
1120 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
1121 LLVMValueRef RHSConstant) {
1122 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
1123 unwrap<Constant>(RHSConstant)));
1126 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1127 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
1128 unwrap<Constant>(RHSConstant)));
1131 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1132 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
1133 unwrap<Constant>(RHSConstant)));
1136 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
1137 LLVMValueRef RHSConstant) {
1138 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
1139 unwrap<Constant>(RHSConstant)));
1142 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
1143 LLVMValueRef RHSConstant) {
1144 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
1145 unwrap<Constant>(RHSConstant)));
1148 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1149 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
1150 unwrap<Constant>(RHSConstant)));
1153 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1154 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
1155 unwrap<Constant>(RHSConstant)));
1158 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
1159 LLVMValueRef RHSConstant) {
1160 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
1161 unwrap<Constant>(RHSConstant)));
1164 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
1165 LLVMValueRef RHSConstant) {
1166 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
1167 unwrap<Constant>(RHSConstant)));
1170 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1171 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
1172 unwrap<Constant>(RHSConstant)));
1175 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1176 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
1177 unwrap<Constant>(RHSConstant)));
1180 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1181 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
1182 unwrap<Constant>(RHSConstant)));
1185 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
1186 LLVMValueRef RHSConstant) {
1187 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
1188 unwrap<Constant>(RHSConstant)));
1191 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1192 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
1193 unwrap<Constant>(RHSConstant)));
1196 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1197 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
1198 unwrap<Constant>(RHSConstant)));
1201 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1202 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
1203 unwrap<Constant>(RHSConstant)));
1206 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1207 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
1208 unwrap<Constant>(RHSConstant)));
1211 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1212 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
1213 unwrap<Constant>(RHSConstant)));
1216 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1217 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
1218 unwrap<Constant>(RHSConstant)));
1221 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1222 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
1223 unwrap<Constant>(RHSConstant)));
1226 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
1227 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1228 return wrap(ConstantExpr::getICmp(Predicate,
1229 unwrap<Constant>(LHSConstant),
1230 unwrap<Constant>(RHSConstant)));
1233 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
1234 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1235 return wrap(ConstantExpr::getFCmp(Predicate,
1236 unwrap<Constant>(LHSConstant),
1237 unwrap<Constant>(RHSConstant)));
1240 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1241 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1242 unwrap<Constant>(RHSConstant)));
1245 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1246 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
1247 unwrap<Constant>(RHSConstant)));
1250 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1251 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1252 unwrap<Constant>(RHSConstant)));
1255 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
1256 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1257 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1259 return wrap(ConstantExpr::getGetElementPtr(
1260 nullptr, unwrap<Constant>(ConstantVal), IdxList));
1263 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
1264 LLVMValueRef *ConstantIndices,
1265 unsigned NumIndices) {
1266 Constant* Val = unwrap<Constant>(ConstantVal);
1267 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1269 return wrap(ConstantExpr::getInBoundsGetElementPtr(nullptr, Val, IdxList));
1272 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1273 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1277 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1278 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1282 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1283 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1287 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1288 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1292 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1293 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1297 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1298 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1302 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1303 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1307 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1308 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1312 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1313 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1317 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1318 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1322 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1323 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1327 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1328 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1332 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1333 LLVMTypeRef ToType) {
1334 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1338 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1339 LLVMTypeRef ToType) {
1340 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1344 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1345 LLVMTypeRef ToType) {
1346 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1350 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1351 LLVMTypeRef ToType) {
1352 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1356 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1357 LLVMTypeRef ToType) {
1358 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1362 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1363 LLVMBool isSigned) {
1364 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1365 unwrap(ToType), isSigned));
1368 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1369 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1373 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1374 LLVMValueRef ConstantIfTrue,
1375 LLVMValueRef ConstantIfFalse) {
1376 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1377 unwrap<Constant>(ConstantIfTrue),
1378 unwrap<Constant>(ConstantIfFalse)));
1381 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1382 LLVMValueRef IndexConstant) {
1383 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1384 unwrap<Constant>(IndexConstant)));
1387 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1388 LLVMValueRef ElementValueConstant,
1389 LLVMValueRef IndexConstant) {
1390 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1391 unwrap<Constant>(ElementValueConstant),
1392 unwrap<Constant>(IndexConstant)));
1395 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1396 LLVMValueRef VectorBConstant,
1397 LLVMValueRef MaskConstant) {
1398 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1399 unwrap<Constant>(VectorBConstant),
1400 unwrap<Constant>(MaskConstant)));
1403 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1405 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1406 makeArrayRef(IdxList, NumIdx)));
1409 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1410 LLVMValueRef ElementValueConstant,
1411 unsigned *IdxList, unsigned NumIdx) {
1412 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1413 unwrap<Constant>(ElementValueConstant),
1414 makeArrayRef(IdxList, NumIdx)));
1417 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1418 const char *Constraints,
1419 LLVMBool HasSideEffects,
1420 LLVMBool IsAlignStack) {
1421 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1422 Constraints, HasSideEffects, IsAlignStack));
1425 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1426 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1429 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1431 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1432 return wrap(unwrap<GlobalValue>(Global)->getParent());
1435 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1436 return unwrap<GlobalValue>(Global)->isDeclaration();
1439 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1440 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1441 case GlobalValue::ExternalLinkage:
1442 return LLVMExternalLinkage;
1443 case GlobalValue::AvailableExternallyLinkage:
1444 return LLVMAvailableExternallyLinkage;
1445 case GlobalValue::LinkOnceAnyLinkage:
1446 return LLVMLinkOnceAnyLinkage;
1447 case GlobalValue::LinkOnceODRLinkage:
1448 return LLVMLinkOnceODRLinkage;
1449 case GlobalValue::WeakAnyLinkage:
1450 return LLVMWeakAnyLinkage;
1451 case GlobalValue::WeakODRLinkage:
1452 return LLVMWeakODRLinkage;
1453 case GlobalValue::AppendingLinkage:
1454 return LLVMAppendingLinkage;
1455 case GlobalValue::InternalLinkage:
1456 return LLVMInternalLinkage;
1457 case GlobalValue::PrivateLinkage:
1458 return LLVMPrivateLinkage;
1459 case GlobalValue::ExternalWeakLinkage:
1460 return LLVMExternalWeakLinkage;
1461 case GlobalValue::CommonLinkage:
1462 return LLVMCommonLinkage;
1465 llvm_unreachable("Invalid GlobalValue linkage!");
1468 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1469 GlobalValue *GV = unwrap<GlobalValue>(Global);
1472 case LLVMExternalLinkage:
1473 GV->setLinkage(GlobalValue::ExternalLinkage);
1475 case LLVMAvailableExternallyLinkage:
1476 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1478 case LLVMLinkOnceAnyLinkage:
1479 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1481 case LLVMLinkOnceODRLinkage:
1482 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1484 case LLVMLinkOnceODRAutoHideLinkage:
1485 DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1486 "longer supported.");
1488 case LLVMWeakAnyLinkage:
1489 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1491 case LLVMWeakODRLinkage:
1492 GV->setLinkage(GlobalValue::WeakODRLinkage);
1494 case LLVMAppendingLinkage:
1495 GV->setLinkage(GlobalValue::AppendingLinkage);
1497 case LLVMInternalLinkage:
1498 GV->setLinkage(GlobalValue::InternalLinkage);
1500 case LLVMPrivateLinkage:
1501 GV->setLinkage(GlobalValue::PrivateLinkage);
1503 case LLVMLinkerPrivateLinkage:
1504 GV->setLinkage(GlobalValue::PrivateLinkage);
1506 case LLVMLinkerPrivateWeakLinkage:
1507 GV->setLinkage(GlobalValue::PrivateLinkage);
1509 case LLVMDLLImportLinkage:
1511 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1513 case LLVMDLLExportLinkage:
1515 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1517 case LLVMExternalWeakLinkage:
1518 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1520 case LLVMGhostLinkage:
1522 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1524 case LLVMCommonLinkage:
1525 GV->setLinkage(GlobalValue::CommonLinkage);
1530 const char *LLVMGetSection(LLVMValueRef Global) {
1531 // Using .data() is safe because of how GlobalObject::setSection is
1533 return unwrap<GlobalValue>(Global)->getSection().data();
1536 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1537 unwrap<GlobalObject>(Global)->setSection(Section);
1540 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1541 return static_cast<LLVMVisibility>(
1542 unwrap<GlobalValue>(Global)->getVisibility());
1545 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1546 unwrap<GlobalValue>(Global)
1547 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1550 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
1551 return static_cast<LLVMDLLStorageClass>(
1552 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1555 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
1556 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1557 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1560 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
1561 return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
1564 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
1565 unwrap<GlobalValue>(Global)->setUnnamedAddr(
1566 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
1567 : GlobalValue::UnnamedAddr::None);
1570 /*--.. Operations on global variables, load and store instructions .........--*/
1572 unsigned LLVMGetAlignment(LLVMValueRef V) {
1573 Value *P = unwrap<Value>(V);
1574 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1575 return GV->getAlignment();
1576 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1577 return AI->getAlignment();
1578 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1579 return LI->getAlignment();
1580 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1581 return SI->getAlignment();
1584 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1587 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1588 Value *P = unwrap<Value>(V);
1589 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
1590 GV->setAlignment(Bytes);
1591 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1592 AI->setAlignment(Bytes);
1593 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1594 LI->setAlignment(Bytes);
1595 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1596 SI->setAlignment(Bytes);
1599 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1602 /*--.. Operations on global variables ......................................--*/
1604 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1605 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1606 GlobalValue::ExternalLinkage, nullptr, Name));
1609 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1611 unsigned AddressSpace) {
1612 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1613 GlobalValue::ExternalLinkage, nullptr, Name,
1614 nullptr, GlobalVariable::NotThreadLocal,
1618 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1619 return wrap(unwrap(M)->getNamedGlobal(Name));
1622 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1623 Module *Mod = unwrap(M);
1624 Module::global_iterator I = Mod->global_begin();
1625 if (I == Mod->global_end())
1630 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1631 Module *Mod = unwrap(M);
1632 Module::global_iterator I = Mod->global_end();
1633 if (I == Mod->global_begin())
1638 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1639 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1640 Module::global_iterator I(GV);
1641 if (++I == GV->getParent()->global_end())
1646 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1647 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1648 Module::global_iterator I(GV);
1649 if (I == GV->getParent()->global_begin())
1654 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1655 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1658 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1659 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1660 if ( !GV->hasInitializer() )
1662 return wrap(GV->getInitializer());
1665 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1666 unwrap<GlobalVariable>(GlobalVar)
1667 ->setInitializer(unwrap<Constant>(ConstantVal));
1670 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1671 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1674 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1675 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1678 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1679 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1682 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1683 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1686 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1687 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1688 case GlobalVariable::NotThreadLocal:
1689 return LLVMNotThreadLocal;
1690 case GlobalVariable::GeneralDynamicTLSModel:
1691 return LLVMGeneralDynamicTLSModel;
1692 case GlobalVariable::LocalDynamicTLSModel:
1693 return LLVMLocalDynamicTLSModel;
1694 case GlobalVariable::InitialExecTLSModel:
1695 return LLVMInitialExecTLSModel;
1696 case GlobalVariable::LocalExecTLSModel:
1697 return LLVMLocalExecTLSModel;
1700 llvm_unreachable("Invalid GlobalVariable thread local mode");
1703 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1704 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1707 case LLVMNotThreadLocal:
1708 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1710 case LLVMGeneralDynamicTLSModel:
1711 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1713 case LLVMLocalDynamicTLSModel:
1714 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1716 case LLVMInitialExecTLSModel:
1717 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1719 case LLVMLocalExecTLSModel:
1720 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1725 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1726 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1729 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1730 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1733 /*--.. Operations on aliases ......................................--*/
1735 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1737 auto *PTy = cast<PointerType>(unwrap(Ty));
1738 return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
1739 GlobalValue::ExternalLinkage, Name,
1740 unwrap<Constant>(Aliasee), unwrap(M)));
1743 /*--.. Operations on functions .............................................--*/
1745 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1746 LLVMTypeRef FunctionTy) {
1747 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1748 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1751 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1752 return wrap(unwrap(M)->getFunction(Name));
1755 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1756 Module *Mod = unwrap(M);
1757 Module::iterator I = Mod->begin();
1758 if (I == Mod->end())
1763 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1764 Module *Mod = unwrap(M);
1765 Module::iterator I = Mod->end();
1766 if (I == Mod->begin())
1771 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1772 Function *Func = unwrap<Function>(Fn);
1773 Module::iterator I(Func);
1774 if (++I == Func->getParent()->end())
1779 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1780 Function *Func = unwrap<Function>(Fn);
1781 Module::iterator I(Func);
1782 if (I == Func->getParent()->begin())
1787 void LLVMDeleteFunction(LLVMValueRef Fn) {
1788 unwrap<Function>(Fn)->eraseFromParent();
1791 LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) {
1792 return unwrap<Function>(Fn)->hasPersonalityFn();
1795 LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) {
1796 return wrap(unwrap<Function>(Fn)->getPersonalityFn());
1799 void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) {
1800 unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
1803 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1804 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1805 return F->getIntrinsicID();
1809 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1810 return unwrap<Function>(Fn)->getCallingConv();
1813 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1814 return unwrap<Function>(Fn)->setCallingConv(
1815 static_cast<CallingConv::ID>(CC));
1818 const char *LLVMGetGC(LLVMValueRef Fn) {
1819 Function *F = unwrap<Function>(Fn);
1820 return F->hasGC()? F->getGC().c_str() : nullptr;
1823 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1824 Function *F = unwrap<Function>(Fn);
1831 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1832 Function *Func = unwrap<Function>(Fn);
1833 const AttributeSet PAL = Func->getAttributes();
1835 const AttributeSet PALnew =
1836 PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1837 AttributeSet::get(Func->getContext(),
1838 AttributeSet::FunctionIndex, B));
1839 Func->setAttributes(PALnew);
1842 void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1843 LLVMAttributeRef A) {
1844 unwrap<Function>(F)->addAttribute(Idx, unwrap(A));
1847 LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F,
1848 LLVMAttributeIndex Idx,
1850 return wrap(unwrap<Function>(F)->getAttribute(Idx,
1851 (Attribute::AttrKind)KindID));
1854 LLVMAttributeRef LLVMGetStringAttributeAtIndex(LLVMValueRef F,
1855 LLVMAttributeIndex Idx,
1856 const char *K, unsigned KLen) {
1857 return wrap(unwrap<Function>(F)->getAttribute(Idx, StringRef(K, KLen)));
1860 void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1862 unwrap<Function>(F)->removeAttribute(Idx, (Attribute::AttrKind)KindID);
1865 void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1866 const char *K, unsigned KLen) {
1867 unwrap<Function>(F)->removeAttribute(Idx, StringRef(K, KLen));
1870 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1872 Function *Func = unwrap<Function>(Fn);
1873 AttributeSet::AttrIndex Idx =
1874 AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
1877 B.addAttribute(A, V);
1878 AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
1879 Func->addAttributes(Idx, Set);
1882 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1883 Function *Func = unwrap<Function>(Fn);
1884 const AttributeSet PAL = Func->getAttributes();
1886 const AttributeSet PALnew =
1887 PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1888 AttributeSet::get(Func->getContext(),
1889 AttributeSet::FunctionIndex, B));
1890 Func->setAttributes(PALnew);
1893 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1894 Function *Func = unwrap<Function>(Fn);
1895 const AttributeSet PAL = Func->getAttributes();
1896 return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
1899 /*--.. Operations on parameters ............................................--*/
1901 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1902 // This function is strictly redundant to
1903 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1904 return unwrap<Function>(FnRef)->arg_size();
1907 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1908 Function *Fn = unwrap<Function>(FnRef);
1909 for (Function::arg_iterator I = Fn->arg_begin(),
1910 E = Fn->arg_end(); I != E; I++)
1911 *ParamRefs++ = wrap(&*I);
1914 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1915 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1921 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1922 return wrap(unwrap<Argument>(V)->getParent());
1925 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1926 Function *Func = unwrap<Function>(Fn);
1927 Function::arg_iterator I = Func->arg_begin();
1928 if (I == Func->arg_end())
1933 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1934 Function *Func = unwrap<Function>(Fn);
1935 Function::arg_iterator I = Func->arg_end();
1936 if (I == Func->arg_begin())
1941 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1942 Argument *A = unwrap<Argument>(Arg);
1943 Function::arg_iterator I(A);
1944 if (++I == A->getParent()->arg_end())
1949 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1950 Argument *A = unwrap<Argument>(Arg);
1951 Function::arg_iterator I(A);
1952 if (I == A->getParent()->arg_begin())
1957 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1958 Argument *A = unwrap<Argument>(Arg);
1960 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1963 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1964 Argument *A = unwrap<Argument>(Arg);
1966 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1969 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1970 Argument *A = unwrap<Argument>(Arg);
1971 return (LLVMAttribute)A->getParent()->getAttributes().
1972 Raw(A->getArgNo()+1);
1975 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1976 Argument *A = unwrap<Argument>(Arg);
1978 B.addAlignmentAttr(align);
1979 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
1982 /*--.. Operations on basic blocks ..........................................--*/
1984 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1985 return wrap(static_cast<Value*>(unwrap(BB)));
1988 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1989 return isa<BasicBlock>(unwrap(Val));
1992 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1993 return wrap(unwrap<BasicBlock>(Val));
1996 const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) {
1997 return unwrap(BB)->getName().data();
2000 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
2001 return wrap(unwrap(BB)->getParent());
2004 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
2005 return wrap(unwrap(BB)->getTerminator());
2008 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
2009 return unwrap<Function>(FnRef)->size();
2012 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
2013 Function *Fn = unwrap<Function>(FnRef);
2014 for (BasicBlock &BB : *Fn)
2015 *BasicBlocksRefs++ = wrap(&BB);
2018 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
2019 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2022 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
2023 Function *Func = unwrap<Function>(Fn);
2024 Function::iterator I = Func->begin();
2025 if (I == Func->end())
2030 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
2031 Function *Func = unwrap<Function>(Fn);
2032 Function::iterator I = Func->end();
2033 if (I == Func->begin())
2038 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
2039 BasicBlock *Block = unwrap(BB);
2040 Function::iterator I(Block);
2041 if (++I == Block->getParent()->end())
2046 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
2047 BasicBlock *Block = unwrap(BB);
2048 Function::iterator I(Block);
2049 if (I == Block->getParent()->begin())
2054 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
2057 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
2060 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
2061 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
2064 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
2065 LLVMBasicBlockRef BBRef,
2067 BasicBlock *BB = unwrap(BBRef);
2068 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2071 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
2073 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
2076 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
2077 unwrap(BBRef)->eraseFromParent();
2080 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
2081 unwrap(BBRef)->removeFromParent();
2084 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2085 unwrap(BB)->moveBefore(unwrap(MovePos));
2088 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2089 unwrap(BB)->moveAfter(unwrap(MovePos));
2092 /*--.. Operations on instructions ..........................................--*/
2094 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
2095 return wrap(unwrap<Instruction>(Inst)->getParent());
2098 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
2099 BasicBlock *Block = unwrap(BB);
2100 BasicBlock::iterator I = Block->begin();
2101 if (I == Block->end())
2106 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
2107 BasicBlock *Block = unwrap(BB);
2108 BasicBlock::iterator I = Block->end();
2109 if (I == Block->begin())
2114 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
2115 Instruction *Instr = unwrap<Instruction>(Inst);
2116 BasicBlock::iterator I(Instr);
2117 if (++I == Instr->getParent()->end())
2122 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
2123 Instruction *Instr = unwrap<Instruction>(Inst);
2124 BasicBlock::iterator I(Instr);
2125 if (I == Instr->getParent()->begin())
2130 void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) {
2131 unwrap<Instruction>(Inst)->removeFromParent();
2134 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
2135 unwrap<Instruction>(Inst)->eraseFromParent();
2138 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
2139 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2140 return (LLVMIntPredicate)I->getPredicate();
2141 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2142 if (CE->getOpcode() == Instruction::ICmp)
2143 return (LLVMIntPredicate)CE->getPredicate();
2144 return (LLVMIntPredicate)0;
2147 LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
2148 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2149 return (LLVMRealPredicate)I->getPredicate();
2150 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2151 if (CE->getOpcode() == Instruction::FCmp)
2152 return (LLVMRealPredicate)CE->getPredicate();
2153 return (LLVMRealPredicate)0;
2156 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
2157 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2158 return map_to_llvmopcode(C->getOpcode());
2159 return (LLVMOpcode)0;
2162 LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {
2163 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2164 return wrap(C->clone());
2168 /*--.. Call and invoke instructions ........................................--*/
2170 unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) {
2171 return CallSite(unwrap<Instruction>(Instr)).getNumArgOperands();
2174 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
2175 return CallSite(unwrap<Instruction>(Instr)).getCallingConv();
2178 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
2179 return CallSite(unwrap<Instruction>(Instr))
2180 .setCallingConv(static_cast<CallingConv::ID>(CC));
2183 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
2185 CallSite Call = CallSite(unwrap<Instruction>(Instr));
2188 Call.getAttributes().addAttributes(Call->getContext(), index,
2189 AttributeSet::get(Call->getContext(),
2193 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
2195 CallSite Call = CallSite(unwrap<Instruction>(Instr));
2197 Call.setAttributes(Call.getAttributes()
2198 .removeAttributes(Call->getContext(), index,
2199 AttributeSet::get(Call->getContext(),
2203 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
2205 CallSite Call = CallSite(unwrap<Instruction>(Instr));
2207 B.addAlignmentAttr(align);
2208 Call.setAttributes(Call.getAttributes()
2209 .addAttributes(Call->getContext(), index,
2210 AttributeSet::get(Call->getContext(),
2214 void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2215 LLVMAttributeRef A) {
2216 CallSite(unwrap<Instruction>(C)).addAttribute(Idx, unwrap(A));
2219 LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C,
2220 LLVMAttributeIndex Idx,
2222 return wrap(CallSite(unwrap<Instruction>(C))
2223 .getAttribute(Idx, (Attribute::AttrKind)KindID));
2226 LLVMAttributeRef LLVMGetCallSiteStringAttribute(LLVMValueRef C,
2227 LLVMAttributeIndex Idx,
2228 const char *K, unsigned KLen) {
2229 return wrap(CallSite(unwrap<Instruction>(C))
2230 .getAttribute(Idx, StringRef(K, KLen)));
2233 void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2235 CallSite(unwrap<Instruction>(C))
2236 .removeAttribute(Idx, (Attribute::AttrKind)KindID);
2239 void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2240 const char *K, unsigned KLen) {
2241 CallSite(unwrap<Instruction>(C)).removeAttribute(Idx, StringRef(K, KLen));
2244 LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) {
2245 return wrap(CallSite(unwrap<Instruction>(Instr)).getCalledValue());
2248 /*--.. Operations on call instructions (only) ..............................--*/
2250 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
2251 return unwrap<CallInst>(Call)->isTailCall();
2254 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
2255 unwrap<CallInst>(Call)->setTailCall(isTailCall);
2258 /*--.. Operations on invoke instructions (only) ............................--*/
2260 LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) {
2261 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2264 LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) {
2265 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2268 void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2269 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2272 void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2273 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2276 /*--.. Operations on terminators ...........................................--*/
2278 unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {
2279 return unwrap<TerminatorInst>(Term)->getNumSuccessors();
2282 LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) {
2283 return wrap(unwrap<TerminatorInst>(Term)->getSuccessor(i));
2286 void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
2287 return unwrap<TerminatorInst>(Term)->setSuccessor(i,unwrap(block));
2290 /*--.. Operations on branch instructions (only) ............................--*/
2292 LLVMBool LLVMIsConditional(LLVMValueRef Branch) {
2293 return unwrap<BranchInst>(Branch)->isConditional();
2296 LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) {
2297 return wrap(unwrap<BranchInst>(Branch)->getCondition());
2300 void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) {
2301 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2304 /*--.. Operations on switch instructions (only) ............................--*/
2306 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
2307 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2310 /*--.. Operations on alloca instructions (only) ............................--*/
2312 LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) {
2313 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
2316 /*--.. Operations on gep instructions (only) ...............................--*/
2318 LLVMBool LLVMIsInBounds(LLVMValueRef GEP) {
2319 return unwrap<GetElementPtrInst>(GEP)->isInBounds();
2322 void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) {
2323 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
2326 /*--.. Operations on phi nodes .............................................--*/
2328 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
2329 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
2330 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
2331 for (unsigned I = 0; I != Count; ++I)
2332 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
2335 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
2336 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
2339 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
2340 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
2343 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
2344 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
2347 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/
2349 unsigned LLVMGetNumIndices(LLVMValueRef Inst) {
2350 auto *I = unwrap(Inst);
2351 if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
2352 return GEP->getNumIndices();
2353 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2354 return EV->getNumIndices();
2355 if (auto *IV = dyn_cast<InsertValueInst>(I))
2356 return IV->getNumIndices();
2358 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
2361 const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
2362 auto *I = unwrap(Inst);
2363 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2364 return EV->getIndices().data();
2365 if (auto *IV = dyn_cast<InsertValueInst>(I))
2366 return IV->getIndices().data();
2368 "LLVMGetIndices applies only to extractvalue and insertvalue!");
2372 /*===-- Instruction builders ----------------------------------------------===*/
2374 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
2375 return wrap(new IRBuilder<>(*unwrap(C)));
2378 LLVMBuilderRef LLVMCreateBuilder(void) {
2379 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
2382 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
2383 LLVMValueRef Instr) {
2384 BasicBlock *BB = unwrap(Block);
2385 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
2386 unwrap(Builder)->SetInsertPoint(BB, I->getIterator());
2389 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2390 Instruction *I = unwrap<Instruction>(Instr);
2391 unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
2394 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
2395 BasicBlock *BB = unwrap(Block);
2396 unwrap(Builder)->SetInsertPoint(BB);
2399 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
2400 return wrap(unwrap(Builder)->GetInsertBlock());
2403 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
2404 unwrap(Builder)->ClearInsertionPoint();
2407 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2408 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
2411 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
2413 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
2416 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
2417 delete unwrap(Builder);
2420 /*--.. Metadata builders ...................................................--*/
2422 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
2424 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
2425 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
2428 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
2429 LLVMContext &Context = unwrap(Builder)->getContext();
2430 return wrap(MetadataAsValue::get(
2431 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
2434 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
2435 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
2439 /*--.. Instruction builders ................................................--*/
2441 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
2442 return wrap(unwrap(B)->CreateRetVoid());
2445 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
2446 return wrap(unwrap(B)->CreateRet(unwrap(V)));
2449 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
2451 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
2454 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
2455 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
2458 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
2459 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
2460 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
2463 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
2464 LLVMBasicBlockRef Else, unsigned NumCases) {
2465 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
2468 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
2469 unsigned NumDests) {
2470 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
2473 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
2474 LLVMValueRef *Args, unsigned NumArgs,
2475 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
2477 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
2478 makeArrayRef(unwrap(Args), NumArgs),
2482 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
2483 LLVMValueRef PersFn, unsigned NumClauses,
2485 // The personality used to live on the landingpad instruction, but now it
2486 // lives on the parent function. For compatibility, take the provided
2487 // personality and put it on the parent function.
2489 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
2490 cast<Function>(unwrap(PersFn)));
2491 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
2494 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
2495 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
2498 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
2499 return wrap(unwrap(B)->CreateUnreachable());
2502 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
2503 LLVMBasicBlockRef Dest) {
2504 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
2507 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
2508 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
2511 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
2512 return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
2515 LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
2516 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
2519 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
2520 unwrap<LandingPadInst>(LandingPad)->
2521 addClause(cast<Constant>(unwrap(ClauseVal)));
2524 LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) {
2525 return unwrap<LandingPadInst>(LandingPad)->isCleanup();
2528 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
2529 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2532 /*--.. Arithmetic ..........................................................--*/
2534 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2536 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2539 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2541 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2544 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2546 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2549 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2551 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2554 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2556 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2559 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2561 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2564 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2566 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2569 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2571 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2574 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2576 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2579 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2581 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2584 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2586 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2589 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2591 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2594 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2596 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2599 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2601 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2604 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2605 LLVMValueRef RHS, const char *Name) {
2606 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2609 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2611 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2614 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2616 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2619 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2621 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2624 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2626 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2629 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2631 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2634 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2636 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2639 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2641 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2644 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2646 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2649 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2651 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2654 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2656 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2659 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2660 LLVMValueRef LHS, LLVMValueRef RHS,
2662 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2663 unwrap(RHS), Name));
2666 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2667 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2670 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2672 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2675 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2677 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2680 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2681 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2684 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2685 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2688 /*--.. Memory ..............................................................--*/
2690 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2692 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2693 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2694 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2695 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2696 ITy, unwrap(Ty), AllocSize,
2697 nullptr, nullptr, "");
2698 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2701 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2702 LLVMValueRef Val, const char *Name) {
2703 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2704 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2705 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2706 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2707 ITy, unwrap(Ty), AllocSize,
2708 unwrap(Val), nullptr, "");
2709 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2712 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2714 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
2717 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2718 LLVMValueRef Val, const char *Name) {
2719 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2722 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2723 return wrap(unwrap(B)->Insert(
2724 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2727 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2729 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2732 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2733 LLVMValueRef PointerVal) {
2734 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2737 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
2739 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic;
2740 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered;
2741 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic;
2742 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire;
2743 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release;
2744 case LLVMAtomicOrderingAcquireRelease:
2745 return AtomicOrdering::AcquireRelease;
2746 case LLVMAtomicOrderingSequentiallyConsistent:
2747 return AtomicOrdering::SequentiallyConsistent;
2750 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
2753 static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) {
2755 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic;
2756 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered;
2757 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic;
2758 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire;
2759 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease;
2760 case AtomicOrdering::AcquireRelease:
2761 return LLVMAtomicOrderingAcquireRelease;
2762 case AtomicOrdering::SequentiallyConsistent:
2763 return LLVMAtomicOrderingSequentiallyConsistent;
2766 llvm_unreachable("Invalid AtomicOrdering value!");
2769 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
2770 LLVMBool isSingleThread, const char *Name) {
2772 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
2773 isSingleThread ? SingleThread : CrossThread,
2777 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2778 LLVMValueRef *Indices, unsigned NumIndices,
2780 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2781 return wrap(unwrap(B)->CreateGEP(nullptr, unwrap(Pointer), IdxList, Name));
2784 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2785 LLVMValueRef *Indices, unsigned NumIndices,
2787 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2789 unwrap(B)->CreateInBoundsGEP(nullptr, unwrap(Pointer), IdxList, Name));
2792 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2793 unsigned Idx, const char *Name) {
2794 return wrap(unwrap(B)->CreateStructGEP(nullptr, unwrap(Pointer), Idx, Name));
2797 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2799 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2802 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2804 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2807 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2808 Value *P = unwrap<Value>(MemAccessInst);
2809 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2810 return LI->isVolatile();
2811 return cast<StoreInst>(P)->isVolatile();
2814 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2815 Value *P = unwrap<Value>(MemAccessInst);
2816 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2817 return LI->setVolatile(isVolatile);
2818 return cast<StoreInst>(P)->setVolatile(isVolatile);
2821 LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) {
2822 Value *P = unwrap<Value>(MemAccessInst);
2824 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2825 O = LI->getOrdering();
2827 O = cast<StoreInst>(P)->getOrdering();
2828 return mapToLLVMOrdering(O);
2831 void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
2832 Value *P = unwrap<Value>(MemAccessInst);
2833 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
2835 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2836 return LI->setOrdering(O);
2837 return cast<StoreInst>(P)->setOrdering(O);
2840 /*--.. Casts ...............................................................--*/
2842 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2843 LLVMTypeRef DestTy, const char *Name) {
2844 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2847 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2848 LLVMTypeRef DestTy, const char *Name) {
2849 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2852 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2853 LLVMTypeRef DestTy, const char *Name) {
2854 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2857 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2858 LLVMTypeRef DestTy, const char *Name) {
2859 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2862 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2863 LLVMTypeRef DestTy, const char *Name) {
2864 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2867 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2868 LLVMTypeRef DestTy, const char *Name) {
2869 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2872 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2873 LLVMTypeRef DestTy, const char *Name) {
2874 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2877 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2878 LLVMTypeRef DestTy, const char *Name) {
2879 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2882 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2883 LLVMTypeRef DestTy, const char *Name) {
2884 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2887 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2888 LLVMTypeRef DestTy, const char *Name) {
2889 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2892 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2893 LLVMTypeRef DestTy, const char *Name) {
2894 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2897 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2898 LLVMTypeRef DestTy, const char *Name) {
2899 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2902 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
2903 LLVMTypeRef DestTy, const char *Name) {
2904 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
2907 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2908 LLVMTypeRef DestTy, const char *Name) {
2909 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2913 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2914 LLVMTypeRef DestTy, const char *Name) {
2915 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2919 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2920 LLVMTypeRef DestTy, const char *Name) {
2921 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2925 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2926 LLVMTypeRef DestTy, const char *Name) {
2927 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2928 unwrap(DestTy), Name));
2931 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2932 LLVMTypeRef DestTy, const char *Name) {
2933 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2936 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2937 LLVMTypeRef DestTy, const char *Name) {
2938 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2939 /*isSigned*/true, Name));
2942 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2943 LLVMTypeRef DestTy, const char *Name) {
2944 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2947 /*--.. Comparisons .........................................................--*/
2949 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2950 LLVMValueRef LHS, LLVMValueRef RHS,
2952 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2953 unwrap(LHS), unwrap(RHS), Name));
2956 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2957 LLVMValueRef LHS, LLVMValueRef RHS,
2959 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2960 unwrap(LHS), unwrap(RHS), Name));
2963 /*--.. Miscellaneous instructions ..........................................--*/
2965 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2966 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2969 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2970 LLVMValueRef *Args, unsigned NumArgs,
2972 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2973 makeArrayRef(unwrap(Args), NumArgs),
2977 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2978 LLVMValueRef Then, LLVMValueRef Else,
2980 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2984 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2985 LLVMTypeRef Ty, const char *Name) {
2986 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2989 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2990 LLVMValueRef Index, const char *Name) {
2991 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2995 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2996 LLVMValueRef EltVal, LLVMValueRef Index,
2998 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2999 unwrap(Index), Name));
3002 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
3003 LLVMValueRef V2, LLVMValueRef Mask,
3005 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
3006 unwrap(Mask), Name));
3009 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
3010 unsigned Index, const char *Name) {
3011 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
3014 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
3015 LLVMValueRef EltVal, unsigned Index,
3017 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
3021 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
3023 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
3026 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
3028 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
3031 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
3032 LLVMValueRef RHS, const char *Name) {
3033 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
3036 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
3037 LLVMValueRef PTR, LLVMValueRef Val,
3038 LLVMAtomicOrdering ordering,
3039 LLVMBool singleThread) {
3040 AtomicRMWInst::BinOp intop;
3042 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
3043 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
3044 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
3045 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
3046 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
3047 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
3048 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
3049 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
3050 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
3051 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
3052 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
3054 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
3055 mapFromLLVMOrdering(ordering), singleThread ? SingleThread : CrossThread));
3058 LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr,
3059 LLVMValueRef Cmp, LLVMValueRef New,
3060 LLVMAtomicOrdering SuccessOrdering,
3061 LLVMAtomicOrdering FailureOrdering,
3062 LLVMBool singleThread) {
3064 return wrap(unwrap(B)->CreateAtomicCmpXchg(unwrap(Ptr), unwrap(Cmp),
3065 unwrap(New), mapFromLLVMOrdering(SuccessOrdering),
3066 mapFromLLVMOrdering(FailureOrdering),
3067 singleThread ? SingleThread : CrossThread));
3071 LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) {
3072 Value *P = unwrap<Value>(AtomicInst);
3074 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
3075 return I->getSynchScope() == SingleThread;
3076 return cast<AtomicCmpXchgInst>(P)->getSynchScope() == SingleThread;
3079 void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) {
3080 Value *P = unwrap<Value>(AtomicInst);
3081 SynchronizationScope Sync = NewValue ? SingleThread : CrossThread;
3083 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
3084 return I->setSynchScope(Sync);
3085 return cast<AtomicCmpXchgInst>(P)->setSynchScope(Sync);
3088 LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) {
3089 Value *P = unwrap<Value>(CmpXchgInst);
3090 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering());
3093 void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst,
3094 LLVMAtomicOrdering Ordering) {
3095 Value *P = unwrap<Value>(CmpXchgInst);
3096 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3098 return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O);
3101 LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst) {
3102 Value *P = unwrap<Value>(CmpXchgInst);
3103 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering());
3106 void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst,
3107 LLVMAtomicOrdering Ordering) {
3108 Value *P = unwrap<Value>(CmpXchgInst);
3109 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3111 return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O);
3114 /*===-- Module providers --------------------------------------------------===*/
3116 LLVMModuleProviderRef
3117 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
3118 return reinterpret_cast<LLVMModuleProviderRef>(M);
3121 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
3126 /*===-- Memory buffers ----------------------------------------------------===*/
3128 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
3130 LLVMMemoryBufferRef *OutMemBuf,
3131 char **OutMessage) {
3133 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
3134 if (std::error_code EC = MBOrErr.getError()) {
3135 *OutMessage = strdup(EC.message().c_str());
3138 *OutMemBuf = wrap(MBOrErr.get().release());
3142 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
3143 char **OutMessage) {
3144 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
3145 if (std::error_code EC = MBOrErr.getError()) {
3146 *OutMessage = strdup(EC.message().c_str());
3149 *OutMemBuf = wrap(MBOrErr.get().release());
3153 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
3154 const char *InputData,
3155 size_t InputDataLength,
3156 const char *BufferName,
3157 LLVMBool RequiresNullTerminator) {
3159 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
3160 StringRef(BufferName),
3161 RequiresNullTerminator).release());
3164 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
3165 const char *InputData,
3166 size_t InputDataLength,
3167 const char *BufferName) {
3170 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
3171 StringRef(BufferName)).release());
3174 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
3175 return unwrap(MemBuf)->getBufferStart();
3178 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
3179 return unwrap(MemBuf)->getBufferSize();
3182 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
3183 delete unwrap(MemBuf);
3186 /*===-- Pass Registry -----------------------------------------------------===*/
3188 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
3189 return wrap(PassRegistry::getPassRegistry());
3192 /*===-- Pass Manager ------------------------------------------------------===*/
3194 LLVMPassManagerRef LLVMCreatePassManager() {
3195 return wrap(new legacy::PassManager());
3198 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
3199 return wrap(new legacy::FunctionPassManager(unwrap(M)));
3202 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
3203 return LLVMCreateFunctionPassManagerForModule(
3204 reinterpret_cast<LLVMModuleRef>(P));
3207 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
3208 return unwrap<legacy::PassManager>(PM)->run(*unwrap(M));
3211 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
3212 return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization();
3215 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
3216 return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
3219 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
3220 return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization();
3223 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
3227 /*===-- Threading ------------------------------------------------------===*/
3229 LLVMBool LLVMStartMultithreaded() {
3230 return LLVMIsMultithreaded();
3233 void LLVMStopMultithreaded() {
3236 LLVMBool LLVMIsMultithreaded() {
3237 return llvm_is_multithreaded();