[RISCV] Fix mgather -> riscv.masked.strided.load combine not extending indices (...
[llvm-project.git] / llvm / lib / IR / Core.cpp
blobfb30fbce0ba22e38319facd29faf41efe2290f90
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/IR/Attributes.h"
16 #include "llvm/IR/BasicBlock.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/InitializePasses.h"
31 #include "llvm/PassRegistry.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/FileSystem.h"
35 #include "llvm/Support/ManagedStatic.h"
36 #include "llvm/Support/MemoryBuffer.h"
37 #include "llvm/Support/Threading.h"
38 #include "llvm/Support/raw_ostream.h"
39 #include <cassert>
40 #include <cstdlib>
41 #include <cstring>
42 #include <system_error>
44 using namespace llvm;
46 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(OperandBundleDef, LLVMOperandBundleRef)
48 #define DEBUG_TYPE "ir"
50 void llvm::initializeCore(PassRegistry &Registry) {
51 initializeDominatorTreeWrapperPassPass(Registry);
52 initializePrintModulePassWrapperPass(Registry);
53 initializePrintFunctionPassWrapperPass(Registry);
54 initializeSafepointIRVerifierPass(Registry);
55 initializeVerifierLegacyPassPass(Registry);
58 void LLVMShutdown() {
59 llvm_shutdown();
62 /*===-- Version query -----------------------------------------------------===*/
64 void LLVMGetVersion(unsigned *Major, unsigned *Minor, unsigned *Patch) {
65 if (Major)
66 *Major = LLVM_VERSION_MAJOR;
67 if (Minor)
68 *Minor = LLVM_VERSION_MINOR;
69 if (Patch)
70 *Patch = LLVM_VERSION_PATCH;
73 /*===-- Error handling ----------------------------------------------------===*/
75 char *LLVMCreateMessage(const char *Message) {
76 return strdup(Message);
79 void LLVMDisposeMessage(char *Message) {
80 free(Message);
84 /*===-- Operations on contexts --------------------------------------------===*/
86 static LLVMContext &getGlobalContext() {
87 static LLVMContext GlobalContext;
88 return GlobalContext;
91 LLVMContextRef LLVMContextCreate() {
92 return wrap(new LLVMContext());
95 LLVMContextRef LLVMGetGlobalContext() { return wrap(&getGlobalContext()); }
97 void LLVMContextSetDiagnosticHandler(LLVMContextRef C,
98 LLVMDiagnosticHandler Handler,
99 void *DiagnosticContext) {
100 unwrap(C)->setDiagnosticHandlerCallBack(
101 LLVM_EXTENSION reinterpret_cast<DiagnosticHandler::DiagnosticHandlerTy>(
102 Handler),
103 DiagnosticContext);
106 LLVMDiagnosticHandler LLVMContextGetDiagnosticHandler(LLVMContextRef C) {
107 return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>(
108 unwrap(C)->getDiagnosticHandlerCallBack());
111 void *LLVMContextGetDiagnosticContext(LLVMContextRef C) {
112 return unwrap(C)->getDiagnosticContext();
115 void LLVMContextSetYieldCallback(LLVMContextRef C, LLVMYieldCallback Callback,
116 void *OpaqueHandle) {
117 auto YieldCallback =
118 LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
119 unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
122 LLVMBool LLVMContextShouldDiscardValueNames(LLVMContextRef C) {
123 return unwrap(C)->shouldDiscardValueNames();
126 void LLVMContextSetDiscardValueNames(LLVMContextRef C, LLVMBool Discard) {
127 unwrap(C)->setDiscardValueNames(Discard);
130 void LLVMContextDispose(LLVMContextRef C) {
131 delete unwrap(C);
134 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char *Name,
135 unsigned SLen) {
136 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
139 unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) {
140 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
143 unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) {
144 return Attribute::getAttrKindFromName(StringRef(Name, SLen));
147 unsigned LLVMGetLastEnumAttributeKind(void) {
148 return Attribute::AttrKind::EndAttrKinds;
151 LLVMAttributeRef LLVMCreateEnumAttribute(LLVMContextRef C, unsigned KindID,
152 uint64_t Val) {
153 auto &Ctx = *unwrap(C);
154 auto AttrKind = (Attribute::AttrKind)KindID;
155 return wrap(Attribute::get(Ctx, AttrKind, Val));
158 unsigned LLVMGetEnumAttributeKind(LLVMAttributeRef A) {
159 return unwrap(A).getKindAsEnum();
162 uint64_t LLVMGetEnumAttributeValue(LLVMAttributeRef A) {
163 auto Attr = unwrap(A);
164 if (Attr.isEnumAttribute())
165 return 0;
166 return Attr.getValueAsInt();
169 LLVMAttributeRef LLVMCreateTypeAttribute(LLVMContextRef C, unsigned KindID,
170 LLVMTypeRef type_ref) {
171 auto &Ctx = *unwrap(C);
172 auto AttrKind = (Attribute::AttrKind)KindID;
173 return wrap(Attribute::get(Ctx, AttrKind, unwrap(type_ref)));
176 LLVMTypeRef LLVMGetTypeAttributeValue(LLVMAttributeRef A) {
177 auto Attr = unwrap(A);
178 return wrap(Attr.getValueAsType());
181 LLVMAttributeRef LLVMCreateStringAttribute(LLVMContextRef C,
182 const char *K, unsigned KLength,
183 const char *V, unsigned VLength) {
184 return wrap(Attribute::get(*unwrap(C), StringRef(K, KLength),
185 StringRef(V, VLength)));
188 const char *LLVMGetStringAttributeKind(LLVMAttributeRef A,
189 unsigned *Length) {
190 auto S = unwrap(A).getKindAsString();
191 *Length = S.size();
192 return S.data();
195 const char *LLVMGetStringAttributeValue(LLVMAttributeRef A,
196 unsigned *Length) {
197 auto S = unwrap(A).getValueAsString();
198 *Length = S.size();
199 return S.data();
202 LLVMBool LLVMIsEnumAttribute(LLVMAttributeRef A) {
203 auto Attr = unwrap(A);
204 return Attr.isEnumAttribute() || Attr.isIntAttribute();
207 LLVMBool LLVMIsStringAttribute(LLVMAttributeRef A) {
208 return unwrap(A).isStringAttribute();
211 LLVMBool LLVMIsTypeAttribute(LLVMAttributeRef A) {
212 return unwrap(A).isTypeAttribute();
215 char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) {
216 std::string MsgStorage;
217 raw_string_ostream Stream(MsgStorage);
218 DiagnosticPrinterRawOStream DP(Stream);
220 unwrap(DI)->print(DP);
221 Stream.flush();
223 return LLVMCreateMessage(MsgStorage.c_str());
226 LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI) {
227 LLVMDiagnosticSeverity severity;
229 switch(unwrap(DI)->getSeverity()) {
230 default:
231 severity = LLVMDSError;
232 break;
233 case DS_Warning:
234 severity = LLVMDSWarning;
235 break;
236 case DS_Remark:
237 severity = LLVMDSRemark;
238 break;
239 case DS_Note:
240 severity = LLVMDSNote;
241 break;
244 return severity;
247 /*===-- Operations on modules ---------------------------------------------===*/
249 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
250 return wrap(new Module(ModuleID, getGlobalContext()));
253 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
254 LLVMContextRef C) {
255 return wrap(new Module(ModuleID, *unwrap(C)));
258 void LLVMDisposeModule(LLVMModuleRef M) {
259 delete unwrap(M);
262 const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) {
263 auto &Str = unwrap(M)->getModuleIdentifier();
264 *Len = Str.length();
265 return Str.c_str();
268 void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) {
269 unwrap(M)->setModuleIdentifier(StringRef(Ident, Len));
272 const char *LLVMGetSourceFileName(LLVMModuleRef M, size_t *Len) {
273 auto &Str = unwrap(M)->getSourceFileName();
274 *Len = Str.length();
275 return Str.c_str();
278 void LLVMSetSourceFileName(LLVMModuleRef M, const char *Name, size_t Len) {
279 unwrap(M)->setSourceFileName(StringRef(Name, Len));
282 /*--.. Data layout .........................................................--*/
283 const char *LLVMGetDataLayoutStr(LLVMModuleRef M) {
284 return unwrap(M)->getDataLayoutStr().c_str();
287 const char *LLVMGetDataLayout(LLVMModuleRef M) {
288 return LLVMGetDataLayoutStr(M);
291 void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
292 unwrap(M)->setDataLayout(DataLayoutStr);
295 /*--.. Target triple .......................................................--*/
296 const char * LLVMGetTarget(LLVMModuleRef M) {
297 return unwrap(M)->getTargetTriple().c_str();
300 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
301 unwrap(M)->setTargetTriple(Triple);
304 /*--.. Module flags ........................................................--*/
305 struct LLVMOpaqueModuleFlagEntry {
306 LLVMModuleFlagBehavior Behavior;
307 const char *Key;
308 size_t KeyLen;
309 LLVMMetadataRef Metadata;
312 static Module::ModFlagBehavior
313 map_to_llvmModFlagBehavior(LLVMModuleFlagBehavior Behavior) {
314 switch (Behavior) {
315 case LLVMModuleFlagBehaviorError:
316 return Module::ModFlagBehavior::Error;
317 case LLVMModuleFlagBehaviorWarning:
318 return Module::ModFlagBehavior::Warning;
319 case LLVMModuleFlagBehaviorRequire:
320 return Module::ModFlagBehavior::Require;
321 case LLVMModuleFlagBehaviorOverride:
322 return Module::ModFlagBehavior::Override;
323 case LLVMModuleFlagBehaviorAppend:
324 return Module::ModFlagBehavior::Append;
325 case LLVMModuleFlagBehaviorAppendUnique:
326 return Module::ModFlagBehavior::AppendUnique;
328 llvm_unreachable("Unknown LLVMModuleFlagBehavior");
331 static LLVMModuleFlagBehavior
332 map_from_llvmModFlagBehavior(Module::ModFlagBehavior Behavior) {
333 switch (Behavior) {
334 case Module::ModFlagBehavior::Error:
335 return LLVMModuleFlagBehaviorError;
336 case Module::ModFlagBehavior::Warning:
337 return LLVMModuleFlagBehaviorWarning;
338 case Module::ModFlagBehavior::Require:
339 return LLVMModuleFlagBehaviorRequire;
340 case Module::ModFlagBehavior::Override:
341 return LLVMModuleFlagBehaviorOverride;
342 case Module::ModFlagBehavior::Append:
343 return LLVMModuleFlagBehaviorAppend;
344 case Module::ModFlagBehavior::AppendUnique:
345 return LLVMModuleFlagBehaviorAppendUnique;
346 default:
347 llvm_unreachable("Unhandled Flag Behavior");
351 LLVMModuleFlagEntry *LLVMCopyModuleFlagsMetadata(LLVMModuleRef M, size_t *Len) {
352 SmallVector<Module::ModuleFlagEntry, 8> MFEs;
353 unwrap(M)->getModuleFlagsMetadata(MFEs);
355 LLVMOpaqueModuleFlagEntry *Result = static_cast<LLVMOpaqueModuleFlagEntry *>(
356 safe_malloc(MFEs.size() * sizeof(LLVMOpaqueModuleFlagEntry)));
357 for (unsigned i = 0; i < MFEs.size(); ++i) {
358 const auto &ModuleFlag = MFEs[i];
359 Result[i].Behavior = map_from_llvmModFlagBehavior(ModuleFlag.Behavior);
360 Result[i].Key = ModuleFlag.Key->getString().data();
361 Result[i].KeyLen = ModuleFlag.Key->getString().size();
362 Result[i].Metadata = wrap(ModuleFlag.Val);
364 *Len = MFEs.size();
365 return Result;
368 void LLVMDisposeModuleFlagsMetadata(LLVMModuleFlagEntry *Entries) {
369 free(Entries);
372 LLVMModuleFlagBehavior
373 LLVMModuleFlagEntriesGetFlagBehavior(LLVMModuleFlagEntry *Entries,
374 unsigned Index) {
375 LLVMOpaqueModuleFlagEntry MFE =
376 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
377 return MFE.Behavior;
380 const char *LLVMModuleFlagEntriesGetKey(LLVMModuleFlagEntry *Entries,
381 unsigned Index, size_t *Len) {
382 LLVMOpaqueModuleFlagEntry MFE =
383 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
384 *Len = MFE.KeyLen;
385 return MFE.Key;
388 LLVMMetadataRef LLVMModuleFlagEntriesGetMetadata(LLVMModuleFlagEntry *Entries,
389 unsigned Index) {
390 LLVMOpaqueModuleFlagEntry MFE =
391 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
392 return MFE.Metadata;
395 LLVMMetadataRef LLVMGetModuleFlag(LLVMModuleRef M,
396 const char *Key, size_t KeyLen) {
397 return wrap(unwrap(M)->getModuleFlag({Key, KeyLen}));
400 void LLVMAddModuleFlag(LLVMModuleRef M, LLVMModuleFlagBehavior Behavior,
401 const char *Key, size_t KeyLen,
402 LLVMMetadataRef Val) {
403 unwrap(M)->addModuleFlag(map_to_llvmModFlagBehavior(Behavior),
404 {Key, KeyLen}, unwrap(Val));
407 /*--.. Printing modules ....................................................--*/
409 void LLVMDumpModule(LLVMModuleRef M) {
410 unwrap(M)->print(errs(), nullptr,
411 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
414 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
415 char **ErrorMessage) {
416 std::error_code EC;
417 raw_fd_ostream dest(Filename, EC, sys::fs::OF_TextWithCRLF);
418 if (EC) {
419 *ErrorMessage = strdup(EC.message().c_str());
420 return true;
423 unwrap(M)->print(dest, nullptr);
425 dest.close();
427 if (dest.has_error()) {
428 std::string E = "Error printing to file: " + dest.error().message();
429 *ErrorMessage = strdup(E.c_str());
430 return true;
433 return false;
436 char *LLVMPrintModuleToString(LLVMModuleRef M) {
437 std::string buf;
438 raw_string_ostream os(buf);
440 unwrap(M)->print(os, nullptr);
441 os.flush();
443 return strdup(buf.c_str());
446 /*--.. Operations on inline assembler ......................................--*/
447 void LLVMSetModuleInlineAsm2(LLVMModuleRef M, const char *Asm, size_t Len) {
448 unwrap(M)->setModuleInlineAsm(StringRef(Asm, Len));
451 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
452 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
455 void LLVMAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm, size_t Len) {
456 unwrap(M)->appendModuleInlineAsm(StringRef(Asm, Len));
459 const char *LLVMGetModuleInlineAsm(LLVMModuleRef M, size_t *Len) {
460 auto &Str = unwrap(M)->getModuleInlineAsm();
461 *Len = Str.length();
462 return Str.c_str();
465 LLVMValueRef LLVMGetInlineAsm(LLVMTypeRef Ty, const char *AsmString,
466 size_t AsmStringSize, const char *Constraints,
467 size_t ConstraintsSize, LLVMBool HasSideEffects,
468 LLVMBool IsAlignStack,
469 LLVMInlineAsmDialect Dialect, LLVMBool CanThrow) {
470 InlineAsm::AsmDialect AD;
471 switch (Dialect) {
472 case LLVMInlineAsmDialectATT:
473 AD = InlineAsm::AD_ATT;
474 break;
475 case LLVMInlineAsmDialectIntel:
476 AD = InlineAsm::AD_Intel;
477 break;
479 return wrap(InlineAsm::get(unwrap<FunctionType>(Ty),
480 StringRef(AsmString, AsmStringSize),
481 StringRef(Constraints, ConstraintsSize),
482 HasSideEffects, IsAlignStack, AD, CanThrow));
485 const char *LLVMGetInlineAsmAsmString(LLVMValueRef InlineAsmVal, size_t *Len) {
487 Value *Val = unwrap<Value>(InlineAsmVal);
488 const std::string &AsmString = cast<InlineAsm>(Val)->getAsmString();
490 *Len = AsmString.length();
491 return AsmString.c_str();
494 const char *LLVMGetInlineAsmConstraintString(LLVMValueRef InlineAsmVal,
495 size_t *Len) {
496 Value *Val = unwrap<Value>(InlineAsmVal);
497 const std::string &ConstraintString =
498 cast<InlineAsm>(Val)->getConstraintString();
500 *Len = ConstraintString.length();
501 return ConstraintString.c_str();
504 LLVMInlineAsmDialect LLVMGetInlineAsmDialect(LLVMValueRef InlineAsmVal) {
506 Value *Val = unwrap<Value>(InlineAsmVal);
507 InlineAsm::AsmDialect Dialect = cast<InlineAsm>(Val)->getDialect();
509 switch (Dialect) {
510 case InlineAsm::AD_ATT:
511 return LLVMInlineAsmDialectATT;
512 case InlineAsm::AD_Intel:
513 return LLVMInlineAsmDialectIntel;
516 llvm_unreachable("Unrecognized inline assembly dialect");
517 return LLVMInlineAsmDialectATT;
520 LLVMTypeRef LLVMGetInlineAsmFunctionType(LLVMValueRef InlineAsmVal) {
521 Value *Val = unwrap<Value>(InlineAsmVal);
522 return (LLVMTypeRef)cast<InlineAsm>(Val)->getFunctionType();
525 LLVMBool LLVMGetInlineAsmHasSideEffects(LLVMValueRef InlineAsmVal) {
526 Value *Val = unwrap<Value>(InlineAsmVal);
527 return cast<InlineAsm>(Val)->hasSideEffects();
530 LLVMBool LLVMGetInlineAsmNeedsAlignedStack(LLVMValueRef InlineAsmVal) {
531 Value *Val = unwrap<Value>(InlineAsmVal);
532 return cast<InlineAsm>(Val)->isAlignStack();
535 LLVMBool LLVMGetInlineAsmCanUnwind(LLVMValueRef InlineAsmVal) {
536 Value *Val = unwrap<Value>(InlineAsmVal);
537 return cast<InlineAsm>(Val)->canThrow();
540 /*--.. Operations on module contexts ......................................--*/
541 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
542 return wrap(&unwrap(M)->getContext());
546 /*===-- Operations on types -----------------------------------------------===*/
548 /*--.. Operations on all types (mostly) ....................................--*/
550 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
551 switch (unwrap(Ty)->getTypeID()) {
552 case Type::VoidTyID:
553 return LLVMVoidTypeKind;
554 case Type::HalfTyID:
555 return LLVMHalfTypeKind;
556 case Type::BFloatTyID:
557 return LLVMBFloatTypeKind;
558 case Type::FloatTyID:
559 return LLVMFloatTypeKind;
560 case Type::DoubleTyID:
561 return LLVMDoubleTypeKind;
562 case Type::X86_FP80TyID:
563 return LLVMX86_FP80TypeKind;
564 case Type::FP128TyID:
565 return LLVMFP128TypeKind;
566 case Type::PPC_FP128TyID:
567 return LLVMPPC_FP128TypeKind;
568 case Type::LabelTyID:
569 return LLVMLabelTypeKind;
570 case Type::MetadataTyID:
571 return LLVMMetadataTypeKind;
572 case Type::IntegerTyID:
573 return LLVMIntegerTypeKind;
574 case Type::FunctionTyID:
575 return LLVMFunctionTypeKind;
576 case Type::StructTyID:
577 return LLVMStructTypeKind;
578 case Type::ArrayTyID:
579 return LLVMArrayTypeKind;
580 case Type::PointerTyID:
581 return LLVMPointerTypeKind;
582 case Type::FixedVectorTyID:
583 return LLVMVectorTypeKind;
584 case Type::X86_MMXTyID:
585 return LLVMX86_MMXTypeKind;
586 case Type::X86_AMXTyID:
587 return LLVMX86_AMXTypeKind;
588 case Type::TokenTyID:
589 return LLVMTokenTypeKind;
590 case Type::ScalableVectorTyID:
591 return LLVMScalableVectorTypeKind;
592 case Type::TargetExtTyID:
593 return LLVMTargetExtTypeKind;
594 case Type::TypedPointerTyID:
595 llvm_unreachable("Typed pointers are unsupported via the C API");
597 llvm_unreachable("Unhandled TypeID.");
600 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
602 return unwrap(Ty)->isSized();
605 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
606 return wrap(&unwrap(Ty)->getContext());
609 void LLVMDumpType(LLVMTypeRef Ty) {
610 return unwrap(Ty)->print(errs(), /*IsForDebug=*/true);
613 char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
614 std::string buf;
615 raw_string_ostream os(buf);
617 if (unwrap(Ty))
618 unwrap(Ty)->print(os);
619 else
620 os << "Printing <null> Type";
622 os.flush();
624 return strdup(buf.c_str());
627 /*--.. Operations on integer types .........................................--*/
629 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
630 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
632 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
633 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
635 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
636 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
638 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
639 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
641 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
642 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
644 LLVMTypeRef LLVMInt128TypeInContext(LLVMContextRef C) {
645 return (LLVMTypeRef) Type::getInt128Ty(*unwrap(C));
647 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
648 return wrap(IntegerType::get(*unwrap(C), NumBits));
651 LLVMTypeRef LLVMInt1Type(void) {
652 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
654 LLVMTypeRef LLVMInt8Type(void) {
655 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
657 LLVMTypeRef LLVMInt16Type(void) {
658 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
660 LLVMTypeRef LLVMInt32Type(void) {
661 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
663 LLVMTypeRef LLVMInt64Type(void) {
664 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
666 LLVMTypeRef LLVMInt128Type(void) {
667 return LLVMInt128TypeInContext(LLVMGetGlobalContext());
669 LLVMTypeRef LLVMIntType(unsigned NumBits) {
670 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
673 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
674 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
677 /*--.. Operations on real types ............................................--*/
679 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
680 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
682 LLVMTypeRef LLVMBFloatTypeInContext(LLVMContextRef C) {
683 return (LLVMTypeRef) Type::getBFloatTy(*unwrap(C));
685 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
686 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
688 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
689 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
691 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
692 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
694 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
695 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
697 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
698 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
700 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
701 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
703 LLVMTypeRef LLVMX86AMXTypeInContext(LLVMContextRef C) {
704 return (LLVMTypeRef) Type::getX86_AMXTy(*unwrap(C));
707 LLVMTypeRef LLVMHalfType(void) {
708 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
710 LLVMTypeRef LLVMBFloatType(void) {
711 return LLVMBFloatTypeInContext(LLVMGetGlobalContext());
713 LLVMTypeRef LLVMFloatType(void) {
714 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
716 LLVMTypeRef LLVMDoubleType(void) {
717 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
719 LLVMTypeRef LLVMX86FP80Type(void) {
720 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
722 LLVMTypeRef LLVMFP128Type(void) {
723 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
725 LLVMTypeRef LLVMPPCFP128Type(void) {
726 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
728 LLVMTypeRef LLVMX86MMXType(void) {
729 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
731 LLVMTypeRef LLVMX86AMXType(void) {
732 return LLVMX86AMXTypeInContext(LLVMGetGlobalContext());
735 /*--.. Operations on function types ........................................--*/
737 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
738 LLVMTypeRef *ParamTypes, unsigned ParamCount,
739 LLVMBool IsVarArg) {
740 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
741 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
744 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
745 return unwrap<FunctionType>(FunctionTy)->isVarArg();
748 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
749 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
752 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
753 return unwrap<FunctionType>(FunctionTy)->getNumParams();
756 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
757 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
758 for (Type *T : Ty->params())
759 *Dest++ = wrap(T);
762 /*--.. Operations on struct types ..........................................--*/
764 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
765 unsigned ElementCount, LLVMBool Packed) {
766 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
767 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
770 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
771 unsigned ElementCount, LLVMBool Packed) {
772 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
773 ElementCount, Packed);
776 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
778 return wrap(StructType::create(*unwrap(C), Name));
781 const char *LLVMGetStructName(LLVMTypeRef Ty)
783 StructType *Type = unwrap<StructType>(Ty);
784 if (!Type->hasName())
785 return nullptr;
786 return Type->getName().data();
789 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
790 unsigned ElementCount, LLVMBool Packed) {
791 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
792 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
795 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
796 return unwrap<StructType>(StructTy)->getNumElements();
799 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
800 StructType *Ty = unwrap<StructType>(StructTy);
801 for (Type *T : Ty->elements())
802 *Dest++ = wrap(T);
805 LLVMTypeRef LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy, unsigned i) {
806 StructType *Ty = unwrap<StructType>(StructTy);
807 return wrap(Ty->getTypeAtIndex(i));
810 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
811 return unwrap<StructType>(StructTy)->isPacked();
814 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
815 return unwrap<StructType>(StructTy)->isOpaque();
818 LLVMBool LLVMIsLiteralStruct(LLVMTypeRef StructTy) {
819 return unwrap<StructType>(StructTy)->isLiteral();
822 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
823 return wrap(StructType::getTypeByName(unwrap(M)->getContext(), Name));
826 LLVMTypeRef LLVMGetTypeByName2(LLVMContextRef C, const char *Name) {
827 return wrap(StructType::getTypeByName(*unwrap(C), Name));
830 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
832 void LLVMGetSubtypes(LLVMTypeRef Tp, LLVMTypeRef *Arr) {
833 int i = 0;
834 for (auto *T : unwrap(Tp)->subtypes()) {
835 Arr[i] = wrap(T);
836 i++;
840 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
841 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
844 LLVMTypeRef LLVMArrayType2(LLVMTypeRef ElementType, uint64_t ElementCount) {
845 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
848 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
849 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
852 LLVMBool LLVMPointerTypeIsOpaque(LLVMTypeRef Ty) {
853 return true;
856 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
857 return wrap(FixedVectorType::get(unwrap(ElementType), ElementCount));
860 LLVMTypeRef LLVMScalableVectorType(LLVMTypeRef ElementType,
861 unsigned ElementCount) {
862 return wrap(ScalableVectorType::get(unwrap(ElementType), ElementCount));
865 LLVMTypeRef LLVMGetElementType(LLVMTypeRef WrappedTy) {
866 auto *Ty = unwrap(WrappedTy);
867 if (auto *ATy = dyn_cast<ArrayType>(Ty))
868 return wrap(ATy->getElementType());
869 return wrap(cast<VectorType>(Ty)->getElementType());
872 unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp) {
873 return unwrap(Tp)->getNumContainedTypes();
876 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
877 return unwrap<ArrayType>(ArrayTy)->getNumElements();
880 uint64_t LLVMGetArrayLength2(LLVMTypeRef ArrayTy) {
881 return unwrap<ArrayType>(ArrayTy)->getNumElements();
884 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
885 return unwrap<PointerType>(PointerTy)->getAddressSpace();
888 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
889 return unwrap<VectorType>(VectorTy)->getElementCount().getKnownMinValue();
892 /*--.. Operations on other types ...........................................--*/
894 LLVMTypeRef LLVMPointerTypeInContext(LLVMContextRef C, unsigned AddressSpace) {
895 return wrap(PointerType::get(*unwrap(C), AddressSpace));
898 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
899 return wrap(Type::getVoidTy(*unwrap(C)));
901 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
902 return wrap(Type::getLabelTy(*unwrap(C)));
904 LLVMTypeRef LLVMTokenTypeInContext(LLVMContextRef C) {
905 return wrap(Type::getTokenTy(*unwrap(C)));
907 LLVMTypeRef LLVMMetadataTypeInContext(LLVMContextRef C) {
908 return wrap(Type::getMetadataTy(*unwrap(C)));
911 LLVMTypeRef LLVMVoidType(void) {
912 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
914 LLVMTypeRef LLVMLabelType(void) {
915 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
918 LLVMTypeRef LLVMTargetExtTypeInContext(LLVMContextRef C, const char *Name,
919 LLVMTypeRef *TypeParams,
920 unsigned TypeParamCount,
921 unsigned *IntParams,
922 unsigned IntParamCount) {
923 ArrayRef<Type *> TypeParamArray(unwrap(TypeParams), TypeParamCount);
924 ArrayRef<unsigned> IntParamArray(IntParams, IntParamCount);
925 return wrap(
926 TargetExtType::get(*unwrap(C), Name, TypeParamArray, IntParamArray));
929 /*===-- Operations on values ----------------------------------------------===*/
931 /*--.. Operations on all values ............................................--*/
933 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
934 return wrap(unwrap(Val)->getType());
937 LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) {
938 switch(unwrap(Val)->getValueID()) {
939 #define LLVM_C_API 1
940 #define HANDLE_VALUE(Name) \
941 case Value::Name##Val: \
942 return LLVM##Name##ValueKind;
943 #include "llvm/IR/Value.def"
944 default:
945 return LLVMInstructionValueKind;
949 const char *LLVMGetValueName2(LLVMValueRef Val, size_t *Length) {
950 auto *V = unwrap(Val);
951 *Length = V->getName().size();
952 return V->getName().data();
955 void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen) {
956 unwrap(Val)->setName(StringRef(Name, NameLen));
959 const char *LLVMGetValueName(LLVMValueRef Val) {
960 return unwrap(Val)->getName().data();
963 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
964 unwrap(Val)->setName(Name);
967 void LLVMDumpValue(LLVMValueRef Val) {
968 unwrap(Val)->print(errs(), /*IsForDebug=*/true);
971 char* LLVMPrintValueToString(LLVMValueRef Val) {
972 std::string buf;
973 raw_string_ostream os(buf);
975 if (unwrap(Val))
976 unwrap(Val)->print(os);
977 else
978 os << "Printing <null> Value";
980 os.flush();
982 return strdup(buf.c_str());
985 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
986 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
989 int LLVMHasMetadata(LLVMValueRef Inst) {
990 return unwrap<Instruction>(Inst)->hasMetadata();
993 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
994 auto *I = unwrap<Instruction>(Inst);
995 assert(I && "Expected instruction");
996 if (auto *MD = I->getMetadata(KindID))
997 return wrap(MetadataAsValue::get(I->getContext(), MD));
998 return nullptr;
1001 // MetadataAsValue uses a canonical format which strips the actual MDNode for
1002 // MDNode with just a single constant value, storing just a ConstantAsMetadata
1003 // This undoes this canonicalization, reconstructing the MDNode.
1004 static MDNode *extractMDNode(MetadataAsValue *MAV) {
1005 Metadata *MD = MAV->getMetadata();
1006 assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
1007 "Expected a metadata node or a canonicalized constant");
1009 if (MDNode *N = dyn_cast<MDNode>(MD))
1010 return N;
1012 return MDNode::get(MAV->getContext(), MD);
1015 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
1016 MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
1018 unwrap<Instruction>(Inst)->setMetadata(KindID, N);
1021 struct LLVMOpaqueValueMetadataEntry {
1022 unsigned Kind;
1023 LLVMMetadataRef Metadata;
1026 using MetadataEntries = SmallVectorImpl<std::pair<unsigned, MDNode *>>;
1027 static LLVMValueMetadataEntry *
1028 llvm_getMetadata(size_t *NumEntries,
1029 llvm::function_ref<void(MetadataEntries &)> AccessMD) {
1030 SmallVector<std::pair<unsigned, MDNode *>, 8> MVEs;
1031 AccessMD(MVEs);
1033 LLVMOpaqueValueMetadataEntry *Result =
1034 static_cast<LLVMOpaqueValueMetadataEntry *>(
1035 safe_malloc(MVEs.size() * sizeof(LLVMOpaqueValueMetadataEntry)));
1036 for (unsigned i = 0; i < MVEs.size(); ++i) {
1037 const auto &ModuleFlag = MVEs[i];
1038 Result[i].Kind = ModuleFlag.first;
1039 Result[i].Metadata = wrap(ModuleFlag.second);
1041 *NumEntries = MVEs.size();
1042 return Result;
1045 LLVMValueMetadataEntry *
1046 LLVMInstructionGetAllMetadataOtherThanDebugLoc(LLVMValueRef Value,
1047 size_t *NumEntries) {
1048 return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
1049 Entries.clear();
1050 unwrap<Instruction>(Value)->getAllMetadata(Entries);
1054 /*--.. Conversion functions ................................................--*/
1056 #define LLVM_DEFINE_VALUE_CAST(name) \
1057 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
1058 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
1061 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
1063 LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) {
1064 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
1065 if (isa<MDNode>(MD->getMetadata()) ||
1066 isa<ValueAsMetadata>(MD->getMetadata()))
1067 return Val;
1068 return nullptr;
1071 LLVMValueRef LLVMIsAValueAsMetadata(LLVMValueRef Val) {
1072 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
1073 if (isa<ValueAsMetadata>(MD->getMetadata()))
1074 return Val;
1075 return nullptr;
1078 LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) {
1079 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
1080 if (isa<MDString>(MD->getMetadata()))
1081 return Val;
1082 return nullptr;
1085 /*--.. Operations on Uses ..................................................--*/
1086 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
1087 Value *V = unwrap(Val);
1088 Value::use_iterator I = V->use_begin();
1089 if (I == V->use_end())
1090 return nullptr;
1091 return wrap(&*I);
1094 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
1095 Use *Next = unwrap(U)->getNext();
1096 if (Next)
1097 return wrap(Next);
1098 return nullptr;
1101 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
1102 return wrap(unwrap(U)->getUser());
1105 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
1106 return wrap(unwrap(U)->get());
1109 /*--.. Operations on Users .................................................--*/
1111 static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N,
1112 unsigned Index) {
1113 Metadata *Op = N->getOperand(Index);
1114 if (!Op)
1115 return nullptr;
1116 if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
1117 return wrap(C->getValue());
1118 return wrap(MetadataAsValue::get(Context, Op));
1121 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
1122 Value *V = unwrap(Val);
1123 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
1124 if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1125 assert(Index == 0 && "Function-local metadata can only have one operand");
1126 return wrap(L->getValue());
1128 return getMDNodeOperandImpl(V->getContext(),
1129 cast<MDNode>(MD->getMetadata()), Index);
1132 return wrap(cast<User>(V)->getOperand(Index));
1135 LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) {
1136 Value *V = unwrap(Val);
1137 return wrap(&cast<User>(V)->getOperandUse(Index));
1140 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
1141 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
1144 int LLVMGetNumOperands(LLVMValueRef Val) {
1145 Value *V = unwrap(Val);
1146 if (isa<MetadataAsValue>(V))
1147 return LLVMGetMDNodeNumOperands(Val);
1149 return cast<User>(V)->getNumOperands();
1152 /*--.. Operations on constants of any type .................................--*/
1154 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
1155 return wrap(Constant::getNullValue(unwrap(Ty)));
1158 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
1159 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
1162 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
1163 return wrap(UndefValue::get(unwrap(Ty)));
1166 LLVMValueRef LLVMGetPoison(LLVMTypeRef Ty) {
1167 return wrap(PoisonValue::get(unwrap(Ty)));
1170 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
1171 return isa<Constant>(unwrap(Ty));
1174 LLVMBool LLVMIsNull(LLVMValueRef Val) {
1175 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
1176 return C->isNullValue();
1177 return false;
1180 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
1181 return isa<UndefValue>(unwrap(Val));
1184 LLVMBool LLVMIsPoison(LLVMValueRef Val) {
1185 return isa<PoisonValue>(unwrap(Val));
1188 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
1189 return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
1192 /*--.. Operations on metadata nodes ........................................--*/
1194 LLVMMetadataRef LLVMMDStringInContext2(LLVMContextRef C, const char *Str,
1195 size_t SLen) {
1196 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
1199 LLVMMetadataRef LLVMMDNodeInContext2(LLVMContextRef C, LLVMMetadataRef *MDs,
1200 size_t Count) {
1201 return wrap(MDNode::get(*unwrap(C), ArrayRef<Metadata*>(unwrap(MDs), Count)));
1204 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
1205 unsigned SLen) {
1206 LLVMContext &Context = *unwrap(C);
1207 return wrap(MetadataAsValue::get(
1208 Context, MDString::get(Context, StringRef(Str, SLen))));
1211 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
1212 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
1215 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
1216 unsigned Count) {
1217 LLVMContext &Context = *unwrap(C);
1218 SmallVector<Metadata *, 8> MDs;
1219 for (auto *OV : ArrayRef(Vals, Count)) {
1220 Value *V = unwrap(OV);
1221 Metadata *MD;
1222 if (!V)
1223 MD = nullptr;
1224 else if (auto *C = dyn_cast<Constant>(V))
1225 MD = ConstantAsMetadata::get(C);
1226 else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
1227 MD = MDV->getMetadata();
1228 assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
1229 "outside of direct argument to call");
1230 } else {
1231 // This is function-local metadata. Pretend to make an MDNode.
1232 assert(Count == 1 &&
1233 "Expected only one operand to function-local metadata");
1234 return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V)));
1237 MDs.push_back(MD);
1239 return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs)));
1242 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
1243 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
1246 LLVMValueRef LLVMMetadataAsValue(LLVMContextRef C, LLVMMetadataRef MD) {
1247 return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
1250 LLVMMetadataRef LLVMValueAsMetadata(LLVMValueRef Val) {
1251 auto *V = unwrap(Val);
1252 if (auto *C = dyn_cast<Constant>(V))
1253 return wrap(ConstantAsMetadata::get(C));
1254 if (auto *MAV = dyn_cast<MetadataAsValue>(V))
1255 return wrap(MAV->getMetadata());
1256 return wrap(ValueAsMetadata::get(V));
1259 const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
1260 if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
1261 if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
1262 *Length = S->getString().size();
1263 return S->getString().data();
1265 *Length = 0;
1266 return nullptr;
1269 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) {
1270 auto *MD = unwrap<MetadataAsValue>(V);
1271 if (isa<ValueAsMetadata>(MD->getMetadata()))
1272 return 1;
1273 return cast<MDNode>(MD->getMetadata())->getNumOperands();
1276 LLVMNamedMDNodeRef LLVMGetFirstNamedMetadata(LLVMModuleRef M) {
1277 Module *Mod = unwrap(M);
1278 Module::named_metadata_iterator I = Mod->named_metadata_begin();
1279 if (I == Mod->named_metadata_end())
1280 return nullptr;
1281 return wrap(&*I);
1284 LLVMNamedMDNodeRef LLVMGetLastNamedMetadata(LLVMModuleRef M) {
1285 Module *Mod = unwrap(M);
1286 Module::named_metadata_iterator I = Mod->named_metadata_end();
1287 if (I == Mod->named_metadata_begin())
1288 return nullptr;
1289 return wrap(&*--I);
1292 LLVMNamedMDNodeRef LLVMGetNextNamedMetadata(LLVMNamedMDNodeRef NMD) {
1293 NamedMDNode *NamedNode = unwrap(NMD);
1294 Module::named_metadata_iterator I(NamedNode);
1295 if (++I == NamedNode->getParent()->named_metadata_end())
1296 return nullptr;
1297 return wrap(&*I);
1300 LLVMNamedMDNodeRef LLVMGetPreviousNamedMetadata(LLVMNamedMDNodeRef NMD) {
1301 NamedMDNode *NamedNode = unwrap(NMD);
1302 Module::named_metadata_iterator I(NamedNode);
1303 if (I == NamedNode->getParent()->named_metadata_begin())
1304 return nullptr;
1305 return wrap(&*--I);
1308 LLVMNamedMDNodeRef LLVMGetNamedMetadata(LLVMModuleRef M,
1309 const char *Name, size_t NameLen) {
1310 return wrap(unwrap(M)->getNamedMetadata(StringRef(Name, NameLen)));
1313 LLVMNamedMDNodeRef LLVMGetOrInsertNamedMetadata(LLVMModuleRef M,
1314 const char *Name, size_t NameLen) {
1315 return wrap(unwrap(M)->getOrInsertNamedMetadata({Name, NameLen}));
1318 const char *LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NMD, size_t *NameLen) {
1319 NamedMDNode *NamedNode = unwrap(NMD);
1320 *NameLen = NamedNode->getName().size();
1321 return NamedNode->getName().data();
1324 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) {
1325 auto *MD = unwrap<MetadataAsValue>(V);
1326 if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1327 *Dest = wrap(MDV->getValue());
1328 return;
1330 const auto *N = cast<MDNode>(MD->getMetadata());
1331 const unsigned numOperands = N->getNumOperands();
1332 LLVMContext &Context = unwrap(V)->getContext();
1333 for (unsigned i = 0; i < numOperands; i++)
1334 Dest[i] = getMDNodeOperandImpl(Context, N, i);
1337 void LLVMReplaceMDNodeOperandWith(LLVMValueRef V, unsigned Index,
1338 LLVMMetadataRef Replacement) {
1339 auto *MD = cast<MetadataAsValue>(unwrap(V));
1340 auto *N = cast<MDNode>(MD->getMetadata());
1341 N->replaceOperandWith(Index, unwrap<Metadata>(Replacement));
1344 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char *Name) {
1345 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
1346 return N->getNumOperands();
1348 return 0;
1351 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char *Name,
1352 LLVMValueRef *Dest) {
1353 NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
1354 if (!N)
1355 return;
1356 LLVMContext &Context = unwrap(M)->getContext();
1357 for (unsigned i=0;i<N->getNumOperands();i++)
1358 Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
1361 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char *Name,
1362 LLVMValueRef Val) {
1363 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
1364 if (!N)
1365 return;
1366 if (!Val)
1367 return;
1368 N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
1371 const char *LLVMGetDebugLocDirectory(LLVMValueRef Val, unsigned *Length) {
1372 if (!Length) return nullptr;
1373 StringRef S;
1374 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1375 if (const auto &DL = I->getDebugLoc()) {
1376 S = DL->getDirectory();
1378 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1379 SmallVector<DIGlobalVariableExpression *, 1> GVEs;
1380 GV->getDebugInfo(GVEs);
1381 if (GVEs.size())
1382 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1383 S = DGV->getDirectory();
1384 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1385 if (const DISubprogram *DSP = F->getSubprogram())
1386 S = DSP->getDirectory();
1387 } else {
1388 assert(0 && "Expected Instruction, GlobalVariable or Function");
1389 return nullptr;
1391 *Length = S.size();
1392 return S.data();
1395 const char *LLVMGetDebugLocFilename(LLVMValueRef Val, unsigned *Length) {
1396 if (!Length) return nullptr;
1397 StringRef S;
1398 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1399 if (const auto &DL = I->getDebugLoc()) {
1400 S = DL->getFilename();
1402 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1403 SmallVector<DIGlobalVariableExpression *, 1> GVEs;
1404 GV->getDebugInfo(GVEs);
1405 if (GVEs.size())
1406 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1407 S = DGV->getFilename();
1408 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1409 if (const DISubprogram *DSP = F->getSubprogram())
1410 S = DSP->getFilename();
1411 } else {
1412 assert(0 && "Expected Instruction, GlobalVariable or Function");
1413 return nullptr;
1415 *Length = S.size();
1416 return S.data();
1419 unsigned LLVMGetDebugLocLine(LLVMValueRef Val) {
1420 unsigned L = 0;
1421 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1422 if (const auto &DL = I->getDebugLoc()) {
1423 L = DL->getLine();
1425 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1426 SmallVector<DIGlobalVariableExpression *, 1> GVEs;
1427 GV->getDebugInfo(GVEs);
1428 if (GVEs.size())
1429 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1430 L = DGV->getLine();
1431 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1432 if (const DISubprogram *DSP = F->getSubprogram())
1433 L = DSP->getLine();
1434 } else {
1435 assert(0 && "Expected Instruction, GlobalVariable or Function");
1436 return -1;
1438 return L;
1441 unsigned LLVMGetDebugLocColumn(LLVMValueRef Val) {
1442 unsigned C = 0;
1443 if (const auto *I = dyn_cast<Instruction>(unwrap(Val)))
1444 if (const auto &DL = I->getDebugLoc())
1445 C = DL->getColumn();
1446 return C;
1449 /*--.. Operations on scalar constants ......................................--*/
1451 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
1452 LLVMBool SignExtend) {
1453 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
1456 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
1457 unsigned NumWords,
1458 const uint64_t Words[]) {
1459 IntegerType *Ty = unwrap<IntegerType>(IntTy);
1460 return wrap(ConstantInt::get(
1461 Ty->getContext(), APInt(Ty->getBitWidth(), ArrayRef(Words, NumWords))));
1464 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
1465 uint8_t Radix) {
1466 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
1467 Radix));
1470 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
1471 unsigned SLen, uint8_t Radix) {
1472 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
1473 Radix));
1476 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
1477 return wrap(ConstantFP::get(unwrap(RealTy), N));
1480 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
1481 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
1484 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
1485 unsigned SLen) {
1486 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
1489 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
1490 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
1493 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
1494 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
1497 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
1498 ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
1499 Type *Ty = cFP->getType();
1501 if (Ty->isHalfTy() || Ty->isBFloatTy() || Ty->isFloatTy() ||
1502 Ty->isDoubleTy()) {
1503 *LosesInfo = false;
1504 return cFP->getValueAPF().convertToDouble();
1507 bool APFLosesInfo;
1508 APFloat APF = cFP->getValueAPF();
1509 APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo);
1510 *LosesInfo = APFLosesInfo;
1511 return APF.convertToDouble();
1514 /*--.. Operations on composite constants ...................................--*/
1516 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
1517 unsigned Length,
1518 LLVMBool DontNullTerminate) {
1519 /* Inverted the sense of AddNull because ', 0)' is a
1520 better mnemonic for null termination than ', 1)'. */
1521 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
1522 DontNullTerminate == 0));
1525 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
1526 LLVMBool DontNullTerminate) {
1527 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
1528 DontNullTerminate);
1531 LLVMValueRef LLVMGetAggregateElement(LLVMValueRef C, unsigned Idx) {
1532 return wrap(unwrap<Constant>(C)->getAggregateElement(Idx));
1535 LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef C, unsigned idx) {
1536 return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
1539 LLVMBool LLVMIsConstantString(LLVMValueRef C) {
1540 return unwrap<ConstantDataSequential>(C)->isString();
1543 const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1544 StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1545 *Length = Str.size();
1546 return Str.data();
1549 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
1550 LLVMValueRef *ConstantVals, unsigned Length) {
1551 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1552 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1555 LLVMValueRef LLVMConstArray2(LLVMTypeRef ElementTy, LLVMValueRef *ConstantVals,
1556 uint64_t Length) {
1557 ArrayRef<Constant *> V(unwrap<Constant>(ConstantVals, Length), Length);
1558 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1561 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
1562 LLVMValueRef *ConstantVals,
1563 unsigned Count, LLVMBool Packed) {
1564 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1565 return wrap(ConstantStruct::getAnon(*unwrap(C), ArrayRef(Elements, Count),
1566 Packed != 0));
1569 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
1570 LLVMBool Packed) {
1571 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1572 Packed);
1575 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
1576 LLVMValueRef *ConstantVals,
1577 unsigned Count) {
1578 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1579 StructType *Ty = unwrap<StructType>(StructTy);
1581 return wrap(ConstantStruct::get(Ty, ArrayRef(Elements, Count)));
1584 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
1585 return wrap(ConstantVector::get(
1586 ArrayRef(unwrap<Constant>(ScalarConstantVals, Size), Size)));
1589 /*-- Opcode mapping */
1591 static LLVMOpcode map_to_llvmopcode(int opcode)
1593 switch (opcode) {
1594 default: llvm_unreachable("Unhandled Opcode.");
1595 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
1596 #include "llvm/IR/Instruction.def"
1597 #undef HANDLE_INST
1601 static int map_from_llvmopcode(LLVMOpcode code)
1603 switch (code) {
1604 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
1605 #include "llvm/IR/Instruction.def"
1606 #undef HANDLE_INST
1608 llvm_unreachable("Unhandled Opcode.");
1611 /*--.. Constant expressions ................................................--*/
1613 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
1614 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
1617 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
1618 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
1621 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
1622 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
1625 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
1626 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1629 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
1630 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
1633 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
1634 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
1638 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
1639 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
1642 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1643 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
1644 unwrap<Constant>(RHSConstant)));
1647 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
1648 LLVMValueRef RHSConstant) {
1649 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
1650 unwrap<Constant>(RHSConstant)));
1653 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
1654 LLVMValueRef RHSConstant) {
1655 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
1656 unwrap<Constant>(RHSConstant)));
1659 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1660 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
1661 unwrap<Constant>(RHSConstant)));
1664 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
1665 LLVMValueRef RHSConstant) {
1666 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
1667 unwrap<Constant>(RHSConstant)));
1670 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
1671 LLVMValueRef RHSConstant) {
1672 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
1673 unwrap<Constant>(RHSConstant)));
1676 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1677 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
1678 unwrap<Constant>(RHSConstant)));
1681 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
1682 LLVMValueRef RHSConstant) {
1683 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
1684 unwrap<Constant>(RHSConstant)));
1687 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
1688 LLVMValueRef RHSConstant) {
1689 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
1690 unwrap<Constant>(RHSConstant)));
1693 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1694 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
1695 unwrap<Constant>(RHSConstant)));
1698 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
1699 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1700 return wrap(ConstantExpr::getICmp(Predicate,
1701 unwrap<Constant>(LHSConstant),
1702 unwrap<Constant>(RHSConstant)));
1705 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
1706 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1707 return wrap(ConstantExpr::getFCmp(Predicate,
1708 unwrap<Constant>(LHSConstant),
1709 unwrap<Constant>(RHSConstant)));
1712 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1713 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1714 unwrap<Constant>(RHSConstant)));
1717 LLVMValueRef LLVMConstGEP2(LLVMTypeRef Ty, LLVMValueRef ConstantVal,
1718 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1719 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1720 NumIndices);
1721 Constant *Val = unwrap<Constant>(ConstantVal);
1722 return wrap(ConstantExpr::getGetElementPtr(unwrap(Ty), Val, IdxList));
1725 LLVMValueRef LLVMConstInBoundsGEP2(LLVMTypeRef Ty, LLVMValueRef ConstantVal,
1726 LLVMValueRef *ConstantIndices,
1727 unsigned NumIndices) {
1728 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1729 NumIndices);
1730 Constant *Val = unwrap<Constant>(ConstantVal);
1731 return wrap(ConstantExpr::getInBoundsGetElementPtr(unwrap(Ty), Val, IdxList));
1734 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1735 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1736 unwrap(ToType)));
1739 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1740 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1741 unwrap(ToType)));
1744 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1745 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1746 unwrap(ToType)));
1749 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1750 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1751 unwrap(ToType)));
1754 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1755 LLVMTypeRef ToType) {
1756 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1757 unwrap(ToType)));
1760 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1761 LLVMTypeRef ToType) {
1762 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1763 unwrap(ToType)));
1766 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1767 LLVMTypeRef ToType) {
1768 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1769 unwrap(ToType)));
1772 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1773 LLVMValueRef IndexConstant) {
1774 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1775 unwrap<Constant>(IndexConstant)));
1778 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1779 LLVMValueRef ElementValueConstant,
1780 LLVMValueRef IndexConstant) {
1781 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1782 unwrap<Constant>(ElementValueConstant),
1783 unwrap<Constant>(IndexConstant)));
1786 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1787 LLVMValueRef VectorBConstant,
1788 LLVMValueRef MaskConstant) {
1789 SmallVector<int, 16> IntMask;
1790 ShuffleVectorInst::getShuffleMask(unwrap<Constant>(MaskConstant), IntMask);
1791 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1792 unwrap<Constant>(VectorBConstant),
1793 IntMask));
1796 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1797 const char *Constraints,
1798 LLVMBool HasSideEffects,
1799 LLVMBool IsAlignStack) {
1800 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1801 Constraints, HasSideEffects, IsAlignStack));
1804 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1805 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1808 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1810 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1811 return wrap(unwrap<GlobalValue>(Global)->getParent());
1814 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1815 return unwrap<GlobalValue>(Global)->isDeclaration();
1818 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1819 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1820 case GlobalValue::ExternalLinkage:
1821 return LLVMExternalLinkage;
1822 case GlobalValue::AvailableExternallyLinkage:
1823 return LLVMAvailableExternallyLinkage;
1824 case GlobalValue::LinkOnceAnyLinkage:
1825 return LLVMLinkOnceAnyLinkage;
1826 case GlobalValue::LinkOnceODRLinkage:
1827 return LLVMLinkOnceODRLinkage;
1828 case GlobalValue::WeakAnyLinkage:
1829 return LLVMWeakAnyLinkage;
1830 case GlobalValue::WeakODRLinkage:
1831 return LLVMWeakODRLinkage;
1832 case GlobalValue::AppendingLinkage:
1833 return LLVMAppendingLinkage;
1834 case GlobalValue::InternalLinkage:
1835 return LLVMInternalLinkage;
1836 case GlobalValue::PrivateLinkage:
1837 return LLVMPrivateLinkage;
1838 case GlobalValue::ExternalWeakLinkage:
1839 return LLVMExternalWeakLinkage;
1840 case GlobalValue::CommonLinkage:
1841 return LLVMCommonLinkage;
1844 llvm_unreachable("Invalid GlobalValue linkage!");
1847 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1848 GlobalValue *GV = unwrap<GlobalValue>(Global);
1850 switch (Linkage) {
1851 case LLVMExternalLinkage:
1852 GV->setLinkage(GlobalValue::ExternalLinkage);
1853 break;
1854 case LLVMAvailableExternallyLinkage:
1855 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1856 break;
1857 case LLVMLinkOnceAnyLinkage:
1858 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1859 break;
1860 case LLVMLinkOnceODRLinkage:
1861 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1862 break;
1863 case LLVMLinkOnceODRAutoHideLinkage:
1864 LLVM_DEBUG(
1865 errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1866 "longer supported.");
1867 break;
1868 case LLVMWeakAnyLinkage:
1869 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1870 break;
1871 case LLVMWeakODRLinkage:
1872 GV->setLinkage(GlobalValue::WeakODRLinkage);
1873 break;
1874 case LLVMAppendingLinkage:
1875 GV->setLinkage(GlobalValue::AppendingLinkage);
1876 break;
1877 case LLVMInternalLinkage:
1878 GV->setLinkage(GlobalValue::InternalLinkage);
1879 break;
1880 case LLVMPrivateLinkage:
1881 GV->setLinkage(GlobalValue::PrivateLinkage);
1882 break;
1883 case LLVMLinkerPrivateLinkage:
1884 GV->setLinkage(GlobalValue::PrivateLinkage);
1885 break;
1886 case LLVMLinkerPrivateWeakLinkage:
1887 GV->setLinkage(GlobalValue::PrivateLinkage);
1888 break;
1889 case LLVMDLLImportLinkage:
1890 LLVM_DEBUG(
1891 errs()
1892 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1893 break;
1894 case LLVMDLLExportLinkage:
1895 LLVM_DEBUG(
1896 errs()
1897 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1898 break;
1899 case LLVMExternalWeakLinkage:
1900 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1901 break;
1902 case LLVMGhostLinkage:
1903 LLVM_DEBUG(
1904 errs() << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1905 break;
1906 case LLVMCommonLinkage:
1907 GV->setLinkage(GlobalValue::CommonLinkage);
1908 break;
1912 const char *LLVMGetSection(LLVMValueRef Global) {
1913 // Using .data() is safe because of how GlobalObject::setSection is
1914 // implemented.
1915 return unwrap<GlobalValue>(Global)->getSection().data();
1918 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1919 unwrap<GlobalObject>(Global)->setSection(Section);
1922 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1923 return static_cast<LLVMVisibility>(
1924 unwrap<GlobalValue>(Global)->getVisibility());
1927 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1928 unwrap<GlobalValue>(Global)
1929 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1932 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
1933 return static_cast<LLVMDLLStorageClass>(
1934 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1937 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
1938 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1939 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1942 LLVMUnnamedAddr LLVMGetUnnamedAddress(LLVMValueRef Global) {
1943 switch (unwrap<GlobalValue>(Global)->getUnnamedAddr()) {
1944 case GlobalVariable::UnnamedAddr::None:
1945 return LLVMNoUnnamedAddr;
1946 case GlobalVariable::UnnamedAddr::Local:
1947 return LLVMLocalUnnamedAddr;
1948 case GlobalVariable::UnnamedAddr::Global:
1949 return LLVMGlobalUnnamedAddr;
1951 llvm_unreachable("Unknown UnnamedAddr kind!");
1954 void LLVMSetUnnamedAddress(LLVMValueRef Global, LLVMUnnamedAddr UnnamedAddr) {
1955 GlobalValue *GV = unwrap<GlobalValue>(Global);
1957 switch (UnnamedAddr) {
1958 case LLVMNoUnnamedAddr:
1959 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::None);
1960 case LLVMLocalUnnamedAddr:
1961 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Local);
1962 case LLVMGlobalUnnamedAddr:
1963 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Global);
1967 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
1968 return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
1971 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
1972 unwrap<GlobalValue>(Global)->setUnnamedAddr(
1973 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
1974 : GlobalValue::UnnamedAddr::None);
1977 LLVMTypeRef LLVMGlobalGetValueType(LLVMValueRef Global) {
1978 return wrap(unwrap<GlobalValue>(Global)->getValueType());
1981 /*--.. Operations on global variables, load and store instructions .........--*/
1983 unsigned LLVMGetAlignment(LLVMValueRef V) {
1984 Value *P = unwrap(V);
1985 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
1986 return GV->getAlign() ? GV->getAlign()->value() : 0;
1987 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1988 return AI->getAlign().value();
1989 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1990 return LI->getAlign().value();
1991 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1992 return SI->getAlign().value();
1993 if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
1994 return RMWI->getAlign().value();
1995 if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
1996 return CXI->getAlign().value();
1998 llvm_unreachable(
1999 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, "
2000 "and AtomicCmpXchgInst have alignment");
2003 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
2004 Value *P = unwrap(V);
2005 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2006 GV->setAlignment(MaybeAlign(Bytes));
2007 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
2008 AI->setAlignment(Align(Bytes));
2009 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
2010 LI->setAlignment(Align(Bytes));
2011 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
2012 SI->setAlignment(Align(Bytes));
2013 else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2014 RMWI->setAlignment(Align(Bytes));
2015 else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2016 CXI->setAlignment(Align(Bytes));
2017 else
2018 llvm_unreachable(
2019 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, and "
2020 "and AtomicCmpXchgInst have alignment");
2023 LLVMValueMetadataEntry *LLVMGlobalCopyAllMetadata(LLVMValueRef Value,
2024 size_t *NumEntries) {
2025 return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
2026 Entries.clear();
2027 if (Instruction *Instr = dyn_cast<Instruction>(unwrap(Value))) {
2028 Instr->getAllMetadata(Entries);
2029 } else {
2030 unwrap<GlobalObject>(Value)->getAllMetadata(Entries);
2035 unsigned LLVMValueMetadataEntriesGetKind(LLVMValueMetadataEntry *Entries,
2036 unsigned Index) {
2037 LLVMOpaqueValueMetadataEntry MVE =
2038 static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2039 return MVE.Kind;
2042 LLVMMetadataRef
2043 LLVMValueMetadataEntriesGetMetadata(LLVMValueMetadataEntry *Entries,
2044 unsigned Index) {
2045 LLVMOpaqueValueMetadataEntry MVE =
2046 static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2047 return MVE.Metadata;
2050 void LLVMDisposeValueMetadataEntries(LLVMValueMetadataEntry *Entries) {
2051 free(Entries);
2054 void LLVMGlobalSetMetadata(LLVMValueRef Global, unsigned Kind,
2055 LLVMMetadataRef MD) {
2056 unwrap<GlobalObject>(Global)->setMetadata(Kind, unwrap<MDNode>(MD));
2059 void LLVMGlobalEraseMetadata(LLVMValueRef Global, unsigned Kind) {
2060 unwrap<GlobalObject>(Global)->eraseMetadata(Kind);
2063 void LLVMGlobalClearMetadata(LLVMValueRef Global) {
2064 unwrap<GlobalObject>(Global)->clearMetadata();
2067 /*--.. Operations on global variables ......................................--*/
2069 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
2070 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2071 GlobalValue::ExternalLinkage, nullptr, Name));
2074 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
2075 const char *Name,
2076 unsigned AddressSpace) {
2077 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2078 GlobalValue::ExternalLinkage, nullptr, Name,
2079 nullptr, GlobalVariable::NotThreadLocal,
2080 AddressSpace));
2083 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
2084 return wrap(unwrap(M)->getNamedGlobal(Name));
2087 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
2088 Module *Mod = unwrap(M);
2089 Module::global_iterator I = Mod->global_begin();
2090 if (I == Mod->global_end())
2091 return nullptr;
2092 return wrap(&*I);
2095 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
2096 Module *Mod = unwrap(M);
2097 Module::global_iterator I = Mod->global_end();
2098 if (I == Mod->global_begin())
2099 return nullptr;
2100 return wrap(&*--I);
2103 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
2104 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2105 Module::global_iterator I(GV);
2106 if (++I == GV->getParent()->global_end())
2107 return nullptr;
2108 return wrap(&*I);
2111 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
2112 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2113 Module::global_iterator I(GV);
2114 if (I == GV->getParent()->global_begin())
2115 return nullptr;
2116 return wrap(&*--I);
2119 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
2120 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
2123 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
2124 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
2125 if ( !GV->hasInitializer() )
2126 return nullptr;
2127 return wrap(GV->getInitializer());
2130 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
2131 unwrap<GlobalVariable>(GlobalVar)
2132 ->setInitializer(unwrap<Constant>(ConstantVal));
2135 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
2136 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
2139 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
2140 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
2143 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
2144 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
2147 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
2148 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
2151 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
2152 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
2153 case GlobalVariable::NotThreadLocal:
2154 return LLVMNotThreadLocal;
2155 case GlobalVariable::GeneralDynamicTLSModel:
2156 return LLVMGeneralDynamicTLSModel;
2157 case GlobalVariable::LocalDynamicTLSModel:
2158 return LLVMLocalDynamicTLSModel;
2159 case GlobalVariable::InitialExecTLSModel:
2160 return LLVMInitialExecTLSModel;
2161 case GlobalVariable::LocalExecTLSModel:
2162 return LLVMLocalExecTLSModel;
2165 llvm_unreachable("Invalid GlobalVariable thread local mode");
2168 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
2169 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2171 switch (Mode) {
2172 case LLVMNotThreadLocal:
2173 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
2174 break;
2175 case LLVMGeneralDynamicTLSModel:
2176 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
2177 break;
2178 case LLVMLocalDynamicTLSModel:
2179 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
2180 break;
2181 case LLVMInitialExecTLSModel:
2182 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
2183 break;
2184 case LLVMLocalExecTLSModel:
2185 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
2186 break;
2190 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
2191 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
2194 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
2195 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
2198 /*--.. Operations on aliases ......................................--*/
2200 LLVMValueRef LLVMAddAlias2(LLVMModuleRef M, LLVMTypeRef ValueTy,
2201 unsigned AddrSpace, LLVMValueRef Aliasee,
2202 const char *Name) {
2203 return wrap(GlobalAlias::create(unwrap(ValueTy), AddrSpace,
2204 GlobalValue::ExternalLinkage, Name,
2205 unwrap<Constant>(Aliasee), unwrap(M)));
2208 LLVMValueRef LLVMGetNamedGlobalAlias(LLVMModuleRef M,
2209 const char *Name, size_t NameLen) {
2210 return wrap(unwrap(M)->getNamedAlias(StringRef(Name, NameLen)));
2213 LLVMValueRef LLVMGetFirstGlobalAlias(LLVMModuleRef M) {
2214 Module *Mod = unwrap(M);
2215 Module::alias_iterator I = Mod->alias_begin();
2216 if (I == Mod->alias_end())
2217 return nullptr;
2218 return wrap(&*I);
2221 LLVMValueRef LLVMGetLastGlobalAlias(LLVMModuleRef M) {
2222 Module *Mod = unwrap(M);
2223 Module::alias_iterator I = Mod->alias_end();
2224 if (I == Mod->alias_begin())
2225 return nullptr;
2226 return wrap(&*--I);
2229 LLVMValueRef LLVMGetNextGlobalAlias(LLVMValueRef GA) {
2230 GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2231 Module::alias_iterator I(Alias);
2232 if (++I == Alias->getParent()->alias_end())
2233 return nullptr;
2234 return wrap(&*I);
2237 LLVMValueRef LLVMGetPreviousGlobalAlias(LLVMValueRef GA) {
2238 GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2239 Module::alias_iterator I(Alias);
2240 if (I == Alias->getParent()->alias_begin())
2241 return nullptr;
2242 return wrap(&*--I);
2245 LLVMValueRef LLVMAliasGetAliasee(LLVMValueRef Alias) {
2246 return wrap(unwrap<GlobalAlias>(Alias)->getAliasee());
2249 void LLVMAliasSetAliasee(LLVMValueRef Alias, LLVMValueRef Aliasee) {
2250 unwrap<GlobalAlias>(Alias)->setAliasee(unwrap<Constant>(Aliasee));
2253 /*--.. Operations on functions .............................................--*/
2255 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
2256 LLVMTypeRef FunctionTy) {
2257 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
2258 GlobalValue::ExternalLinkage, Name, unwrap(M)));
2261 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
2262 return wrap(unwrap(M)->getFunction(Name));
2265 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
2266 Module *Mod = unwrap(M);
2267 Module::iterator I = Mod->begin();
2268 if (I == Mod->end())
2269 return nullptr;
2270 return wrap(&*I);
2273 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
2274 Module *Mod = unwrap(M);
2275 Module::iterator I = Mod->end();
2276 if (I == Mod->begin())
2277 return nullptr;
2278 return wrap(&*--I);
2281 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
2282 Function *Func = unwrap<Function>(Fn);
2283 Module::iterator I(Func);
2284 if (++I == Func->getParent()->end())
2285 return nullptr;
2286 return wrap(&*I);
2289 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
2290 Function *Func = unwrap<Function>(Fn);
2291 Module::iterator I(Func);
2292 if (I == Func->getParent()->begin())
2293 return nullptr;
2294 return wrap(&*--I);
2297 void LLVMDeleteFunction(LLVMValueRef Fn) {
2298 unwrap<Function>(Fn)->eraseFromParent();
2301 LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) {
2302 return unwrap<Function>(Fn)->hasPersonalityFn();
2305 LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) {
2306 return wrap(unwrap<Function>(Fn)->getPersonalityFn());
2309 void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) {
2310 unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
2313 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
2314 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
2315 return F->getIntrinsicID();
2316 return 0;
2319 static Intrinsic::ID llvm_map_to_intrinsic_id(unsigned ID) {
2320 assert(ID < llvm::Intrinsic::num_intrinsics && "Intrinsic ID out of range");
2321 return llvm::Intrinsic::ID(ID);
2324 LLVMValueRef LLVMGetIntrinsicDeclaration(LLVMModuleRef Mod,
2325 unsigned ID,
2326 LLVMTypeRef *ParamTypes,
2327 size_t ParamCount) {
2328 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2329 auto IID = llvm_map_to_intrinsic_id(ID);
2330 return wrap(llvm::Intrinsic::getDeclaration(unwrap(Mod), IID, Tys));
2333 const char *LLVMIntrinsicGetName(unsigned ID, size_t *NameLength) {
2334 auto IID = llvm_map_to_intrinsic_id(ID);
2335 auto Str = llvm::Intrinsic::getName(IID);
2336 *NameLength = Str.size();
2337 return Str.data();
2340 LLVMTypeRef LLVMIntrinsicGetType(LLVMContextRef Ctx, unsigned ID,
2341 LLVMTypeRef *ParamTypes, size_t ParamCount) {
2342 auto IID = llvm_map_to_intrinsic_id(ID);
2343 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2344 return wrap(llvm::Intrinsic::getType(*unwrap(Ctx), IID, Tys));
2347 const char *LLVMIntrinsicCopyOverloadedName(unsigned ID,
2348 LLVMTypeRef *ParamTypes,
2349 size_t ParamCount,
2350 size_t *NameLength) {
2351 auto IID = llvm_map_to_intrinsic_id(ID);
2352 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2353 auto Str = llvm::Intrinsic::getNameNoUnnamedTypes(IID, Tys);
2354 *NameLength = Str.length();
2355 return strdup(Str.c_str());
2358 const char *LLVMIntrinsicCopyOverloadedName2(LLVMModuleRef Mod, unsigned ID,
2359 LLVMTypeRef *ParamTypes,
2360 size_t ParamCount,
2361 size_t *NameLength) {
2362 auto IID = llvm_map_to_intrinsic_id(ID);
2363 ArrayRef<Type *> Tys(unwrap(ParamTypes), ParamCount);
2364 auto Str = llvm::Intrinsic::getName(IID, Tys, unwrap(Mod));
2365 *NameLength = Str.length();
2366 return strdup(Str.c_str());
2369 unsigned LLVMLookupIntrinsicID(const char *Name, size_t NameLen) {
2370 return Function::lookupIntrinsicID({Name, NameLen});
2373 LLVMBool LLVMIntrinsicIsOverloaded(unsigned ID) {
2374 auto IID = llvm_map_to_intrinsic_id(ID);
2375 return llvm::Intrinsic::isOverloaded(IID);
2378 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
2379 return unwrap<Function>(Fn)->getCallingConv();
2382 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
2383 return unwrap<Function>(Fn)->setCallingConv(
2384 static_cast<CallingConv::ID>(CC));
2387 const char *LLVMGetGC(LLVMValueRef Fn) {
2388 Function *F = unwrap<Function>(Fn);
2389 return F->hasGC()? F->getGC().c_str() : nullptr;
2392 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
2393 Function *F = unwrap<Function>(Fn);
2394 if (GC)
2395 F->setGC(GC);
2396 else
2397 F->clearGC();
2400 void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2401 LLVMAttributeRef A) {
2402 unwrap<Function>(F)->addAttributeAtIndex(Idx, unwrap(A));
2405 unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) {
2406 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2407 return AS.getNumAttributes();
2410 void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2411 LLVMAttributeRef *Attrs) {
2412 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2413 for (auto A : AS)
2414 *Attrs++ = wrap(A);
2417 LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F,
2418 LLVMAttributeIndex Idx,
2419 unsigned KindID) {
2420 return wrap(unwrap<Function>(F)->getAttributeAtIndex(
2421 Idx, (Attribute::AttrKind)KindID));
2424 LLVMAttributeRef LLVMGetStringAttributeAtIndex(LLVMValueRef F,
2425 LLVMAttributeIndex Idx,
2426 const char *K, unsigned KLen) {
2427 return wrap(
2428 unwrap<Function>(F)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
2431 void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2432 unsigned KindID) {
2433 unwrap<Function>(F)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
2436 void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2437 const char *K, unsigned KLen) {
2438 unwrap<Function>(F)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
2441 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
2442 const char *V) {
2443 Function *Func = unwrap<Function>(Fn);
2444 Attribute Attr = Attribute::get(Func->getContext(), A, V);
2445 Func->addFnAttr(Attr);
2448 /*--.. Operations on parameters ............................................--*/
2450 unsigned LLVMCountParams(LLVMValueRef FnRef) {
2451 // This function is strictly redundant to
2452 // LLVMCountParamTypes(LLVMGlobalGetValueType(FnRef))
2453 return unwrap<Function>(FnRef)->arg_size();
2456 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
2457 Function *Fn = unwrap<Function>(FnRef);
2458 for (Argument &A : Fn->args())
2459 *ParamRefs++ = wrap(&A);
2462 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
2463 Function *Fn = unwrap<Function>(FnRef);
2464 return wrap(&Fn->arg_begin()[index]);
2467 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
2468 return wrap(unwrap<Argument>(V)->getParent());
2471 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
2472 Function *Func = unwrap<Function>(Fn);
2473 Function::arg_iterator I = Func->arg_begin();
2474 if (I == Func->arg_end())
2475 return nullptr;
2476 return wrap(&*I);
2479 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
2480 Function *Func = unwrap<Function>(Fn);
2481 Function::arg_iterator I = Func->arg_end();
2482 if (I == Func->arg_begin())
2483 return nullptr;
2484 return wrap(&*--I);
2487 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
2488 Argument *A = unwrap<Argument>(Arg);
2489 Function *Fn = A->getParent();
2490 if (A->getArgNo() + 1 >= Fn->arg_size())
2491 return nullptr;
2492 return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
2495 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
2496 Argument *A = unwrap<Argument>(Arg);
2497 if (A->getArgNo() == 0)
2498 return nullptr;
2499 return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
2502 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
2503 Argument *A = unwrap<Argument>(Arg);
2504 A->addAttr(Attribute::getWithAlignment(A->getContext(), Align(align)));
2507 /*--.. Operations on ifuncs ................................................--*/
2509 LLVMValueRef LLVMAddGlobalIFunc(LLVMModuleRef M,
2510 const char *Name, size_t NameLen,
2511 LLVMTypeRef Ty, unsigned AddrSpace,
2512 LLVMValueRef Resolver) {
2513 return wrap(GlobalIFunc::create(unwrap(Ty), AddrSpace,
2514 GlobalValue::ExternalLinkage,
2515 StringRef(Name, NameLen),
2516 unwrap<Constant>(Resolver), unwrap(M)));
2519 LLVMValueRef LLVMGetNamedGlobalIFunc(LLVMModuleRef M,
2520 const char *Name, size_t NameLen) {
2521 return wrap(unwrap(M)->getNamedIFunc(StringRef(Name, NameLen)));
2524 LLVMValueRef LLVMGetFirstGlobalIFunc(LLVMModuleRef M) {
2525 Module *Mod = unwrap(M);
2526 Module::ifunc_iterator I = Mod->ifunc_begin();
2527 if (I == Mod->ifunc_end())
2528 return nullptr;
2529 return wrap(&*I);
2532 LLVMValueRef LLVMGetLastGlobalIFunc(LLVMModuleRef M) {
2533 Module *Mod = unwrap(M);
2534 Module::ifunc_iterator I = Mod->ifunc_end();
2535 if (I == Mod->ifunc_begin())
2536 return nullptr;
2537 return wrap(&*--I);
2540 LLVMValueRef LLVMGetNextGlobalIFunc(LLVMValueRef IFunc) {
2541 GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2542 Module::ifunc_iterator I(GIF);
2543 if (++I == GIF->getParent()->ifunc_end())
2544 return nullptr;
2545 return wrap(&*I);
2548 LLVMValueRef LLVMGetPreviousGlobalIFunc(LLVMValueRef IFunc) {
2549 GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2550 Module::ifunc_iterator I(GIF);
2551 if (I == GIF->getParent()->ifunc_begin())
2552 return nullptr;
2553 return wrap(&*--I);
2556 LLVMValueRef LLVMGetGlobalIFuncResolver(LLVMValueRef IFunc) {
2557 return wrap(unwrap<GlobalIFunc>(IFunc)->getResolver());
2560 void LLVMSetGlobalIFuncResolver(LLVMValueRef IFunc, LLVMValueRef Resolver) {
2561 unwrap<GlobalIFunc>(IFunc)->setResolver(unwrap<Constant>(Resolver));
2564 void LLVMEraseGlobalIFunc(LLVMValueRef IFunc) {
2565 unwrap<GlobalIFunc>(IFunc)->eraseFromParent();
2568 void LLVMRemoveGlobalIFunc(LLVMValueRef IFunc) {
2569 unwrap<GlobalIFunc>(IFunc)->removeFromParent();
2572 /*--.. Operations on operand bundles........................................--*/
2574 LLVMOperandBundleRef LLVMCreateOperandBundle(const char *Tag, size_t TagLen,
2575 LLVMValueRef *Args,
2576 unsigned NumArgs) {
2577 return wrap(new OperandBundleDef(std::string(Tag, TagLen),
2578 ArrayRef(unwrap(Args), NumArgs)));
2581 void LLVMDisposeOperandBundle(LLVMOperandBundleRef Bundle) {
2582 delete unwrap(Bundle);
2585 const char *LLVMGetOperandBundleTag(LLVMOperandBundleRef Bundle, size_t *Len) {
2586 StringRef Str = unwrap(Bundle)->getTag();
2587 *Len = Str.size();
2588 return Str.data();
2591 unsigned LLVMGetNumOperandBundleArgs(LLVMOperandBundleRef Bundle) {
2592 return unwrap(Bundle)->inputs().size();
2595 LLVMValueRef LLVMGetOperandBundleArgAtIndex(LLVMOperandBundleRef Bundle,
2596 unsigned Index) {
2597 return wrap(unwrap(Bundle)->inputs()[Index]);
2600 /*--.. Operations on basic blocks ..........................................--*/
2602 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
2603 return wrap(static_cast<Value*>(unwrap(BB)));
2606 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
2607 return isa<BasicBlock>(unwrap(Val));
2610 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
2611 return wrap(unwrap<BasicBlock>(Val));
2614 const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) {
2615 return unwrap(BB)->getName().data();
2618 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
2619 return wrap(unwrap(BB)->getParent());
2622 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
2623 return wrap(unwrap(BB)->getTerminator());
2626 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
2627 return unwrap<Function>(FnRef)->size();
2630 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
2631 Function *Fn = unwrap<Function>(FnRef);
2632 for (BasicBlock &BB : *Fn)
2633 *BasicBlocksRefs++ = wrap(&BB);
2636 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
2637 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2640 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
2641 Function *Func = unwrap<Function>(Fn);
2642 Function::iterator I = Func->begin();
2643 if (I == Func->end())
2644 return nullptr;
2645 return wrap(&*I);
2648 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
2649 Function *Func = unwrap<Function>(Fn);
2650 Function::iterator I = Func->end();
2651 if (I == Func->begin())
2652 return nullptr;
2653 return wrap(&*--I);
2656 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
2657 BasicBlock *Block = unwrap(BB);
2658 Function::iterator I(Block);
2659 if (++I == Block->getParent()->end())
2660 return nullptr;
2661 return wrap(&*I);
2664 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
2665 BasicBlock *Block = unwrap(BB);
2666 Function::iterator I(Block);
2667 if (I == Block->getParent()->begin())
2668 return nullptr;
2669 return wrap(&*--I);
2672 LLVMBasicBlockRef LLVMCreateBasicBlockInContext(LLVMContextRef C,
2673 const char *Name) {
2674 return wrap(llvm::BasicBlock::Create(*unwrap(C), Name));
2677 void LLVMInsertExistingBasicBlockAfterInsertBlock(LLVMBuilderRef Builder,
2678 LLVMBasicBlockRef BB) {
2679 BasicBlock *ToInsert = unwrap(BB);
2680 BasicBlock *CurBB = unwrap(Builder)->GetInsertBlock();
2681 assert(CurBB && "current insertion point is invalid!");
2682 CurBB->getParent()->insert(std::next(CurBB->getIterator()), ToInsert);
2685 void LLVMAppendExistingBasicBlock(LLVMValueRef Fn,
2686 LLVMBasicBlockRef BB) {
2687 unwrap<Function>(Fn)->insert(unwrap<Function>(Fn)->end(), unwrap(BB));
2690 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
2691 LLVMValueRef FnRef,
2692 const char *Name) {
2693 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
2696 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
2697 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
2700 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
2701 LLVMBasicBlockRef BBRef,
2702 const char *Name) {
2703 BasicBlock *BB = unwrap(BBRef);
2704 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2707 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
2708 const char *Name) {
2709 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
2712 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
2713 unwrap(BBRef)->eraseFromParent();
2716 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
2717 unwrap(BBRef)->removeFromParent();
2720 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2721 unwrap(BB)->moveBefore(unwrap(MovePos));
2724 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2725 unwrap(BB)->moveAfter(unwrap(MovePos));
2728 /*--.. Operations on instructions ..........................................--*/
2730 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
2731 return wrap(unwrap<Instruction>(Inst)->getParent());
2734 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
2735 BasicBlock *Block = unwrap(BB);
2736 BasicBlock::iterator I = Block->begin();
2737 if (I == Block->end())
2738 return nullptr;
2739 return wrap(&*I);
2742 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
2743 BasicBlock *Block = unwrap(BB);
2744 BasicBlock::iterator I = Block->end();
2745 if (I == Block->begin())
2746 return nullptr;
2747 return wrap(&*--I);
2750 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
2751 Instruction *Instr = unwrap<Instruction>(Inst);
2752 BasicBlock::iterator I(Instr);
2753 if (++I == Instr->getParent()->end())
2754 return nullptr;
2755 return wrap(&*I);
2758 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
2759 Instruction *Instr = unwrap<Instruction>(Inst);
2760 BasicBlock::iterator I(Instr);
2761 if (I == Instr->getParent()->begin())
2762 return nullptr;
2763 return wrap(&*--I);
2766 void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) {
2767 unwrap<Instruction>(Inst)->removeFromParent();
2770 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
2771 unwrap<Instruction>(Inst)->eraseFromParent();
2774 void LLVMDeleteInstruction(LLVMValueRef Inst) {
2775 unwrap<Instruction>(Inst)->deleteValue();
2778 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
2779 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2780 return (LLVMIntPredicate)I->getPredicate();
2781 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2782 if (CE->getOpcode() == Instruction::ICmp)
2783 return (LLVMIntPredicate)CE->getPredicate();
2784 return (LLVMIntPredicate)0;
2787 LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
2788 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2789 return (LLVMRealPredicate)I->getPredicate();
2790 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2791 if (CE->getOpcode() == Instruction::FCmp)
2792 return (LLVMRealPredicate)CE->getPredicate();
2793 return (LLVMRealPredicate)0;
2796 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
2797 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2798 return map_to_llvmopcode(C->getOpcode());
2799 return (LLVMOpcode)0;
2802 LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {
2803 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2804 return wrap(C->clone());
2805 return nullptr;
2808 LLVMValueRef LLVMIsATerminatorInst(LLVMValueRef Inst) {
2809 Instruction *I = dyn_cast<Instruction>(unwrap(Inst));
2810 return (I && I->isTerminator()) ? wrap(I) : nullptr;
2813 unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) {
2814 if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) {
2815 return FPI->arg_size();
2817 return unwrap<CallBase>(Instr)->arg_size();
2820 /*--.. Call and invoke instructions ........................................--*/
2822 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
2823 return unwrap<CallBase>(Instr)->getCallingConv();
2826 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
2827 return unwrap<CallBase>(Instr)->setCallingConv(
2828 static_cast<CallingConv::ID>(CC));
2831 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, LLVMAttributeIndex Idx,
2832 unsigned align) {
2833 auto *Call = unwrap<CallBase>(Instr);
2834 Attribute AlignAttr =
2835 Attribute::getWithAlignment(Call->getContext(), Align(align));
2836 Call->addAttributeAtIndex(Idx, AlignAttr);
2839 void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2840 LLVMAttributeRef A) {
2841 unwrap<CallBase>(C)->addAttributeAtIndex(Idx, unwrap(A));
2844 unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C,
2845 LLVMAttributeIndex Idx) {
2846 auto *Call = unwrap<CallBase>(C);
2847 auto AS = Call->getAttributes().getAttributes(Idx);
2848 return AS.getNumAttributes();
2851 void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx,
2852 LLVMAttributeRef *Attrs) {
2853 auto *Call = unwrap<CallBase>(C);
2854 auto AS = Call->getAttributes().getAttributes(Idx);
2855 for (auto A : AS)
2856 *Attrs++ = wrap(A);
2859 LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C,
2860 LLVMAttributeIndex Idx,
2861 unsigned KindID) {
2862 return wrap(unwrap<CallBase>(C)->getAttributeAtIndex(
2863 Idx, (Attribute::AttrKind)KindID));
2866 LLVMAttributeRef LLVMGetCallSiteStringAttribute(LLVMValueRef C,
2867 LLVMAttributeIndex Idx,
2868 const char *K, unsigned KLen) {
2869 return wrap(
2870 unwrap<CallBase>(C)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
2873 void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2874 unsigned KindID) {
2875 unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
2878 void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2879 const char *K, unsigned KLen) {
2880 unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
2883 LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) {
2884 return wrap(unwrap<CallBase>(Instr)->getCalledOperand());
2887 LLVMTypeRef LLVMGetCalledFunctionType(LLVMValueRef Instr) {
2888 return wrap(unwrap<CallBase>(Instr)->getFunctionType());
2891 unsigned LLVMGetNumOperandBundles(LLVMValueRef C) {
2892 return unwrap<CallBase>(C)->getNumOperandBundles();
2895 LLVMOperandBundleRef LLVMGetOperandBundleAtIndex(LLVMValueRef C,
2896 unsigned Index) {
2897 return wrap(
2898 new OperandBundleDef(unwrap<CallBase>(C)->getOperandBundleAt(Index)));
2901 /*--.. Operations on call instructions (only) ..............................--*/
2903 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
2904 return unwrap<CallInst>(Call)->isTailCall();
2907 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
2908 unwrap<CallInst>(Call)->setTailCall(isTailCall);
2911 LLVMTailCallKind LLVMGetTailCallKind(LLVMValueRef Call) {
2912 return (LLVMTailCallKind)unwrap<CallInst>(Call)->getTailCallKind();
2915 void LLVMSetTailCallKind(LLVMValueRef Call, LLVMTailCallKind kind) {
2916 unwrap<CallInst>(Call)->setTailCallKind((CallInst::TailCallKind)kind);
2919 /*--.. Operations on invoke instructions (only) ............................--*/
2921 LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) {
2922 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2925 LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) {
2926 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2927 return wrap(CRI->getUnwindDest());
2928 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2929 return wrap(CSI->getUnwindDest());
2931 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2934 void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2935 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2938 void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2939 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2940 return CRI->setUnwindDest(unwrap(B));
2941 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2942 return CSI->setUnwindDest(unwrap(B));
2944 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2947 /*--.. Operations on terminators ...........................................--*/
2949 unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {
2950 return unwrap<Instruction>(Term)->getNumSuccessors();
2953 LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) {
2954 return wrap(unwrap<Instruction>(Term)->getSuccessor(i));
2957 void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
2958 return unwrap<Instruction>(Term)->setSuccessor(i, unwrap(block));
2961 /*--.. Operations on branch instructions (only) ............................--*/
2963 LLVMBool LLVMIsConditional(LLVMValueRef Branch) {
2964 return unwrap<BranchInst>(Branch)->isConditional();
2967 LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) {
2968 return wrap(unwrap<BranchInst>(Branch)->getCondition());
2971 void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) {
2972 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2975 /*--.. Operations on switch instructions (only) ............................--*/
2977 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
2978 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2981 /*--.. Operations on alloca instructions (only) ............................--*/
2983 LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) {
2984 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
2987 /*--.. Operations on gep instructions (only) ...............................--*/
2989 LLVMBool LLVMIsInBounds(LLVMValueRef GEP) {
2990 return unwrap<GEPOperator>(GEP)->isInBounds();
2993 void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) {
2994 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
2997 LLVMTypeRef LLVMGetGEPSourceElementType(LLVMValueRef GEP) {
2998 return wrap(unwrap<GEPOperator>(GEP)->getSourceElementType());
3001 /*--.. Operations on phi nodes .............................................--*/
3003 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
3004 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
3005 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
3006 for (unsigned I = 0; I != Count; ++I)
3007 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
3010 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
3011 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
3014 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
3015 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
3018 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
3019 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
3022 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/
3024 unsigned LLVMGetNumIndices(LLVMValueRef Inst) {
3025 auto *I = unwrap(Inst);
3026 if (auto *GEP = dyn_cast<GEPOperator>(I))
3027 return GEP->getNumIndices();
3028 if (auto *EV = dyn_cast<ExtractValueInst>(I))
3029 return EV->getNumIndices();
3030 if (auto *IV = dyn_cast<InsertValueInst>(I))
3031 return IV->getNumIndices();
3032 llvm_unreachable(
3033 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
3036 const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
3037 auto *I = unwrap(Inst);
3038 if (auto *EV = dyn_cast<ExtractValueInst>(I))
3039 return EV->getIndices().data();
3040 if (auto *IV = dyn_cast<InsertValueInst>(I))
3041 return IV->getIndices().data();
3042 llvm_unreachable(
3043 "LLVMGetIndices applies only to extractvalue and insertvalue!");
3047 /*===-- Instruction builders ----------------------------------------------===*/
3049 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
3050 return wrap(new IRBuilder<>(*unwrap(C)));
3053 LLVMBuilderRef LLVMCreateBuilder(void) {
3054 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
3057 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
3058 LLVMValueRef Instr) {
3059 BasicBlock *BB = unwrap(Block);
3060 auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
3061 unwrap(Builder)->SetInsertPoint(BB, I);
3064 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
3065 Instruction *I = unwrap<Instruction>(Instr);
3066 unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
3069 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
3070 BasicBlock *BB = unwrap(Block);
3071 unwrap(Builder)->SetInsertPoint(BB);
3074 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
3075 return wrap(unwrap(Builder)->GetInsertBlock());
3078 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
3079 unwrap(Builder)->ClearInsertionPoint();
3082 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
3083 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
3086 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
3087 const char *Name) {
3088 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
3091 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
3092 delete unwrap(Builder);
3095 /*--.. Metadata builders ...................................................--*/
3097 LLVMMetadataRef LLVMGetCurrentDebugLocation2(LLVMBuilderRef Builder) {
3098 return wrap(unwrap(Builder)->getCurrentDebugLocation().getAsMDNode());
3101 void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Builder, LLVMMetadataRef Loc) {
3102 if (Loc)
3103 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(unwrap<MDNode>(Loc)));
3104 else
3105 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc());
3108 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
3109 MDNode *Loc =
3110 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
3111 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
3114 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
3115 LLVMContext &Context = unwrap(Builder)->getContext();
3116 return wrap(MetadataAsValue::get(
3117 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
3120 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
3121 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
3124 void LLVMAddMetadataToInst(LLVMBuilderRef Builder, LLVMValueRef Inst) {
3125 unwrap(Builder)->AddMetadataToInst(unwrap<Instruction>(Inst));
3128 void LLVMBuilderSetDefaultFPMathTag(LLVMBuilderRef Builder,
3129 LLVMMetadataRef FPMathTag) {
3131 unwrap(Builder)->setDefaultFPMathTag(FPMathTag
3132 ? unwrap<MDNode>(FPMathTag)
3133 : nullptr);
3136 LLVMMetadataRef LLVMBuilderGetDefaultFPMathTag(LLVMBuilderRef Builder) {
3137 return wrap(unwrap(Builder)->getDefaultFPMathTag());
3140 /*--.. Instruction builders ................................................--*/
3142 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
3143 return wrap(unwrap(B)->CreateRetVoid());
3146 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
3147 return wrap(unwrap(B)->CreateRet(unwrap(V)));
3150 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
3151 unsigned N) {
3152 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
3155 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
3156 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
3159 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
3160 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
3161 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
3164 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
3165 LLVMBasicBlockRef Else, unsigned NumCases) {
3166 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
3169 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
3170 unsigned NumDests) {
3171 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
3174 LLVMValueRef LLVMBuildInvoke2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn,
3175 LLVMValueRef *Args, unsigned NumArgs,
3176 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
3177 const char *Name) {
3178 return wrap(unwrap(B)->CreateInvoke(unwrap<FunctionType>(Ty), unwrap(Fn),
3179 unwrap(Then), unwrap(Catch),
3180 ArrayRef(unwrap(Args), NumArgs), Name));
3183 LLVMValueRef LLVMBuildInvokeWithOperandBundles(
3184 LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn, LLVMValueRef *Args,
3185 unsigned NumArgs, LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
3186 LLVMOperandBundleRef *Bundles, unsigned NumBundles, const char *Name) {
3187 SmallVector<OperandBundleDef, 8> OBs;
3188 for (auto *Bundle : ArrayRef(Bundles, NumBundles)) {
3189 OperandBundleDef *OB = unwrap(Bundle);
3190 OBs.push_back(*OB);
3192 return wrap(unwrap(B)->CreateInvoke(
3193 unwrap<FunctionType>(Ty), unwrap(Fn), unwrap(Then), unwrap(Catch),
3194 ArrayRef(unwrap(Args), NumArgs), OBs, Name));
3197 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
3198 LLVMValueRef PersFn, unsigned NumClauses,
3199 const char *Name) {
3200 // The personality used to live on the landingpad instruction, but now it
3201 // lives on the parent function. For compatibility, take the provided
3202 // personality and put it on the parent function.
3203 if (PersFn)
3204 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
3205 unwrap<Function>(PersFn));
3206 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
3209 LLVMValueRef LLVMBuildCatchPad(LLVMBuilderRef B, LLVMValueRef ParentPad,
3210 LLVMValueRef *Args, unsigned NumArgs,
3211 const char *Name) {
3212 return wrap(unwrap(B)->CreateCatchPad(unwrap(ParentPad),
3213 ArrayRef(unwrap(Args), NumArgs), Name));
3216 LLVMValueRef LLVMBuildCleanupPad(LLVMBuilderRef B, LLVMValueRef ParentPad,
3217 LLVMValueRef *Args, unsigned NumArgs,
3218 const char *Name) {
3219 if (ParentPad == nullptr) {
3220 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3221 ParentPad = wrap(Constant::getNullValue(Ty));
3223 return wrap(unwrap(B)->CreateCleanupPad(
3224 unwrap(ParentPad), ArrayRef(unwrap(Args), NumArgs), Name));
3227 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
3228 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
3231 LLVMValueRef LLVMBuildCatchSwitch(LLVMBuilderRef B, LLVMValueRef ParentPad,
3232 LLVMBasicBlockRef UnwindBB,
3233 unsigned NumHandlers, const char *Name) {
3234 if (ParentPad == nullptr) {
3235 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3236 ParentPad = wrap(Constant::getNullValue(Ty));
3238 return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(UnwindBB),
3239 NumHandlers, Name));
3242 LLVMValueRef LLVMBuildCatchRet(LLVMBuilderRef B, LLVMValueRef CatchPad,
3243 LLVMBasicBlockRef BB) {
3244 return wrap(unwrap(B)->CreateCatchRet(unwrap<CatchPadInst>(CatchPad),
3245 unwrap(BB)));
3248 LLVMValueRef LLVMBuildCleanupRet(LLVMBuilderRef B, LLVMValueRef CatchPad,
3249 LLVMBasicBlockRef BB) {
3250 return wrap(unwrap(B)->CreateCleanupRet(unwrap<CleanupPadInst>(CatchPad),
3251 unwrap(BB)));
3254 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
3255 return wrap(unwrap(B)->CreateUnreachable());
3258 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
3259 LLVMBasicBlockRef Dest) {
3260 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
3263 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
3264 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
3267 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
3268 return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
3271 LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
3272 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
3275 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
3276 unwrap<LandingPadInst>(LandingPad)->addClause(unwrap<Constant>(ClauseVal));
3279 LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) {
3280 return unwrap<LandingPadInst>(LandingPad)->isCleanup();
3283 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
3284 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
3287 void LLVMAddHandler(LLVMValueRef CatchSwitch, LLVMBasicBlockRef Dest) {
3288 unwrap<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Dest));
3291 unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) {
3292 return unwrap<CatchSwitchInst>(CatchSwitch)->getNumHandlers();
3295 void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) {
3296 CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(CatchSwitch);
3297 for (const BasicBlock *H : CSI->handlers())
3298 *Handlers++ = wrap(H);
3301 LLVMValueRef LLVMGetParentCatchSwitch(LLVMValueRef CatchPad) {
3302 return wrap(unwrap<CatchPadInst>(CatchPad)->getCatchSwitch());
3305 void LLVMSetParentCatchSwitch(LLVMValueRef CatchPad, LLVMValueRef CatchSwitch) {
3306 unwrap<CatchPadInst>(CatchPad)
3307 ->setCatchSwitch(unwrap<CatchSwitchInst>(CatchSwitch));
3310 /*--.. Funclets ...........................................................--*/
3312 LLVMValueRef LLVMGetArgOperand(LLVMValueRef Funclet, unsigned i) {
3313 return wrap(unwrap<FuncletPadInst>(Funclet)->getArgOperand(i));
3316 void LLVMSetArgOperand(LLVMValueRef Funclet, unsigned i, LLVMValueRef value) {
3317 unwrap<FuncletPadInst>(Funclet)->setArgOperand(i, unwrap(value));
3320 /*--.. Arithmetic ..........................................................--*/
3322 static FastMathFlags mapFromLLVMFastMathFlags(LLVMFastMathFlags FMF) {
3323 FastMathFlags NewFMF;
3324 NewFMF.setAllowReassoc((FMF & LLVMFastMathAllowReassoc) != 0);
3325 NewFMF.setNoNaNs((FMF & LLVMFastMathNoNaNs) != 0);
3326 NewFMF.setNoInfs((FMF & LLVMFastMathNoInfs) != 0);
3327 NewFMF.setNoSignedZeros((FMF & LLVMFastMathNoSignedZeros) != 0);
3328 NewFMF.setAllowReciprocal((FMF & LLVMFastMathAllowReciprocal) != 0);
3329 NewFMF.setAllowContract((FMF & LLVMFastMathAllowContract) != 0);
3330 NewFMF.setApproxFunc((FMF & LLVMFastMathApproxFunc) != 0);
3332 return NewFMF;
3335 static LLVMFastMathFlags mapToLLVMFastMathFlags(FastMathFlags FMF) {
3336 LLVMFastMathFlags NewFMF = LLVMFastMathNone;
3337 if (FMF.allowReassoc())
3338 NewFMF |= LLVMFastMathAllowReassoc;
3339 if (FMF.noNaNs())
3340 NewFMF |= LLVMFastMathNoNaNs;
3341 if (FMF.noInfs())
3342 NewFMF |= LLVMFastMathNoInfs;
3343 if (FMF.noSignedZeros())
3344 NewFMF |= LLVMFastMathNoSignedZeros;
3345 if (FMF.allowReciprocal())
3346 NewFMF |= LLVMFastMathAllowReciprocal;
3347 if (FMF.allowContract())
3348 NewFMF |= LLVMFastMathAllowContract;
3349 if (FMF.approxFunc())
3350 NewFMF |= LLVMFastMathApproxFunc;
3352 return NewFMF;
3355 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3356 const char *Name) {
3357 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
3360 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3361 const char *Name) {
3362 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
3365 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3366 const char *Name) {
3367 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
3370 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3371 const char *Name) {
3372 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
3375 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3376 const char *Name) {
3377 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
3380 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3381 const char *Name) {
3382 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
3385 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3386 const char *Name) {
3387 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
3390 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3391 const char *Name) {
3392 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
3395 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3396 const char *Name) {
3397 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
3400 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3401 const char *Name) {
3402 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
3405 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3406 const char *Name) {
3407 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
3410 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3411 const char *Name) {
3412 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
3415 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3416 const char *Name) {
3417 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
3420 LLVMValueRef LLVMBuildExactUDiv(LLVMBuilderRef B, LLVMValueRef LHS,
3421 LLVMValueRef RHS, const char *Name) {
3422 return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
3425 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3426 const char *Name) {
3427 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
3430 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
3431 LLVMValueRef RHS, const char *Name) {
3432 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
3435 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3436 const char *Name) {
3437 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
3440 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3441 const char *Name) {
3442 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
3445 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3446 const char *Name) {
3447 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
3450 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3451 const char *Name) {
3452 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
3455 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3456 const char *Name) {
3457 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
3460 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3461 const char *Name) {
3462 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
3465 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3466 const char *Name) {
3467 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
3470 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3471 const char *Name) {
3472 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
3475 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3476 const char *Name) {
3477 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
3480 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3481 const char *Name) {
3482 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
3485 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
3486 LLVMValueRef LHS, LLVMValueRef RHS,
3487 const char *Name) {
3488 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
3489 unwrap(RHS), Name));
3492 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
3493 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
3496 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
3497 const char *Name) {
3498 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
3501 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
3502 const char *Name) {
3503 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
3506 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
3507 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
3510 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
3511 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
3514 LLVMBool LLVMGetNUW(LLVMValueRef ArithInst) {
3515 Value *P = unwrap<Value>(ArithInst);
3516 return cast<Instruction>(P)->hasNoUnsignedWrap();
3519 void LLVMSetNUW(LLVMValueRef ArithInst, LLVMBool HasNUW) {
3520 Value *P = unwrap<Value>(ArithInst);
3521 cast<Instruction>(P)->setHasNoUnsignedWrap(HasNUW);
3524 LLVMBool LLVMGetNSW(LLVMValueRef ArithInst) {
3525 Value *P = unwrap<Value>(ArithInst);
3526 return cast<Instruction>(P)->hasNoSignedWrap();
3529 void LLVMSetNSW(LLVMValueRef ArithInst, LLVMBool HasNSW) {
3530 Value *P = unwrap<Value>(ArithInst);
3531 cast<Instruction>(P)->setHasNoSignedWrap(HasNSW);
3534 LLVMBool LLVMGetExact(LLVMValueRef DivOrShrInst) {
3535 Value *P = unwrap<Value>(DivOrShrInst);
3536 return cast<Instruction>(P)->isExact();
3539 void LLVMSetExact(LLVMValueRef DivOrShrInst, LLVMBool IsExact) {
3540 Value *P = unwrap<Value>(DivOrShrInst);
3541 cast<Instruction>(P)->setIsExact(IsExact);
3544 LLVMBool LLVMGetNNeg(LLVMValueRef NonNegInst) {
3545 Value *P = unwrap<Value>(NonNegInst);
3546 return cast<Instruction>(P)->hasNonNeg();
3549 void LLVMSetNNeg(LLVMValueRef NonNegInst, LLVMBool IsNonNeg) {
3550 Value *P = unwrap<Value>(NonNegInst);
3551 cast<Instruction>(P)->setNonNeg(IsNonNeg);
3554 LLVMFastMathFlags LLVMGetFastMathFlags(LLVMValueRef FPMathInst) {
3555 Value *P = unwrap<Value>(FPMathInst);
3556 FastMathFlags FMF = cast<Instruction>(P)->getFastMathFlags();
3557 return mapToLLVMFastMathFlags(FMF);
3560 void LLVMSetFastMathFlags(LLVMValueRef FPMathInst, LLVMFastMathFlags FMF) {
3561 Value *P = unwrap<Value>(FPMathInst);
3562 cast<Instruction>(P)->setFastMathFlags(mapFromLLVMFastMathFlags(FMF));
3565 LLVMBool LLVMCanValueUseFastMathFlags(LLVMValueRef V) {
3566 Value *Val = unwrap<Value>(V);
3567 return isa<FPMathOperator>(Val);
3570 LLVMBool LLVMGetIsDisjoint(LLVMValueRef Inst) {
3571 Value *P = unwrap<Value>(Inst);
3572 return cast<PossiblyDisjointInst>(P)->isDisjoint();
3575 void LLVMSetIsDisjoint(LLVMValueRef Inst, LLVMBool IsDisjoint) {
3576 Value *P = unwrap<Value>(Inst);
3577 cast<PossiblyDisjointInst>(P)->setIsDisjoint(IsDisjoint);
3580 /*--.. Memory ..............................................................--*/
3582 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
3583 const char *Name) {
3584 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3585 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3586 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3587 return wrap(unwrap(B)->CreateMalloc(ITy, unwrap(Ty), AllocSize, nullptr,
3588 nullptr, Name));
3591 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
3592 LLVMValueRef Val, const char *Name) {
3593 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3594 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3595 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3596 return wrap(unwrap(B)->CreateMalloc(ITy, unwrap(Ty), AllocSize, unwrap(Val),
3597 nullptr, Name));
3600 LLVMValueRef LLVMBuildMemSet(LLVMBuilderRef B, LLVMValueRef Ptr,
3601 LLVMValueRef Val, LLVMValueRef Len,
3602 unsigned Align) {
3603 return wrap(unwrap(B)->CreateMemSet(unwrap(Ptr), unwrap(Val), unwrap(Len),
3604 MaybeAlign(Align)));
3607 LLVMValueRef LLVMBuildMemCpy(LLVMBuilderRef B,
3608 LLVMValueRef Dst, unsigned DstAlign,
3609 LLVMValueRef Src, unsigned SrcAlign,
3610 LLVMValueRef Size) {
3611 return wrap(unwrap(B)->CreateMemCpy(unwrap(Dst), MaybeAlign(DstAlign),
3612 unwrap(Src), MaybeAlign(SrcAlign),
3613 unwrap(Size)));
3616 LLVMValueRef LLVMBuildMemMove(LLVMBuilderRef B,
3617 LLVMValueRef Dst, unsigned DstAlign,
3618 LLVMValueRef Src, unsigned SrcAlign,
3619 LLVMValueRef Size) {
3620 return wrap(unwrap(B)->CreateMemMove(unwrap(Dst), MaybeAlign(DstAlign),
3621 unwrap(Src), MaybeAlign(SrcAlign),
3622 unwrap(Size)));
3625 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
3626 const char *Name) {
3627 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
3630 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
3631 LLVMValueRef Val, const char *Name) {
3632 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
3635 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
3636 return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
3639 LLVMValueRef LLVMBuildLoad2(LLVMBuilderRef B, LLVMTypeRef Ty,
3640 LLVMValueRef PointerVal, const char *Name) {
3641 return wrap(unwrap(B)->CreateLoad(unwrap(Ty), unwrap(PointerVal), Name));
3644 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
3645 LLVMValueRef PointerVal) {
3646 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
3649 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
3650 switch (Ordering) {
3651 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic;
3652 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered;
3653 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic;
3654 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire;
3655 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release;
3656 case LLVMAtomicOrderingAcquireRelease:
3657 return AtomicOrdering::AcquireRelease;
3658 case LLVMAtomicOrderingSequentiallyConsistent:
3659 return AtomicOrdering::SequentiallyConsistent;
3662 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
3665 static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) {
3666 switch (Ordering) {
3667 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic;
3668 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered;
3669 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic;
3670 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire;
3671 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease;
3672 case AtomicOrdering::AcquireRelease:
3673 return LLVMAtomicOrderingAcquireRelease;
3674 case AtomicOrdering::SequentiallyConsistent:
3675 return LLVMAtomicOrderingSequentiallyConsistent;
3678 llvm_unreachable("Invalid AtomicOrdering value!");
3681 static AtomicRMWInst::BinOp mapFromLLVMRMWBinOp(LLVMAtomicRMWBinOp BinOp) {
3682 switch (BinOp) {
3683 case LLVMAtomicRMWBinOpXchg: return AtomicRMWInst::Xchg;
3684 case LLVMAtomicRMWBinOpAdd: return AtomicRMWInst::Add;
3685 case LLVMAtomicRMWBinOpSub: return AtomicRMWInst::Sub;
3686 case LLVMAtomicRMWBinOpAnd: return AtomicRMWInst::And;
3687 case LLVMAtomicRMWBinOpNand: return AtomicRMWInst::Nand;
3688 case LLVMAtomicRMWBinOpOr: return AtomicRMWInst::Or;
3689 case LLVMAtomicRMWBinOpXor: return AtomicRMWInst::Xor;
3690 case LLVMAtomicRMWBinOpMax: return AtomicRMWInst::Max;
3691 case LLVMAtomicRMWBinOpMin: return AtomicRMWInst::Min;
3692 case LLVMAtomicRMWBinOpUMax: return AtomicRMWInst::UMax;
3693 case LLVMAtomicRMWBinOpUMin: return AtomicRMWInst::UMin;
3694 case LLVMAtomicRMWBinOpFAdd: return AtomicRMWInst::FAdd;
3695 case LLVMAtomicRMWBinOpFSub: return AtomicRMWInst::FSub;
3696 case LLVMAtomicRMWBinOpFMax: return AtomicRMWInst::FMax;
3697 case LLVMAtomicRMWBinOpFMin: return AtomicRMWInst::FMin;
3700 llvm_unreachable("Invalid LLVMAtomicRMWBinOp value!");
3703 static LLVMAtomicRMWBinOp mapToLLVMRMWBinOp(AtomicRMWInst::BinOp BinOp) {
3704 switch (BinOp) {
3705 case AtomicRMWInst::Xchg: return LLVMAtomicRMWBinOpXchg;
3706 case AtomicRMWInst::Add: return LLVMAtomicRMWBinOpAdd;
3707 case AtomicRMWInst::Sub: return LLVMAtomicRMWBinOpSub;
3708 case AtomicRMWInst::And: return LLVMAtomicRMWBinOpAnd;
3709 case AtomicRMWInst::Nand: return LLVMAtomicRMWBinOpNand;
3710 case AtomicRMWInst::Or: return LLVMAtomicRMWBinOpOr;
3711 case AtomicRMWInst::Xor: return LLVMAtomicRMWBinOpXor;
3712 case AtomicRMWInst::Max: return LLVMAtomicRMWBinOpMax;
3713 case AtomicRMWInst::Min: return LLVMAtomicRMWBinOpMin;
3714 case AtomicRMWInst::UMax: return LLVMAtomicRMWBinOpUMax;
3715 case AtomicRMWInst::UMin: return LLVMAtomicRMWBinOpUMin;
3716 case AtomicRMWInst::FAdd: return LLVMAtomicRMWBinOpFAdd;
3717 case AtomicRMWInst::FSub: return LLVMAtomicRMWBinOpFSub;
3718 case AtomicRMWInst::FMax: return LLVMAtomicRMWBinOpFMax;
3719 case AtomicRMWInst::FMin: return LLVMAtomicRMWBinOpFMin;
3720 default: break;
3723 llvm_unreachable("Invalid AtomicRMWBinOp value!");
3726 // TODO: Should this and other atomic instructions support building with
3727 // "syncscope"?
3728 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
3729 LLVMBool isSingleThread, const char *Name) {
3730 return wrap(
3731 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
3732 isSingleThread ? SyncScope::SingleThread
3733 : SyncScope::System,
3734 Name));
3737 LLVMValueRef LLVMBuildGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,
3738 LLVMValueRef Pointer, LLVMValueRef *Indices,
3739 unsigned NumIndices, const char *Name) {
3740 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3741 return wrap(unwrap(B)->CreateGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3744 LLVMValueRef LLVMBuildInBoundsGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,
3745 LLVMValueRef Pointer, LLVMValueRef *Indices,
3746 unsigned NumIndices, const char *Name) {
3747 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3748 return wrap(
3749 unwrap(B)->CreateInBoundsGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3752 LLVMValueRef LLVMBuildStructGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,
3753 LLVMValueRef Pointer, unsigned Idx,
3754 const char *Name) {
3755 return wrap(
3756 unwrap(B)->CreateStructGEP(unwrap(Ty), unwrap(Pointer), Idx, Name));
3759 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
3760 const char *Name) {
3761 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
3764 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
3765 const char *Name) {
3766 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
3769 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
3770 Value *P = unwrap(MemAccessInst);
3771 if (LoadInst *LI = dyn_cast<LoadInst>(P))
3772 return LI->isVolatile();
3773 if (StoreInst *SI = dyn_cast<StoreInst>(P))
3774 return SI->isVolatile();
3775 if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
3776 return AI->isVolatile();
3777 return cast<AtomicCmpXchgInst>(P)->isVolatile();
3780 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
3781 Value *P = unwrap(MemAccessInst);
3782 if (LoadInst *LI = dyn_cast<LoadInst>(P))
3783 return LI->setVolatile(isVolatile);
3784 if (StoreInst *SI = dyn_cast<StoreInst>(P))
3785 return SI->setVolatile(isVolatile);
3786 if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
3787 return AI->setVolatile(isVolatile);
3788 return cast<AtomicCmpXchgInst>(P)->setVolatile(isVolatile);
3791 LLVMBool LLVMGetWeak(LLVMValueRef CmpXchgInst) {
3792 return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->isWeak();
3795 void LLVMSetWeak(LLVMValueRef CmpXchgInst, LLVMBool isWeak) {
3796 return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->setWeak(isWeak);
3799 LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) {
3800 Value *P = unwrap(MemAccessInst);
3801 AtomicOrdering O;
3802 if (LoadInst *LI = dyn_cast<LoadInst>(P))
3803 O = LI->getOrdering();
3804 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
3805 O = SI->getOrdering();
3806 else if (FenceInst *FI = dyn_cast<FenceInst>(P))
3807 O = FI->getOrdering();
3808 else
3809 O = cast<AtomicRMWInst>(P)->getOrdering();
3810 return mapToLLVMOrdering(O);
3813 void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
3814 Value *P = unwrap(MemAccessInst);
3815 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3817 if (LoadInst *LI = dyn_cast<LoadInst>(P))
3818 return LI->setOrdering(O);
3819 else if (FenceInst *FI = dyn_cast<FenceInst>(P))
3820 return FI->setOrdering(O);
3821 else if (AtomicRMWInst *ARWI = dyn_cast<AtomicRMWInst>(P))
3822 return ARWI->setOrdering(O);
3823 return cast<StoreInst>(P)->setOrdering(O);
3826 LLVMAtomicRMWBinOp LLVMGetAtomicRMWBinOp(LLVMValueRef Inst) {
3827 return mapToLLVMRMWBinOp(unwrap<AtomicRMWInst>(Inst)->getOperation());
3830 void LLVMSetAtomicRMWBinOp(LLVMValueRef Inst, LLVMAtomicRMWBinOp BinOp) {
3831 unwrap<AtomicRMWInst>(Inst)->setOperation(mapFromLLVMRMWBinOp(BinOp));
3834 /*--.. Casts ...............................................................--*/
3836 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
3837 LLVMTypeRef DestTy, const char *Name) {
3838 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
3841 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
3842 LLVMTypeRef DestTy, const char *Name) {
3843 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
3846 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
3847 LLVMTypeRef DestTy, const char *Name) {
3848 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
3851 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
3852 LLVMTypeRef DestTy, const char *Name) {
3853 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
3856 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
3857 LLVMTypeRef DestTy, const char *Name) {
3858 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
3861 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
3862 LLVMTypeRef DestTy, const char *Name) {
3863 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
3866 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
3867 LLVMTypeRef DestTy, const char *Name) {
3868 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
3871 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
3872 LLVMTypeRef DestTy, const char *Name) {
3873 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
3876 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
3877 LLVMTypeRef DestTy, const char *Name) {
3878 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
3881 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
3882 LLVMTypeRef DestTy, const char *Name) {
3883 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
3886 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
3887 LLVMTypeRef DestTy, const char *Name) {
3888 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
3891 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3892 LLVMTypeRef DestTy, const char *Name) {
3893 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
3896 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
3897 LLVMTypeRef DestTy, const char *Name) {
3898 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
3901 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3902 LLVMTypeRef DestTy, const char *Name) {
3903 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
3904 Name));
3907 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3908 LLVMTypeRef DestTy, const char *Name) {
3909 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
3910 Name));
3913 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
3914 LLVMTypeRef DestTy, const char *Name) {
3915 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
3916 Name));
3919 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
3920 LLVMTypeRef DestTy, const char *Name) {
3921 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
3922 unwrap(DestTy), Name));
3925 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
3926 LLVMTypeRef DestTy, const char *Name) {
3927 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
3930 LLVMValueRef LLVMBuildIntCast2(LLVMBuilderRef B, LLVMValueRef Val,
3931 LLVMTypeRef DestTy, LLVMBool IsSigned,
3932 const char *Name) {
3933 return wrap(
3934 unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy), IsSigned, Name));
3937 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
3938 LLVMTypeRef DestTy, const char *Name) {
3939 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
3940 /*isSigned*/true, Name));
3943 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
3944 LLVMTypeRef DestTy, const char *Name) {
3945 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
3948 LLVMOpcode LLVMGetCastOpcode(LLVMValueRef Src, LLVMBool SrcIsSigned,
3949 LLVMTypeRef DestTy, LLVMBool DestIsSigned) {
3950 return map_to_llvmopcode(CastInst::getCastOpcode(
3951 unwrap(Src), SrcIsSigned, unwrap(DestTy), DestIsSigned));
3954 /*--.. Comparisons .........................................................--*/
3956 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
3957 LLVMValueRef LHS, LLVMValueRef RHS,
3958 const char *Name) {
3959 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
3960 unwrap(LHS), unwrap(RHS), Name));
3963 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
3964 LLVMValueRef LHS, LLVMValueRef RHS,
3965 const char *Name) {
3966 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
3967 unwrap(LHS), unwrap(RHS), Name));
3970 /*--.. Miscellaneous instructions ..........................................--*/
3972 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
3973 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
3976 LLVMValueRef LLVMBuildCall2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn,
3977 LLVMValueRef *Args, unsigned NumArgs,
3978 const char *Name) {
3979 FunctionType *FTy = unwrap<FunctionType>(Ty);
3980 return wrap(unwrap(B)->CreateCall(FTy, unwrap(Fn),
3981 ArrayRef(unwrap(Args), NumArgs), Name));
3984 LLVMValueRef
3985 LLVMBuildCallWithOperandBundles(LLVMBuilderRef B, LLVMTypeRef Ty,
3986 LLVMValueRef Fn, LLVMValueRef *Args,
3987 unsigned NumArgs, LLVMOperandBundleRef *Bundles,
3988 unsigned NumBundles, const char *Name) {
3989 FunctionType *FTy = unwrap<FunctionType>(Ty);
3990 SmallVector<OperandBundleDef, 8> OBs;
3991 for (auto *Bundle : ArrayRef(Bundles, NumBundles)) {
3992 OperandBundleDef *OB = unwrap(Bundle);
3993 OBs.push_back(*OB);
3995 return wrap(unwrap(B)->CreateCall(
3996 FTy, unwrap(Fn), ArrayRef(unwrap(Args), NumArgs), OBs, Name));
3999 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
4000 LLVMValueRef Then, LLVMValueRef Else,
4001 const char *Name) {
4002 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
4003 Name));
4006 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
4007 LLVMTypeRef Ty, const char *Name) {
4008 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
4011 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
4012 LLVMValueRef Index, const char *Name) {
4013 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
4014 Name));
4017 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
4018 LLVMValueRef EltVal, LLVMValueRef Index,
4019 const char *Name) {
4020 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
4021 unwrap(Index), Name));
4024 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
4025 LLVMValueRef V2, LLVMValueRef Mask,
4026 const char *Name) {
4027 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
4028 unwrap(Mask), Name));
4031 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
4032 unsigned Index, const char *Name) {
4033 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
4036 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
4037 LLVMValueRef EltVal, unsigned Index,
4038 const char *Name) {
4039 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
4040 Index, Name));
4043 LLVMValueRef LLVMBuildFreeze(LLVMBuilderRef B, LLVMValueRef Val,
4044 const char *Name) {
4045 return wrap(unwrap(B)->CreateFreeze(unwrap(Val), Name));
4048 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
4049 const char *Name) {
4050 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
4053 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
4054 const char *Name) {
4055 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
4058 LLVMValueRef LLVMBuildPtrDiff2(LLVMBuilderRef B, LLVMTypeRef ElemTy,
4059 LLVMValueRef LHS, LLVMValueRef RHS,
4060 const char *Name) {
4061 return wrap(unwrap(B)->CreatePtrDiff(unwrap(ElemTy), unwrap(LHS),
4062 unwrap(RHS), Name));
4065 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
4066 LLVMValueRef PTR, LLVMValueRef Val,
4067 LLVMAtomicOrdering ordering,
4068 LLVMBool singleThread) {
4069 AtomicRMWInst::BinOp intop = mapFromLLVMRMWBinOp(op);
4070 return wrap(unwrap(B)->CreateAtomicRMW(
4071 intop, unwrap(PTR), unwrap(Val), MaybeAlign(),
4072 mapFromLLVMOrdering(ordering),
4073 singleThread ? SyncScope::SingleThread : SyncScope::System));
4076 LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr,
4077 LLVMValueRef Cmp, LLVMValueRef New,
4078 LLVMAtomicOrdering SuccessOrdering,
4079 LLVMAtomicOrdering FailureOrdering,
4080 LLVMBool singleThread) {
4082 return wrap(unwrap(B)->CreateAtomicCmpXchg(
4083 unwrap(Ptr), unwrap(Cmp), unwrap(New), MaybeAlign(),
4084 mapFromLLVMOrdering(SuccessOrdering),
4085 mapFromLLVMOrdering(FailureOrdering),
4086 singleThread ? SyncScope::SingleThread : SyncScope::System));
4089 unsigned LLVMGetNumMaskElements(LLVMValueRef SVInst) {
4090 Value *P = unwrap(SVInst);
4091 ShuffleVectorInst *I = cast<ShuffleVectorInst>(P);
4092 return I->getShuffleMask().size();
4095 int LLVMGetMaskValue(LLVMValueRef SVInst, unsigned Elt) {
4096 Value *P = unwrap(SVInst);
4097 ShuffleVectorInst *I = cast<ShuffleVectorInst>(P);
4098 return I->getMaskValue(Elt);
4101 int LLVMGetUndefMaskElem(void) { return PoisonMaskElem; }
4103 LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) {
4104 Value *P = unwrap(AtomicInst);
4106 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
4107 return I->getSyncScopeID() == SyncScope::SingleThread;
4108 else if (FenceInst *FI = dyn_cast<FenceInst>(P))
4109 return FI->getSyncScopeID() == SyncScope::SingleThread;
4110 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
4111 return SI->getSyncScopeID() == SyncScope::SingleThread;
4112 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
4113 return LI->getSyncScopeID() == SyncScope::SingleThread;
4114 return cast<AtomicCmpXchgInst>(P)->getSyncScopeID() ==
4115 SyncScope::SingleThread;
4118 void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) {
4119 Value *P = unwrap(AtomicInst);
4120 SyncScope::ID SSID = NewValue ? SyncScope::SingleThread : SyncScope::System;
4122 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
4123 return I->setSyncScopeID(SSID);
4124 else if (FenceInst *FI = dyn_cast<FenceInst>(P))
4125 return FI->setSyncScopeID(SSID);
4126 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
4127 return SI->setSyncScopeID(SSID);
4128 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
4129 return LI->setSyncScopeID(SSID);
4130 return cast<AtomicCmpXchgInst>(P)->setSyncScopeID(SSID);
4133 LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) {
4134 Value *P = unwrap(CmpXchgInst);
4135 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering());
4138 void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst,
4139 LLVMAtomicOrdering Ordering) {
4140 Value *P = unwrap(CmpXchgInst);
4141 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
4143 return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O);
4146 LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst) {
4147 Value *P = unwrap(CmpXchgInst);
4148 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering());
4151 void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst,
4152 LLVMAtomicOrdering Ordering) {
4153 Value *P = unwrap(CmpXchgInst);
4154 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
4156 return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O);
4159 /*===-- Module providers --------------------------------------------------===*/
4161 LLVMModuleProviderRef
4162 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
4163 return reinterpret_cast<LLVMModuleProviderRef>(M);
4166 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
4167 delete unwrap(MP);
4171 /*===-- Memory buffers ----------------------------------------------------===*/
4173 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
4174 const char *Path,
4175 LLVMMemoryBufferRef *OutMemBuf,
4176 char **OutMessage) {
4178 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
4179 if (std::error_code EC = MBOrErr.getError()) {
4180 *OutMessage = strdup(EC.message().c_str());
4181 return 1;
4183 *OutMemBuf = wrap(MBOrErr.get().release());
4184 return 0;
4187 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
4188 char **OutMessage) {
4189 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
4190 if (std::error_code EC = MBOrErr.getError()) {
4191 *OutMessage = strdup(EC.message().c_str());
4192 return 1;
4194 *OutMemBuf = wrap(MBOrErr.get().release());
4195 return 0;
4198 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
4199 const char *InputData,
4200 size_t InputDataLength,
4201 const char *BufferName,
4202 LLVMBool RequiresNullTerminator) {
4204 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
4205 StringRef(BufferName),
4206 RequiresNullTerminator).release());
4209 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
4210 const char *InputData,
4211 size_t InputDataLength,
4212 const char *BufferName) {
4214 return wrap(
4215 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
4216 StringRef(BufferName)).release());
4219 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
4220 return unwrap(MemBuf)->getBufferStart();
4223 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
4224 return unwrap(MemBuf)->getBufferSize();
4227 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
4228 delete unwrap(MemBuf);
4231 /*===-- Pass Manager ------------------------------------------------------===*/
4233 LLVMPassManagerRef LLVMCreatePassManager() {
4234 return wrap(new legacy::PassManager());
4237 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
4238 return wrap(new legacy::FunctionPassManager(unwrap(M)));
4241 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
4242 return LLVMCreateFunctionPassManagerForModule(
4243 reinterpret_cast<LLVMModuleRef>(P));
4246 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
4247 return unwrap<legacy::PassManager>(PM)->run(*unwrap(M));
4250 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
4251 return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization();
4254 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
4255 return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
4258 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
4259 return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization();
4262 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
4263 delete unwrap(PM);
4266 /*===-- Threading ------------------------------------------------------===*/
4268 LLVMBool LLVMStartMultithreaded() {
4269 return LLVMIsMultithreaded();
4272 void LLVMStopMultithreaded() {
4275 LLVMBool LLVMIsMultithreaded() {
4276 return llvm_is_multithreaded();