Revert " [LoongArch][ISel] Check the number of sign bits in `PatGprGpr_32` (#107432)"
[llvm-project.git] / llvm / lib / IR / Core.cpp
blobcf7bbf6b2576f002464cd9bd59ba39a86d8c8e63
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/ConstantRange.h"
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/DebugInfoMetadata.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/DiagnosticInfo.h"
22 #include "llvm/IR/DiagnosticPrinter.h"
23 #include "llvm/IR/GlobalAlias.h"
24 #include "llvm/IR/GlobalVariable.h"
25 #include "llvm/IR/IRBuilder.h"
26 #include "llvm/IR/InlineAsm.h"
27 #include "llvm/IR/IntrinsicInst.h"
28 #include "llvm/IR/LLVMContext.h"
29 #include "llvm/IR/LegacyPassManager.h"
30 #include "llvm/IR/Module.h"
31 #include "llvm/InitializePasses.h"
32 #include "llvm/PassRegistry.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/FileSystem.h"
36 #include "llvm/Support/ManagedStatic.h"
37 #include "llvm/Support/MathExtras.h"
38 #include "llvm/Support/MemoryBuffer.h"
39 #include "llvm/Support/Threading.h"
40 #include "llvm/Support/raw_ostream.h"
41 #include <cassert>
42 #include <cstdlib>
43 #include <cstring>
44 #include <system_error>
46 using namespace llvm;
48 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(OperandBundleDef, LLVMOperandBundleRef)
50 inline BasicBlock **unwrap(LLVMBasicBlockRef *BBs) {
51 return reinterpret_cast<BasicBlock **>(BBs);
54 #define DEBUG_TYPE "ir"
56 void llvm::initializeCore(PassRegistry &Registry) {
57 initializeDominatorTreeWrapperPassPass(Registry);
58 initializePrintModulePassWrapperPass(Registry);
59 initializePrintFunctionPassWrapperPass(Registry);
60 initializeSafepointIRVerifierPass(Registry);
61 initializeVerifierLegacyPassPass(Registry);
64 void LLVMShutdown() {
65 llvm_shutdown();
68 /*===-- Version query -----------------------------------------------------===*/
70 void LLVMGetVersion(unsigned *Major, unsigned *Minor, unsigned *Patch) {
71 if (Major)
72 *Major = LLVM_VERSION_MAJOR;
73 if (Minor)
74 *Minor = LLVM_VERSION_MINOR;
75 if (Patch)
76 *Patch = LLVM_VERSION_PATCH;
79 /*===-- Error handling ----------------------------------------------------===*/
81 char *LLVMCreateMessage(const char *Message) {
82 return strdup(Message);
85 void LLVMDisposeMessage(char *Message) {
86 free(Message);
90 /*===-- Operations on contexts --------------------------------------------===*/
92 static LLVMContext &getGlobalContext() {
93 static LLVMContext GlobalContext;
94 return GlobalContext;
97 LLVMContextRef LLVMContextCreate() {
98 return wrap(new LLVMContext());
101 LLVMContextRef LLVMGetGlobalContext() { return wrap(&getGlobalContext()); }
103 void LLVMContextSetDiagnosticHandler(LLVMContextRef C,
104 LLVMDiagnosticHandler Handler,
105 void *DiagnosticContext) {
106 unwrap(C)->setDiagnosticHandlerCallBack(
107 LLVM_EXTENSION reinterpret_cast<DiagnosticHandler::DiagnosticHandlerTy>(
108 Handler),
109 DiagnosticContext);
112 LLVMDiagnosticHandler LLVMContextGetDiagnosticHandler(LLVMContextRef C) {
113 return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>(
114 unwrap(C)->getDiagnosticHandlerCallBack());
117 void *LLVMContextGetDiagnosticContext(LLVMContextRef C) {
118 return unwrap(C)->getDiagnosticContext();
121 void LLVMContextSetYieldCallback(LLVMContextRef C, LLVMYieldCallback Callback,
122 void *OpaqueHandle) {
123 auto YieldCallback =
124 LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
125 unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
128 LLVMBool LLVMContextShouldDiscardValueNames(LLVMContextRef C) {
129 return unwrap(C)->shouldDiscardValueNames();
132 void LLVMContextSetDiscardValueNames(LLVMContextRef C, LLVMBool Discard) {
133 unwrap(C)->setDiscardValueNames(Discard);
136 void LLVMContextDispose(LLVMContextRef C) {
137 delete unwrap(C);
140 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char *Name,
141 unsigned SLen) {
142 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
145 unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) {
146 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
149 unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) {
150 return Attribute::getAttrKindFromName(StringRef(Name, SLen));
153 unsigned LLVMGetLastEnumAttributeKind(void) {
154 return Attribute::AttrKind::EndAttrKinds;
157 LLVMAttributeRef LLVMCreateEnumAttribute(LLVMContextRef C, unsigned KindID,
158 uint64_t Val) {
159 auto &Ctx = *unwrap(C);
160 auto AttrKind = (Attribute::AttrKind)KindID;
161 return wrap(Attribute::get(Ctx, AttrKind, Val));
164 unsigned LLVMGetEnumAttributeKind(LLVMAttributeRef A) {
165 return unwrap(A).getKindAsEnum();
168 uint64_t LLVMGetEnumAttributeValue(LLVMAttributeRef A) {
169 auto Attr = unwrap(A);
170 if (Attr.isEnumAttribute())
171 return 0;
172 return Attr.getValueAsInt();
175 LLVMAttributeRef LLVMCreateTypeAttribute(LLVMContextRef C, unsigned KindID,
176 LLVMTypeRef type_ref) {
177 auto &Ctx = *unwrap(C);
178 auto AttrKind = (Attribute::AttrKind)KindID;
179 return wrap(Attribute::get(Ctx, AttrKind, unwrap(type_ref)));
182 LLVMTypeRef LLVMGetTypeAttributeValue(LLVMAttributeRef A) {
183 auto Attr = unwrap(A);
184 return wrap(Attr.getValueAsType());
187 LLVMAttributeRef LLVMCreateConstantRangeAttribute(LLVMContextRef C,
188 unsigned KindID,
189 unsigned NumBits,
190 const uint64_t LowerWords[],
191 const uint64_t UpperWords[]) {
192 auto &Ctx = *unwrap(C);
193 auto AttrKind = (Attribute::AttrKind)KindID;
194 unsigned NumWords = divideCeil(NumBits, 64);
195 return wrap(Attribute::get(
196 Ctx, AttrKind,
197 ConstantRange(APInt(NumBits, ArrayRef(LowerWords, NumWords)),
198 APInt(NumBits, ArrayRef(UpperWords, NumWords)))));
201 LLVMAttributeRef LLVMCreateStringAttribute(LLVMContextRef C,
202 const char *K, unsigned KLength,
203 const char *V, unsigned VLength) {
204 return wrap(Attribute::get(*unwrap(C), StringRef(K, KLength),
205 StringRef(V, VLength)));
208 const char *LLVMGetStringAttributeKind(LLVMAttributeRef A,
209 unsigned *Length) {
210 auto S = unwrap(A).getKindAsString();
211 *Length = S.size();
212 return S.data();
215 const char *LLVMGetStringAttributeValue(LLVMAttributeRef A,
216 unsigned *Length) {
217 auto S = unwrap(A).getValueAsString();
218 *Length = S.size();
219 return S.data();
222 LLVMBool LLVMIsEnumAttribute(LLVMAttributeRef A) {
223 auto Attr = unwrap(A);
224 return Attr.isEnumAttribute() || Attr.isIntAttribute();
227 LLVMBool LLVMIsStringAttribute(LLVMAttributeRef A) {
228 return unwrap(A).isStringAttribute();
231 LLVMBool LLVMIsTypeAttribute(LLVMAttributeRef A) {
232 return unwrap(A).isTypeAttribute();
235 char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) {
236 std::string MsgStorage;
237 raw_string_ostream Stream(MsgStorage);
238 DiagnosticPrinterRawOStream DP(Stream);
240 unwrap(DI)->print(DP);
241 Stream.flush();
243 return LLVMCreateMessage(MsgStorage.c_str());
246 LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI) {
247 LLVMDiagnosticSeverity severity;
249 switch(unwrap(DI)->getSeverity()) {
250 default:
251 severity = LLVMDSError;
252 break;
253 case DS_Warning:
254 severity = LLVMDSWarning;
255 break;
256 case DS_Remark:
257 severity = LLVMDSRemark;
258 break;
259 case DS_Note:
260 severity = LLVMDSNote;
261 break;
264 return severity;
267 /*===-- Operations on modules ---------------------------------------------===*/
269 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
270 return wrap(new Module(ModuleID, getGlobalContext()));
273 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
274 LLVMContextRef C) {
275 return wrap(new Module(ModuleID, *unwrap(C)));
278 void LLVMDisposeModule(LLVMModuleRef M) {
279 delete unwrap(M);
282 const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) {
283 auto &Str = unwrap(M)->getModuleIdentifier();
284 *Len = Str.length();
285 return Str.c_str();
288 void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) {
289 unwrap(M)->setModuleIdentifier(StringRef(Ident, Len));
292 const char *LLVMGetSourceFileName(LLVMModuleRef M, size_t *Len) {
293 auto &Str = unwrap(M)->getSourceFileName();
294 *Len = Str.length();
295 return Str.c_str();
298 void LLVMSetSourceFileName(LLVMModuleRef M, const char *Name, size_t Len) {
299 unwrap(M)->setSourceFileName(StringRef(Name, Len));
302 /*--.. Data layout .........................................................--*/
303 const char *LLVMGetDataLayoutStr(LLVMModuleRef M) {
304 return unwrap(M)->getDataLayoutStr().c_str();
307 const char *LLVMGetDataLayout(LLVMModuleRef M) {
308 return LLVMGetDataLayoutStr(M);
311 void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
312 unwrap(M)->setDataLayout(DataLayoutStr);
315 /*--.. Target triple .......................................................--*/
316 const char * LLVMGetTarget(LLVMModuleRef M) {
317 return unwrap(M)->getTargetTriple().c_str();
320 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
321 unwrap(M)->setTargetTriple(Triple);
324 /*--.. Module flags ........................................................--*/
325 struct LLVMOpaqueModuleFlagEntry {
326 LLVMModuleFlagBehavior Behavior;
327 const char *Key;
328 size_t KeyLen;
329 LLVMMetadataRef Metadata;
332 static Module::ModFlagBehavior
333 map_to_llvmModFlagBehavior(LLVMModuleFlagBehavior Behavior) {
334 switch (Behavior) {
335 case LLVMModuleFlagBehaviorError:
336 return Module::ModFlagBehavior::Error;
337 case LLVMModuleFlagBehaviorWarning:
338 return Module::ModFlagBehavior::Warning;
339 case LLVMModuleFlagBehaviorRequire:
340 return Module::ModFlagBehavior::Require;
341 case LLVMModuleFlagBehaviorOverride:
342 return Module::ModFlagBehavior::Override;
343 case LLVMModuleFlagBehaviorAppend:
344 return Module::ModFlagBehavior::Append;
345 case LLVMModuleFlagBehaviorAppendUnique:
346 return Module::ModFlagBehavior::AppendUnique;
348 llvm_unreachable("Unknown LLVMModuleFlagBehavior");
351 static LLVMModuleFlagBehavior
352 map_from_llvmModFlagBehavior(Module::ModFlagBehavior Behavior) {
353 switch (Behavior) {
354 case Module::ModFlagBehavior::Error:
355 return LLVMModuleFlagBehaviorError;
356 case Module::ModFlagBehavior::Warning:
357 return LLVMModuleFlagBehaviorWarning;
358 case Module::ModFlagBehavior::Require:
359 return LLVMModuleFlagBehaviorRequire;
360 case Module::ModFlagBehavior::Override:
361 return LLVMModuleFlagBehaviorOverride;
362 case Module::ModFlagBehavior::Append:
363 return LLVMModuleFlagBehaviorAppend;
364 case Module::ModFlagBehavior::AppendUnique:
365 return LLVMModuleFlagBehaviorAppendUnique;
366 default:
367 llvm_unreachable("Unhandled Flag Behavior");
371 LLVMModuleFlagEntry *LLVMCopyModuleFlagsMetadata(LLVMModuleRef M, size_t *Len) {
372 SmallVector<Module::ModuleFlagEntry, 8> MFEs;
373 unwrap(M)->getModuleFlagsMetadata(MFEs);
375 LLVMOpaqueModuleFlagEntry *Result = static_cast<LLVMOpaqueModuleFlagEntry *>(
376 safe_malloc(MFEs.size() * sizeof(LLVMOpaqueModuleFlagEntry)));
377 for (unsigned i = 0; i < MFEs.size(); ++i) {
378 const auto &ModuleFlag = MFEs[i];
379 Result[i].Behavior = map_from_llvmModFlagBehavior(ModuleFlag.Behavior);
380 Result[i].Key = ModuleFlag.Key->getString().data();
381 Result[i].KeyLen = ModuleFlag.Key->getString().size();
382 Result[i].Metadata = wrap(ModuleFlag.Val);
384 *Len = MFEs.size();
385 return Result;
388 void LLVMDisposeModuleFlagsMetadata(LLVMModuleFlagEntry *Entries) {
389 free(Entries);
392 LLVMModuleFlagBehavior
393 LLVMModuleFlagEntriesGetFlagBehavior(LLVMModuleFlagEntry *Entries,
394 unsigned Index) {
395 LLVMOpaqueModuleFlagEntry MFE =
396 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
397 return MFE.Behavior;
400 const char *LLVMModuleFlagEntriesGetKey(LLVMModuleFlagEntry *Entries,
401 unsigned Index, size_t *Len) {
402 LLVMOpaqueModuleFlagEntry MFE =
403 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
404 *Len = MFE.KeyLen;
405 return MFE.Key;
408 LLVMMetadataRef LLVMModuleFlagEntriesGetMetadata(LLVMModuleFlagEntry *Entries,
409 unsigned Index) {
410 LLVMOpaqueModuleFlagEntry MFE =
411 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
412 return MFE.Metadata;
415 LLVMMetadataRef LLVMGetModuleFlag(LLVMModuleRef M,
416 const char *Key, size_t KeyLen) {
417 return wrap(unwrap(M)->getModuleFlag({Key, KeyLen}));
420 void LLVMAddModuleFlag(LLVMModuleRef M, LLVMModuleFlagBehavior Behavior,
421 const char *Key, size_t KeyLen,
422 LLVMMetadataRef Val) {
423 unwrap(M)->addModuleFlag(map_to_llvmModFlagBehavior(Behavior),
424 {Key, KeyLen}, unwrap(Val));
427 LLVMBool LLVMIsNewDbgInfoFormat(LLVMModuleRef M) {
428 return unwrap(M)->IsNewDbgInfoFormat;
431 void LLVMSetIsNewDbgInfoFormat(LLVMModuleRef M, LLVMBool UseNewFormat) {
432 unwrap(M)->setIsNewDbgInfoFormat(UseNewFormat);
435 /*--.. Printing modules ....................................................--*/
437 void LLVMDumpModule(LLVMModuleRef M) {
438 unwrap(M)->print(errs(), nullptr,
439 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
442 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
443 char **ErrorMessage) {
444 std::error_code EC;
445 raw_fd_ostream dest(Filename, EC, sys::fs::OF_TextWithCRLF);
446 if (EC) {
447 *ErrorMessage = strdup(EC.message().c_str());
448 return true;
451 unwrap(M)->print(dest, nullptr);
453 dest.close();
455 if (dest.has_error()) {
456 std::string E = "Error printing to file: " + dest.error().message();
457 *ErrorMessage = strdup(E.c_str());
458 return true;
461 return false;
464 char *LLVMPrintModuleToString(LLVMModuleRef M) {
465 std::string buf;
466 raw_string_ostream os(buf);
468 unwrap(M)->print(os, nullptr);
469 os.flush();
471 return strdup(buf.c_str());
474 /*--.. Operations on inline assembler ......................................--*/
475 void LLVMSetModuleInlineAsm2(LLVMModuleRef M, const char *Asm, size_t Len) {
476 unwrap(M)->setModuleInlineAsm(StringRef(Asm, Len));
479 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
480 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
483 void LLVMAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm, size_t Len) {
484 unwrap(M)->appendModuleInlineAsm(StringRef(Asm, Len));
487 const char *LLVMGetModuleInlineAsm(LLVMModuleRef M, size_t *Len) {
488 auto &Str = unwrap(M)->getModuleInlineAsm();
489 *Len = Str.length();
490 return Str.c_str();
493 LLVMValueRef LLVMGetInlineAsm(LLVMTypeRef Ty, const char *AsmString,
494 size_t AsmStringSize, const char *Constraints,
495 size_t ConstraintsSize, LLVMBool HasSideEffects,
496 LLVMBool IsAlignStack,
497 LLVMInlineAsmDialect Dialect, LLVMBool CanThrow) {
498 InlineAsm::AsmDialect AD;
499 switch (Dialect) {
500 case LLVMInlineAsmDialectATT:
501 AD = InlineAsm::AD_ATT;
502 break;
503 case LLVMInlineAsmDialectIntel:
504 AD = InlineAsm::AD_Intel;
505 break;
507 return wrap(InlineAsm::get(unwrap<FunctionType>(Ty),
508 StringRef(AsmString, AsmStringSize),
509 StringRef(Constraints, ConstraintsSize),
510 HasSideEffects, IsAlignStack, AD, CanThrow));
513 const char *LLVMGetInlineAsmAsmString(LLVMValueRef InlineAsmVal, size_t *Len) {
515 Value *Val = unwrap<Value>(InlineAsmVal);
516 const std::string &AsmString = cast<InlineAsm>(Val)->getAsmString();
518 *Len = AsmString.length();
519 return AsmString.c_str();
522 const char *LLVMGetInlineAsmConstraintString(LLVMValueRef InlineAsmVal,
523 size_t *Len) {
524 Value *Val = unwrap<Value>(InlineAsmVal);
525 const std::string &ConstraintString =
526 cast<InlineAsm>(Val)->getConstraintString();
528 *Len = ConstraintString.length();
529 return ConstraintString.c_str();
532 LLVMInlineAsmDialect LLVMGetInlineAsmDialect(LLVMValueRef InlineAsmVal) {
534 Value *Val = unwrap<Value>(InlineAsmVal);
535 InlineAsm::AsmDialect Dialect = cast<InlineAsm>(Val)->getDialect();
537 switch (Dialect) {
538 case InlineAsm::AD_ATT:
539 return LLVMInlineAsmDialectATT;
540 case InlineAsm::AD_Intel:
541 return LLVMInlineAsmDialectIntel;
544 llvm_unreachable("Unrecognized inline assembly dialect");
545 return LLVMInlineAsmDialectATT;
548 LLVMTypeRef LLVMGetInlineAsmFunctionType(LLVMValueRef InlineAsmVal) {
549 Value *Val = unwrap<Value>(InlineAsmVal);
550 return (LLVMTypeRef)cast<InlineAsm>(Val)->getFunctionType();
553 LLVMBool LLVMGetInlineAsmHasSideEffects(LLVMValueRef InlineAsmVal) {
554 Value *Val = unwrap<Value>(InlineAsmVal);
555 return cast<InlineAsm>(Val)->hasSideEffects();
558 LLVMBool LLVMGetInlineAsmNeedsAlignedStack(LLVMValueRef InlineAsmVal) {
559 Value *Val = unwrap<Value>(InlineAsmVal);
560 return cast<InlineAsm>(Val)->isAlignStack();
563 LLVMBool LLVMGetInlineAsmCanUnwind(LLVMValueRef InlineAsmVal) {
564 Value *Val = unwrap<Value>(InlineAsmVal);
565 return cast<InlineAsm>(Val)->canThrow();
568 /*--.. Operations on module contexts ......................................--*/
569 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
570 return wrap(&unwrap(M)->getContext());
574 /*===-- Operations on types -----------------------------------------------===*/
576 /*--.. Operations on all types (mostly) ....................................--*/
578 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
579 switch (unwrap(Ty)->getTypeID()) {
580 case Type::VoidTyID:
581 return LLVMVoidTypeKind;
582 case Type::HalfTyID:
583 return LLVMHalfTypeKind;
584 case Type::BFloatTyID:
585 return LLVMBFloatTypeKind;
586 case Type::FloatTyID:
587 return LLVMFloatTypeKind;
588 case Type::DoubleTyID:
589 return LLVMDoubleTypeKind;
590 case Type::X86_FP80TyID:
591 return LLVMX86_FP80TypeKind;
592 case Type::FP128TyID:
593 return LLVMFP128TypeKind;
594 case Type::PPC_FP128TyID:
595 return LLVMPPC_FP128TypeKind;
596 case Type::LabelTyID:
597 return LLVMLabelTypeKind;
598 case Type::MetadataTyID:
599 return LLVMMetadataTypeKind;
600 case Type::IntegerTyID:
601 return LLVMIntegerTypeKind;
602 case Type::FunctionTyID:
603 return LLVMFunctionTypeKind;
604 case Type::StructTyID:
605 return LLVMStructTypeKind;
606 case Type::ArrayTyID:
607 return LLVMArrayTypeKind;
608 case Type::PointerTyID:
609 return LLVMPointerTypeKind;
610 case Type::FixedVectorTyID:
611 return LLVMVectorTypeKind;
612 case Type::X86_MMXTyID:
613 return LLVMX86_MMXTypeKind;
614 case Type::X86_AMXTyID:
615 return LLVMX86_AMXTypeKind;
616 case Type::TokenTyID:
617 return LLVMTokenTypeKind;
618 case Type::ScalableVectorTyID:
619 return LLVMScalableVectorTypeKind;
620 case Type::TargetExtTyID:
621 return LLVMTargetExtTypeKind;
622 case Type::TypedPointerTyID:
623 llvm_unreachable("Typed pointers are unsupported via the C API");
625 llvm_unreachable("Unhandled TypeID.");
628 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
630 return unwrap(Ty)->isSized();
633 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
634 return wrap(&unwrap(Ty)->getContext());
637 void LLVMDumpType(LLVMTypeRef Ty) {
638 return unwrap(Ty)->print(errs(), /*IsForDebug=*/true);
641 char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
642 std::string buf;
643 raw_string_ostream os(buf);
645 if (unwrap(Ty))
646 unwrap(Ty)->print(os);
647 else
648 os << "Printing <null> Type";
650 os.flush();
652 return strdup(buf.c_str());
655 /*--.. Operations on integer types .........................................--*/
657 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
658 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
660 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
661 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
663 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
664 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
666 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
667 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
669 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
670 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
672 LLVMTypeRef LLVMInt128TypeInContext(LLVMContextRef C) {
673 return (LLVMTypeRef) Type::getInt128Ty(*unwrap(C));
675 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
676 return wrap(IntegerType::get(*unwrap(C), NumBits));
679 LLVMTypeRef LLVMInt1Type(void) {
680 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
682 LLVMTypeRef LLVMInt8Type(void) {
683 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
685 LLVMTypeRef LLVMInt16Type(void) {
686 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
688 LLVMTypeRef LLVMInt32Type(void) {
689 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
691 LLVMTypeRef LLVMInt64Type(void) {
692 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
694 LLVMTypeRef LLVMInt128Type(void) {
695 return LLVMInt128TypeInContext(LLVMGetGlobalContext());
697 LLVMTypeRef LLVMIntType(unsigned NumBits) {
698 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
701 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
702 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
705 /*--.. Operations on real types ............................................--*/
707 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
708 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
710 LLVMTypeRef LLVMBFloatTypeInContext(LLVMContextRef C) {
711 return (LLVMTypeRef) Type::getBFloatTy(*unwrap(C));
713 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
714 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
716 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
717 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
719 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
720 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
722 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
723 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
725 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
726 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
728 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
729 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
731 LLVMTypeRef LLVMX86AMXTypeInContext(LLVMContextRef C) {
732 return (LLVMTypeRef) Type::getX86_AMXTy(*unwrap(C));
735 LLVMTypeRef LLVMHalfType(void) {
736 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
738 LLVMTypeRef LLVMBFloatType(void) {
739 return LLVMBFloatTypeInContext(LLVMGetGlobalContext());
741 LLVMTypeRef LLVMFloatType(void) {
742 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
744 LLVMTypeRef LLVMDoubleType(void) {
745 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
747 LLVMTypeRef LLVMX86FP80Type(void) {
748 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
750 LLVMTypeRef LLVMFP128Type(void) {
751 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
753 LLVMTypeRef LLVMPPCFP128Type(void) {
754 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
756 LLVMTypeRef LLVMX86MMXType(void) {
757 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
759 LLVMTypeRef LLVMX86AMXType(void) {
760 return LLVMX86AMXTypeInContext(LLVMGetGlobalContext());
763 /*--.. Operations on function types ........................................--*/
765 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
766 LLVMTypeRef *ParamTypes, unsigned ParamCount,
767 LLVMBool IsVarArg) {
768 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
769 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
772 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
773 return unwrap<FunctionType>(FunctionTy)->isVarArg();
776 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
777 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
780 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
781 return unwrap<FunctionType>(FunctionTy)->getNumParams();
784 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
785 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
786 for (Type *T : Ty->params())
787 *Dest++ = wrap(T);
790 /*--.. Operations on struct types ..........................................--*/
792 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
793 unsigned ElementCount, LLVMBool Packed) {
794 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
795 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
798 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
799 unsigned ElementCount, LLVMBool Packed) {
800 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
801 ElementCount, Packed);
804 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
806 return wrap(StructType::create(*unwrap(C), Name));
809 const char *LLVMGetStructName(LLVMTypeRef Ty)
811 StructType *Type = unwrap<StructType>(Ty);
812 if (!Type->hasName())
813 return nullptr;
814 return Type->getName().data();
817 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
818 unsigned ElementCount, LLVMBool Packed) {
819 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
820 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
823 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
824 return unwrap<StructType>(StructTy)->getNumElements();
827 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
828 StructType *Ty = unwrap<StructType>(StructTy);
829 for (Type *T : Ty->elements())
830 *Dest++ = wrap(T);
833 LLVMTypeRef LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy, unsigned i) {
834 StructType *Ty = unwrap<StructType>(StructTy);
835 return wrap(Ty->getTypeAtIndex(i));
838 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
839 return unwrap<StructType>(StructTy)->isPacked();
842 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
843 return unwrap<StructType>(StructTy)->isOpaque();
846 LLVMBool LLVMIsLiteralStruct(LLVMTypeRef StructTy) {
847 return unwrap<StructType>(StructTy)->isLiteral();
850 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
851 return wrap(StructType::getTypeByName(unwrap(M)->getContext(), Name));
854 LLVMTypeRef LLVMGetTypeByName2(LLVMContextRef C, const char *Name) {
855 return wrap(StructType::getTypeByName(*unwrap(C), Name));
858 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
860 void LLVMGetSubtypes(LLVMTypeRef Tp, LLVMTypeRef *Arr) {
861 int i = 0;
862 for (auto *T : unwrap(Tp)->subtypes()) {
863 Arr[i] = wrap(T);
864 i++;
868 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
869 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
872 LLVMTypeRef LLVMArrayType2(LLVMTypeRef ElementType, uint64_t ElementCount) {
873 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
876 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
877 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
880 LLVMBool LLVMPointerTypeIsOpaque(LLVMTypeRef Ty) {
881 return true;
884 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
885 return wrap(FixedVectorType::get(unwrap(ElementType), ElementCount));
888 LLVMTypeRef LLVMScalableVectorType(LLVMTypeRef ElementType,
889 unsigned ElementCount) {
890 return wrap(ScalableVectorType::get(unwrap(ElementType), ElementCount));
893 LLVMTypeRef LLVMGetElementType(LLVMTypeRef WrappedTy) {
894 auto *Ty = unwrap(WrappedTy);
895 if (auto *ATy = dyn_cast<ArrayType>(Ty))
896 return wrap(ATy->getElementType());
897 return wrap(cast<VectorType>(Ty)->getElementType());
900 unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp) {
901 return unwrap(Tp)->getNumContainedTypes();
904 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
905 return unwrap<ArrayType>(ArrayTy)->getNumElements();
908 uint64_t LLVMGetArrayLength2(LLVMTypeRef ArrayTy) {
909 return unwrap<ArrayType>(ArrayTy)->getNumElements();
912 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
913 return unwrap<PointerType>(PointerTy)->getAddressSpace();
916 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
917 return unwrap<VectorType>(VectorTy)->getElementCount().getKnownMinValue();
920 LLVMValueRef LLVMGetConstantPtrAuthPointer(LLVMValueRef PtrAuth) {
921 return wrap(unwrap<ConstantPtrAuth>(PtrAuth)->getPointer());
924 LLVMValueRef LLVMGetConstantPtrAuthKey(LLVMValueRef PtrAuth) {
925 return wrap(unwrap<ConstantPtrAuth>(PtrAuth)->getKey());
928 LLVMValueRef LLVMGetConstantPtrAuthDiscriminator(LLVMValueRef PtrAuth) {
929 return wrap(unwrap<ConstantPtrAuth>(PtrAuth)->getDiscriminator());
932 LLVMValueRef LLVMGetConstantPtrAuthAddrDiscriminator(LLVMValueRef PtrAuth) {
933 return wrap(unwrap<ConstantPtrAuth>(PtrAuth)->getAddrDiscriminator());
936 /*--.. Operations on other types ...........................................--*/
938 LLVMTypeRef LLVMPointerTypeInContext(LLVMContextRef C, unsigned AddressSpace) {
939 return wrap(PointerType::get(*unwrap(C), AddressSpace));
942 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
943 return wrap(Type::getVoidTy(*unwrap(C)));
945 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
946 return wrap(Type::getLabelTy(*unwrap(C)));
948 LLVMTypeRef LLVMTokenTypeInContext(LLVMContextRef C) {
949 return wrap(Type::getTokenTy(*unwrap(C)));
951 LLVMTypeRef LLVMMetadataTypeInContext(LLVMContextRef C) {
952 return wrap(Type::getMetadataTy(*unwrap(C)));
955 LLVMTypeRef LLVMVoidType(void) {
956 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
958 LLVMTypeRef LLVMLabelType(void) {
959 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
962 LLVMTypeRef LLVMTargetExtTypeInContext(LLVMContextRef C, const char *Name,
963 LLVMTypeRef *TypeParams,
964 unsigned TypeParamCount,
965 unsigned *IntParams,
966 unsigned IntParamCount) {
967 ArrayRef<Type *> TypeParamArray(unwrap(TypeParams), TypeParamCount);
968 ArrayRef<unsigned> IntParamArray(IntParams, IntParamCount);
969 return wrap(
970 TargetExtType::get(*unwrap(C), Name, TypeParamArray, IntParamArray));
973 const char *LLVMGetTargetExtTypeName(LLVMTypeRef TargetExtTy) {
974 TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);
975 return Type->getName().data();
978 unsigned LLVMGetTargetExtTypeNumTypeParams(LLVMTypeRef TargetExtTy) {
979 TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);
980 return Type->getNumTypeParameters();
983 LLVMTypeRef LLVMGetTargetExtTypeTypeParam(LLVMTypeRef TargetExtTy,
984 unsigned Idx) {
985 TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);
986 return wrap(Type->getTypeParameter(Idx));
989 unsigned LLVMGetTargetExtTypeNumIntParams(LLVMTypeRef TargetExtTy) {
990 TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);
991 return Type->getNumIntParameters();
994 unsigned LLVMGetTargetExtTypeIntParam(LLVMTypeRef TargetExtTy, unsigned Idx) {
995 TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);
996 return Type->getIntParameter(Idx);
999 /*===-- Operations on values ----------------------------------------------===*/
1001 /*--.. Operations on all values ............................................--*/
1003 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
1004 return wrap(unwrap(Val)->getType());
1007 LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) {
1008 switch(unwrap(Val)->getValueID()) {
1009 #define LLVM_C_API 1
1010 #define HANDLE_VALUE(Name) \
1011 case Value::Name##Val: \
1012 return LLVM##Name##ValueKind;
1013 #include "llvm/IR/Value.def"
1014 default:
1015 return LLVMInstructionValueKind;
1019 const char *LLVMGetValueName2(LLVMValueRef Val, size_t *Length) {
1020 auto *V = unwrap(Val);
1021 *Length = V->getName().size();
1022 return V->getName().data();
1025 void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen) {
1026 unwrap(Val)->setName(StringRef(Name, NameLen));
1029 const char *LLVMGetValueName(LLVMValueRef Val) {
1030 return unwrap(Val)->getName().data();
1033 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
1034 unwrap(Val)->setName(Name);
1037 void LLVMDumpValue(LLVMValueRef Val) {
1038 unwrap(Val)->print(errs(), /*IsForDebug=*/true);
1041 char* LLVMPrintValueToString(LLVMValueRef Val) {
1042 std::string buf;
1043 raw_string_ostream os(buf);
1045 if (unwrap(Val))
1046 unwrap(Val)->print(os);
1047 else
1048 os << "Printing <null> Value";
1050 os.flush();
1052 return strdup(buf.c_str());
1055 char *LLVMPrintDbgRecordToString(LLVMDbgRecordRef Record) {
1056 std::string buf;
1057 raw_string_ostream os(buf);
1059 if (unwrap(Record))
1060 unwrap(Record)->print(os);
1061 else
1062 os << "Printing <null> DbgRecord";
1064 os.flush();
1066 return strdup(buf.c_str());
1069 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
1070 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
1073 int LLVMHasMetadata(LLVMValueRef Inst) {
1074 return unwrap<Instruction>(Inst)->hasMetadata();
1077 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
1078 auto *I = unwrap<Instruction>(Inst);
1079 assert(I && "Expected instruction");
1080 if (auto *MD = I->getMetadata(KindID))
1081 return wrap(MetadataAsValue::get(I->getContext(), MD));
1082 return nullptr;
1085 // MetadataAsValue uses a canonical format which strips the actual MDNode for
1086 // MDNode with just a single constant value, storing just a ConstantAsMetadata
1087 // This undoes this canonicalization, reconstructing the MDNode.
1088 static MDNode *extractMDNode(MetadataAsValue *MAV) {
1089 Metadata *MD = MAV->getMetadata();
1090 assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
1091 "Expected a metadata node or a canonicalized constant");
1093 if (MDNode *N = dyn_cast<MDNode>(MD))
1094 return N;
1096 return MDNode::get(MAV->getContext(), MD);
1099 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
1100 MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
1102 unwrap<Instruction>(Inst)->setMetadata(KindID, N);
1105 struct LLVMOpaqueValueMetadataEntry {
1106 unsigned Kind;
1107 LLVMMetadataRef Metadata;
1110 using MetadataEntries = SmallVectorImpl<std::pair<unsigned, MDNode *>>;
1111 static LLVMValueMetadataEntry *
1112 llvm_getMetadata(size_t *NumEntries,
1113 llvm::function_ref<void(MetadataEntries &)> AccessMD) {
1114 SmallVector<std::pair<unsigned, MDNode *>, 8> MVEs;
1115 AccessMD(MVEs);
1117 LLVMOpaqueValueMetadataEntry *Result =
1118 static_cast<LLVMOpaqueValueMetadataEntry *>(
1119 safe_malloc(MVEs.size() * sizeof(LLVMOpaqueValueMetadataEntry)));
1120 for (unsigned i = 0; i < MVEs.size(); ++i) {
1121 const auto &ModuleFlag = MVEs[i];
1122 Result[i].Kind = ModuleFlag.first;
1123 Result[i].Metadata = wrap(ModuleFlag.second);
1125 *NumEntries = MVEs.size();
1126 return Result;
1129 LLVMValueMetadataEntry *
1130 LLVMInstructionGetAllMetadataOtherThanDebugLoc(LLVMValueRef Value,
1131 size_t *NumEntries) {
1132 return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
1133 Entries.clear();
1134 unwrap<Instruction>(Value)->getAllMetadata(Entries);
1138 /*--.. Conversion functions ................................................--*/
1140 #define LLVM_DEFINE_VALUE_CAST(name) \
1141 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
1142 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
1145 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
1147 LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) {
1148 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
1149 if (isa<MDNode>(MD->getMetadata()) ||
1150 isa<ValueAsMetadata>(MD->getMetadata()))
1151 return Val;
1152 return nullptr;
1155 LLVMValueRef LLVMIsAValueAsMetadata(LLVMValueRef Val) {
1156 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
1157 if (isa<ValueAsMetadata>(MD->getMetadata()))
1158 return Val;
1159 return nullptr;
1162 LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) {
1163 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
1164 if (isa<MDString>(MD->getMetadata()))
1165 return Val;
1166 return nullptr;
1169 /*--.. Operations on Uses ..................................................--*/
1170 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
1171 Value *V = unwrap(Val);
1172 Value::use_iterator I = V->use_begin();
1173 if (I == V->use_end())
1174 return nullptr;
1175 return wrap(&*I);
1178 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
1179 Use *Next = unwrap(U)->getNext();
1180 if (Next)
1181 return wrap(Next);
1182 return nullptr;
1185 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
1186 return wrap(unwrap(U)->getUser());
1189 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
1190 return wrap(unwrap(U)->get());
1193 /*--.. Operations on Users .................................................--*/
1195 static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N,
1196 unsigned Index) {
1197 Metadata *Op = N->getOperand(Index);
1198 if (!Op)
1199 return nullptr;
1200 if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
1201 return wrap(C->getValue());
1202 return wrap(MetadataAsValue::get(Context, Op));
1205 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
1206 Value *V = unwrap(Val);
1207 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
1208 if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1209 assert(Index == 0 && "Function-local metadata can only have one operand");
1210 return wrap(L->getValue());
1212 return getMDNodeOperandImpl(V->getContext(),
1213 cast<MDNode>(MD->getMetadata()), Index);
1216 return wrap(cast<User>(V)->getOperand(Index));
1219 LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) {
1220 Value *V = unwrap(Val);
1221 return wrap(&cast<User>(V)->getOperandUse(Index));
1224 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
1225 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
1228 int LLVMGetNumOperands(LLVMValueRef Val) {
1229 Value *V = unwrap(Val);
1230 if (isa<MetadataAsValue>(V))
1231 return LLVMGetMDNodeNumOperands(Val);
1233 return cast<User>(V)->getNumOperands();
1236 /*--.. Operations on constants of any type .................................--*/
1238 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
1239 return wrap(Constant::getNullValue(unwrap(Ty)));
1242 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
1243 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
1246 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
1247 return wrap(UndefValue::get(unwrap(Ty)));
1250 LLVMValueRef LLVMGetPoison(LLVMTypeRef Ty) {
1251 return wrap(PoisonValue::get(unwrap(Ty)));
1254 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
1255 return isa<Constant>(unwrap(Ty));
1258 LLVMBool LLVMIsNull(LLVMValueRef Val) {
1259 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
1260 return C->isNullValue();
1261 return false;
1264 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
1265 return isa<UndefValue>(unwrap(Val));
1268 LLVMBool LLVMIsPoison(LLVMValueRef Val) {
1269 return isa<PoisonValue>(unwrap(Val));
1272 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
1273 return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
1276 /*--.. Operations on metadata nodes ........................................--*/
1278 LLVMMetadataRef LLVMMDStringInContext2(LLVMContextRef C, const char *Str,
1279 size_t SLen) {
1280 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
1283 LLVMMetadataRef LLVMMDNodeInContext2(LLVMContextRef C, LLVMMetadataRef *MDs,
1284 size_t Count) {
1285 return wrap(MDNode::get(*unwrap(C), ArrayRef<Metadata*>(unwrap(MDs), Count)));
1288 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
1289 unsigned SLen) {
1290 LLVMContext &Context = *unwrap(C);
1291 return wrap(MetadataAsValue::get(
1292 Context, MDString::get(Context, StringRef(Str, SLen))));
1295 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
1296 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
1299 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
1300 unsigned Count) {
1301 LLVMContext &Context = *unwrap(C);
1302 SmallVector<Metadata *, 8> MDs;
1303 for (auto *OV : ArrayRef(Vals, Count)) {
1304 Value *V = unwrap(OV);
1305 Metadata *MD;
1306 if (!V)
1307 MD = nullptr;
1308 else if (auto *C = dyn_cast<Constant>(V))
1309 MD = ConstantAsMetadata::get(C);
1310 else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
1311 MD = MDV->getMetadata();
1312 assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
1313 "outside of direct argument to call");
1314 } else {
1315 // This is function-local metadata. Pretend to make an MDNode.
1316 assert(Count == 1 &&
1317 "Expected only one operand to function-local metadata");
1318 return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V)));
1321 MDs.push_back(MD);
1323 return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs)));
1326 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
1327 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
1330 LLVMValueRef LLVMMetadataAsValue(LLVMContextRef C, LLVMMetadataRef MD) {
1331 return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
1334 LLVMMetadataRef LLVMValueAsMetadata(LLVMValueRef Val) {
1335 auto *V = unwrap(Val);
1336 if (auto *C = dyn_cast<Constant>(V))
1337 return wrap(ConstantAsMetadata::get(C));
1338 if (auto *MAV = dyn_cast<MetadataAsValue>(V))
1339 return wrap(MAV->getMetadata());
1340 return wrap(ValueAsMetadata::get(V));
1343 const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
1344 if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
1345 if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
1346 *Length = S->getString().size();
1347 return S->getString().data();
1349 *Length = 0;
1350 return nullptr;
1353 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) {
1354 auto *MD = unwrap<MetadataAsValue>(V);
1355 if (isa<ValueAsMetadata>(MD->getMetadata()))
1356 return 1;
1357 return cast<MDNode>(MD->getMetadata())->getNumOperands();
1360 LLVMNamedMDNodeRef LLVMGetFirstNamedMetadata(LLVMModuleRef M) {
1361 Module *Mod = unwrap(M);
1362 Module::named_metadata_iterator I = Mod->named_metadata_begin();
1363 if (I == Mod->named_metadata_end())
1364 return nullptr;
1365 return wrap(&*I);
1368 LLVMNamedMDNodeRef LLVMGetLastNamedMetadata(LLVMModuleRef M) {
1369 Module *Mod = unwrap(M);
1370 Module::named_metadata_iterator I = Mod->named_metadata_end();
1371 if (I == Mod->named_metadata_begin())
1372 return nullptr;
1373 return wrap(&*--I);
1376 LLVMNamedMDNodeRef LLVMGetNextNamedMetadata(LLVMNamedMDNodeRef NMD) {
1377 NamedMDNode *NamedNode = unwrap(NMD);
1378 Module::named_metadata_iterator I(NamedNode);
1379 if (++I == NamedNode->getParent()->named_metadata_end())
1380 return nullptr;
1381 return wrap(&*I);
1384 LLVMNamedMDNodeRef LLVMGetPreviousNamedMetadata(LLVMNamedMDNodeRef NMD) {
1385 NamedMDNode *NamedNode = unwrap(NMD);
1386 Module::named_metadata_iterator I(NamedNode);
1387 if (I == NamedNode->getParent()->named_metadata_begin())
1388 return nullptr;
1389 return wrap(&*--I);
1392 LLVMNamedMDNodeRef LLVMGetNamedMetadata(LLVMModuleRef M,
1393 const char *Name, size_t NameLen) {
1394 return wrap(unwrap(M)->getNamedMetadata(StringRef(Name, NameLen)));
1397 LLVMNamedMDNodeRef LLVMGetOrInsertNamedMetadata(LLVMModuleRef M,
1398 const char *Name, size_t NameLen) {
1399 return wrap(unwrap(M)->getOrInsertNamedMetadata({Name, NameLen}));
1402 const char *LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NMD, size_t *NameLen) {
1403 NamedMDNode *NamedNode = unwrap(NMD);
1404 *NameLen = NamedNode->getName().size();
1405 return NamedNode->getName().data();
1408 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) {
1409 auto *MD = unwrap<MetadataAsValue>(V);
1410 if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1411 *Dest = wrap(MDV->getValue());
1412 return;
1414 const auto *N = cast<MDNode>(MD->getMetadata());
1415 const unsigned numOperands = N->getNumOperands();
1416 LLVMContext &Context = unwrap(V)->getContext();
1417 for (unsigned i = 0; i < numOperands; i++)
1418 Dest[i] = getMDNodeOperandImpl(Context, N, i);
1421 void LLVMReplaceMDNodeOperandWith(LLVMValueRef V, unsigned Index,
1422 LLVMMetadataRef Replacement) {
1423 auto *MD = cast<MetadataAsValue>(unwrap(V));
1424 auto *N = cast<MDNode>(MD->getMetadata());
1425 N->replaceOperandWith(Index, unwrap<Metadata>(Replacement));
1428 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char *Name) {
1429 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
1430 return N->getNumOperands();
1432 return 0;
1435 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char *Name,
1436 LLVMValueRef *Dest) {
1437 NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
1438 if (!N)
1439 return;
1440 LLVMContext &Context = unwrap(M)->getContext();
1441 for (unsigned i=0;i<N->getNumOperands();i++)
1442 Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
1445 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char *Name,
1446 LLVMValueRef Val) {
1447 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
1448 if (!N)
1449 return;
1450 if (!Val)
1451 return;
1452 N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
1455 const char *LLVMGetDebugLocDirectory(LLVMValueRef Val, unsigned *Length) {
1456 if (!Length) return nullptr;
1457 StringRef S;
1458 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1459 if (const auto &DL = I->getDebugLoc()) {
1460 S = DL->getDirectory();
1462 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1463 SmallVector<DIGlobalVariableExpression *, 1> GVEs;
1464 GV->getDebugInfo(GVEs);
1465 if (GVEs.size())
1466 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1467 S = DGV->getDirectory();
1468 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1469 if (const DISubprogram *DSP = F->getSubprogram())
1470 S = DSP->getDirectory();
1471 } else {
1472 assert(0 && "Expected Instruction, GlobalVariable or Function");
1473 return nullptr;
1475 *Length = S.size();
1476 return S.data();
1479 const char *LLVMGetDebugLocFilename(LLVMValueRef Val, unsigned *Length) {
1480 if (!Length) return nullptr;
1481 StringRef S;
1482 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1483 if (const auto &DL = I->getDebugLoc()) {
1484 S = DL->getFilename();
1486 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1487 SmallVector<DIGlobalVariableExpression *, 1> GVEs;
1488 GV->getDebugInfo(GVEs);
1489 if (GVEs.size())
1490 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1491 S = DGV->getFilename();
1492 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1493 if (const DISubprogram *DSP = F->getSubprogram())
1494 S = DSP->getFilename();
1495 } else {
1496 assert(0 && "Expected Instruction, GlobalVariable or Function");
1497 return nullptr;
1499 *Length = S.size();
1500 return S.data();
1503 unsigned LLVMGetDebugLocLine(LLVMValueRef Val) {
1504 unsigned L = 0;
1505 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1506 if (const auto &DL = I->getDebugLoc()) {
1507 L = DL->getLine();
1509 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1510 SmallVector<DIGlobalVariableExpression *, 1> GVEs;
1511 GV->getDebugInfo(GVEs);
1512 if (GVEs.size())
1513 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1514 L = DGV->getLine();
1515 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1516 if (const DISubprogram *DSP = F->getSubprogram())
1517 L = DSP->getLine();
1518 } else {
1519 assert(0 && "Expected Instruction, GlobalVariable or Function");
1520 return -1;
1522 return L;
1525 unsigned LLVMGetDebugLocColumn(LLVMValueRef Val) {
1526 unsigned C = 0;
1527 if (const auto *I = dyn_cast<Instruction>(unwrap(Val)))
1528 if (const auto &DL = I->getDebugLoc())
1529 C = DL->getColumn();
1530 return C;
1533 /*--.. Operations on scalar constants ......................................--*/
1535 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
1536 LLVMBool SignExtend) {
1537 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
1540 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
1541 unsigned NumWords,
1542 const uint64_t Words[]) {
1543 IntegerType *Ty = unwrap<IntegerType>(IntTy);
1544 return wrap(ConstantInt::get(
1545 Ty->getContext(), APInt(Ty->getBitWidth(), ArrayRef(Words, NumWords))));
1548 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
1549 uint8_t Radix) {
1550 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
1551 Radix));
1554 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
1555 unsigned SLen, uint8_t Radix) {
1556 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
1557 Radix));
1560 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
1561 return wrap(ConstantFP::get(unwrap(RealTy), N));
1564 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
1565 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
1568 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
1569 unsigned SLen) {
1570 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
1573 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
1574 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
1577 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
1578 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
1581 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
1582 ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
1583 Type *Ty = cFP->getType();
1585 if (Ty->isHalfTy() || Ty->isBFloatTy() || Ty->isFloatTy() ||
1586 Ty->isDoubleTy()) {
1587 *LosesInfo = false;
1588 return cFP->getValueAPF().convertToDouble();
1591 bool APFLosesInfo;
1592 APFloat APF = cFP->getValueAPF();
1593 APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo);
1594 *LosesInfo = APFLosesInfo;
1595 return APF.convertToDouble();
1598 /*--.. Operations on composite constants ...................................--*/
1600 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
1601 unsigned Length,
1602 LLVMBool DontNullTerminate) {
1603 /* Inverted the sense of AddNull because ', 0)' is a
1604 better mnemonic for null termination than ', 1)'. */
1605 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
1606 DontNullTerminate == 0));
1609 LLVMValueRef LLVMConstStringInContext2(LLVMContextRef C, const char *Str,
1610 size_t Length,
1611 LLVMBool DontNullTerminate) {
1612 /* Inverted the sense of AddNull because ', 0)' is a
1613 better mnemonic for null termination than ', 1)'. */
1614 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
1615 DontNullTerminate == 0));
1618 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
1619 LLVMBool DontNullTerminate) {
1620 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
1621 DontNullTerminate);
1624 LLVMValueRef LLVMGetAggregateElement(LLVMValueRef C, unsigned Idx) {
1625 return wrap(unwrap<Constant>(C)->getAggregateElement(Idx));
1628 LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef C, unsigned idx) {
1629 return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
1632 LLVMBool LLVMIsConstantString(LLVMValueRef C) {
1633 return unwrap<ConstantDataSequential>(C)->isString();
1636 const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1637 StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1638 *Length = Str.size();
1639 return Str.data();
1642 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
1643 LLVMValueRef *ConstantVals, unsigned Length) {
1644 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1645 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1648 LLVMValueRef LLVMConstArray2(LLVMTypeRef ElementTy, LLVMValueRef *ConstantVals,
1649 uint64_t Length) {
1650 ArrayRef<Constant *> V(unwrap<Constant>(ConstantVals, Length), Length);
1651 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1654 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
1655 LLVMValueRef *ConstantVals,
1656 unsigned Count, LLVMBool Packed) {
1657 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1658 return wrap(ConstantStruct::getAnon(*unwrap(C), ArrayRef(Elements, Count),
1659 Packed != 0));
1662 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
1663 LLVMBool Packed) {
1664 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1665 Packed);
1668 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
1669 LLVMValueRef *ConstantVals,
1670 unsigned Count) {
1671 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1672 StructType *Ty = unwrap<StructType>(StructTy);
1674 return wrap(ConstantStruct::get(Ty, ArrayRef(Elements, Count)));
1677 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
1678 return wrap(ConstantVector::get(
1679 ArrayRef(unwrap<Constant>(ScalarConstantVals, Size), Size)));
1682 LLVMValueRef LLVMConstantPtrAuth(LLVMValueRef Ptr, LLVMValueRef Key,
1683 LLVMValueRef Disc, LLVMValueRef AddrDisc) {
1684 return wrap(ConstantPtrAuth::get(
1685 unwrap<Constant>(Ptr), unwrap<ConstantInt>(Key),
1686 unwrap<ConstantInt>(Disc), unwrap<Constant>(AddrDisc)));
1689 /*-- Opcode mapping */
1691 static LLVMOpcode map_to_llvmopcode(int opcode)
1693 switch (opcode) {
1694 default: llvm_unreachable("Unhandled Opcode.");
1695 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
1696 #include "llvm/IR/Instruction.def"
1697 #undef HANDLE_INST
1701 static int map_from_llvmopcode(LLVMOpcode code)
1703 switch (code) {
1704 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
1705 #include "llvm/IR/Instruction.def"
1706 #undef HANDLE_INST
1708 llvm_unreachable("Unhandled Opcode.");
1711 /*-- GEP wrap flag conversions */
1713 static GEPNoWrapFlags mapFromLLVMGEPNoWrapFlags(LLVMGEPNoWrapFlags GEPFlags) {
1714 GEPNoWrapFlags NewGEPFlags;
1715 if ((GEPFlags & LLVMGEPFlagInBounds) != 0)
1716 NewGEPFlags |= GEPNoWrapFlags::inBounds();
1717 if ((GEPFlags & LLVMGEPFlagNUSW) != 0)
1718 NewGEPFlags |= GEPNoWrapFlags::noUnsignedSignedWrap();
1719 if ((GEPFlags & LLVMGEPFlagNUW) != 0)
1720 NewGEPFlags |= GEPNoWrapFlags::noUnsignedWrap();
1722 return NewGEPFlags;
1725 static LLVMGEPNoWrapFlags mapToLLVMGEPNoWrapFlags(GEPNoWrapFlags GEPFlags) {
1726 LLVMGEPNoWrapFlags NewGEPFlags = 0;
1727 if (GEPFlags.isInBounds())
1728 NewGEPFlags |= LLVMGEPFlagInBounds;
1729 if (GEPFlags.hasNoUnsignedSignedWrap())
1730 NewGEPFlags |= LLVMGEPFlagNUSW;
1731 if (GEPFlags.hasNoUnsignedWrap())
1732 NewGEPFlags |= LLVMGEPFlagNUW;
1734 return NewGEPFlags;
1737 /*--.. Constant expressions ................................................--*/
1739 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
1740 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
1743 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
1744 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
1747 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
1748 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
1751 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
1752 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1755 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
1756 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
1759 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
1760 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1764 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
1765 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
1768 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1769 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
1770 unwrap<Constant>(RHSConstant)));
1773 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
1774 LLVMValueRef RHSConstant) {
1775 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
1776 unwrap<Constant>(RHSConstant)));
1779 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
1780 LLVMValueRef RHSConstant) {
1781 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
1782 unwrap<Constant>(RHSConstant)));
1785 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1786 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
1787 unwrap<Constant>(RHSConstant)));
1790 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
1791 LLVMValueRef RHSConstant) {
1792 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
1793 unwrap<Constant>(RHSConstant)));
1796 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
1797 LLVMValueRef RHSConstant) {
1798 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
1799 unwrap<Constant>(RHSConstant)));
1802 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1803 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
1804 unwrap<Constant>(RHSConstant)));
1807 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
1808 LLVMValueRef RHSConstant) {
1809 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
1810 unwrap<Constant>(RHSConstant)));
1813 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
1814 LLVMValueRef RHSConstant) {
1815 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
1816 unwrap<Constant>(RHSConstant)));
1819 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1820 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
1821 unwrap<Constant>(RHSConstant)));
1824 LLVMValueRef LLVMConstGEP2(LLVMTypeRef Ty, LLVMValueRef ConstantVal,
1825 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1826 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1827 NumIndices);
1828 Constant *Val = unwrap<Constant>(ConstantVal);
1829 return wrap(ConstantExpr::getGetElementPtr(unwrap(Ty), Val, IdxList));
1832 LLVMValueRef LLVMConstInBoundsGEP2(LLVMTypeRef Ty, LLVMValueRef ConstantVal,
1833 LLVMValueRef *ConstantIndices,
1834 unsigned NumIndices) {
1835 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1836 NumIndices);
1837 Constant *Val = unwrap<Constant>(ConstantVal);
1838 return wrap(ConstantExpr::getInBoundsGetElementPtr(unwrap(Ty), Val, IdxList));
1841 LLVMValueRef LLVMConstGEPWithNoWrapFlags(LLVMTypeRef Ty,
1842 LLVMValueRef ConstantVal,
1843 LLVMValueRef *ConstantIndices,
1844 unsigned NumIndices,
1845 LLVMGEPNoWrapFlags NoWrapFlags) {
1846 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1847 NumIndices);
1848 Constant *Val = unwrap<Constant>(ConstantVal);
1849 return wrap(ConstantExpr::getGetElementPtr(
1850 unwrap(Ty), Val, IdxList, mapFromLLVMGEPNoWrapFlags(NoWrapFlags)));
1853 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1854 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1855 unwrap(ToType)));
1858 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1859 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1860 unwrap(ToType)));
1863 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1864 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1865 unwrap(ToType)));
1868 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1869 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1870 unwrap(ToType)));
1873 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1874 LLVMTypeRef ToType) {
1875 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1876 unwrap(ToType)));
1879 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1880 LLVMTypeRef ToType) {
1881 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1882 unwrap(ToType)));
1885 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1886 LLVMTypeRef ToType) {
1887 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1888 unwrap(ToType)));
1891 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1892 LLVMValueRef IndexConstant) {
1893 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1894 unwrap<Constant>(IndexConstant)));
1897 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1898 LLVMValueRef ElementValueConstant,
1899 LLVMValueRef IndexConstant) {
1900 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1901 unwrap<Constant>(ElementValueConstant),
1902 unwrap<Constant>(IndexConstant)));
1905 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1906 LLVMValueRef VectorBConstant,
1907 LLVMValueRef MaskConstant) {
1908 SmallVector<int, 16> IntMask;
1909 ShuffleVectorInst::getShuffleMask(unwrap<Constant>(MaskConstant), IntMask);
1910 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1911 unwrap<Constant>(VectorBConstant),
1912 IntMask));
1915 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1916 const char *Constraints,
1917 LLVMBool HasSideEffects,
1918 LLVMBool IsAlignStack) {
1919 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1920 Constraints, HasSideEffects, IsAlignStack));
1923 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1924 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1927 LLVMValueRef LLVMGetBlockAddressFunction(LLVMValueRef BlockAddr) {
1928 return wrap(unwrap<BlockAddress>(BlockAddr)->getFunction());
1931 LLVMBasicBlockRef LLVMGetBlockAddressBasicBlock(LLVMValueRef BlockAddr) {
1932 return wrap(unwrap<BlockAddress>(BlockAddr)->getBasicBlock());
1935 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1937 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1938 return wrap(unwrap<GlobalValue>(Global)->getParent());
1941 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1942 return unwrap<GlobalValue>(Global)->isDeclaration();
1945 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1946 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1947 case GlobalValue::ExternalLinkage:
1948 return LLVMExternalLinkage;
1949 case GlobalValue::AvailableExternallyLinkage:
1950 return LLVMAvailableExternallyLinkage;
1951 case GlobalValue::LinkOnceAnyLinkage:
1952 return LLVMLinkOnceAnyLinkage;
1953 case GlobalValue::LinkOnceODRLinkage:
1954 return LLVMLinkOnceODRLinkage;
1955 case GlobalValue::WeakAnyLinkage:
1956 return LLVMWeakAnyLinkage;
1957 case GlobalValue::WeakODRLinkage:
1958 return LLVMWeakODRLinkage;
1959 case GlobalValue::AppendingLinkage:
1960 return LLVMAppendingLinkage;
1961 case GlobalValue::InternalLinkage:
1962 return LLVMInternalLinkage;
1963 case GlobalValue::PrivateLinkage:
1964 return LLVMPrivateLinkage;
1965 case GlobalValue::ExternalWeakLinkage:
1966 return LLVMExternalWeakLinkage;
1967 case GlobalValue::CommonLinkage:
1968 return LLVMCommonLinkage;
1971 llvm_unreachable("Invalid GlobalValue linkage!");
1974 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1975 GlobalValue *GV = unwrap<GlobalValue>(Global);
1977 switch (Linkage) {
1978 case LLVMExternalLinkage:
1979 GV->setLinkage(GlobalValue::ExternalLinkage);
1980 break;
1981 case LLVMAvailableExternallyLinkage:
1982 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1983 break;
1984 case LLVMLinkOnceAnyLinkage:
1985 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1986 break;
1987 case LLVMLinkOnceODRLinkage:
1988 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1989 break;
1990 case LLVMLinkOnceODRAutoHideLinkage:
1991 LLVM_DEBUG(
1992 errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1993 "longer supported.");
1994 break;
1995 case LLVMWeakAnyLinkage:
1996 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1997 break;
1998 case LLVMWeakODRLinkage:
1999 GV->setLinkage(GlobalValue::WeakODRLinkage);
2000 break;
2001 case LLVMAppendingLinkage:
2002 GV->setLinkage(GlobalValue::AppendingLinkage);
2003 break;
2004 case LLVMInternalLinkage:
2005 GV->setLinkage(GlobalValue::InternalLinkage);
2006 break;
2007 case LLVMPrivateLinkage:
2008 GV->setLinkage(GlobalValue::PrivateLinkage);
2009 break;
2010 case LLVMLinkerPrivateLinkage:
2011 GV->setLinkage(GlobalValue::PrivateLinkage);
2012 break;
2013 case LLVMLinkerPrivateWeakLinkage:
2014 GV->setLinkage(GlobalValue::PrivateLinkage);
2015 break;
2016 case LLVMDLLImportLinkage:
2017 LLVM_DEBUG(
2018 errs()
2019 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
2020 break;
2021 case LLVMDLLExportLinkage:
2022 LLVM_DEBUG(
2023 errs()
2024 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
2025 break;
2026 case LLVMExternalWeakLinkage:
2027 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
2028 break;
2029 case LLVMGhostLinkage:
2030 LLVM_DEBUG(
2031 errs() << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
2032 break;
2033 case LLVMCommonLinkage:
2034 GV->setLinkage(GlobalValue::CommonLinkage);
2035 break;
2039 const char *LLVMGetSection(LLVMValueRef Global) {
2040 // Using .data() is safe because of how GlobalObject::setSection is
2041 // implemented.
2042 return unwrap<GlobalValue>(Global)->getSection().data();
2045 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
2046 unwrap<GlobalObject>(Global)->setSection(Section);
2049 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
2050 return static_cast<LLVMVisibility>(
2051 unwrap<GlobalValue>(Global)->getVisibility());
2054 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
2055 unwrap<GlobalValue>(Global)
2056 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
2059 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
2060 return static_cast<LLVMDLLStorageClass>(
2061 unwrap<GlobalValue>(Global)->getDLLStorageClass());
2064 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
2065 unwrap<GlobalValue>(Global)->setDLLStorageClass(
2066 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
2069 LLVMUnnamedAddr LLVMGetUnnamedAddress(LLVMValueRef Global) {
2070 switch (unwrap<GlobalValue>(Global)->getUnnamedAddr()) {
2071 case GlobalVariable::UnnamedAddr::None:
2072 return LLVMNoUnnamedAddr;
2073 case GlobalVariable::UnnamedAddr::Local:
2074 return LLVMLocalUnnamedAddr;
2075 case GlobalVariable::UnnamedAddr::Global:
2076 return LLVMGlobalUnnamedAddr;
2078 llvm_unreachable("Unknown UnnamedAddr kind!");
2081 void LLVMSetUnnamedAddress(LLVMValueRef Global, LLVMUnnamedAddr UnnamedAddr) {
2082 GlobalValue *GV = unwrap<GlobalValue>(Global);
2084 switch (UnnamedAddr) {
2085 case LLVMNoUnnamedAddr:
2086 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::None);
2087 case LLVMLocalUnnamedAddr:
2088 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Local);
2089 case LLVMGlobalUnnamedAddr:
2090 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Global);
2094 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
2095 return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
2098 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
2099 unwrap<GlobalValue>(Global)->setUnnamedAddr(
2100 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
2101 : GlobalValue::UnnamedAddr::None);
2104 LLVMTypeRef LLVMGlobalGetValueType(LLVMValueRef Global) {
2105 return wrap(unwrap<GlobalValue>(Global)->getValueType());
2108 /*--.. Operations on global variables, load and store instructions .........--*/
2110 unsigned LLVMGetAlignment(LLVMValueRef V) {
2111 Value *P = unwrap(V);
2112 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2113 return GV->getAlign() ? GV->getAlign()->value() : 0;
2114 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
2115 return AI->getAlign().value();
2116 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2117 return LI->getAlign().value();
2118 if (StoreInst *SI = dyn_cast<StoreInst>(P))
2119 return SI->getAlign().value();
2120 if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2121 return RMWI->getAlign().value();
2122 if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2123 return CXI->getAlign().value();
2125 llvm_unreachable(
2126 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, "
2127 "and AtomicCmpXchgInst have alignment");
2130 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
2131 Value *P = unwrap(V);
2132 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2133 GV->setAlignment(MaybeAlign(Bytes));
2134 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
2135 AI->setAlignment(Align(Bytes));
2136 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
2137 LI->setAlignment(Align(Bytes));
2138 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
2139 SI->setAlignment(Align(Bytes));
2140 else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2141 RMWI->setAlignment(Align(Bytes));
2142 else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2143 CXI->setAlignment(Align(Bytes));
2144 else
2145 llvm_unreachable(
2146 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, and "
2147 "and AtomicCmpXchgInst have alignment");
2150 LLVMValueMetadataEntry *LLVMGlobalCopyAllMetadata(LLVMValueRef Value,
2151 size_t *NumEntries) {
2152 return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
2153 Entries.clear();
2154 if (Instruction *Instr = dyn_cast<Instruction>(unwrap(Value))) {
2155 Instr->getAllMetadata(Entries);
2156 } else {
2157 unwrap<GlobalObject>(Value)->getAllMetadata(Entries);
2162 unsigned LLVMValueMetadataEntriesGetKind(LLVMValueMetadataEntry *Entries,
2163 unsigned Index) {
2164 LLVMOpaqueValueMetadataEntry MVE =
2165 static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2166 return MVE.Kind;
2169 LLVMMetadataRef
2170 LLVMValueMetadataEntriesGetMetadata(LLVMValueMetadataEntry *Entries,
2171 unsigned Index) {
2172 LLVMOpaqueValueMetadataEntry MVE =
2173 static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2174 return MVE.Metadata;
2177 void LLVMDisposeValueMetadataEntries(LLVMValueMetadataEntry *Entries) {
2178 free(Entries);
2181 void LLVMGlobalSetMetadata(LLVMValueRef Global, unsigned Kind,
2182 LLVMMetadataRef MD) {
2183 unwrap<GlobalObject>(Global)->setMetadata(Kind, unwrap<MDNode>(MD));
2186 void LLVMGlobalEraseMetadata(LLVMValueRef Global, unsigned Kind) {
2187 unwrap<GlobalObject>(Global)->eraseMetadata(Kind);
2190 void LLVMGlobalClearMetadata(LLVMValueRef Global) {
2191 unwrap<GlobalObject>(Global)->clearMetadata();
2194 /*--.. Operations on global variables ......................................--*/
2196 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
2197 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2198 GlobalValue::ExternalLinkage, nullptr, Name));
2201 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
2202 const char *Name,
2203 unsigned AddressSpace) {
2204 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2205 GlobalValue::ExternalLinkage, nullptr, Name,
2206 nullptr, GlobalVariable::NotThreadLocal,
2207 AddressSpace));
2210 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
2211 return wrap(unwrap(M)->getNamedGlobal(Name));
2214 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
2215 Module *Mod = unwrap(M);
2216 Module::global_iterator I = Mod->global_begin();
2217 if (I == Mod->global_end())
2218 return nullptr;
2219 return wrap(&*I);
2222 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
2223 Module *Mod = unwrap(M);
2224 Module::global_iterator I = Mod->global_end();
2225 if (I == Mod->global_begin())
2226 return nullptr;
2227 return wrap(&*--I);
2230 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
2231 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2232 Module::global_iterator I(GV);
2233 if (++I == GV->getParent()->global_end())
2234 return nullptr;
2235 return wrap(&*I);
2238 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
2239 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2240 Module::global_iterator I(GV);
2241 if (I == GV->getParent()->global_begin())
2242 return nullptr;
2243 return wrap(&*--I);
2246 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
2247 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
2250 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
2251 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
2252 if ( !GV->hasInitializer() )
2253 return nullptr;
2254 return wrap(GV->getInitializer());
2257 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
2258 unwrap<GlobalVariable>(GlobalVar)
2259 ->setInitializer(unwrap<Constant>(ConstantVal));
2262 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
2263 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
2266 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
2267 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
2270 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
2271 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
2274 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
2275 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
2278 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
2279 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
2280 case GlobalVariable::NotThreadLocal:
2281 return LLVMNotThreadLocal;
2282 case GlobalVariable::GeneralDynamicTLSModel:
2283 return LLVMGeneralDynamicTLSModel;
2284 case GlobalVariable::LocalDynamicTLSModel:
2285 return LLVMLocalDynamicTLSModel;
2286 case GlobalVariable::InitialExecTLSModel:
2287 return LLVMInitialExecTLSModel;
2288 case GlobalVariable::LocalExecTLSModel:
2289 return LLVMLocalExecTLSModel;
2292 llvm_unreachable("Invalid GlobalVariable thread local mode");
2295 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
2296 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2298 switch (Mode) {
2299 case LLVMNotThreadLocal:
2300 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
2301 break;
2302 case LLVMGeneralDynamicTLSModel:
2303 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
2304 break;
2305 case LLVMLocalDynamicTLSModel:
2306 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
2307 break;
2308 case LLVMInitialExecTLSModel:
2309 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
2310 break;
2311 case LLVMLocalExecTLSModel:
2312 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
2313 break;
2317 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
2318 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
2321 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
2322 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
2325 /*--.. Operations on aliases ......................................--*/
2327 LLVMValueRef LLVMAddAlias2(LLVMModuleRef M, LLVMTypeRef ValueTy,
2328 unsigned AddrSpace, LLVMValueRef Aliasee,
2329 const char *Name) {
2330 return wrap(GlobalAlias::create(unwrap(ValueTy), AddrSpace,
2331 GlobalValue::ExternalLinkage, Name,
2332 unwrap<Constant>(Aliasee), unwrap(M)));
2335 LLVMValueRef LLVMGetNamedGlobalAlias(LLVMModuleRef M,
2336 const char *Name, size_t NameLen) {
2337 return wrap(unwrap(M)->getNamedAlias(StringRef(Name, NameLen)));
2340 LLVMValueRef LLVMGetFirstGlobalAlias(LLVMModuleRef M) {
2341 Module *Mod = unwrap(M);
2342 Module::alias_iterator I = Mod->alias_begin();
2343 if (I == Mod->alias_end())
2344 return nullptr;
2345 return wrap(&*I);
2348 LLVMValueRef LLVMGetLastGlobalAlias(LLVMModuleRef M) {
2349 Module *Mod = unwrap(M);
2350 Module::alias_iterator I = Mod->alias_end();
2351 if (I == Mod->alias_begin())
2352 return nullptr;
2353 return wrap(&*--I);
2356 LLVMValueRef LLVMGetNextGlobalAlias(LLVMValueRef GA) {
2357 GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2358 Module::alias_iterator I(Alias);
2359 if (++I == Alias->getParent()->alias_end())
2360 return nullptr;
2361 return wrap(&*I);
2364 LLVMValueRef LLVMGetPreviousGlobalAlias(LLVMValueRef GA) {
2365 GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2366 Module::alias_iterator I(Alias);
2367 if (I == Alias->getParent()->alias_begin())
2368 return nullptr;
2369 return wrap(&*--I);
2372 LLVMValueRef LLVMAliasGetAliasee(LLVMValueRef Alias) {
2373 return wrap(unwrap<GlobalAlias>(Alias)->getAliasee());
2376 void LLVMAliasSetAliasee(LLVMValueRef Alias, LLVMValueRef Aliasee) {
2377 unwrap<GlobalAlias>(Alias)->setAliasee(unwrap<Constant>(Aliasee));
2380 /*--.. Operations on functions .............................................--*/
2382 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
2383 LLVMTypeRef FunctionTy) {
2384 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
2385 GlobalValue::ExternalLinkage, Name, unwrap(M)));
2388 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
2389 return wrap(unwrap(M)->getFunction(Name));
2392 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
2393 Module *Mod = unwrap(M);
2394 Module::iterator I = Mod->begin();
2395 if (I == Mod->end())
2396 return nullptr;
2397 return wrap(&*I);
2400 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
2401 Module *Mod = unwrap(M);
2402 Module::iterator I = Mod->end();
2403 if (I == Mod->begin())
2404 return nullptr;
2405 return wrap(&*--I);
2408 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
2409 Function *Func = unwrap<Function>(Fn);
2410 Module::iterator I(Func);
2411 if (++I == Func->getParent()->end())
2412 return nullptr;
2413 return wrap(&*I);
2416 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
2417 Function *Func = unwrap<Function>(Fn);
2418 Module::iterator I(Func);
2419 if (I == Func->getParent()->begin())
2420 return nullptr;
2421 return wrap(&*--I);
2424 void LLVMDeleteFunction(LLVMValueRef Fn) {
2425 unwrap<Function>(Fn)->eraseFromParent();
2428 LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) {
2429 return unwrap<Function>(Fn)->hasPersonalityFn();
2432 LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) {
2433 return wrap(unwrap<Function>(Fn)->getPersonalityFn());
2436 void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) {
2437 unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
2440 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
2441 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
2442 return F->getIntrinsicID();
2443 return 0;
2446 static Intrinsic::ID llvm_map_to_intrinsic_id(unsigned ID) {
2447 assert(ID < llvm::Intrinsic::num_intrinsics && "Intrinsic ID out of range");
2448 return llvm::Intrinsic::ID(ID);
2451 LLVMValueRef LLVMGetIntrinsicDeclaration(LLVMModuleRef Mod,
2452 unsigned ID,
2453 LLVMTypeRef *ParamTypes,
2454 size_t ParamCount) {
2455 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2456 auto IID = llvm_map_to_intrinsic_id(ID);
2457 return wrap(llvm::Intrinsic::getDeclaration(unwrap(Mod), IID, Tys));
2460 const char *LLVMIntrinsicGetName(unsigned ID, size_t *NameLength) {
2461 auto IID = llvm_map_to_intrinsic_id(ID);
2462 auto Str = llvm::Intrinsic::getName(IID);
2463 *NameLength = Str.size();
2464 return Str.data();
2467 LLVMTypeRef LLVMIntrinsicGetType(LLVMContextRef Ctx, unsigned ID,
2468 LLVMTypeRef *ParamTypes, size_t ParamCount) {
2469 auto IID = llvm_map_to_intrinsic_id(ID);
2470 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2471 return wrap(llvm::Intrinsic::getType(*unwrap(Ctx), IID, Tys));
2474 const char *LLVMIntrinsicCopyOverloadedName(unsigned ID,
2475 LLVMTypeRef *ParamTypes,
2476 size_t ParamCount,
2477 size_t *NameLength) {
2478 auto IID = llvm_map_to_intrinsic_id(ID);
2479 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2480 auto Str = llvm::Intrinsic::getNameNoUnnamedTypes(IID, Tys);
2481 *NameLength = Str.length();
2482 return strdup(Str.c_str());
2485 const char *LLVMIntrinsicCopyOverloadedName2(LLVMModuleRef Mod, unsigned ID,
2486 LLVMTypeRef *ParamTypes,
2487 size_t ParamCount,
2488 size_t *NameLength) {
2489 auto IID = llvm_map_to_intrinsic_id(ID);
2490 ArrayRef<Type *> Tys(unwrap(ParamTypes), ParamCount);
2491 auto Str = llvm::Intrinsic::getName(IID, Tys, unwrap(Mod));
2492 *NameLength = Str.length();
2493 return strdup(Str.c_str());
2496 unsigned LLVMLookupIntrinsicID(const char *Name, size_t NameLen) {
2497 return Function::lookupIntrinsicID({Name, NameLen});
2500 LLVMBool LLVMIntrinsicIsOverloaded(unsigned ID) {
2501 auto IID = llvm_map_to_intrinsic_id(ID);
2502 return llvm::Intrinsic::isOverloaded(IID);
2505 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
2506 return unwrap<Function>(Fn)->getCallingConv();
2509 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
2510 return unwrap<Function>(Fn)->setCallingConv(
2511 static_cast<CallingConv::ID>(CC));
2514 const char *LLVMGetGC(LLVMValueRef Fn) {
2515 Function *F = unwrap<Function>(Fn);
2516 return F->hasGC()? F->getGC().c_str() : nullptr;
2519 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
2520 Function *F = unwrap<Function>(Fn);
2521 if (GC)
2522 F->setGC(GC);
2523 else
2524 F->clearGC();
2527 LLVMValueRef LLVMGetPrefixData(LLVMValueRef Fn) {
2528 Function *F = unwrap<Function>(Fn);
2529 return wrap(F->getPrefixData());
2532 LLVMBool LLVMHasPrefixData(LLVMValueRef Fn) {
2533 Function *F = unwrap<Function>(Fn);
2534 return F->hasPrefixData();
2537 void LLVMSetPrefixData(LLVMValueRef Fn, LLVMValueRef prefixData) {
2538 Function *F = unwrap<Function>(Fn);
2539 Constant *prefix = unwrap<Constant>(prefixData);
2540 F->setPrefixData(prefix);
2543 LLVMValueRef LLVMGetPrologueData(LLVMValueRef Fn) {
2544 Function *F = unwrap<Function>(Fn);
2545 return wrap(F->getPrologueData());
2548 LLVMBool LLVMHasPrologueData(LLVMValueRef Fn) {
2549 Function *F = unwrap<Function>(Fn);
2550 return F->hasPrologueData();
2553 void LLVMSetPrologueData(LLVMValueRef Fn, LLVMValueRef prologueData) {
2554 Function *F = unwrap<Function>(Fn);
2555 Constant *prologue = unwrap<Constant>(prologueData);
2556 F->setPrologueData(prologue);
2559 void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2560 LLVMAttributeRef A) {
2561 unwrap<Function>(F)->addAttributeAtIndex(Idx, unwrap(A));
2564 unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) {
2565 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2566 return AS.getNumAttributes();
2569 void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2570 LLVMAttributeRef *Attrs) {
2571 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2572 for (auto A : AS)
2573 *Attrs++ = wrap(A);
2576 LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F,
2577 LLVMAttributeIndex Idx,
2578 unsigned KindID) {
2579 return wrap(unwrap<Function>(F)->getAttributeAtIndex(
2580 Idx, (Attribute::AttrKind)KindID));
2583 LLVMAttributeRef LLVMGetStringAttributeAtIndex(LLVMValueRef F,
2584 LLVMAttributeIndex Idx,
2585 const char *K, unsigned KLen) {
2586 return wrap(
2587 unwrap<Function>(F)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
2590 void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2591 unsigned KindID) {
2592 unwrap<Function>(F)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
2595 void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2596 const char *K, unsigned KLen) {
2597 unwrap<Function>(F)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
2600 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
2601 const char *V) {
2602 Function *Func = unwrap<Function>(Fn);
2603 Attribute Attr = Attribute::get(Func->getContext(), A, V);
2604 Func->addFnAttr(Attr);
2607 /*--.. Operations on parameters ............................................--*/
2609 unsigned LLVMCountParams(LLVMValueRef FnRef) {
2610 // This function is strictly redundant to
2611 // LLVMCountParamTypes(LLVMGlobalGetValueType(FnRef))
2612 return unwrap<Function>(FnRef)->arg_size();
2615 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
2616 Function *Fn = unwrap<Function>(FnRef);
2617 for (Argument &A : Fn->args())
2618 *ParamRefs++ = wrap(&A);
2621 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
2622 Function *Fn = unwrap<Function>(FnRef);
2623 return wrap(&Fn->arg_begin()[index]);
2626 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
2627 return wrap(unwrap<Argument>(V)->getParent());
2630 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
2631 Function *Func = unwrap<Function>(Fn);
2632 Function::arg_iterator I = Func->arg_begin();
2633 if (I == Func->arg_end())
2634 return nullptr;
2635 return wrap(&*I);
2638 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
2639 Function *Func = unwrap<Function>(Fn);
2640 Function::arg_iterator I = Func->arg_end();
2641 if (I == Func->arg_begin())
2642 return nullptr;
2643 return wrap(&*--I);
2646 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
2647 Argument *A = unwrap<Argument>(Arg);
2648 Function *Fn = A->getParent();
2649 if (A->getArgNo() + 1 >= Fn->arg_size())
2650 return nullptr;
2651 return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
2654 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
2655 Argument *A = unwrap<Argument>(Arg);
2656 if (A->getArgNo() == 0)
2657 return nullptr;
2658 return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
2661 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
2662 Argument *A = unwrap<Argument>(Arg);
2663 A->addAttr(Attribute::getWithAlignment(A->getContext(), Align(align)));
2666 /*--.. Operations on ifuncs ................................................--*/
2668 LLVMValueRef LLVMAddGlobalIFunc(LLVMModuleRef M,
2669 const char *Name, size_t NameLen,
2670 LLVMTypeRef Ty, unsigned AddrSpace,
2671 LLVMValueRef Resolver) {
2672 return wrap(GlobalIFunc::create(unwrap(Ty), AddrSpace,
2673 GlobalValue::ExternalLinkage,
2674 StringRef(Name, NameLen),
2675 unwrap<Constant>(Resolver), unwrap(M)));
2678 LLVMValueRef LLVMGetNamedGlobalIFunc(LLVMModuleRef M,
2679 const char *Name, size_t NameLen) {
2680 return wrap(unwrap(M)->getNamedIFunc(StringRef(Name, NameLen)));
2683 LLVMValueRef LLVMGetFirstGlobalIFunc(LLVMModuleRef M) {
2684 Module *Mod = unwrap(M);
2685 Module::ifunc_iterator I = Mod->ifunc_begin();
2686 if (I == Mod->ifunc_end())
2687 return nullptr;
2688 return wrap(&*I);
2691 LLVMValueRef LLVMGetLastGlobalIFunc(LLVMModuleRef M) {
2692 Module *Mod = unwrap(M);
2693 Module::ifunc_iterator I = Mod->ifunc_end();
2694 if (I == Mod->ifunc_begin())
2695 return nullptr;
2696 return wrap(&*--I);
2699 LLVMValueRef LLVMGetNextGlobalIFunc(LLVMValueRef IFunc) {
2700 GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2701 Module::ifunc_iterator I(GIF);
2702 if (++I == GIF->getParent()->ifunc_end())
2703 return nullptr;
2704 return wrap(&*I);
2707 LLVMValueRef LLVMGetPreviousGlobalIFunc(LLVMValueRef IFunc) {
2708 GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2709 Module::ifunc_iterator I(GIF);
2710 if (I == GIF->getParent()->ifunc_begin())
2711 return nullptr;
2712 return wrap(&*--I);
2715 LLVMValueRef LLVMGetGlobalIFuncResolver(LLVMValueRef IFunc) {
2716 return wrap(unwrap<GlobalIFunc>(IFunc)->getResolver());
2719 void LLVMSetGlobalIFuncResolver(LLVMValueRef IFunc, LLVMValueRef Resolver) {
2720 unwrap<GlobalIFunc>(IFunc)->setResolver(unwrap<Constant>(Resolver));
2723 void LLVMEraseGlobalIFunc(LLVMValueRef IFunc) {
2724 unwrap<GlobalIFunc>(IFunc)->eraseFromParent();
2727 void LLVMRemoveGlobalIFunc(LLVMValueRef IFunc) {
2728 unwrap<GlobalIFunc>(IFunc)->removeFromParent();
2731 /*--.. Operations on operand bundles........................................--*/
2733 LLVMOperandBundleRef LLVMCreateOperandBundle(const char *Tag, size_t TagLen,
2734 LLVMValueRef *Args,
2735 unsigned NumArgs) {
2736 return wrap(new OperandBundleDef(std::string(Tag, TagLen),
2737 ArrayRef(unwrap(Args), NumArgs)));
2740 void LLVMDisposeOperandBundle(LLVMOperandBundleRef Bundle) {
2741 delete unwrap(Bundle);
2744 const char *LLVMGetOperandBundleTag(LLVMOperandBundleRef Bundle, size_t *Len) {
2745 StringRef Str = unwrap(Bundle)->getTag();
2746 *Len = Str.size();
2747 return Str.data();
2750 unsigned LLVMGetNumOperandBundleArgs(LLVMOperandBundleRef Bundle) {
2751 return unwrap(Bundle)->inputs().size();
2754 LLVMValueRef LLVMGetOperandBundleArgAtIndex(LLVMOperandBundleRef Bundle,
2755 unsigned Index) {
2756 return wrap(unwrap(Bundle)->inputs()[Index]);
2759 /*--.. Operations on basic blocks ..........................................--*/
2761 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
2762 return wrap(static_cast<Value*>(unwrap(BB)));
2765 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
2766 return isa<BasicBlock>(unwrap(Val));
2769 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
2770 return wrap(unwrap<BasicBlock>(Val));
2773 const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) {
2774 return unwrap(BB)->getName().data();
2777 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
2778 return wrap(unwrap(BB)->getParent());
2781 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
2782 return wrap(unwrap(BB)->getTerminator());
2785 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
2786 return unwrap<Function>(FnRef)->size();
2789 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
2790 Function *Fn = unwrap<Function>(FnRef);
2791 for (BasicBlock &BB : *Fn)
2792 *BasicBlocksRefs++ = wrap(&BB);
2795 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
2796 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2799 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
2800 Function *Func = unwrap<Function>(Fn);
2801 Function::iterator I = Func->begin();
2802 if (I == Func->end())
2803 return nullptr;
2804 return wrap(&*I);
2807 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
2808 Function *Func = unwrap<Function>(Fn);
2809 Function::iterator I = Func->end();
2810 if (I == Func->begin())
2811 return nullptr;
2812 return wrap(&*--I);
2815 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
2816 BasicBlock *Block = unwrap(BB);
2817 Function::iterator I(Block);
2818 if (++I == Block->getParent()->end())
2819 return nullptr;
2820 return wrap(&*I);
2823 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
2824 BasicBlock *Block = unwrap(BB);
2825 Function::iterator I(Block);
2826 if (I == Block->getParent()->begin())
2827 return nullptr;
2828 return wrap(&*--I);
2831 LLVMBasicBlockRef LLVMCreateBasicBlockInContext(LLVMContextRef C,
2832 const char *Name) {
2833 return wrap(llvm::BasicBlock::Create(*unwrap(C), Name));
2836 void LLVMInsertExistingBasicBlockAfterInsertBlock(LLVMBuilderRef Builder,
2837 LLVMBasicBlockRef BB) {
2838 BasicBlock *ToInsert = unwrap(BB);
2839 BasicBlock *CurBB = unwrap(Builder)->GetInsertBlock();
2840 assert(CurBB && "current insertion point is invalid!");
2841 CurBB->getParent()->insert(std::next(CurBB->getIterator()), ToInsert);
2844 void LLVMAppendExistingBasicBlock(LLVMValueRef Fn,
2845 LLVMBasicBlockRef BB) {
2846 unwrap<Function>(Fn)->insert(unwrap<Function>(Fn)->end(), unwrap(BB));
2849 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
2850 LLVMValueRef FnRef,
2851 const char *Name) {
2852 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
2855 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
2856 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
2859 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
2860 LLVMBasicBlockRef BBRef,
2861 const char *Name) {
2862 BasicBlock *BB = unwrap(BBRef);
2863 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2866 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
2867 const char *Name) {
2868 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
2871 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
2872 unwrap(BBRef)->eraseFromParent();
2875 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
2876 unwrap(BBRef)->removeFromParent();
2879 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2880 unwrap(BB)->moveBefore(unwrap(MovePos));
2883 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2884 unwrap(BB)->moveAfter(unwrap(MovePos));
2887 /*--.. Operations on instructions ..........................................--*/
2889 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
2890 return wrap(unwrap<Instruction>(Inst)->getParent());
2893 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
2894 BasicBlock *Block = unwrap(BB);
2895 BasicBlock::iterator I = Block->begin();
2896 if (I == Block->end())
2897 return nullptr;
2898 return wrap(&*I);
2901 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
2902 BasicBlock *Block = unwrap(BB);
2903 BasicBlock::iterator I = Block->end();
2904 if (I == Block->begin())
2905 return nullptr;
2906 return wrap(&*--I);
2909 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
2910 Instruction *Instr = unwrap<Instruction>(Inst);
2911 BasicBlock::iterator I(Instr);
2912 if (++I == Instr->getParent()->end())
2913 return nullptr;
2914 return wrap(&*I);
2917 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
2918 Instruction *Instr = unwrap<Instruction>(Inst);
2919 BasicBlock::iterator I(Instr);
2920 if (I == Instr->getParent()->begin())
2921 return nullptr;
2922 return wrap(&*--I);
2925 void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) {
2926 unwrap<Instruction>(Inst)->removeFromParent();
2929 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
2930 unwrap<Instruction>(Inst)->eraseFromParent();
2933 void LLVMDeleteInstruction(LLVMValueRef Inst) {
2934 unwrap<Instruction>(Inst)->deleteValue();
2937 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
2938 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2939 return (LLVMIntPredicate)I->getPredicate();
2940 return (LLVMIntPredicate)0;
2943 LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
2944 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2945 return (LLVMRealPredicate)I->getPredicate();
2946 return (LLVMRealPredicate)0;
2949 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
2950 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2951 return map_to_llvmopcode(C->getOpcode());
2952 return (LLVMOpcode)0;
2955 LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {
2956 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2957 return wrap(C->clone());
2958 return nullptr;
2961 LLVMValueRef LLVMIsATerminatorInst(LLVMValueRef Inst) {
2962 Instruction *I = dyn_cast<Instruction>(unwrap(Inst));
2963 return (I && I->isTerminator()) ? wrap(I) : nullptr;
2966 unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) {
2967 if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) {
2968 return FPI->arg_size();
2970 return unwrap<CallBase>(Instr)->arg_size();
2973 /*--.. Call and invoke instructions ........................................--*/
2975 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
2976 return unwrap<CallBase>(Instr)->getCallingConv();
2979 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
2980 return unwrap<CallBase>(Instr)->setCallingConv(
2981 static_cast<CallingConv::ID>(CC));
2984 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, LLVMAttributeIndex Idx,
2985 unsigned align) {
2986 auto *Call = unwrap<CallBase>(Instr);
2987 Attribute AlignAttr =
2988 Attribute::getWithAlignment(Call->getContext(), Align(align));
2989 Call->addAttributeAtIndex(Idx, AlignAttr);
2992 void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2993 LLVMAttributeRef A) {
2994 unwrap<CallBase>(C)->addAttributeAtIndex(Idx, unwrap(A));
2997 unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C,
2998 LLVMAttributeIndex Idx) {
2999 auto *Call = unwrap<CallBase>(C);
3000 auto AS = Call->getAttributes().getAttributes(Idx);
3001 return AS.getNumAttributes();
3004 void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx,
3005 LLVMAttributeRef *Attrs) {
3006 auto *Call = unwrap<CallBase>(C);
3007 auto AS = Call->getAttributes().getAttributes(Idx);
3008 for (auto A : AS)
3009 *Attrs++ = wrap(A);
3012 LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C,
3013 LLVMAttributeIndex Idx,
3014 unsigned KindID) {
3015 return wrap(unwrap<CallBase>(C)->getAttributeAtIndex(
3016 Idx, (Attribute::AttrKind)KindID));
3019 LLVMAttributeRef LLVMGetCallSiteStringAttribute(LLVMValueRef C,
3020 LLVMAttributeIndex Idx,
3021 const char *K, unsigned KLen) {
3022 return wrap(
3023 unwrap<CallBase>(C)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
3026 void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
3027 unsigned KindID) {
3028 unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
3031 void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
3032 const char *K, unsigned KLen) {
3033 unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
3036 LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) {
3037 return wrap(unwrap<CallBase>(Instr)->getCalledOperand());
3040 LLVMTypeRef LLVMGetCalledFunctionType(LLVMValueRef Instr) {
3041 return wrap(unwrap<CallBase>(Instr)->getFunctionType());
3044 unsigned LLVMGetNumOperandBundles(LLVMValueRef C) {
3045 return unwrap<CallBase>(C)->getNumOperandBundles();
3048 LLVMOperandBundleRef LLVMGetOperandBundleAtIndex(LLVMValueRef C,
3049 unsigned Index) {
3050 return wrap(
3051 new OperandBundleDef(unwrap<CallBase>(C)->getOperandBundleAt(Index)));
3054 /*--.. Operations on call instructions (only) ..............................--*/
3056 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
3057 return unwrap<CallInst>(Call)->isTailCall();
3060 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
3061 unwrap<CallInst>(Call)->setTailCall(isTailCall);
3064 LLVMTailCallKind LLVMGetTailCallKind(LLVMValueRef Call) {
3065 return (LLVMTailCallKind)unwrap<CallInst>(Call)->getTailCallKind();
3068 void LLVMSetTailCallKind(LLVMValueRef Call, LLVMTailCallKind kind) {
3069 unwrap<CallInst>(Call)->setTailCallKind((CallInst::TailCallKind)kind);
3072 /*--.. Operations on invoke instructions (only) ............................--*/
3074 LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) {
3075 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
3078 LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) {
3079 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
3080 return wrap(CRI->getUnwindDest());
3081 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
3082 return wrap(CSI->getUnwindDest());
3084 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
3087 void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
3088 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
3091 void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
3092 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
3093 return CRI->setUnwindDest(unwrap(B));
3094 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
3095 return CSI->setUnwindDest(unwrap(B));
3097 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
3100 LLVMBasicBlockRef LLVMGetCallBrDefaultDest(LLVMValueRef CallBr) {
3101 return wrap(unwrap<CallBrInst>(CallBr)->getDefaultDest());
3104 unsigned LLVMGetCallBrNumIndirectDests(LLVMValueRef CallBr) {
3105 return unwrap<CallBrInst>(CallBr)->getNumIndirectDests();
3108 LLVMBasicBlockRef LLVMGetCallBrIndirectDest(LLVMValueRef CallBr, unsigned Idx) {
3109 return wrap(unwrap<CallBrInst>(CallBr)->getIndirectDest(Idx));
3112 /*--.. Operations on terminators ...........................................--*/
3114 unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {
3115 return unwrap<Instruction>(Term)->getNumSuccessors();
3118 LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) {
3119 return wrap(unwrap<Instruction>(Term)->getSuccessor(i));
3122 void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
3123 return unwrap<Instruction>(Term)->setSuccessor(i, unwrap(block));
3126 /*--.. Operations on branch instructions (only) ............................--*/
3128 LLVMBool LLVMIsConditional(LLVMValueRef Branch) {
3129 return unwrap<BranchInst>(Branch)->isConditional();
3132 LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) {
3133 return wrap(unwrap<BranchInst>(Branch)->getCondition());
3136 void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) {
3137 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
3140 /*--.. Operations on switch instructions (only) ............................--*/
3142 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
3143 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
3146 /*--.. Operations on alloca instructions (only) ............................--*/
3148 LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) {
3149 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
3152 /*--.. Operations on gep instructions (only) ...............................--*/
3154 LLVMBool LLVMIsInBounds(LLVMValueRef GEP) {
3155 return unwrap<GEPOperator>(GEP)->isInBounds();
3158 void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) {
3159 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
3162 LLVMTypeRef LLVMGetGEPSourceElementType(LLVMValueRef GEP) {
3163 return wrap(unwrap<GEPOperator>(GEP)->getSourceElementType());
3166 LLVMGEPNoWrapFlags LLVMGEPGetNoWrapFlags(LLVMValueRef GEP) {
3167 GEPOperator *GEPOp = unwrap<GEPOperator>(GEP);
3168 return mapToLLVMGEPNoWrapFlags(GEPOp->getNoWrapFlags());
3171 void LLVMGEPSetNoWrapFlags(LLVMValueRef GEP, LLVMGEPNoWrapFlags NoWrapFlags) {
3172 GetElementPtrInst *GEPInst = unwrap<GetElementPtrInst>(GEP);
3173 GEPInst->setNoWrapFlags(mapFromLLVMGEPNoWrapFlags(NoWrapFlags));
3176 /*--.. Operations on phi nodes .............................................--*/
3178 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
3179 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
3180 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
3181 for (unsigned I = 0; I != Count; ++I)
3182 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
3185 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
3186 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
3189 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
3190 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
3193 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
3194 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
3197 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/
3199 unsigned LLVMGetNumIndices(LLVMValueRef Inst) {
3200 auto *I = unwrap(Inst);
3201 if (auto *GEP = dyn_cast<GEPOperator>(I))
3202 return GEP->getNumIndices();
3203 if (auto *EV = dyn_cast<ExtractValueInst>(I))
3204 return EV->getNumIndices();
3205 if (auto *IV = dyn_cast<InsertValueInst>(I))
3206 return IV->getNumIndices();
3207 llvm_unreachable(
3208 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
3211 const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
3212 auto *I = unwrap(Inst);
3213 if (auto *EV = dyn_cast<ExtractValueInst>(I))
3214 return EV->getIndices().data();
3215 if (auto *IV = dyn_cast<InsertValueInst>(I))
3216 return IV->getIndices().data();
3217 llvm_unreachable(
3218 "LLVMGetIndices applies only to extractvalue and insertvalue!");
3222 /*===-- Instruction builders ----------------------------------------------===*/
3224 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
3225 return wrap(new IRBuilder<>(*unwrap(C)));
3228 LLVMBuilderRef LLVMCreateBuilder(void) {
3229 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
3232 static void LLVMPositionBuilderImpl(IRBuilder<> *Builder, BasicBlock *Block,
3233 Instruction *Instr, bool BeforeDbgRecords) {
3234 BasicBlock::iterator I = Instr ? Instr->getIterator() : Block->end();
3235 I.setHeadBit(BeforeDbgRecords);
3236 Builder->SetInsertPoint(Block, I);
3239 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
3240 LLVMValueRef Instr) {
3241 return LLVMPositionBuilderImpl(unwrap(Builder), unwrap(Block),
3242 unwrap<Instruction>(Instr), false);
3245 void LLVMPositionBuilderBeforeDbgRecords(LLVMBuilderRef Builder,
3246 LLVMBasicBlockRef Block,
3247 LLVMValueRef Instr) {
3248 return LLVMPositionBuilderImpl(unwrap(Builder), unwrap(Block),
3249 unwrap<Instruction>(Instr), true);
3252 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
3253 Instruction *I = unwrap<Instruction>(Instr);
3254 return LLVMPositionBuilderImpl(unwrap(Builder), I->getParent(), I, false);
3257 void LLVMPositionBuilderBeforeInstrAndDbgRecords(LLVMBuilderRef Builder,
3258 LLVMValueRef Instr) {
3259 Instruction *I = unwrap<Instruction>(Instr);
3260 return LLVMPositionBuilderImpl(unwrap(Builder), I->getParent(), I, true);
3263 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
3264 BasicBlock *BB = unwrap(Block);
3265 unwrap(Builder)->SetInsertPoint(BB);
3268 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
3269 return wrap(unwrap(Builder)->GetInsertBlock());
3272 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
3273 unwrap(Builder)->ClearInsertionPoint();
3276 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
3277 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
3280 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
3281 const char *Name) {
3282 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
3285 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
3286 delete unwrap(Builder);
3289 /*--.. Metadata builders ...................................................--*/
3291 LLVMMetadataRef LLVMGetCurrentDebugLocation2(LLVMBuilderRef Builder) {
3292 return wrap(unwrap(Builder)->getCurrentDebugLocation().getAsMDNode());
3295 void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Builder, LLVMMetadataRef Loc) {
3296 if (Loc)
3297 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(unwrap<MDNode>(Loc)));
3298 else
3299 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc());
3302 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
3303 MDNode *Loc =
3304 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
3305 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
3308 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
3309 LLVMContext &Context = unwrap(Builder)->getContext();
3310 return wrap(MetadataAsValue::get(
3311 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
3314 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
3315 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
3318 void LLVMAddMetadataToInst(LLVMBuilderRef Builder, LLVMValueRef Inst) {
3319 unwrap(Builder)->AddMetadataToInst(unwrap<Instruction>(Inst));
3322 void LLVMBuilderSetDefaultFPMathTag(LLVMBuilderRef Builder,
3323 LLVMMetadataRef FPMathTag) {
3325 unwrap(Builder)->setDefaultFPMathTag(FPMathTag
3326 ? unwrap<MDNode>(FPMathTag)
3327 : nullptr);
3330 LLVMMetadataRef LLVMBuilderGetDefaultFPMathTag(LLVMBuilderRef Builder) {
3331 return wrap(unwrap(Builder)->getDefaultFPMathTag());
3334 /*--.. Instruction builders ................................................--*/
3336 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
3337 return wrap(unwrap(B)->CreateRetVoid());
3340 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
3341 return wrap(unwrap(B)->CreateRet(unwrap(V)));
3344 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
3345 unsigned N) {
3346 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
3349 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
3350 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
3353 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
3354 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
3355 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
3358 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
3359 LLVMBasicBlockRef Else, unsigned NumCases) {
3360 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
3363 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
3364 unsigned NumDests) {
3365 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
3368 LLVMValueRef LLVMBuildCallBr(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn,
3369 LLVMBasicBlockRef DefaultDest,
3370 LLVMBasicBlockRef *IndirectDests,
3371 unsigned NumIndirectDests, LLVMValueRef *Args,
3372 unsigned NumArgs, LLVMOperandBundleRef *Bundles,
3373 unsigned NumBundles, const char *Name) {
3375 SmallVector<OperandBundleDef, 8> OBs;
3376 for (auto *Bundle : ArrayRef(Bundles, NumBundles)) {
3377 OperandBundleDef *OB = unwrap(Bundle);
3378 OBs.push_back(*OB);
3381 return wrap(unwrap(B)->CreateCallBr(
3382 unwrap<FunctionType>(Ty), unwrap(Fn), unwrap(DefaultDest),
3383 ArrayRef(unwrap(IndirectDests), NumIndirectDests),
3384 ArrayRef<Value *>(unwrap(Args), NumArgs), OBs, Name));
3387 LLVMValueRef LLVMBuildInvoke2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn,
3388 LLVMValueRef *Args, unsigned NumArgs,
3389 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
3390 const char *Name) {
3391 return wrap(unwrap(B)->CreateInvoke(unwrap<FunctionType>(Ty), unwrap(Fn),
3392 unwrap(Then), unwrap(Catch),
3393 ArrayRef(unwrap(Args), NumArgs), Name));
3396 LLVMValueRef LLVMBuildInvokeWithOperandBundles(
3397 LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn, LLVMValueRef *Args,
3398 unsigned NumArgs, LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
3399 LLVMOperandBundleRef *Bundles, unsigned NumBundles, const char *Name) {
3400 SmallVector<OperandBundleDef, 8> OBs;
3401 for (auto *Bundle : ArrayRef(Bundles, NumBundles)) {
3402 OperandBundleDef *OB = unwrap(Bundle);
3403 OBs.push_back(*OB);
3405 return wrap(unwrap(B)->CreateInvoke(
3406 unwrap<FunctionType>(Ty), unwrap(Fn), unwrap(Then), unwrap(Catch),
3407 ArrayRef(unwrap(Args), NumArgs), OBs, Name));
3410 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
3411 LLVMValueRef PersFn, unsigned NumClauses,
3412 const char *Name) {
3413 // The personality used to live on the landingpad instruction, but now it
3414 // lives on the parent function. For compatibility, take the provided
3415 // personality and put it on the parent function.
3416 if (PersFn)
3417 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
3418 unwrap<Function>(PersFn));
3419 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
3422 LLVMValueRef LLVMBuildCatchPad(LLVMBuilderRef B, LLVMValueRef ParentPad,
3423 LLVMValueRef *Args, unsigned NumArgs,
3424 const char *Name) {
3425 return wrap(unwrap(B)->CreateCatchPad(unwrap(ParentPad),
3426 ArrayRef(unwrap(Args), NumArgs), Name));
3429 LLVMValueRef LLVMBuildCleanupPad(LLVMBuilderRef B, LLVMValueRef ParentPad,
3430 LLVMValueRef *Args, unsigned NumArgs,
3431 const char *Name) {
3432 if (ParentPad == nullptr) {
3433 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3434 ParentPad = wrap(Constant::getNullValue(Ty));
3436 return wrap(unwrap(B)->CreateCleanupPad(
3437 unwrap(ParentPad), ArrayRef(unwrap(Args), NumArgs), Name));
3440 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
3441 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
3444 LLVMValueRef LLVMBuildCatchSwitch(LLVMBuilderRef B, LLVMValueRef ParentPad,
3445 LLVMBasicBlockRef UnwindBB,
3446 unsigned NumHandlers, const char *Name) {
3447 if (ParentPad == nullptr) {
3448 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3449 ParentPad = wrap(Constant::getNullValue(Ty));
3451 return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(UnwindBB),
3452 NumHandlers, Name));
3455 LLVMValueRef LLVMBuildCatchRet(LLVMBuilderRef B, LLVMValueRef CatchPad,
3456 LLVMBasicBlockRef BB) {
3457 return wrap(unwrap(B)->CreateCatchRet(unwrap<CatchPadInst>(CatchPad),
3458 unwrap(BB)));
3461 LLVMValueRef LLVMBuildCleanupRet(LLVMBuilderRef B, LLVMValueRef CatchPad,
3462 LLVMBasicBlockRef BB) {
3463 return wrap(unwrap(B)->CreateCleanupRet(unwrap<CleanupPadInst>(CatchPad),
3464 unwrap(BB)));
3467 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
3468 return wrap(unwrap(B)->CreateUnreachable());
3471 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
3472 LLVMBasicBlockRef Dest) {
3473 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
3476 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
3477 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
3480 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
3481 return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
3484 LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
3485 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
3488 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
3489 unwrap<LandingPadInst>(LandingPad)->addClause(unwrap<Constant>(ClauseVal));
3492 LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) {
3493 return unwrap<LandingPadInst>(LandingPad)->isCleanup();
3496 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
3497 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
3500 void LLVMAddHandler(LLVMValueRef CatchSwitch, LLVMBasicBlockRef Dest) {
3501 unwrap<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Dest));
3504 unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) {
3505 return unwrap<CatchSwitchInst>(CatchSwitch)->getNumHandlers();
3508 void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) {
3509 CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(CatchSwitch);
3510 for (const BasicBlock *H : CSI->handlers())
3511 *Handlers++ = wrap(H);
3514 LLVMValueRef LLVMGetParentCatchSwitch(LLVMValueRef CatchPad) {
3515 return wrap(unwrap<CatchPadInst>(CatchPad)->getCatchSwitch());
3518 void LLVMSetParentCatchSwitch(LLVMValueRef CatchPad, LLVMValueRef CatchSwitch) {
3519 unwrap<CatchPadInst>(CatchPad)
3520 ->setCatchSwitch(unwrap<CatchSwitchInst>(CatchSwitch));
3523 /*--.. Funclets ...........................................................--*/
3525 LLVMValueRef LLVMGetArgOperand(LLVMValueRef Funclet, unsigned i) {
3526 return wrap(unwrap<FuncletPadInst>(Funclet)->getArgOperand(i));
3529 void LLVMSetArgOperand(LLVMValueRef Funclet, unsigned i, LLVMValueRef value) {
3530 unwrap<FuncletPadInst>(Funclet)->setArgOperand(i, unwrap(value));
3533 /*--.. Arithmetic ..........................................................--*/
3535 static FastMathFlags mapFromLLVMFastMathFlags(LLVMFastMathFlags FMF) {
3536 FastMathFlags NewFMF;
3537 NewFMF.setAllowReassoc((FMF & LLVMFastMathAllowReassoc) != 0);
3538 NewFMF.setNoNaNs((FMF & LLVMFastMathNoNaNs) != 0);
3539 NewFMF.setNoInfs((FMF & LLVMFastMathNoInfs) != 0);
3540 NewFMF.setNoSignedZeros((FMF & LLVMFastMathNoSignedZeros) != 0);
3541 NewFMF.setAllowReciprocal((FMF & LLVMFastMathAllowReciprocal) != 0);
3542 NewFMF.setAllowContract((FMF & LLVMFastMathAllowContract) != 0);
3543 NewFMF.setApproxFunc((FMF & LLVMFastMathApproxFunc) != 0);
3545 return NewFMF;
3548 static LLVMFastMathFlags mapToLLVMFastMathFlags(FastMathFlags FMF) {
3549 LLVMFastMathFlags NewFMF = LLVMFastMathNone;
3550 if (FMF.allowReassoc())
3551 NewFMF |= LLVMFastMathAllowReassoc;
3552 if (FMF.noNaNs())
3553 NewFMF |= LLVMFastMathNoNaNs;
3554 if (FMF.noInfs())
3555 NewFMF |= LLVMFastMathNoInfs;
3556 if (FMF.noSignedZeros())
3557 NewFMF |= LLVMFastMathNoSignedZeros;
3558 if (FMF.allowReciprocal())
3559 NewFMF |= LLVMFastMathAllowReciprocal;
3560 if (FMF.allowContract())
3561 NewFMF |= LLVMFastMathAllowContract;
3562 if (FMF.approxFunc())
3563 NewFMF |= LLVMFastMathApproxFunc;
3565 return NewFMF;
3568 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3569 const char *Name) {
3570 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
3573 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3574 const char *Name) {
3575 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
3578 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3579 const char *Name) {
3580 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
3583 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3584 const char *Name) {
3585 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
3588 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3589 const char *Name) {
3590 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
3593 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3594 const char *Name) {
3595 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
3598 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3599 const char *Name) {
3600 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
3603 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3604 const char *Name) {
3605 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
3608 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3609 const char *Name) {
3610 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
3613 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3614 const char *Name) {
3615 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
3618 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3619 const char *Name) {
3620 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
3623 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3624 const char *Name) {
3625 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
3628 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3629 const char *Name) {
3630 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
3633 LLVMValueRef LLVMBuildExactUDiv(LLVMBuilderRef B, LLVMValueRef LHS,
3634 LLVMValueRef RHS, const char *Name) {
3635 return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
3638 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3639 const char *Name) {
3640 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
3643 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
3644 LLVMValueRef RHS, const char *Name) {
3645 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
3648 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3649 const char *Name) {
3650 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
3653 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3654 const char *Name) {
3655 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
3658 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3659 const char *Name) {
3660 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
3663 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3664 const char *Name) {
3665 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
3668 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3669 const char *Name) {
3670 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
3673 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3674 const char *Name) {
3675 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
3678 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3679 const char *Name) {
3680 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
3683 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3684 const char *Name) {
3685 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
3688 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3689 const char *Name) {
3690 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
3693 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3694 const char *Name) {
3695 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
3698 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
3699 LLVMValueRef LHS, LLVMValueRef RHS,
3700 const char *Name) {
3701 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
3702 unwrap(RHS), Name));
3705 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
3706 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
3709 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
3710 const char *Name) {
3711 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
3714 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
3715 const char *Name) {
3716 Value *Neg = unwrap(B)->CreateNeg(unwrap(V), Name);
3717 if (auto *I = dyn_cast<BinaryOperator>(Neg))
3718 I->setHasNoUnsignedWrap();
3719 return wrap(Neg);
3722 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
3723 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
3726 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
3727 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
3730 LLVMBool LLVMGetNUW(LLVMValueRef ArithInst) {
3731 Value *P = unwrap<Value>(ArithInst);
3732 return cast<Instruction>(P)->hasNoUnsignedWrap();
3735 void LLVMSetNUW(LLVMValueRef ArithInst, LLVMBool HasNUW) {
3736 Value *P = unwrap<Value>(ArithInst);
3737 cast<Instruction>(P)->setHasNoUnsignedWrap(HasNUW);
3740 LLVMBool LLVMGetNSW(LLVMValueRef ArithInst) {
3741 Value *P = unwrap<Value>(ArithInst);
3742 return cast<Instruction>(P)->hasNoSignedWrap();
3745 void LLVMSetNSW(LLVMValueRef ArithInst, LLVMBool HasNSW) {
3746 Value *P = unwrap<Value>(ArithInst);
3747 cast<Instruction>(P)->setHasNoSignedWrap(HasNSW);
3750 LLVMBool LLVMGetExact(LLVMValueRef DivOrShrInst) {
3751 Value *P = unwrap<Value>(DivOrShrInst);
3752 return cast<Instruction>(P)->isExact();
3755 void LLVMSetExact(LLVMValueRef DivOrShrInst, LLVMBool IsExact) {
3756 Value *P = unwrap<Value>(DivOrShrInst);
3757 cast<Instruction>(P)->setIsExact(IsExact);
3760 LLVMBool LLVMGetNNeg(LLVMValueRef NonNegInst) {
3761 Value *P = unwrap<Value>(NonNegInst);
3762 return cast<Instruction>(P)->hasNonNeg();
3765 void LLVMSetNNeg(LLVMValueRef NonNegInst, LLVMBool IsNonNeg) {
3766 Value *P = unwrap<Value>(NonNegInst);
3767 cast<Instruction>(P)->setNonNeg(IsNonNeg);
3770 LLVMFastMathFlags LLVMGetFastMathFlags(LLVMValueRef FPMathInst) {
3771 Value *P = unwrap<Value>(FPMathInst);
3772 FastMathFlags FMF = cast<Instruction>(P)->getFastMathFlags();
3773 return mapToLLVMFastMathFlags(FMF);
3776 void LLVMSetFastMathFlags(LLVMValueRef FPMathInst, LLVMFastMathFlags FMF) {
3777 Value *P = unwrap<Value>(FPMathInst);
3778 cast<Instruction>(P)->setFastMathFlags(mapFromLLVMFastMathFlags(FMF));
3781 LLVMBool LLVMCanValueUseFastMathFlags(LLVMValueRef V) {
3782 Value *Val = unwrap<Value>(V);
3783 return isa<FPMathOperator>(Val);
3786 LLVMBool LLVMGetIsDisjoint(LLVMValueRef Inst) {
3787 Value *P = unwrap<Value>(Inst);
3788 return cast<PossiblyDisjointInst>(P)->isDisjoint();
3791 void LLVMSetIsDisjoint(LLVMValueRef Inst, LLVMBool IsDisjoint) {
3792 Value *P = unwrap<Value>(Inst);
3793 cast<PossiblyDisjointInst>(P)->setIsDisjoint(IsDisjoint);
3796 /*--.. Memory ..............................................................--*/
3798 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
3799 const char *Name) {
3800 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3801 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3802 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3803 return wrap(unwrap(B)->CreateMalloc(ITy, unwrap(Ty), AllocSize, nullptr,
3804 nullptr, Name));
3807 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
3808 LLVMValueRef Val, const char *Name) {
3809 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3810 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3811 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3812 return wrap(unwrap(B)->CreateMalloc(ITy, unwrap(Ty), AllocSize, unwrap(Val),
3813 nullptr, Name));
3816 LLVMValueRef LLVMBuildMemSet(LLVMBuilderRef B, LLVMValueRef Ptr,
3817 LLVMValueRef Val, LLVMValueRef Len,
3818 unsigned Align) {
3819 return wrap(unwrap(B)->CreateMemSet(unwrap(Ptr), unwrap(Val), unwrap(Len),
3820 MaybeAlign(Align)));
3823 LLVMValueRef LLVMBuildMemCpy(LLVMBuilderRef B,
3824 LLVMValueRef Dst, unsigned DstAlign,
3825 LLVMValueRef Src, unsigned SrcAlign,
3826 LLVMValueRef Size) {
3827 return wrap(unwrap(B)->CreateMemCpy(unwrap(Dst), MaybeAlign(DstAlign),
3828 unwrap(Src), MaybeAlign(SrcAlign),
3829 unwrap(Size)));
3832 LLVMValueRef LLVMBuildMemMove(LLVMBuilderRef B,
3833 LLVMValueRef Dst, unsigned DstAlign,
3834 LLVMValueRef Src, unsigned SrcAlign,
3835 LLVMValueRef Size) {
3836 return wrap(unwrap(B)->CreateMemMove(unwrap(Dst), MaybeAlign(DstAlign),
3837 unwrap(Src), MaybeAlign(SrcAlign),
3838 unwrap(Size)));
3841 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
3842 const char *Name) {
3843 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
3846 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
3847 LLVMValueRef Val, const char *Name) {
3848 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
3851 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
3852 return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
3855 LLVMValueRef LLVMBuildLoad2(LLVMBuilderRef B, LLVMTypeRef Ty,
3856 LLVMValueRef PointerVal, const char *Name) {
3857 return wrap(unwrap(B)->CreateLoad(unwrap(Ty), unwrap(PointerVal), Name));
3860 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
3861 LLVMValueRef PointerVal) {
3862 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
3865 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
3866 switch (Ordering) {
3867 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic;
3868 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered;
3869 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic;
3870 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire;
3871 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release;
3872 case LLVMAtomicOrderingAcquireRelease:
3873 return AtomicOrdering::AcquireRelease;
3874 case LLVMAtomicOrderingSequentiallyConsistent:
3875 return AtomicOrdering::SequentiallyConsistent;
3878 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
3881 static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) {
3882 switch (Ordering) {
3883 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic;
3884 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered;
3885 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic;
3886 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire;
3887 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease;
3888 case AtomicOrdering::AcquireRelease:
3889 return LLVMAtomicOrderingAcquireRelease;
3890 case AtomicOrdering::SequentiallyConsistent:
3891 return LLVMAtomicOrderingSequentiallyConsistent;
3894 llvm_unreachable("Invalid AtomicOrdering value!");
3897 static AtomicRMWInst::BinOp mapFromLLVMRMWBinOp(LLVMAtomicRMWBinOp BinOp) {
3898 switch (BinOp) {
3899 case LLVMAtomicRMWBinOpXchg: return AtomicRMWInst::Xchg;
3900 case LLVMAtomicRMWBinOpAdd: return AtomicRMWInst::Add;
3901 case LLVMAtomicRMWBinOpSub: return AtomicRMWInst::Sub;
3902 case LLVMAtomicRMWBinOpAnd: return AtomicRMWInst::And;
3903 case LLVMAtomicRMWBinOpNand: return AtomicRMWInst::Nand;
3904 case LLVMAtomicRMWBinOpOr: return AtomicRMWInst::Or;
3905 case LLVMAtomicRMWBinOpXor: return AtomicRMWInst::Xor;
3906 case LLVMAtomicRMWBinOpMax: return AtomicRMWInst::Max;
3907 case LLVMAtomicRMWBinOpMin: return AtomicRMWInst::Min;
3908 case LLVMAtomicRMWBinOpUMax: return AtomicRMWInst::UMax;
3909 case LLVMAtomicRMWBinOpUMin: return AtomicRMWInst::UMin;
3910 case LLVMAtomicRMWBinOpFAdd: return AtomicRMWInst::FAdd;
3911 case LLVMAtomicRMWBinOpFSub: return AtomicRMWInst::FSub;
3912 case LLVMAtomicRMWBinOpFMax: return AtomicRMWInst::FMax;
3913 case LLVMAtomicRMWBinOpFMin: return AtomicRMWInst::FMin;
3914 case LLVMAtomicRMWBinOpUIncWrap:
3915 return AtomicRMWInst::UIncWrap;
3916 case LLVMAtomicRMWBinOpUDecWrap:
3917 return AtomicRMWInst::UDecWrap;
3920 llvm_unreachable("Invalid LLVMAtomicRMWBinOp value!");
3923 static LLVMAtomicRMWBinOp mapToLLVMRMWBinOp(AtomicRMWInst::BinOp BinOp) {
3924 switch (BinOp) {
3925 case AtomicRMWInst::Xchg: return LLVMAtomicRMWBinOpXchg;
3926 case AtomicRMWInst::Add: return LLVMAtomicRMWBinOpAdd;
3927 case AtomicRMWInst::Sub: return LLVMAtomicRMWBinOpSub;
3928 case AtomicRMWInst::And: return LLVMAtomicRMWBinOpAnd;
3929 case AtomicRMWInst::Nand: return LLVMAtomicRMWBinOpNand;
3930 case AtomicRMWInst::Or: return LLVMAtomicRMWBinOpOr;
3931 case AtomicRMWInst::Xor: return LLVMAtomicRMWBinOpXor;
3932 case AtomicRMWInst::Max: return LLVMAtomicRMWBinOpMax;
3933 case AtomicRMWInst::Min: return LLVMAtomicRMWBinOpMin;
3934 case AtomicRMWInst::UMax: return LLVMAtomicRMWBinOpUMax;
3935 case AtomicRMWInst::UMin: return LLVMAtomicRMWBinOpUMin;
3936 case AtomicRMWInst::FAdd: return LLVMAtomicRMWBinOpFAdd;
3937 case AtomicRMWInst::FSub: return LLVMAtomicRMWBinOpFSub;
3938 case AtomicRMWInst::FMax: return LLVMAtomicRMWBinOpFMax;
3939 case AtomicRMWInst::FMin: return LLVMAtomicRMWBinOpFMin;
3940 case AtomicRMWInst::UIncWrap:
3941 return LLVMAtomicRMWBinOpUIncWrap;
3942 case AtomicRMWInst::UDecWrap:
3943 return LLVMAtomicRMWBinOpUDecWrap;
3944 default: break;
3947 llvm_unreachable("Invalid AtomicRMWBinOp value!");
3950 // TODO: Should this and other atomic instructions support building with
3951 // "syncscope"?
3952 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
3953 LLVMBool isSingleThread, const char *Name) {
3954 return wrap(
3955 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
3956 isSingleThread ? SyncScope::SingleThread
3957 : SyncScope::System,
3958 Name));
3961 LLVMValueRef LLVMBuildGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,
3962 LLVMValueRef Pointer, LLVMValueRef *Indices,
3963 unsigned NumIndices, const char *Name) {
3964 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3965 return wrap(unwrap(B)->CreateGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3968 LLVMValueRef LLVMBuildInBoundsGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,
3969 LLVMValueRef Pointer, LLVMValueRef *Indices,
3970 unsigned NumIndices, const char *Name) {
3971 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3972 return wrap(
3973 unwrap(B)->CreateInBoundsGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3976 LLVMValueRef LLVMBuildGEPWithNoWrapFlags(LLVMBuilderRef B, LLVMTypeRef Ty,
3977 LLVMValueRef Pointer,
3978 LLVMValueRef *Indices,
3979 unsigned NumIndices, const char *Name,
3980 LLVMGEPNoWrapFlags NoWrapFlags) {
3981 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3982 return wrap(unwrap(B)->CreateGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name,
3983 mapFromLLVMGEPNoWrapFlags(NoWrapFlags)));
3986 LLVMValueRef LLVMBuildStructGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,
3987 LLVMValueRef Pointer, unsigned Idx,
3988 const char *Name) {
3989 return wrap(
3990 unwrap(B)->CreateStructGEP(unwrap(Ty), unwrap(Pointer), Idx, Name));
3993 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
3994 const char *Name) {
3995 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
3998 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
3999 const char *Name) {
4000 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
4003 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
4004 Value *P = unwrap(MemAccessInst);
4005 if (LoadInst *LI = dyn_cast<LoadInst>(P))
4006 return LI->isVolatile();
4007 if (StoreInst *SI = dyn_cast<StoreInst>(P))
4008 return SI->isVolatile();
4009 if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
4010 return AI->isVolatile();
4011 return cast<AtomicCmpXchgInst>(P)->isVolatile();
4014 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
4015 Value *P = unwrap(MemAccessInst);
4016 if (LoadInst *LI = dyn_cast<LoadInst>(P))
4017 return LI->setVolatile(isVolatile);
4018 if (StoreInst *SI = dyn_cast<StoreInst>(P))
4019 return SI->setVolatile(isVolatile);
4020 if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
4021 return AI->setVolatile(isVolatile);
4022 return cast<AtomicCmpXchgInst>(P)->setVolatile(isVolatile);
4025 LLVMBool LLVMGetWeak(LLVMValueRef CmpXchgInst) {
4026 return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->isWeak();
4029 void LLVMSetWeak(LLVMValueRef CmpXchgInst, LLVMBool isWeak) {
4030 return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->setWeak(isWeak);
4033 LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) {
4034 Value *P = unwrap(MemAccessInst);
4035 AtomicOrdering O;
4036 if (LoadInst *LI = dyn_cast<LoadInst>(P))
4037 O = LI->getOrdering();
4038 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
4039 O = SI->getOrdering();
4040 else if (FenceInst *FI = dyn_cast<FenceInst>(P))
4041 O = FI->getOrdering();
4042 else
4043 O = cast<AtomicRMWInst>(P)->getOrdering();
4044 return mapToLLVMOrdering(O);
4047 void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
4048 Value *P = unwrap(MemAccessInst);
4049 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
4051 if (LoadInst *LI = dyn_cast<LoadInst>(P))
4052 return LI->setOrdering(O);
4053 else if (FenceInst *FI = dyn_cast<FenceInst>(P))
4054 return FI->setOrdering(O);
4055 else if (AtomicRMWInst *ARWI = dyn_cast<AtomicRMWInst>(P))
4056 return ARWI->setOrdering(O);
4057 return cast<StoreInst>(P)->setOrdering(O);
4060 LLVMAtomicRMWBinOp LLVMGetAtomicRMWBinOp(LLVMValueRef Inst) {
4061 return mapToLLVMRMWBinOp(unwrap<AtomicRMWInst>(Inst)->getOperation());
4064 void LLVMSetAtomicRMWBinOp(LLVMValueRef Inst, LLVMAtomicRMWBinOp BinOp) {
4065 unwrap<AtomicRMWInst>(Inst)->setOperation(mapFromLLVMRMWBinOp(BinOp));
4068 /*--.. Casts ...............................................................--*/
4070 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
4071 LLVMTypeRef DestTy, const char *Name) {
4072 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
4075 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
4076 LLVMTypeRef DestTy, const char *Name) {
4077 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
4080 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
4081 LLVMTypeRef DestTy, const char *Name) {
4082 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
4085 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
4086 LLVMTypeRef DestTy, const char *Name) {
4087 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
4090 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
4091 LLVMTypeRef DestTy, const char *Name) {
4092 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
4095 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
4096 LLVMTypeRef DestTy, const char *Name) {
4097 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
4100 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
4101 LLVMTypeRef DestTy, const char *Name) {
4102 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
4105 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
4106 LLVMTypeRef DestTy, const char *Name) {
4107 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
4110 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
4111 LLVMTypeRef DestTy, const char *Name) {
4112 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
4115 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
4116 LLVMTypeRef DestTy, const char *Name) {
4117 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
4120 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
4121 LLVMTypeRef DestTy, const char *Name) {
4122 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
4125 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
4126 LLVMTypeRef DestTy, const char *Name) {
4127 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
4130 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
4131 LLVMTypeRef DestTy, const char *Name) {
4132 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
4135 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
4136 LLVMTypeRef DestTy, const char *Name) {
4137 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
4138 Name));
4141 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
4142 LLVMTypeRef DestTy, const char *Name) {
4143 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
4144 Name));
4147 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
4148 LLVMTypeRef DestTy, const char *Name) {
4149 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
4150 Name));
4153 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
4154 LLVMTypeRef DestTy, const char *Name) {
4155 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
4156 unwrap(DestTy), Name));
4159 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
4160 LLVMTypeRef DestTy, const char *Name) {
4161 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
4164 LLVMValueRef LLVMBuildIntCast2(LLVMBuilderRef B, LLVMValueRef Val,
4165 LLVMTypeRef DestTy, LLVMBool IsSigned,
4166 const char *Name) {
4167 return wrap(
4168 unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy), IsSigned, Name));
4171 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
4172 LLVMTypeRef DestTy, const char *Name) {
4173 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
4174 /*isSigned*/true, Name));
4177 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
4178 LLVMTypeRef DestTy, const char *Name) {
4179 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
4182 LLVMOpcode LLVMGetCastOpcode(LLVMValueRef Src, LLVMBool SrcIsSigned,
4183 LLVMTypeRef DestTy, LLVMBool DestIsSigned) {
4184 return map_to_llvmopcode(CastInst::getCastOpcode(
4185 unwrap(Src), SrcIsSigned, unwrap(DestTy), DestIsSigned));
4188 /*--.. Comparisons .........................................................--*/
4190 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
4191 LLVMValueRef LHS, LLVMValueRef RHS,
4192 const char *Name) {
4193 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
4194 unwrap(LHS), unwrap(RHS), Name));
4197 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
4198 LLVMValueRef LHS, LLVMValueRef RHS,
4199 const char *Name) {
4200 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
4201 unwrap(LHS), unwrap(RHS), Name));
4204 /*--.. Miscellaneous instructions ..........................................--*/
4206 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
4207 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
4210 LLVMValueRef LLVMBuildCall2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn,
4211 LLVMValueRef *Args, unsigned NumArgs,
4212 const char *Name) {
4213 FunctionType *FTy = unwrap<FunctionType>(Ty);
4214 return wrap(unwrap(B)->CreateCall(FTy, unwrap(Fn),
4215 ArrayRef(unwrap(Args), NumArgs), Name));
4218 LLVMValueRef
4219 LLVMBuildCallWithOperandBundles(LLVMBuilderRef B, LLVMTypeRef Ty,
4220 LLVMValueRef Fn, LLVMValueRef *Args,
4221 unsigned NumArgs, LLVMOperandBundleRef *Bundles,
4222 unsigned NumBundles, const char *Name) {
4223 FunctionType *FTy = unwrap<FunctionType>(Ty);
4224 SmallVector<OperandBundleDef, 8> OBs;
4225 for (auto *Bundle : ArrayRef(Bundles, NumBundles)) {
4226 OperandBundleDef *OB = unwrap(Bundle);
4227 OBs.push_back(*OB);
4229 return wrap(unwrap(B)->CreateCall(
4230 FTy, unwrap(Fn), ArrayRef(unwrap(Args), NumArgs), OBs, Name));
4233 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
4234 LLVMValueRef Then, LLVMValueRef Else,
4235 const char *Name) {
4236 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
4237 Name));
4240 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
4241 LLVMTypeRef Ty, const char *Name) {
4242 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
4245 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
4246 LLVMValueRef Index, const char *Name) {
4247 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
4248 Name));
4251 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
4252 LLVMValueRef EltVal, LLVMValueRef Index,
4253 const char *Name) {
4254 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
4255 unwrap(Index), Name));
4258 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
4259 LLVMValueRef V2, LLVMValueRef Mask,
4260 const char *Name) {
4261 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
4262 unwrap(Mask), Name));
4265 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
4266 unsigned Index, const char *Name) {
4267 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
4270 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
4271 LLVMValueRef EltVal, unsigned Index,
4272 const char *Name) {
4273 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
4274 Index, Name));
4277 LLVMValueRef LLVMBuildFreeze(LLVMBuilderRef B, LLVMValueRef Val,
4278 const char *Name) {
4279 return wrap(unwrap(B)->CreateFreeze(unwrap(Val), Name));
4282 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
4283 const char *Name) {
4284 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
4287 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
4288 const char *Name) {
4289 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
4292 LLVMValueRef LLVMBuildPtrDiff2(LLVMBuilderRef B, LLVMTypeRef ElemTy,
4293 LLVMValueRef LHS, LLVMValueRef RHS,
4294 const char *Name) {
4295 return wrap(unwrap(B)->CreatePtrDiff(unwrap(ElemTy), unwrap(LHS),
4296 unwrap(RHS), Name));
4299 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
4300 LLVMValueRef PTR, LLVMValueRef Val,
4301 LLVMAtomicOrdering ordering,
4302 LLVMBool singleThread) {
4303 AtomicRMWInst::BinOp intop = mapFromLLVMRMWBinOp(op);
4304 return wrap(unwrap(B)->CreateAtomicRMW(
4305 intop, unwrap(PTR), unwrap(Val), MaybeAlign(),
4306 mapFromLLVMOrdering(ordering),
4307 singleThread ? SyncScope::SingleThread : SyncScope::System));
4310 LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr,
4311 LLVMValueRef Cmp, LLVMValueRef New,
4312 LLVMAtomicOrdering SuccessOrdering,
4313 LLVMAtomicOrdering FailureOrdering,
4314 LLVMBool singleThread) {
4316 return wrap(unwrap(B)->CreateAtomicCmpXchg(
4317 unwrap(Ptr), unwrap(Cmp), unwrap(New), MaybeAlign(),
4318 mapFromLLVMOrdering(SuccessOrdering),
4319 mapFromLLVMOrdering(FailureOrdering),
4320 singleThread ? SyncScope::SingleThread : SyncScope::System));
4323 unsigned LLVMGetNumMaskElements(LLVMValueRef SVInst) {
4324 Value *P = unwrap(SVInst);
4325 ShuffleVectorInst *I = cast<ShuffleVectorInst>(P);
4326 return I->getShuffleMask().size();
4329 int LLVMGetMaskValue(LLVMValueRef SVInst, unsigned Elt) {
4330 Value *P = unwrap(SVInst);
4331 ShuffleVectorInst *I = cast<ShuffleVectorInst>(P);
4332 return I->getMaskValue(Elt);
4335 int LLVMGetUndefMaskElem(void) { return PoisonMaskElem; }
4337 LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) {
4338 Value *P = unwrap(AtomicInst);
4340 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
4341 return I->getSyncScopeID() == SyncScope::SingleThread;
4342 else if (FenceInst *FI = dyn_cast<FenceInst>(P))
4343 return FI->getSyncScopeID() == SyncScope::SingleThread;
4344 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
4345 return SI->getSyncScopeID() == SyncScope::SingleThread;
4346 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
4347 return LI->getSyncScopeID() == SyncScope::SingleThread;
4348 return cast<AtomicCmpXchgInst>(P)->getSyncScopeID() ==
4349 SyncScope::SingleThread;
4352 void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) {
4353 Value *P = unwrap(AtomicInst);
4354 SyncScope::ID SSID = NewValue ? SyncScope::SingleThread : SyncScope::System;
4356 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
4357 return I->setSyncScopeID(SSID);
4358 else if (FenceInst *FI = dyn_cast<FenceInst>(P))
4359 return FI->setSyncScopeID(SSID);
4360 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
4361 return SI->setSyncScopeID(SSID);
4362 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
4363 return LI->setSyncScopeID(SSID);
4364 return cast<AtomicCmpXchgInst>(P)->setSyncScopeID(SSID);
4367 LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) {
4368 Value *P = unwrap(CmpXchgInst);
4369 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering());
4372 void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst,
4373 LLVMAtomicOrdering Ordering) {
4374 Value *P = unwrap(CmpXchgInst);
4375 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
4377 return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O);
4380 LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst) {
4381 Value *P = unwrap(CmpXchgInst);
4382 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering());
4385 void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst,
4386 LLVMAtomicOrdering Ordering) {
4387 Value *P = unwrap(CmpXchgInst);
4388 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
4390 return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O);
4393 /*===-- Module providers --------------------------------------------------===*/
4395 LLVMModuleProviderRef
4396 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
4397 return reinterpret_cast<LLVMModuleProviderRef>(M);
4400 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
4401 delete unwrap(MP);
4405 /*===-- Memory buffers ----------------------------------------------------===*/
4407 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
4408 const char *Path,
4409 LLVMMemoryBufferRef *OutMemBuf,
4410 char **OutMessage) {
4412 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
4413 if (std::error_code EC = MBOrErr.getError()) {
4414 *OutMessage = strdup(EC.message().c_str());
4415 return 1;
4417 *OutMemBuf = wrap(MBOrErr.get().release());
4418 return 0;
4421 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
4422 char **OutMessage) {
4423 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
4424 if (std::error_code EC = MBOrErr.getError()) {
4425 *OutMessage = strdup(EC.message().c_str());
4426 return 1;
4428 *OutMemBuf = wrap(MBOrErr.get().release());
4429 return 0;
4432 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
4433 const char *InputData,
4434 size_t InputDataLength,
4435 const char *BufferName,
4436 LLVMBool RequiresNullTerminator) {
4438 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
4439 StringRef(BufferName),
4440 RequiresNullTerminator).release());
4443 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
4444 const char *InputData,
4445 size_t InputDataLength,
4446 const char *BufferName) {
4448 return wrap(
4449 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
4450 StringRef(BufferName)).release());
4453 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
4454 return unwrap(MemBuf)->getBufferStart();
4457 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
4458 return unwrap(MemBuf)->getBufferSize();
4461 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
4462 delete unwrap(MemBuf);
4465 /*===-- Pass Manager ------------------------------------------------------===*/
4467 LLVMPassManagerRef LLVMCreatePassManager() {
4468 return wrap(new legacy::PassManager());
4471 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
4472 return wrap(new legacy::FunctionPassManager(unwrap(M)));
4475 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
4476 return LLVMCreateFunctionPassManagerForModule(
4477 reinterpret_cast<LLVMModuleRef>(P));
4480 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
4481 return unwrap<legacy::PassManager>(PM)->run(*unwrap(M));
4484 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
4485 return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization();
4488 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
4489 return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
4492 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
4493 return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization();
4496 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
4497 delete unwrap(PM);
4500 /*===-- Threading ------------------------------------------------------===*/
4502 LLVMBool LLVMStartMultithreaded() {
4503 return LLVMIsMultithreaded();
4506 void LLVMStopMultithreaded() {
4509 LLVMBool LLVMIsMultithreaded() {
4510 return llvm_is_multithreaded();