1 //===-- Core.cpp ----------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the common infrastructure (including the C bindings)
11 // for libLLVMCore.a, which implements the LLVM intermediate representation.
13 //===----------------------------------------------------------------------===//
15 #include "llvm-c/Core.h"
16 #include "llvm/ADT/StringSwitch.h"
17 #include "llvm/IR/Attributes.h"
18 #include "llvm/IR/CallSite.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/DiagnosticInfo.h"
22 #include "llvm/IR/DiagnosticPrinter.h"
23 #include "llvm/IR/GlobalAlias.h"
24 #include "llvm/IR/GlobalVariable.h"
25 #include "llvm/IR/IRBuilder.h"
26 #include "llvm/IR/InlineAsm.h"
27 #include "llvm/IR/IntrinsicInst.h"
28 #include "llvm/IR/LLVMContext.h"
29 #include "llvm/IR/LegacyPassManager.h"
30 #include "llvm/IR/Module.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/FileSystem.h"
34 #include "llvm/Support/ManagedStatic.h"
35 #include "llvm/Support/MemoryBuffer.h"
36 #include "llvm/Support/Threading.h"
37 #include "llvm/Support/raw_ostream.h"
41 #include <system_error>
45 #define DEBUG_TYPE "ir"
47 void llvm::initializeCore(PassRegistry &Registry) {
48 initializeDominatorTreeWrapperPassPass(Registry);
49 initializePrintModulePassWrapperPass(Registry);
50 initializePrintFunctionPassWrapperPass(Registry);
51 initializePrintBasicBlockPassPass(Registry);
52 initializeSafepointIRVerifierPass(Registry);
53 initializeVerifierLegacyPassPass(Registry);
56 void LLVMInitializeCore(LLVMPassRegistryRef R) {
57 initializeCore(*unwrap(R));
64 /*===-- Error handling ----------------------------------------------------===*/
66 char *LLVMCreateMessage(const char *Message) {
67 return strdup(Message);
70 void LLVMDisposeMessage(char *Message) {
75 /*===-- Operations on contexts --------------------------------------------===*/
77 static ManagedStatic<LLVMContext> GlobalContext;
79 LLVMContextRef LLVMContextCreate() {
80 return wrap(new LLVMContext());
83 LLVMContextRef LLVMGetGlobalContext() { return wrap(&*GlobalContext); }
85 void LLVMContextSetDiagnosticHandler(LLVMContextRef C,
86 LLVMDiagnosticHandler Handler,
87 void *DiagnosticContext) {
88 unwrap(C)->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) {
261 unwrap(M)->print(errs(), nullptr,
262 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
265 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
266 char **ErrorMessage) {
268 raw_fd_ostream dest(Filename, EC, sys::fs::F_Text);
270 *ErrorMessage = strdup(EC.message().c_str());
274 unwrap(M)->print(dest, nullptr);
278 if (dest.has_error()) {
279 *ErrorMessage = strdup("Error printing to file");
286 char *LLVMPrintModuleToString(LLVMModuleRef M) {
288 raw_string_ostream os(buf);
290 unwrap(M)->print(os, nullptr);
293 return strdup(buf.c_str());
296 /*--.. Operations on inline assembler ......................................--*/
297 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
298 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
302 /*--.. Operations on module contexts ......................................--*/
303 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
304 return wrap(&unwrap(M)->getContext());
308 /*===-- Operations on types -----------------------------------------------===*/
310 /*--.. Operations on all types (mostly) ....................................--*/
312 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
313 switch (unwrap(Ty)->getTypeID()) {
315 return LLVMVoidTypeKind;
317 return LLVMHalfTypeKind;
318 case Type::FloatTyID:
319 return LLVMFloatTypeKind;
320 case Type::DoubleTyID:
321 return LLVMDoubleTypeKind;
322 case Type::X86_FP80TyID:
323 return LLVMX86_FP80TypeKind;
324 case Type::FP128TyID:
325 return LLVMFP128TypeKind;
326 case Type::PPC_FP128TyID:
327 return LLVMPPC_FP128TypeKind;
328 case Type::LabelTyID:
329 return LLVMLabelTypeKind;
330 case Type::MetadataTyID:
331 return LLVMMetadataTypeKind;
332 case Type::IntegerTyID:
333 return LLVMIntegerTypeKind;
334 case Type::FunctionTyID:
335 return LLVMFunctionTypeKind;
336 case Type::StructTyID:
337 return LLVMStructTypeKind;
338 case Type::ArrayTyID:
339 return LLVMArrayTypeKind;
340 case Type::PointerTyID:
341 return LLVMPointerTypeKind;
342 case Type::VectorTyID:
343 return LLVMVectorTypeKind;
344 case Type::X86_MMXTyID:
345 return LLVMX86_MMXTypeKind;
346 case Type::TokenTyID:
347 return LLVMTokenTypeKind;
349 llvm_unreachable("Unhandled TypeID.");
352 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
354 return unwrap(Ty)->isSized();
357 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
358 return wrap(&unwrap(Ty)->getContext());
361 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
362 LLVM_DUMP_METHOD void LLVMDumpType(LLVMTypeRef Ty) {
363 return unwrap(Ty)->dump();
367 char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
369 raw_string_ostream os(buf);
372 unwrap(Ty)->print(os);
374 os << "Printing <null> Type";
378 return strdup(buf.c_str());
381 /*--.. Operations on integer types .........................................--*/
383 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
384 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
386 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
387 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
389 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
390 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
392 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
393 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
395 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
396 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
398 LLVMTypeRef LLVMInt128TypeInContext(LLVMContextRef C) {
399 return (LLVMTypeRef) Type::getInt128Ty(*unwrap(C));
401 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
402 return wrap(IntegerType::get(*unwrap(C), NumBits));
405 LLVMTypeRef LLVMInt1Type(void) {
406 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
408 LLVMTypeRef LLVMInt8Type(void) {
409 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
411 LLVMTypeRef LLVMInt16Type(void) {
412 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
414 LLVMTypeRef LLVMInt32Type(void) {
415 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
417 LLVMTypeRef LLVMInt64Type(void) {
418 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
420 LLVMTypeRef LLVMInt128Type(void) {
421 return LLVMInt128TypeInContext(LLVMGetGlobalContext());
423 LLVMTypeRef LLVMIntType(unsigned NumBits) {
424 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
427 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
428 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
431 /*--.. Operations on real types ............................................--*/
433 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
434 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
436 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
437 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
439 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
440 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
442 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
443 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
445 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
446 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
448 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
449 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
451 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
452 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
454 LLVMTypeRef LLVMTokenTypeInContext(LLVMContextRef C) {
455 return (LLVMTypeRef) Type::getTokenTy(*unwrap(C));
458 LLVMTypeRef LLVMHalfType(void) {
459 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
461 LLVMTypeRef LLVMFloatType(void) {
462 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
464 LLVMTypeRef LLVMDoubleType(void) {
465 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
467 LLVMTypeRef LLVMX86FP80Type(void) {
468 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
470 LLVMTypeRef LLVMFP128Type(void) {
471 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
473 LLVMTypeRef LLVMPPCFP128Type(void) {
474 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
476 LLVMTypeRef LLVMX86MMXType(void) {
477 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
480 /*--.. Operations on function types ........................................--*/
482 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
483 LLVMTypeRef *ParamTypes, unsigned ParamCount,
485 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
486 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
489 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
490 return unwrap<FunctionType>(FunctionTy)->isVarArg();
493 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
494 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
497 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
498 return unwrap<FunctionType>(FunctionTy)->getNumParams();
501 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
502 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
503 for (FunctionType::param_iterator I = Ty->param_begin(),
504 E = Ty->param_end(); I != E; ++I)
508 /*--.. Operations on struct types ..........................................--*/
510 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
511 unsigned ElementCount, LLVMBool Packed) {
512 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
513 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
516 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
517 unsigned ElementCount, LLVMBool Packed) {
518 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
519 ElementCount, Packed);
522 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
524 return wrap(StructType::create(*unwrap(C), Name));
527 const char *LLVMGetStructName(LLVMTypeRef Ty)
529 StructType *Type = unwrap<StructType>(Ty);
530 if (!Type->hasName())
532 return Type->getName().data();
535 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
536 unsigned ElementCount, LLVMBool Packed) {
537 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
538 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
541 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
542 return unwrap<StructType>(StructTy)->getNumElements();
545 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
546 StructType *Ty = unwrap<StructType>(StructTy);
547 for (StructType::element_iterator I = Ty->element_begin(),
548 E = Ty->element_end(); I != E; ++I)
552 LLVMTypeRef LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy, unsigned i) {
553 StructType *Ty = unwrap<StructType>(StructTy);
554 return wrap(Ty->getTypeAtIndex(i));
557 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
558 return unwrap<StructType>(StructTy)->isPacked();
561 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
562 return unwrap<StructType>(StructTy)->isOpaque();
565 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
566 return wrap(unwrap(M)->getTypeByName(Name));
569 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
571 void LLVMGetSubtypes(LLVMTypeRef Tp, LLVMTypeRef *Arr) {
573 for (auto *T : unwrap(Tp)->subtypes()) {
579 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
580 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
583 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
584 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
587 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
588 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
591 LLVMTypeRef LLVMGetElementType(LLVMTypeRef WrappedTy) {
592 auto *Ty = unwrap<Type>(WrappedTy);
593 if (auto *PTy = dyn_cast<PointerType>(Ty))
594 return wrap(PTy->getElementType());
595 return wrap(cast<SequentialType>(Ty)->getElementType());
598 unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp) {
599 return unwrap(Tp)->getNumContainedTypes();
602 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
603 return unwrap<ArrayType>(ArrayTy)->getNumElements();
606 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
607 return unwrap<PointerType>(PointerTy)->getAddressSpace();
610 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
611 return unwrap<VectorType>(VectorTy)->getNumElements();
614 /*--.. Operations on other types ...........................................--*/
616 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
617 return wrap(Type::getVoidTy(*unwrap(C)));
619 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
620 return wrap(Type::getLabelTy(*unwrap(C)));
623 LLVMTypeRef LLVMVoidType(void) {
624 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
626 LLVMTypeRef LLVMLabelType(void) {
627 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
630 /*===-- Operations on values ----------------------------------------------===*/
632 /*--.. Operations on all values ............................................--*/
634 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
635 return wrap(unwrap(Val)->getType());
638 LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) {
639 switch(unwrap(Val)->getValueID()) {
640 #define HANDLE_VALUE(Name) \
641 case Value::Name##Val: \
642 return LLVM##Name##ValueKind;
643 #include "llvm/IR/Value.def"
645 return LLVMInstructionValueKind;
649 const char *LLVMGetValueName(LLVMValueRef Val) {
650 return unwrap(Val)->getName().data();
653 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
654 unwrap(Val)->setName(Name);
657 LLVM_DUMP_METHOD void LLVMDumpValue(LLVMValueRef Val) {
658 unwrap(Val)->print(errs(), /*IsForDebug=*/true);
661 char* LLVMPrintValueToString(LLVMValueRef Val) {
663 raw_string_ostream os(buf);
666 unwrap(Val)->print(os);
668 os << "Printing <null> Value";
672 return strdup(buf.c_str());
675 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
676 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
679 int LLVMHasMetadata(LLVMValueRef Inst) {
680 return unwrap<Instruction>(Inst)->hasMetadata();
683 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
684 auto *I = unwrap<Instruction>(Inst);
685 assert(I && "Expected instruction");
686 if (auto *MD = I->getMetadata(KindID))
687 return wrap(MetadataAsValue::get(I->getContext(), MD));
691 // MetadataAsValue uses a canonical format which strips the actual MDNode for
692 // MDNode with just a single constant value, storing just a ConstantAsMetadata
693 // This undoes this canonicalization, reconstructing the MDNode.
694 static MDNode *extractMDNode(MetadataAsValue *MAV) {
695 Metadata *MD = MAV->getMetadata();
696 assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
697 "Expected a metadata node or a canonicalized constant");
699 if (MDNode *N = dyn_cast<MDNode>(MD))
702 return MDNode::get(MAV->getContext(), MD);
705 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
706 MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
708 unwrap<Instruction>(Inst)->setMetadata(KindID, N);
711 /*--.. Conversion functions ................................................--*/
713 #define LLVM_DEFINE_VALUE_CAST(name) \
714 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
715 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
718 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
720 LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) {
721 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
722 if (isa<MDNode>(MD->getMetadata()) ||
723 isa<ValueAsMetadata>(MD->getMetadata()))
728 LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) {
729 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
730 if (isa<MDString>(MD->getMetadata()))
735 /*--.. Operations on Uses ..................................................--*/
736 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
737 Value *V = unwrap(Val);
738 Value::use_iterator I = V->use_begin();
739 if (I == V->use_end())
744 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
745 Use *Next = unwrap(U)->getNext();
751 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
752 return wrap(unwrap(U)->getUser());
755 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
756 return wrap(unwrap(U)->get());
759 /*--.. Operations on Users .................................................--*/
761 static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N,
763 Metadata *Op = N->getOperand(Index);
766 if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
767 return wrap(C->getValue());
768 return wrap(MetadataAsValue::get(Context, Op));
771 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
772 Value *V = unwrap(Val);
773 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
774 if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
775 assert(Index == 0 && "Function-local metadata can only have one operand");
776 return wrap(L->getValue());
778 return getMDNodeOperandImpl(V->getContext(),
779 cast<MDNode>(MD->getMetadata()), Index);
782 return wrap(cast<User>(V)->getOperand(Index));
785 LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) {
786 Value *V = unwrap(Val);
787 return wrap(&cast<User>(V)->getOperandUse(Index));
790 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
791 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
794 int LLVMGetNumOperands(LLVMValueRef Val) {
795 Value *V = unwrap(Val);
796 if (isa<MetadataAsValue>(V))
797 return LLVMGetMDNodeNumOperands(Val);
799 return cast<User>(V)->getNumOperands();
802 /*--.. Operations on constants of any type .................................--*/
804 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
805 return wrap(Constant::getNullValue(unwrap(Ty)));
808 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
809 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
812 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
813 return wrap(UndefValue::get(unwrap(Ty)));
816 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
817 return isa<Constant>(unwrap(Ty));
820 LLVMBool LLVMIsNull(LLVMValueRef Val) {
821 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
822 return C->isNullValue();
826 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
827 return isa<UndefValue>(unwrap(Val));
830 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
831 return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
834 /*--.. Operations on metadata nodes ........................................--*/
836 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
838 LLVMContext &Context = *unwrap(C);
839 return wrap(MetadataAsValue::get(
840 Context, MDString::get(Context, StringRef(Str, SLen))));
843 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
844 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
847 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
849 LLVMContext &Context = *unwrap(C);
850 SmallVector<Metadata *, 8> MDs;
851 for (auto *OV : makeArrayRef(Vals, Count)) {
852 Value *V = unwrap(OV);
856 else if (auto *C = dyn_cast<Constant>(V))
857 MD = ConstantAsMetadata::get(C);
858 else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
859 MD = MDV->getMetadata();
860 assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
861 "outside of direct argument to call");
863 // This is function-local metadata. Pretend to make an MDNode.
865 "Expected only one operand to function-local metadata");
866 return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V)));
871 return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs)));
874 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
875 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
878 LLVMValueRef LLVMMetadataAsValue(LLVMContextRef C, LLVMMetadataRef MD) {
879 return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
882 LLVMMetadataRef LLVMValueAsMetadata(LLVMValueRef Val) {
883 auto *V = unwrap(Val);
884 if (auto *C = dyn_cast<Constant>(V))
885 return wrap(ConstantAsMetadata::get(C));
886 if (auto *MAV = dyn_cast<MetadataAsValue>(V))
887 return wrap(MAV->getMetadata());
888 return wrap(ValueAsMetadata::get(V));
891 const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
892 if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
893 if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
894 *Length = S->getString().size();
895 return S->getString().data();
901 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) {
902 auto *MD = cast<MetadataAsValue>(unwrap(V));
903 if (isa<ValueAsMetadata>(MD->getMetadata()))
905 return cast<MDNode>(MD->getMetadata())->getNumOperands();
908 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) {
909 auto *MD = cast<MetadataAsValue>(unwrap(V));
910 if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
911 *Dest = wrap(MDV->getValue());
914 const auto *N = cast<MDNode>(MD->getMetadata());
915 const unsigned numOperands = N->getNumOperands();
916 LLVMContext &Context = unwrap(V)->getContext();
917 for (unsigned i = 0; i < numOperands; i++)
918 Dest[i] = getMDNodeOperandImpl(Context, N, i);
921 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char *Name) {
922 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
923 return N->getNumOperands();
928 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char *Name,
929 LLVMValueRef *Dest) {
930 NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
933 LLVMContext &Context = unwrap(M)->getContext();
934 for (unsigned i=0;i<N->getNumOperands();i++)
935 Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
938 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char *Name,
940 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
945 N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
948 /*--.. Operations on scalar constants ......................................--*/
950 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
951 LLVMBool SignExtend) {
952 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
955 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
957 const uint64_t Words[]) {
958 IntegerType *Ty = unwrap<IntegerType>(IntTy);
959 return wrap(ConstantInt::get(Ty->getContext(),
960 APInt(Ty->getBitWidth(),
961 makeArrayRef(Words, NumWords))));
964 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
966 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
970 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
971 unsigned SLen, uint8_t Radix) {
972 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
976 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
977 return wrap(ConstantFP::get(unwrap(RealTy), N));
980 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
981 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
984 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
986 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
989 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
990 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
993 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
994 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
997 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
998 ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
999 Type *Ty = cFP->getType();
1001 if (Ty->isFloatTy()) {
1003 return cFP->getValueAPF().convertToFloat();
1006 if (Ty->isDoubleTy()) {
1008 return cFP->getValueAPF().convertToDouble();
1012 APFloat APF = cFP->getValueAPF();
1013 APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo);
1014 *LosesInfo = APFLosesInfo;
1015 return APF.convertToDouble();
1018 /*--.. Operations on composite constants ...................................--*/
1020 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
1022 LLVMBool DontNullTerminate) {
1023 /* Inverted the sense of AddNull because ', 0)' is a
1024 better mnemonic for null termination than ', 1)'. */
1025 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
1026 DontNullTerminate == 0));
1029 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
1030 LLVMBool DontNullTerminate) {
1031 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
1035 LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef C, unsigned idx) {
1036 return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
1039 LLVMBool LLVMIsConstantString(LLVMValueRef C) {
1040 return unwrap<ConstantDataSequential>(C)->isString();
1043 const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1044 StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1045 *Length = Str.size();
1049 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
1050 LLVMValueRef *ConstantVals, unsigned Length) {
1051 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1052 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1055 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
1056 LLVMValueRef *ConstantVals,
1057 unsigned Count, LLVMBool Packed) {
1058 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1059 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
1063 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
1065 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1069 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
1070 LLVMValueRef *ConstantVals,
1072 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1073 StructType *Ty = cast<StructType>(unwrap(StructTy));
1075 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
1078 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
1079 return wrap(ConstantVector::get(makeArrayRef(
1080 unwrap<Constant>(ScalarConstantVals, Size), Size)));
1083 /*-- Opcode mapping */
1085 static LLVMOpcode map_to_llvmopcode(int opcode)
1088 default: llvm_unreachable("Unhandled Opcode.");
1089 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
1090 #include "llvm/IR/Instruction.def"
1095 static int map_from_llvmopcode(LLVMOpcode code)
1098 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
1099 #include "llvm/IR/Instruction.def"
1102 llvm_unreachable("Unhandled Opcode.");
1105 /*--.. Constant expressions ................................................--*/
1107 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
1108 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
1111 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
1112 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
1115 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
1116 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
1119 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
1120 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1123 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
1124 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
1127 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
1128 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
1132 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
1133 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
1136 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
1137 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
1140 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1141 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
1142 unwrap<Constant>(RHSConstant)));
1145 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
1146 LLVMValueRef RHSConstant) {
1147 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
1148 unwrap<Constant>(RHSConstant)));
1151 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
1152 LLVMValueRef RHSConstant) {
1153 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
1154 unwrap<Constant>(RHSConstant)));
1157 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1158 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
1159 unwrap<Constant>(RHSConstant)));
1162 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1163 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
1164 unwrap<Constant>(RHSConstant)));
1167 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
1168 LLVMValueRef RHSConstant) {
1169 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
1170 unwrap<Constant>(RHSConstant)));
1173 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
1174 LLVMValueRef RHSConstant) {
1175 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
1176 unwrap<Constant>(RHSConstant)));
1179 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1180 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
1181 unwrap<Constant>(RHSConstant)));
1184 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1185 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
1186 unwrap<Constant>(RHSConstant)));
1189 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
1190 LLVMValueRef RHSConstant) {
1191 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
1192 unwrap<Constant>(RHSConstant)));
1195 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
1196 LLVMValueRef RHSConstant) {
1197 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
1198 unwrap<Constant>(RHSConstant)));
1201 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1202 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
1203 unwrap<Constant>(RHSConstant)));
1206 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1207 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
1208 unwrap<Constant>(RHSConstant)));
1211 LLVMValueRef LLVMConstExactUDiv(LLVMValueRef LHSConstant,
1212 LLVMValueRef RHSConstant) {
1213 return wrap(ConstantExpr::getExactUDiv(unwrap<Constant>(LHSConstant),
1214 unwrap<Constant>(RHSConstant)));
1217 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1218 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
1219 unwrap<Constant>(RHSConstant)));
1222 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
1223 LLVMValueRef RHSConstant) {
1224 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
1225 unwrap<Constant>(RHSConstant)));
1228 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1229 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
1230 unwrap<Constant>(RHSConstant)));
1233 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1234 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
1235 unwrap<Constant>(RHSConstant)));
1238 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1239 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
1240 unwrap<Constant>(RHSConstant)));
1243 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1244 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
1245 unwrap<Constant>(RHSConstant)));
1248 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1249 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
1250 unwrap<Constant>(RHSConstant)));
1253 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1254 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
1255 unwrap<Constant>(RHSConstant)));
1258 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1259 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
1260 unwrap<Constant>(RHSConstant)));
1263 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
1264 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1265 return wrap(ConstantExpr::getICmp(Predicate,
1266 unwrap<Constant>(LHSConstant),
1267 unwrap<Constant>(RHSConstant)));
1270 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
1271 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1272 return wrap(ConstantExpr::getFCmp(Predicate,
1273 unwrap<Constant>(LHSConstant),
1274 unwrap<Constant>(RHSConstant)));
1277 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1278 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1279 unwrap<Constant>(RHSConstant)));
1282 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1283 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
1284 unwrap<Constant>(RHSConstant)));
1287 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1288 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1289 unwrap<Constant>(RHSConstant)));
1292 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
1293 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1294 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1296 return wrap(ConstantExpr::getGetElementPtr(
1297 nullptr, unwrap<Constant>(ConstantVal), IdxList));
1300 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
1301 LLVMValueRef *ConstantIndices,
1302 unsigned NumIndices) {
1303 Constant* Val = unwrap<Constant>(ConstantVal);
1304 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1306 return wrap(ConstantExpr::getInBoundsGetElementPtr(nullptr, Val, IdxList));
1309 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1310 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1314 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1315 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1319 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1320 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1324 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1325 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1329 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1330 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1334 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1335 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1339 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1340 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1344 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1345 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1349 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1350 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1354 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1355 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1359 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1360 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1364 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1365 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1369 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1370 LLVMTypeRef ToType) {
1371 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1375 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1376 LLVMTypeRef ToType) {
1377 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1381 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1382 LLVMTypeRef ToType) {
1383 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1387 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1388 LLVMTypeRef ToType) {
1389 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1393 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1394 LLVMTypeRef ToType) {
1395 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1399 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1400 LLVMBool isSigned) {
1401 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1402 unwrap(ToType), isSigned));
1405 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1406 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1410 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1411 LLVMValueRef ConstantIfTrue,
1412 LLVMValueRef ConstantIfFalse) {
1413 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1414 unwrap<Constant>(ConstantIfTrue),
1415 unwrap<Constant>(ConstantIfFalse)));
1418 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1419 LLVMValueRef IndexConstant) {
1420 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1421 unwrap<Constant>(IndexConstant)));
1424 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1425 LLVMValueRef ElementValueConstant,
1426 LLVMValueRef IndexConstant) {
1427 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1428 unwrap<Constant>(ElementValueConstant),
1429 unwrap<Constant>(IndexConstant)));
1432 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1433 LLVMValueRef VectorBConstant,
1434 LLVMValueRef MaskConstant) {
1435 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1436 unwrap<Constant>(VectorBConstant),
1437 unwrap<Constant>(MaskConstant)));
1440 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1442 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1443 makeArrayRef(IdxList, NumIdx)));
1446 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1447 LLVMValueRef ElementValueConstant,
1448 unsigned *IdxList, unsigned NumIdx) {
1449 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1450 unwrap<Constant>(ElementValueConstant),
1451 makeArrayRef(IdxList, NumIdx)));
1454 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1455 const char *Constraints,
1456 LLVMBool HasSideEffects,
1457 LLVMBool IsAlignStack) {
1458 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1459 Constraints, HasSideEffects, IsAlignStack));
1462 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1463 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1466 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1468 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1469 return wrap(unwrap<GlobalValue>(Global)->getParent());
1472 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1473 return unwrap<GlobalValue>(Global)->isDeclaration();
1476 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1477 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1478 case GlobalValue::ExternalLinkage:
1479 return LLVMExternalLinkage;
1480 case GlobalValue::AvailableExternallyLinkage:
1481 return LLVMAvailableExternallyLinkage;
1482 case GlobalValue::LinkOnceAnyLinkage:
1483 return LLVMLinkOnceAnyLinkage;
1484 case GlobalValue::LinkOnceODRLinkage:
1485 return LLVMLinkOnceODRLinkage;
1486 case GlobalValue::WeakAnyLinkage:
1487 return LLVMWeakAnyLinkage;
1488 case GlobalValue::WeakODRLinkage:
1489 return LLVMWeakODRLinkage;
1490 case GlobalValue::AppendingLinkage:
1491 return LLVMAppendingLinkage;
1492 case GlobalValue::InternalLinkage:
1493 return LLVMInternalLinkage;
1494 case GlobalValue::PrivateLinkage:
1495 return LLVMPrivateLinkage;
1496 case GlobalValue::ExternalWeakLinkage:
1497 return LLVMExternalWeakLinkage;
1498 case GlobalValue::CommonLinkage:
1499 return LLVMCommonLinkage;
1502 llvm_unreachable("Invalid GlobalValue linkage!");
1505 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1506 GlobalValue *GV = unwrap<GlobalValue>(Global);
1509 case LLVMExternalLinkage:
1510 GV->setLinkage(GlobalValue::ExternalLinkage);
1512 case LLVMAvailableExternallyLinkage:
1513 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1515 case LLVMLinkOnceAnyLinkage:
1516 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1518 case LLVMLinkOnceODRLinkage:
1519 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1521 case LLVMLinkOnceODRAutoHideLinkage:
1522 DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1523 "longer supported.");
1525 case LLVMWeakAnyLinkage:
1526 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1528 case LLVMWeakODRLinkage:
1529 GV->setLinkage(GlobalValue::WeakODRLinkage);
1531 case LLVMAppendingLinkage:
1532 GV->setLinkage(GlobalValue::AppendingLinkage);
1534 case LLVMInternalLinkage:
1535 GV->setLinkage(GlobalValue::InternalLinkage);
1537 case LLVMPrivateLinkage:
1538 GV->setLinkage(GlobalValue::PrivateLinkage);
1540 case LLVMLinkerPrivateLinkage:
1541 GV->setLinkage(GlobalValue::PrivateLinkage);
1543 case LLVMLinkerPrivateWeakLinkage:
1544 GV->setLinkage(GlobalValue::PrivateLinkage);
1546 case LLVMDLLImportLinkage:
1548 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1550 case LLVMDLLExportLinkage:
1552 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1554 case LLVMExternalWeakLinkage:
1555 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1557 case LLVMGhostLinkage:
1559 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1561 case LLVMCommonLinkage:
1562 GV->setLinkage(GlobalValue::CommonLinkage);
1567 const char *LLVMGetSection(LLVMValueRef Global) {
1568 // Using .data() is safe because of how GlobalObject::setSection is
1570 return unwrap<GlobalValue>(Global)->getSection().data();
1573 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1574 unwrap<GlobalObject>(Global)->setSection(Section);
1577 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1578 return static_cast<LLVMVisibility>(
1579 unwrap<GlobalValue>(Global)->getVisibility());
1582 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1583 unwrap<GlobalValue>(Global)
1584 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1587 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
1588 return static_cast<LLVMDLLStorageClass>(
1589 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1592 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
1593 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1594 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1597 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
1598 return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
1601 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
1602 unwrap<GlobalValue>(Global)->setUnnamedAddr(
1603 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
1604 : GlobalValue::UnnamedAddr::None);
1607 /*--.. Operations on global variables, load and store instructions .........--*/
1609 unsigned LLVMGetAlignment(LLVMValueRef V) {
1610 Value *P = unwrap<Value>(V);
1611 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1612 return GV->getAlignment();
1613 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1614 return AI->getAlignment();
1615 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1616 return LI->getAlignment();
1617 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1618 return SI->getAlignment();
1621 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1624 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1625 Value *P = unwrap<Value>(V);
1626 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
1627 GV->setAlignment(Bytes);
1628 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1629 AI->setAlignment(Bytes);
1630 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1631 LI->setAlignment(Bytes);
1632 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1633 SI->setAlignment(Bytes);
1636 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1639 /*--.. Operations on global variables ......................................--*/
1641 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1642 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1643 GlobalValue::ExternalLinkage, nullptr, Name));
1646 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1648 unsigned AddressSpace) {
1649 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1650 GlobalValue::ExternalLinkage, nullptr, Name,
1651 nullptr, GlobalVariable::NotThreadLocal,
1655 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1656 return wrap(unwrap(M)->getNamedGlobal(Name));
1659 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1660 Module *Mod = unwrap(M);
1661 Module::global_iterator I = Mod->global_begin();
1662 if (I == Mod->global_end())
1667 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1668 Module *Mod = unwrap(M);
1669 Module::global_iterator I = Mod->global_end();
1670 if (I == Mod->global_begin())
1675 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1676 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1677 Module::global_iterator I(GV);
1678 if (++I == GV->getParent()->global_end())
1683 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1684 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1685 Module::global_iterator I(GV);
1686 if (I == GV->getParent()->global_begin())
1691 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1692 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1695 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1696 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1697 if ( !GV->hasInitializer() )
1699 return wrap(GV->getInitializer());
1702 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1703 unwrap<GlobalVariable>(GlobalVar)
1704 ->setInitializer(unwrap<Constant>(ConstantVal));
1707 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1708 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1711 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1712 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1715 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1716 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1719 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1720 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1723 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1724 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1725 case GlobalVariable::NotThreadLocal:
1726 return LLVMNotThreadLocal;
1727 case GlobalVariable::GeneralDynamicTLSModel:
1728 return LLVMGeneralDynamicTLSModel;
1729 case GlobalVariable::LocalDynamicTLSModel:
1730 return LLVMLocalDynamicTLSModel;
1731 case GlobalVariable::InitialExecTLSModel:
1732 return LLVMInitialExecTLSModel;
1733 case GlobalVariable::LocalExecTLSModel:
1734 return LLVMLocalExecTLSModel;
1737 llvm_unreachable("Invalid GlobalVariable thread local mode");
1740 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1741 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1744 case LLVMNotThreadLocal:
1745 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1747 case LLVMGeneralDynamicTLSModel:
1748 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1750 case LLVMLocalDynamicTLSModel:
1751 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1753 case LLVMInitialExecTLSModel:
1754 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1756 case LLVMLocalExecTLSModel:
1757 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1762 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1763 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1766 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1767 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1770 /*--.. Operations on aliases ......................................--*/
1772 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1774 auto *PTy = cast<PointerType>(unwrap(Ty));
1775 return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
1776 GlobalValue::ExternalLinkage, Name,
1777 unwrap<Constant>(Aliasee), unwrap(M)));
1780 /*--.. Operations on functions .............................................--*/
1782 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1783 LLVMTypeRef FunctionTy) {
1784 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1785 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1788 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1789 return wrap(unwrap(M)->getFunction(Name));
1792 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1793 Module *Mod = unwrap(M);
1794 Module::iterator I = Mod->begin();
1795 if (I == Mod->end())
1800 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1801 Module *Mod = unwrap(M);
1802 Module::iterator I = Mod->end();
1803 if (I == Mod->begin())
1808 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1809 Function *Func = unwrap<Function>(Fn);
1810 Module::iterator I(Func);
1811 if (++I == Func->getParent()->end())
1816 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1817 Function *Func = unwrap<Function>(Fn);
1818 Module::iterator I(Func);
1819 if (I == Func->getParent()->begin())
1824 void LLVMDeleteFunction(LLVMValueRef Fn) {
1825 unwrap<Function>(Fn)->eraseFromParent();
1828 LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) {
1829 return unwrap<Function>(Fn)->hasPersonalityFn();
1832 LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) {
1833 return wrap(unwrap<Function>(Fn)->getPersonalityFn());
1836 void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) {
1837 unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
1840 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1841 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1842 return F->getIntrinsicID();
1846 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1847 return unwrap<Function>(Fn)->getCallingConv();
1850 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1851 return unwrap<Function>(Fn)->setCallingConv(
1852 static_cast<CallingConv::ID>(CC));
1855 const char *LLVMGetGC(LLVMValueRef Fn) {
1856 Function *F = unwrap<Function>(Fn);
1857 return F->hasGC()? F->getGC().c_str() : nullptr;
1860 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1861 Function *F = unwrap<Function>(Fn);
1868 void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1869 LLVMAttributeRef A) {
1870 unwrap<Function>(F)->addAttribute(Idx, unwrap(A));
1873 unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) {
1874 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
1875 return AS.getNumAttributes();
1878 void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1879 LLVMAttributeRef *Attrs) {
1880 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
1885 LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F,
1886 LLVMAttributeIndex Idx,
1888 return wrap(unwrap<Function>(F)->getAttribute(Idx,
1889 (Attribute::AttrKind)KindID));
1892 LLVMAttributeRef LLVMGetStringAttributeAtIndex(LLVMValueRef F,
1893 LLVMAttributeIndex Idx,
1894 const char *K, unsigned KLen) {
1895 return wrap(unwrap<Function>(F)->getAttribute(Idx, StringRef(K, KLen)));
1898 void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1900 unwrap<Function>(F)->removeAttribute(Idx, (Attribute::AttrKind)KindID);
1903 void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1904 const char *K, unsigned KLen) {
1905 unwrap<Function>(F)->removeAttribute(Idx, StringRef(K, KLen));
1908 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1910 Function *Func = unwrap<Function>(Fn);
1911 Attribute Attr = Attribute::get(Func->getContext(), A, V);
1912 Func->addAttribute(AttributeList::FunctionIndex, Attr);
1915 /*--.. Operations on parameters ............................................--*/
1917 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1918 // This function is strictly redundant to
1919 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1920 return unwrap<Function>(FnRef)->arg_size();
1923 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1924 Function *Fn = unwrap<Function>(FnRef);
1925 for (Function::arg_iterator I = Fn->arg_begin(),
1926 E = Fn->arg_end(); I != E; I++)
1927 *ParamRefs++ = wrap(&*I);
1930 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1931 Function *Fn = unwrap<Function>(FnRef);
1932 return wrap(&Fn->arg_begin()[index]);
1935 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1936 return wrap(unwrap<Argument>(V)->getParent());
1939 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1940 Function *Func = unwrap<Function>(Fn);
1941 Function::arg_iterator I = Func->arg_begin();
1942 if (I == Func->arg_end())
1947 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1948 Function *Func = unwrap<Function>(Fn);
1949 Function::arg_iterator I = Func->arg_end();
1950 if (I == Func->arg_begin())
1955 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1956 Argument *A = unwrap<Argument>(Arg);
1957 Function *Fn = A->getParent();
1958 if (A->getArgNo() + 1 >= Fn->arg_size())
1960 return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
1963 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1964 Argument *A = unwrap<Argument>(Arg);
1965 if (A->getArgNo() == 0)
1967 return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
1970 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1971 Argument *A = unwrap<Argument>(Arg);
1972 A->addAttr(Attribute::getWithAlignment(A->getContext(), align));
1975 /*--.. Operations on basic blocks ..........................................--*/
1977 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1978 return wrap(static_cast<Value*>(unwrap(BB)));
1981 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1982 return isa<BasicBlock>(unwrap(Val));
1985 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1986 return wrap(unwrap<BasicBlock>(Val));
1989 const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) {
1990 return unwrap(BB)->getName().data();
1993 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1994 return wrap(unwrap(BB)->getParent());
1997 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1998 return wrap(unwrap(BB)->getTerminator());
2001 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
2002 return unwrap<Function>(FnRef)->size();
2005 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
2006 Function *Fn = unwrap<Function>(FnRef);
2007 for (BasicBlock &BB : *Fn)
2008 *BasicBlocksRefs++ = wrap(&BB);
2011 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
2012 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2015 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
2016 Function *Func = unwrap<Function>(Fn);
2017 Function::iterator I = Func->begin();
2018 if (I == Func->end())
2023 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
2024 Function *Func = unwrap<Function>(Fn);
2025 Function::iterator I = Func->end();
2026 if (I == Func->begin())
2031 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
2032 BasicBlock *Block = unwrap(BB);
2033 Function::iterator I(Block);
2034 if (++I == Block->getParent()->end())
2039 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
2040 BasicBlock *Block = unwrap(BB);
2041 Function::iterator I(Block);
2042 if (I == Block->getParent()->begin())
2047 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
2050 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
2053 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
2054 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
2057 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
2058 LLVMBasicBlockRef BBRef,
2060 BasicBlock *BB = unwrap(BBRef);
2061 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2064 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
2066 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
2069 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
2070 unwrap(BBRef)->eraseFromParent();
2073 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
2074 unwrap(BBRef)->removeFromParent();
2077 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2078 unwrap(BB)->moveBefore(unwrap(MovePos));
2081 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2082 unwrap(BB)->moveAfter(unwrap(MovePos));
2085 /*--.. Operations on instructions ..........................................--*/
2087 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
2088 return wrap(unwrap<Instruction>(Inst)->getParent());
2091 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
2092 BasicBlock *Block = unwrap(BB);
2093 BasicBlock::iterator I = Block->begin();
2094 if (I == Block->end())
2099 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
2100 BasicBlock *Block = unwrap(BB);
2101 BasicBlock::iterator I = Block->end();
2102 if (I == Block->begin())
2107 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
2108 Instruction *Instr = unwrap<Instruction>(Inst);
2109 BasicBlock::iterator I(Instr);
2110 if (++I == Instr->getParent()->end())
2115 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
2116 Instruction *Instr = unwrap<Instruction>(Inst);
2117 BasicBlock::iterator I(Instr);
2118 if (I == Instr->getParent()->begin())
2123 void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) {
2124 unwrap<Instruction>(Inst)->removeFromParent();
2127 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
2128 unwrap<Instruction>(Inst)->eraseFromParent();
2131 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
2132 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2133 return (LLVMIntPredicate)I->getPredicate();
2134 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2135 if (CE->getOpcode() == Instruction::ICmp)
2136 return (LLVMIntPredicate)CE->getPredicate();
2137 return (LLVMIntPredicate)0;
2140 LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
2141 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2142 return (LLVMRealPredicate)I->getPredicate();
2143 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2144 if (CE->getOpcode() == Instruction::FCmp)
2145 return (LLVMRealPredicate)CE->getPredicate();
2146 return (LLVMRealPredicate)0;
2149 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
2150 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2151 return map_to_llvmopcode(C->getOpcode());
2152 return (LLVMOpcode)0;
2155 LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {
2156 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2157 return wrap(C->clone());
2161 /*--.. Call and invoke instructions ........................................--*/
2163 unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) {
2164 return CallSite(unwrap<Instruction>(Instr)).getNumArgOperands();
2167 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
2168 return CallSite(unwrap<Instruction>(Instr)).getCallingConv();
2171 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
2172 return CallSite(unwrap<Instruction>(Instr))
2173 .setCallingConv(static_cast<CallingConv::ID>(CC));
2176 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
2178 CallSite Call = CallSite(unwrap<Instruction>(Instr));
2179 Attribute AlignAttr = Attribute::getWithAlignment(Call->getContext(), align);
2180 Call.addAttribute(index, AlignAttr);
2183 void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2184 LLVMAttributeRef A) {
2185 CallSite(unwrap<Instruction>(C)).addAttribute(Idx, unwrap(A));
2188 unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C,
2189 LLVMAttributeIndex Idx) {
2190 auto CS = CallSite(unwrap<Instruction>(C));
2191 auto AS = CS.getAttributes().getAttributes(Idx);
2192 return AS.getNumAttributes();
2195 void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx,
2196 LLVMAttributeRef *Attrs) {
2197 auto CS = CallSite(unwrap<Instruction>(C));
2198 auto AS = CS.getAttributes().getAttributes(Idx);
2203 LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C,
2204 LLVMAttributeIndex Idx,
2206 return wrap(CallSite(unwrap<Instruction>(C))
2207 .getAttribute(Idx, (Attribute::AttrKind)KindID));
2210 LLVMAttributeRef LLVMGetCallSiteStringAttribute(LLVMValueRef C,
2211 LLVMAttributeIndex Idx,
2212 const char *K, unsigned KLen) {
2213 return wrap(CallSite(unwrap<Instruction>(C))
2214 .getAttribute(Idx, StringRef(K, KLen)));
2217 void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2219 CallSite(unwrap<Instruction>(C))
2220 .removeAttribute(Idx, (Attribute::AttrKind)KindID);
2223 void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2224 const char *K, unsigned KLen) {
2225 CallSite(unwrap<Instruction>(C)).removeAttribute(Idx, StringRef(K, KLen));
2228 LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) {
2229 return wrap(CallSite(unwrap<Instruction>(Instr)).getCalledValue());
2232 /*--.. Operations on call instructions (only) ..............................--*/
2234 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
2235 return unwrap<CallInst>(Call)->isTailCall();
2238 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
2239 unwrap<CallInst>(Call)->setTailCall(isTailCall);
2242 /*--.. Operations on invoke instructions (only) ............................--*/
2244 LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) {
2245 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2248 LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) {
2249 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2252 void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2253 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2256 void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2257 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2260 /*--.. Operations on terminators ...........................................--*/
2262 unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {
2263 return unwrap<TerminatorInst>(Term)->getNumSuccessors();
2266 LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) {
2267 return wrap(unwrap<TerminatorInst>(Term)->getSuccessor(i));
2270 void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
2271 return unwrap<TerminatorInst>(Term)->setSuccessor(i,unwrap(block));
2274 /*--.. Operations on branch instructions (only) ............................--*/
2276 LLVMBool LLVMIsConditional(LLVMValueRef Branch) {
2277 return unwrap<BranchInst>(Branch)->isConditional();
2280 LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) {
2281 return wrap(unwrap<BranchInst>(Branch)->getCondition());
2284 void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) {
2285 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2288 /*--.. Operations on switch instructions (only) ............................--*/
2290 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
2291 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2294 /*--.. Operations on alloca instructions (only) ............................--*/
2296 LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) {
2297 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
2300 /*--.. Operations on gep instructions (only) ...............................--*/
2302 LLVMBool LLVMIsInBounds(LLVMValueRef GEP) {
2303 return unwrap<GetElementPtrInst>(GEP)->isInBounds();
2306 void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) {
2307 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
2310 /*--.. Operations on phi nodes .............................................--*/
2312 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
2313 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
2314 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
2315 for (unsigned I = 0; I != Count; ++I)
2316 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
2319 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
2320 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
2323 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
2324 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
2327 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
2328 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
2331 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/
2333 unsigned LLVMGetNumIndices(LLVMValueRef Inst) {
2334 auto *I = unwrap(Inst);
2335 if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
2336 return GEP->getNumIndices();
2337 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2338 return EV->getNumIndices();
2339 if (auto *IV = dyn_cast<InsertValueInst>(I))
2340 return IV->getNumIndices();
2342 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
2345 const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
2346 auto *I = unwrap(Inst);
2347 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2348 return EV->getIndices().data();
2349 if (auto *IV = dyn_cast<InsertValueInst>(I))
2350 return IV->getIndices().data();
2352 "LLVMGetIndices applies only to extractvalue and insertvalue!");
2356 /*===-- Instruction builders ----------------------------------------------===*/
2358 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
2359 return wrap(new IRBuilder<>(*unwrap(C)));
2362 LLVMBuilderRef LLVMCreateBuilder(void) {
2363 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
2366 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
2367 LLVMValueRef Instr) {
2368 BasicBlock *BB = unwrap(Block);
2369 auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
2370 unwrap(Builder)->SetInsertPoint(BB, I);
2373 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2374 Instruction *I = unwrap<Instruction>(Instr);
2375 unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
2378 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
2379 BasicBlock *BB = unwrap(Block);
2380 unwrap(Builder)->SetInsertPoint(BB);
2383 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
2384 return wrap(unwrap(Builder)->GetInsertBlock());
2387 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
2388 unwrap(Builder)->ClearInsertionPoint();
2391 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2392 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
2395 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
2397 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
2400 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
2401 delete unwrap(Builder);
2404 /*--.. Metadata builders ...................................................--*/
2406 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
2408 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
2409 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
2412 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
2413 LLVMContext &Context = unwrap(Builder)->getContext();
2414 return wrap(MetadataAsValue::get(
2415 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
2418 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
2419 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
2423 /*--.. Instruction builders ................................................--*/
2425 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
2426 return wrap(unwrap(B)->CreateRetVoid());
2429 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
2430 return wrap(unwrap(B)->CreateRet(unwrap(V)));
2433 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
2435 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
2438 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
2439 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
2442 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
2443 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
2444 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
2447 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
2448 LLVMBasicBlockRef Else, unsigned NumCases) {
2449 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
2452 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
2453 unsigned NumDests) {
2454 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
2457 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
2458 LLVMValueRef *Args, unsigned NumArgs,
2459 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
2461 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
2462 makeArrayRef(unwrap(Args), NumArgs),
2466 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
2467 LLVMValueRef PersFn, unsigned NumClauses,
2469 // The personality used to live on the landingpad instruction, but now it
2470 // lives on the parent function. For compatibility, take the provided
2471 // personality and put it on the parent function.
2473 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
2474 cast<Function>(unwrap(PersFn)));
2475 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
2478 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
2479 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
2482 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
2483 return wrap(unwrap(B)->CreateUnreachable());
2486 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
2487 LLVMBasicBlockRef Dest) {
2488 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
2491 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
2492 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
2495 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
2496 return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
2499 LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
2500 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
2503 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
2504 unwrap<LandingPadInst>(LandingPad)->
2505 addClause(cast<Constant>(unwrap(ClauseVal)));
2508 LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) {
2509 return unwrap<LandingPadInst>(LandingPad)->isCleanup();
2512 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
2513 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2516 /*--.. Arithmetic ..........................................................--*/
2518 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2520 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2523 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2525 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2528 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2530 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2533 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2535 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2538 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2540 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2543 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2545 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2548 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2550 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2553 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2555 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2558 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2560 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2563 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2565 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2568 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2570 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2573 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2575 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2578 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2580 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2583 LLVMValueRef LLVMBuildExactUDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2584 LLVMValueRef RHS, const char *Name) {
2585 return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
2588 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2590 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2593 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2594 LLVMValueRef RHS, const char *Name) {
2595 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2598 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2600 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2603 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2605 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2608 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2610 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2613 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2615 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2618 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2620 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2623 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2625 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2628 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2630 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2633 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2635 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2638 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2640 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2643 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2645 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2648 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2649 LLVMValueRef LHS, LLVMValueRef RHS,
2651 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2652 unwrap(RHS), Name));
2655 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2656 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2659 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2661 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2664 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2666 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2669 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2670 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2673 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2674 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2677 /*--.. Memory ..............................................................--*/
2679 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2681 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2682 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2683 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2684 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2685 ITy, unwrap(Ty), AllocSize,
2686 nullptr, nullptr, "");
2687 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2690 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2691 LLVMValueRef Val, const char *Name) {
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 unwrap(Val), nullptr, "");
2698 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2701 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2703 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
2706 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2707 LLVMValueRef Val, const char *Name) {
2708 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2711 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2712 return wrap(unwrap(B)->Insert(
2713 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2716 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2718 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2721 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2722 LLVMValueRef PointerVal) {
2723 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2726 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
2728 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic;
2729 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered;
2730 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic;
2731 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire;
2732 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release;
2733 case LLVMAtomicOrderingAcquireRelease:
2734 return AtomicOrdering::AcquireRelease;
2735 case LLVMAtomicOrderingSequentiallyConsistent:
2736 return AtomicOrdering::SequentiallyConsistent;
2739 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
2742 static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) {
2744 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic;
2745 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered;
2746 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic;
2747 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire;
2748 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease;
2749 case AtomicOrdering::AcquireRelease:
2750 return LLVMAtomicOrderingAcquireRelease;
2751 case AtomicOrdering::SequentiallyConsistent:
2752 return LLVMAtomicOrderingSequentiallyConsistent;
2755 llvm_unreachable("Invalid AtomicOrdering value!");
2758 // TODO: Should this and other atomic instructions support building with
2760 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
2761 LLVMBool isSingleThread, const char *Name) {
2763 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
2764 isSingleThread ? SyncScope::SingleThread
2765 : SyncScope::System,
2769 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2770 LLVMValueRef *Indices, unsigned NumIndices,
2772 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2773 return wrap(unwrap(B)->CreateGEP(nullptr, unwrap(Pointer), IdxList, Name));
2776 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2777 LLVMValueRef *Indices, unsigned NumIndices,
2779 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2781 unwrap(B)->CreateInBoundsGEP(nullptr, unwrap(Pointer), IdxList, Name));
2784 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2785 unsigned Idx, const char *Name) {
2786 return wrap(unwrap(B)->CreateStructGEP(nullptr, unwrap(Pointer), Idx, Name));
2789 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2791 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2794 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2796 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2799 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2800 Value *P = unwrap<Value>(MemAccessInst);
2801 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2802 return LI->isVolatile();
2803 return cast<StoreInst>(P)->isVolatile();
2806 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2807 Value *P = unwrap<Value>(MemAccessInst);
2808 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2809 return LI->setVolatile(isVolatile);
2810 return cast<StoreInst>(P)->setVolatile(isVolatile);
2813 LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) {
2814 Value *P = unwrap<Value>(MemAccessInst);
2816 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2817 O = LI->getOrdering();
2819 O = cast<StoreInst>(P)->getOrdering();
2820 return mapToLLVMOrdering(O);
2823 void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
2824 Value *P = unwrap<Value>(MemAccessInst);
2825 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
2827 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2828 return LI->setOrdering(O);
2829 return cast<StoreInst>(P)->setOrdering(O);
2832 /*--.. Casts ...............................................................--*/
2834 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2835 LLVMTypeRef DestTy, const char *Name) {
2836 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2839 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2840 LLVMTypeRef DestTy, const char *Name) {
2841 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2844 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2845 LLVMTypeRef DestTy, const char *Name) {
2846 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2849 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2850 LLVMTypeRef DestTy, const char *Name) {
2851 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2854 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2855 LLVMTypeRef DestTy, const char *Name) {
2856 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2859 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2860 LLVMTypeRef DestTy, const char *Name) {
2861 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2864 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2865 LLVMTypeRef DestTy, const char *Name) {
2866 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2869 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2870 LLVMTypeRef DestTy, const char *Name) {
2871 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2874 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2875 LLVMTypeRef DestTy, const char *Name) {
2876 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2879 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2880 LLVMTypeRef DestTy, const char *Name) {
2881 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2884 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2885 LLVMTypeRef DestTy, const char *Name) {
2886 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2889 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2890 LLVMTypeRef DestTy, const char *Name) {
2891 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2894 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
2895 LLVMTypeRef DestTy, const char *Name) {
2896 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
2899 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2900 LLVMTypeRef DestTy, const char *Name) {
2901 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2905 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2906 LLVMTypeRef DestTy, const char *Name) {
2907 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2911 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2912 LLVMTypeRef DestTy, const char *Name) {
2913 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2917 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2918 LLVMTypeRef DestTy, const char *Name) {
2919 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2920 unwrap(DestTy), Name));
2923 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2924 LLVMTypeRef DestTy, const char *Name) {
2925 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2928 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2929 LLVMTypeRef DestTy, const char *Name) {
2930 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2931 /*isSigned*/true, Name));
2934 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2935 LLVMTypeRef DestTy, const char *Name) {
2936 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2939 /*--.. Comparisons .........................................................--*/
2941 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2942 LLVMValueRef LHS, LLVMValueRef RHS,
2944 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2945 unwrap(LHS), unwrap(RHS), Name));
2948 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2949 LLVMValueRef LHS, LLVMValueRef RHS,
2951 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2952 unwrap(LHS), unwrap(RHS), Name));
2955 /*--.. Miscellaneous instructions ..........................................--*/
2957 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2958 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2961 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2962 LLVMValueRef *Args, unsigned NumArgs,
2964 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2965 makeArrayRef(unwrap(Args), NumArgs),
2969 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2970 LLVMValueRef Then, LLVMValueRef Else,
2972 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2976 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2977 LLVMTypeRef Ty, const char *Name) {
2978 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2981 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2982 LLVMValueRef Index, const char *Name) {
2983 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2987 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2988 LLVMValueRef EltVal, LLVMValueRef Index,
2990 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2991 unwrap(Index), Name));
2994 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2995 LLVMValueRef V2, LLVMValueRef Mask,
2997 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2998 unwrap(Mask), Name));
3001 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
3002 unsigned Index, const char *Name) {
3003 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
3006 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
3007 LLVMValueRef EltVal, unsigned Index,
3009 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
3013 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
3015 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
3018 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
3020 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
3023 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
3024 LLVMValueRef RHS, const char *Name) {
3025 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
3028 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
3029 LLVMValueRef PTR, LLVMValueRef Val,
3030 LLVMAtomicOrdering ordering,
3031 LLVMBool singleThread) {
3032 AtomicRMWInst::BinOp intop;
3034 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
3035 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
3036 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
3037 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
3038 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
3039 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
3040 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
3041 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
3042 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
3043 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
3044 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
3046 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
3047 mapFromLLVMOrdering(ordering), singleThread ? SyncScope::SingleThread
3048 : SyncScope::System));
3051 LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr,
3052 LLVMValueRef Cmp, LLVMValueRef New,
3053 LLVMAtomicOrdering SuccessOrdering,
3054 LLVMAtomicOrdering FailureOrdering,
3055 LLVMBool singleThread) {
3057 return wrap(unwrap(B)->CreateAtomicCmpXchg(unwrap(Ptr), unwrap(Cmp),
3058 unwrap(New), mapFromLLVMOrdering(SuccessOrdering),
3059 mapFromLLVMOrdering(FailureOrdering),
3060 singleThread ? SyncScope::SingleThread : SyncScope::System));
3064 LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) {
3065 Value *P = unwrap<Value>(AtomicInst);
3067 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
3068 return I->getSyncScopeID() == SyncScope::SingleThread;
3069 return cast<AtomicCmpXchgInst>(P)->getSyncScopeID() ==
3070 SyncScope::SingleThread;
3073 void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) {
3074 Value *P = unwrap<Value>(AtomicInst);
3075 SyncScope::ID SSID = NewValue ? SyncScope::SingleThread : SyncScope::System;
3077 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
3078 return I->setSyncScopeID(SSID);
3079 return cast<AtomicCmpXchgInst>(P)->setSyncScopeID(SSID);
3082 LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) {
3083 Value *P = unwrap<Value>(CmpXchgInst);
3084 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering());
3087 void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst,
3088 LLVMAtomicOrdering Ordering) {
3089 Value *P = unwrap<Value>(CmpXchgInst);
3090 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3092 return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O);
3095 LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst) {
3096 Value *P = unwrap<Value>(CmpXchgInst);
3097 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering());
3100 void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst,
3101 LLVMAtomicOrdering Ordering) {
3102 Value *P = unwrap<Value>(CmpXchgInst);
3103 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3105 return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O);
3108 /*===-- Module providers --------------------------------------------------===*/
3110 LLVMModuleProviderRef
3111 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
3112 return reinterpret_cast<LLVMModuleProviderRef>(M);
3115 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
3120 /*===-- Memory buffers ----------------------------------------------------===*/
3122 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
3124 LLVMMemoryBufferRef *OutMemBuf,
3125 char **OutMessage) {
3127 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
3128 if (std::error_code EC = MBOrErr.getError()) {
3129 *OutMessage = strdup(EC.message().c_str());
3132 *OutMemBuf = wrap(MBOrErr.get().release());
3136 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
3137 char **OutMessage) {
3138 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
3139 if (std::error_code EC = MBOrErr.getError()) {
3140 *OutMessage = strdup(EC.message().c_str());
3143 *OutMemBuf = wrap(MBOrErr.get().release());
3147 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
3148 const char *InputData,
3149 size_t InputDataLength,
3150 const char *BufferName,
3151 LLVMBool RequiresNullTerminator) {
3153 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
3154 StringRef(BufferName),
3155 RequiresNullTerminator).release());
3158 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
3159 const char *InputData,
3160 size_t InputDataLength,
3161 const char *BufferName) {
3164 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
3165 StringRef(BufferName)).release());
3168 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
3169 return unwrap(MemBuf)->getBufferStart();
3172 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
3173 return unwrap(MemBuf)->getBufferSize();
3176 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
3177 delete unwrap(MemBuf);
3180 /*===-- Pass Registry -----------------------------------------------------===*/
3182 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
3183 return wrap(PassRegistry::getPassRegistry());
3186 /*===-- Pass Manager ------------------------------------------------------===*/
3188 LLVMPassManagerRef LLVMCreatePassManager() {
3189 return wrap(new legacy::PassManager());
3192 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
3193 return wrap(new legacy::FunctionPassManager(unwrap(M)));
3196 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
3197 return LLVMCreateFunctionPassManagerForModule(
3198 reinterpret_cast<LLVMModuleRef>(P));
3201 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
3202 return unwrap<legacy::PassManager>(PM)->run(*unwrap(M));
3205 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
3206 return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization();
3209 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
3210 return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
3213 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
3214 return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization();
3217 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
3221 /*===-- Threading ------------------------------------------------------===*/
3223 LLVMBool LLVMStartMultithreaded() {
3224 return LLVMIsMultithreaded();
3227 void LLVMStopMultithreaded() {
3230 LLVMBool LLVMIsMultithreaded() {
3231 return llvm_is_multithreaded();