1 //===-- Core.cpp ----------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the common infrastructure (including the C bindings)
11 // for libLLVMCore.a, which implements the LLVM intermediate representation.
13 //===----------------------------------------------------------------------===//
15 #include "llvm-c/Core.h"
16 #include "llvm/ADT/StringSwitch.h"
17 #include "llvm/Bitcode/ReaderWriter.h"
18 #include "llvm/IR/Attributes.h"
19 #include "AttributeSetNode.h"
20 #include "llvm/IR/CallSite.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/DerivedTypes.h"
23 #include "llvm/IR/DiagnosticInfo.h"
24 #include "llvm/IR/DiagnosticPrinter.h"
25 #include "llvm/IR/GlobalAlias.h"
26 #include "llvm/IR/GlobalVariable.h"
27 #include "llvm/IR/IRBuilder.h"
28 #include "llvm/IR/InlineAsm.h"
29 #include "llvm/IR/IntrinsicInst.h"
30 #include "llvm/IR/LLVMContext.h"
31 #include "llvm/IR/LegacyPassManager.h"
32 #include "llvm/IR/Module.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/FileSystem.h"
36 #include "llvm/Support/ManagedStatic.h"
37 #include "llvm/Support/MemoryBuffer.h"
38 #include "llvm/Support/Threading.h"
39 #include "llvm/Support/raw_ostream.h"
43 #include <system_error>
47 #define DEBUG_TYPE "ir"
49 void llvm::initializeCore(PassRegistry &Registry) {
50 initializeDominatorTreeWrapperPassPass(Registry);
51 initializePrintModulePassWrapperPass(Registry);
52 initializePrintFunctionPassWrapperPass(Registry);
53 initializePrintBasicBlockPassPass(Registry);
54 initializeVerifierLegacyPassPass(Registry);
57 void LLVMInitializeCore(LLVMPassRegistryRef R) {
58 initializeCore(*unwrap(R));
65 /*===-- Error handling ----------------------------------------------------===*/
67 char *LLVMCreateMessage(const char *Message) {
68 return strdup(Message);
71 void LLVMDisposeMessage(char *Message) {
76 /*===-- Operations on contexts --------------------------------------------===*/
78 static ManagedStatic<LLVMContext> GlobalContext;
80 LLVMContextRef LLVMContextCreate() {
81 return wrap(new LLVMContext());
84 LLVMContextRef LLVMGetGlobalContext() { return wrap(&*GlobalContext); }
86 void LLVMContextSetDiagnosticHandler(LLVMContextRef C,
87 LLVMDiagnosticHandler Handler,
88 void *DiagnosticContext) {
89 unwrap(C)->setDiagnosticHandler(
90 LLVM_EXTENSION reinterpret_cast<LLVMContext::DiagnosticHandlerTy>(
95 LLVMDiagnosticHandler LLVMContextGetDiagnosticHandler(LLVMContextRef C) {
96 return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>(
97 unwrap(C)->getDiagnosticHandler());
100 void *LLVMContextGetDiagnosticContext(LLVMContextRef C) {
101 return unwrap(C)->getDiagnosticContext();
104 void LLVMContextSetYieldCallback(LLVMContextRef C, LLVMYieldCallback Callback,
105 void *OpaqueHandle) {
107 LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
108 unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
111 void LLVMContextDispose(LLVMContextRef C) {
115 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char *Name,
117 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
120 unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) {
121 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
124 #define GET_ATTR_KIND_FROM_NAME
125 #include "AttributesCompatFunc.inc"
127 unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) {
128 return getAttrKindFromName(StringRef(Name, SLen));
131 unsigned LLVMGetLastEnumAttributeKind(void) {
132 return Attribute::AttrKind::EndAttrKinds;
135 LLVMAttributeRef LLVMCreateEnumAttribute(LLVMContextRef C, unsigned KindID,
137 return wrap(Attribute::get(*unwrap(C), (Attribute::AttrKind)KindID, Val));
140 unsigned LLVMGetEnumAttributeKind(LLVMAttributeRef A) {
141 return unwrap(A).getKindAsEnum();
144 uint64_t LLVMGetEnumAttributeValue(LLVMAttributeRef A) {
145 auto Attr = unwrap(A);
146 if (Attr.isEnumAttribute())
148 return Attr.getValueAsInt();
151 LLVMAttributeRef LLVMCreateStringAttribute(LLVMContextRef C,
152 const char *K, unsigned KLength,
153 const char *V, unsigned VLength) {
154 return wrap(Attribute::get(*unwrap(C), StringRef(K, KLength),
155 StringRef(V, VLength)));
158 const char *LLVMGetStringAttributeKind(LLVMAttributeRef A,
160 auto S = unwrap(A).getKindAsString();
165 const char *LLVMGetStringAttributeValue(LLVMAttributeRef A,
167 auto S = unwrap(A).getValueAsString();
172 LLVMBool LLVMIsEnumAttribute(LLVMAttributeRef A) {
173 auto Attr = unwrap(A);
174 return Attr.isEnumAttribute() || Attr.isIntAttribute();
177 LLVMBool LLVMIsStringAttribute(LLVMAttributeRef A) {
178 return unwrap(A).isStringAttribute();
181 char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) {
182 std::string MsgStorage;
183 raw_string_ostream Stream(MsgStorage);
184 DiagnosticPrinterRawOStream DP(Stream);
186 unwrap(DI)->print(DP);
189 return LLVMCreateMessage(MsgStorage.c_str());
192 LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI) {
193 LLVMDiagnosticSeverity severity;
195 switch(unwrap(DI)->getSeverity()) {
197 severity = LLVMDSError;
200 severity = LLVMDSWarning;
203 severity = LLVMDSRemark;
206 severity = LLVMDSNote;
213 /*===-- Operations on modules ---------------------------------------------===*/
215 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
216 return wrap(new Module(ModuleID, *GlobalContext));
219 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
221 return wrap(new Module(ModuleID, *unwrap(C)));
224 void LLVMDisposeModule(LLVMModuleRef M) {
228 const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) {
229 auto &Str = unwrap(M)->getModuleIdentifier();
234 void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) {
235 unwrap(M)->setModuleIdentifier(StringRef(Ident, Len));
239 /*--.. Data layout .........................................................--*/
240 const char *LLVMGetDataLayoutStr(LLVMModuleRef M) {
241 return unwrap(M)->getDataLayoutStr().c_str();
244 const char *LLVMGetDataLayout(LLVMModuleRef M) {
245 return LLVMGetDataLayoutStr(M);
248 void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
249 unwrap(M)->setDataLayout(DataLayoutStr);
252 /*--.. Target triple .......................................................--*/
253 const char * LLVMGetTarget(LLVMModuleRef M) {
254 return unwrap(M)->getTargetTriple().c_str();
257 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
258 unwrap(M)->setTargetTriple(Triple);
261 void LLVMDumpModule(LLVMModuleRef M) {
265 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
266 char **ErrorMessage) {
268 raw_fd_ostream dest(Filename, EC, sys::fs::F_Text);
270 *ErrorMessage = strdup(EC.message().c_str());
274 unwrap(M)->print(dest, nullptr);
278 if (dest.has_error()) {
279 *ErrorMessage = strdup("Error printing to file");
286 char *LLVMPrintModuleToString(LLVMModuleRef M) {
288 raw_string_ostream os(buf);
290 unwrap(M)->print(os, nullptr);
293 return strdup(buf.c_str());
296 /*--.. Operations on inline assembler ......................................--*/
297 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
298 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
302 /*--.. Operations on module contexts ......................................--*/
303 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
304 return wrap(&unwrap(M)->getContext());
308 /*===-- Operations on types -----------------------------------------------===*/
310 /*--.. Operations on all types (mostly) ....................................--*/
312 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
313 switch (unwrap(Ty)->getTypeID()) {
315 return LLVMVoidTypeKind;
317 return LLVMHalfTypeKind;
318 case Type::FloatTyID:
319 return LLVMFloatTypeKind;
320 case Type::DoubleTyID:
321 return LLVMDoubleTypeKind;
322 case Type::X86_FP80TyID:
323 return LLVMX86_FP80TypeKind;
324 case Type::FP128TyID:
325 return LLVMFP128TypeKind;
326 case Type::PPC_FP128TyID:
327 return LLVMPPC_FP128TypeKind;
328 case Type::LabelTyID:
329 return LLVMLabelTypeKind;
330 case Type::MetadataTyID:
331 return LLVMMetadataTypeKind;
332 case Type::IntegerTyID:
333 return LLVMIntegerTypeKind;
334 case Type::FunctionTyID:
335 return LLVMFunctionTypeKind;
336 case Type::StructTyID:
337 return LLVMStructTypeKind;
338 case Type::ArrayTyID:
339 return LLVMArrayTypeKind;
340 case Type::PointerTyID:
341 return LLVMPointerTypeKind;
342 case Type::VectorTyID:
343 return LLVMVectorTypeKind;
344 case Type::X86_MMXTyID:
345 return LLVMX86_MMXTypeKind;
346 case Type::TokenTyID:
347 return LLVMTokenTypeKind;
349 llvm_unreachable("Unhandled TypeID.");
352 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
354 return unwrap(Ty)->isSized();
357 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
358 return wrap(&unwrap(Ty)->getContext());
361 void LLVMDumpType(LLVMTypeRef Ty) {
362 return unwrap(Ty)->dump();
365 char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
367 raw_string_ostream os(buf);
370 unwrap(Ty)->print(os);
372 os << "Printing <null> Type";
376 return strdup(buf.c_str());
379 /*--.. Operations on integer types .........................................--*/
381 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
382 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
384 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
385 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
387 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
388 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
390 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
391 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
393 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
394 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
396 LLVMTypeRef LLVMInt128TypeInContext(LLVMContextRef C) {
397 return (LLVMTypeRef) Type::getInt128Ty(*unwrap(C));
399 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
400 return wrap(IntegerType::get(*unwrap(C), NumBits));
403 LLVMTypeRef LLVMInt1Type(void) {
404 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
406 LLVMTypeRef LLVMInt8Type(void) {
407 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
409 LLVMTypeRef LLVMInt16Type(void) {
410 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
412 LLVMTypeRef LLVMInt32Type(void) {
413 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
415 LLVMTypeRef LLVMInt64Type(void) {
416 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
418 LLVMTypeRef LLVMInt128Type(void) {
419 return LLVMInt128TypeInContext(LLVMGetGlobalContext());
421 LLVMTypeRef LLVMIntType(unsigned NumBits) {
422 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
425 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
426 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
429 /*--.. Operations on real types ............................................--*/
431 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
432 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
434 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
435 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
437 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
438 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
440 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
441 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
443 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
444 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
446 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
447 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
449 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
450 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
452 LLVMTypeRef LLVMTokenTypeInContext(LLVMContextRef C) {
453 return (LLVMTypeRef) Type::getTokenTy(*unwrap(C));
456 LLVMTypeRef LLVMHalfType(void) {
457 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
459 LLVMTypeRef LLVMFloatType(void) {
460 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
462 LLVMTypeRef LLVMDoubleType(void) {
463 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
465 LLVMTypeRef LLVMX86FP80Type(void) {
466 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
468 LLVMTypeRef LLVMFP128Type(void) {
469 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
471 LLVMTypeRef LLVMPPCFP128Type(void) {
472 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
474 LLVMTypeRef LLVMX86MMXType(void) {
475 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
478 /*--.. Operations on function types ........................................--*/
480 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
481 LLVMTypeRef *ParamTypes, unsigned ParamCount,
483 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
484 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
487 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
488 return unwrap<FunctionType>(FunctionTy)->isVarArg();
491 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
492 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
495 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
496 return unwrap<FunctionType>(FunctionTy)->getNumParams();
499 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
500 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
501 for (FunctionType::param_iterator I = Ty->param_begin(),
502 E = Ty->param_end(); I != E; ++I)
506 /*--.. Operations on struct types ..........................................--*/
508 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
509 unsigned ElementCount, LLVMBool Packed) {
510 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
511 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
514 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
515 unsigned ElementCount, LLVMBool Packed) {
516 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
517 ElementCount, Packed);
520 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
522 return wrap(StructType::create(*unwrap(C), Name));
525 const char *LLVMGetStructName(LLVMTypeRef Ty)
527 StructType *Type = unwrap<StructType>(Ty);
528 if (!Type->hasName())
530 return Type->getName().data();
533 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
534 unsigned ElementCount, LLVMBool Packed) {
535 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
536 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
539 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
540 return unwrap<StructType>(StructTy)->getNumElements();
543 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
544 StructType *Ty = unwrap<StructType>(StructTy);
545 for (StructType::element_iterator I = Ty->element_begin(),
546 E = Ty->element_end(); I != E; ++I)
550 LLVMTypeRef LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy, unsigned i) {
551 StructType *Ty = unwrap<StructType>(StructTy);
552 return wrap(Ty->getTypeAtIndex(i));
555 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
556 return unwrap<StructType>(StructTy)->isPacked();
559 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
560 return unwrap<StructType>(StructTy)->isOpaque();
563 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
564 return wrap(unwrap(M)->getTypeByName(Name));
567 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
569 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
570 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
573 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
574 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
577 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
578 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
581 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
582 return wrap(unwrap<SequentialType>(Ty)->getElementType());
585 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
586 return unwrap<ArrayType>(ArrayTy)->getNumElements();
589 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
590 return unwrap<PointerType>(PointerTy)->getAddressSpace();
593 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
594 return unwrap<VectorType>(VectorTy)->getNumElements();
597 /*--.. Operations on other types ...........................................--*/
599 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
600 return wrap(Type::getVoidTy(*unwrap(C)));
602 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
603 return wrap(Type::getLabelTy(*unwrap(C)));
606 LLVMTypeRef LLVMVoidType(void) {
607 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
609 LLVMTypeRef LLVMLabelType(void) {
610 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
613 /*===-- Operations on values ----------------------------------------------===*/
615 /*--.. Operations on all values ............................................--*/
617 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
618 return wrap(unwrap(Val)->getType());
621 LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) {
622 switch(unwrap(Val)->getValueID()) {
623 #define HANDLE_VALUE(Name) \
624 case Value::Name##Val: \
625 return LLVM##Name##ValueKind;
626 #include "llvm/IR/Value.def"
628 return LLVMInstructionValueKind;
632 const char *LLVMGetValueName(LLVMValueRef Val) {
633 return unwrap(Val)->getName().data();
636 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
637 unwrap(Val)->setName(Name);
640 void LLVMDumpValue(LLVMValueRef Val) {
644 char* LLVMPrintValueToString(LLVMValueRef Val) {
646 raw_string_ostream os(buf);
649 unwrap(Val)->print(os);
651 os << "Printing <null> Value";
655 return strdup(buf.c_str());
658 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
659 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
662 int LLVMHasMetadata(LLVMValueRef Inst) {
663 return unwrap<Instruction>(Inst)->hasMetadata();
666 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
667 auto *I = unwrap<Instruction>(Inst);
668 assert(I && "Expected instruction");
669 if (auto *MD = I->getMetadata(KindID))
670 return wrap(MetadataAsValue::get(I->getContext(), MD));
674 // MetadataAsValue uses a canonical format which strips the actual MDNode for
675 // MDNode with just a single constant value, storing just a ConstantAsMetadata
676 // This undoes this canonicalization, reconstructing the MDNode.
677 static MDNode *extractMDNode(MetadataAsValue *MAV) {
678 Metadata *MD = MAV->getMetadata();
679 assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
680 "Expected a metadata node or a canonicalized constant");
682 if (MDNode *N = dyn_cast<MDNode>(MD))
685 return MDNode::get(MAV->getContext(), MD);
688 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
689 MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
691 unwrap<Instruction>(Inst)->setMetadata(KindID, N);
694 /*--.. Conversion functions ................................................--*/
696 #define LLVM_DEFINE_VALUE_CAST(name) \
697 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
698 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
701 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
703 LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) {
704 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
705 if (isa<MDNode>(MD->getMetadata()) ||
706 isa<ValueAsMetadata>(MD->getMetadata()))
711 LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) {
712 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
713 if (isa<MDString>(MD->getMetadata()))
718 /*--.. Operations on Uses ..................................................--*/
719 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
720 Value *V = unwrap(Val);
721 Value::use_iterator I = V->use_begin();
722 if (I == V->use_end())
727 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
728 Use *Next = unwrap(U)->getNext();
734 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
735 return wrap(unwrap(U)->getUser());
738 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
739 return wrap(unwrap(U)->get());
742 /*--.. Operations on Users .................................................--*/
744 static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N,
746 Metadata *Op = N->getOperand(Index);
749 if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
750 return wrap(C->getValue());
751 return wrap(MetadataAsValue::get(Context, Op));
754 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
755 Value *V = unwrap(Val);
756 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
757 if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
758 assert(Index == 0 && "Function-local metadata can only have one operand");
759 return wrap(L->getValue());
761 return getMDNodeOperandImpl(V->getContext(),
762 cast<MDNode>(MD->getMetadata()), Index);
765 return wrap(cast<User>(V)->getOperand(Index));
768 LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) {
769 Value *V = unwrap(Val);
770 return wrap(&cast<User>(V)->getOperandUse(Index));
773 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
774 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
777 int LLVMGetNumOperands(LLVMValueRef Val) {
778 Value *V = unwrap(Val);
779 if (isa<MetadataAsValue>(V))
780 return LLVMGetMDNodeNumOperands(Val);
782 return cast<User>(V)->getNumOperands();
785 /*--.. Operations on constants of any type .................................--*/
787 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
788 return wrap(Constant::getNullValue(unwrap(Ty)));
791 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
792 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
795 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
796 return wrap(UndefValue::get(unwrap(Ty)));
799 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
800 return isa<Constant>(unwrap(Ty));
803 LLVMBool LLVMIsNull(LLVMValueRef Val) {
804 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
805 return C->isNullValue();
809 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
810 return isa<UndefValue>(unwrap(Val));
813 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
814 return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
817 /*--.. Operations on metadata nodes ........................................--*/
819 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
821 LLVMContext &Context = *unwrap(C);
822 return wrap(MetadataAsValue::get(
823 Context, MDString::get(Context, StringRef(Str, SLen))));
826 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
827 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
830 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
832 LLVMContext &Context = *unwrap(C);
833 SmallVector<Metadata *, 8> MDs;
834 for (auto *OV : makeArrayRef(Vals, Count)) {
835 Value *V = unwrap(OV);
839 else if (auto *C = dyn_cast<Constant>(V))
840 MD = ConstantAsMetadata::get(C);
841 else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
842 MD = MDV->getMetadata();
843 assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
844 "outside of direct argument to call");
846 // This is function-local metadata. Pretend to make an MDNode.
848 "Expected only one operand to function-local metadata");
849 return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V)));
854 return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs)));
857 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
858 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
861 const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
862 if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
863 if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
864 *Length = S->getString().size();
865 return S->getString().data();
871 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) {
872 auto *MD = cast<MetadataAsValue>(unwrap(V));
873 if (isa<ValueAsMetadata>(MD->getMetadata()))
875 return cast<MDNode>(MD->getMetadata())->getNumOperands();
878 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) {
879 auto *MD = cast<MetadataAsValue>(unwrap(V));
880 if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
881 *Dest = wrap(MDV->getValue());
884 const auto *N = cast<MDNode>(MD->getMetadata());
885 const unsigned numOperands = N->getNumOperands();
886 LLVMContext &Context = unwrap(V)->getContext();
887 for (unsigned i = 0; i < numOperands; i++)
888 Dest[i] = getMDNodeOperandImpl(Context, N, i);
891 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char *Name) {
892 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
893 return N->getNumOperands();
898 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char *Name,
899 LLVMValueRef *Dest) {
900 NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
903 LLVMContext &Context = unwrap(M)->getContext();
904 for (unsigned i=0;i<N->getNumOperands();i++)
905 Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
908 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char *Name,
910 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
915 N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
918 /*--.. Operations on scalar constants ......................................--*/
920 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
921 LLVMBool SignExtend) {
922 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
925 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
927 const uint64_t Words[]) {
928 IntegerType *Ty = unwrap<IntegerType>(IntTy);
929 return wrap(ConstantInt::get(Ty->getContext(),
930 APInt(Ty->getBitWidth(),
931 makeArrayRef(Words, NumWords))));
934 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
936 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
940 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
941 unsigned SLen, uint8_t Radix) {
942 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
946 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
947 return wrap(ConstantFP::get(unwrap(RealTy), N));
950 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
951 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
954 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
956 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
959 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
960 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
963 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
964 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
967 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
968 ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
969 Type *Ty = cFP->getType();
971 if (Ty->isFloatTy()) {
973 return cFP->getValueAPF().convertToFloat();
976 if (Ty->isDoubleTy()) {
978 return cFP->getValueAPF().convertToDouble();
982 APFloat APF = cFP->getValueAPF();
983 APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &APFLosesInfo);
984 *LosesInfo = APFLosesInfo;
985 return APF.convertToDouble();
988 /*--.. Operations on composite constants ...................................--*/
990 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
992 LLVMBool DontNullTerminate) {
993 /* Inverted the sense of AddNull because ', 0)' is a
994 better mnemonic for null termination than ', 1)'. */
995 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
996 DontNullTerminate == 0));
999 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
1000 LLVMBool DontNullTerminate) {
1001 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
1005 LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef C, unsigned idx) {
1006 return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
1009 LLVMBool LLVMIsConstantString(LLVMValueRef C) {
1010 return unwrap<ConstantDataSequential>(C)->isString();
1013 const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1014 StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1015 *Length = Str.size();
1019 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
1020 LLVMValueRef *ConstantVals, unsigned Length) {
1021 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1022 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1025 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
1026 LLVMValueRef *ConstantVals,
1027 unsigned Count, LLVMBool Packed) {
1028 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1029 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
1033 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
1035 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1039 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
1040 LLVMValueRef *ConstantVals,
1042 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1043 StructType *Ty = cast<StructType>(unwrap(StructTy));
1045 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
1048 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
1049 return wrap(ConstantVector::get(makeArrayRef(
1050 unwrap<Constant>(ScalarConstantVals, Size), Size)));
1053 /*-- Opcode mapping */
1055 static LLVMOpcode map_to_llvmopcode(int opcode)
1058 default: llvm_unreachable("Unhandled Opcode.");
1059 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
1060 #include "llvm/IR/Instruction.def"
1065 static int map_from_llvmopcode(LLVMOpcode code)
1068 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
1069 #include "llvm/IR/Instruction.def"
1072 llvm_unreachable("Unhandled Opcode.");
1075 /*--.. Constant expressions ................................................--*/
1077 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
1078 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
1081 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
1082 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
1085 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
1086 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
1089 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
1090 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1093 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
1094 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
1097 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
1098 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
1102 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
1103 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
1106 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
1107 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
1110 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1111 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
1112 unwrap<Constant>(RHSConstant)));
1115 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
1116 LLVMValueRef RHSConstant) {
1117 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
1118 unwrap<Constant>(RHSConstant)));
1121 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
1122 LLVMValueRef RHSConstant) {
1123 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
1124 unwrap<Constant>(RHSConstant)));
1127 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1128 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
1129 unwrap<Constant>(RHSConstant)));
1132 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1133 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
1134 unwrap<Constant>(RHSConstant)));
1137 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
1138 LLVMValueRef RHSConstant) {
1139 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
1140 unwrap<Constant>(RHSConstant)));
1143 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
1144 LLVMValueRef RHSConstant) {
1145 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
1146 unwrap<Constant>(RHSConstant)));
1149 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1150 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
1151 unwrap<Constant>(RHSConstant)));
1154 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1155 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
1156 unwrap<Constant>(RHSConstant)));
1159 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
1160 LLVMValueRef RHSConstant) {
1161 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
1162 unwrap<Constant>(RHSConstant)));
1165 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
1166 LLVMValueRef RHSConstant) {
1167 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
1168 unwrap<Constant>(RHSConstant)));
1171 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1172 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
1173 unwrap<Constant>(RHSConstant)));
1176 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1177 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
1178 unwrap<Constant>(RHSConstant)));
1181 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1182 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
1183 unwrap<Constant>(RHSConstant)));
1186 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
1187 LLVMValueRef RHSConstant) {
1188 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
1189 unwrap<Constant>(RHSConstant)));
1192 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1193 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
1194 unwrap<Constant>(RHSConstant)));
1197 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1198 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
1199 unwrap<Constant>(RHSConstant)));
1202 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1203 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
1204 unwrap<Constant>(RHSConstant)));
1207 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1208 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
1209 unwrap<Constant>(RHSConstant)));
1212 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1213 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
1214 unwrap<Constant>(RHSConstant)));
1217 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1218 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
1219 unwrap<Constant>(RHSConstant)));
1222 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1223 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
1224 unwrap<Constant>(RHSConstant)));
1227 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
1228 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1229 return wrap(ConstantExpr::getICmp(Predicate,
1230 unwrap<Constant>(LHSConstant),
1231 unwrap<Constant>(RHSConstant)));
1234 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
1235 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1236 return wrap(ConstantExpr::getFCmp(Predicate,
1237 unwrap<Constant>(LHSConstant),
1238 unwrap<Constant>(RHSConstant)));
1241 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1242 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1243 unwrap<Constant>(RHSConstant)));
1246 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1247 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
1248 unwrap<Constant>(RHSConstant)));
1251 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1252 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1253 unwrap<Constant>(RHSConstant)));
1256 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
1257 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1258 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1260 return wrap(ConstantExpr::getGetElementPtr(
1261 nullptr, unwrap<Constant>(ConstantVal), IdxList));
1264 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
1265 LLVMValueRef *ConstantIndices,
1266 unsigned NumIndices) {
1267 Constant* Val = unwrap<Constant>(ConstantVal);
1268 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1270 return wrap(ConstantExpr::getInBoundsGetElementPtr(nullptr, Val, IdxList));
1273 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1274 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1278 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1279 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1283 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1284 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1288 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1289 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1293 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1294 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1298 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1299 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1303 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1304 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1308 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1309 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1313 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1314 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1318 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1319 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1323 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1324 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1328 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1329 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1333 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1334 LLVMTypeRef ToType) {
1335 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1339 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1340 LLVMTypeRef ToType) {
1341 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1345 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1346 LLVMTypeRef ToType) {
1347 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1351 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1352 LLVMTypeRef ToType) {
1353 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1357 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1358 LLVMTypeRef ToType) {
1359 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1363 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1364 LLVMBool isSigned) {
1365 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1366 unwrap(ToType), isSigned));
1369 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1370 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1374 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1375 LLVMValueRef ConstantIfTrue,
1376 LLVMValueRef ConstantIfFalse) {
1377 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1378 unwrap<Constant>(ConstantIfTrue),
1379 unwrap<Constant>(ConstantIfFalse)));
1382 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1383 LLVMValueRef IndexConstant) {
1384 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1385 unwrap<Constant>(IndexConstant)));
1388 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1389 LLVMValueRef ElementValueConstant,
1390 LLVMValueRef IndexConstant) {
1391 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1392 unwrap<Constant>(ElementValueConstant),
1393 unwrap<Constant>(IndexConstant)));
1396 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1397 LLVMValueRef VectorBConstant,
1398 LLVMValueRef MaskConstant) {
1399 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1400 unwrap<Constant>(VectorBConstant),
1401 unwrap<Constant>(MaskConstant)));
1404 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1406 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1407 makeArrayRef(IdxList, NumIdx)));
1410 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1411 LLVMValueRef ElementValueConstant,
1412 unsigned *IdxList, unsigned NumIdx) {
1413 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1414 unwrap<Constant>(ElementValueConstant),
1415 makeArrayRef(IdxList, NumIdx)));
1418 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1419 const char *Constraints,
1420 LLVMBool HasSideEffects,
1421 LLVMBool IsAlignStack) {
1422 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1423 Constraints, HasSideEffects, IsAlignStack));
1426 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1427 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1430 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1432 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1433 return wrap(unwrap<GlobalValue>(Global)->getParent());
1436 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1437 return unwrap<GlobalValue>(Global)->isDeclaration();
1440 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1441 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1442 case GlobalValue::ExternalLinkage:
1443 return LLVMExternalLinkage;
1444 case GlobalValue::AvailableExternallyLinkage:
1445 return LLVMAvailableExternallyLinkage;
1446 case GlobalValue::LinkOnceAnyLinkage:
1447 return LLVMLinkOnceAnyLinkage;
1448 case GlobalValue::LinkOnceODRLinkage:
1449 return LLVMLinkOnceODRLinkage;
1450 case GlobalValue::WeakAnyLinkage:
1451 return LLVMWeakAnyLinkage;
1452 case GlobalValue::WeakODRLinkage:
1453 return LLVMWeakODRLinkage;
1454 case GlobalValue::AppendingLinkage:
1455 return LLVMAppendingLinkage;
1456 case GlobalValue::InternalLinkage:
1457 return LLVMInternalLinkage;
1458 case GlobalValue::PrivateLinkage:
1459 return LLVMPrivateLinkage;
1460 case GlobalValue::ExternalWeakLinkage:
1461 return LLVMExternalWeakLinkage;
1462 case GlobalValue::CommonLinkage:
1463 return LLVMCommonLinkage;
1466 llvm_unreachable("Invalid GlobalValue linkage!");
1469 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1470 GlobalValue *GV = unwrap<GlobalValue>(Global);
1473 case LLVMExternalLinkage:
1474 GV->setLinkage(GlobalValue::ExternalLinkage);
1476 case LLVMAvailableExternallyLinkage:
1477 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1479 case LLVMLinkOnceAnyLinkage:
1480 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1482 case LLVMLinkOnceODRLinkage:
1483 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1485 case LLVMLinkOnceODRAutoHideLinkage:
1486 DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1487 "longer supported.");
1489 case LLVMWeakAnyLinkage:
1490 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1492 case LLVMWeakODRLinkage:
1493 GV->setLinkage(GlobalValue::WeakODRLinkage);
1495 case LLVMAppendingLinkage:
1496 GV->setLinkage(GlobalValue::AppendingLinkage);
1498 case LLVMInternalLinkage:
1499 GV->setLinkage(GlobalValue::InternalLinkage);
1501 case LLVMPrivateLinkage:
1502 GV->setLinkage(GlobalValue::PrivateLinkage);
1504 case LLVMLinkerPrivateLinkage:
1505 GV->setLinkage(GlobalValue::PrivateLinkage);
1507 case LLVMLinkerPrivateWeakLinkage:
1508 GV->setLinkage(GlobalValue::PrivateLinkage);
1510 case LLVMDLLImportLinkage:
1512 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1514 case LLVMDLLExportLinkage:
1516 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1518 case LLVMExternalWeakLinkage:
1519 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1521 case LLVMGhostLinkage:
1523 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1525 case LLVMCommonLinkage:
1526 GV->setLinkage(GlobalValue::CommonLinkage);
1531 const char *LLVMGetSection(LLVMValueRef Global) {
1532 // Using .data() is safe because of how GlobalObject::setSection is
1534 return unwrap<GlobalValue>(Global)->getSection().data();
1537 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1538 unwrap<GlobalObject>(Global)->setSection(Section);
1541 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1542 return static_cast<LLVMVisibility>(
1543 unwrap<GlobalValue>(Global)->getVisibility());
1546 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1547 unwrap<GlobalValue>(Global)
1548 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1551 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
1552 return static_cast<LLVMDLLStorageClass>(
1553 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1556 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
1557 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1558 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1561 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
1562 return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
1565 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
1566 unwrap<GlobalValue>(Global)->setUnnamedAddr(
1567 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
1568 : GlobalValue::UnnamedAddr::None);
1571 /*--.. Operations on global variables, load and store instructions .........--*/
1573 unsigned LLVMGetAlignment(LLVMValueRef V) {
1574 Value *P = unwrap<Value>(V);
1575 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1576 return GV->getAlignment();
1577 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1578 return AI->getAlignment();
1579 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1580 return LI->getAlignment();
1581 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1582 return SI->getAlignment();
1585 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1588 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1589 Value *P = unwrap<Value>(V);
1590 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
1591 GV->setAlignment(Bytes);
1592 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1593 AI->setAlignment(Bytes);
1594 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1595 LI->setAlignment(Bytes);
1596 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1597 SI->setAlignment(Bytes);
1600 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1603 /*--.. Operations on global variables ......................................--*/
1605 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1606 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1607 GlobalValue::ExternalLinkage, nullptr, Name));
1610 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1612 unsigned AddressSpace) {
1613 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1614 GlobalValue::ExternalLinkage, nullptr, Name,
1615 nullptr, GlobalVariable::NotThreadLocal,
1619 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1620 return wrap(unwrap(M)->getNamedGlobal(Name));
1623 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1624 Module *Mod = unwrap(M);
1625 Module::global_iterator I = Mod->global_begin();
1626 if (I == Mod->global_end())
1631 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1632 Module *Mod = unwrap(M);
1633 Module::global_iterator I = Mod->global_end();
1634 if (I == Mod->global_begin())
1639 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1640 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1641 Module::global_iterator I(GV);
1642 if (++I == GV->getParent()->global_end())
1647 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1648 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1649 Module::global_iterator I(GV);
1650 if (I == GV->getParent()->global_begin())
1655 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1656 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1659 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1660 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1661 if ( !GV->hasInitializer() )
1663 return wrap(GV->getInitializer());
1666 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1667 unwrap<GlobalVariable>(GlobalVar)
1668 ->setInitializer(unwrap<Constant>(ConstantVal));
1671 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1672 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1675 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1676 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1679 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1680 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1683 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1684 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1687 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1688 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1689 case GlobalVariable::NotThreadLocal:
1690 return LLVMNotThreadLocal;
1691 case GlobalVariable::GeneralDynamicTLSModel:
1692 return LLVMGeneralDynamicTLSModel;
1693 case GlobalVariable::LocalDynamicTLSModel:
1694 return LLVMLocalDynamicTLSModel;
1695 case GlobalVariable::InitialExecTLSModel:
1696 return LLVMInitialExecTLSModel;
1697 case GlobalVariable::LocalExecTLSModel:
1698 return LLVMLocalExecTLSModel;
1701 llvm_unreachable("Invalid GlobalVariable thread local mode");
1704 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1705 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1708 case LLVMNotThreadLocal:
1709 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1711 case LLVMGeneralDynamicTLSModel:
1712 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1714 case LLVMLocalDynamicTLSModel:
1715 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1717 case LLVMInitialExecTLSModel:
1718 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1720 case LLVMLocalExecTLSModel:
1721 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1726 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1727 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1730 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1731 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1734 /*--.. Operations on aliases ......................................--*/
1736 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1738 auto *PTy = cast<PointerType>(unwrap(Ty));
1739 return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
1740 GlobalValue::ExternalLinkage, Name,
1741 unwrap<Constant>(Aliasee), unwrap(M)));
1744 /*--.. Operations on functions .............................................--*/
1746 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1747 LLVMTypeRef FunctionTy) {
1748 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1749 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1752 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1753 return wrap(unwrap(M)->getFunction(Name));
1756 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1757 Module *Mod = unwrap(M);
1758 Module::iterator I = Mod->begin();
1759 if (I == Mod->end())
1764 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1765 Module *Mod = unwrap(M);
1766 Module::iterator I = Mod->end();
1767 if (I == Mod->begin())
1772 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1773 Function *Func = unwrap<Function>(Fn);
1774 Module::iterator I(Func);
1775 if (++I == Func->getParent()->end())
1780 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1781 Function *Func = unwrap<Function>(Fn);
1782 Module::iterator I(Func);
1783 if (I == Func->getParent()->begin())
1788 void LLVMDeleteFunction(LLVMValueRef Fn) {
1789 unwrap<Function>(Fn)->eraseFromParent();
1792 LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) {
1793 return unwrap<Function>(Fn)->hasPersonalityFn();
1796 LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) {
1797 return wrap(unwrap<Function>(Fn)->getPersonalityFn());
1800 void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) {
1801 unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
1804 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1805 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1806 return F->getIntrinsicID();
1810 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1811 return unwrap<Function>(Fn)->getCallingConv();
1814 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1815 return unwrap<Function>(Fn)->setCallingConv(
1816 static_cast<CallingConv::ID>(CC));
1819 const char *LLVMGetGC(LLVMValueRef Fn) {
1820 Function *F = unwrap<Function>(Fn);
1821 return F->hasGC()? F->getGC().c_str() : nullptr;
1824 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1825 Function *F = unwrap<Function>(Fn);
1832 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1833 Function *Func = unwrap<Function>(Fn);
1834 const AttributeSet PAL = Func->getAttributes();
1836 const AttributeSet PALnew =
1837 PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1838 AttributeSet::get(Func->getContext(),
1839 AttributeSet::FunctionIndex, B));
1840 Func->setAttributes(PALnew);
1843 void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1844 LLVMAttributeRef A) {
1845 unwrap<Function>(F)->addAttribute(Idx, unwrap(A));
1848 unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) {
1849 auto *ASN = AttributeSetNode::get(unwrap<Function>(F)->getAttributes(), Idx);
1850 return ASN->getNumAttributes();
1853 void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1854 LLVMAttributeRef *Attrs) {
1855 auto *ASN = AttributeSetNode::get(unwrap<Function>(F)->getAttributes(), Idx);
1856 for (auto A: make_range(ASN->begin(), ASN->end()))
1860 LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F,
1861 LLVMAttributeIndex Idx,
1863 return wrap(unwrap<Function>(F)->getAttribute(Idx,
1864 (Attribute::AttrKind)KindID));
1867 LLVMAttributeRef LLVMGetStringAttributeAtIndex(LLVMValueRef F,
1868 LLVMAttributeIndex Idx,
1869 const char *K, unsigned KLen) {
1870 return wrap(unwrap<Function>(F)->getAttribute(Idx, StringRef(K, KLen)));
1873 void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1875 unwrap<Function>(F)->removeAttribute(Idx, (Attribute::AttrKind)KindID);
1878 void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1879 const char *K, unsigned KLen) {
1880 unwrap<Function>(F)->removeAttribute(Idx, StringRef(K, KLen));
1883 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1885 Function *Func = unwrap<Function>(Fn);
1886 AttributeSet::AttrIndex Idx =
1887 AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
1890 B.addAttribute(A, V);
1891 AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
1892 Func->addAttributes(Idx, Set);
1895 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1896 Function *Func = unwrap<Function>(Fn);
1897 const AttributeSet PAL = Func->getAttributes();
1899 const AttributeSet PALnew =
1900 PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1901 AttributeSet::get(Func->getContext(),
1902 AttributeSet::FunctionIndex, B));
1903 Func->setAttributes(PALnew);
1906 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1907 Function *Func = unwrap<Function>(Fn);
1908 const AttributeSet PAL = Func->getAttributes();
1909 return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
1912 /*--.. Operations on parameters ............................................--*/
1914 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1915 // This function is strictly redundant to
1916 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1917 return unwrap<Function>(FnRef)->arg_size();
1920 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1921 Function *Fn = unwrap<Function>(FnRef);
1922 for (Function::arg_iterator I = Fn->arg_begin(),
1923 E = Fn->arg_end(); I != E; I++)
1924 *ParamRefs++ = wrap(&*I);
1927 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1928 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1934 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1935 return wrap(unwrap<Argument>(V)->getParent());
1938 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1939 Function *Func = unwrap<Function>(Fn);
1940 Function::arg_iterator I = Func->arg_begin();
1941 if (I == Func->arg_end())
1946 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1947 Function *Func = unwrap<Function>(Fn);
1948 Function::arg_iterator I = Func->arg_end();
1949 if (I == Func->arg_begin())
1954 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1955 Argument *A = unwrap<Argument>(Arg);
1956 Function::arg_iterator I(A);
1957 if (++I == A->getParent()->arg_end())
1962 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1963 Argument *A = unwrap<Argument>(Arg);
1964 Function::arg_iterator I(A);
1965 if (I == A->getParent()->arg_begin())
1970 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1971 Argument *A = unwrap<Argument>(Arg);
1973 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1976 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1977 Argument *A = unwrap<Argument>(Arg);
1979 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1982 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1983 Argument *A = unwrap<Argument>(Arg);
1984 return (LLVMAttribute)A->getParent()->getAttributes().
1985 Raw(A->getArgNo()+1);
1988 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1989 Argument *A = unwrap<Argument>(Arg);
1991 B.addAlignmentAttr(align);
1992 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
1995 /*--.. Operations on basic blocks ..........................................--*/
1997 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1998 return wrap(static_cast<Value*>(unwrap(BB)));
2001 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
2002 return isa<BasicBlock>(unwrap(Val));
2005 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
2006 return wrap(unwrap<BasicBlock>(Val));
2009 const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) {
2010 return unwrap(BB)->getName().data();
2013 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
2014 return wrap(unwrap(BB)->getParent());
2017 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
2018 return wrap(unwrap(BB)->getTerminator());
2021 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
2022 return unwrap<Function>(FnRef)->size();
2025 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
2026 Function *Fn = unwrap<Function>(FnRef);
2027 for (BasicBlock &BB : *Fn)
2028 *BasicBlocksRefs++ = wrap(&BB);
2031 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
2032 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2035 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
2036 Function *Func = unwrap<Function>(Fn);
2037 Function::iterator I = Func->begin();
2038 if (I == Func->end())
2043 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
2044 Function *Func = unwrap<Function>(Fn);
2045 Function::iterator I = Func->end();
2046 if (I == Func->begin())
2051 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
2052 BasicBlock *Block = unwrap(BB);
2053 Function::iterator I(Block);
2054 if (++I == Block->getParent()->end())
2059 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
2060 BasicBlock *Block = unwrap(BB);
2061 Function::iterator I(Block);
2062 if (I == Block->getParent()->begin())
2067 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
2070 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
2073 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
2074 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
2077 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
2078 LLVMBasicBlockRef BBRef,
2080 BasicBlock *BB = unwrap(BBRef);
2081 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2084 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
2086 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
2089 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
2090 unwrap(BBRef)->eraseFromParent();
2093 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
2094 unwrap(BBRef)->removeFromParent();
2097 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2098 unwrap(BB)->moveBefore(unwrap(MovePos));
2101 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2102 unwrap(BB)->moveAfter(unwrap(MovePos));
2105 /*--.. Operations on instructions ..........................................--*/
2107 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
2108 return wrap(unwrap<Instruction>(Inst)->getParent());
2111 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
2112 BasicBlock *Block = unwrap(BB);
2113 BasicBlock::iterator I = Block->begin();
2114 if (I == Block->end())
2119 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
2120 BasicBlock *Block = unwrap(BB);
2121 BasicBlock::iterator I = Block->end();
2122 if (I == Block->begin())
2127 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
2128 Instruction *Instr = unwrap<Instruction>(Inst);
2129 BasicBlock::iterator I(Instr);
2130 if (++I == Instr->getParent()->end())
2135 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
2136 Instruction *Instr = unwrap<Instruction>(Inst);
2137 BasicBlock::iterator I(Instr);
2138 if (I == Instr->getParent()->begin())
2143 void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) {
2144 unwrap<Instruction>(Inst)->removeFromParent();
2147 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
2148 unwrap<Instruction>(Inst)->eraseFromParent();
2151 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
2152 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2153 return (LLVMIntPredicate)I->getPredicate();
2154 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2155 if (CE->getOpcode() == Instruction::ICmp)
2156 return (LLVMIntPredicate)CE->getPredicate();
2157 return (LLVMIntPredicate)0;
2160 LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
2161 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2162 return (LLVMRealPredicate)I->getPredicate();
2163 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2164 if (CE->getOpcode() == Instruction::FCmp)
2165 return (LLVMRealPredicate)CE->getPredicate();
2166 return (LLVMRealPredicate)0;
2169 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
2170 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2171 return map_to_llvmopcode(C->getOpcode());
2172 return (LLVMOpcode)0;
2175 LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {
2176 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2177 return wrap(C->clone());
2181 /*--.. Call and invoke instructions ........................................--*/
2183 unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) {
2184 return CallSite(unwrap<Instruction>(Instr)).getNumArgOperands();
2187 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
2188 return CallSite(unwrap<Instruction>(Instr)).getCallingConv();
2191 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
2192 return CallSite(unwrap<Instruction>(Instr))
2193 .setCallingConv(static_cast<CallingConv::ID>(CC));
2196 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
2198 CallSite Call = CallSite(unwrap<Instruction>(Instr));
2201 Call.getAttributes().addAttributes(Call->getContext(), index,
2202 AttributeSet::get(Call->getContext(),
2206 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
2208 CallSite Call = CallSite(unwrap<Instruction>(Instr));
2210 Call.setAttributes(Call.getAttributes()
2211 .removeAttributes(Call->getContext(), index,
2212 AttributeSet::get(Call->getContext(),
2216 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
2218 CallSite Call = CallSite(unwrap<Instruction>(Instr));
2220 B.addAlignmentAttr(align);
2221 Call.setAttributes(Call.getAttributes()
2222 .addAttributes(Call->getContext(), index,
2223 AttributeSet::get(Call->getContext(),
2227 void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2228 LLVMAttributeRef A) {
2229 CallSite(unwrap<Instruction>(C)).addAttribute(Idx, unwrap(A));
2232 unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C,
2233 LLVMAttributeIndex Idx) {
2234 auto CS = CallSite(unwrap<Instruction>(C));
2235 auto *ASN = AttributeSetNode::get(CS.getAttributes(), Idx);
2236 return ASN->getNumAttributes();
2239 void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx,
2240 LLVMAttributeRef *Attrs) {
2241 auto CS = CallSite(unwrap<Instruction>(C));
2242 auto *ASN = AttributeSetNode::get(CS.getAttributes(), Idx);
2243 for (auto A: make_range(ASN->begin(), ASN->end()))
2247 LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C,
2248 LLVMAttributeIndex Idx,
2250 return wrap(CallSite(unwrap<Instruction>(C))
2251 .getAttribute(Idx, (Attribute::AttrKind)KindID));
2254 LLVMAttributeRef LLVMGetCallSiteStringAttribute(LLVMValueRef C,
2255 LLVMAttributeIndex Idx,
2256 const char *K, unsigned KLen) {
2257 return wrap(CallSite(unwrap<Instruction>(C))
2258 .getAttribute(Idx, StringRef(K, KLen)));
2261 void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2263 CallSite(unwrap<Instruction>(C))
2264 .removeAttribute(Idx, (Attribute::AttrKind)KindID);
2267 void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2268 const char *K, unsigned KLen) {
2269 CallSite(unwrap<Instruction>(C)).removeAttribute(Idx, StringRef(K, KLen));
2272 LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) {
2273 return wrap(CallSite(unwrap<Instruction>(Instr)).getCalledValue());
2276 /*--.. Operations on call instructions (only) ..............................--*/
2278 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
2279 return unwrap<CallInst>(Call)->isTailCall();
2282 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
2283 unwrap<CallInst>(Call)->setTailCall(isTailCall);
2286 /*--.. Operations on invoke instructions (only) ............................--*/
2288 LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) {
2289 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2292 LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) {
2293 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2296 void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2297 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2300 void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2301 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2304 /*--.. Operations on terminators ...........................................--*/
2306 unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {
2307 return unwrap<TerminatorInst>(Term)->getNumSuccessors();
2310 LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) {
2311 return wrap(unwrap<TerminatorInst>(Term)->getSuccessor(i));
2314 void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
2315 return unwrap<TerminatorInst>(Term)->setSuccessor(i,unwrap(block));
2318 /*--.. Operations on branch instructions (only) ............................--*/
2320 LLVMBool LLVMIsConditional(LLVMValueRef Branch) {
2321 return unwrap<BranchInst>(Branch)->isConditional();
2324 LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) {
2325 return wrap(unwrap<BranchInst>(Branch)->getCondition());
2328 void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) {
2329 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2332 /*--.. Operations on switch instructions (only) ............................--*/
2334 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
2335 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2338 /*--.. Operations on alloca instructions (only) ............................--*/
2340 LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) {
2341 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
2344 /*--.. Operations on gep instructions (only) ...............................--*/
2346 LLVMBool LLVMIsInBounds(LLVMValueRef GEP) {
2347 return unwrap<GetElementPtrInst>(GEP)->isInBounds();
2350 void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) {
2351 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
2354 /*--.. Operations on phi nodes .............................................--*/
2356 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
2357 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
2358 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
2359 for (unsigned I = 0; I != Count; ++I)
2360 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
2363 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
2364 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
2367 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
2368 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
2371 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
2372 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
2375 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/
2377 unsigned LLVMGetNumIndices(LLVMValueRef Inst) {
2378 auto *I = unwrap(Inst);
2379 if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
2380 return GEP->getNumIndices();
2381 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2382 return EV->getNumIndices();
2383 if (auto *IV = dyn_cast<InsertValueInst>(I))
2384 return IV->getNumIndices();
2386 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
2389 const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
2390 auto *I = unwrap(Inst);
2391 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2392 return EV->getIndices().data();
2393 if (auto *IV = dyn_cast<InsertValueInst>(I))
2394 return IV->getIndices().data();
2396 "LLVMGetIndices applies only to extractvalue and insertvalue!");
2400 /*===-- Instruction builders ----------------------------------------------===*/
2402 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
2403 return wrap(new IRBuilder<>(*unwrap(C)));
2406 LLVMBuilderRef LLVMCreateBuilder(void) {
2407 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
2410 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
2411 LLVMValueRef Instr) {
2412 BasicBlock *BB = unwrap(Block);
2413 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
2414 unwrap(Builder)->SetInsertPoint(BB, I->getIterator());
2417 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2418 Instruction *I = unwrap<Instruction>(Instr);
2419 unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
2422 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
2423 BasicBlock *BB = unwrap(Block);
2424 unwrap(Builder)->SetInsertPoint(BB);
2427 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
2428 return wrap(unwrap(Builder)->GetInsertBlock());
2431 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
2432 unwrap(Builder)->ClearInsertionPoint();
2435 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2436 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
2439 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
2441 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
2444 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
2445 delete unwrap(Builder);
2448 /*--.. Metadata builders ...................................................--*/
2450 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
2452 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
2453 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
2456 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
2457 LLVMContext &Context = unwrap(Builder)->getContext();
2458 return wrap(MetadataAsValue::get(
2459 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
2462 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
2463 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
2467 /*--.. Instruction builders ................................................--*/
2469 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
2470 return wrap(unwrap(B)->CreateRetVoid());
2473 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
2474 return wrap(unwrap(B)->CreateRet(unwrap(V)));
2477 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
2479 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
2482 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
2483 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
2486 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
2487 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
2488 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
2491 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
2492 LLVMBasicBlockRef Else, unsigned NumCases) {
2493 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
2496 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
2497 unsigned NumDests) {
2498 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
2501 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
2502 LLVMValueRef *Args, unsigned NumArgs,
2503 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
2505 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
2506 makeArrayRef(unwrap(Args), NumArgs),
2510 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
2511 LLVMValueRef PersFn, unsigned NumClauses,
2513 // The personality used to live on the landingpad instruction, but now it
2514 // lives on the parent function. For compatibility, take the provided
2515 // personality and put it on the parent function.
2517 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
2518 cast<Function>(unwrap(PersFn)));
2519 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
2522 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
2523 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
2526 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
2527 return wrap(unwrap(B)->CreateUnreachable());
2530 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
2531 LLVMBasicBlockRef Dest) {
2532 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
2535 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
2536 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
2539 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
2540 return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
2543 LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
2544 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
2547 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
2548 unwrap<LandingPadInst>(LandingPad)->
2549 addClause(cast<Constant>(unwrap(ClauseVal)));
2552 LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) {
2553 return unwrap<LandingPadInst>(LandingPad)->isCleanup();
2556 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
2557 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2560 /*--.. Arithmetic ..........................................................--*/
2562 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2564 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2567 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2569 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2572 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2574 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2577 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2579 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2582 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2584 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2587 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2589 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2592 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2594 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2597 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2599 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2602 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2604 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2607 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2609 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2612 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2614 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2617 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2619 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2622 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2624 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2627 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2629 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2632 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2633 LLVMValueRef RHS, const char *Name) {
2634 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2637 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2639 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2642 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2644 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2647 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2649 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2652 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2654 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2657 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2659 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2662 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2664 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2667 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2669 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2672 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2674 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2677 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2679 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2682 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2684 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2687 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2688 LLVMValueRef LHS, LLVMValueRef RHS,
2690 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2691 unwrap(RHS), Name));
2694 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2695 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2698 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2700 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2703 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2705 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2708 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2709 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2712 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2713 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2716 /*--.. Memory ..............................................................--*/
2718 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2720 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2721 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2722 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2723 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2724 ITy, unwrap(Ty), AllocSize,
2725 nullptr, nullptr, "");
2726 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2729 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2730 LLVMValueRef Val, const char *Name) {
2731 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2732 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2733 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2734 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2735 ITy, unwrap(Ty), AllocSize,
2736 unwrap(Val), nullptr, "");
2737 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2740 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2742 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
2745 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2746 LLVMValueRef Val, const char *Name) {
2747 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2750 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2751 return wrap(unwrap(B)->Insert(
2752 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2755 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2757 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2760 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2761 LLVMValueRef PointerVal) {
2762 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2765 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
2767 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic;
2768 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered;
2769 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic;
2770 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire;
2771 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release;
2772 case LLVMAtomicOrderingAcquireRelease:
2773 return AtomicOrdering::AcquireRelease;
2774 case LLVMAtomicOrderingSequentiallyConsistent:
2775 return AtomicOrdering::SequentiallyConsistent;
2778 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
2781 static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) {
2783 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic;
2784 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered;
2785 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic;
2786 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire;
2787 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease;
2788 case AtomicOrdering::AcquireRelease:
2789 return LLVMAtomicOrderingAcquireRelease;
2790 case AtomicOrdering::SequentiallyConsistent:
2791 return LLVMAtomicOrderingSequentiallyConsistent;
2794 llvm_unreachable("Invalid AtomicOrdering value!");
2797 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
2798 LLVMBool isSingleThread, const char *Name) {
2800 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
2801 isSingleThread ? SingleThread : CrossThread,
2805 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2806 LLVMValueRef *Indices, unsigned NumIndices,
2808 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2809 return wrap(unwrap(B)->CreateGEP(nullptr, unwrap(Pointer), IdxList, Name));
2812 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2813 LLVMValueRef *Indices, unsigned NumIndices,
2815 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2817 unwrap(B)->CreateInBoundsGEP(nullptr, unwrap(Pointer), IdxList, Name));
2820 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2821 unsigned Idx, const char *Name) {
2822 return wrap(unwrap(B)->CreateStructGEP(nullptr, unwrap(Pointer), Idx, Name));
2825 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2827 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2830 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2832 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2835 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2836 Value *P = unwrap<Value>(MemAccessInst);
2837 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2838 return LI->isVolatile();
2839 return cast<StoreInst>(P)->isVolatile();
2842 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2843 Value *P = unwrap<Value>(MemAccessInst);
2844 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2845 return LI->setVolatile(isVolatile);
2846 return cast<StoreInst>(P)->setVolatile(isVolatile);
2849 LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) {
2850 Value *P = unwrap<Value>(MemAccessInst);
2852 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2853 O = LI->getOrdering();
2855 O = cast<StoreInst>(P)->getOrdering();
2856 return mapToLLVMOrdering(O);
2859 void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
2860 Value *P = unwrap<Value>(MemAccessInst);
2861 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
2863 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2864 return LI->setOrdering(O);
2865 return cast<StoreInst>(P)->setOrdering(O);
2868 /*--.. Casts ...............................................................--*/
2870 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2871 LLVMTypeRef DestTy, const char *Name) {
2872 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2875 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2876 LLVMTypeRef DestTy, const char *Name) {
2877 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2880 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2881 LLVMTypeRef DestTy, const char *Name) {
2882 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2885 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2886 LLVMTypeRef DestTy, const char *Name) {
2887 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2890 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2891 LLVMTypeRef DestTy, const char *Name) {
2892 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2895 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2896 LLVMTypeRef DestTy, const char *Name) {
2897 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2900 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2901 LLVMTypeRef DestTy, const char *Name) {
2902 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2905 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2906 LLVMTypeRef DestTy, const char *Name) {
2907 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2910 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2911 LLVMTypeRef DestTy, const char *Name) {
2912 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2915 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2916 LLVMTypeRef DestTy, const char *Name) {
2917 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2920 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2921 LLVMTypeRef DestTy, const char *Name) {
2922 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2925 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2926 LLVMTypeRef DestTy, const char *Name) {
2927 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2930 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
2931 LLVMTypeRef DestTy, const char *Name) {
2932 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
2935 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2936 LLVMTypeRef DestTy, const char *Name) {
2937 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2941 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2942 LLVMTypeRef DestTy, const char *Name) {
2943 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2947 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2948 LLVMTypeRef DestTy, const char *Name) {
2949 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2953 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2954 LLVMTypeRef DestTy, const char *Name) {
2955 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2956 unwrap(DestTy), Name));
2959 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2960 LLVMTypeRef DestTy, const char *Name) {
2961 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2964 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2965 LLVMTypeRef DestTy, const char *Name) {
2966 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2967 /*isSigned*/true, Name));
2970 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2971 LLVMTypeRef DestTy, const char *Name) {
2972 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2975 /*--.. Comparisons .........................................................--*/
2977 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2978 LLVMValueRef LHS, LLVMValueRef RHS,
2980 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2981 unwrap(LHS), unwrap(RHS), Name));
2984 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2985 LLVMValueRef LHS, LLVMValueRef RHS,
2987 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2988 unwrap(LHS), unwrap(RHS), Name));
2991 /*--.. Miscellaneous instructions ..........................................--*/
2993 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2994 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2997 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2998 LLVMValueRef *Args, unsigned NumArgs,
3000 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
3001 makeArrayRef(unwrap(Args), NumArgs),
3005 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
3006 LLVMValueRef Then, LLVMValueRef Else,
3008 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
3012 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
3013 LLVMTypeRef Ty, const char *Name) {
3014 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
3017 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
3018 LLVMValueRef Index, const char *Name) {
3019 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
3023 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
3024 LLVMValueRef EltVal, LLVMValueRef Index,
3026 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
3027 unwrap(Index), Name));
3030 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
3031 LLVMValueRef V2, LLVMValueRef Mask,
3033 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
3034 unwrap(Mask), Name));
3037 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
3038 unsigned Index, const char *Name) {
3039 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
3042 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
3043 LLVMValueRef EltVal, unsigned Index,
3045 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
3049 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
3051 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
3054 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
3056 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
3059 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
3060 LLVMValueRef RHS, const char *Name) {
3061 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
3064 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
3065 LLVMValueRef PTR, LLVMValueRef Val,
3066 LLVMAtomicOrdering ordering,
3067 LLVMBool singleThread) {
3068 AtomicRMWInst::BinOp intop;
3070 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
3071 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
3072 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
3073 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
3074 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
3075 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
3076 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
3077 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
3078 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
3079 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
3080 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
3082 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
3083 mapFromLLVMOrdering(ordering), singleThread ? SingleThread : CrossThread));
3086 LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr,
3087 LLVMValueRef Cmp, LLVMValueRef New,
3088 LLVMAtomicOrdering SuccessOrdering,
3089 LLVMAtomicOrdering FailureOrdering,
3090 LLVMBool singleThread) {
3092 return wrap(unwrap(B)->CreateAtomicCmpXchg(unwrap(Ptr), unwrap(Cmp),
3093 unwrap(New), mapFromLLVMOrdering(SuccessOrdering),
3094 mapFromLLVMOrdering(FailureOrdering),
3095 singleThread ? SingleThread : CrossThread));
3099 LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) {
3100 Value *P = unwrap<Value>(AtomicInst);
3102 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
3103 return I->getSynchScope() == SingleThread;
3104 return cast<AtomicCmpXchgInst>(P)->getSynchScope() == SingleThread;
3107 void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) {
3108 Value *P = unwrap<Value>(AtomicInst);
3109 SynchronizationScope Sync = NewValue ? SingleThread : CrossThread;
3111 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
3112 return I->setSynchScope(Sync);
3113 return cast<AtomicCmpXchgInst>(P)->setSynchScope(Sync);
3116 LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) {
3117 Value *P = unwrap<Value>(CmpXchgInst);
3118 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering());
3121 void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst,
3122 LLVMAtomicOrdering Ordering) {
3123 Value *P = unwrap<Value>(CmpXchgInst);
3124 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3126 return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O);
3129 LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst) {
3130 Value *P = unwrap<Value>(CmpXchgInst);
3131 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering());
3134 void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst,
3135 LLVMAtomicOrdering Ordering) {
3136 Value *P = unwrap<Value>(CmpXchgInst);
3137 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3139 return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O);
3142 /*===-- Module providers --------------------------------------------------===*/
3144 LLVMModuleProviderRef
3145 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
3146 return reinterpret_cast<LLVMModuleProviderRef>(M);
3149 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
3154 /*===-- Memory buffers ----------------------------------------------------===*/
3156 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
3158 LLVMMemoryBufferRef *OutMemBuf,
3159 char **OutMessage) {
3161 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
3162 if (std::error_code EC = MBOrErr.getError()) {
3163 *OutMessage = strdup(EC.message().c_str());
3166 *OutMemBuf = wrap(MBOrErr.get().release());
3170 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
3171 char **OutMessage) {
3172 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
3173 if (std::error_code EC = MBOrErr.getError()) {
3174 *OutMessage = strdup(EC.message().c_str());
3177 *OutMemBuf = wrap(MBOrErr.get().release());
3181 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
3182 const char *InputData,
3183 size_t InputDataLength,
3184 const char *BufferName,
3185 LLVMBool RequiresNullTerminator) {
3187 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
3188 StringRef(BufferName),
3189 RequiresNullTerminator).release());
3192 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
3193 const char *InputData,
3194 size_t InputDataLength,
3195 const char *BufferName) {
3198 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
3199 StringRef(BufferName)).release());
3202 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
3203 return unwrap(MemBuf)->getBufferStart();
3206 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
3207 return unwrap(MemBuf)->getBufferSize();
3210 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
3211 delete unwrap(MemBuf);
3214 /*===-- Pass Registry -----------------------------------------------------===*/
3216 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
3217 return wrap(PassRegistry::getPassRegistry());
3220 /*===-- Pass Manager ------------------------------------------------------===*/
3222 LLVMPassManagerRef LLVMCreatePassManager() {
3223 return wrap(new legacy::PassManager());
3226 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
3227 return wrap(new legacy::FunctionPassManager(unwrap(M)));
3230 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
3231 return LLVMCreateFunctionPassManagerForModule(
3232 reinterpret_cast<LLVMModuleRef>(P));
3235 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
3236 return unwrap<legacy::PassManager>(PM)->run(*unwrap(M));
3239 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
3240 return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization();
3243 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
3244 return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
3247 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
3248 return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization();
3251 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
3255 /*===-- Threading ------------------------------------------------------===*/
3257 LLVMBool LLVMStartMultithreaded() {
3258 return LLVMIsMultithreaded();
3261 void LLVMStopMultithreaded() {
3264 LLVMBool LLVMIsMultithreaded() {
3265 return llvm_is_multithreaded();