1 //===-- Core.cpp ----------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the common infrastructure (including the C bindings)
11 // for libLLVMCore.a, which implements the LLVM intermediate representation.
13 //===----------------------------------------------------------------------===//
15 #include "llvm-c/Core.h"
16 #include "llvm/ADT/StringSwitch.h"
17 #include "llvm/IR/Attributes.h"
18 #include "llvm/IR/CallSite.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/DiagnosticInfo.h"
22 #include "llvm/IR/DiagnosticPrinter.h"
23 #include "llvm/IR/GlobalAlias.h"
24 #include "llvm/IR/GlobalVariable.h"
25 #include "llvm/IR/IRBuilder.h"
26 #include "llvm/IR/InlineAsm.h"
27 #include "llvm/IR/IntrinsicInst.h"
28 #include "llvm/IR/LLVMContext.h"
29 #include "llvm/IR/LegacyPassManager.h"
30 #include "llvm/IR/Module.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/FileSystem.h"
34 #include "llvm/Support/ManagedStatic.h"
35 #include "llvm/Support/MemoryBuffer.h"
36 #include "llvm/Support/Threading.h"
37 #include "llvm/Support/raw_ostream.h"
41 #include <system_error>
45 #define DEBUG_TYPE "ir"
47 void llvm::initializeCore(PassRegistry &Registry) {
48 initializeDominatorTreeWrapperPassPass(Registry);
49 initializePrintModulePassWrapperPass(Registry);
50 initializePrintFunctionPassWrapperPass(Registry);
51 initializePrintBasicBlockPassPass(Registry);
52 initializeSafepointIRVerifierPass(Registry);
53 initializeVerifierLegacyPassPass(Registry);
56 void LLVMInitializeCore(LLVMPassRegistryRef R) {
57 initializeCore(*unwrap(R));
64 /*===-- Error handling ----------------------------------------------------===*/
66 char *LLVMCreateMessage(const char *Message) {
67 return strdup(Message);
70 void LLVMDisposeMessage(char *Message) {
75 /*===-- Operations on contexts --------------------------------------------===*/
77 static ManagedStatic<LLVMContext> GlobalContext;
79 LLVMContextRef LLVMContextCreate() {
80 return wrap(new LLVMContext());
83 LLVMContextRef LLVMGetGlobalContext() { return wrap(&*GlobalContext); }
85 void LLVMContextSetDiagnosticHandler(LLVMContextRef C,
86 LLVMDiagnosticHandler Handler,
87 void *DiagnosticContext) {
88 unwrap(C)->setDiagnosticHandlerCallBack(
89 LLVM_EXTENSION reinterpret_cast<DiagnosticHandler::DiagnosticHandlerTy>(
94 LLVMDiagnosticHandler LLVMContextGetDiagnosticHandler(LLVMContextRef C) {
95 return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>(
96 unwrap(C)->getDiagnosticHandlerCallBack());
99 void *LLVMContextGetDiagnosticContext(LLVMContextRef C) {
100 return unwrap(C)->getDiagnosticContext();
103 void LLVMContextSetYieldCallback(LLVMContextRef C, LLVMYieldCallback Callback,
104 void *OpaqueHandle) {
106 LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
107 unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
110 void LLVMContextDispose(LLVMContextRef C) {
114 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char *Name,
116 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
119 unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) {
120 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
123 #define GET_ATTR_KIND_FROM_NAME
124 #include "AttributesCompatFunc.inc"
126 unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) {
127 return getAttrKindFromName(StringRef(Name, SLen));
130 unsigned LLVMGetLastEnumAttributeKind(void) {
131 return Attribute::AttrKind::EndAttrKinds;
134 LLVMAttributeRef LLVMCreateEnumAttribute(LLVMContextRef C, unsigned KindID,
136 return wrap(Attribute::get(*unwrap(C), (Attribute::AttrKind)KindID, Val));
139 unsigned LLVMGetEnumAttributeKind(LLVMAttributeRef A) {
140 return unwrap(A).getKindAsEnum();
143 uint64_t LLVMGetEnumAttributeValue(LLVMAttributeRef A) {
144 auto Attr = unwrap(A);
145 if (Attr.isEnumAttribute())
147 return Attr.getValueAsInt();
150 LLVMAttributeRef LLVMCreateStringAttribute(LLVMContextRef C,
151 const char *K, unsigned KLength,
152 const char *V, unsigned VLength) {
153 return wrap(Attribute::get(*unwrap(C), StringRef(K, KLength),
154 StringRef(V, VLength)));
157 const char *LLVMGetStringAttributeKind(LLVMAttributeRef A,
159 auto S = unwrap(A).getKindAsString();
164 const char *LLVMGetStringAttributeValue(LLVMAttributeRef A,
166 auto S = unwrap(A).getValueAsString();
171 LLVMBool LLVMIsEnumAttribute(LLVMAttributeRef A) {
172 auto Attr = unwrap(A);
173 return Attr.isEnumAttribute() || Attr.isIntAttribute();
176 LLVMBool LLVMIsStringAttribute(LLVMAttributeRef A) {
177 return unwrap(A).isStringAttribute();
180 char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) {
181 std::string MsgStorage;
182 raw_string_ostream Stream(MsgStorage);
183 DiagnosticPrinterRawOStream DP(Stream);
185 unwrap(DI)->print(DP);
188 return LLVMCreateMessage(MsgStorage.c_str());
191 LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI) {
192 LLVMDiagnosticSeverity severity;
194 switch(unwrap(DI)->getSeverity()) {
196 severity = LLVMDSError;
199 severity = LLVMDSWarning;
202 severity = LLVMDSRemark;
205 severity = LLVMDSNote;
212 /*===-- Operations on modules ---------------------------------------------===*/
214 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
215 return wrap(new Module(ModuleID, *GlobalContext));
218 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
220 return wrap(new Module(ModuleID, *unwrap(C)));
223 void LLVMDisposeModule(LLVMModuleRef M) {
227 const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) {
228 auto &Str = unwrap(M)->getModuleIdentifier();
233 void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) {
234 unwrap(M)->setModuleIdentifier(StringRef(Ident, Len));
237 const char *LLVMGetSourceFileName(LLVMModuleRef M, size_t *Len) {
238 auto &Str = unwrap(M)->getSourceFileName();
243 void LLVMSetSourceFileName(LLVMModuleRef M, const char *Name, size_t Len) {
244 unwrap(M)->setSourceFileName(StringRef(Name, Len));
247 /*--.. Data layout .........................................................--*/
248 const char *LLVMGetDataLayoutStr(LLVMModuleRef M) {
249 return unwrap(M)->getDataLayoutStr().c_str();
252 const char *LLVMGetDataLayout(LLVMModuleRef M) {
253 return LLVMGetDataLayoutStr(M);
256 void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
257 unwrap(M)->setDataLayout(DataLayoutStr);
260 /*--.. Target triple .......................................................--*/
261 const char * LLVMGetTarget(LLVMModuleRef M) {
262 return unwrap(M)->getTargetTriple().c_str();
265 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
266 unwrap(M)->setTargetTriple(Triple);
269 /*--.. Module flags ........................................................--*/
270 struct LLVMOpaqueModuleFlagEntry {
271 LLVMModuleFlagBehavior Behavior;
274 LLVMMetadataRef Metadata;
277 static Module::ModFlagBehavior
278 map_to_llvmModFlagBehavior(LLVMModuleFlagBehavior Behavior) {
280 case LLVMModuleFlagBehaviorError:
281 return Module::ModFlagBehavior::Error;
282 case LLVMModuleFlagBehaviorWarning:
283 return Module::ModFlagBehavior::Warning;
284 case LLVMModuleFlagBehaviorRequire:
285 return Module::ModFlagBehavior::Require;
286 case LLVMModuleFlagBehaviorOverride:
287 return Module::ModFlagBehavior::Override;
288 case LLVMModuleFlagBehaviorAppend:
289 return Module::ModFlagBehavior::Append;
290 case LLVMModuleFlagBehaviorAppendUnique:
291 return Module::ModFlagBehavior::AppendUnique;
293 llvm_unreachable("Unknown LLVMModuleFlagBehavior");
296 static LLVMModuleFlagBehavior
297 map_from_llvmModFlagBehavior(Module::ModFlagBehavior Behavior) {
299 case Module::ModFlagBehavior::Error:
300 return LLVMModuleFlagBehaviorError;
301 case Module::ModFlagBehavior::Warning:
302 return LLVMModuleFlagBehaviorWarning;
303 case Module::ModFlagBehavior::Require:
304 return LLVMModuleFlagBehaviorRequire;
305 case Module::ModFlagBehavior::Override:
306 return LLVMModuleFlagBehaviorOverride;
307 case Module::ModFlagBehavior::Append:
308 return LLVMModuleFlagBehaviorAppend;
309 case Module::ModFlagBehavior::AppendUnique:
310 return LLVMModuleFlagBehaviorAppendUnique;
312 llvm_unreachable("Unhandled Flag Behavior");
316 LLVMModuleFlagEntry *LLVMCopyModuleFlagsMetadata(LLVMModuleRef M, size_t *Len) {
317 SmallVector<Module::ModuleFlagEntry, 8> MFEs;
318 unwrap(M)->getModuleFlagsMetadata(MFEs);
320 LLVMOpaqueModuleFlagEntry *Result = static_cast<LLVMOpaqueModuleFlagEntry *>(
321 safe_malloc(MFEs.size() * sizeof(LLVMOpaqueModuleFlagEntry)));
322 for (unsigned i = 0; i < MFEs.size(); ++i) {
323 const auto &ModuleFlag = MFEs[i];
324 Result[i].Behavior = map_from_llvmModFlagBehavior(ModuleFlag.Behavior);
325 Result[i].Key = ModuleFlag.Key->getString().data();
326 Result[i].KeyLen = ModuleFlag.Key->getString().size();
327 Result[i].Metadata = wrap(ModuleFlag.Val);
333 void LLVMDisposeModuleFlagsMetadata(LLVMModuleFlagEntry *Entries) {
337 LLVMModuleFlagBehavior
338 LLVMModuleFlagEntriesGetFlagBehavior(LLVMModuleFlagEntry *Entries,
340 LLVMOpaqueModuleFlagEntry MFE =
341 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
345 const char *LLVMModuleFlagEntriesGetKey(LLVMModuleFlagEntry *Entries,
346 unsigned Index, size_t *Len) {
347 LLVMOpaqueModuleFlagEntry MFE =
348 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
353 LLVMMetadataRef LLVMModuleFlagEntriesGetMetadata(LLVMModuleFlagEntry *Entries,
355 LLVMOpaqueModuleFlagEntry MFE =
356 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
360 LLVMMetadataRef LLVMGetModuleFlag(LLVMModuleRef M,
361 const char *Key, size_t KeyLen) {
362 return wrap(unwrap(M)->getModuleFlag({Key, KeyLen}));
365 void LLVMAddModuleFlag(LLVMModuleRef M, LLVMModuleFlagBehavior Behavior,
366 const char *Key, size_t KeyLen,
367 LLVMMetadataRef Val) {
368 unwrap(M)->addModuleFlag(map_to_llvmModFlagBehavior(Behavior),
369 {Key, KeyLen}, unwrap(Val));
372 /*--.. Printing modules ....................................................--*/
374 void LLVMDumpModule(LLVMModuleRef M) {
375 unwrap(M)->print(errs(), nullptr,
376 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
379 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
380 char **ErrorMessage) {
382 raw_fd_ostream dest(Filename, EC, sys::fs::F_Text);
384 *ErrorMessage = strdup(EC.message().c_str());
388 unwrap(M)->print(dest, nullptr);
392 if (dest.has_error()) {
393 std::string E = "Error printing to file: " + dest.error().message();
394 *ErrorMessage = strdup(E.c_str());
401 char *LLVMPrintModuleToString(LLVMModuleRef M) {
403 raw_string_ostream os(buf);
405 unwrap(M)->print(os, nullptr);
408 return strdup(buf.c_str());
411 /*--.. Operations on inline assembler ......................................--*/
412 void LLVMSetModuleInlineAsm2(LLVMModuleRef M, const char *Asm, size_t Len) {
413 unwrap(M)->setModuleInlineAsm(StringRef(Asm, Len));
416 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
417 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
420 void LLVMAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm, size_t Len) {
421 unwrap(M)->appendModuleInlineAsm(StringRef(Asm, Len));
424 const char *LLVMGetModuleInlineAsm(LLVMModuleRef M, size_t *Len) {
425 auto &Str = unwrap(M)->getModuleInlineAsm();
430 LLVMValueRef LLVMGetInlineAsm(LLVMTypeRef Ty,
431 char *AsmString, size_t AsmStringSize,
432 char *Constraints, size_t ConstraintsSize,
433 LLVMBool HasSideEffects, LLVMBool IsAlignStack,
434 LLVMInlineAsmDialect Dialect) {
435 InlineAsm::AsmDialect AD;
437 case LLVMInlineAsmDialectATT:
438 AD = InlineAsm::AD_ATT;
440 case LLVMInlineAsmDialectIntel:
441 AD = InlineAsm::AD_Intel;
444 return wrap(InlineAsm::get(unwrap<FunctionType>(Ty),
445 StringRef(AsmString, AsmStringSize),
446 StringRef(Constraints, ConstraintsSize),
447 HasSideEffects, IsAlignStack, AD));
451 /*--.. Operations on module contexts ......................................--*/
452 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
453 return wrap(&unwrap(M)->getContext());
457 /*===-- Operations on types -----------------------------------------------===*/
459 /*--.. Operations on all types (mostly) ....................................--*/
461 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
462 switch (unwrap(Ty)->getTypeID()) {
464 return LLVMVoidTypeKind;
466 return LLVMHalfTypeKind;
467 case Type::FloatTyID:
468 return LLVMFloatTypeKind;
469 case Type::DoubleTyID:
470 return LLVMDoubleTypeKind;
471 case Type::X86_FP80TyID:
472 return LLVMX86_FP80TypeKind;
473 case Type::FP128TyID:
474 return LLVMFP128TypeKind;
475 case Type::PPC_FP128TyID:
476 return LLVMPPC_FP128TypeKind;
477 case Type::LabelTyID:
478 return LLVMLabelTypeKind;
479 case Type::MetadataTyID:
480 return LLVMMetadataTypeKind;
481 case Type::IntegerTyID:
482 return LLVMIntegerTypeKind;
483 case Type::FunctionTyID:
484 return LLVMFunctionTypeKind;
485 case Type::StructTyID:
486 return LLVMStructTypeKind;
487 case Type::ArrayTyID:
488 return LLVMArrayTypeKind;
489 case Type::PointerTyID:
490 return LLVMPointerTypeKind;
491 case Type::VectorTyID:
492 return LLVMVectorTypeKind;
493 case Type::X86_MMXTyID:
494 return LLVMX86_MMXTypeKind;
495 case Type::TokenTyID:
496 return LLVMTokenTypeKind;
498 llvm_unreachable("Unhandled TypeID.");
501 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
503 return unwrap(Ty)->isSized();
506 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
507 return wrap(&unwrap(Ty)->getContext());
510 void LLVMDumpType(LLVMTypeRef Ty) {
511 return unwrap(Ty)->print(errs(), /*IsForDebug=*/true);
514 char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
516 raw_string_ostream os(buf);
519 unwrap(Ty)->print(os);
521 os << "Printing <null> Type";
525 return strdup(buf.c_str());
528 /*--.. Operations on integer types .........................................--*/
530 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
531 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
533 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
534 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
536 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
537 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
539 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
540 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
542 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
543 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
545 LLVMTypeRef LLVMInt128TypeInContext(LLVMContextRef C) {
546 return (LLVMTypeRef) Type::getInt128Ty(*unwrap(C));
548 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
549 return wrap(IntegerType::get(*unwrap(C), NumBits));
552 LLVMTypeRef LLVMInt1Type(void) {
553 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
555 LLVMTypeRef LLVMInt8Type(void) {
556 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
558 LLVMTypeRef LLVMInt16Type(void) {
559 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
561 LLVMTypeRef LLVMInt32Type(void) {
562 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
564 LLVMTypeRef LLVMInt64Type(void) {
565 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
567 LLVMTypeRef LLVMInt128Type(void) {
568 return LLVMInt128TypeInContext(LLVMGetGlobalContext());
570 LLVMTypeRef LLVMIntType(unsigned NumBits) {
571 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
574 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
575 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
578 /*--.. Operations on real types ............................................--*/
580 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
581 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
583 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
584 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
586 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
587 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
589 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
590 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
592 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
593 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
595 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
596 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
598 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
599 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
602 LLVMTypeRef LLVMHalfType(void) {
603 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
605 LLVMTypeRef LLVMFloatType(void) {
606 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
608 LLVMTypeRef LLVMDoubleType(void) {
609 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
611 LLVMTypeRef LLVMX86FP80Type(void) {
612 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
614 LLVMTypeRef LLVMFP128Type(void) {
615 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
617 LLVMTypeRef LLVMPPCFP128Type(void) {
618 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
620 LLVMTypeRef LLVMX86MMXType(void) {
621 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
624 /*--.. Operations on function types ........................................--*/
626 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
627 LLVMTypeRef *ParamTypes, unsigned ParamCount,
629 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
630 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
633 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
634 return unwrap<FunctionType>(FunctionTy)->isVarArg();
637 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
638 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
641 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
642 return unwrap<FunctionType>(FunctionTy)->getNumParams();
645 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
646 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
647 for (FunctionType::param_iterator I = Ty->param_begin(),
648 E = Ty->param_end(); I != E; ++I)
652 /*--.. Operations on struct types ..........................................--*/
654 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
655 unsigned ElementCount, LLVMBool Packed) {
656 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
657 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
660 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
661 unsigned ElementCount, LLVMBool Packed) {
662 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
663 ElementCount, Packed);
666 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
668 return wrap(StructType::create(*unwrap(C), Name));
671 const char *LLVMGetStructName(LLVMTypeRef Ty)
673 StructType *Type = unwrap<StructType>(Ty);
674 if (!Type->hasName())
676 return Type->getName().data();
679 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
680 unsigned ElementCount, LLVMBool Packed) {
681 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
682 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
685 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
686 return unwrap<StructType>(StructTy)->getNumElements();
689 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
690 StructType *Ty = unwrap<StructType>(StructTy);
691 for (StructType::element_iterator I = Ty->element_begin(),
692 E = Ty->element_end(); I != E; ++I)
696 LLVMTypeRef LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy, unsigned i) {
697 StructType *Ty = unwrap<StructType>(StructTy);
698 return wrap(Ty->getTypeAtIndex(i));
701 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
702 return unwrap<StructType>(StructTy)->isPacked();
705 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
706 return unwrap<StructType>(StructTy)->isOpaque();
709 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
710 return wrap(unwrap(M)->getTypeByName(Name));
713 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
715 void LLVMGetSubtypes(LLVMTypeRef Tp, LLVMTypeRef *Arr) {
717 for (auto *T : unwrap(Tp)->subtypes()) {
723 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
724 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
727 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
728 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
731 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
732 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
735 LLVMTypeRef LLVMGetElementType(LLVMTypeRef WrappedTy) {
736 auto *Ty = unwrap<Type>(WrappedTy);
737 if (auto *PTy = dyn_cast<PointerType>(Ty))
738 return wrap(PTy->getElementType());
739 return wrap(cast<SequentialType>(Ty)->getElementType());
742 unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp) {
743 return unwrap(Tp)->getNumContainedTypes();
746 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
747 return unwrap<ArrayType>(ArrayTy)->getNumElements();
750 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
751 return unwrap<PointerType>(PointerTy)->getAddressSpace();
754 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
755 return unwrap<VectorType>(VectorTy)->getNumElements();
758 /*--.. Operations on other types ...........................................--*/
760 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
761 return wrap(Type::getVoidTy(*unwrap(C)));
763 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
764 return wrap(Type::getLabelTy(*unwrap(C)));
766 LLVMTypeRef LLVMTokenTypeInContext(LLVMContextRef C) {
767 return wrap(Type::getTokenTy(*unwrap(C)));
769 LLVMTypeRef LLVMMetadataTypeInContext(LLVMContextRef C) {
770 return wrap(Type::getMetadataTy(*unwrap(C)));
773 LLVMTypeRef LLVMVoidType(void) {
774 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
776 LLVMTypeRef LLVMLabelType(void) {
777 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
780 /*===-- Operations on values ----------------------------------------------===*/
782 /*--.. Operations on all values ............................................--*/
784 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
785 return wrap(unwrap(Val)->getType());
788 LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) {
789 switch(unwrap(Val)->getValueID()) {
790 #define HANDLE_VALUE(Name) \
791 case Value::Name##Val: \
792 return LLVM##Name##ValueKind;
793 #include "llvm/IR/Value.def"
795 return LLVMInstructionValueKind;
799 const char *LLVMGetValueName2(LLVMValueRef Val, size_t *Length) {
800 auto *V = unwrap(Val);
801 *Length = V->getName().size();
802 return V->getName().data();
805 void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen) {
806 unwrap(Val)->setName(StringRef(Name, NameLen));
809 const char *LLVMGetValueName(LLVMValueRef Val) {
810 return unwrap(Val)->getName().data();
813 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
814 unwrap(Val)->setName(Name);
817 void LLVMDumpValue(LLVMValueRef Val) {
818 unwrap(Val)->print(errs(), /*IsForDebug=*/true);
821 char* LLVMPrintValueToString(LLVMValueRef Val) {
823 raw_string_ostream os(buf);
826 unwrap(Val)->print(os);
828 os << "Printing <null> Value";
832 return strdup(buf.c_str());
835 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
836 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
839 int LLVMHasMetadata(LLVMValueRef Inst) {
840 return unwrap<Instruction>(Inst)->hasMetadata();
843 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
844 auto *I = unwrap<Instruction>(Inst);
845 assert(I && "Expected instruction");
846 if (auto *MD = I->getMetadata(KindID))
847 return wrap(MetadataAsValue::get(I->getContext(), MD));
851 // MetadataAsValue uses a canonical format which strips the actual MDNode for
852 // MDNode with just a single constant value, storing just a ConstantAsMetadata
853 // This undoes this canonicalization, reconstructing the MDNode.
854 static MDNode *extractMDNode(MetadataAsValue *MAV) {
855 Metadata *MD = MAV->getMetadata();
856 assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
857 "Expected a metadata node or a canonicalized constant");
859 if (MDNode *N = dyn_cast<MDNode>(MD))
862 return MDNode::get(MAV->getContext(), MD);
865 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
866 MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
868 unwrap<Instruction>(Inst)->setMetadata(KindID, N);
871 /*--.. Conversion functions ................................................--*/
873 #define LLVM_DEFINE_VALUE_CAST(name) \
874 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
875 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
878 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
880 LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) {
881 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
882 if (isa<MDNode>(MD->getMetadata()) ||
883 isa<ValueAsMetadata>(MD->getMetadata()))
888 LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) {
889 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
890 if (isa<MDString>(MD->getMetadata()))
895 /*--.. Operations on Uses ..................................................--*/
896 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
897 Value *V = unwrap(Val);
898 Value::use_iterator I = V->use_begin();
899 if (I == V->use_end())
904 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
905 Use *Next = unwrap(U)->getNext();
911 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
912 return wrap(unwrap(U)->getUser());
915 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
916 return wrap(unwrap(U)->get());
919 /*--.. Operations on Users .................................................--*/
921 static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N,
923 Metadata *Op = N->getOperand(Index);
926 if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
927 return wrap(C->getValue());
928 return wrap(MetadataAsValue::get(Context, Op));
931 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
932 Value *V = unwrap(Val);
933 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
934 if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
935 assert(Index == 0 && "Function-local metadata can only have one operand");
936 return wrap(L->getValue());
938 return getMDNodeOperandImpl(V->getContext(),
939 cast<MDNode>(MD->getMetadata()), Index);
942 return wrap(cast<User>(V)->getOperand(Index));
945 LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) {
946 Value *V = unwrap(Val);
947 return wrap(&cast<User>(V)->getOperandUse(Index));
950 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
951 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
954 int LLVMGetNumOperands(LLVMValueRef Val) {
955 Value *V = unwrap(Val);
956 if (isa<MetadataAsValue>(V))
957 return LLVMGetMDNodeNumOperands(Val);
959 return cast<User>(V)->getNumOperands();
962 /*--.. Operations on constants of any type .................................--*/
964 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
965 return wrap(Constant::getNullValue(unwrap(Ty)));
968 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
969 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
972 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
973 return wrap(UndefValue::get(unwrap(Ty)));
976 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
977 return isa<Constant>(unwrap(Ty));
980 LLVMBool LLVMIsNull(LLVMValueRef Val) {
981 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
982 return C->isNullValue();
986 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
987 return isa<UndefValue>(unwrap(Val));
990 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
991 return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
994 /*--.. Operations on metadata nodes ........................................--*/
996 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
998 LLVMContext &Context = *unwrap(C);
999 return wrap(MetadataAsValue::get(
1000 Context, MDString::get(Context, StringRef(Str, SLen))));
1003 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
1004 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
1007 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
1009 LLVMContext &Context = *unwrap(C);
1010 SmallVector<Metadata *, 8> MDs;
1011 for (auto *OV : makeArrayRef(Vals, Count)) {
1012 Value *V = unwrap(OV);
1016 else if (auto *C = dyn_cast<Constant>(V))
1017 MD = ConstantAsMetadata::get(C);
1018 else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
1019 MD = MDV->getMetadata();
1020 assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
1021 "outside of direct argument to call");
1023 // This is function-local metadata. Pretend to make an MDNode.
1024 assert(Count == 1 &&
1025 "Expected only one operand to function-local metadata");
1026 return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V)));
1031 return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs)));
1034 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
1035 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
1038 LLVMValueRef LLVMMetadataAsValue(LLVMContextRef C, LLVMMetadataRef MD) {
1039 return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
1042 LLVMMetadataRef LLVMValueAsMetadata(LLVMValueRef Val) {
1043 auto *V = unwrap(Val);
1044 if (auto *C = dyn_cast<Constant>(V))
1045 return wrap(ConstantAsMetadata::get(C));
1046 if (auto *MAV = dyn_cast<MetadataAsValue>(V))
1047 return wrap(MAV->getMetadata());
1048 return wrap(ValueAsMetadata::get(V));
1051 const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
1052 if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
1053 if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
1054 *Length = S->getString().size();
1055 return S->getString().data();
1061 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) {
1062 auto *MD = cast<MetadataAsValue>(unwrap(V));
1063 if (isa<ValueAsMetadata>(MD->getMetadata()))
1065 return cast<MDNode>(MD->getMetadata())->getNumOperands();
1068 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) {
1069 auto *MD = cast<MetadataAsValue>(unwrap(V));
1070 if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1071 *Dest = wrap(MDV->getValue());
1074 const auto *N = cast<MDNode>(MD->getMetadata());
1075 const unsigned numOperands = N->getNumOperands();
1076 LLVMContext &Context = unwrap(V)->getContext();
1077 for (unsigned i = 0; i < numOperands; i++)
1078 Dest[i] = getMDNodeOperandImpl(Context, N, i);
1081 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char *Name) {
1082 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
1083 return N->getNumOperands();
1088 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char *Name,
1089 LLVMValueRef *Dest) {
1090 NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
1093 LLVMContext &Context = unwrap(M)->getContext();
1094 for (unsigned i=0;i<N->getNumOperands();i++)
1095 Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
1098 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char *Name,
1100 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
1105 N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
1108 /*--.. Operations on scalar constants ......................................--*/
1110 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
1111 LLVMBool SignExtend) {
1112 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
1115 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
1117 const uint64_t Words[]) {
1118 IntegerType *Ty = unwrap<IntegerType>(IntTy);
1119 return wrap(ConstantInt::get(Ty->getContext(),
1120 APInt(Ty->getBitWidth(),
1121 makeArrayRef(Words, NumWords))));
1124 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
1126 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
1130 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
1131 unsigned SLen, uint8_t Radix) {
1132 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
1136 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
1137 return wrap(ConstantFP::get(unwrap(RealTy), N));
1140 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
1141 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
1144 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
1146 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
1149 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
1150 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
1153 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
1154 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
1157 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
1158 ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
1159 Type *Ty = cFP->getType();
1161 if (Ty->isFloatTy()) {
1163 return cFP->getValueAPF().convertToFloat();
1166 if (Ty->isDoubleTy()) {
1168 return cFP->getValueAPF().convertToDouble();
1172 APFloat APF = cFP->getValueAPF();
1173 APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo);
1174 *LosesInfo = APFLosesInfo;
1175 return APF.convertToDouble();
1178 /*--.. Operations on composite constants ...................................--*/
1180 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
1182 LLVMBool DontNullTerminate) {
1183 /* Inverted the sense of AddNull because ', 0)' is a
1184 better mnemonic for null termination than ', 1)'. */
1185 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
1186 DontNullTerminate == 0));
1189 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
1190 LLVMBool DontNullTerminate) {
1191 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
1195 LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef C, unsigned idx) {
1196 return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
1199 LLVMBool LLVMIsConstantString(LLVMValueRef C) {
1200 return unwrap<ConstantDataSequential>(C)->isString();
1203 const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1204 StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1205 *Length = Str.size();
1209 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
1210 LLVMValueRef *ConstantVals, unsigned Length) {
1211 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1212 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1215 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
1216 LLVMValueRef *ConstantVals,
1217 unsigned Count, LLVMBool Packed) {
1218 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1219 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
1223 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
1225 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1229 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
1230 LLVMValueRef *ConstantVals,
1232 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1233 StructType *Ty = cast<StructType>(unwrap(StructTy));
1235 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
1238 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
1239 return wrap(ConstantVector::get(makeArrayRef(
1240 unwrap<Constant>(ScalarConstantVals, Size), Size)));
1243 /*-- Opcode mapping */
1245 static LLVMOpcode map_to_llvmopcode(int opcode)
1248 default: llvm_unreachable("Unhandled Opcode.");
1249 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
1250 #include "llvm/IR/Instruction.def"
1255 static int map_from_llvmopcode(LLVMOpcode code)
1258 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
1259 #include "llvm/IR/Instruction.def"
1262 llvm_unreachable("Unhandled Opcode.");
1265 /*--.. Constant expressions ................................................--*/
1267 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
1268 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
1271 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
1272 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
1275 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
1276 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
1279 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
1280 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1283 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
1284 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
1287 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
1288 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
1292 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
1293 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
1296 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
1297 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
1300 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1301 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
1302 unwrap<Constant>(RHSConstant)));
1305 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
1306 LLVMValueRef RHSConstant) {
1307 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
1308 unwrap<Constant>(RHSConstant)));
1311 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
1312 LLVMValueRef RHSConstant) {
1313 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
1314 unwrap<Constant>(RHSConstant)));
1317 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1318 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
1319 unwrap<Constant>(RHSConstant)));
1322 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1323 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
1324 unwrap<Constant>(RHSConstant)));
1327 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
1328 LLVMValueRef RHSConstant) {
1329 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
1330 unwrap<Constant>(RHSConstant)));
1333 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
1334 LLVMValueRef RHSConstant) {
1335 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
1336 unwrap<Constant>(RHSConstant)));
1339 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1340 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
1341 unwrap<Constant>(RHSConstant)));
1344 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1345 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
1346 unwrap<Constant>(RHSConstant)));
1349 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
1350 LLVMValueRef RHSConstant) {
1351 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
1352 unwrap<Constant>(RHSConstant)));
1355 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
1356 LLVMValueRef RHSConstant) {
1357 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
1358 unwrap<Constant>(RHSConstant)));
1361 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1362 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
1363 unwrap<Constant>(RHSConstant)));
1366 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1367 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
1368 unwrap<Constant>(RHSConstant)));
1371 LLVMValueRef LLVMConstExactUDiv(LLVMValueRef LHSConstant,
1372 LLVMValueRef RHSConstant) {
1373 return wrap(ConstantExpr::getExactUDiv(unwrap<Constant>(LHSConstant),
1374 unwrap<Constant>(RHSConstant)));
1377 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1378 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
1379 unwrap<Constant>(RHSConstant)));
1382 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
1383 LLVMValueRef RHSConstant) {
1384 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
1385 unwrap<Constant>(RHSConstant)));
1388 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1389 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
1390 unwrap<Constant>(RHSConstant)));
1393 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1394 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
1395 unwrap<Constant>(RHSConstant)));
1398 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1399 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
1400 unwrap<Constant>(RHSConstant)));
1403 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1404 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
1405 unwrap<Constant>(RHSConstant)));
1408 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1409 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
1410 unwrap<Constant>(RHSConstant)));
1413 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1414 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
1415 unwrap<Constant>(RHSConstant)));
1418 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1419 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
1420 unwrap<Constant>(RHSConstant)));
1423 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
1424 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1425 return wrap(ConstantExpr::getICmp(Predicate,
1426 unwrap<Constant>(LHSConstant),
1427 unwrap<Constant>(RHSConstant)));
1430 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
1431 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1432 return wrap(ConstantExpr::getFCmp(Predicate,
1433 unwrap<Constant>(LHSConstant),
1434 unwrap<Constant>(RHSConstant)));
1437 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1438 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1439 unwrap<Constant>(RHSConstant)));
1442 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1443 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
1444 unwrap<Constant>(RHSConstant)));
1447 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1448 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1449 unwrap<Constant>(RHSConstant)));
1452 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
1453 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1454 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1456 return wrap(ConstantExpr::getGetElementPtr(
1457 nullptr, unwrap<Constant>(ConstantVal), IdxList));
1460 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
1461 LLVMValueRef *ConstantIndices,
1462 unsigned NumIndices) {
1463 Constant* Val = unwrap<Constant>(ConstantVal);
1464 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1466 return wrap(ConstantExpr::getInBoundsGetElementPtr(nullptr, Val, IdxList));
1469 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1470 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1474 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1475 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1479 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1480 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1484 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1485 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1489 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1490 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1494 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1495 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1499 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1500 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1504 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1505 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1509 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1510 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1514 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1515 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1519 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1520 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1524 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1525 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1529 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1530 LLVMTypeRef ToType) {
1531 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1535 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1536 LLVMTypeRef ToType) {
1537 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1541 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1542 LLVMTypeRef ToType) {
1543 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1547 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1548 LLVMTypeRef ToType) {
1549 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1553 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1554 LLVMTypeRef ToType) {
1555 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1559 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1560 LLVMBool isSigned) {
1561 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1562 unwrap(ToType), isSigned));
1565 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1566 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1570 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1571 LLVMValueRef ConstantIfTrue,
1572 LLVMValueRef ConstantIfFalse) {
1573 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1574 unwrap<Constant>(ConstantIfTrue),
1575 unwrap<Constant>(ConstantIfFalse)));
1578 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1579 LLVMValueRef IndexConstant) {
1580 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1581 unwrap<Constant>(IndexConstant)));
1584 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1585 LLVMValueRef ElementValueConstant,
1586 LLVMValueRef IndexConstant) {
1587 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1588 unwrap<Constant>(ElementValueConstant),
1589 unwrap<Constant>(IndexConstant)));
1592 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1593 LLVMValueRef VectorBConstant,
1594 LLVMValueRef MaskConstant) {
1595 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1596 unwrap<Constant>(VectorBConstant),
1597 unwrap<Constant>(MaskConstant)));
1600 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1602 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1603 makeArrayRef(IdxList, NumIdx)));
1606 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1607 LLVMValueRef ElementValueConstant,
1608 unsigned *IdxList, unsigned NumIdx) {
1609 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1610 unwrap<Constant>(ElementValueConstant),
1611 makeArrayRef(IdxList, NumIdx)));
1614 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1615 const char *Constraints,
1616 LLVMBool HasSideEffects,
1617 LLVMBool IsAlignStack) {
1618 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1619 Constraints, HasSideEffects, IsAlignStack));
1622 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1623 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1626 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1628 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1629 return wrap(unwrap<GlobalValue>(Global)->getParent());
1632 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1633 return unwrap<GlobalValue>(Global)->isDeclaration();
1636 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1637 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1638 case GlobalValue::ExternalLinkage:
1639 return LLVMExternalLinkage;
1640 case GlobalValue::AvailableExternallyLinkage:
1641 return LLVMAvailableExternallyLinkage;
1642 case GlobalValue::LinkOnceAnyLinkage:
1643 return LLVMLinkOnceAnyLinkage;
1644 case GlobalValue::LinkOnceODRLinkage:
1645 return LLVMLinkOnceODRLinkage;
1646 case GlobalValue::WeakAnyLinkage:
1647 return LLVMWeakAnyLinkage;
1648 case GlobalValue::WeakODRLinkage:
1649 return LLVMWeakODRLinkage;
1650 case GlobalValue::AppendingLinkage:
1651 return LLVMAppendingLinkage;
1652 case GlobalValue::InternalLinkage:
1653 return LLVMInternalLinkage;
1654 case GlobalValue::PrivateLinkage:
1655 return LLVMPrivateLinkage;
1656 case GlobalValue::ExternalWeakLinkage:
1657 return LLVMExternalWeakLinkage;
1658 case GlobalValue::CommonLinkage:
1659 return LLVMCommonLinkage;
1662 llvm_unreachable("Invalid GlobalValue linkage!");
1665 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1666 GlobalValue *GV = unwrap<GlobalValue>(Global);
1669 case LLVMExternalLinkage:
1670 GV->setLinkage(GlobalValue::ExternalLinkage);
1672 case LLVMAvailableExternallyLinkage:
1673 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1675 case LLVMLinkOnceAnyLinkage:
1676 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1678 case LLVMLinkOnceODRLinkage:
1679 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1681 case LLVMLinkOnceODRAutoHideLinkage:
1683 errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1684 "longer supported.");
1686 case LLVMWeakAnyLinkage:
1687 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1689 case LLVMWeakODRLinkage:
1690 GV->setLinkage(GlobalValue::WeakODRLinkage);
1692 case LLVMAppendingLinkage:
1693 GV->setLinkage(GlobalValue::AppendingLinkage);
1695 case LLVMInternalLinkage:
1696 GV->setLinkage(GlobalValue::InternalLinkage);
1698 case LLVMPrivateLinkage:
1699 GV->setLinkage(GlobalValue::PrivateLinkage);
1701 case LLVMLinkerPrivateLinkage:
1702 GV->setLinkage(GlobalValue::PrivateLinkage);
1704 case LLVMLinkerPrivateWeakLinkage:
1705 GV->setLinkage(GlobalValue::PrivateLinkage);
1707 case LLVMDLLImportLinkage:
1710 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1712 case LLVMDLLExportLinkage:
1715 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1717 case LLVMExternalWeakLinkage:
1718 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1720 case LLVMGhostLinkage:
1722 errs() << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1724 case LLVMCommonLinkage:
1725 GV->setLinkage(GlobalValue::CommonLinkage);
1730 const char *LLVMGetSection(LLVMValueRef Global) {
1731 // Using .data() is safe because of how GlobalObject::setSection is
1733 return unwrap<GlobalValue>(Global)->getSection().data();
1736 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1737 unwrap<GlobalObject>(Global)->setSection(Section);
1740 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1741 return static_cast<LLVMVisibility>(
1742 unwrap<GlobalValue>(Global)->getVisibility());
1745 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1746 unwrap<GlobalValue>(Global)
1747 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1750 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
1751 return static_cast<LLVMDLLStorageClass>(
1752 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1755 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
1756 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1757 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1760 LLVMUnnamedAddr LLVMGetUnnamedAddress(LLVMValueRef Global) {
1761 switch (unwrap<GlobalValue>(Global)->getUnnamedAddr()) {
1762 case GlobalVariable::UnnamedAddr::None:
1763 return LLVMNoUnnamedAddr;
1764 case GlobalVariable::UnnamedAddr::Local:
1765 return LLVMLocalUnnamedAddr;
1766 case GlobalVariable::UnnamedAddr::Global:
1767 return LLVMGlobalUnnamedAddr;
1769 llvm_unreachable("Unknown UnnamedAddr kind!");
1772 void LLVMSetUnnamedAddress(LLVMValueRef Global, LLVMUnnamedAddr UnnamedAddr) {
1773 GlobalValue *GV = unwrap<GlobalValue>(Global);
1775 switch (UnnamedAddr) {
1776 case LLVMNoUnnamedAddr:
1777 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::None);
1778 case LLVMLocalUnnamedAddr:
1779 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Local);
1780 case LLVMGlobalUnnamedAddr:
1781 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Global);
1785 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
1786 return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
1789 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
1790 unwrap<GlobalValue>(Global)->setUnnamedAddr(
1791 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
1792 : GlobalValue::UnnamedAddr::None);
1795 /*--.. Operations on global variables, load and store instructions .........--*/
1797 unsigned LLVMGetAlignment(LLVMValueRef V) {
1798 Value *P = unwrap<Value>(V);
1799 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1800 return GV->getAlignment();
1801 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1802 return AI->getAlignment();
1803 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1804 return LI->getAlignment();
1805 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1806 return SI->getAlignment();
1809 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1812 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1813 Value *P = unwrap<Value>(V);
1814 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
1815 GV->setAlignment(Bytes);
1816 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1817 AI->setAlignment(Bytes);
1818 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1819 LI->setAlignment(Bytes);
1820 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1821 SI->setAlignment(Bytes);
1824 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1827 /*--.. Operations on global variables ......................................--*/
1829 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1830 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1831 GlobalValue::ExternalLinkage, nullptr, Name));
1834 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1836 unsigned AddressSpace) {
1837 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1838 GlobalValue::ExternalLinkage, nullptr, Name,
1839 nullptr, GlobalVariable::NotThreadLocal,
1843 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1844 return wrap(unwrap(M)->getNamedGlobal(Name));
1847 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1848 Module *Mod = unwrap(M);
1849 Module::global_iterator I = Mod->global_begin();
1850 if (I == Mod->global_end())
1855 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1856 Module *Mod = unwrap(M);
1857 Module::global_iterator I = Mod->global_end();
1858 if (I == Mod->global_begin())
1863 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1864 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1865 Module::global_iterator I(GV);
1866 if (++I == GV->getParent()->global_end())
1871 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1872 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1873 Module::global_iterator I(GV);
1874 if (I == GV->getParent()->global_begin())
1879 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1880 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1883 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1884 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1885 if ( !GV->hasInitializer() )
1887 return wrap(GV->getInitializer());
1890 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1891 unwrap<GlobalVariable>(GlobalVar)
1892 ->setInitializer(unwrap<Constant>(ConstantVal));
1895 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1896 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1899 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1900 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1903 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1904 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1907 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1908 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1911 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1912 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1913 case GlobalVariable::NotThreadLocal:
1914 return LLVMNotThreadLocal;
1915 case GlobalVariable::GeneralDynamicTLSModel:
1916 return LLVMGeneralDynamicTLSModel;
1917 case GlobalVariable::LocalDynamicTLSModel:
1918 return LLVMLocalDynamicTLSModel;
1919 case GlobalVariable::InitialExecTLSModel:
1920 return LLVMInitialExecTLSModel;
1921 case GlobalVariable::LocalExecTLSModel:
1922 return LLVMLocalExecTLSModel;
1925 llvm_unreachable("Invalid GlobalVariable thread local mode");
1928 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1929 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1932 case LLVMNotThreadLocal:
1933 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1935 case LLVMGeneralDynamicTLSModel:
1936 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1938 case LLVMLocalDynamicTLSModel:
1939 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1941 case LLVMInitialExecTLSModel:
1942 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1944 case LLVMLocalExecTLSModel:
1945 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1950 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1951 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1954 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1955 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1958 /*--.. Operations on aliases ......................................--*/
1960 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1962 auto *PTy = cast<PointerType>(unwrap(Ty));
1963 return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
1964 GlobalValue::ExternalLinkage, Name,
1965 unwrap<Constant>(Aliasee), unwrap(M)));
1968 LLVMValueRef LLVMGetNamedGlobalAlias(LLVMModuleRef M,
1969 const char *Name, size_t NameLen) {
1970 return wrap(unwrap(M)->getNamedAlias(Name));
1973 LLVMValueRef LLVMGetFirstGlobalAlias(LLVMModuleRef M) {
1974 Module *Mod = unwrap(M);
1975 Module::alias_iterator I = Mod->alias_begin();
1976 if (I == Mod->alias_end())
1981 LLVMValueRef LLVMGetLastGlobalAlias(LLVMModuleRef M) {
1982 Module *Mod = unwrap(M);
1983 Module::alias_iterator I = Mod->alias_end();
1984 if (I == Mod->alias_begin())
1989 LLVMValueRef LLVMGetNextGlobalAlias(LLVMValueRef GA) {
1990 GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
1991 Module::alias_iterator I(Alias);
1992 if (++I == Alias->getParent()->alias_end())
1997 LLVMValueRef LLVMGetPreviousGlobalAlias(LLVMValueRef GA) {
1998 GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
1999 Module::alias_iterator I(Alias);
2000 if (I == Alias->getParent()->alias_begin())
2005 LLVMValueRef LLVMAliasGetAliasee(LLVMValueRef Alias) {
2006 return wrap(unwrap<GlobalAlias>(Alias)->getAliasee());
2009 void LLVMAliasSetAliasee(LLVMValueRef Alias, LLVMValueRef Aliasee) {
2010 unwrap<GlobalAlias>(Alias)->setAliasee(unwrap<Constant>(Aliasee));
2013 /*--.. Operations on functions .............................................--*/
2015 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
2016 LLVMTypeRef FunctionTy) {
2017 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
2018 GlobalValue::ExternalLinkage, Name, unwrap(M)));
2021 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
2022 return wrap(unwrap(M)->getFunction(Name));
2025 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
2026 Module *Mod = unwrap(M);
2027 Module::iterator I = Mod->begin();
2028 if (I == Mod->end())
2033 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
2034 Module *Mod = unwrap(M);
2035 Module::iterator I = Mod->end();
2036 if (I == Mod->begin())
2041 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
2042 Function *Func = unwrap<Function>(Fn);
2043 Module::iterator I(Func);
2044 if (++I == Func->getParent()->end())
2049 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
2050 Function *Func = unwrap<Function>(Fn);
2051 Module::iterator I(Func);
2052 if (I == Func->getParent()->begin())
2057 void LLVMDeleteFunction(LLVMValueRef Fn) {
2058 unwrap<Function>(Fn)->eraseFromParent();
2061 LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) {
2062 return unwrap<Function>(Fn)->hasPersonalityFn();
2065 LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) {
2066 return wrap(unwrap<Function>(Fn)->getPersonalityFn());
2069 void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) {
2070 unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
2073 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
2074 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
2075 return F->getIntrinsicID();
2079 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
2080 return unwrap<Function>(Fn)->getCallingConv();
2083 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
2084 return unwrap<Function>(Fn)->setCallingConv(
2085 static_cast<CallingConv::ID>(CC));
2088 const char *LLVMGetGC(LLVMValueRef Fn) {
2089 Function *F = unwrap<Function>(Fn);
2090 return F->hasGC()? F->getGC().c_str() : nullptr;
2093 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
2094 Function *F = unwrap<Function>(Fn);
2101 void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2102 LLVMAttributeRef A) {
2103 unwrap<Function>(F)->addAttribute(Idx, unwrap(A));
2106 unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) {
2107 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2108 return AS.getNumAttributes();
2111 void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2112 LLVMAttributeRef *Attrs) {
2113 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2118 LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F,
2119 LLVMAttributeIndex Idx,
2121 return wrap(unwrap<Function>(F)->getAttribute(Idx,
2122 (Attribute::AttrKind)KindID));
2125 LLVMAttributeRef LLVMGetStringAttributeAtIndex(LLVMValueRef F,
2126 LLVMAttributeIndex Idx,
2127 const char *K, unsigned KLen) {
2128 return wrap(unwrap<Function>(F)->getAttribute(Idx, StringRef(K, KLen)));
2131 void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2133 unwrap<Function>(F)->removeAttribute(Idx, (Attribute::AttrKind)KindID);
2136 void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2137 const char *K, unsigned KLen) {
2138 unwrap<Function>(F)->removeAttribute(Idx, StringRef(K, KLen));
2141 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
2143 Function *Func = unwrap<Function>(Fn);
2144 Attribute Attr = Attribute::get(Func->getContext(), A, V);
2145 Func->addAttribute(AttributeList::FunctionIndex, Attr);
2148 /*--.. Operations on parameters ............................................--*/
2150 unsigned LLVMCountParams(LLVMValueRef FnRef) {
2151 // This function is strictly redundant to
2152 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
2153 return unwrap<Function>(FnRef)->arg_size();
2156 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
2157 Function *Fn = unwrap<Function>(FnRef);
2158 for (Function::arg_iterator I = Fn->arg_begin(),
2159 E = Fn->arg_end(); I != E; I++)
2160 *ParamRefs++ = wrap(&*I);
2163 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
2164 Function *Fn = unwrap<Function>(FnRef);
2165 return wrap(&Fn->arg_begin()[index]);
2168 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
2169 return wrap(unwrap<Argument>(V)->getParent());
2172 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
2173 Function *Func = unwrap<Function>(Fn);
2174 Function::arg_iterator I = Func->arg_begin();
2175 if (I == Func->arg_end())
2180 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
2181 Function *Func = unwrap<Function>(Fn);
2182 Function::arg_iterator I = Func->arg_end();
2183 if (I == Func->arg_begin())
2188 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
2189 Argument *A = unwrap<Argument>(Arg);
2190 Function *Fn = A->getParent();
2191 if (A->getArgNo() + 1 >= Fn->arg_size())
2193 return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
2196 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
2197 Argument *A = unwrap<Argument>(Arg);
2198 if (A->getArgNo() == 0)
2200 return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
2203 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
2204 Argument *A = unwrap<Argument>(Arg);
2205 A->addAttr(Attribute::getWithAlignment(A->getContext(), align));
2208 /*--.. Operations on basic blocks ..........................................--*/
2210 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
2211 return wrap(static_cast<Value*>(unwrap(BB)));
2214 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
2215 return isa<BasicBlock>(unwrap(Val));
2218 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
2219 return wrap(unwrap<BasicBlock>(Val));
2222 const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) {
2223 return unwrap(BB)->getName().data();
2226 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
2227 return wrap(unwrap(BB)->getParent());
2230 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
2231 return wrap(unwrap(BB)->getTerminator());
2234 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
2235 return unwrap<Function>(FnRef)->size();
2238 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
2239 Function *Fn = unwrap<Function>(FnRef);
2240 for (BasicBlock &BB : *Fn)
2241 *BasicBlocksRefs++ = wrap(&BB);
2244 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
2245 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2248 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
2249 Function *Func = unwrap<Function>(Fn);
2250 Function::iterator I = Func->begin();
2251 if (I == Func->end())
2256 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
2257 Function *Func = unwrap<Function>(Fn);
2258 Function::iterator I = Func->end();
2259 if (I == Func->begin())
2264 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
2265 BasicBlock *Block = unwrap(BB);
2266 Function::iterator I(Block);
2267 if (++I == Block->getParent()->end())
2272 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
2273 BasicBlock *Block = unwrap(BB);
2274 Function::iterator I(Block);
2275 if (I == Block->getParent()->begin())
2280 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
2283 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
2286 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
2287 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
2290 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
2291 LLVMBasicBlockRef BBRef,
2293 BasicBlock *BB = unwrap(BBRef);
2294 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2297 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
2299 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
2302 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
2303 unwrap(BBRef)->eraseFromParent();
2306 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
2307 unwrap(BBRef)->removeFromParent();
2310 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2311 unwrap(BB)->moveBefore(unwrap(MovePos));
2314 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2315 unwrap(BB)->moveAfter(unwrap(MovePos));
2318 /*--.. Operations on instructions ..........................................--*/
2320 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
2321 return wrap(unwrap<Instruction>(Inst)->getParent());
2324 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
2325 BasicBlock *Block = unwrap(BB);
2326 BasicBlock::iterator I = Block->begin();
2327 if (I == Block->end())
2332 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
2333 BasicBlock *Block = unwrap(BB);
2334 BasicBlock::iterator I = Block->end();
2335 if (I == Block->begin())
2340 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
2341 Instruction *Instr = unwrap<Instruction>(Inst);
2342 BasicBlock::iterator I(Instr);
2343 if (++I == Instr->getParent()->end())
2348 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
2349 Instruction *Instr = unwrap<Instruction>(Inst);
2350 BasicBlock::iterator I(Instr);
2351 if (I == Instr->getParent()->begin())
2356 void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) {
2357 unwrap<Instruction>(Inst)->removeFromParent();
2360 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
2361 unwrap<Instruction>(Inst)->eraseFromParent();
2364 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
2365 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2366 return (LLVMIntPredicate)I->getPredicate();
2367 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2368 if (CE->getOpcode() == Instruction::ICmp)
2369 return (LLVMIntPredicate)CE->getPredicate();
2370 return (LLVMIntPredicate)0;
2373 LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
2374 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2375 return (LLVMRealPredicate)I->getPredicate();
2376 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2377 if (CE->getOpcode() == Instruction::FCmp)
2378 return (LLVMRealPredicate)CE->getPredicate();
2379 return (LLVMRealPredicate)0;
2382 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
2383 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2384 return map_to_llvmopcode(C->getOpcode());
2385 return (LLVMOpcode)0;
2388 LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {
2389 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2390 return wrap(C->clone());
2394 unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) {
2395 if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) {
2396 return FPI->getNumArgOperands();
2398 return CallSite(unwrap<Instruction>(Instr)).getNumArgOperands();
2401 /*--.. Call and invoke instructions ........................................--*/
2403 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
2404 return CallSite(unwrap<Instruction>(Instr)).getCallingConv();
2407 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
2408 return CallSite(unwrap<Instruction>(Instr))
2409 .setCallingConv(static_cast<CallingConv::ID>(CC));
2412 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
2414 CallSite Call = CallSite(unwrap<Instruction>(Instr));
2415 Attribute AlignAttr = Attribute::getWithAlignment(Call->getContext(), align);
2416 Call.addAttribute(index, AlignAttr);
2419 void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2420 LLVMAttributeRef A) {
2421 CallSite(unwrap<Instruction>(C)).addAttribute(Idx, unwrap(A));
2424 unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C,
2425 LLVMAttributeIndex Idx) {
2426 auto CS = CallSite(unwrap<Instruction>(C));
2427 auto AS = CS.getAttributes().getAttributes(Idx);
2428 return AS.getNumAttributes();
2431 void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx,
2432 LLVMAttributeRef *Attrs) {
2433 auto CS = CallSite(unwrap<Instruction>(C));
2434 auto AS = CS.getAttributes().getAttributes(Idx);
2439 LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C,
2440 LLVMAttributeIndex Idx,
2442 return wrap(CallSite(unwrap<Instruction>(C))
2443 .getAttribute(Idx, (Attribute::AttrKind)KindID));
2446 LLVMAttributeRef LLVMGetCallSiteStringAttribute(LLVMValueRef C,
2447 LLVMAttributeIndex Idx,
2448 const char *K, unsigned KLen) {
2449 return wrap(CallSite(unwrap<Instruction>(C))
2450 .getAttribute(Idx, StringRef(K, KLen)));
2453 void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2455 CallSite(unwrap<Instruction>(C))
2456 .removeAttribute(Idx, (Attribute::AttrKind)KindID);
2459 void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2460 const char *K, unsigned KLen) {
2461 CallSite(unwrap<Instruction>(C)).removeAttribute(Idx, StringRef(K, KLen));
2464 LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) {
2465 return wrap(CallSite(unwrap<Instruction>(Instr)).getCalledValue());
2468 /*--.. Operations on call instructions (only) ..............................--*/
2470 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
2471 return unwrap<CallInst>(Call)->isTailCall();
2474 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
2475 unwrap<CallInst>(Call)->setTailCall(isTailCall);
2478 /*--.. Operations on invoke instructions (only) ............................--*/
2480 LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) {
2481 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2484 LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) {
2485 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2486 return wrap(CRI->getUnwindDest());
2487 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2488 return wrap(CSI->getUnwindDest());
2490 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2493 void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2494 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2497 void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2498 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2499 return CRI->setUnwindDest(unwrap(B));
2500 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2501 return CSI->setUnwindDest(unwrap(B));
2503 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2506 /*--.. Operations on terminators ...........................................--*/
2508 unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {
2509 return unwrap<TerminatorInst>(Term)->getNumSuccessors();
2512 LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) {
2513 return wrap(unwrap<TerminatorInst>(Term)->getSuccessor(i));
2516 void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
2517 return unwrap<TerminatorInst>(Term)->setSuccessor(i,unwrap(block));
2520 /*--.. Operations on branch instructions (only) ............................--*/
2522 LLVMBool LLVMIsConditional(LLVMValueRef Branch) {
2523 return unwrap<BranchInst>(Branch)->isConditional();
2526 LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) {
2527 return wrap(unwrap<BranchInst>(Branch)->getCondition());
2530 void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) {
2531 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2534 /*--.. Operations on switch instructions (only) ............................--*/
2536 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
2537 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2540 /*--.. Operations on alloca instructions (only) ............................--*/
2542 LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) {
2543 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
2546 /*--.. Operations on gep instructions (only) ...............................--*/
2548 LLVMBool LLVMIsInBounds(LLVMValueRef GEP) {
2549 return unwrap<GetElementPtrInst>(GEP)->isInBounds();
2552 void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) {
2553 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
2556 /*--.. Operations on phi nodes .............................................--*/
2558 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
2559 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
2560 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
2561 for (unsigned I = 0; I != Count; ++I)
2562 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
2565 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
2566 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
2569 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
2570 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
2573 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
2574 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
2577 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/
2579 unsigned LLVMGetNumIndices(LLVMValueRef Inst) {
2580 auto *I = unwrap(Inst);
2581 if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
2582 return GEP->getNumIndices();
2583 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2584 return EV->getNumIndices();
2585 if (auto *IV = dyn_cast<InsertValueInst>(I))
2586 return IV->getNumIndices();
2588 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
2591 const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
2592 auto *I = unwrap(Inst);
2593 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2594 return EV->getIndices().data();
2595 if (auto *IV = dyn_cast<InsertValueInst>(I))
2596 return IV->getIndices().data();
2598 "LLVMGetIndices applies only to extractvalue and insertvalue!");
2602 /*===-- Instruction builders ----------------------------------------------===*/
2604 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
2605 return wrap(new IRBuilder<>(*unwrap(C)));
2608 LLVMBuilderRef LLVMCreateBuilder(void) {
2609 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
2612 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
2613 LLVMValueRef Instr) {
2614 BasicBlock *BB = unwrap(Block);
2615 auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
2616 unwrap(Builder)->SetInsertPoint(BB, I);
2619 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2620 Instruction *I = unwrap<Instruction>(Instr);
2621 unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
2624 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
2625 BasicBlock *BB = unwrap(Block);
2626 unwrap(Builder)->SetInsertPoint(BB);
2629 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
2630 return wrap(unwrap(Builder)->GetInsertBlock());
2633 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
2634 unwrap(Builder)->ClearInsertionPoint();
2637 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2638 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
2641 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
2643 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
2646 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
2647 delete unwrap(Builder);
2650 /*--.. Metadata builders ...................................................--*/
2652 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
2654 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
2655 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
2658 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
2659 LLVMContext &Context = unwrap(Builder)->getContext();
2660 return wrap(MetadataAsValue::get(
2661 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
2664 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
2665 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
2669 /*--.. Instruction builders ................................................--*/
2671 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
2672 return wrap(unwrap(B)->CreateRetVoid());
2675 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
2676 return wrap(unwrap(B)->CreateRet(unwrap(V)));
2679 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
2681 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
2684 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
2685 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
2688 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
2689 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
2690 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
2693 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
2694 LLVMBasicBlockRef Else, unsigned NumCases) {
2695 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
2698 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
2699 unsigned NumDests) {
2700 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
2703 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
2704 LLVMValueRef *Args, unsigned NumArgs,
2705 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
2707 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
2708 makeArrayRef(unwrap(Args), NumArgs),
2712 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
2713 LLVMValueRef PersFn, unsigned NumClauses,
2715 // The personality used to live on the landingpad instruction, but now it
2716 // lives on the parent function. For compatibility, take the provided
2717 // personality and put it on the parent function.
2719 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
2720 cast<Function>(unwrap(PersFn)));
2721 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
2724 LLVMValueRef LLVMBuildCatchPad(LLVMBuilderRef B, LLVMValueRef ParentPad,
2725 LLVMValueRef *Args, unsigned NumArgs,
2727 return wrap(unwrap(B)->CreateCatchPad(unwrap(ParentPad),
2728 makeArrayRef(unwrap(Args), NumArgs),
2732 LLVMValueRef LLVMBuildCleanupPad(LLVMBuilderRef B, LLVMValueRef ParentPad,
2733 LLVMValueRef *Args, unsigned NumArgs,
2735 if (ParentPad == nullptr) {
2736 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
2737 ParentPad = wrap(Constant::getNullValue(Ty));
2739 return wrap(unwrap(B)->CreateCleanupPad(unwrap(ParentPad),
2740 makeArrayRef(unwrap(Args), NumArgs),
2744 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
2745 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
2748 LLVMValueRef LLVMBuildCatchSwitch(LLVMBuilderRef B, LLVMValueRef ParentPad,
2749 LLVMBasicBlockRef UnwindBB,
2750 unsigned NumHandlers, const char *Name) {
2751 if (ParentPad == nullptr) {
2752 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
2753 ParentPad = wrap(Constant::getNullValue(Ty));
2755 return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(UnwindBB),
2756 NumHandlers, Name));
2759 LLVMValueRef LLVMBuildCatchRet(LLVMBuilderRef B, LLVMValueRef CatchPad,
2760 LLVMBasicBlockRef BB) {
2761 return wrap(unwrap(B)->CreateCatchRet(unwrap<CatchPadInst>(CatchPad),
2765 LLVMValueRef LLVMBuildCleanupRet(LLVMBuilderRef B, LLVMValueRef CatchPad,
2766 LLVMBasicBlockRef BB) {
2767 return wrap(unwrap(B)->CreateCleanupRet(unwrap<CleanupPadInst>(CatchPad),
2771 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
2772 return wrap(unwrap(B)->CreateUnreachable());
2775 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
2776 LLVMBasicBlockRef Dest) {
2777 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
2780 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
2781 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
2784 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
2785 return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
2788 LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
2789 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
2792 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
2793 unwrap<LandingPadInst>(LandingPad)->
2794 addClause(cast<Constant>(unwrap(ClauseVal)));
2797 LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) {
2798 return unwrap<LandingPadInst>(LandingPad)->isCleanup();
2801 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
2802 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2805 void LLVMAddHandler(LLVMValueRef CatchSwitch, LLVMBasicBlockRef Dest) {
2806 unwrap<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Dest));
2809 unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) {
2810 return unwrap<CatchSwitchInst>(CatchSwitch)->getNumHandlers();
2813 void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) {
2814 CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(CatchSwitch);
2815 for (CatchSwitchInst::handler_iterator I = CSI->handler_begin(),
2816 E = CSI->handler_end(); I != E; ++I)
2817 *Handlers++ = wrap(*I);
2820 LLVMValueRef LLVMGetParentCatchSwitch(LLVMValueRef CatchPad) {
2821 return wrap(unwrap<CatchPadInst>(CatchPad)->getCatchSwitch());
2824 void LLVMSetParentCatchSwitch(LLVMValueRef CatchPad, LLVMValueRef CatchSwitch) {
2825 unwrap<CatchPadInst>(CatchPad)
2826 ->setCatchSwitch(unwrap<CatchSwitchInst>(CatchSwitch));
2829 /*--.. Funclets ...........................................................--*/
2831 LLVMValueRef LLVMGetArgOperand(LLVMValueRef Funclet, unsigned i) {
2832 return wrap(unwrap<FuncletPadInst>(Funclet)->getArgOperand(i));
2835 void LLVMSetArgOperand(LLVMValueRef Funclet, unsigned i, LLVMValueRef value) {
2836 unwrap<FuncletPadInst>(Funclet)->setArgOperand(i, unwrap(value));
2839 /*--.. Arithmetic ..........................................................--*/
2841 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2843 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2846 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2848 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2851 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2853 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2856 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2858 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2861 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2863 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2866 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2868 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2871 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2873 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2876 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2878 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2881 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2883 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2886 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2888 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2891 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2893 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2896 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2898 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2901 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2903 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2906 LLVMValueRef LLVMBuildExactUDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2907 LLVMValueRef RHS, const char *Name) {
2908 return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
2911 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2913 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2916 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2917 LLVMValueRef RHS, const char *Name) {
2918 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2921 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2923 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2926 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2928 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2931 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2933 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2936 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2938 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2941 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2943 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2946 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2948 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2951 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2953 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2956 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2958 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2961 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2963 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2966 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2968 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2971 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2972 LLVMValueRef LHS, LLVMValueRef RHS,
2974 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2975 unwrap(RHS), Name));
2978 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2979 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2982 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2984 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2987 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2989 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2992 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2993 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2996 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2997 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
3000 /*--.. Memory ..............................................................--*/
3002 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
3004 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3005 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3006 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3007 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3008 ITy, unwrap(Ty), AllocSize,
3009 nullptr, nullptr, "");
3010 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3013 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
3014 LLVMValueRef Val, const char *Name) {
3015 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3016 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3017 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3018 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3019 ITy, unwrap(Ty), AllocSize,
3020 unwrap(Val), nullptr, "");
3021 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3024 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
3026 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
3029 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
3030 LLVMValueRef Val, const char *Name) {
3031 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
3034 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
3035 return wrap(unwrap(B)->Insert(
3036 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
3039 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
3041 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
3044 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
3045 LLVMValueRef PointerVal) {
3046 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
3049 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
3051 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic;
3052 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered;
3053 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic;
3054 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire;
3055 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release;
3056 case LLVMAtomicOrderingAcquireRelease:
3057 return AtomicOrdering::AcquireRelease;
3058 case LLVMAtomicOrderingSequentiallyConsistent:
3059 return AtomicOrdering::SequentiallyConsistent;
3062 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
3065 static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) {
3067 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic;
3068 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered;
3069 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic;
3070 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire;
3071 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease;
3072 case AtomicOrdering::AcquireRelease:
3073 return LLVMAtomicOrderingAcquireRelease;
3074 case AtomicOrdering::SequentiallyConsistent:
3075 return LLVMAtomicOrderingSequentiallyConsistent;
3078 llvm_unreachable("Invalid AtomicOrdering value!");
3081 // TODO: Should this and other atomic instructions support building with
3083 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
3084 LLVMBool isSingleThread, const char *Name) {
3086 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
3087 isSingleThread ? SyncScope::SingleThread
3088 : SyncScope::System,
3092 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
3093 LLVMValueRef *Indices, unsigned NumIndices,
3095 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3096 return wrap(unwrap(B)->CreateGEP(nullptr, unwrap(Pointer), IdxList, Name));
3099 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
3100 LLVMValueRef *Indices, unsigned NumIndices,
3102 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3104 unwrap(B)->CreateInBoundsGEP(nullptr, unwrap(Pointer), IdxList, Name));
3107 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
3108 unsigned Idx, const char *Name) {
3109 return wrap(unwrap(B)->CreateStructGEP(nullptr, unwrap(Pointer), Idx, Name));
3112 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
3114 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
3117 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
3119 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
3122 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
3123 Value *P = unwrap<Value>(MemAccessInst);
3124 if (LoadInst *LI = dyn_cast<LoadInst>(P))
3125 return LI->isVolatile();
3126 return cast<StoreInst>(P)->isVolatile();
3129 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
3130 Value *P = unwrap<Value>(MemAccessInst);
3131 if (LoadInst *LI = dyn_cast<LoadInst>(P))
3132 return LI->setVolatile(isVolatile);
3133 return cast<StoreInst>(P)->setVolatile(isVolatile);
3136 LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) {
3137 Value *P = unwrap<Value>(MemAccessInst);
3139 if (LoadInst *LI = dyn_cast<LoadInst>(P))
3140 O = LI->getOrdering();
3142 O = cast<StoreInst>(P)->getOrdering();
3143 return mapToLLVMOrdering(O);
3146 void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
3147 Value *P = unwrap<Value>(MemAccessInst);
3148 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3150 if (LoadInst *LI = dyn_cast<LoadInst>(P))
3151 return LI->setOrdering(O);
3152 return cast<StoreInst>(P)->setOrdering(O);
3155 /*--.. Casts ...............................................................--*/
3157 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
3158 LLVMTypeRef DestTy, const char *Name) {
3159 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
3162 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
3163 LLVMTypeRef DestTy, const char *Name) {
3164 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
3167 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
3168 LLVMTypeRef DestTy, const char *Name) {
3169 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
3172 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
3173 LLVMTypeRef DestTy, const char *Name) {
3174 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
3177 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
3178 LLVMTypeRef DestTy, const char *Name) {
3179 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
3182 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
3183 LLVMTypeRef DestTy, const char *Name) {
3184 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
3187 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
3188 LLVMTypeRef DestTy, const char *Name) {
3189 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
3192 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
3193 LLVMTypeRef DestTy, const char *Name) {
3194 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
3197 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
3198 LLVMTypeRef DestTy, const char *Name) {
3199 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
3202 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
3203 LLVMTypeRef DestTy, const char *Name) {
3204 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
3207 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
3208 LLVMTypeRef DestTy, const char *Name) {
3209 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
3212 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3213 LLVMTypeRef DestTy, const char *Name) {
3214 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
3217 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
3218 LLVMTypeRef DestTy, const char *Name) {
3219 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
3222 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3223 LLVMTypeRef DestTy, const char *Name) {
3224 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
3228 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3229 LLVMTypeRef DestTy, const char *Name) {
3230 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
3234 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3235 LLVMTypeRef DestTy, const char *Name) {
3236 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
3240 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
3241 LLVMTypeRef DestTy, const char *Name) {
3242 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
3243 unwrap(DestTy), Name));
3246 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
3247 LLVMTypeRef DestTy, const char *Name) {
3248 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
3251 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
3252 LLVMTypeRef DestTy, const char *Name) {
3253 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
3254 /*isSigned*/true, Name));
3257 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
3258 LLVMTypeRef DestTy, const char *Name) {
3259 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
3262 /*--.. Comparisons .........................................................--*/
3264 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
3265 LLVMValueRef LHS, LLVMValueRef RHS,
3267 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
3268 unwrap(LHS), unwrap(RHS), Name));
3271 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
3272 LLVMValueRef LHS, LLVMValueRef RHS,
3274 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
3275 unwrap(LHS), unwrap(RHS), Name));
3278 /*--.. Miscellaneous instructions ..........................................--*/
3280 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
3281 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
3284 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
3285 LLVMValueRef *Args, unsigned NumArgs,
3287 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
3288 makeArrayRef(unwrap(Args), NumArgs),
3292 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
3293 LLVMValueRef Then, LLVMValueRef Else,
3295 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
3299 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
3300 LLVMTypeRef Ty, const char *Name) {
3301 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
3304 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
3305 LLVMValueRef Index, const char *Name) {
3306 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
3310 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
3311 LLVMValueRef EltVal, LLVMValueRef Index,
3313 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
3314 unwrap(Index), Name));
3317 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
3318 LLVMValueRef V2, LLVMValueRef Mask,
3320 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
3321 unwrap(Mask), Name));
3324 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
3325 unsigned Index, const char *Name) {
3326 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
3329 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
3330 LLVMValueRef EltVal, unsigned Index,
3332 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
3336 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
3338 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
3341 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
3343 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
3346 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
3347 LLVMValueRef RHS, const char *Name) {
3348 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
3351 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
3352 LLVMValueRef PTR, LLVMValueRef Val,
3353 LLVMAtomicOrdering ordering,
3354 LLVMBool singleThread) {
3355 AtomicRMWInst::BinOp intop;
3357 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
3358 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
3359 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
3360 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
3361 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
3362 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
3363 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
3364 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
3365 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
3366 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
3367 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
3369 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
3370 mapFromLLVMOrdering(ordering), singleThread ? SyncScope::SingleThread
3371 : SyncScope::System));
3374 LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr,
3375 LLVMValueRef Cmp, LLVMValueRef New,
3376 LLVMAtomicOrdering SuccessOrdering,
3377 LLVMAtomicOrdering FailureOrdering,
3378 LLVMBool singleThread) {
3380 return wrap(unwrap(B)->CreateAtomicCmpXchg(unwrap(Ptr), unwrap(Cmp),
3381 unwrap(New), mapFromLLVMOrdering(SuccessOrdering),
3382 mapFromLLVMOrdering(FailureOrdering),
3383 singleThread ? SyncScope::SingleThread : SyncScope::System));
3387 LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) {
3388 Value *P = unwrap<Value>(AtomicInst);
3390 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
3391 return I->getSyncScopeID() == SyncScope::SingleThread;
3392 return cast<AtomicCmpXchgInst>(P)->getSyncScopeID() ==
3393 SyncScope::SingleThread;
3396 void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) {
3397 Value *P = unwrap<Value>(AtomicInst);
3398 SyncScope::ID SSID = NewValue ? SyncScope::SingleThread : SyncScope::System;
3400 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
3401 return I->setSyncScopeID(SSID);
3402 return cast<AtomicCmpXchgInst>(P)->setSyncScopeID(SSID);
3405 LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) {
3406 Value *P = unwrap<Value>(CmpXchgInst);
3407 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering());
3410 void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst,
3411 LLVMAtomicOrdering Ordering) {
3412 Value *P = unwrap<Value>(CmpXchgInst);
3413 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3415 return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O);
3418 LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst) {
3419 Value *P = unwrap<Value>(CmpXchgInst);
3420 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering());
3423 void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst,
3424 LLVMAtomicOrdering Ordering) {
3425 Value *P = unwrap<Value>(CmpXchgInst);
3426 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3428 return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O);
3431 /*===-- Module providers --------------------------------------------------===*/
3433 LLVMModuleProviderRef
3434 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
3435 return reinterpret_cast<LLVMModuleProviderRef>(M);
3438 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
3443 /*===-- Memory buffers ----------------------------------------------------===*/
3445 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
3447 LLVMMemoryBufferRef *OutMemBuf,
3448 char **OutMessage) {
3450 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
3451 if (std::error_code EC = MBOrErr.getError()) {
3452 *OutMessage = strdup(EC.message().c_str());
3455 *OutMemBuf = wrap(MBOrErr.get().release());
3459 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
3460 char **OutMessage) {
3461 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
3462 if (std::error_code EC = MBOrErr.getError()) {
3463 *OutMessage = strdup(EC.message().c_str());
3466 *OutMemBuf = wrap(MBOrErr.get().release());
3470 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
3471 const char *InputData,
3472 size_t InputDataLength,
3473 const char *BufferName,
3474 LLVMBool RequiresNullTerminator) {
3476 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
3477 StringRef(BufferName),
3478 RequiresNullTerminator).release());
3481 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
3482 const char *InputData,
3483 size_t InputDataLength,
3484 const char *BufferName) {
3487 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
3488 StringRef(BufferName)).release());
3491 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
3492 return unwrap(MemBuf)->getBufferStart();
3495 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
3496 return unwrap(MemBuf)->getBufferSize();
3499 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
3500 delete unwrap(MemBuf);
3503 /*===-- Pass Registry -----------------------------------------------------===*/
3505 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
3506 return wrap(PassRegistry::getPassRegistry());
3509 /*===-- Pass Manager ------------------------------------------------------===*/
3511 LLVMPassManagerRef LLVMCreatePassManager() {
3512 return wrap(new legacy::PassManager());
3515 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
3516 return wrap(new legacy::FunctionPassManager(unwrap(M)));
3519 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
3520 return LLVMCreateFunctionPassManagerForModule(
3521 reinterpret_cast<LLVMModuleRef>(P));
3524 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
3525 return unwrap<legacy::PassManager>(PM)->run(*unwrap(M));
3528 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
3529 return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization();
3532 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
3533 return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
3536 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
3537 return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization();
3540 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
3544 /*===-- Threading ------------------------------------------------------===*/
3546 LLVMBool LLVMStartMultithreaded() {
3547 return LLVMIsMultithreaded();
3550 void LLVMStopMultithreaded() {
3553 LLVMBool LLVMIsMultithreaded() {
3554 return llvm_is_multithreaded();