[Alignment] Migrate Attribute::getWith(Stack)Alignment
[llvm-core.git] / lib / IR / Core.cpp
bloba5f46b16e600bef356c1c5013be1a98d745f2765
1 //===-- Core.cpp ----------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the common infrastructure (including the C bindings)
10 // for libLLVMCore.a, which implements the LLVM intermediate representation.
12 //===----------------------------------------------------------------------===//
14 #include "llvm-c/Core.h"
15 #include "llvm/ADT/StringSwitch.h"
16 #include "llvm/IR/Attributes.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/DebugInfoMetadata.h"
19 #include "llvm/IR/DerivedTypes.h"
20 #include "llvm/IR/DiagnosticInfo.h"
21 #include "llvm/IR/DiagnosticPrinter.h"
22 #include "llvm/IR/GlobalAlias.h"
23 #include "llvm/IR/GlobalVariable.h"
24 #include "llvm/IR/IRBuilder.h"
25 #include "llvm/IR/InlineAsm.h"
26 #include "llvm/IR/IntrinsicInst.h"
27 #include "llvm/IR/LLVMContext.h"
28 #include "llvm/IR/LegacyPassManager.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/FileSystem.h"
33 #include "llvm/Support/ManagedStatic.h"
34 #include "llvm/Support/MemoryBuffer.h"
35 #include "llvm/Support/Threading.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include <cassert>
38 #include <cstdlib>
39 #include <cstring>
40 #include <system_error>
42 using namespace llvm;
44 #define DEBUG_TYPE "ir"
46 void llvm::initializeCore(PassRegistry &Registry) {
47 initializeDominatorTreeWrapperPassPass(Registry);
48 initializePrintModulePassWrapperPass(Registry);
49 initializePrintFunctionPassWrapperPass(Registry);
50 initializePrintBasicBlockPassPass(Registry);
51 initializeSafepointIRVerifierPass(Registry);
52 initializeVerifierLegacyPassPass(Registry);
55 void LLVMInitializeCore(LLVMPassRegistryRef R) {
56 initializeCore(*unwrap(R));
59 void LLVMShutdown() {
60 llvm_shutdown();
63 /*===-- Error handling ----------------------------------------------------===*/
65 char *LLVMCreateMessage(const char *Message) {
66 return strdup(Message);
69 void LLVMDisposeMessage(char *Message) {
70 free(Message);
74 /*===-- Operations on contexts --------------------------------------------===*/
76 static ManagedStatic<LLVMContext> GlobalContext;
78 LLVMContextRef LLVMContextCreate() {
79 return wrap(new LLVMContext());
82 LLVMContextRef LLVMGetGlobalContext() { return wrap(&*GlobalContext); }
84 void LLVMContextSetDiagnosticHandler(LLVMContextRef C,
85 LLVMDiagnosticHandler Handler,
86 void *DiagnosticContext) {
87 unwrap(C)->setDiagnosticHandlerCallBack(
88 LLVM_EXTENSION reinterpret_cast<DiagnosticHandler::DiagnosticHandlerTy>(
89 Handler),
90 DiagnosticContext);
93 LLVMDiagnosticHandler LLVMContextGetDiagnosticHandler(LLVMContextRef C) {
94 return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>(
95 unwrap(C)->getDiagnosticHandlerCallBack());
98 void *LLVMContextGetDiagnosticContext(LLVMContextRef C) {
99 return unwrap(C)->getDiagnosticContext();
102 void LLVMContextSetYieldCallback(LLVMContextRef C, LLVMYieldCallback Callback,
103 void *OpaqueHandle) {
104 auto YieldCallback =
105 LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
106 unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
109 LLVMBool LLVMContextShouldDiscardValueNames(LLVMContextRef C) {
110 return unwrap(C)->shouldDiscardValueNames();
113 void LLVMContextSetDiscardValueNames(LLVMContextRef C, LLVMBool Discard) {
114 unwrap(C)->setDiscardValueNames(Discard);
117 void LLVMContextDispose(LLVMContextRef C) {
118 delete unwrap(C);
121 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char *Name,
122 unsigned SLen) {
123 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
126 unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) {
127 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
130 #define GET_ATTR_KIND_FROM_NAME
131 #include "AttributesCompatFunc.inc"
133 unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) {
134 return getAttrKindFromName(StringRef(Name, SLen));
137 unsigned LLVMGetLastEnumAttributeKind(void) {
138 return Attribute::AttrKind::EndAttrKinds;
141 LLVMAttributeRef LLVMCreateEnumAttribute(LLVMContextRef C, unsigned KindID,
142 uint64_t Val) {
143 auto &Ctx = *unwrap(C);
144 auto AttrKind = (Attribute::AttrKind)KindID;
146 if (AttrKind == Attribute::AttrKind::ByVal) {
147 // After r362128, byval attributes need to have a type attribute. Provide a
148 // NULL one until a proper API is added for this.
149 return wrap(Attribute::getWithByValType(Ctx, NULL));
150 } else {
151 return wrap(Attribute::get(Ctx, AttrKind, Val));
155 unsigned LLVMGetEnumAttributeKind(LLVMAttributeRef A) {
156 return unwrap(A).getKindAsEnum();
159 uint64_t LLVMGetEnumAttributeValue(LLVMAttributeRef A) {
160 auto Attr = unwrap(A);
161 if (Attr.isEnumAttribute())
162 return 0;
163 return Attr.getValueAsInt();
166 LLVMAttributeRef LLVMCreateStringAttribute(LLVMContextRef C,
167 const char *K, unsigned KLength,
168 const char *V, unsigned VLength) {
169 return wrap(Attribute::get(*unwrap(C), StringRef(K, KLength),
170 StringRef(V, VLength)));
173 const char *LLVMGetStringAttributeKind(LLVMAttributeRef A,
174 unsigned *Length) {
175 auto S = unwrap(A).getKindAsString();
176 *Length = S.size();
177 return S.data();
180 const char *LLVMGetStringAttributeValue(LLVMAttributeRef A,
181 unsigned *Length) {
182 auto S = unwrap(A).getValueAsString();
183 *Length = S.size();
184 return S.data();
187 LLVMBool LLVMIsEnumAttribute(LLVMAttributeRef A) {
188 auto Attr = unwrap(A);
189 return Attr.isEnumAttribute() || Attr.isIntAttribute();
192 LLVMBool LLVMIsStringAttribute(LLVMAttributeRef A) {
193 return unwrap(A).isStringAttribute();
196 char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) {
197 std::string MsgStorage;
198 raw_string_ostream Stream(MsgStorage);
199 DiagnosticPrinterRawOStream DP(Stream);
201 unwrap(DI)->print(DP);
202 Stream.flush();
204 return LLVMCreateMessage(MsgStorage.c_str());
207 LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI) {
208 LLVMDiagnosticSeverity severity;
210 switch(unwrap(DI)->getSeverity()) {
211 default:
212 severity = LLVMDSError;
213 break;
214 case DS_Warning:
215 severity = LLVMDSWarning;
216 break;
217 case DS_Remark:
218 severity = LLVMDSRemark;
219 break;
220 case DS_Note:
221 severity = LLVMDSNote;
222 break;
225 return severity;
228 /*===-- Operations on modules ---------------------------------------------===*/
230 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
231 return wrap(new Module(ModuleID, *GlobalContext));
234 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
235 LLVMContextRef C) {
236 return wrap(new Module(ModuleID, *unwrap(C)));
239 void LLVMDisposeModule(LLVMModuleRef M) {
240 delete unwrap(M);
243 const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) {
244 auto &Str = unwrap(M)->getModuleIdentifier();
245 *Len = Str.length();
246 return Str.c_str();
249 void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) {
250 unwrap(M)->setModuleIdentifier(StringRef(Ident, Len));
253 const char *LLVMGetSourceFileName(LLVMModuleRef M, size_t *Len) {
254 auto &Str = unwrap(M)->getSourceFileName();
255 *Len = Str.length();
256 return Str.c_str();
259 void LLVMSetSourceFileName(LLVMModuleRef M, const char *Name, size_t Len) {
260 unwrap(M)->setSourceFileName(StringRef(Name, Len));
263 /*--.. Data layout .........................................................--*/
264 const char *LLVMGetDataLayoutStr(LLVMModuleRef M) {
265 return unwrap(M)->getDataLayoutStr().c_str();
268 const char *LLVMGetDataLayout(LLVMModuleRef M) {
269 return LLVMGetDataLayoutStr(M);
272 void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
273 unwrap(M)->setDataLayout(DataLayoutStr);
276 /*--.. Target triple .......................................................--*/
277 const char * LLVMGetTarget(LLVMModuleRef M) {
278 return unwrap(M)->getTargetTriple().c_str();
281 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
282 unwrap(M)->setTargetTriple(Triple);
285 /*--.. Module flags ........................................................--*/
286 struct LLVMOpaqueModuleFlagEntry {
287 LLVMModuleFlagBehavior Behavior;
288 const char *Key;
289 size_t KeyLen;
290 LLVMMetadataRef Metadata;
293 static Module::ModFlagBehavior
294 map_to_llvmModFlagBehavior(LLVMModuleFlagBehavior Behavior) {
295 switch (Behavior) {
296 case LLVMModuleFlagBehaviorError:
297 return Module::ModFlagBehavior::Error;
298 case LLVMModuleFlagBehaviorWarning:
299 return Module::ModFlagBehavior::Warning;
300 case LLVMModuleFlagBehaviorRequire:
301 return Module::ModFlagBehavior::Require;
302 case LLVMModuleFlagBehaviorOverride:
303 return Module::ModFlagBehavior::Override;
304 case LLVMModuleFlagBehaviorAppend:
305 return Module::ModFlagBehavior::Append;
306 case LLVMModuleFlagBehaviorAppendUnique:
307 return Module::ModFlagBehavior::AppendUnique;
309 llvm_unreachable("Unknown LLVMModuleFlagBehavior");
312 static LLVMModuleFlagBehavior
313 map_from_llvmModFlagBehavior(Module::ModFlagBehavior Behavior) {
314 switch (Behavior) {
315 case Module::ModFlagBehavior::Error:
316 return LLVMModuleFlagBehaviorError;
317 case Module::ModFlagBehavior::Warning:
318 return LLVMModuleFlagBehaviorWarning;
319 case Module::ModFlagBehavior::Require:
320 return LLVMModuleFlagBehaviorRequire;
321 case Module::ModFlagBehavior::Override:
322 return LLVMModuleFlagBehaviorOverride;
323 case Module::ModFlagBehavior::Append:
324 return LLVMModuleFlagBehaviorAppend;
325 case Module::ModFlagBehavior::AppendUnique:
326 return LLVMModuleFlagBehaviorAppendUnique;
327 default:
328 llvm_unreachable("Unhandled Flag Behavior");
332 LLVMModuleFlagEntry *LLVMCopyModuleFlagsMetadata(LLVMModuleRef M, size_t *Len) {
333 SmallVector<Module::ModuleFlagEntry, 8> MFEs;
334 unwrap(M)->getModuleFlagsMetadata(MFEs);
336 LLVMOpaqueModuleFlagEntry *Result = static_cast<LLVMOpaqueModuleFlagEntry *>(
337 safe_malloc(MFEs.size() * sizeof(LLVMOpaqueModuleFlagEntry)));
338 for (unsigned i = 0; i < MFEs.size(); ++i) {
339 const auto &ModuleFlag = MFEs[i];
340 Result[i].Behavior = map_from_llvmModFlagBehavior(ModuleFlag.Behavior);
341 Result[i].Key = ModuleFlag.Key->getString().data();
342 Result[i].KeyLen = ModuleFlag.Key->getString().size();
343 Result[i].Metadata = wrap(ModuleFlag.Val);
345 *Len = MFEs.size();
346 return Result;
349 void LLVMDisposeModuleFlagsMetadata(LLVMModuleFlagEntry *Entries) {
350 free(Entries);
353 LLVMModuleFlagBehavior
354 LLVMModuleFlagEntriesGetFlagBehavior(LLVMModuleFlagEntry *Entries,
355 unsigned Index) {
356 LLVMOpaqueModuleFlagEntry MFE =
357 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
358 return MFE.Behavior;
361 const char *LLVMModuleFlagEntriesGetKey(LLVMModuleFlagEntry *Entries,
362 unsigned Index, size_t *Len) {
363 LLVMOpaqueModuleFlagEntry MFE =
364 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
365 *Len = MFE.KeyLen;
366 return MFE.Key;
369 LLVMMetadataRef LLVMModuleFlagEntriesGetMetadata(LLVMModuleFlagEntry *Entries,
370 unsigned Index) {
371 LLVMOpaqueModuleFlagEntry MFE =
372 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
373 return MFE.Metadata;
376 LLVMMetadataRef LLVMGetModuleFlag(LLVMModuleRef M,
377 const char *Key, size_t KeyLen) {
378 return wrap(unwrap(M)->getModuleFlag({Key, KeyLen}));
381 void LLVMAddModuleFlag(LLVMModuleRef M, LLVMModuleFlagBehavior Behavior,
382 const char *Key, size_t KeyLen,
383 LLVMMetadataRef Val) {
384 unwrap(M)->addModuleFlag(map_to_llvmModFlagBehavior(Behavior),
385 {Key, KeyLen}, unwrap(Val));
388 /*--.. Printing modules ....................................................--*/
390 void LLVMDumpModule(LLVMModuleRef M) {
391 unwrap(M)->print(errs(), nullptr,
392 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
395 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
396 char **ErrorMessage) {
397 std::error_code EC;
398 raw_fd_ostream dest(Filename, EC, sys::fs::OF_Text);
399 if (EC) {
400 *ErrorMessage = strdup(EC.message().c_str());
401 return true;
404 unwrap(M)->print(dest, nullptr);
406 dest.close();
408 if (dest.has_error()) {
409 std::string E = "Error printing to file: " + dest.error().message();
410 *ErrorMessage = strdup(E.c_str());
411 return true;
414 return false;
417 char *LLVMPrintModuleToString(LLVMModuleRef M) {
418 std::string buf;
419 raw_string_ostream os(buf);
421 unwrap(M)->print(os, nullptr);
422 os.flush();
424 return strdup(buf.c_str());
427 /*--.. Operations on inline assembler ......................................--*/
428 void LLVMSetModuleInlineAsm2(LLVMModuleRef M, const char *Asm, size_t Len) {
429 unwrap(M)->setModuleInlineAsm(StringRef(Asm, Len));
432 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
433 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
436 void LLVMAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm, size_t Len) {
437 unwrap(M)->appendModuleInlineAsm(StringRef(Asm, Len));
440 const char *LLVMGetModuleInlineAsm(LLVMModuleRef M, size_t *Len) {
441 auto &Str = unwrap(M)->getModuleInlineAsm();
442 *Len = Str.length();
443 return Str.c_str();
446 LLVMValueRef LLVMGetInlineAsm(LLVMTypeRef Ty,
447 char *AsmString, size_t AsmStringSize,
448 char *Constraints, size_t ConstraintsSize,
449 LLVMBool HasSideEffects, LLVMBool IsAlignStack,
450 LLVMInlineAsmDialect Dialect) {
451 InlineAsm::AsmDialect AD;
452 switch (Dialect) {
453 case LLVMInlineAsmDialectATT:
454 AD = InlineAsm::AD_ATT;
455 break;
456 case LLVMInlineAsmDialectIntel:
457 AD = InlineAsm::AD_Intel;
458 break;
460 return wrap(InlineAsm::get(unwrap<FunctionType>(Ty),
461 StringRef(AsmString, AsmStringSize),
462 StringRef(Constraints, ConstraintsSize),
463 HasSideEffects, IsAlignStack, AD));
467 /*--.. Operations on module contexts ......................................--*/
468 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
469 return wrap(&unwrap(M)->getContext());
473 /*===-- Operations on types -----------------------------------------------===*/
475 /*--.. Operations on all types (mostly) ....................................--*/
477 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
478 switch (unwrap(Ty)->getTypeID()) {
479 case Type::VoidTyID:
480 return LLVMVoidTypeKind;
481 case Type::HalfTyID:
482 return LLVMHalfTypeKind;
483 case Type::FloatTyID:
484 return LLVMFloatTypeKind;
485 case Type::DoubleTyID:
486 return LLVMDoubleTypeKind;
487 case Type::X86_FP80TyID:
488 return LLVMX86_FP80TypeKind;
489 case Type::FP128TyID:
490 return LLVMFP128TypeKind;
491 case Type::PPC_FP128TyID:
492 return LLVMPPC_FP128TypeKind;
493 case Type::LabelTyID:
494 return LLVMLabelTypeKind;
495 case Type::MetadataTyID:
496 return LLVMMetadataTypeKind;
497 case Type::IntegerTyID:
498 return LLVMIntegerTypeKind;
499 case Type::FunctionTyID:
500 return LLVMFunctionTypeKind;
501 case Type::StructTyID:
502 return LLVMStructTypeKind;
503 case Type::ArrayTyID:
504 return LLVMArrayTypeKind;
505 case Type::PointerTyID:
506 return LLVMPointerTypeKind;
507 case Type::VectorTyID:
508 return LLVMVectorTypeKind;
509 case Type::X86_MMXTyID:
510 return LLVMX86_MMXTypeKind;
511 case Type::TokenTyID:
512 return LLVMTokenTypeKind;
514 llvm_unreachable("Unhandled TypeID.");
517 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
519 return unwrap(Ty)->isSized();
522 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
523 return wrap(&unwrap(Ty)->getContext());
526 void LLVMDumpType(LLVMTypeRef Ty) {
527 return unwrap(Ty)->print(errs(), /*IsForDebug=*/true);
530 char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
531 std::string buf;
532 raw_string_ostream os(buf);
534 if (unwrap(Ty))
535 unwrap(Ty)->print(os);
536 else
537 os << "Printing <null> Type";
539 os.flush();
541 return strdup(buf.c_str());
544 /*--.. Operations on integer types .........................................--*/
546 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
547 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
549 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
550 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
552 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
553 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
555 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
556 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
558 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
559 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
561 LLVMTypeRef LLVMInt128TypeInContext(LLVMContextRef C) {
562 return (LLVMTypeRef) Type::getInt128Ty(*unwrap(C));
564 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
565 return wrap(IntegerType::get(*unwrap(C), NumBits));
568 LLVMTypeRef LLVMInt1Type(void) {
569 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
571 LLVMTypeRef LLVMInt8Type(void) {
572 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
574 LLVMTypeRef LLVMInt16Type(void) {
575 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
577 LLVMTypeRef LLVMInt32Type(void) {
578 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
580 LLVMTypeRef LLVMInt64Type(void) {
581 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
583 LLVMTypeRef LLVMInt128Type(void) {
584 return LLVMInt128TypeInContext(LLVMGetGlobalContext());
586 LLVMTypeRef LLVMIntType(unsigned NumBits) {
587 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
590 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
591 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
594 /*--.. Operations on real types ............................................--*/
596 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
597 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
599 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
600 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
602 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
603 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
605 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
606 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
608 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
609 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
611 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
612 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
614 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
615 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
618 LLVMTypeRef LLVMHalfType(void) {
619 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
621 LLVMTypeRef LLVMFloatType(void) {
622 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
624 LLVMTypeRef LLVMDoubleType(void) {
625 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
627 LLVMTypeRef LLVMX86FP80Type(void) {
628 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
630 LLVMTypeRef LLVMFP128Type(void) {
631 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
633 LLVMTypeRef LLVMPPCFP128Type(void) {
634 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
636 LLVMTypeRef LLVMX86MMXType(void) {
637 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
640 /*--.. Operations on function types ........................................--*/
642 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
643 LLVMTypeRef *ParamTypes, unsigned ParamCount,
644 LLVMBool IsVarArg) {
645 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
646 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
649 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
650 return unwrap<FunctionType>(FunctionTy)->isVarArg();
653 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
654 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
657 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
658 return unwrap<FunctionType>(FunctionTy)->getNumParams();
661 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
662 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
663 for (FunctionType::param_iterator I = Ty->param_begin(),
664 E = Ty->param_end(); I != E; ++I)
665 *Dest++ = wrap(*I);
668 /*--.. Operations on struct types ..........................................--*/
670 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
671 unsigned ElementCount, LLVMBool Packed) {
672 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
673 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
676 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
677 unsigned ElementCount, LLVMBool Packed) {
678 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
679 ElementCount, Packed);
682 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
684 return wrap(StructType::create(*unwrap(C), Name));
687 const char *LLVMGetStructName(LLVMTypeRef Ty)
689 StructType *Type = unwrap<StructType>(Ty);
690 if (!Type->hasName())
691 return nullptr;
692 return Type->getName().data();
695 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
696 unsigned ElementCount, LLVMBool Packed) {
697 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
698 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
701 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
702 return unwrap<StructType>(StructTy)->getNumElements();
705 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
706 StructType *Ty = unwrap<StructType>(StructTy);
707 for (StructType::element_iterator I = Ty->element_begin(),
708 E = Ty->element_end(); I != E; ++I)
709 *Dest++ = wrap(*I);
712 LLVMTypeRef LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy, unsigned i) {
713 StructType *Ty = unwrap<StructType>(StructTy);
714 return wrap(Ty->getTypeAtIndex(i));
717 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
718 return unwrap<StructType>(StructTy)->isPacked();
721 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
722 return unwrap<StructType>(StructTy)->isOpaque();
725 LLVMBool LLVMIsLiteralStruct(LLVMTypeRef StructTy) {
726 return unwrap<StructType>(StructTy)->isLiteral();
729 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
730 return wrap(unwrap(M)->getTypeByName(Name));
733 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
735 void LLVMGetSubtypes(LLVMTypeRef Tp, LLVMTypeRef *Arr) {
736 int i = 0;
737 for (auto *T : unwrap(Tp)->subtypes()) {
738 Arr[i] = wrap(T);
739 i++;
743 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
744 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
747 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
748 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
751 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
752 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
755 LLVMTypeRef LLVMGetElementType(LLVMTypeRef WrappedTy) {
756 auto *Ty = unwrap<Type>(WrappedTy);
757 if (auto *PTy = dyn_cast<PointerType>(Ty))
758 return wrap(PTy->getElementType());
759 return wrap(cast<SequentialType>(Ty)->getElementType());
762 unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp) {
763 return unwrap(Tp)->getNumContainedTypes();
766 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
767 return unwrap<ArrayType>(ArrayTy)->getNumElements();
770 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
771 return unwrap<PointerType>(PointerTy)->getAddressSpace();
774 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
775 return unwrap<VectorType>(VectorTy)->getNumElements();
778 /*--.. Operations on other types ...........................................--*/
780 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
781 return wrap(Type::getVoidTy(*unwrap(C)));
783 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
784 return wrap(Type::getLabelTy(*unwrap(C)));
786 LLVMTypeRef LLVMTokenTypeInContext(LLVMContextRef C) {
787 return wrap(Type::getTokenTy(*unwrap(C)));
789 LLVMTypeRef LLVMMetadataTypeInContext(LLVMContextRef C) {
790 return wrap(Type::getMetadataTy(*unwrap(C)));
793 LLVMTypeRef LLVMVoidType(void) {
794 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
796 LLVMTypeRef LLVMLabelType(void) {
797 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
800 /*===-- Operations on values ----------------------------------------------===*/
802 /*--.. Operations on all values ............................................--*/
804 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
805 return wrap(unwrap(Val)->getType());
808 LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) {
809 switch(unwrap(Val)->getValueID()) {
810 #define HANDLE_VALUE(Name) \
811 case Value::Name##Val: \
812 return LLVM##Name##ValueKind;
813 #include "llvm/IR/Value.def"
814 default:
815 return LLVMInstructionValueKind;
819 const char *LLVMGetValueName2(LLVMValueRef Val, size_t *Length) {
820 auto *V = unwrap(Val);
821 *Length = V->getName().size();
822 return V->getName().data();
825 void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen) {
826 unwrap(Val)->setName(StringRef(Name, NameLen));
829 const char *LLVMGetValueName(LLVMValueRef Val) {
830 return unwrap(Val)->getName().data();
833 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
834 unwrap(Val)->setName(Name);
837 void LLVMDumpValue(LLVMValueRef Val) {
838 unwrap(Val)->print(errs(), /*IsForDebug=*/true);
841 char* LLVMPrintValueToString(LLVMValueRef Val) {
842 std::string buf;
843 raw_string_ostream os(buf);
845 if (unwrap(Val))
846 unwrap(Val)->print(os);
847 else
848 os << "Printing <null> Value";
850 os.flush();
852 return strdup(buf.c_str());
855 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
856 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
859 int LLVMHasMetadata(LLVMValueRef Inst) {
860 return unwrap<Instruction>(Inst)->hasMetadata();
863 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
864 auto *I = unwrap<Instruction>(Inst);
865 assert(I && "Expected instruction");
866 if (auto *MD = I->getMetadata(KindID))
867 return wrap(MetadataAsValue::get(I->getContext(), MD));
868 return nullptr;
871 // MetadataAsValue uses a canonical format which strips the actual MDNode for
872 // MDNode with just a single constant value, storing just a ConstantAsMetadata
873 // This undoes this canonicalization, reconstructing the MDNode.
874 static MDNode *extractMDNode(MetadataAsValue *MAV) {
875 Metadata *MD = MAV->getMetadata();
876 assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
877 "Expected a metadata node or a canonicalized constant");
879 if (MDNode *N = dyn_cast<MDNode>(MD))
880 return N;
882 return MDNode::get(MAV->getContext(), MD);
885 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
886 MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
888 unwrap<Instruction>(Inst)->setMetadata(KindID, N);
891 struct LLVMOpaqueValueMetadataEntry {
892 unsigned Kind;
893 LLVMMetadataRef Metadata;
896 using MetadataEntries = SmallVectorImpl<std::pair<unsigned, MDNode *>>;
897 static LLVMValueMetadataEntry *
898 llvm_getMetadata(size_t *NumEntries,
899 llvm::function_ref<void(MetadataEntries &)> AccessMD) {
900 SmallVector<std::pair<unsigned, MDNode *>, 8> MVEs;
901 AccessMD(MVEs);
903 LLVMOpaqueValueMetadataEntry *Result =
904 static_cast<LLVMOpaqueValueMetadataEntry *>(
905 safe_malloc(MVEs.size() * sizeof(LLVMOpaqueValueMetadataEntry)));
906 for (unsigned i = 0; i < MVEs.size(); ++i) {
907 const auto &ModuleFlag = MVEs[i];
908 Result[i].Kind = ModuleFlag.first;
909 Result[i].Metadata = wrap(ModuleFlag.second);
911 *NumEntries = MVEs.size();
912 return Result;
915 LLVMValueMetadataEntry *
916 LLVMInstructionGetAllMetadataOtherThanDebugLoc(LLVMValueRef Value,
917 size_t *NumEntries) {
918 return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
919 unwrap<Instruction>(Value)->getAllMetadata(Entries);
923 /*--.. Conversion functions ................................................--*/
925 #define LLVM_DEFINE_VALUE_CAST(name) \
926 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
927 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
930 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
932 LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) {
933 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
934 if (isa<MDNode>(MD->getMetadata()) ||
935 isa<ValueAsMetadata>(MD->getMetadata()))
936 return Val;
937 return nullptr;
940 LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) {
941 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
942 if (isa<MDString>(MD->getMetadata()))
943 return Val;
944 return nullptr;
947 /*--.. Operations on Uses ..................................................--*/
948 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
949 Value *V = unwrap(Val);
950 Value::use_iterator I = V->use_begin();
951 if (I == V->use_end())
952 return nullptr;
953 return wrap(&*I);
956 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
957 Use *Next = unwrap(U)->getNext();
958 if (Next)
959 return wrap(Next);
960 return nullptr;
963 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
964 return wrap(unwrap(U)->getUser());
967 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
968 return wrap(unwrap(U)->get());
971 /*--.. Operations on Users .................................................--*/
973 static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N,
974 unsigned Index) {
975 Metadata *Op = N->getOperand(Index);
976 if (!Op)
977 return nullptr;
978 if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
979 return wrap(C->getValue());
980 return wrap(MetadataAsValue::get(Context, Op));
983 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
984 Value *V = unwrap(Val);
985 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
986 if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
987 assert(Index == 0 && "Function-local metadata can only have one operand");
988 return wrap(L->getValue());
990 return getMDNodeOperandImpl(V->getContext(),
991 cast<MDNode>(MD->getMetadata()), Index);
994 return wrap(cast<User>(V)->getOperand(Index));
997 LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) {
998 Value *V = unwrap(Val);
999 return wrap(&cast<User>(V)->getOperandUse(Index));
1002 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
1003 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
1006 int LLVMGetNumOperands(LLVMValueRef Val) {
1007 Value *V = unwrap(Val);
1008 if (isa<MetadataAsValue>(V))
1009 return LLVMGetMDNodeNumOperands(Val);
1011 return cast<User>(V)->getNumOperands();
1014 /*--.. Operations on constants of any type .................................--*/
1016 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
1017 return wrap(Constant::getNullValue(unwrap(Ty)));
1020 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
1021 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
1024 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
1025 return wrap(UndefValue::get(unwrap(Ty)));
1028 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
1029 return isa<Constant>(unwrap(Ty));
1032 LLVMBool LLVMIsNull(LLVMValueRef Val) {
1033 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
1034 return C->isNullValue();
1035 return false;
1038 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
1039 return isa<UndefValue>(unwrap(Val));
1042 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
1043 return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
1046 /*--.. Operations on metadata nodes ........................................--*/
1048 LLVMMetadataRef LLVMMDStringInContext2(LLVMContextRef C, const char *Str,
1049 size_t SLen) {
1050 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
1053 LLVMMetadataRef LLVMMDNodeInContext2(LLVMContextRef C, LLVMMetadataRef *MDs,
1054 size_t Count) {
1055 return wrap(MDNode::get(*unwrap(C), ArrayRef<Metadata*>(unwrap(MDs), Count)));
1058 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
1059 unsigned SLen) {
1060 LLVMContext &Context = *unwrap(C);
1061 return wrap(MetadataAsValue::get(
1062 Context, MDString::get(Context, StringRef(Str, SLen))));
1065 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
1066 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
1069 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
1070 unsigned Count) {
1071 LLVMContext &Context = *unwrap(C);
1072 SmallVector<Metadata *, 8> MDs;
1073 for (auto *OV : makeArrayRef(Vals, Count)) {
1074 Value *V = unwrap(OV);
1075 Metadata *MD;
1076 if (!V)
1077 MD = nullptr;
1078 else if (auto *C = dyn_cast<Constant>(V))
1079 MD = ConstantAsMetadata::get(C);
1080 else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
1081 MD = MDV->getMetadata();
1082 assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
1083 "outside of direct argument to call");
1084 } else {
1085 // This is function-local metadata. Pretend to make an MDNode.
1086 assert(Count == 1 &&
1087 "Expected only one operand to function-local metadata");
1088 return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V)));
1091 MDs.push_back(MD);
1093 return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs)));
1096 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
1097 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
1100 LLVMValueRef LLVMMetadataAsValue(LLVMContextRef C, LLVMMetadataRef MD) {
1101 return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
1104 LLVMMetadataRef LLVMValueAsMetadata(LLVMValueRef Val) {
1105 auto *V = unwrap(Val);
1106 if (auto *C = dyn_cast<Constant>(V))
1107 return wrap(ConstantAsMetadata::get(C));
1108 if (auto *MAV = dyn_cast<MetadataAsValue>(V))
1109 return wrap(MAV->getMetadata());
1110 return wrap(ValueAsMetadata::get(V));
1113 const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
1114 if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
1115 if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
1116 *Length = S->getString().size();
1117 return S->getString().data();
1119 *Length = 0;
1120 return nullptr;
1123 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) {
1124 auto *MD = cast<MetadataAsValue>(unwrap(V));
1125 if (isa<ValueAsMetadata>(MD->getMetadata()))
1126 return 1;
1127 return cast<MDNode>(MD->getMetadata())->getNumOperands();
1130 LLVMNamedMDNodeRef LLVMGetFirstNamedMetadata(LLVMModuleRef M) {
1131 Module *Mod = unwrap(M);
1132 Module::named_metadata_iterator I = Mod->named_metadata_begin();
1133 if (I == Mod->named_metadata_end())
1134 return nullptr;
1135 return wrap(&*I);
1138 LLVMNamedMDNodeRef LLVMGetLastNamedMetadata(LLVMModuleRef M) {
1139 Module *Mod = unwrap(M);
1140 Module::named_metadata_iterator I = Mod->named_metadata_end();
1141 if (I == Mod->named_metadata_begin())
1142 return nullptr;
1143 return wrap(&*--I);
1146 LLVMNamedMDNodeRef LLVMGetNextNamedMetadata(LLVMNamedMDNodeRef NMD) {
1147 NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD);
1148 Module::named_metadata_iterator I(NamedNode);
1149 if (++I == NamedNode->getParent()->named_metadata_end())
1150 return nullptr;
1151 return wrap(&*I);
1154 LLVMNamedMDNodeRef LLVMGetPreviousNamedMetadata(LLVMNamedMDNodeRef NMD) {
1155 NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD);
1156 Module::named_metadata_iterator I(NamedNode);
1157 if (I == NamedNode->getParent()->named_metadata_begin())
1158 return nullptr;
1159 return wrap(&*--I);
1162 LLVMNamedMDNodeRef LLVMGetNamedMetadata(LLVMModuleRef M,
1163 const char *Name, size_t NameLen) {
1164 return wrap(unwrap(M)->getNamedMetadata(StringRef(Name, NameLen)));
1167 LLVMNamedMDNodeRef LLVMGetOrInsertNamedMetadata(LLVMModuleRef M,
1168 const char *Name, size_t NameLen) {
1169 return wrap(unwrap(M)->getOrInsertNamedMetadata({Name, NameLen}));
1172 const char *LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NMD, size_t *NameLen) {
1173 NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD);
1174 *NameLen = NamedNode->getName().size();
1175 return NamedNode->getName().data();
1178 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) {
1179 auto *MD = cast<MetadataAsValue>(unwrap(V));
1180 if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1181 *Dest = wrap(MDV->getValue());
1182 return;
1184 const auto *N = cast<MDNode>(MD->getMetadata());
1185 const unsigned numOperands = N->getNumOperands();
1186 LLVMContext &Context = unwrap(V)->getContext();
1187 for (unsigned i = 0; i < numOperands; i++)
1188 Dest[i] = getMDNodeOperandImpl(Context, N, i);
1191 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char *Name) {
1192 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
1193 return N->getNumOperands();
1195 return 0;
1198 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char *Name,
1199 LLVMValueRef *Dest) {
1200 NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
1201 if (!N)
1202 return;
1203 LLVMContext &Context = unwrap(M)->getContext();
1204 for (unsigned i=0;i<N->getNumOperands();i++)
1205 Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
1208 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char *Name,
1209 LLVMValueRef Val) {
1210 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
1211 if (!N)
1212 return;
1213 if (!Val)
1214 return;
1215 N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
1218 const char *LLVMGetDebugLocDirectory(LLVMValueRef Val, unsigned *Length) {
1219 if (!Length) return nullptr;
1220 StringRef S;
1221 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1222 if (const auto &DL = I->getDebugLoc()) {
1223 S = DL->getDirectory();
1225 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1226 SmallVector<DIGlobalVariableExpression *, 1> GVEs;
1227 GV->getDebugInfo(GVEs);
1228 if (GVEs.size())
1229 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1230 S = DGV->getDirectory();
1231 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1232 if (const DISubprogram *DSP = F->getSubprogram())
1233 S = DSP->getDirectory();
1234 } else {
1235 assert(0 && "Expected Instruction, GlobalVariable or Function");
1236 return nullptr;
1238 *Length = S.size();
1239 return S.data();
1242 const char *LLVMGetDebugLocFilename(LLVMValueRef Val, unsigned *Length) {
1243 if (!Length) return nullptr;
1244 StringRef S;
1245 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1246 if (const auto &DL = I->getDebugLoc()) {
1247 S = DL->getFilename();
1249 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1250 SmallVector<DIGlobalVariableExpression *, 1> GVEs;
1251 GV->getDebugInfo(GVEs);
1252 if (GVEs.size())
1253 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1254 S = DGV->getFilename();
1255 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1256 if (const DISubprogram *DSP = F->getSubprogram())
1257 S = DSP->getFilename();
1258 } else {
1259 assert(0 && "Expected Instruction, GlobalVariable or Function");
1260 return nullptr;
1262 *Length = S.size();
1263 return S.data();
1266 unsigned LLVMGetDebugLocLine(LLVMValueRef Val) {
1267 unsigned L = 0;
1268 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1269 if (const auto &DL = I->getDebugLoc()) {
1270 L = DL->getLine();
1272 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1273 SmallVector<DIGlobalVariableExpression *, 1> GVEs;
1274 GV->getDebugInfo(GVEs);
1275 if (GVEs.size())
1276 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1277 L = DGV->getLine();
1278 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1279 if (const DISubprogram *DSP = F->getSubprogram())
1280 L = DSP->getLine();
1281 } else {
1282 assert(0 && "Expected Instruction, GlobalVariable or Function");
1283 return -1;
1285 return L;
1288 unsigned LLVMGetDebugLocColumn(LLVMValueRef Val) {
1289 unsigned C = 0;
1290 if (const auto *I = dyn_cast<Instruction>(unwrap(Val)))
1291 if (const auto &DL = I->getDebugLoc())
1292 C = DL->getColumn();
1293 return C;
1296 /*--.. Operations on scalar constants ......................................--*/
1298 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
1299 LLVMBool SignExtend) {
1300 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
1303 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
1304 unsigned NumWords,
1305 const uint64_t Words[]) {
1306 IntegerType *Ty = unwrap<IntegerType>(IntTy);
1307 return wrap(ConstantInt::get(Ty->getContext(),
1308 APInt(Ty->getBitWidth(),
1309 makeArrayRef(Words, NumWords))));
1312 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
1313 uint8_t Radix) {
1314 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
1315 Radix));
1318 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
1319 unsigned SLen, uint8_t Radix) {
1320 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
1321 Radix));
1324 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
1325 return wrap(ConstantFP::get(unwrap(RealTy), N));
1328 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
1329 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
1332 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
1333 unsigned SLen) {
1334 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
1337 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
1338 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
1341 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
1342 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
1345 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
1346 ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
1347 Type *Ty = cFP->getType();
1349 if (Ty->isFloatTy()) {
1350 *LosesInfo = false;
1351 return cFP->getValueAPF().convertToFloat();
1354 if (Ty->isDoubleTy()) {
1355 *LosesInfo = false;
1356 return cFP->getValueAPF().convertToDouble();
1359 bool APFLosesInfo;
1360 APFloat APF = cFP->getValueAPF();
1361 APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo);
1362 *LosesInfo = APFLosesInfo;
1363 return APF.convertToDouble();
1366 /*--.. Operations on composite constants ...................................--*/
1368 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
1369 unsigned Length,
1370 LLVMBool DontNullTerminate) {
1371 /* Inverted the sense of AddNull because ', 0)' is a
1372 better mnemonic for null termination than ', 1)'. */
1373 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
1374 DontNullTerminate == 0));
1377 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
1378 LLVMBool DontNullTerminate) {
1379 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
1380 DontNullTerminate);
1383 LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef C, unsigned idx) {
1384 return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
1387 LLVMBool LLVMIsConstantString(LLVMValueRef C) {
1388 return unwrap<ConstantDataSequential>(C)->isString();
1391 const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1392 StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1393 *Length = Str.size();
1394 return Str.data();
1397 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
1398 LLVMValueRef *ConstantVals, unsigned Length) {
1399 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1400 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1403 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
1404 LLVMValueRef *ConstantVals,
1405 unsigned Count, LLVMBool Packed) {
1406 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1407 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
1408 Packed != 0));
1411 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
1412 LLVMBool Packed) {
1413 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1414 Packed);
1417 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
1418 LLVMValueRef *ConstantVals,
1419 unsigned Count) {
1420 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1421 StructType *Ty = cast<StructType>(unwrap(StructTy));
1423 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
1426 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
1427 return wrap(ConstantVector::get(makeArrayRef(
1428 unwrap<Constant>(ScalarConstantVals, Size), Size)));
1431 /*-- Opcode mapping */
1433 static LLVMOpcode map_to_llvmopcode(int opcode)
1435 switch (opcode) {
1436 default: llvm_unreachable("Unhandled Opcode.");
1437 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
1438 #include "llvm/IR/Instruction.def"
1439 #undef HANDLE_INST
1443 static int map_from_llvmopcode(LLVMOpcode code)
1445 switch (code) {
1446 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
1447 #include "llvm/IR/Instruction.def"
1448 #undef HANDLE_INST
1450 llvm_unreachable("Unhandled Opcode.");
1453 /*--.. Constant expressions ................................................--*/
1455 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
1456 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
1459 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
1460 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
1463 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
1464 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
1467 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
1468 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1471 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
1472 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
1475 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
1476 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
1480 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
1481 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
1484 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
1485 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
1488 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1489 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
1490 unwrap<Constant>(RHSConstant)));
1493 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
1494 LLVMValueRef RHSConstant) {
1495 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
1496 unwrap<Constant>(RHSConstant)));
1499 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
1500 LLVMValueRef RHSConstant) {
1501 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
1502 unwrap<Constant>(RHSConstant)));
1505 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1506 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
1507 unwrap<Constant>(RHSConstant)));
1510 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1511 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
1512 unwrap<Constant>(RHSConstant)));
1515 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
1516 LLVMValueRef RHSConstant) {
1517 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
1518 unwrap<Constant>(RHSConstant)));
1521 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
1522 LLVMValueRef RHSConstant) {
1523 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
1524 unwrap<Constant>(RHSConstant)));
1527 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1528 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
1529 unwrap<Constant>(RHSConstant)));
1532 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1533 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
1534 unwrap<Constant>(RHSConstant)));
1537 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
1538 LLVMValueRef RHSConstant) {
1539 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
1540 unwrap<Constant>(RHSConstant)));
1543 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
1544 LLVMValueRef RHSConstant) {
1545 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
1546 unwrap<Constant>(RHSConstant)));
1549 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1550 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
1551 unwrap<Constant>(RHSConstant)));
1554 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1555 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
1556 unwrap<Constant>(RHSConstant)));
1559 LLVMValueRef LLVMConstExactUDiv(LLVMValueRef LHSConstant,
1560 LLVMValueRef RHSConstant) {
1561 return wrap(ConstantExpr::getExactUDiv(unwrap<Constant>(LHSConstant),
1562 unwrap<Constant>(RHSConstant)));
1565 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1566 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
1567 unwrap<Constant>(RHSConstant)));
1570 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
1571 LLVMValueRef RHSConstant) {
1572 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
1573 unwrap<Constant>(RHSConstant)));
1576 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1577 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
1578 unwrap<Constant>(RHSConstant)));
1581 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1582 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
1583 unwrap<Constant>(RHSConstant)));
1586 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1587 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
1588 unwrap<Constant>(RHSConstant)));
1591 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1592 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
1593 unwrap<Constant>(RHSConstant)));
1596 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1597 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
1598 unwrap<Constant>(RHSConstant)));
1601 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1602 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
1603 unwrap<Constant>(RHSConstant)));
1606 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1607 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
1608 unwrap<Constant>(RHSConstant)));
1611 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
1612 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1613 return wrap(ConstantExpr::getICmp(Predicate,
1614 unwrap<Constant>(LHSConstant),
1615 unwrap<Constant>(RHSConstant)));
1618 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
1619 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1620 return wrap(ConstantExpr::getFCmp(Predicate,
1621 unwrap<Constant>(LHSConstant),
1622 unwrap<Constant>(RHSConstant)));
1625 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1626 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1627 unwrap<Constant>(RHSConstant)));
1630 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1631 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
1632 unwrap<Constant>(RHSConstant)));
1635 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1636 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1637 unwrap<Constant>(RHSConstant)));
1640 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
1641 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1642 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1643 NumIndices);
1644 Constant *Val = unwrap<Constant>(ConstantVal);
1645 Type *Ty =
1646 cast<PointerType>(Val->getType()->getScalarType())->getElementType();
1647 return wrap(ConstantExpr::getGetElementPtr(Ty, Val, IdxList));
1650 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
1651 LLVMValueRef *ConstantIndices,
1652 unsigned NumIndices) {
1653 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1654 NumIndices);
1655 Constant *Val = unwrap<Constant>(ConstantVal);
1656 Type *Ty =
1657 cast<PointerType>(Val->getType()->getScalarType())->getElementType();
1658 return wrap(ConstantExpr::getInBoundsGetElementPtr(Ty, Val, IdxList));
1661 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1662 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1663 unwrap(ToType)));
1666 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1667 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1668 unwrap(ToType)));
1671 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1672 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1673 unwrap(ToType)));
1676 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1677 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1678 unwrap(ToType)));
1681 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1682 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1683 unwrap(ToType)));
1686 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1687 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1688 unwrap(ToType)));
1691 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1692 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1693 unwrap(ToType)));
1696 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1697 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1698 unwrap(ToType)));
1701 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1702 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1703 unwrap(ToType)));
1706 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1707 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1708 unwrap(ToType)));
1711 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1712 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1713 unwrap(ToType)));
1716 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1717 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1718 unwrap(ToType)));
1721 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1722 LLVMTypeRef ToType) {
1723 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1724 unwrap(ToType)));
1727 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1728 LLVMTypeRef ToType) {
1729 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1730 unwrap(ToType)));
1733 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1734 LLVMTypeRef ToType) {
1735 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1736 unwrap(ToType)));
1739 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1740 LLVMTypeRef ToType) {
1741 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1742 unwrap(ToType)));
1745 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1746 LLVMTypeRef ToType) {
1747 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1748 unwrap(ToType)));
1751 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1752 LLVMBool isSigned) {
1753 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1754 unwrap(ToType), isSigned));
1757 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1758 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1759 unwrap(ToType)));
1762 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1763 LLVMValueRef ConstantIfTrue,
1764 LLVMValueRef ConstantIfFalse) {
1765 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1766 unwrap<Constant>(ConstantIfTrue),
1767 unwrap<Constant>(ConstantIfFalse)));
1770 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1771 LLVMValueRef IndexConstant) {
1772 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1773 unwrap<Constant>(IndexConstant)));
1776 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1777 LLVMValueRef ElementValueConstant,
1778 LLVMValueRef IndexConstant) {
1779 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1780 unwrap<Constant>(ElementValueConstant),
1781 unwrap<Constant>(IndexConstant)));
1784 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1785 LLVMValueRef VectorBConstant,
1786 LLVMValueRef MaskConstant) {
1787 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1788 unwrap<Constant>(VectorBConstant),
1789 unwrap<Constant>(MaskConstant)));
1792 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1793 unsigned NumIdx) {
1794 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1795 makeArrayRef(IdxList, NumIdx)));
1798 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1799 LLVMValueRef ElementValueConstant,
1800 unsigned *IdxList, unsigned NumIdx) {
1801 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1802 unwrap<Constant>(ElementValueConstant),
1803 makeArrayRef(IdxList, NumIdx)));
1806 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1807 const char *Constraints,
1808 LLVMBool HasSideEffects,
1809 LLVMBool IsAlignStack) {
1810 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1811 Constraints, HasSideEffects, IsAlignStack));
1814 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1815 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1818 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1820 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1821 return wrap(unwrap<GlobalValue>(Global)->getParent());
1824 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1825 return unwrap<GlobalValue>(Global)->isDeclaration();
1828 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1829 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1830 case GlobalValue::ExternalLinkage:
1831 return LLVMExternalLinkage;
1832 case GlobalValue::AvailableExternallyLinkage:
1833 return LLVMAvailableExternallyLinkage;
1834 case GlobalValue::LinkOnceAnyLinkage:
1835 return LLVMLinkOnceAnyLinkage;
1836 case GlobalValue::LinkOnceODRLinkage:
1837 return LLVMLinkOnceODRLinkage;
1838 case GlobalValue::WeakAnyLinkage:
1839 return LLVMWeakAnyLinkage;
1840 case GlobalValue::WeakODRLinkage:
1841 return LLVMWeakODRLinkage;
1842 case GlobalValue::AppendingLinkage:
1843 return LLVMAppendingLinkage;
1844 case GlobalValue::InternalLinkage:
1845 return LLVMInternalLinkage;
1846 case GlobalValue::PrivateLinkage:
1847 return LLVMPrivateLinkage;
1848 case GlobalValue::ExternalWeakLinkage:
1849 return LLVMExternalWeakLinkage;
1850 case GlobalValue::CommonLinkage:
1851 return LLVMCommonLinkage;
1854 llvm_unreachable("Invalid GlobalValue linkage!");
1857 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1858 GlobalValue *GV = unwrap<GlobalValue>(Global);
1860 switch (Linkage) {
1861 case LLVMExternalLinkage:
1862 GV->setLinkage(GlobalValue::ExternalLinkage);
1863 break;
1864 case LLVMAvailableExternallyLinkage:
1865 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1866 break;
1867 case LLVMLinkOnceAnyLinkage:
1868 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1869 break;
1870 case LLVMLinkOnceODRLinkage:
1871 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1872 break;
1873 case LLVMLinkOnceODRAutoHideLinkage:
1874 LLVM_DEBUG(
1875 errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1876 "longer supported.");
1877 break;
1878 case LLVMWeakAnyLinkage:
1879 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1880 break;
1881 case LLVMWeakODRLinkage:
1882 GV->setLinkage(GlobalValue::WeakODRLinkage);
1883 break;
1884 case LLVMAppendingLinkage:
1885 GV->setLinkage(GlobalValue::AppendingLinkage);
1886 break;
1887 case LLVMInternalLinkage:
1888 GV->setLinkage(GlobalValue::InternalLinkage);
1889 break;
1890 case LLVMPrivateLinkage:
1891 GV->setLinkage(GlobalValue::PrivateLinkage);
1892 break;
1893 case LLVMLinkerPrivateLinkage:
1894 GV->setLinkage(GlobalValue::PrivateLinkage);
1895 break;
1896 case LLVMLinkerPrivateWeakLinkage:
1897 GV->setLinkage(GlobalValue::PrivateLinkage);
1898 break;
1899 case LLVMDLLImportLinkage:
1900 LLVM_DEBUG(
1901 errs()
1902 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1903 break;
1904 case LLVMDLLExportLinkage:
1905 LLVM_DEBUG(
1906 errs()
1907 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1908 break;
1909 case LLVMExternalWeakLinkage:
1910 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1911 break;
1912 case LLVMGhostLinkage:
1913 LLVM_DEBUG(
1914 errs() << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1915 break;
1916 case LLVMCommonLinkage:
1917 GV->setLinkage(GlobalValue::CommonLinkage);
1918 break;
1922 const char *LLVMGetSection(LLVMValueRef Global) {
1923 // Using .data() is safe because of how GlobalObject::setSection is
1924 // implemented.
1925 return unwrap<GlobalValue>(Global)->getSection().data();
1928 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1929 unwrap<GlobalObject>(Global)->setSection(Section);
1932 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1933 return static_cast<LLVMVisibility>(
1934 unwrap<GlobalValue>(Global)->getVisibility());
1937 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1938 unwrap<GlobalValue>(Global)
1939 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1942 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
1943 return static_cast<LLVMDLLStorageClass>(
1944 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1947 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
1948 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1949 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1952 LLVMUnnamedAddr LLVMGetUnnamedAddress(LLVMValueRef Global) {
1953 switch (unwrap<GlobalValue>(Global)->getUnnamedAddr()) {
1954 case GlobalVariable::UnnamedAddr::None:
1955 return LLVMNoUnnamedAddr;
1956 case GlobalVariable::UnnamedAddr::Local:
1957 return LLVMLocalUnnamedAddr;
1958 case GlobalVariable::UnnamedAddr::Global:
1959 return LLVMGlobalUnnamedAddr;
1961 llvm_unreachable("Unknown UnnamedAddr kind!");
1964 void LLVMSetUnnamedAddress(LLVMValueRef Global, LLVMUnnamedAddr UnnamedAddr) {
1965 GlobalValue *GV = unwrap<GlobalValue>(Global);
1967 switch (UnnamedAddr) {
1968 case LLVMNoUnnamedAddr:
1969 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::None);
1970 case LLVMLocalUnnamedAddr:
1971 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Local);
1972 case LLVMGlobalUnnamedAddr:
1973 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Global);
1977 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
1978 return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
1981 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
1982 unwrap<GlobalValue>(Global)->setUnnamedAddr(
1983 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
1984 : GlobalValue::UnnamedAddr::None);
1987 LLVMTypeRef LLVMGlobalGetValueType(LLVMValueRef Global) {
1988 return wrap(unwrap<GlobalValue>(Global)->getValueType());
1991 /*--.. Operations on global variables, load and store instructions .........--*/
1993 unsigned LLVMGetAlignment(LLVMValueRef V) {
1994 Value *P = unwrap<Value>(V);
1995 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1996 return GV->getAlignment();
1997 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1998 return AI->getAlignment();
1999 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2000 return LI->getAlignment();
2001 if (StoreInst *SI = dyn_cast<StoreInst>(P))
2002 return SI->getAlignment();
2004 llvm_unreachable(
2005 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
2008 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
2009 Value *P = unwrap<Value>(V);
2010 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2011 GV->setAlignment(MaybeAlign(Bytes));
2012 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
2013 AI->setAlignment(MaybeAlign(Bytes));
2014 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
2015 LI->setAlignment(MaybeAlign(Bytes));
2016 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
2017 SI->setAlignment(MaybeAlign(Bytes));
2018 else
2019 llvm_unreachable(
2020 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
2023 LLVMValueMetadataEntry *LLVMGlobalCopyAllMetadata(LLVMValueRef Value,
2024 size_t *NumEntries) {
2025 return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
2026 if (Instruction *Instr = dyn_cast<Instruction>(unwrap(Value))) {
2027 Instr->getAllMetadata(Entries);
2028 } else {
2029 unwrap<GlobalObject>(Value)->getAllMetadata(Entries);
2034 unsigned LLVMValueMetadataEntriesGetKind(LLVMValueMetadataEntry *Entries,
2035 unsigned Index) {
2036 LLVMOpaqueValueMetadataEntry MVE =
2037 static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2038 return MVE.Kind;
2041 LLVMMetadataRef
2042 LLVMValueMetadataEntriesGetMetadata(LLVMValueMetadataEntry *Entries,
2043 unsigned Index) {
2044 LLVMOpaqueValueMetadataEntry MVE =
2045 static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2046 return MVE.Metadata;
2049 void LLVMDisposeValueMetadataEntries(LLVMValueMetadataEntry *Entries) {
2050 free(Entries);
2053 void LLVMGlobalSetMetadata(LLVMValueRef Global, unsigned Kind,
2054 LLVMMetadataRef MD) {
2055 unwrap<GlobalObject>(Global)->setMetadata(Kind, unwrap<MDNode>(MD));
2058 void LLVMGlobalEraseMetadata(LLVMValueRef Global, unsigned Kind) {
2059 unwrap<GlobalObject>(Global)->eraseMetadata(Kind);
2062 void LLVMGlobalClearMetadata(LLVMValueRef Global) {
2063 unwrap<GlobalObject>(Global)->clearMetadata();
2066 /*--.. Operations on global variables ......................................--*/
2068 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
2069 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2070 GlobalValue::ExternalLinkage, nullptr, Name));
2073 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
2074 const char *Name,
2075 unsigned AddressSpace) {
2076 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2077 GlobalValue::ExternalLinkage, nullptr, Name,
2078 nullptr, GlobalVariable::NotThreadLocal,
2079 AddressSpace));
2082 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
2083 return wrap(unwrap(M)->getNamedGlobal(Name));
2086 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
2087 Module *Mod = unwrap(M);
2088 Module::global_iterator I = Mod->global_begin();
2089 if (I == Mod->global_end())
2090 return nullptr;
2091 return wrap(&*I);
2094 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
2095 Module *Mod = unwrap(M);
2096 Module::global_iterator I = Mod->global_end();
2097 if (I == Mod->global_begin())
2098 return nullptr;
2099 return wrap(&*--I);
2102 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
2103 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2104 Module::global_iterator I(GV);
2105 if (++I == GV->getParent()->global_end())
2106 return nullptr;
2107 return wrap(&*I);
2110 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
2111 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2112 Module::global_iterator I(GV);
2113 if (I == GV->getParent()->global_begin())
2114 return nullptr;
2115 return wrap(&*--I);
2118 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
2119 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
2122 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
2123 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
2124 if ( !GV->hasInitializer() )
2125 return nullptr;
2126 return wrap(GV->getInitializer());
2129 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
2130 unwrap<GlobalVariable>(GlobalVar)
2131 ->setInitializer(unwrap<Constant>(ConstantVal));
2134 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
2135 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
2138 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
2139 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
2142 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
2143 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
2146 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
2147 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
2150 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
2151 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
2152 case GlobalVariable::NotThreadLocal:
2153 return LLVMNotThreadLocal;
2154 case GlobalVariable::GeneralDynamicTLSModel:
2155 return LLVMGeneralDynamicTLSModel;
2156 case GlobalVariable::LocalDynamicTLSModel:
2157 return LLVMLocalDynamicTLSModel;
2158 case GlobalVariable::InitialExecTLSModel:
2159 return LLVMInitialExecTLSModel;
2160 case GlobalVariable::LocalExecTLSModel:
2161 return LLVMLocalExecTLSModel;
2164 llvm_unreachable("Invalid GlobalVariable thread local mode");
2167 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
2168 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2170 switch (Mode) {
2171 case LLVMNotThreadLocal:
2172 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
2173 break;
2174 case LLVMGeneralDynamicTLSModel:
2175 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
2176 break;
2177 case LLVMLocalDynamicTLSModel:
2178 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
2179 break;
2180 case LLVMInitialExecTLSModel:
2181 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
2182 break;
2183 case LLVMLocalExecTLSModel:
2184 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
2185 break;
2189 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
2190 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
2193 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
2194 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
2197 /*--.. Operations on aliases ......................................--*/
2199 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
2200 const char *Name) {
2201 auto *PTy = cast<PointerType>(unwrap(Ty));
2202 return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
2203 GlobalValue::ExternalLinkage, Name,
2204 unwrap<Constant>(Aliasee), unwrap(M)));
2207 LLVMValueRef LLVMGetNamedGlobalAlias(LLVMModuleRef M,
2208 const char *Name, size_t NameLen) {
2209 return wrap(unwrap(M)->getNamedAlias(Name));
2212 LLVMValueRef LLVMGetFirstGlobalAlias(LLVMModuleRef M) {
2213 Module *Mod = unwrap(M);
2214 Module::alias_iterator I = Mod->alias_begin();
2215 if (I == Mod->alias_end())
2216 return nullptr;
2217 return wrap(&*I);
2220 LLVMValueRef LLVMGetLastGlobalAlias(LLVMModuleRef M) {
2221 Module *Mod = unwrap(M);
2222 Module::alias_iterator I = Mod->alias_end();
2223 if (I == Mod->alias_begin())
2224 return nullptr;
2225 return wrap(&*--I);
2228 LLVMValueRef LLVMGetNextGlobalAlias(LLVMValueRef GA) {
2229 GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2230 Module::alias_iterator I(Alias);
2231 if (++I == Alias->getParent()->alias_end())
2232 return nullptr;
2233 return wrap(&*I);
2236 LLVMValueRef LLVMGetPreviousGlobalAlias(LLVMValueRef GA) {
2237 GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2238 Module::alias_iterator I(Alias);
2239 if (I == Alias->getParent()->alias_begin())
2240 return nullptr;
2241 return wrap(&*--I);
2244 LLVMValueRef LLVMAliasGetAliasee(LLVMValueRef Alias) {
2245 return wrap(unwrap<GlobalAlias>(Alias)->getAliasee());
2248 void LLVMAliasSetAliasee(LLVMValueRef Alias, LLVMValueRef Aliasee) {
2249 unwrap<GlobalAlias>(Alias)->setAliasee(unwrap<Constant>(Aliasee));
2252 /*--.. Operations on functions .............................................--*/
2254 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
2255 LLVMTypeRef FunctionTy) {
2256 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
2257 GlobalValue::ExternalLinkage, Name, unwrap(M)));
2260 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
2261 return wrap(unwrap(M)->getFunction(Name));
2264 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
2265 Module *Mod = unwrap(M);
2266 Module::iterator I = Mod->begin();
2267 if (I == Mod->end())
2268 return nullptr;
2269 return wrap(&*I);
2272 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
2273 Module *Mod = unwrap(M);
2274 Module::iterator I = Mod->end();
2275 if (I == Mod->begin())
2276 return nullptr;
2277 return wrap(&*--I);
2280 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
2281 Function *Func = unwrap<Function>(Fn);
2282 Module::iterator I(Func);
2283 if (++I == Func->getParent()->end())
2284 return nullptr;
2285 return wrap(&*I);
2288 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
2289 Function *Func = unwrap<Function>(Fn);
2290 Module::iterator I(Func);
2291 if (I == Func->getParent()->begin())
2292 return nullptr;
2293 return wrap(&*--I);
2296 void LLVMDeleteFunction(LLVMValueRef Fn) {
2297 unwrap<Function>(Fn)->eraseFromParent();
2300 LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) {
2301 return unwrap<Function>(Fn)->hasPersonalityFn();
2304 LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) {
2305 return wrap(unwrap<Function>(Fn)->getPersonalityFn());
2308 void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) {
2309 unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
2312 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
2313 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
2314 return F->getIntrinsicID();
2315 return 0;
2318 static Intrinsic::ID llvm_map_to_intrinsic_id(unsigned ID) {
2319 assert(ID < llvm::Intrinsic::num_intrinsics && "Intrinsic ID out of range");
2320 return llvm::Intrinsic::ID(ID);
2323 LLVMValueRef LLVMGetIntrinsicDeclaration(LLVMModuleRef Mod,
2324 unsigned ID,
2325 LLVMTypeRef *ParamTypes,
2326 size_t ParamCount) {
2327 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2328 auto IID = llvm_map_to_intrinsic_id(ID);
2329 return wrap(llvm::Intrinsic::getDeclaration(unwrap(Mod), IID, Tys));
2332 const char *LLVMIntrinsicGetName(unsigned ID, size_t *NameLength) {
2333 auto IID = llvm_map_to_intrinsic_id(ID);
2334 auto Str = llvm::Intrinsic::getName(IID);
2335 *NameLength = Str.size();
2336 return Str.data();
2339 LLVMTypeRef LLVMIntrinsicGetType(LLVMContextRef Ctx, unsigned ID,
2340 LLVMTypeRef *ParamTypes, size_t ParamCount) {
2341 auto IID = llvm_map_to_intrinsic_id(ID);
2342 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2343 return wrap(llvm::Intrinsic::getType(*unwrap(Ctx), IID, Tys));
2346 const char *LLVMIntrinsicCopyOverloadedName(unsigned ID,
2347 LLVMTypeRef *ParamTypes,
2348 size_t ParamCount,
2349 size_t *NameLength) {
2350 auto IID = llvm_map_to_intrinsic_id(ID);
2351 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2352 auto Str = llvm::Intrinsic::getName(IID, Tys);
2353 *NameLength = Str.length();
2354 return strdup(Str.c_str());
2357 unsigned LLVMLookupIntrinsicID(const char *Name, size_t NameLen) {
2358 return Function::lookupIntrinsicID({Name, NameLen});
2361 LLVMBool LLVMIntrinsicIsOverloaded(unsigned ID) {
2362 auto IID = llvm_map_to_intrinsic_id(ID);
2363 return llvm::Intrinsic::isOverloaded(IID);
2366 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
2367 return unwrap<Function>(Fn)->getCallingConv();
2370 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
2371 return unwrap<Function>(Fn)->setCallingConv(
2372 static_cast<CallingConv::ID>(CC));
2375 const char *LLVMGetGC(LLVMValueRef Fn) {
2376 Function *F = unwrap<Function>(Fn);
2377 return F->hasGC()? F->getGC().c_str() : nullptr;
2380 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
2381 Function *F = unwrap<Function>(Fn);
2382 if (GC)
2383 F->setGC(GC);
2384 else
2385 F->clearGC();
2388 void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2389 LLVMAttributeRef A) {
2390 unwrap<Function>(F)->addAttribute(Idx, unwrap(A));
2393 unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) {
2394 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2395 return AS.getNumAttributes();
2398 void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2399 LLVMAttributeRef *Attrs) {
2400 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2401 for (auto A : AS)
2402 *Attrs++ = wrap(A);
2405 LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F,
2406 LLVMAttributeIndex Idx,
2407 unsigned KindID) {
2408 return wrap(unwrap<Function>(F)->getAttribute(Idx,
2409 (Attribute::AttrKind)KindID));
2412 LLVMAttributeRef LLVMGetStringAttributeAtIndex(LLVMValueRef F,
2413 LLVMAttributeIndex Idx,
2414 const char *K, unsigned KLen) {
2415 return wrap(unwrap<Function>(F)->getAttribute(Idx, StringRef(K, KLen)));
2418 void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2419 unsigned KindID) {
2420 unwrap<Function>(F)->removeAttribute(Idx, (Attribute::AttrKind)KindID);
2423 void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2424 const char *K, unsigned KLen) {
2425 unwrap<Function>(F)->removeAttribute(Idx, StringRef(K, KLen));
2428 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
2429 const char *V) {
2430 Function *Func = unwrap<Function>(Fn);
2431 Attribute Attr = Attribute::get(Func->getContext(), A, V);
2432 Func->addAttribute(AttributeList::FunctionIndex, Attr);
2435 /*--.. Operations on parameters ............................................--*/
2437 unsigned LLVMCountParams(LLVMValueRef FnRef) {
2438 // This function is strictly redundant to
2439 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
2440 return unwrap<Function>(FnRef)->arg_size();
2443 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
2444 Function *Fn = unwrap<Function>(FnRef);
2445 for (Function::arg_iterator I = Fn->arg_begin(),
2446 E = Fn->arg_end(); I != E; I++)
2447 *ParamRefs++ = wrap(&*I);
2450 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
2451 Function *Fn = unwrap<Function>(FnRef);
2452 return wrap(&Fn->arg_begin()[index]);
2455 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
2456 return wrap(unwrap<Argument>(V)->getParent());
2459 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
2460 Function *Func = unwrap<Function>(Fn);
2461 Function::arg_iterator I = Func->arg_begin();
2462 if (I == Func->arg_end())
2463 return nullptr;
2464 return wrap(&*I);
2467 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
2468 Function *Func = unwrap<Function>(Fn);
2469 Function::arg_iterator I = Func->arg_end();
2470 if (I == Func->arg_begin())
2471 return nullptr;
2472 return wrap(&*--I);
2475 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
2476 Argument *A = unwrap<Argument>(Arg);
2477 Function *Fn = A->getParent();
2478 if (A->getArgNo() + 1 >= Fn->arg_size())
2479 return nullptr;
2480 return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
2483 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
2484 Argument *A = unwrap<Argument>(Arg);
2485 if (A->getArgNo() == 0)
2486 return nullptr;
2487 return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
2490 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
2491 Argument *A = unwrap<Argument>(Arg);
2492 A->addAttr(Attribute::getWithAlignment(A->getContext(), Align(align)));
2495 /*--.. Operations on ifuncs ................................................--*/
2497 LLVMValueRef LLVMAddGlobalIFunc(LLVMModuleRef M,
2498 const char *Name, size_t NameLen,
2499 LLVMTypeRef Ty, unsigned AddrSpace,
2500 LLVMValueRef Resolver) {
2501 return wrap(GlobalIFunc::create(unwrap(Ty), AddrSpace,
2502 GlobalValue::ExternalLinkage,
2503 StringRef(Name, NameLen),
2504 unwrap<Constant>(Resolver), unwrap(M)));
2507 LLVMValueRef LLVMGetNamedGlobalIFunc(LLVMModuleRef M,
2508 const char *Name, size_t NameLen) {
2509 return wrap(unwrap(M)->getNamedIFunc(StringRef(Name, NameLen)));
2512 LLVMValueRef LLVMGetFirstGlobalIFunc(LLVMModuleRef M) {
2513 Module *Mod = unwrap(M);
2514 Module::ifunc_iterator I = Mod->ifunc_begin();
2515 if (I == Mod->ifunc_end())
2516 return nullptr;
2517 return wrap(&*I);
2520 LLVMValueRef LLVMGetLastGlobalIFunc(LLVMModuleRef M) {
2521 Module *Mod = unwrap(M);
2522 Module::ifunc_iterator I = Mod->ifunc_end();
2523 if (I == Mod->ifunc_begin())
2524 return nullptr;
2525 return wrap(&*--I);
2528 LLVMValueRef LLVMGetNextGlobalIFunc(LLVMValueRef IFunc) {
2529 GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2530 Module::ifunc_iterator I(GIF);
2531 if (++I == GIF->getParent()->ifunc_end())
2532 return nullptr;
2533 return wrap(&*I);
2536 LLVMValueRef LLVMGetPreviousGlobalIFunc(LLVMValueRef IFunc) {
2537 GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2538 Module::ifunc_iterator I(GIF);
2539 if (I == GIF->getParent()->ifunc_begin())
2540 return nullptr;
2541 return wrap(&*--I);
2544 LLVMValueRef LLVMGetGlobalIFuncResolver(LLVMValueRef IFunc) {
2545 return wrap(unwrap<GlobalIFunc>(IFunc)->getResolver());
2548 void LLVMSetGlobalIFuncResolver(LLVMValueRef IFunc, LLVMValueRef Resolver) {
2549 unwrap<GlobalIFunc>(IFunc)->setResolver(unwrap<Constant>(Resolver));
2552 void LLVMEraseGlobalIFunc(LLVMValueRef IFunc) {
2553 unwrap<GlobalIFunc>(IFunc)->eraseFromParent();
2556 void LLVMRemoveGlobalIFunc(LLVMValueRef IFunc) {
2557 unwrap<GlobalIFunc>(IFunc)->removeFromParent();
2560 /*--.. Operations on basic blocks ..........................................--*/
2562 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
2563 return wrap(static_cast<Value*>(unwrap(BB)));
2566 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
2567 return isa<BasicBlock>(unwrap(Val));
2570 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
2571 return wrap(unwrap<BasicBlock>(Val));
2574 const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) {
2575 return unwrap(BB)->getName().data();
2578 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
2579 return wrap(unwrap(BB)->getParent());
2582 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
2583 return wrap(unwrap(BB)->getTerminator());
2586 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
2587 return unwrap<Function>(FnRef)->size();
2590 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
2591 Function *Fn = unwrap<Function>(FnRef);
2592 for (BasicBlock &BB : *Fn)
2593 *BasicBlocksRefs++ = wrap(&BB);
2596 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
2597 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2600 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
2601 Function *Func = unwrap<Function>(Fn);
2602 Function::iterator I = Func->begin();
2603 if (I == Func->end())
2604 return nullptr;
2605 return wrap(&*I);
2608 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
2609 Function *Func = unwrap<Function>(Fn);
2610 Function::iterator I = Func->end();
2611 if (I == Func->begin())
2612 return nullptr;
2613 return wrap(&*--I);
2616 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
2617 BasicBlock *Block = unwrap(BB);
2618 Function::iterator I(Block);
2619 if (++I == Block->getParent()->end())
2620 return nullptr;
2621 return wrap(&*I);
2624 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
2625 BasicBlock *Block = unwrap(BB);
2626 Function::iterator I(Block);
2627 if (I == Block->getParent()->begin())
2628 return nullptr;
2629 return wrap(&*--I);
2632 LLVMBasicBlockRef LLVMCreateBasicBlockInContext(LLVMContextRef C,
2633 const char *Name) {
2634 return wrap(llvm::BasicBlock::Create(*unwrap(C), Name));
2637 void LLVMInsertExistingBasicBlockAfterInsertBlock(LLVMBuilderRef Builder,
2638 LLVMBasicBlockRef BB) {
2639 BasicBlock *ToInsert = unwrap(BB);
2640 BasicBlock *CurBB = unwrap(Builder)->GetInsertBlock();
2641 assert(CurBB && "current insertion point is invalid!");
2642 CurBB->getParent()->getBasicBlockList().insertAfter(CurBB->getIterator(),
2643 ToInsert);
2646 void LLVMAppendExistingBasicBlock(LLVMValueRef Fn,
2647 LLVMBasicBlockRef BB) {
2648 unwrap<Function>(Fn)->getBasicBlockList().push_back(unwrap(BB));
2651 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
2652 LLVMValueRef FnRef,
2653 const char *Name) {
2654 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
2657 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
2658 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
2661 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
2662 LLVMBasicBlockRef BBRef,
2663 const char *Name) {
2664 BasicBlock *BB = unwrap(BBRef);
2665 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2668 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
2669 const char *Name) {
2670 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
2673 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
2674 unwrap(BBRef)->eraseFromParent();
2677 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
2678 unwrap(BBRef)->removeFromParent();
2681 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2682 unwrap(BB)->moveBefore(unwrap(MovePos));
2685 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2686 unwrap(BB)->moveAfter(unwrap(MovePos));
2689 /*--.. Operations on instructions ..........................................--*/
2691 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
2692 return wrap(unwrap<Instruction>(Inst)->getParent());
2695 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
2696 BasicBlock *Block = unwrap(BB);
2697 BasicBlock::iterator I = Block->begin();
2698 if (I == Block->end())
2699 return nullptr;
2700 return wrap(&*I);
2703 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
2704 BasicBlock *Block = unwrap(BB);
2705 BasicBlock::iterator I = Block->end();
2706 if (I == Block->begin())
2707 return nullptr;
2708 return wrap(&*--I);
2711 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
2712 Instruction *Instr = unwrap<Instruction>(Inst);
2713 BasicBlock::iterator I(Instr);
2714 if (++I == Instr->getParent()->end())
2715 return nullptr;
2716 return wrap(&*I);
2719 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
2720 Instruction *Instr = unwrap<Instruction>(Inst);
2721 BasicBlock::iterator I(Instr);
2722 if (I == Instr->getParent()->begin())
2723 return nullptr;
2724 return wrap(&*--I);
2727 void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) {
2728 unwrap<Instruction>(Inst)->removeFromParent();
2731 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
2732 unwrap<Instruction>(Inst)->eraseFromParent();
2735 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
2736 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2737 return (LLVMIntPredicate)I->getPredicate();
2738 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2739 if (CE->getOpcode() == Instruction::ICmp)
2740 return (LLVMIntPredicate)CE->getPredicate();
2741 return (LLVMIntPredicate)0;
2744 LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
2745 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2746 return (LLVMRealPredicate)I->getPredicate();
2747 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2748 if (CE->getOpcode() == Instruction::FCmp)
2749 return (LLVMRealPredicate)CE->getPredicate();
2750 return (LLVMRealPredicate)0;
2753 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
2754 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2755 return map_to_llvmopcode(C->getOpcode());
2756 return (LLVMOpcode)0;
2759 LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {
2760 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2761 return wrap(C->clone());
2762 return nullptr;
2765 LLVMValueRef LLVMIsATerminatorInst(LLVMValueRef Inst) {
2766 Instruction *I = dyn_cast<Instruction>(unwrap(Inst));
2767 return (I && I->isTerminator()) ? wrap(I) : nullptr;
2770 unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) {
2771 if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) {
2772 return FPI->getNumArgOperands();
2774 return unwrap<CallBase>(Instr)->getNumArgOperands();
2777 /*--.. Call and invoke instructions ........................................--*/
2779 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
2780 return unwrap<CallBase>(Instr)->getCallingConv();
2783 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
2784 return unwrap<CallBase>(Instr)->setCallingConv(
2785 static_cast<CallingConv::ID>(CC));
2788 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
2789 unsigned align) {
2790 auto *Call = unwrap<CallBase>(Instr);
2791 Attribute AlignAttr =
2792 Attribute::getWithAlignment(Call->getContext(), Align(align));
2793 Call->addAttribute(index, AlignAttr);
2796 void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2797 LLVMAttributeRef A) {
2798 unwrap<CallBase>(C)->addAttribute(Idx, unwrap(A));
2801 unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C,
2802 LLVMAttributeIndex Idx) {
2803 auto *Call = unwrap<CallBase>(C);
2804 auto AS = Call->getAttributes().getAttributes(Idx);
2805 return AS.getNumAttributes();
2808 void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx,
2809 LLVMAttributeRef *Attrs) {
2810 auto *Call = unwrap<CallBase>(C);
2811 auto AS = Call->getAttributes().getAttributes(Idx);
2812 for (auto A : AS)
2813 *Attrs++ = wrap(A);
2816 LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C,
2817 LLVMAttributeIndex Idx,
2818 unsigned KindID) {
2819 return wrap(
2820 unwrap<CallBase>(C)->getAttribute(Idx, (Attribute::AttrKind)KindID));
2823 LLVMAttributeRef LLVMGetCallSiteStringAttribute(LLVMValueRef C,
2824 LLVMAttributeIndex Idx,
2825 const char *K, unsigned KLen) {
2826 return wrap(unwrap<CallBase>(C)->getAttribute(Idx, StringRef(K, KLen)));
2829 void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2830 unsigned KindID) {
2831 unwrap<CallBase>(C)->removeAttribute(Idx, (Attribute::AttrKind)KindID);
2834 void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2835 const char *K, unsigned KLen) {
2836 unwrap<CallBase>(C)->removeAttribute(Idx, StringRef(K, KLen));
2839 LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) {
2840 return wrap(unwrap<CallBase>(Instr)->getCalledValue());
2843 LLVMTypeRef LLVMGetCalledFunctionType(LLVMValueRef Instr) {
2844 return wrap(unwrap<CallBase>(Instr)->getFunctionType());
2847 /*--.. Operations on call instructions (only) ..............................--*/
2849 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
2850 return unwrap<CallInst>(Call)->isTailCall();
2853 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
2854 unwrap<CallInst>(Call)->setTailCall(isTailCall);
2857 /*--.. Operations on invoke instructions (only) ............................--*/
2859 LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) {
2860 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2863 LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) {
2864 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2865 return wrap(CRI->getUnwindDest());
2866 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2867 return wrap(CSI->getUnwindDest());
2869 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2872 void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2873 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2876 void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2877 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2878 return CRI->setUnwindDest(unwrap(B));
2879 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2880 return CSI->setUnwindDest(unwrap(B));
2882 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2885 /*--.. Operations on terminators ...........................................--*/
2887 unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {
2888 return unwrap<Instruction>(Term)->getNumSuccessors();
2891 LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) {
2892 return wrap(unwrap<Instruction>(Term)->getSuccessor(i));
2895 void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
2896 return unwrap<Instruction>(Term)->setSuccessor(i, unwrap(block));
2899 /*--.. Operations on branch instructions (only) ............................--*/
2901 LLVMBool LLVMIsConditional(LLVMValueRef Branch) {
2902 return unwrap<BranchInst>(Branch)->isConditional();
2905 LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) {
2906 return wrap(unwrap<BranchInst>(Branch)->getCondition());
2909 void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) {
2910 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2913 /*--.. Operations on switch instructions (only) ............................--*/
2915 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
2916 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2919 /*--.. Operations on alloca instructions (only) ............................--*/
2921 LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) {
2922 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
2925 /*--.. Operations on gep instructions (only) ...............................--*/
2927 LLVMBool LLVMIsInBounds(LLVMValueRef GEP) {
2928 return unwrap<GetElementPtrInst>(GEP)->isInBounds();
2931 void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) {
2932 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
2935 /*--.. Operations on phi nodes .............................................--*/
2937 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
2938 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
2939 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
2940 for (unsigned I = 0; I != Count; ++I)
2941 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
2944 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
2945 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
2948 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
2949 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
2952 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
2953 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
2956 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/
2958 unsigned LLVMGetNumIndices(LLVMValueRef Inst) {
2959 auto *I = unwrap(Inst);
2960 if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
2961 return GEP->getNumIndices();
2962 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2963 return EV->getNumIndices();
2964 if (auto *IV = dyn_cast<InsertValueInst>(I))
2965 return IV->getNumIndices();
2966 if (auto *CE = dyn_cast<ConstantExpr>(I))
2967 return CE->getIndices().size();
2968 llvm_unreachable(
2969 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
2972 const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
2973 auto *I = unwrap(Inst);
2974 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2975 return EV->getIndices().data();
2976 if (auto *IV = dyn_cast<InsertValueInst>(I))
2977 return IV->getIndices().data();
2978 if (auto *CE = dyn_cast<ConstantExpr>(I))
2979 return CE->getIndices().data();
2980 llvm_unreachable(
2981 "LLVMGetIndices applies only to extractvalue and insertvalue!");
2985 /*===-- Instruction builders ----------------------------------------------===*/
2987 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
2988 return wrap(new IRBuilder<>(*unwrap(C)));
2991 LLVMBuilderRef LLVMCreateBuilder(void) {
2992 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
2995 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
2996 LLVMValueRef Instr) {
2997 BasicBlock *BB = unwrap(Block);
2998 auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
2999 unwrap(Builder)->SetInsertPoint(BB, I);
3002 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
3003 Instruction *I = unwrap<Instruction>(Instr);
3004 unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
3007 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
3008 BasicBlock *BB = unwrap(Block);
3009 unwrap(Builder)->SetInsertPoint(BB);
3012 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
3013 return wrap(unwrap(Builder)->GetInsertBlock());
3016 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
3017 unwrap(Builder)->ClearInsertionPoint();
3020 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
3021 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
3024 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
3025 const char *Name) {
3026 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
3029 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
3030 delete unwrap(Builder);
3033 /*--.. Metadata builders ...................................................--*/
3035 LLVMMetadataRef LLVMGetCurrentDebugLocation2(LLVMBuilderRef Builder) {
3036 return wrap(unwrap(Builder)->getCurrentDebugLocation().getAsMDNode());
3039 void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Builder, LLVMMetadataRef Loc) {
3040 if (Loc)
3041 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(unwrap<MDNode>(Loc)));
3042 else
3043 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc());
3046 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
3047 MDNode *Loc =
3048 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
3049 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
3052 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
3053 LLVMContext &Context = unwrap(Builder)->getContext();
3054 return wrap(MetadataAsValue::get(
3055 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
3058 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
3059 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
3062 void LLVMBuilderSetDefaultFPMathTag(LLVMBuilderRef Builder,
3063 LLVMMetadataRef FPMathTag) {
3065 unwrap(Builder)->setDefaultFPMathTag(FPMathTag
3066 ? unwrap<MDNode>(FPMathTag)
3067 : nullptr);
3070 LLVMMetadataRef LLVMBuilderGetDefaultFPMathTag(LLVMBuilderRef Builder) {
3071 return wrap(unwrap(Builder)->getDefaultFPMathTag());
3074 /*--.. Instruction builders ................................................--*/
3076 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
3077 return wrap(unwrap(B)->CreateRetVoid());
3080 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
3081 return wrap(unwrap(B)->CreateRet(unwrap(V)));
3084 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
3085 unsigned N) {
3086 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
3089 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
3090 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
3093 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
3094 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
3095 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
3098 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
3099 LLVMBasicBlockRef Else, unsigned NumCases) {
3100 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
3103 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
3104 unsigned NumDests) {
3105 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
3108 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
3109 LLVMValueRef *Args, unsigned NumArgs,
3110 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
3111 const char *Name) {
3112 Value *V = unwrap(Fn);
3113 FunctionType *FnT =
3114 cast<FunctionType>(cast<PointerType>(V->getType())->getElementType());
3116 return wrap(
3117 unwrap(B)->CreateInvoke(FnT, unwrap(Fn), unwrap(Then), unwrap(Catch),
3118 makeArrayRef(unwrap(Args), NumArgs), Name));
3121 LLVMValueRef LLVMBuildInvoke2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn,
3122 LLVMValueRef *Args, unsigned NumArgs,
3123 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
3124 const char *Name) {
3125 return wrap(unwrap(B)->CreateInvoke(
3126 unwrap<FunctionType>(Ty), unwrap(Fn), unwrap(Then), unwrap(Catch),
3127 makeArrayRef(unwrap(Args), NumArgs), Name));
3130 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
3131 LLVMValueRef PersFn, unsigned NumClauses,
3132 const char *Name) {
3133 // The personality used to live on the landingpad instruction, but now it
3134 // lives on the parent function. For compatibility, take the provided
3135 // personality and put it on the parent function.
3136 if (PersFn)
3137 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
3138 cast<Function>(unwrap(PersFn)));
3139 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
3142 LLVMValueRef LLVMBuildCatchPad(LLVMBuilderRef B, LLVMValueRef ParentPad,
3143 LLVMValueRef *Args, unsigned NumArgs,
3144 const char *Name) {
3145 return wrap(unwrap(B)->CreateCatchPad(unwrap(ParentPad),
3146 makeArrayRef(unwrap(Args), NumArgs),
3147 Name));
3150 LLVMValueRef LLVMBuildCleanupPad(LLVMBuilderRef B, LLVMValueRef ParentPad,
3151 LLVMValueRef *Args, unsigned NumArgs,
3152 const char *Name) {
3153 if (ParentPad == nullptr) {
3154 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3155 ParentPad = wrap(Constant::getNullValue(Ty));
3157 return wrap(unwrap(B)->CreateCleanupPad(unwrap(ParentPad),
3158 makeArrayRef(unwrap(Args), NumArgs),
3159 Name));
3162 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
3163 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
3166 LLVMValueRef LLVMBuildCatchSwitch(LLVMBuilderRef B, LLVMValueRef ParentPad,
3167 LLVMBasicBlockRef UnwindBB,
3168 unsigned NumHandlers, const char *Name) {
3169 if (ParentPad == nullptr) {
3170 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3171 ParentPad = wrap(Constant::getNullValue(Ty));
3173 return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(UnwindBB),
3174 NumHandlers, Name));
3177 LLVMValueRef LLVMBuildCatchRet(LLVMBuilderRef B, LLVMValueRef CatchPad,
3178 LLVMBasicBlockRef BB) {
3179 return wrap(unwrap(B)->CreateCatchRet(unwrap<CatchPadInst>(CatchPad),
3180 unwrap(BB)));
3183 LLVMValueRef LLVMBuildCleanupRet(LLVMBuilderRef B, LLVMValueRef CatchPad,
3184 LLVMBasicBlockRef BB) {
3185 return wrap(unwrap(B)->CreateCleanupRet(unwrap<CleanupPadInst>(CatchPad),
3186 unwrap(BB)));
3189 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
3190 return wrap(unwrap(B)->CreateUnreachable());
3193 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
3194 LLVMBasicBlockRef Dest) {
3195 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
3198 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
3199 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
3202 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
3203 return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
3206 LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
3207 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
3210 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
3211 unwrap<LandingPadInst>(LandingPad)->
3212 addClause(cast<Constant>(unwrap(ClauseVal)));
3215 LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) {
3216 return unwrap<LandingPadInst>(LandingPad)->isCleanup();
3219 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
3220 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
3223 void LLVMAddHandler(LLVMValueRef CatchSwitch, LLVMBasicBlockRef Dest) {
3224 unwrap<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Dest));
3227 unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) {
3228 return unwrap<CatchSwitchInst>(CatchSwitch)->getNumHandlers();
3231 void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) {
3232 CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(CatchSwitch);
3233 for (CatchSwitchInst::handler_iterator I = CSI->handler_begin(),
3234 E = CSI->handler_end(); I != E; ++I)
3235 *Handlers++ = wrap(*I);
3238 LLVMValueRef LLVMGetParentCatchSwitch(LLVMValueRef CatchPad) {
3239 return wrap(unwrap<CatchPadInst>(CatchPad)->getCatchSwitch());
3242 void LLVMSetParentCatchSwitch(LLVMValueRef CatchPad, LLVMValueRef CatchSwitch) {
3243 unwrap<CatchPadInst>(CatchPad)
3244 ->setCatchSwitch(unwrap<CatchSwitchInst>(CatchSwitch));
3247 /*--.. Funclets ...........................................................--*/
3249 LLVMValueRef LLVMGetArgOperand(LLVMValueRef Funclet, unsigned i) {
3250 return wrap(unwrap<FuncletPadInst>(Funclet)->getArgOperand(i));
3253 void LLVMSetArgOperand(LLVMValueRef Funclet, unsigned i, LLVMValueRef value) {
3254 unwrap<FuncletPadInst>(Funclet)->setArgOperand(i, unwrap(value));
3257 /*--.. Arithmetic ..........................................................--*/
3259 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3260 const char *Name) {
3261 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
3264 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3265 const char *Name) {
3266 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
3269 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3270 const char *Name) {
3271 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
3274 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3275 const char *Name) {
3276 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
3279 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3280 const char *Name) {
3281 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
3284 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3285 const char *Name) {
3286 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
3289 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3290 const char *Name) {
3291 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
3294 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3295 const char *Name) {
3296 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
3299 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3300 const char *Name) {
3301 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
3304 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3305 const char *Name) {
3306 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
3309 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3310 const char *Name) {
3311 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
3314 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3315 const char *Name) {
3316 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
3319 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3320 const char *Name) {
3321 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
3324 LLVMValueRef LLVMBuildExactUDiv(LLVMBuilderRef B, LLVMValueRef LHS,
3325 LLVMValueRef RHS, const char *Name) {
3326 return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
3329 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3330 const char *Name) {
3331 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
3334 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
3335 LLVMValueRef RHS, const char *Name) {
3336 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
3339 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3340 const char *Name) {
3341 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
3344 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3345 const char *Name) {
3346 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
3349 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3350 const char *Name) {
3351 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
3354 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3355 const char *Name) {
3356 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
3359 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3360 const char *Name) {
3361 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
3364 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3365 const char *Name) {
3366 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
3369 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3370 const char *Name) {
3371 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
3374 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3375 const char *Name) {
3376 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
3379 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3380 const char *Name) {
3381 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
3384 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3385 const char *Name) {
3386 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
3389 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
3390 LLVMValueRef LHS, LLVMValueRef RHS,
3391 const char *Name) {
3392 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
3393 unwrap(RHS), Name));
3396 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
3397 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
3400 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
3401 const char *Name) {
3402 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
3405 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
3406 const char *Name) {
3407 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
3410 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
3411 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
3414 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
3415 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
3418 /*--.. Memory ..............................................................--*/
3420 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
3421 const char *Name) {
3422 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3423 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3424 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3425 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3426 ITy, unwrap(Ty), AllocSize,
3427 nullptr, nullptr, "");
3428 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3431 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
3432 LLVMValueRef Val, const char *Name) {
3433 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3434 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3435 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3436 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3437 ITy, unwrap(Ty), AllocSize,
3438 unwrap(Val), nullptr, "");
3439 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3442 LLVMValueRef LLVMBuildMemSet(LLVMBuilderRef B, LLVMValueRef Ptr,
3443 LLVMValueRef Val, LLVMValueRef Len,
3444 unsigned Align) {
3445 return wrap(unwrap(B)->CreateMemSet(unwrap(Ptr), unwrap(Val), unwrap(Len), Align));
3448 LLVMValueRef LLVMBuildMemCpy(LLVMBuilderRef B,
3449 LLVMValueRef Dst, unsigned DstAlign,
3450 LLVMValueRef Src, unsigned SrcAlign,
3451 LLVMValueRef Size) {
3452 return wrap(unwrap(B)->CreateMemCpy(unwrap(Dst), DstAlign,
3453 unwrap(Src), SrcAlign,
3454 unwrap(Size)));
3457 LLVMValueRef LLVMBuildMemMove(LLVMBuilderRef B,
3458 LLVMValueRef Dst, unsigned DstAlign,
3459 LLVMValueRef Src, unsigned SrcAlign,
3460 LLVMValueRef Size) {
3461 return wrap(unwrap(B)->CreateMemMove(unwrap(Dst), DstAlign,
3462 unwrap(Src), SrcAlign,
3463 unwrap(Size)));
3466 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
3467 const char *Name) {
3468 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
3471 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
3472 LLVMValueRef Val, const char *Name) {
3473 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
3476 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
3477 return wrap(unwrap(B)->Insert(
3478 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
3481 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
3482 const char *Name) {
3483 Value *V = unwrap(PointerVal);
3484 PointerType *Ty = cast<PointerType>(V->getType());
3486 return wrap(unwrap(B)->CreateLoad(Ty->getElementType(), V, Name));
3489 LLVMValueRef LLVMBuildLoad2(LLVMBuilderRef B, LLVMTypeRef Ty,
3490 LLVMValueRef PointerVal, const char *Name) {
3491 return wrap(unwrap(B)->CreateLoad(unwrap(Ty), unwrap(PointerVal), Name));
3494 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
3495 LLVMValueRef PointerVal) {
3496 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
3499 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
3500 switch (Ordering) {
3501 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic;
3502 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered;
3503 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic;
3504 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire;
3505 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release;
3506 case LLVMAtomicOrderingAcquireRelease:
3507 return AtomicOrdering::AcquireRelease;
3508 case LLVMAtomicOrderingSequentiallyConsistent:
3509 return AtomicOrdering::SequentiallyConsistent;
3512 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
3515 static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) {
3516 switch (Ordering) {
3517 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic;
3518 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered;
3519 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic;
3520 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire;
3521 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease;
3522 case AtomicOrdering::AcquireRelease:
3523 return LLVMAtomicOrderingAcquireRelease;
3524 case AtomicOrdering::SequentiallyConsistent:
3525 return LLVMAtomicOrderingSequentiallyConsistent;
3528 llvm_unreachable("Invalid AtomicOrdering value!");
3531 static AtomicRMWInst::BinOp mapFromLLVMRMWBinOp(LLVMAtomicRMWBinOp BinOp) {
3532 switch (BinOp) {
3533 case LLVMAtomicRMWBinOpXchg: return AtomicRMWInst::Xchg;
3534 case LLVMAtomicRMWBinOpAdd: return AtomicRMWInst::Add;
3535 case LLVMAtomicRMWBinOpSub: return AtomicRMWInst::Sub;
3536 case LLVMAtomicRMWBinOpAnd: return AtomicRMWInst::And;
3537 case LLVMAtomicRMWBinOpNand: return AtomicRMWInst::Nand;
3538 case LLVMAtomicRMWBinOpOr: return AtomicRMWInst::Or;
3539 case LLVMAtomicRMWBinOpXor: return AtomicRMWInst::Xor;
3540 case LLVMAtomicRMWBinOpMax: return AtomicRMWInst::Max;
3541 case LLVMAtomicRMWBinOpMin: return AtomicRMWInst::Min;
3542 case LLVMAtomicRMWBinOpUMax: return AtomicRMWInst::UMax;
3543 case LLVMAtomicRMWBinOpUMin: return AtomicRMWInst::UMin;
3544 case LLVMAtomicRMWBinOpFAdd: return AtomicRMWInst::FAdd;
3545 case LLVMAtomicRMWBinOpFSub: return AtomicRMWInst::FSub;
3548 llvm_unreachable("Invalid LLVMAtomicRMWBinOp value!");
3551 static LLVMAtomicRMWBinOp mapToLLVMRMWBinOp(AtomicRMWInst::BinOp BinOp) {
3552 switch (BinOp) {
3553 case AtomicRMWInst::Xchg: return LLVMAtomicRMWBinOpXchg;
3554 case AtomicRMWInst::Add: return LLVMAtomicRMWBinOpAdd;
3555 case AtomicRMWInst::Sub: return LLVMAtomicRMWBinOpSub;
3556 case AtomicRMWInst::And: return LLVMAtomicRMWBinOpAnd;
3557 case AtomicRMWInst::Nand: return LLVMAtomicRMWBinOpNand;
3558 case AtomicRMWInst::Or: return LLVMAtomicRMWBinOpOr;
3559 case AtomicRMWInst::Xor: return LLVMAtomicRMWBinOpXor;
3560 case AtomicRMWInst::Max: return LLVMAtomicRMWBinOpMax;
3561 case AtomicRMWInst::Min: return LLVMAtomicRMWBinOpMin;
3562 case AtomicRMWInst::UMax: return LLVMAtomicRMWBinOpUMax;
3563 case AtomicRMWInst::UMin: return LLVMAtomicRMWBinOpUMin;
3564 case AtomicRMWInst::FAdd: return LLVMAtomicRMWBinOpFAdd;
3565 case AtomicRMWInst::FSub: return LLVMAtomicRMWBinOpFSub;
3566 default: break;
3569 llvm_unreachable("Invalid AtomicRMWBinOp value!");
3572 // TODO: Should this and other atomic instructions support building with
3573 // "syncscope"?
3574 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
3575 LLVMBool isSingleThread, const char *Name) {
3576 return wrap(
3577 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
3578 isSingleThread ? SyncScope::SingleThread
3579 : SyncScope::System,
3580 Name));
3583 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
3584 LLVMValueRef *Indices, unsigned NumIndices,
3585 const char *Name) {
3586 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3587 Value *Val = unwrap(Pointer);
3588 Type *Ty =
3589 cast<PointerType>(Val->getType()->getScalarType())->getElementType();
3590 return wrap(unwrap(B)->CreateGEP(Ty, Val, IdxList, Name));
3593 LLVMValueRef LLVMBuildGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,
3594 LLVMValueRef Pointer, LLVMValueRef *Indices,
3595 unsigned NumIndices, const char *Name) {
3596 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3597 return wrap(unwrap(B)->CreateGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3600 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
3601 LLVMValueRef *Indices, unsigned NumIndices,
3602 const char *Name) {
3603 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3604 Value *Val = unwrap(Pointer);
3605 Type *Ty =
3606 cast<PointerType>(Val->getType()->getScalarType())->getElementType();
3607 return wrap(unwrap(B)->CreateInBoundsGEP(Ty, Val, IdxList, Name));
3610 LLVMValueRef LLVMBuildInBoundsGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,
3611 LLVMValueRef Pointer, LLVMValueRef *Indices,
3612 unsigned NumIndices, const char *Name) {
3613 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3614 return wrap(
3615 unwrap(B)->CreateInBoundsGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3618 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
3619 unsigned Idx, const char *Name) {
3620 Value *Val = unwrap(Pointer);
3621 Type *Ty =
3622 cast<PointerType>(Val->getType()->getScalarType())->getElementType();
3623 return wrap(unwrap(B)->CreateStructGEP(Ty, Val, Idx, Name));
3626 LLVMValueRef LLVMBuildStructGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,
3627 LLVMValueRef Pointer, unsigned Idx,
3628 const char *Name) {
3629 return wrap(
3630 unwrap(B)->CreateStructGEP(unwrap(Ty), unwrap(Pointer), Idx, Name));
3633 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
3634 const char *Name) {
3635 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
3638 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
3639 const char *Name) {
3640 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
3643 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
3644 Value *P = unwrap<Value>(MemAccessInst);
3645 if (LoadInst *LI = dyn_cast<LoadInst>(P))
3646 return LI->isVolatile();
3647 if (StoreInst *SI = dyn_cast<StoreInst>(P))
3648 return SI->isVolatile();
3649 if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
3650 return AI->isVolatile();
3651 return cast<AtomicCmpXchgInst>(P)->isVolatile();
3654 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
3655 Value *P = unwrap<Value>(MemAccessInst);
3656 if (LoadInst *LI = dyn_cast<LoadInst>(P))
3657 return LI->setVolatile(isVolatile);
3658 if (StoreInst *SI = dyn_cast<StoreInst>(P))
3659 return SI->setVolatile(isVolatile);
3660 if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
3661 return AI->setVolatile(isVolatile);
3662 return cast<AtomicCmpXchgInst>(P)->setVolatile(isVolatile);
3665 LLVMBool LLVMGetWeak(LLVMValueRef CmpXchgInst) {
3666 return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->isWeak();
3669 void LLVMSetWeak(LLVMValueRef CmpXchgInst, LLVMBool isWeak) {
3670 return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->setWeak(isWeak);
3673 LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) {
3674 Value *P = unwrap<Value>(MemAccessInst);
3675 AtomicOrdering O;
3676 if (LoadInst *LI = dyn_cast<LoadInst>(P))
3677 O = LI->getOrdering();
3678 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
3679 O = SI->getOrdering();
3680 else
3681 O = cast<AtomicRMWInst>(P)->getOrdering();
3682 return mapToLLVMOrdering(O);
3685 void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
3686 Value *P = unwrap<Value>(MemAccessInst);
3687 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3689 if (LoadInst *LI = dyn_cast<LoadInst>(P))
3690 return LI->setOrdering(O);
3691 return cast<StoreInst>(P)->setOrdering(O);
3694 LLVMAtomicRMWBinOp LLVMGetAtomicRMWBinOp(LLVMValueRef Inst) {
3695 return mapToLLVMRMWBinOp(unwrap<AtomicRMWInst>(Inst)->getOperation());
3698 void LLVMSetAtomicRMWBinOp(LLVMValueRef Inst, LLVMAtomicRMWBinOp BinOp) {
3699 unwrap<AtomicRMWInst>(Inst)->setOperation(mapFromLLVMRMWBinOp(BinOp));
3702 /*--.. Casts ...............................................................--*/
3704 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
3705 LLVMTypeRef DestTy, const char *Name) {
3706 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
3709 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
3710 LLVMTypeRef DestTy, const char *Name) {
3711 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
3714 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
3715 LLVMTypeRef DestTy, const char *Name) {
3716 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
3719 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
3720 LLVMTypeRef DestTy, const char *Name) {
3721 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
3724 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
3725 LLVMTypeRef DestTy, const char *Name) {
3726 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
3729 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
3730 LLVMTypeRef DestTy, const char *Name) {
3731 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
3734 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
3735 LLVMTypeRef DestTy, const char *Name) {
3736 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
3739 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
3740 LLVMTypeRef DestTy, const char *Name) {
3741 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
3744 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
3745 LLVMTypeRef DestTy, const char *Name) {
3746 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
3749 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
3750 LLVMTypeRef DestTy, const char *Name) {
3751 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
3754 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
3755 LLVMTypeRef DestTy, const char *Name) {
3756 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
3759 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3760 LLVMTypeRef DestTy, const char *Name) {
3761 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
3764 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
3765 LLVMTypeRef DestTy, const char *Name) {
3766 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
3769 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3770 LLVMTypeRef DestTy, const char *Name) {
3771 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
3772 Name));
3775 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3776 LLVMTypeRef DestTy, const char *Name) {
3777 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
3778 Name));
3781 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3782 LLVMTypeRef DestTy, const char *Name) {
3783 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
3784 Name));
3787 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
3788 LLVMTypeRef DestTy, const char *Name) {
3789 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
3790 unwrap(DestTy), Name));
3793 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
3794 LLVMTypeRef DestTy, const char *Name) {
3795 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
3798 LLVMValueRef LLVMBuildIntCast2(LLVMBuilderRef B, LLVMValueRef Val,
3799 LLVMTypeRef DestTy, LLVMBool IsSigned,
3800 const char *Name) {
3801 return wrap(
3802 unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy), IsSigned, Name));
3805 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
3806 LLVMTypeRef DestTy, const char *Name) {
3807 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
3808 /*isSigned*/true, Name));
3811 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
3812 LLVMTypeRef DestTy, const char *Name) {
3813 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
3816 /*--.. Comparisons .........................................................--*/
3818 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
3819 LLVMValueRef LHS, LLVMValueRef RHS,
3820 const char *Name) {
3821 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
3822 unwrap(LHS), unwrap(RHS), Name));
3825 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
3826 LLVMValueRef LHS, LLVMValueRef RHS,
3827 const char *Name) {
3828 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
3829 unwrap(LHS), unwrap(RHS), Name));
3832 /*--.. Miscellaneous instructions ..........................................--*/
3834 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
3835 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
3838 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
3839 LLVMValueRef *Args, unsigned NumArgs,
3840 const char *Name) {
3841 Value *V = unwrap(Fn);
3842 FunctionType *FnT =
3843 cast<FunctionType>(cast<PointerType>(V->getType())->getElementType());
3845 return wrap(unwrap(B)->CreateCall(FnT, unwrap(Fn),
3846 makeArrayRef(unwrap(Args), NumArgs), Name));
3849 LLVMValueRef LLVMBuildCall2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn,
3850 LLVMValueRef *Args, unsigned NumArgs,
3851 const char *Name) {
3852 FunctionType *FTy = unwrap<FunctionType>(Ty);
3853 return wrap(unwrap(B)->CreateCall(FTy, unwrap(Fn),
3854 makeArrayRef(unwrap(Args), NumArgs), Name));
3857 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
3858 LLVMValueRef Then, LLVMValueRef Else,
3859 const char *Name) {
3860 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
3861 Name));
3864 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
3865 LLVMTypeRef Ty, const char *Name) {
3866 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
3869 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
3870 LLVMValueRef Index, const char *Name) {
3871 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
3872 Name));
3875 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
3876 LLVMValueRef EltVal, LLVMValueRef Index,
3877 const char *Name) {
3878 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
3879 unwrap(Index), Name));
3882 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
3883 LLVMValueRef V2, LLVMValueRef Mask,
3884 const char *Name) {
3885 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
3886 unwrap(Mask), Name));
3889 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
3890 unsigned Index, const char *Name) {
3891 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
3894 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
3895 LLVMValueRef EltVal, unsigned Index,
3896 const char *Name) {
3897 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
3898 Index, Name));
3901 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
3902 const char *Name) {
3903 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
3906 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
3907 const char *Name) {
3908 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
3911 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
3912 LLVMValueRef RHS, const char *Name) {
3913 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
3916 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
3917 LLVMValueRef PTR, LLVMValueRef Val,
3918 LLVMAtomicOrdering ordering,
3919 LLVMBool singleThread) {
3920 AtomicRMWInst::BinOp intop = mapFromLLVMRMWBinOp(op);
3921 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
3922 mapFromLLVMOrdering(ordering), singleThread ? SyncScope::SingleThread
3923 : SyncScope::System));
3926 LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr,
3927 LLVMValueRef Cmp, LLVMValueRef New,
3928 LLVMAtomicOrdering SuccessOrdering,
3929 LLVMAtomicOrdering FailureOrdering,
3930 LLVMBool singleThread) {
3932 return wrap(unwrap(B)->CreateAtomicCmpXchg(unwrap(Ptr), unwrap(Cmp),
3933 unwrap(New), mapFromLLVMOrdering(SuccessOrdering),
3934 mapFromLLVMOrdering(FailureOrdering),
3935 singleThread ? SyncScope::SingleThread : SyncScope::System));
3939 LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) {
3940 Value *P = unwrap<Value>(AtomicInst);
3942 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
3943 return I->getSyncScopeID() == SyncScope::SingleThread;
3944 return cast<AtomicCmpXchgInst>(P)->getSyncScopeID() ==
3945 SyncScope::SingleThread;
3948 void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) {
3949 Value *P = unwrap<Value>(AtomicInst);
3950 SyncScope::ID SSID = NewValue ? SyncScope::SingleThread : SyncScope::System;
3952 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
3953 return I->setSyncScopeID(SSID);
3954 return cast<AtomicCmpXchgInst>(P)->setSyncScopeID(SSID);
3957 LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) {
3958 Value *P = unwrap<Value>(CmpXchgInst);
3959 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering());
3962 void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst,
3963 LLVMAtomicOrdering Ordering) {
3964 Value *P = unwrap<Value>(CmpXchgInst);
3965 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3967 return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O);
3970 LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst) {
3971 Value *P = unwrap<Value>(CmpXchgInst);
3972 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering());
3975 void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst,
3976 LLVMAtomicOrdering Ordering) {
3977 Value *P = unwrap<Value>(CmpXchgInst);
3978 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3980 return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O);
3983 /*===-- Module providers --------------------------------------------------===*/
3985 LLVMModuleProviderRef
3986 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
3987 return reinterpret_cast<LLVMModuleProviderRef>(M);
3990 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
3991 delete unwrap(MP);
3995 /*===-- Memory buffers ----------------------------------------------------===*/
3997 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
3998 const char *Path,
3999 LLVMMemoryBufferRef *OutMemBuf,
4000 char **OutMessage) {
4002 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
4003 if (std::error_code EC = MBOrErr.getError()) {
4004 *OutMessage = strdup(EC.message().c_str());
4005 return 1;
4007 *OutMemBuf = wrap(MBOrErr.get().release());
4008 return 0;
4011 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
4012 char **OutMessage) {
4013 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
4014 if (std::error_code EC = MBOrErr.getError()) {
4015 *OutMessage = strdup(EC.message().c_str());
4016 return 1;
4018 *OutMemBuf = wrap(MBOrErr.get().release());
4019 return 0;
4022 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
4023 const char *InputData,
4024 size_t InputDataLength,
4025 const char *BufferName,
4026 LLVMBool RequiresNullTerminator) {
4028 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
4029 StringRef(BufferName),
4030 RequiresNullTerminator).release());
4033 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
4034 const char *InputData,
4035 size_t InputDataLength,
4036 const char *BufferName) {
4038 return wrap(
4039 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
4040 StringRef(BufferName)).release());
4043 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
4044 return unwrap(MemBuf)->getBufferStart();
4047 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
4048 return unwrap(MemBuf)->getBufferSize();
4051 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
4052 delete unwrap(MemBuf);
4055 /*===-- Pass Registry -----------------------------------------------------===*/
4057 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
4058 return wrap(PassRegistry::getPassRegistry());
4061 /*===-- Pass Manager ------------------------------------------------------===*/
4063 LLVMPassManagerRef LLVMCreatePassManager() {
4064 return wrap(new legacy::PassManager());
4067 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
4068 return wrap(new legacy::FunctionPassManager(unwrap(M)));
4071 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
4072 return LLVMCreateFunctionPassManagerForModule(
4073 reinterpret_cast<LLVMModuleRef>(P));
4076 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
4077 return unwrap<legacy::PassManager>(PM)->run(*unwrap(M));
4080 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
4081 return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization();
4084 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
4085 return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
4088 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
4089 return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization();
4092 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
4093 delete unwrap(PM);
4096 /*===-- Threading ------------------------------------------------------===*/
4098 LLVMBool LLVMStartMultithreaded() {
4099 return LLVMIsMultithreaded();
4102 void LLVMStopMultithreaded() {
4105 LLVMBool LLVMIsMultithreaded() {
4106 return llvm_is_multithreaded();