[TableGen] Fix validateOperandClass for non Phyical Reg (#118146)
[llvm-project.git] / llvm / lib / IR / Core.cpp
blobdc5ca68bd9985ec568033517aa2aa8ea39f147f6
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-c/Types.h"
16 #include "llvm/IR/Attributes.h"
17 #include "llvm/IR/BasicBlock.h"
18 #include "llvm/IR/ConstantRange.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DebugInfoMetadata.h"
21 #include "llvm/IR/DebugProgramInstruction.h"
22 #include "llvm/IR/DerivedTypes.h"
23 #include "llvm/IR/DiagnosticInfo.h"
24 #include "llvm/IR/DiagnosticPrinter.h"
25 #include "llvm/IR/GlobalAlias.h"
26 #include "llvm/IR/GlobalVariable.h"
27 #include "llvm/IR/IRBuilder.h"
28 #include "llvm/IR/InlineAsm.h"
29 #include "llvm/IR/Instructions.h"
30 #include "llvm/IR/IntrinsicInst.h"
31 #include "llvm/IR/LLVMContext.h"
32 #include "llvm/IR/LegacyPassManager.h"
33 #include "llvm/IR/Module.h"
34 #include "llvm/InitializePasses.h"
35 #include "llvm/PassRegistry.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include "llvm/Support/FileSystem.h"
39 #include "llvm/Support/ManagedStatic.h"
40 #include "llvm/Support/MathExtras.h"
41 #include "llvm/Support/MemoryBuffer.h"
42 #include "llvm/Support/Threading.h"
43 #include "llvm/Support/raw_ostream.h"
44 #include <cassert>
45 #include <cstdlib>
46 #include <cstring>
47 #include <system_error>
49 using namespace llvm;
51 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(OperandBundleDef, LLVMOperandBundleRef)
53 inline BasicBlock **unwrap(LLVMBasicBlockRef *BBs) {
54 return reinterpret_cast<BasicBlock **>(BBs);
57 #define DEBUG_TYPE "ir"
59 void llvm::initializeCore(PassRegistry &Registry) {
60 initializeDominatorTreeWrapperPassPass(Registry);
61 initializePrintModulePassWrapperPass(Registry);
62 initializePrintFunctionPassWrapperPass(Registry);
63 initializeSafepointIRVerifierPass(Registry);
64 initializeVerifierLegacyPassPass(Registry);
67 void LLVMShutdown() {
68 llvm_shutdown();
71 /*===-- Version query -----------------------------------------------------===*/
73 void LLVMGetVersion(unsigned *Major, unsigned *Minor, unsigned *Patch) {
74 if (Major)
75 *Major = LLVM_VERSION_MAJOR;
76 if (Minor)
77 *Minor = LLVM_VERSION_MINOR;
78 if (Patch)
79 *Patch = LLVM_VERSION_PATCH;
82 /*===-- Error handling ----------------------------------------------------===*/
84 char *LLVMCreateMessage(const char *Message) {
85 return strdup(Message);
88 void LLVMDisposeMessage(char *Message) {
89 free(Message);
93 /*===-- Operations on contexts --------------------------------------------===*/
95 static LLVMContext &getGlobalContext() {
96 static LLVMContext GlobalContext;
97 return GlobalContext;
100 LLVMContextRef LLVMContextCreate() {
101 return wrap(new LLVMContext());
104 LLVMContextRef LLVMGetGlobalContext() { return wrap(&getGlobalContext()); }
106 void LLVMContextSetDiagnosticHandler(LLVMContextRef C,
107 LLVMDiagnosticHandler Handler,
108 void *DiagnosticContext) {
109 unwrap(C)->setDiagnosticHandlerCallBack(
110 LLVM_EXTENSION reinterpret_cast<DiagnosticHandler::DiagnosticHandlerTy>(
111 Handler),
112 DiagnosticContext);
115 LLVMDiagnosticHandler LLVMContextGetDiagnosticHandler(LLVMContextRef C) {
116 return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>(
117 unwrap(C)->getDiagnosticHandlerCallBack());
120 void *LLVMContextGetDiagnosticContext(LLVMContextRef C) {
121 return unwrap(C)->getDiagnosticContext();
124 void LLVMContextSetYieldCallback(LLVMContextRef C, LLVMYieldCallback Callback,
125 void *OpaqueHandle) {
126 auto YieldCallback =
127 LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
128 unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
131 LLVMBool LLVMContextShouldDiscardValueNames(LLVMContextRef C) {
132 return unwrap(C)->shouldDiscardValueNames();
135 void LLVMContextSetDiscardValueNames(LLVMContextRef C, LLVMBool Discard) {
136 unwrap(C)->setDiscardValueNames(Discard);
139 void LLVMContextDispose(LLVMContextRef C) {
140 delete unwrap(C);
143 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char *Name,
144 unsigned SLen) {
145 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
148 unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) {
149 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
152 unsigned LLVMGetSyncScopeID(LLVMContextRef C, const char *Name, size_t SLen) {
153 return unwrap(C)->getOrInsertSyncScopeID(StringRef(Name, SLen));
156 unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) {
157 return Attribute::getAttrKindFromName(StringRef(Name, SLen));
160 unsigned LLVMGetLastEnumAttributeKind(void) {
161 return Attribute::AttrKind::EndAttrKinds;
164 LLVMAttributeRef LLVMCreateEnumAttribute(LLVMContextRef C, unsigned KindID,
165 uint64_t Val) {
166 auto &Ctx = *unwrap(C);
167 auto AttrKind = (Attribute::AttrKind)KindID;
168 return wrap(Attribute::get(Ctx, AttrKind, Val));
171 unsigned LLVMGetEnumAttributeKind(LLVMAttributeRef A) {
172 return unwrap(A).getKindAsEnum();
175 uint64_t LLVMGetEnumAttributeValue(LLVMAttributeRef A) {
176 auto Attr = unwrap(A);
177 if (Attr.isEnumAttribute())
178 return 0;
179 return Attr.getValueAsInt();
182 LLVMAttributeRef LLVMCreateTypeAttribute(LLVMContextRef C, unsigned KindID,
183 LLVMTypeRef type_ref) {
184 auto &Ctx = *unwrap(C);
185 auto AttrKind = (Attribute::AttrKind)KindID;
186 return wrap(Attribute::get(Ctx, AttrKind, unwrap(type_ref)));
189 LLVMTypeRef LLVMGetTypeAttributeValue(LLVMAttributeRef A) {
190 auto Attr = unwrap(A);
191 return wrap(Attr.getValueAsType());
194 LLVMAttributeRef LLVMCreateConstantRangeAttribute(LLVMContextRef C,
195 unsigned KindID,
196 unsigned NumBits,
197 const uint64_t LowerWords[],
198 const uint64_t UpperWords[]) {
199 auto &Ctx = *unwrap(C);
200 auto AttrKind = (Attribute::AttrKind)KindID;
201 unsigned NumWords = divideCeil(NumBits, 64);
202 return wrap(Attribute::get(
203 Ctx, AttrKind,
204 ConstantRange(APInt(NumBits, ArrayRef(LowerWords, NumWords)),
205 APInt(NumBits, ArrayRef(UpperWords, NumWords)))));
208 LLVMAttributeRef LLVMCreateStringAttribute(LLVMContextRef C,
209 const char *K, unsigned KLength,
210 const char *V, unsigned VLength) {
211 return wrap(Attribute::get(*unwrap(C), StringRef(K, KLength),
212 StringRef(V, VLength)));
215 const char *LLVMGetStringAttributeKind(LLVMAttributeRef A,
216 unsigned *Length) {
217 auto S = unwrap(A).getKindAsString();
218 *Length = S.size();
219 return S.data();
222 const char *LLVMGetStringAttributeValue(LLVMAttributeRef A,
223 unsigned *Length) {
224 auto S = unwrap(A).getValueAsString();
225 *Length = S.size();
226 return S.data();
229 LLVMBool LLVMIsEnumAttribute(LLVMAttributeRef A) {
230 auto Attr = unwrap(A);
231 return Attr.isEnumAttribute() || Attr.isIntAttribute();
234 LLVMBool LLVMIsStringAttribute(LLVMAttributeRef A) {
235 return unwrap(A).isStringAttribute();
238 LLVMBool LLVMIsTypeAttribute(LLVMAttributeRef A) {
239 return unwrap(A).isTypeAttribute();
242 char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) {
243 std::string MsgStorage;
244 raw_string_ostream Stream(MsgStorage);
245 DiagnosticPrinterRawOStream DP(Stream);
247 unwrap(DI)->print(DP);
248 Stream.flush();
250 return LLVMCreateMessage(MsgStorage.c_str());
253 LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI) {
254 LLVMDiagnosticSeverity severity;
256 switch(unwrap(DI)->getSeverity()) {
257 default:
258 severity = LLVMDSError;
259 break;
260 case DS_Warning:
261 severity = LLVMDSWarning;
262 break;
263 case DS_Remark:
264 severity = LLVMDSRemark;
265 break;
266 case DS_Note:
267 severity = LLVMDSNote;
268 break;
271 return severity;
274 /*===-- Operations on modules ---------------------------------------------===*/
276 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
277 return wrap(new Module(ModuleID, getGlobalContext()));
280 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
281 LLVMContextRef C) {
282 return wrap(new Module(ModuleID, *unwrap(C)));
285 void LLVMDisposeModule(LLVMModuleRef M) {
286 delete unwrap(M);
289 const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) {
290 auto &Str = unwrap(M)->getModuleIdentifier();
291 *Len = Str.length();
292 return Str.c_str();
295 void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) {
296 unwrap(M)->setModuleIdentifier(StringRef(Ident, Len));
299 const char *LLVMGetSourceFileName(LLVMModuleRef M, size_t *Len) {
300 auto &Str = unwrap(M)->getSourceFileName();
301 *Len = Str.length();
302 return Str.c_str();
305 void LLVMSetSourceFileName(LLVMModuleRef M, const char *Name, size_t Len) {
306 unwrap(M)->setSourceFileName(StringRef(Name, Len));
309 /*--.. Data layout .........................................................--*/
310 const char *LLVMGetDataLayoutStr(LLVMModuleRef M) {
311 return unwrap(M)->getDataLayoutStr().c_str();
314 const char *LLVMGetDataLayout(LLVMModuleRef M) {
315 return LLVMGetDataLayoutStr(M);
318 void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
319 unwrap(M)->setDataLayout(DataLayoutStr);
322 /*--.. Target triple .......................................................--*/
323 const char * LLVMGetTarget(LLVMModuleRef M) {
324 return unwrap(M)->getTargetTriple().c_str();
327 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
328 unwrap(M)->setTargetTriple(Triple);
331 /*--.. Module flags ........................................................--*/
332 struct LLVMOpaqueModuleFlagEntry {
333 LLVMModuleFlagBehavior Behavior;
334 const char *Key;
335 size_t KeyLen;
336 LLVMMetadataRef Metadata;
339 static Module::ModFlagBehavior
340 map_to_llvmModFlagBehavior(LLVMModuleFlagBehavior Behavior) {
341 switch (Behavior) {
342 case LLVMModuleFlagBehaviorError:
343 return Module::ModFlagBehavior::Error;
344 case LLVMModuleFlagBehaviorWarning:
345 return Module::ModFlagBehavior::Warning;
346 case LLVMModuleFlagBehaviorRequire:
347 return Module::ModFlagBehavior::Require;
348 case LLVMModuleFlagBehaviorOverride:
349 return Module::ModFlagBehavior::Override;
350 case LLVMModuleFlagBehaviorAppend:
351 return Module::ModFlagBehavior::Append;
352 case LLVMModuleFlagBehaviorAppendUnique:
353 return Module::ModFlagBehavior::AppendUnique;
355 llvm_unreachable("Unknown LLVMModuleFlagBehavior");
358 static LLVMModuleFlagBehavior
359 map_from_llvmModFlagBehavior(Module::ModFlagBehavior Behavior) {
360 switch (Behavior) {
361 case Module::ModFlagBehavior::Error:
362 return LLVMModuleFlagBehaviorError;
363 case Module::ModFlagBehavior::Warning:
364 return LLVMModuleFlagBehaviorWarning;
365 case Module::ModFlagBehavior::Require:
366 return LLVMModuleFlagBehaviorRequire;
367 case Module::ModFlagBehavior::Override:
368 return LLVMModuleFlagBehaviorOverride;
369 case Module::ModFlagBehavior::Append:
370 return LLVMModuleFlagBehaviorAppend;
371 case Module::ModFlagBehavior::AppendUnique:
372 return LLVMModuleFlagBehaviorAppendUnique;
373 default:
374 llvm_unreachable("Unhandled Flag Behavior");
378 LLVMModuleFlagEntry *LLVMCopyModuleFlagsMetadata(LLVMModuleRef M, size_t *Len) {
379 SmallVector<Module::ModuleFlagEntry, 8> MFEs;
380 unwrap(M)->getModuleFlagsMetadata(MFEs);
382 LLVMOpaqueModuleFlagEntry *Result = static_cast<LLVMOpaqueModuleFlagEntry *>(
383 safe_malloc(MFEs.size() * sizeof(LLVMOpaqueModuleFlagEntry)));
384 for (unsigned i = 0; i < MFEs.size(); ++i) {
385 const auto &ModuleFlag = MFEs[i];
386 Result[i].Behavior = map_from_llvmModFlagBehavior(ModuleFlag.Behavior);
387 Result[i].Key = ModuleFlag.Key->getString().data();
388 Result[i].KeyLen = ModuleFlag.Key->getString().size();
389 Result[i].Metadata = wrap(ModuleFlag.Val);
391 *Len = MFEs.size();
392 return Result;
395 void LLVMDisposeModuleFlagsMetadata(LLVMModuleFlagEntry *Entries) {
396 free(Entries);
399 LLVMModuleFlagBehavior
400 LLVMModuleFlagEntriesGetFlagBehavior(LLVMModuleFlagEntry *Entries,
401 unsigned Index) {
402 LLVMOpaqueModuleFlagEntry MFE =
403 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
404 return MFE.Behavior;
407 const char *LLVMModuleFlagEntriesGetKey(LLVMModuleFlagEntry *Entries,
408 unsigned Index, size_t *Len) {
409 LLVMOpaqueModuleFlagEntry MFE =
410 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
411 *Len = MFE.KeyLen;
412 return MFE.Key;
415 LLVMMetadataRef LLVMModuleFlagEntriesGetMetadata(LLVMModuleFlagEntry *Entries,
416 unsigned Index) {
417 LLVMOpaqueModuleFlagEntry MFE =
418 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
419 return MFE.Metadata;
422 LLVMMetadataRef LLVMGetModuleFlag(LLVMModuleRef M,
423 const char *Key, size_t KeyLen) {
424 return wrap(unwrap(M)->getModuleFlag({Key, KeyLen}));
427 void LLVMAddModuleFlag(LLVMModuleRef M, LLVMModuleFlagBehavior Behavior,
428 const char *Key, size_t KeyLen,
429 LLVMMetadataRef Val) {
430 unwrap(M)->addModuleFlag(map_to_llvmModFlagBehavior(Behavior),
431 {Key, KeyLen}, unwrap(Val));
434 LLVMBool LLVMIsNewDbgInfoFormat(LLVMModuleRef M) {
435 return unwrap(M)->IsNewDbgInfoFormat;
438 void LLVMSetIsNewDbgInfoFormat(LLVMModuleRef M, LLVMBool UseNewFormat) {
439 unwrap(M)->setIsNewDbgInfoFormat(UseNewFormat);
442 /*--.. Printing modules ....................................................--*/
444 void LLVMDumpModule(LLVMModuleRef M) {
445 unwrap(M)->print(errs(), nullptr,
446 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
449 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
450 char **ErrorMessage) {
451 std::error_code EC;
452 raw_fd_ostream dest(Filename, EC, sys::fs::OF_TextWithCRLF);
453 if (EC) {
454 *ErrorMessage = strdup(EC.message().c_str());
455 return true;
458 unwrap(M)->print(dest, nullptr);
460 dest.close();
462 if (dest.has_error()) {
463 std::string E = "Error printing to file: " + dest.error().message();
464 *ErrorMessage = strdup(E.c_str());
465 return true;
468 return false;
471 char *LLVMPrintModuleToString(LLVMModuleRef M) {
472 std::string buf;
473 raw_string_ostream os(buf);
475 unwrap(M)->print(os, nullptr);
476 os.flush();
478 return strdup(buf.c_str());
481 /*--.. Operations on inline assembler ......................................--*/
482 void LLVMSetModuleInlineAsm2(LLVMModuleRef M, const char *Asm, size_t Len) {
483 unwrap(M)->setModuleInlineAsm(StringRef(Asm, Len));
486 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
487 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
490 void LLVMAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm, size_t Len) {
491 unwrap(M)->appendModuleInlineAsm(StringRef(Asm, Len));
494 const char *LLVMGetModuleInlineAsm(LLVMModuleRef M, size_t *Len) {
495 auto &Str = unwrap(M)->getModuleInlineAsm();
496 *Len = Str.length();
497 return Str.c_str();
500 LLVMValueRef LLVMGetInlineAsm(LLVMTypeRef Ty, const char *AsmString,
501 size_t AsmStringSize, const char *Constraints,
502 size_t ConstraintsSize, LLVMBool HasSideEffects,
503 LLVMBool IsAlignStack,
504 LLVMInlineAsmDialect Dialect, LLVMBool CanThrow) {
505 InlineAsm::AsmDialect AD;
506 switch (Dialect) {
507 case LLVMInlineAsmDialectATT:
508 AD = InlineAsm::AD_ATT;
509 break;
510 case LLVMInlineAsmDialectIntel:
511 AD = InlineAsm::AD_Intel;
512 break;
514 return wrap(InlineAsm::get(unwrap<FunctionType>(Ty),
515 StringRef(AsmString, AsmStringSize),
516 StringRef(Constraints, ConstraintsSize),
517 HasSideEffects, IsAlignStack, AD, CanThrow));
520 const char *LLVMGetInlineAsmAsmString(LLVMValueRef InlineAsmVal, size_t *Len) {
522 Value *Val = unwrap<Value>(InlineAsmVal);
523 const std::string &AsmString = cast<InlineAsm>(Val)->getAsmString();
525 *Len = AsmString.length();
526 return AsmString.c_str();
529 const char *LLVMGetInlineAsmConstraintString(LLVMValueRef InlineAsmVal,
530 size_t *Len) {
531 Value *Val = unwrap<Value>(InlineAsmVal);
532 const std::string &ConstraintString =
533 cast<InlineAsm>(Val)->getConstraintString();
535 *Len = ConstraintString.length();
536 return ConstraintString.c_str();
539 LLVMInlineAsmDialect LLVMGetInlineAsmDialect(LLVMValueRef InlineAsmVal) {
541 Value *Val = unwrap<Value>(InlineAsmVal);
542 InlineAsm::AsmDialect Dialect = cast<InlineAsm>(Val)->getDialect();
544 switch (Dialect) {
545 case InlineAsm::AD_ATT:
546 return LLVMInlineAsmDialectATT;
547 case InlineAsm::AD_Intel:
548 return LLVMInlineAsmDialectIntel;
551 llvm_unreachable("Unrecognized inline assembly dialect");
552 return LLVMInlineAsmDialectATT;
555 LLVMTypeRef LLVMGetInlineAsmFunctionType(LLVMValueRef InlineAsmVal) {
556 Value *Val = unwrap<Value>(InlineAsmVal);
557 return (LLVMTypeRef)cast<InlineAsm>(Val)->getFunctionType();
560 LLVMBool LLVMGetInlineAsmHasSideEffects(LLVMValueRef InlineAsmVal) {
561 Value *Val = unwrap<Value>(InlineAsmVal);
562 return cast<InlineAsm>(Val)->hasSideEffects();
565 LLVMBool LLVMGetInlineAsmNeedsAlignedStack(LLVMValueRef InlineAsmVal) {
566 Value *Val = unwrap<Value>(InlineAsmVal);
567 return cast<InlineAsm>(Val)->isAlignStack();
570 LLVMBool LLVMGetInlineAsmCanUnwind(LLVMValueRef InlineAsmVal) {
571 Value *Val = unwrap<Value>(InlineAsmVal);
572 return cast<InlineAsm>(Val)->canThrow();
575 /*--.. Operations on module contexts ......................................--*/
576 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
577 return wrap(&unwrap(M)->getContext());
581 /*===-- Operations on types -----------------------------------------------===*/
583 /*--.. Operations on all types (mostly) ....................................--*/
585 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
586 switch (unwrap(Ty)->getTypeID()) {
587 case Type::VoidTyID:
588 return LLVMVoidTypeKind;
589 case Type::HalfTyID:
590 return LLVMHalfTypeKind;
591 case Type::BFloatTyID:
592 return LLVMBFloatTypeKind;
593 case Type::FloatTyID:
594 return LLVMFloatTypeKind;
595 case Type::DoubleTyID:
596 return LLVMDoubleTypeKind;
597 case Type::X86_FP80TyID:
598 return LLVMX86_FP80TypeKind;
599 case Type::FP128TyID:
600 return LLVMFP128TypeKind;
601 case Type::PPC_FP128TyID:
602 return LLVMPPC_FP128TypeKind;
603 case Type::LabelTyID:
604 return LLVMLabelTypeKind;
605 case Type::MetadataTyID:
606 return LLVMMetadataTypeKind;
607 case Type::IntegerTyID:
608 return LLVMIntegerTypeKind;
609 case Type::FunctionTyID:
610 return LLVMFunctionTypeKind;
611 case Type::StructTyID:
612 return LLVMStructTypeKind;
613 case Type::ArrayTyID:
614 return LLVMArrayTypeKind;
615 case Type::PointerTyID:
616 return LLVMPointerTypeKind;
617 case Type::FixedVectorTyID:
618 return LLVMVectorTypeKind;
619 case Type::X86_AMXTyID:
620 return LLVMX86_AMXTypeKind;
621 case Type::TokenTyID:
622 return LLVMTokenTypeKind;
623 case Type::ScalableVectorTyID:
624 return LLVMScalableVectorTypeKind;
625 case Type::TargetExtTyID:
626 return LLVMTargetExtTypeKind;
627 case Type::TypedPointerTyID:
628 llvm_unreachable("Typed pointers are unsupported via the C API");
630 llvm_unreachable("Unhandled TypeID.");
633 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
635 return unwrap(Ty)->isSized();
638 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
639 return wrap(&unwrap(Ty)->getContext());
642 void LLVMDumpType(LLVMTypeRef Ty) {
643 return unwrap(Ty)->print(errs(), /*IsForDebug=*/true);
646 char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
647 std::string buf;
648 raw_string_ostream os(buf);
650 if (unwrap(Ty))
651 unwrap(Ty)->print(os);
652 else
653 os << "Printing <null> Type";
655 os.flush();
657 return strdup(buf.c_str());
660 /*--.. Operations on integer types .........................................--*/
662 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
663 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
665 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
666 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
668 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
669 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
671 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
672 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
674 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
675 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
677 LLVMTypeRef LLVMInt128TypeInContext(LLVMContextRef C) {
678 return (LLVMTypeRef) Type::getInt128Ty(*unwrap(C));
680 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
681 return wrap(IntegerType::get(*unwrap(C), NumBits));
684 LLVMTypeRef LLVMInt1Type(void) {
685 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
687 LLVMTypeRef LLVMInt8Type(void) {
688 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
690 LLVMTypeRef LLVMInt16Type(void) {
691 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
693 LLVMTypeRef LLVMInt32Type(void) {
694 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
696 LLVMTypeRef LLVMInt64Type(void) {
697 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
699 LLVMTypeRef LLVMInt128Type(void) {
700 return LLVMInt128TypeInContext(LLVMGetGlobalContext());
702 LLVMTypeRef LLVMIntType(unsigned NumBits) {
703 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
706 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
707 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
710 /*--.. Operations on real types ............................................--*/
712 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
713 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
715 LLVMTypeRef LLVMBFloatTypeInContext(LLVMContextRef C) {
716 return (LLVMTypeRef) Type::getBFloatTy(*unwrap(C));
718 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
719 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
721 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
722 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
724 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
725 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
727 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
728 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
730 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
731 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
733 LLVMTypeRef LLVMX86AMXTypeInContext(LLVMContextRef C) {
734 return (LLVMTypeRef) Type::getX86_AMXTy(*unwrap(C));
737 LLVMTypeRef LLVMHalfType(void) {
738 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
740 LLVMTypeRef LLVMBFloatType(void) {
741 return LLVMBFloatTypeInContext(LLVMGetGlobalContext());
743 LLVMTypeRef LLVMFloatType(void) {
744 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
746 LLVMTypeRef LLVMDoubleType(void) {
747 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
749 LLVMTypeRef LLVMX86FP80Type(void) {
750 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
752 LLVMTypeRef LLVMFP128Type(void) {
753 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
755 LLVMTypeRef LLVMPPCFP128Type(void) {
756 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
758 LLVMTypeRef LLVMX86AMXType(void) {
759 return LLVMX86AMXTypeInContext(LLVMGetGlobalContext());
762 /*--.. Operations on function types ........................................--*/
764 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
765 LLVMTypeRef *ParamTypes, unsigned ParamCount,
766 LLVMBool IsVarArg) {
767 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
768 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
771 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
772 return unwrap<FunctionType>(FunctionTy)->isVarArg();
775 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
776 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
779 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
780 return unwrap<FunctionType>(FunctionTy)->getNumParams();
783 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
784 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
785 for (Type *T : Ty->params())
786 *Dest++ = wrap(T);
789 /*--.. Operations on struct types ..........................................--*/
791 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
792 unsigned ElementCount, LLVMBool Packed) {
793 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
794 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
797 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
798 unsigned ElementCount, LLVMBool Packed) {
799 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
800 ElementCount, Packed);
803 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
805 return wrap(StructType::create(*unwrap(C), Name));
808 const char *LLVMGetStructName(LLVMTypeRef Ty)
810 StructType *Type = unwrap<StructType>(Ty);
811 if (!Type->hasName())
812 return nullptr;
813 return Type->getName().data();
816 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
817 unsigned ElementCount, LLVMBool Packed) {
818 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
819 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
822 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
823 return unwrap<StructType>(StructTy)->getNumElements();
826 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
827 StructType *Ty = unwrap<StructType>(StructTy);
828 for (Type *T : Ty->elements())
829 *Dest++ = wrap(T);
832 LLVMTypeRef LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy, unsigned i) {
833 StructType *Ty = unwrap<StructType>(StructTy);
834 return wrap(Ty->getTypeAtIndex(i));
837 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
838 return unwrap<StructType>(StructTy)->isPacked();
841 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
842 return unwrap<StructType>(StructTy)->isOpaque();
845 LLVMBool LLVMIsLiteralStruct(LLVMTypeRef StructTy) {
846 return unwrap<StructType>(StructTy)->isLiteral();
849 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
850 return wrap(StructType::getTypeByName(unwrap(M)->getContext(), Name));
853 LLVMTypeRef LLVMGetTypeByName2(LLVMContextRef C, const char *Name) {
854 return wrap(StructType::getTypeByName(*unwrap(C), Name));
857 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
859 void LLVMGetSubtypes(LLVMTypeRef Tp, LLVMTypeRef *Arr) {
860 int i = 0;
861 for (auto *T : unwrap(Tp)->subtypes()) {
862 Arr[i] = wrap(T);
863 i++;
867 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
868 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
871 LLVMTypeRef LLVMArrayType2(LLVMTypeRef ElementType, uint64_t ElementCount) {
872 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
875 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
876 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
879 LLVMBool LLVMPointerTypeIsOpaque(LLVMTypeRef Ty) {
880 return true;
883 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
884 return wrap(FixedVectorType::get(unwrap(ElementType), ElementCount));
887 LLVMTypeRef LLVMScalableVectorType(LLVMTypeRef ElementType,
888 unsigned ElementCount) {
889 return wrap(ScalableVectorType::get(unwrap(ElementType), ElementCount));
892 LLVMTypeRef LLVMGetElementType(LLVMTypeRef WrappedTy) {
893 auto *Ty = unwrap(WrappedTy);
894 if (auto *ATy = dyn_cast<ArrayType>(Ty))
895 return wrap(ATy->getElementType());
896 return wrap(cast<VectorType>(Ty)->getElementType());
899 unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp) {
900 return unwrap(Tp)->getNumContainedTypes();
903 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
904 return unwrap<ArrayType>(ArrayTy)->getNumElements();
907 uint64_t LLVMGetArrayLength2(LLVMTypeRef ArrayTy) {
908 return unwrap<ArrayType>(ArrayTy)->getNumElements();
911 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
912 return unwrap<PointerType>(PointerTy)->getAddressSpace();
915 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
916 return unwrap<VectorType>(VectorTy)->getElementCount().getKnownMinValue();
919 LLVMValueRef LLVMGetConstantPtrAuthPointer(LLVMValueRef PtrAuth) {
920 return wrap(unwrap<ConstantPtrAuth>(PtrAuth)->getPointer());
923 LLVMValueRef LLVMGetConstantPtrAuthKey(LLVMValueRef PtrAuth) {
924 return wrap(unwrap<ConstantPtrAuth>(PtrAuth)->getKey());
927 LLVMValueRef LLVMGetConstantPtrAuthDiscriminator(LLVMValueRef PtrAuth) {
928 return wrap(unwrap<ConstantPtrAuth>(PtrAuth)->getDiscriminator());
931 LLVMValueRef LLVMGetConstantPtrAuthAddrDiscriminator(LLVMValueRef PtrAuth) {
932 return wrap(unwrap<ConstantPtrAuth>(PtrAuth)->getAddrDiscriminator());
935 /*--.. Operations on other types ...........................................--*/
937 LLVMTypeRef LLVMPointerTypeInContext(LLVMContextRef C, unsigned AddressSpace) {
938 return wrap(PointerType::get(*unwrap(C), AddressSpace));
941 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
942 return wrap(Type::getVoidTy(*unwrap(C)));
944 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
945 return wrap(Type::getLabelTy(*unwrap(C)));
947 LLVMTypeRef LLVMTokenTypeInContext(LLVMContextRef C) {
948 return wrap(Type::getTokenTy(*unwrap(C)));
950 LLVMTypeRef LLVMMetadataTypeInContext(LLVMContextRef C) {
951 return wrap(Type::getMetadataTy(*unwrap(C)));
954 LLVMTypeRef LLVMVoidType(void) {
955 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
957 LLVMTypeRef LLVMLabelType(void) {
958 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
961 LLVMTypeRef LLVMTargetExtTypeInContext(LLVMContextRef C, const char *Name,
962 LLVMTypeRef *TypeParams,
963 unsigned TypeParamCount,
964 unsigned *IntParams,
965 unsigned IntParamCount) {
966 ArrayRef<Type *> TypeParamArray(unwrap(TypeParams), TypeParamCount);
967 ArrayRef<unsigned> IntParamArray(IntParams, IntParamCount);
968 return wrap(
969 TargetExtType::get(*unwrap(C), Name, TypeParamArray, IntParamArray));
972 const char *LLVMGetTargetExtTypeName(LLVMTypeRef TargetExtTy) {
973 TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);
974 return Type->getName().data();
977 unsigned LLVMGetTargetExtTypeNumTypeParams(LLVMTypeRef TargetExtTy) {
978 TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);
979 return Type->getNumTypeParameters();
982 LLVMTypeRef LLVMGetTargetExtTypeTypeParam(LLVMTypeRef TargetExtTy,
983 unsigned Idx) {
984 TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);
985 return wrap(Type->getTypeParameter(Idx));
988 unsigned LLVMGetTargetExtTypeNumIntParams(LLVMTypeRef TargetExtTy) {
989 TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);
990 return Type->getNumIntParameters();
993 unsigned LLVMGetTargetExtTypeIntParam(LLVMTypeRef TargetExtTy, unsigned Idx) {
994 TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);
995 return Type->getIntParameter(Idx);
998 /*===-- Operations on values ----------------------------------------------===*/
1000 /*--.. Operations on all values ............................................--*/
1002 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
1003 return wrap(unwrap(Val)->getType());
1006 LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) {
1007 switch(unwrap(Val)->getValueID()) {
1008 #define LLVM_C_API 1
1009 #define HANDLE_VALUE(Name) \
1010 case Value::Name##Val: \
1011 return LLVM##Name##ValueKind;
1012 #include "llvm/IR/Value.def"
1013 default:
1014 return LLVMInstructionValueKind;
1018 const char *LLVMGetValueName2(LLVMValueRef Val, size_t *Length) {
1019 auto *V = unwrap(Val);
1020 *Length = V->getName().size();
1021 return V->getName().data();
1024 void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen) {
1025 unwrap(Val)->setName(StringRef(Name, NameLen));
1028 const char *LLVMGetValueName(LLVMValueRef Val) {
1029 return unwrap(Val)->getName().data();
1032 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
1033 unwrap(Val)->setName(Name);
1036 void LLVMDumpValue(LLVMValueRef Val) {
1037 unwrap(Val)->print(errs(), /*IsForDebug=*/true);
1040 char* LLVMPrintValueToString(LLVMValueRef Val) {
1041 std::string buf;
1042 raw_string_ostream os(buf);
1044 if (unwrap(Val))
1045 unwrap(Val)->print(os);
1046 else
1047 os << "Printing <null> Value";
1049 os.flush();
1051 return strdup(buf.c_str());
1054 LLVMContextRef LLVMGetValueContext(LLVMValueRef Val) {
1055 return wrap(&unwrap(Val)->getContext());
1058 char *LLVMPrintDbgRecordToString(LLVMDbgRecordRef Record) {
1059 std::string buf;
1060 raw_string_ostream os(buf);
1062 if (unwrap(Record))
1063 unwrap(Record)->print(os);
1064 else
1065 os << "Printing <null> DbgRecord";
1067 os.flush();
1069 return strdup(buf.c_str());
1072 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
1073 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
1076 int LLVMHasMetadata(LLVMValueRef Inst) {
1077 return unwrap<Instruction>(Inst)->hasMetadata();
1080 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
1081 auto *I = unwrap<Instruction>(Inst);
1082 assert(I && "Expected instruction");
1083 if (auto *MD = I->getMetadata(KindID))
1084 return wrap(MetadataAsValue::get(I->getContext(), MD));
1085 return nullptr;
1088 // MetadataAsValue uses a canonical format which strips the actual MDNode for
1089 // MDNode with just a single constant value, storing just a ConstantAsMetadata
1090 // This undoes this canonicalization, reconstructing the MDNode.
1091 static MDNode *extractMDNode(MetadataAsValue *MAV) {
1092 Metadata *MD = MAV->getMetadata();
1093 assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
1094 "Expected a metadata node or a canonicalized constant");
1096 if (MDNode *N = dyn_cast<MDNode>(MD))
1097 return N;
1099 return MDNode::get(MAV->getContext(), MD);
1102 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
1103 MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
1105 unwrap<Instruction>(Inst)->setMetadata(KindID, N);
1108 struct LLVMOpaqueValueMetadataEntry {
1109 unsigned Kind;
1110 LLVMMetadataRef Metadata;
1113 using MetadataEntries = SmallVectorImpl<std::pair<unsigned, MDNode *>>;
1114 static LLVMValueMetadataEntry *
1115 llvm_getMetadata(size_t *NumEntries,
1116 llvm::function_ref<void(MetadataEntries &)> AccessMD) {
1117 SmallVector<std::pair<unsigned, MDNode *>, 8> MVEs;
1118 AccessMD(MVEs);
1120 LLVMOpaqueValueMetadataEntry *Result =
1121 static_cast<LLVMOpaqueValueMetadataEntry *>(
1122 safe_malloc(MVEs.size() * sizeof(LLVMOpaqueValueMetadataEntry)));
1123 for (unsigned i = 0; i < MVEs.size(); ++i) {
1124 const auto &ModuleFlag = MVEs[i];
1125 Result[i].Kind = ModuleFlag.first;
1126 Result[i].Metadata = wrap(ModuleFlag.second);
1128 *NumEntries = MVEs.size();
1129 return Result;
1132 LLVMValueMetadataEntry *
1133 LLVMInstructionGetAllMetadataOtherThanDebugLoc(LLVMValueRef Value,
1134 size_t *NumEntries) {
1135 return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
1136 Entries.clear();
1137 unwrap<Instruction>(Value)->getAllMetadata(Entries);
1141 /*--.. Conversion functions ................................................--*/
1143 #define LLVM_DEFINE_VALUE_CAST(name) \
1144 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
1145 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
1148 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
1150 LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) {
1151 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
1152 if (isa<MDNode>(MD->getMetadata()) ||
1153 isa<ValueAsMetadata>(MD->getMetadata()))
1154 return Val;
1155 return nullptr;
1158 LLVMValueRef LLVMIsAValueAsMetadata(LLVMValueRef Val) {
1159 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
1160 if (isa<ValueAsMetadata>(MD->getMetadata()))
1161 return Val;
1162 return nullptr;
1165 LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) {
1166 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
1167 if (isa<MDString>(MD->getMetadata()))
1168 return Val;
1169 return nullptr;
1172 /*--.. Operations on Uses ..................................................--*/
1173 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
1174 Value *V = unwrap(Val);
1175 Value::use_iterator I = V->use_begin();
1176 if (I == V->use_end())
1177 return nullptr;
1178 return wrap(&*I);
1181 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
1182 Use *Next = unwrap(U)->getNext();
1183 if (Next)
1184 return wrap(Next);
1185 return nullptr;
1188 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
1189 return wrap(unwrap(U)->getUser());
1192 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
1193 return wrap(unwrap(U)->get());
1196 /*--.. Operations on Users .................................................--*/
1198 static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N,
1199 unsigned Index) {
1200 Metadata *Op = N->getOperand(Index);
1201 if (!Op)
1202 return nullptr;
1203 if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
1204 return wrap(C->getValue());
1205 return wrap(MetadataAsValue::get(Context, Op));
1208 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
1209 Value *V = unwrap(Val);
1210 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
1211 if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1212 assert(Index == 0 && "Function-local metadata can only have one operand");
1213 return wrap(L->getValue());
1215 return getMDNodeOperandImpl(V->getContext(),
1216 cast<MDNode>(MD->getMetadata()), Index);
1219 return wrap(cast<User>(V)->getOperand(Index));
1222 LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) {
1223 Value *V = unwrap(Val);
1224 return wrap(&cast<User>(V)->getOperandUse(Index));
1227 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
1228 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
1231 int LLVMGetNumOperands(LLVMValueRef Val) {
1232 Value *V = unwrap(Val);
1233 if (isa<MetadataAsValue>(V))
1234 return LLVMGetMDNodeNumOperands(Val);
1236 return cast<User>(V)->getNumOperands();
1239 /*--.. Operations on constants of any type .................................--*/
1241 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
1242 return wrap(Constant::getNullValue(unwrap(Ty)));
1245 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
1246 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
1249 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
1250 return wrap(UndefValue::get(unwrap(Ty)));
1253 LLVMValueRef LLVMGetPoison(LLVMTypeRef Ty) {
1254 return wrap(PoisonValue::get(unwrap(Ty)));
1257 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
1258 return isa<Constant>(unwrap(Ty));
1261 LLVMBool LLVMIsNull(LLVMValueRef Val) {
1262 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
1263 return C->isNullValue();
1264 return false;
1267 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
1268 return isa<UndefValue>(unwrap(Val));
1271 LLVMBool LLVMIsPoison(LLVMValueRef Val) {
1272 return isa<PoisonValue>(unwrap(Val));
1275 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
1276 return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
1279 /*--.. Operations on metadata nodes ........................................--*/
1281 LLVMMetadataRef LLVMMDStringInContext2(LLVMContextRef C, const char *Str,
1282 size_t SLen) {
1283 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
1286 LLVMMetadataRef LLVMMDNodeInContext2(LLVMContextRef C, LLVMMetadataRef *MDs,
1287 size_t Count) {
1288 return wrap(MDNode::get(*unwrap(C), ArrayRef<Metadata*>(unwrap(MDs), Count)));
1291 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
1292 unsigned SLen) {
1293 LLVMContext &Context = *unwrap(C);
1294 return wrap(MetadataAsValue::get(
1295 Context, MDString::get(Context, StringRef(Str, SLen))));
1298 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
1299 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
1302 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
1303 unsigned Count) {
1304 LLVMContext &Context = *unwrap(C);
1305 SmallVector<Metadata *, 8> MDs;
1306 for (auto *OV : ArrayRef(Vals, Count)) {
1307 Value *V = unwrap(OV);
1308 Metadata *MD;
1309 if (!V)
1310 MD = nullptr;
1311 else if (auto *C = dyn_cast<Constant>(V))
1312 MD = ConstantAsMetadata::get(C);
1313 else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
1314 MD = MDV->getMetadata();
1315 assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
1316 "outside of direct argument to call");
1317 } else {
1318 // This is function-local metadata. Pretend to make an MDNode.
1319 assert(Count == 1 &&
1320 "Expected only one operand to function-local metadata");
1321 return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V)));
1324 MDs.push_back(MD);
1326 return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs)));
1329 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
1330 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
1333 LLVMValueRef LLVMMetadataAsValue(LLVMContextRef C, LLVMMetadataRef MD) {
1334 return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
1337 LLVMMetadataRef LLVMValueAsMetadata(LLVMValueRef Val) {
1338 auto *V = unwrap(Val);
1339 if (auto *C = dyn_cast<Constant>(V))
1340 return wrap(ConstantAsMetadata::get(C));
1341 if (auto *MAV = dyn_cast<MetadataAsValue>(V))
1342 return wrap(MAV->getMetadata());
1343 return wrap(ValueAsMetadata::get(V));
1346 const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
1347 if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
1348 if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
1349 *Length = S->getString().size();
1350 return S->getString().data();
1352 *Length = 0;
1353 return nullptr;
1356 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) {
1357 auto *MD = unwrap<MetadataAsValue>(V);
1358 if (isa<ValueAsMetadata>(MD->getMetadata()))
1359 return 1;
1360 return cast<MDNode>(MD->getMetadata())->getNumOperands();
1363 LLVMNamedMDNodeRef LLVMGetFirstNamedMetadata(LLVMModuleRef M) {
1364 Module *Mod = unwrap(M);
1365 Module::named_metadata_iterator I = Mod->named_metadata_begin();
1366 if (I == Mod->named_metadata_end())
1367 return nullptr;
1368 return wrap(&*I);
1371 LLVMNamedMDNodeRef LLVMGetLastNamedMetadata(LLVMModuleRef M) {
1372 Module *Mod = unwrap(M);
1373 Module::named_metadata_iterator I = Mod->named_metadata_end();
1374 if (I == Mod->named_metadata_begin())
1375 return nullptr;
1376 return wrap(&*--I);
1379 LLVMNamedMDNodeRef LLVMGetNextNamedMetadata(LLVMNamedMDNodeRef NMD) {
1380 NamedMDNode *NamedNode = unwrap(NMD);
1381 Module::named_metadata_iterator I(NamedNode);
1382 if (++I == NamedNode->getParent()->named_metadata_end())
1383 return nullptr;
1384 return wrap(&*I);
1387 LLVMNamedMDNodeRef LLVMGetPreviousNamedMetadata(LLVMNamedMDNodeRef NMD) {
1388 NamedMDNode *NamedNode = unwrap(NMD);
1389 Module::named_metadata_iterator I(NamedNode);
1390 if (I == NamedNode->getParent()->named_metadata_begin())
1391 return nullptr;
1392 return wrap(&*--I);
1395 LLVMNamedMDNodeRef LLVMGetNamedMetadata(LLVMModuleRef M,
1396 const char *Name, size_t NameLen) {
1397 return wrap(unwrap(M)->getNamedMetadata(StringRef(Name, NameLen)));
1400 LLVMNamedMDNodeRef LLVMGetOrInsertNamedMetadata(LLVMModuleRef M,
1401 const char *Name, size_t NameLen) {
1402 return wrap(unwrap(M)->getOrInsertNamedMetadata({Name, NameLen}));
1405 const char *LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NMD, size_t *NameLen) {
1406 NamedMDNode *NamedNode = unwrap(NMD);
1407 *NameLen = NamedNode->getName().size();
1408 return NamedNode->getName().data();
1411 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) {
1412 auto *MD = unwrap<MetadataAsValue>(V);
1413 if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1414 *Dest = wrap(MDV->getValue());
1415 return;
1417 const auto *N = cast<MDNode>(MD->getMetadata());
1418 const unsigned numOperands = N->getNumOperands();
1419 LLVMContext &Context = unwrap(V)->getContext();
1420 for (unsigned i = 0; i < numOperands; i++)
1421 Dest[i] = getMDNodeOperandImpl(Context, N, i);
1424 void LLVMReplaceMDNodeOperandWith(LLVMValueRef V, unsigned Index,
1425 LLVMMetadataRef Replacement) {
1426 auto *MD = cast<MetadataAsValue>(unwrap(V));
1427 auto *N = cast<MDNode>(MD->getMetadata());
1428 N->replaceOperandWith(Index, unwrap<Metadata>(Replacement));
1431 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char *Name) {
1432 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
1433 return N->getNumOperands();
1435 return 0;
1438 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char *Name,
1439 LLVMValueRef *Dest) {
1440 NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
1441 if (!N)
1442 return;
1443 LLVMContext &Context = unwrap(M)->getContext();
1444 for (unsigned i=0;i<N->getNumOperands();i++)
1445 Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
1448 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char *Name,
1449 LLVMValueRef Val) {
1450 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
1451 if (!N)
1452 return;
1453 if (!Val)
1454 return;
1455 N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
1458 const char *LLVMGetDebugLocDirectory(LLVMValueRef Val, unsigned *Length) {
1459 if (!Length) return nullptr;
1460 StringRef S;
1461 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1462 if (const auto &DL = I->getDebugLoc()) {
1463 S = DL->getDirectory();
1465 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1466 SmallVector<DIGlobalVariableExpression *, 1> GVEs;
1467 GV->getDebugInfo(GVEs);
1468 if (GVEs.size())
1469 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1470 S = DGV->getDirectory();
1471 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1472 if (const DISubprogram *DSP = F->getSubprogram())
1473 S = DSP->getDirectory();
1474 } else {
1475 assert(0 && "Expected Instruction, GlobalVariable or Function");
1476 return nullptr;
1478 *Length = S.size();
1479 return S.data();
1482 const char *LLVMGetDebugLocFilename(LLVMValueRef Val, unsigned *Length) {
1483 if (!Length) return nullptr;
1484 StringRef S;
1485 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1486 if (const auto &DL = I->getDebugLoc()) {
1487 S = DL->getFilename();
1489 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1490 SmallVector<DIGlobalVariableExpression *, 1> GVEs;
1491 GV->getDebugInfo(GVEs);
1492 if (GVEs.size())
1493 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1494 S = DGV->getFilename();
1495 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1496 if (const DISubprogram *DSP = F->getSubprogram())
1497 S = DSP->getFilename();
1498 } else {
1499 assert(0 && "Expected Instruction, GlobalVariable or Function");
1500 return nullptr;
1502 *Length = S.size();
1503 return S.data();
1506 unsigned LLVMGetDebugLocLine(LLVMValueRef Val) {
1507 unsigned L = 0;
1508 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1509 if (const auto &DL = I->getDebugLoc()) {
1510 L = DL->getLine();
1512 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1513 SmallVector<DIGlobalVariableExpression *, 1> GVEs;
1514 GV->getDebugInfo(GVEs);
1515 if (GVEs.size())
1516 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1517 L = DGV->getLine();
1518 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1519 if (const DISubprogram *DSP = F->getSubprogram())
1520 L = DSP->getLine();
1521 } else {
1522 assert(0 && "Expected Instruction, GlobalVariable or Function");
1523 return -1;
1525 return L;
1528 unsigned LLVMGetDebugLocColumn(LLVMValueRef Val) {
1529 unsigned C = 0;
1530 if (const auto *I = dyn_cast<Instruction>(unwrap(Val)))
1531 if (const auto &DL = I->getDebugLoc())
1532 C = DL->getColumn();
1533 return C;
1536 /*--.. Operations on scalar constants ......................................--*/
1538 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
1539 LLVMBool SignExtend) {
1540 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
1543 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
1544 unsigned NumWords,
1545 const uint64_t Words[]) {
1546 IntegerType *Ty = unwrap<IntegerType>(IntTy);
1547 return wrap(ConstantInt::get(
1548 Ty->getContext(), APInt(Ty->getBitWidth(), ArrayRef(Words, NumWords))));
1551 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
1552 uint8_t Radix) {
1553 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
1554 Radix));
1557 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
1558 unsigned SLen, uint8_t Radix) {
1559 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
1560 Radix));
1563 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
1564 return wrap(ConstantFP::get(unwrap(RealTy), N));
1567 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
1568 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
1571 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
1572 unsigned SLen) {
1573 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
1576 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
1577 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
1580 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
1581 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
1584 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
1585 ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
1586 Type *Ty = cFP->getType();
1588 if (Ty->isHalfTy() || Ty->isBFloatTy() || Ty->isFloatTy() ||
1589 Ty->isDoubleTy()) {
1590 *LosesInfo = false;
1591 return cFP->getValueAPF().convertToDouble();
1594 bool APFLosesInfo;
1595 APFloat APF = cFP->getValueAPF();
1596 APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo);
1597 *LosesInfo = APFLosesInfo;
1598 return APF.convertToDouble();
1601 /*--.. Operations on composite constants ...................................--*/
1603 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
1604 unsigned Length,
1605 LLVMBool DontNullTerminate) {
1606 /* Inverted the sense of AddNull because ', 0)' is a
1607 better mnemonic for null termination than ', 1)'. */
1608 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
1609 DontNullTerminate == 0));
1612 LLVMValueRef LLVMConstStringInContext2(LLVMContextRef C, const char *Str,
1613 size_t Length,
1614 LLVMBool DontNullTerminate) {
1615 /* Inverted the sense of AddNull because ', 0)' is a
1616 better mnemonic for null termination than ', 1)'. */
1617 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
1618 DontNullTerminate == 0));
1621 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
1622 LLVMBool DontNullTerminate) {
1623 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
1624 DontNullTerminate);
1627 LLVMValueRef LLVMGetAggregateElement(LLVMValueRef C, unsigned Idx) {
1628 return wrap(unwrap<Constant>(C)->getAggregateElement(Idx));
1631 LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef C, unsigned idx) {
1632 return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
1635 LLVMBool LLVMIsConstantString(LLVMValueRef C) {
1636 return unwrap<ConstantDataSequential>(C)->isString();
1639 const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1640 StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1641 *Length = Str.size();
1642 return Str.data();
1645 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
1646 LLVMValueRef *ConstantVals, unsigned Length) {
1647 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1648 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1651 LLVMValueRef LLVMConstArray2(LLVMTypeRef ElementTy, LLVMValueRef *ConstantVals,
1652 uint64_t Length) {
1653 ArrayRef<Constant *> V(unwrap<Constant>(ConstantVals, Length), Length);
1654 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1657 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
1658 LLVMValueRef *ConstantVals,
1659 unsigned Count, LLVMBool Packed) {
1660 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1661 return wrap(ConstantStruct::getAnon(*unwrap(C), ArrayRef(Elements, Count),
1662 Packed != 0));
1665 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
1666 LLVMBool Packed) {
1667 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1668 Packed);
1671 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
1672 LLVMValueRef *ConstantVals,
1673 unsigned Count) {
1674 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1675 StructType *Ty = unwrap<StructType>(StructTy);
1677 return wrap(ConstantStruct::get(Ty, ArrayRef(Elements, Count)));
1680 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
1681 return wrap(ConstantVector::get(
1682 ArrayRef(unwrap<Constant>(ScalarConstantVals, Size), Size)));
1685 LLVMValueRef LLVMConstantPtrAuth(LLVMValueRef Ptr, LLVMValueRef Key,
1686 LLVMValueRef Disc, LLVMValueRef AddrDisc) {
1687 return wrap(ConstantPtrAuth::get(
1688 unwrap<Constant>(Ptr), unwrap<ConstantInt>(Key),
1689 unwrap<ConstantInt>(Disc), unwrap<Constant>(AddrDisc)));
1692 /*-- Opcode mapping */
1694 static LLVMOpcode map_to_llvmopcode(int opcode)
1696 switch (opcode) {
1697 default: llvm_unreachable("Unhandled Opcode.");
1698 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
1699 #include "llvm/IR/Instruction.def"
1700 #undef HANDLE_INST
1704 static int map_from_llvmopcode(LLVMOpcode code)
1706 switch (code) {
1707 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
1708 #include "llvm/IR/Instruction.def"
1709 #undef HANDLE_INST
1711 llvm_unreachable("Unhandled Opcode.");
1714 /*-- GEP wrap flag conversions */
1716 static GEPNoWrapFlags mapFromLLVMGEPNoWrapFlags(LLVMGEPNoWrapFlags GEPFlags) {
1717 GEPNoWrapFlags NewGEPFlags;
1718 if ((GEPFlags & LLVMGEPFlagInBounds) != 0)
1719 NewGEPFlags |= GEPNoWrapFlags::inBounds();
1720 if ((GEPFlags & LLVMGEPFlagNUSW) != 0)
1721 NewGEPFlags |= GEPNoWrapFlags::noUnsignedSignedWrap();
1722 if ((GEPFlags & LLVMGEPFlagNUW) != 0)
1723 NewGEPFlags |= GEPNoWrapFlags::noUnsignedWrap();
1725 return NewGEPFlags;
1728 static LLVMGEPNoWrapFlags mapToLLVMGEPNoWrapFlags(GEPNoWrapFlags GEPFlags) {
1729 LLVMGEPNoWrapFlags NewGEPFlags = 0;
1730 if (GEPFlags.isInBounds())
1731 NewGEPFlags |= LLVMGEPFlagInBounds;
1732 if (GEPFlags.hasNoUnsignedSignedWrap())
1733 NewGEPFlags |= LLVMGEPFlagNUSW;
1734 if (GEPFlags.hasNoUnsignedWrap())
1735 NewGEPFlags |= LLVMGEPFlagNUW;
1737 return NewGEPFlags;
1740 /*--.. Constant expressions ................................................--*/
1742 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
1743 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
1746 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
1747 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
1750 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
1751 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
1754 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
1755 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1758 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
1759 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
1762 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
1763 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1767 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
1768 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
1771 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1772 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
1773 unwrap<Constant>(RHSConstant)));
1776 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
1777 LLVMValueRef RHSConstant) {
1778 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
1779 unwrap<Constant>(RHSConstant)));
1782 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
1783 LLVMValueRef RHSConstant) {
1784 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
1785 unwrap<Constant>(RHSConstant)));
1788 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1789 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
1790 unwrap<Constant>(RHSConstant)));
1793 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
1794 LLVMValueRef RHSConstant) {
1795 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
1796 unwrap<Constant>(RHSConstant)));
1799 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
1800 LLVMValueRef RHSConstant) {
1801 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
1802 unwrap<Constant>(RHSConstant)));
1805 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1806 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
1807 unwrap<Constant>(RHSConstant)));
1810 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
1811 LLVMValueRef RHSConstant) {
1812 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
1813 unwrap<Constant>(RHSConstant)));
1816 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
1817 LLVMValueRef RHSConstant) {
1818 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
1819 unwrap<Constant>(RHSConstant)));
1822 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1823 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
1824 unwrap<Constant>(RHSConstant)));
1827 LLVMValueRef LLVMConstGEP2(LLVMTypeRef Ty, LLVMValueRef ConstantVal,
1828 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1829 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1830 NumIndices);
1831 Constant *Val = unwrap<Constant>(ConstantVal);
1832 return wrap(ConstantExpr::getGetElementPtr(unwrap(Ty), Val, IdxList));
1835 LLVMValueRef LLVMConstInBoundsGEP2(LLVMTypeRef Ty, LLVMValueRef ConstantVal,
1836 LLVMValueRef *ConstantIndices,
1837 unsigned NumIndices) {
1838 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1839 NumIndices);
1840 Constant *Val = unwrap<Constant>(ConstantVal);
1841 return wrap(ConstantExpr::getInBoundsGetElementPtr(unwrap(Ty), Val, IdxList));
1844 LLVMValueRef LLVMConstGEPWithNoWrapFlags(LLVMTypeRef Ty,
1845 LLVMValueRef ConstantVal,
1846 LLVMValueRef *ConstantIndices,
1847 unsigned NumIndices,
1848 LLVMGEPNoWrapFlags NoWrapFlags) {
1849 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1850 NumIndices);
1851 Constant *Val = unwrap<Constant>(ConstantVal);
1852 return wrap(ConstantExpr::getGetElementPtr(
1853 unwrap(Ty), Val, IdxList, mapFromLLVMGEPNoWrapFlags(NoWrapFlags)));
1856 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1857 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1858 unwrap(ToType)));
1861 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1862 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1863 unwrap(ToType)));
1866 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1867 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1868 unwrap(ToType)));
1871 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1872 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1873 unwrap(ToType)));
1876 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1877 LLVMTypeRef ToType) {
1878 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1879 unwrap(ToType)));
1882 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1883 LLVMTypeRef ToType) {
1884 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1885 unwrap(ToType)));
1888 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1889 LLVMTypeRef ToType) {
1890 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1891 unwrap(ToType)));
1894 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1895 LLVMValueRef IndexConstant) {
1896 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1897 unwrap<Constant>(IndexConstant)));
1900 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1901 LLVMValueRef ElementValueConstant,
1902 LLVMValueRef IndexConstant) {
1903 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1904 unwrap<Constant>(ElementValueConstant),
1905 unwrap<Constant>(IndexConstant)));
1908 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1909 LLVMValueRef VectorBConstant,
1910 LLVMValueRef MaskConstant) {
1911 SmallVector<int, 16> IntMask;
1912 ShuffleVectorInst::getShuffleMask(unwrap<Constant>(MaskConstant), IntMask);
1913 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1914 unwrap<Constant>(VectorBConstant),
1915 IntMask));
1918 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1919 const char *Constraints,
1920 LLVMBool HasSideEffects,
1921 LLVMBool IsAlignStack) {
1922 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1923 Constraints, HasSideEffects, IsAlignStack));
1926 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1927 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1930 LLVMValueRef LLVMGetBlockAddressFunction(LLVMValueRef BlockAddr) {
1931 return wrap(unwrap<BlockAddress>(BlockAddr)->getFunction());
1934 LLVMBasicBlockRef LLVMGetBlockAddressBasicBlock(LLVMValueRef BlockAddr) {
1935 return wrap(unwrap<BlockAddress>(BlockAddr)->getBasicBlock());
1938 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1940 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1941 return wrap(unwrap<GlobalValue>(Global)->getParent());
1944 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1945 return unwrap<GlobalValue>(Global)->isDeclaration();
1948 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1949 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1950 case GlobalValue::ExternalLinkage:
1951 return LLVMExternalLinkage;
1952 case GlobalValue::AvailableExternallyLinkage:
1953 return LLVMAvailableExternallyLinkage;
1954 case GlobalValue::LinkOnceAnyLinkage:
1955 return LLVMLinkOnceAnyLinkage;
1956 case GlobalValue::LinkOnceODRLinkage:
1957 return LLVMLinkOnceODRLinkage;
1958 case GlobalValue::WeakAnyLinkage:
1959 return LLVMWeakAnyLinkage;
1960 case GlobalValue::WeakODRLinkage:
1961 return LLVMWeakODRLinkage;
1962 case GlobalValue::AppendingLinkage:
1963 return LLVMAppendingLinkage;
1964 case GlobalValue::InternalLinkage:
1965 return LLVMInternalLinkage;
1966 case GlobalValue::PrivateLinkage:
1967 return LLVMPrivateLinkage;
1968 case GlobalValue::ExternalWeakLinkage:
1969 return LLVMExternalWeakLinkage;
1970 case GlobalValue::CommonLinkage:
1971 return LLVMCommonLinkage;
1974 llvm_unreachable("Invalid GlobalValue linkage!");
1977 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1978 GlobalValue *GV = unwrap<GlobalValue>(Global);
1980 switch (Linkage) {
1981 case LLVMExternalLinkage:
1982 GV->setLinkage(GlobalValue::ExternalLinkage);
1983 break;
1984 case LLVMAvailableExternallyLinkage:
1985 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1986 break;
1987 case LLVMLinkOnceAnyLinkage:
1988 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1989 break;
1990 case LLVMLinkOnceODRLinkage:
1991 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1992 break;
1993 case LLVMLinkOnceODRAutoHideLinkage:
1994 LLVM_DEBUG(
1995 errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1996 "longer supported.");
1997 break;
1998 case LLVMWeakAnyLinkage:
1999 GV->setLinkage(GlobalValue::WeakAnyLinkage);
2000 break;
2001 case LLVMWeakODRLinkage:
2002 GV->setLinkage(GlobalValue::WeakODRLinkage);
2003 break;
2004 case LLVMAppendingLinkage:
2005 GV->setLinkage(GlobalValue::AppendingLinkage);
2006 break;
2007 case LLVMInternalLinkage:
2008 GV->setLinkage(GlobalValue::InternalLinkage);
2009 break;
2010 case LLVMPrivateLinkage:
2011 GV->setLinkage(GlobalValue::PrivateLinkage);
2012 break;
2013 case LLVMLinkerPrivateLinkage:
2014 GV->setLinkage(GlobalValue::PrivateLinkage);
2015 break;
2016 case LLVMLinkerPrivateWeakLinkage:
2017 GV->setLinkage(GlobalValue::PrivateLinkage);
2018 break;
2019 case LLVMDLLImportLinkage:
2020 LLVM_DEBUG(
2021 errs()
2022 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
2023 break;
2024 case LLVMDLLExportLinkage:
2025 LLVM_DEBUG(
2026 errs()
2027 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
2028 break;
2029 case LLVMExternalWeakLinkage:
2030 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
2031 break;
2032 case LLVMGhostLinkage:
2033 LLVM_DEBUG(
2034 errs() << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
2035 break;
2036 case LLVMCommonLinkage:
2037 GV->setLinkage(GlobalValue::CommonLinkage);
2038 break;
2042 const char *LLVMGetSection(LLVMValueRef Global) {
2043 // Using .data() is safe because of how GlobalObject::setSection is
2044 // implemented.
2045 return unwrap<GlobalValue>(Global)->getSection().data();
2048 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
2049 unwrap<GlobalObject>(Global)->setSection(Section);
2052 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
2053 return static_cast<LLVMVisibility>(
2054 unwrap<GlobalValue>(Global)->getVisibility());
2057 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
2058 unwrap<GlobalValue>(Global)
2059 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
2062 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
2063 return static_cast<LLVMDLLStorageClass>(
2064 unwrap<GlobalValue>(Global)->getDLLStorageClass());
2067 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
2068 unwrap<GlobalValue>(Global)->setDLLStorageClass(
2069 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
2072 LLVMUnnamedAddr LLVMGetUnnamedAddress(LLVMValueRef Global) {
2073 switch (unwrap<GlobalValue>(Global)->getUnnamedAddr()) {
2074 case GlobalVariable::UnnamedAddr::None:
2075 return LLVMNoUnnamedAddr;
2076 case GlobalVariable::UnnamedAddr::Local:
2077 return LLVMLocalUnnamedAddr;
2078 case GlobalVariable::UnnamedAddr::Global:
2079 return LLVMGlobalUnnamedAddr;
2081 llvm_unreachable("Unknown UnnamedAddr kind!");
2084 void LLVMSetUnnamedAddress(LLVMValueRef Global, LLVMUnnamedAddr UnnamedAddr) {
2085 GlobalValue *GV = unwrap<GlobalValue>(Global);
2087 switch (UnnamedAddr) {
2088 case LLVMNoUnnamedAddr:
2089 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::None);
2090 case LLVMLocalUnnamedAddr:
2091 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Local);
2092 case LLVMGlobalUnnamedAddr:
2093 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Global);
2097 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
2098 return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
2101 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
2102 unwrap<GlobalValue>(Global)->setUnnamedAddr(
2103 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
2104 : GlobalValue::UnnamedAddr::None);
2107 LLVMTypeRef LLVMGlobalGetValueType(LLVMValueRef Global) {
2108 return wrap(unwrap<GlobalValue>(Global)->getValueType());
2111 /*--.. Operations on global variables, load and store instructions .........--*/
2113 unsigned LLVMGetAlignment(LLVMValueRef V) {
2114 Value *P = unwrap(V);
2115 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2116 return GV->getAlign() ? GV->getAlign()->value() : 0;
2117 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
2118 return AI->getAlign().value();
2119 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2120 return LI->getAlign().value();
2121 if (StoreInst *SI = dyn_cast<StoreInst>(P))
2122 return SI->getAlign().value();
2123 if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2124 return RMWI->getAlign().value();
2125 if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2126 return CXI->getAlign().value();
2128 llvm_unreachable(
2129 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, "
2130 "and AtomicCmpXchgInst have alignment");
2133 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
2134 Value *P = unwrap(V);
2135 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2136 GV->setAlignment(MaybeAlign(Bytes));
2137 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
2138 AI->setAlignment(Align(Bytes));
2139 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
2140 LI->setAlignment(Align(Bytes));
2141 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
2142 SI->setAlignment(Align(Bytes));
2143 else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2144 RMWI->setAlignment(Align(Bytes));
2145 else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2146 CXI->setAlignment(Align(Bytes));
2147 else
2148 llvm_unreachable(
2149 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, and "
2150 "and AtomicCmpXchgInst have alignment");
2153 LLVMValueMetadataEntry *LLVMGlobalCopyAllMetadata(LLVMValueRef Value,
2154 size_t *NumEntries) {
2155 return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
2156 Entries.clear();
2157 if (Instruction *Instr = dyn_cast<Instruction>(unwrap(Value))) {
2158 Instr->getAllMetadata(Entries);
2159 } else {
2160 unwrap<GlobalObject>(Value)->getAllMetadata(Entries);
2165 unsigned LLVMValueMetadataEntriesGetKind(LLVMValueMetadataEntry *Entries,
2166 unsigned Index) {
2167 LLVMOpaqueValueMetadataEntry MVE =
2168 static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2169 return MVE.Kind;
2172 LLVMMetadataRef
2173 LLVMValueMetadataEntriesGetMetadata(LLVMValueMetadataEntry *Entries,
2174 unsigned Index) {
2175 LLVMOpaqueValueMetadataEntry MVE =
2176 static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2177 return MVE.Metadata;
2180 void LLVMDisposeValueMetadataEntries(LLVMValueMetadataEntry *Entries) {
2181 free(Entries);
2184 void LLVMGlobalSetMetadata(LLVMValueRef Global, unsigned Kind,
2185 LLVMMetadataRef MD) {
2186 unwrap<GlobalObject>(Global)->setMetadata(Kind, unwrap<MDNode>(MD));
2189 void LLVMGlobalEraseMetadata(LLVMValueRef Global, unsigned Kind) {
2190 unwrap<GlobalObject>(Global)->eraseMetadata(Kind);
2193 void LLVMGlobalClearMetadata(LLVMValueRef Global) {
2194 unwrap<GlobalObject>(Global)->clearMetadata();
2197 /*--.. Operations on global variables ......................................--*/
2199 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
2200 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2201 GlobalValue::ExternalLinkage, nullptr, Name));
2204 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
2205 const char *Name,
2206 unsigned AddressSpace) {
2207 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2208 GlobalValue::ExternalLinkage, nullptr, Name,
2209 nullptr, GlobalVariable::NotThreadLocal,
2210 AddressSpace));
2213 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
2214 return wrap(unwrap(M)->getNamedGlobal(Name));
2217 LLVMValueRef LLVMGetNamedGlobalWithLength(LLVMModuleRef M, const char *Name,
2218 size_t Length) {
2219 return wrap(unwrap(M)->getNamedGlobal(StringRef(Name, Length)));
2222 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
2223 Module *Mod = unwrap(M);
2224 Module::global_iterator I = Mod->global_begin();
2225 if (I == Mod->global_end())
2226 return nullptr;
2227 return wrap(&*I);
2230 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
2231 Module *Mod = unwrap(M);
2232 Module::global_iterator I = Mod->global_end();
2233 if (I == Mod->global_begin())
2234 return nullptr;
2235 return wrap(&*--I);
2238 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
2239 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2240 Module::global_iterator I(GV);
2241 if (++I == GV->getParent()->global_end())
2242 return nullptr;
2243 return wrap(&*I);
2246 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
2247 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2248 Module::global_iterator I(GV);
2249 if (I == GV->getParent()->global_begin())
2250 return nullptr;
2251 return wrap(&*--I);
2254 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
2255 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
2258 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
2259 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
2260 if ( !GV->hasInitializer() )
2261 return nullptr;
2262 return wrap(GV->getInitializer());
2265 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
2266 unwrap<GlobalVariable>(GlobalVar)->setInitializer(
2267 ConstantVal ? unwrap<Constant>(ConstantVal) : nullptr);
2270 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
2271 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
2274 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
2275 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
2278 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
2279 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
2282 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
2283 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
2286 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
2287 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
2288 case GlobalVariable::NotThreadLocal:
2289 return LLVMNotThreadLocal;
2290 case GlobalVariable::GeneralDynamicTLSModel:
2291 return LLVMGeneralDynamicTLSModel;
2292 case GlobalVariable::LocalDynamicTLSModel:
2293 return LLVMLocalDynamicTLSModel;
2294 case GlobalVariable::InitialExecTLSModel:
2295 return LLVMInitialExecTLSModel;
2296 case GlobalVariable::LocalExecTLSModel:
2297 return LLVMLocalExecTLSModel;
2300 llvm_unreachable("Invalid GlobalVariable thread local mode");
2303 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
2304 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2306 switch (Mode) {
2307 case LLVMNotThreadLocal:
2308 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
2309 break;
2310 case LLVMGeneralDynamicTLSModel:
2311 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
2312 break;
2313 case LLVMLocalDynamicTLSModel:
2314 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
2315 break;
2316 case LLVMInitialExecTLSModel:
2317 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
2318 break;
2319 case LLVMLocalExecTLSModel:
2320 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
2321 break;
2325 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
2326 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
2329 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
2330 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
2333 /*--.. Operations on aliases ......................................--*/
2335 LLVMValueRef LLVMAddAlias2(LLVMModuleRef M, LLVMTypeRef ValueTy,
2336 unsigned AddrSpace, LLVMValueRef Aliasee,
2337 const char *Name) {
2338 return wrap(GlobalAlias::create(unwrap(ValueTy), AddrSpace,
2339 GlobalValue::ExternalLinkage, Name,
2340 unwrap<Constant>(Aliasee), unwrap(M)));
2343 LLVMValueRef LLVMGetNamedGlobalAlias(LLVMModuleRef M,
2344 const char *Name, size_t NameLen) {
2345 return wrap(unwrap(M)->getNamedAlias(StringRef(Name, NameLen)));
2348 LLVMValueRef LLVMGetFirstGlobalAlias(LLVMModuleRef M) {
2349 Module *Mod = unwrap(M);
2350 Module::alias_iterator I = Mod->alias_begin();
2351 if (I == Mod->alias_end())
2352 return nullptr;
2353 return wrap(&*I);
2356 LLVMValueRef LLVMGetLastGlobalAlias(LLVMModuleRef M) {
2357 Module *Mod = unwrap(M);
2358 Module::alias_iterator I = Mod->alias_end();
2359 if (I == Mod->alias_begin())
2360 return nullptr;
2361 return wrap(&*--I);
2364 LLVMValueRef LLVMGetNextGlobalAlias(LLVMValueRef GA) {
2365 GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2366 Module::alias_iterator I(Alias);
2367 if (++I == Alias->getParent()->alias_end())
2368 return nullptr;
2369 return wrap(&*I);
2372 LLVMValueRef LLVMGetPreviousGlobalAlias(LLVMValueRef GA) {
2373 GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2374 Module::alias_iterator I(Alias);
2375 if (I == Alias->getParent()->alias_begin())
2376 return nullptr;
2377 return wrap(&*--I);
2380 LLVMValueRef LLVMAliasGetAliasee(LLVMValueRef Alias) {
2381 return wrap(unwrap<GlobalAlias>(Alias)->getAliasee());
2384 void LLVMAliasSetAliasee(LLVMValueRef Alias, LLVMValueRef Aliasee) {
2385 unwrap<GlobalAlias>(Alias)->setAliasee(unwrap<Constant>(Aliasee));
2388 /*--.. Operations on functions .............................................--*/
2390 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
2391 LLVMTypeRef FunctionTy) {
2392 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
2393 GlobalValue::ExternalLinkage, Name, unwrap(M)));
2396 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
2397 return wrap(unwrap(M)->getFunction(Name));
2400 LLVMValueRef LLVMGetNamedFunctionWithLength(LLVMModuleRef M, const char *Name,
2401 size_t Length) {
2402 return wrap(unwrap(M)->getFunction(StringRef(Name, Length)));
2405 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
2406 Module *Mod = unwrap(M);
2407 Module::iterator I = Mod->begin();
2408 if (I == Mod->end())
2409 return nullptr;
2410 return wrap(&*I);
2413 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
2414 Module *Mod = unwrap(M);
2415 Module::iterator I = Mod->end();
2416 if (I == Mod->begin())
2417 return nullptr;
2418 return wrap(&*--I);
2421 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
2422 Function *Func = unwrap<Function>(Fn);
2423 Module::iterator I(Func);
2424 if (++I == Func->getParent()->end())
2425 return nullptr;
2426 return wrap(&*I);
2429 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
2430 Function *Func = unwrap<Function>(Fn);
2431 Module::iterator I(Func);
2432 if (I == Func->getParent()->begin())
2433 return nullptr;
2434 return wrap(&*--I);
2437 void LLVMDeleteFunction(LLVMValueRef Fn) {
2438 unwrap<Function>(Fn)->eraseFromParent();
2441 LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) {
2442 return unwrap<Function>(Fn)->hasPersonalityFn();
2445 LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) {
2446 return wrap(unwrap<Function>(Fn)->getPersonalityFn());
2449 void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) {
2450 unwrap<Function>(Fn)->setPersonalityFn(
2451 PersonalityFn ? unwrap<Constant>(PersonalityFn) : nullptr);
2454 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
2455 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
2456 return F->getIntrinsicID();
2457 return 0;
2460 static Intrinsic::ID llvm_map_to_intrinsic_id(unsigned ID) {
2461 assert(ID < llvm::Intrinsic::num_intrinsics && "Intrinsic ID out of range");
2462 return llvm::Intrinsic::ID(ID);
2465 LLVMValueRef LLVMGetIntrinsicDeclaration(LLVMModuleRef Mod,
2466 unsigned ID,
2467 LLVMTypeRef *ParamTypes,
2468 size_t ParamCount) {
2469 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2470 auto IID = llvm_map_to_intrinsic_id(ID);
2471 return wrap(llvm::Intrinsic::getOrInsertDeclaration(unwrap(Mod), IID, Tys));
2474 const char *LLVMIntrinsicGetName(unsigned ID, size_t *NameLength) {
2475 auto IID = llvm_map_to_intrinsic_id(ID);
2476 auto Str = llvm::Intrinsic::getName(IID);
2477 *NameLength = Str.size();
2478 return Str.data();
2481 LLVMTypeRef LLVMIntrinsicGetType(LLVMContextRef Ctx, unsigned ID,
2482 LLVMTypeRef *ParamTypes, size_t ParamCount) {
2483 auto IID = llvm_map_to_intrinsic_id(ID);
2484 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2485 return wrap(llvm::Intrinsic::getType(*unwrap(Ctx), IID, Tys));
2488 char *LLVMIntrinsicCopyOverloadedName(unsigned ID, LLVMTypeRef *ParamTypes,
2489 size_t ParamCount, size_t *NameLength) {
2490 auto IID = llvm_map_to_intrinsic_id(ID);
2491 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2492 auto Str = llvm::Intrinsic::getNameNoUnnamedTypes(IID, Tys);
2493 *NameLength = Str.length();
2494 return strdup(Str.c_str());
2497 char *LLVMIntrinsicCopyOverloadedName2(LLVMModuleRef Mod, unsigned ID,
2498 LLVMTypeRef *ParamTypes,
2499 size_t ParamCount, size_t *NameLength) {
2500 auto IID = llvm_map_to_intrinsic_id(ID);
2501 ArrayRef<Type *> Tys(unwrap(ParamTypes), ParamCount);
2502 auto Str = llvm::Intrinsic::getName(IID, Tys, unwrap(Mod));
2503 *NameLength = Str.length();
2504 return strdup(Str.c_str());
2507 unsigned LLVMLookupIntrinsicID(const char *Name, size_t NameLen) {
2508 return Intrinsic::lookupIntrinsicID({Name, NameLen});
2511 LLVMBool LLVMIntrinsicIsOverloaded(unsigned ID) {
2512 auto IID = llvm_map_to_intrinsic_id(ID);
2513 return llvm::Intrinsic::isOverloaded(IID);
2516 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
2517 return unwrap<Function>(Fn)->getCallingConv();
2520 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
2521 return unwrap<Function>(Fn)->setCallingConv(
2522 static_cast<CallingConv::ID>(CC));
2525 const char *LLVMGetGC(LLVMValueRef Fn) {
2526 Function *F = unwrap<Function>(Fn);
2527 return F->hasGC()? F->getGC().c_str() : nullptr;
2530 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
2531 Function *F = unwrap<Function>(Fn);
2532 if (GC)
2533 F->setGC(GC);
2534 else
2535 F->clearGC();
2538 LLVMValueRef LLVMGetPrefixData(LLVMValueRef Fn) {
2539 Function *F = unwrap<Function>(Fn);
2540 return wrap(F->getPrefixData());
2543 LLVMBool LLVMHasPrefixData(LLVMValueRef Fn) {
2544 Function *F = unwrap<Function>(Fn);
2545 return F->hasPrefixData();
2548 void LLVMSetPrefixData(LLVMValueRef Fn, LLVMValueRef prefixData) {
2549 Function *F = unwrap<Function>(Fn);
2550 Constant *prefix = unwrap<Constant>(prefixData);
2551 F->setPrefixData(prefix);
2554 LLVMValueRef LLVMGetPrologueData(LLVMValueRef Fn) {
2555 Function *F = unwrap<Function>(Fn);
2556 return wrap(F->getPrologueData());
2559 LLVMBool LLVMHasPrologueData(LLVMValueRef Fn) {
2560 Function *F = unwrap<Function>(Fn);
2561 return F->hasPrologueData();
2564 void LLVMSetPrologueData(LLVMValueRef Fn, LLVMValueRef prologueData) {
2565 Function *F = unwrap<Function>(Fn);
2566 Constant *prologue = unwrap<Constant>(prologueData);
2567 F->setPrologueData(prologue);
2570 void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2571 LLVMAttributeRef A) {
2572 unwrap<Function>(F)->addAttributeAtIndex(Idx, unwrap(A));
2575 unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) {
2576 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2577 return AS.getNumAttributes();
2580 void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2581 LLVMAttributeRef *Attrs) {
2582 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2583 for (auto A : AS)
2584 *Attrs++ = wrap(A);
2587 LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F,
2588 LLVMAttributeIndex Idx,
2589 unsigned KindID) {
2590 return wrap(unwrap<Function>(F)->getAttributeAtIndex(
2591 Idx, (Attribute::AttrKind)KindID));
2594 LLVMAttributeRef LLVMGetStringAttributeAtIndex(LLVMValueRef F,
2595 LLVMAttributeIndex Idx,
2596 const char *K, unsigned KLen) {
2597 return wrap(
2598 unwrap<Function>(F)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
2601 void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2602 unsigned KindID) {
2603 unwrap<Function>(F)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
2606 void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2607 const char *K, unsigned KLen) {
2608 unwrap<Function>(F)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
2611 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
2612 const char *V) {
2613 Function *Func = unwrap<Function>(Fn);
2614 Attribute Attr = Attribute::get(Func->getContext(), A, V);
2615 Func->addFnAttr(Attr);
2618 /*--.. Operations on parameters ............................................--*/
2620 unsigned LLVMCountParams(LLVMValueRef FnRef) {
2621 // This function is strictly redundant to
2622 // LLVMCountParamTypes(LLVMGlobalGetValueType(FnRef))
2623 return unwrap<Function>(FnRef)->arg_size();
2626 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
2627 Function *Fn = unwrap<Function>(FnRef);
2628 for (Argument &A : Fn->args())
2629 *ParamRefs++ = wrap(&A);
2632 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
2633 Function *Fn = unwrap<Function>(FnRef);
2634 return wrap(&Fn->arg_begin()[index]);
2637 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
2638 return wrap(unwrap<Argument>(V)->getParent());
2641 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
2642 Function *Func = unwrap<Function>(Fn);
2643 Function::arg_iterator I = Func->arg_begin();
2644 if (I == Func->arg_end())
2645 return nullptr;
2646 return wrap(&*I);
2649 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
2650 Function *Func = unwrap<Function>(Fn);
2651 Function::arg_iterator I = Func->arg_end();
2652 if (I == Func->arg_begin())
2653 return nullptr;
2654 return wrap(&*--I);
2657 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
2658 Argument *A = unwrap<Argument>(Arg);
2659 Function *Fn = A->getParent();
2660 if (A->getArgNo() + 1 >= Fn->arg_size())
2661 return nullptr;
2662 return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
2665 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
2666 Argument *A = unwrap<Argument>(Arg);
2667 if (A->getArgNo() == 0)
2668 return nullptr;
2669 return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
2672 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
2673 Argument *A = unwrap<Argument>(Arg);
2674 A->addAttr(Attribute::getWithAlignment(A->getContext(), Align(align)));
2677 /*--.. Operations on ifuncs ................................................--*/
2679 LLVMValueRef LLVMAddGlobalIFunc(LLVMModuleRef M,
2680 const char *Name, size_t NameLen,
2681 LLVMTypeRef Ty, unsigned AddrSpace,
2682 LLVMValueRef Resolver) {
2683 return wrap(GlobalIFunc::create(unwrap(Ty), AddrSpace,
2684 GlobalValue::ExternalLinkage,
2685 StringRef(Name, NameLen),
2686 unwrap<Constant>(Resolver), unwrap(M)));
2689 LLVMValueRef LLVMGetNamedGlobalIFunc(LLVMModuleRef M,
2690 const char *Name, size_t NameLen) {
2691 return wrap(unwrap(M)->getNamedIFunc(StringRef(Name, NameLen)));
2694 LLVMValueRef LLVMGetFirstGlobalIFunc(LLVMModuleRef M) {
2695 Module *Mod = unwrap(M);
2696 Module::ifunc_iterator I = Mod->ifunc_begin();
2697 if (I == Mod->ifunc_end())
2698 return nullptr;
2699 return wrap(&*I);
2702 LLVMValueRef LLVMGetLastGlobalIFunc(LLVMModuleRef M) {
2703 Module *Mod = unwrap(M);
2704 Module::ifunc_iterator I = Mod->ifunc_end();
2705 if (I == Mod->ifunc_begin())
2706 return nullptr;
2707 return wrap(&*--I);
2710 LLVMValueRef LLVMGetNextGlobalIFunc(LLVMValueRef IFunc) {
2711 GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2712 Module::ifunc_iterator I(GIF);
2713 if (++I == GIF->getParent()->ifunc_end())
2714 return nullptr;
2715 return wrap(&*I);
2718 LLVMValueRef LLVMGetPreviousGlobalIFunc(LLVMValueRef IFunc) {
2719 GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2720 Module::ifunc_iterator I(GIF);
2721 if (I == GIF->getParent()->ifunc_begin())
2722 return nullptr;
2723 return wrap(&*--I);
2726 LLVMValueRef LLVMGetGlobalIFuncResolver(LLVMValueRef IFunc) {
2727 return wrap(unwrap<GlobalIFunc>(IFunc)->getResolver());
2730 void LLVMSetGlobalIFuncResolver(LLVMValueRef IFunc, LLVMValueRef Resolver) {
2731 unwrap<GlobalIFunc>(IFunc)->setResolver(unwrap<Constant>(Resolver));
2734 void LLVMEraseGlobalIFunc(LLVMValueRef IFunc) {
2735 unwrap<GlobalIFunc>(IFunc)->eraseFromParent();
2738 void LLVMRemoveGlobalIFunc(LLVMValueRef IFunc) {
2739 unwrap<GlobalIFunc>(IFunc)->removeFromParent();
2742 /*--.. Operations on operand bundles........................................--*/
2744 LLVMOperandBundleRef LLVMCreateOperandBundle(const char *Tag, size_t TagLen,
2745 LLVMValueRef *Args,
2746 unsigned NumArgs) {
2747 return wrap(new OperandBundleDef(std::string(Tag, TagLen),
2748 ArrayRef(unwrap(Args), NumArgs)));
2751 void LLVMDisposeOperandBundle(LLVMOperandBundleRef Bundle) {
2752 delete unwrap(Bundle);
2755 const char *LLVMGetOperandBundleTag(LLVMOperandBundleRef Bundle, size_t *Len) {
2756 StringRef Str = unwrap(Bundle)->getTag();
2757 *Len = Str.size();
2758 return Str.data();
2761 unsigned LLVMGetNumOperandBundleArgs(LLVMOperandBundleRef Bundle) {
2762 return unwrap(Bundle)->inputs().size();
2765 LLVMValueRef LLVMGetOperandBundleArgAtIndex(LLVMOperandBundleRef Bundle,
2766 unsigned Index) {
2767 return wrap(unwrap(Bundle)->inputs()[Index]);
2770 /*--.. Operations on basic blocks ..........................................--*/
2772 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
2773 return wrap(static_cast<Value*>(unwrap(BB)));
2776 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
2777 return isa<BasicBlock>(unwrap(Val));
2780 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
2781 return wrap(unwrap<BasicBlock>(Val));
2784 const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) {
2785 return unwrap(BB)->getName().data();
2788 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
2789 return wrap(unwrap(BB)->getParent());
2792 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
2793 return wrap(unwrap(BB)->getTerminator());
2796 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
2797 return unwrap<Function>(FnRef)->size();
2800 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
2801 Function *Fn = unwrap<Function>(FnRef);
2802 for (BasicBlock &BB : *Fn)
2803 *BasicBlocksRefs++ = wrap(&BB);
2806 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
2807 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2810 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
2811 Function *Func = unwrap<Function>(Fn);
2812 Function::iterator I = Func->begin();
2813 if (I == Func->end())
2814 return nullptr;
2815 return wrap(&*I);
2818 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
2819 Function *Func = unwrap<Function>(Fn);
2820 Function::iterator I = Func->end();
2821 if (I == Func->begin())
2822 return nullptr;
2823 return wrap(&*--I);
2826 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
2827 BasicBlock *Block = unwrap(BB);
2828 Function::iterator I(Block);
2829 if (++I == Block->getParent()->end())
2830 return nullptr;
2831 return wrap(&*I);
2834 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
2835 BasicBlock *Block = unwrap(BB);
2836 Function::iterator I(Block);
2837 if (I == Block->getParent()->begin())
2838 return nullptr;
2839 return wrap(&*--I);
2842 LLVMBasicBlockRef LLVMCreateBasicBlockInContext(LLVMContextRef C,
2843 const char *Name) {
2844 return wrap(llvm::BasicBlock::Create(*unwrap(C), Name));
2847 void LLVMInsertExistingBasicBlockAfterInsertBlock(LLVMBuilderRef Builder,
2848 LLVMBasicBlockRef BB) {
2849 BasicBlock *ToInsert = unwrap(BB);
2850 BasicBlock *CurBB = unwrap(Builder)->GetInsertBlock();
2851 assert(CurBB && "current insertion point is invalid!");
2852 CurBB->getParent()->insert(std::next(CurBB->getIterator()), ToInsert);
2855 void LLVMAppendExistingBasicBlock(LLVMValueRef Fn,
2856 LLVMBasicBlockRef BB) {
2857 unwrap<Function>(Fn)->insert(unwrap<Function>(Fn)->end(), unwrap(BB));
2860 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
2861 LLVMValueRef FnRef,
2862 const char *Name) {
2863 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
2866 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
2867 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
2870 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
2871 LLVMBasicBlockRef BBRef,
2872 const char *Name) {
2873 BasicBlock *BB = unwrap(BBRef);
2874 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2877 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
2878 const char *Name) {
2879 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
2882 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
2883 unwrap(BBRef)->eraseFromParent();
2886 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
2887 unwrap(BBRef)->removeFromParent();
2890 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2891 unwrap(BB)->moveBefore(unwrap(MovePos));
2894 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2895 unwrap(BB)->moveAfter(unwrap(MovePos));
2898 /*--.. Operations on instructions ..........................................--*/
2900 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
2901 return wrap(unwrap<Instruction>(Inst)->getParent());
2904 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
2905 BasicBlock *Block = unwrap(BB);
2906 BasicBlock::iterator I = Block->begin();
2907 if (I == Block->end())
2908 return nullptr;
2909 return wrap(&*I);
2912 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
2913 BasicBlock *Block = unwrap(BB);
2914 BasicBlock::iterator I = Block->end();
2915 if (I == Block->begin())
2916 return nullptr;
2917 return wrap(&*--I);
2920 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
2921 Instruction *Instr = unwrap<Instruction>(Inst);
2922 BasicBlock::iterator I(Instr);
2923 if (++I == Instr->getParent()->end())
2924 return nullptr;
2925 return wrap(&*I);
2928 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
2929 Instruction *Instr = unwrap<Instruction>(Inst);
2930 BasicBlock::iterator I(Instr);
2931 if (I == Instr->getParent()->begin())
2932 return nullptr;
2933 return wrap(&*--I);
2936 void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) {
2937 unwrap<Instruction>(Inst)->removeFromParent();
2940 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
2941 unwrap<Instruction>(Inst)->eraseFromParent();
2944 void LLVMDeleteInstruction(LLVMValueRef Inst) {
2945 unwrap<Instruction>(Inst)->deleteValue();
2948 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
2949 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2950 return (LLVMIntPredicate)I->getPredicate();
2951 return (LLVMIntPredicate)0;
2954 LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
2955 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2956 return (LLVMRealPredicate)I->getPredicate();
2957 return (LLVMRealPredicate)0;
2960 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
2961 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2962 return map_to_llvmopcode(C->getOpcode());
2963 return (LLVMOpcode)0;
2966 LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {
2967 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2968 return wrap(C->clone());
2969 return nullptr;
2972 LLVMValueRef LLVMIsATerminatorInst(LLVMValueRef Inst) {
2973 Instruction *I = dyn_cast<Instruction>(unwrap(Inst));
2974 return (I && I->isTerminator()) ? wrap(I) : nullptr;
2977 LLVMDbgRecordRef LLVMGetFirstDbgRecord(LLVMValueRef Inst) {
2978 Instruction *Instr = unwrap<Instruction>(Inst);
2979 auto I = Instr->DebugMarker->StoredDbgRecords.begin();
2980 if (I == Instr->DebugMarker->StoredDbgRecords.end())
2981 return nullptr;
2982 return wrap(&*I);
2985 LLVMDbgRecordRef LLVMGetLastDbgRecord(LLVMValueRef Inst) {
2986 Instruction *Instr = unwrap<Instruction>(Inst);
2987 auto I = Instr->DebugMarker->StoredDbgRecords.rbegin();
2988 if (I == Instr->DebugMarker->StoredDbgRecords.rend())
2989 return nullptr;
2990 return wrap(&*I);
2993 LLVMDbgRecordRef LLVMGetNextDbgRecord(LLVMDbgRecordRef Rec) {
2994 DbgRecord *Record = unwrap<DbgRecord>(Rec);
2995 simple_ilist<DbgRecord>::iterator I(Record);
2996 if (++I == Record->getInstruction()->DebugMarker->StoredDbgRecords.end())
2997 return nullptr;
2998 return wrap(&*I);
3001 LLVMDbgRecordRef LLVMGetPreviousDbgRecord(LLVMDbgRecordRef Rec) {
3002 DbgRecord *Record = unwrap<DbgRecord>(Rec);
3003 simple_ilist<DbgRecord>::iterator I(Record);
3004 if (I == Record->getInstruction()->DebugMarker->StoredDbgRecords.begin())
3005 return nullptr;
3006 return wrap(&*--I);
3009 unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) {
3010 if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) {
3011 return FPI->arg_size();
3013 return unwrap<CallBase>(Instr)->arg_size();
3016 /*--.. Call and invoke instructions ........................................--*/
3018 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
3019 return unwrap<CallBase>(Instr)->getCallingConv();
3022 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
3023 return unwrap<CallBase>(Instr)->setCallingConv(
3024 static_cast<CallingConv::ID>(CC));
3027 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, LLVMAttributeIndex Idx,
3028 unsigned align) {
3029 auto *Call = unwrap<CallBase>(Instr);
3030 Attribute AlignAttr =
3031 Attribute::getWithAlignment(Call->getContext(), Align(align));
3032 Call->addAttributeAtIndex(Idx, AlignAttr);
3035 void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
3036 LLVMAttributeRef A) {
3037 unwrap<CallBase>(C)->addAttributeAtIndex(Idx, unwrap(A));
3040 unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C,
3041 LLVMAttributeIndex Idx) {
3042 auto *Call = unwrap<CallBase>(C);
3043 auto AS = Call->getAttributes().getAttributes(Idx);
3044 return AS.getNumAttributes();
3047 void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx,
3048 LLVMAttributeRef *Attrs) {
3049 auto *Call = unwrap<CallBase>(C);
3050 auto AS = Call->getAttributes().getAttributes(Idx);
3051 for (auto A : AS)
3052 *Attrs++ = wrap(A);
3055 LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C,
3056 LLVMAttributeIndex Idx,
3057 unsigned KindID) {
3058 return wrap(unwrap<CallBase>(C)->getAttributeAtIndex(
3059 Idx, (Attribute::AttrKind)KindID));
3062 LLVMAttributeRef LLVMGetCallSiteStringAttribute(LLVMValueRef C,
3063 LLVMAttributeIndex Idx,
3064 const char *K, unsigned KLen) {
3065 return wrap(
3066 unwrap<CallBase>(C)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
3069 void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
3070 unsigned KindID) {
3071 unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
3074 void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
3075 const char *K, unsigned KLen) {
3076 unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
3079 LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) {
3080 return wrap(unwrap<CallBase>(Instr)->getCalledOperand());
3083 LLVMTypeRef LLVMGetCalledFunctionType(LLVMValueRef Instr) {
3084 return wrap(unwrap<CallBase>(Instr)->getFunctionType());
3087 unsigned LLVMGetNumOperandBundles(LLVMValueRef C) {
3088 return unwrap<CallBase>(C)->getNumOperandBundles();
3091 LLVMOperandBundleRef LLVMGetOperandBundleAtIndex(LLVMValueRef C,
3092 unsigned Index) {
3093 return wrap(
3094 new OperandBundleDef(unwrap<CallBase>(C)->getOperandBundleAt(Index)));
3097 /*--.. Operations on call instructions (only) ..............................--*/
3099 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
3100 return unwrap<CallInst>(Call)->isTailCall();
3103 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
3104 unwrap<CallInst>(Call)->setTailCall(isTailCall);
3107 LLVMTailCallKind LLVMGetTailCallKind(LLVMValueRef Call) {
3108 return (LLVMTailCallKind)unwrap<CallInst>(Call)->getTailCallKind();
3111 void LLVMSetTailCallKind(LLVMValueRef Call, LLVMTailCallKind kind) {
3112 unwrap<CallInst>(Call)->setTailCallKind((CallInst::TailCallKind)kind);
3115 /*--.. Operations on invoke instructions (only) ............................--*/
3117 LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) {
3118 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
3121 LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) {
3122 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
3123 return wrap(CRI->getUnwindDest());
3124 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
3125 return wrap(CSI->getUnwindDest());
3127 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
3130 void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
3131 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
3134 void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
3135 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
3136 return CRI->setUnwindDest(unwrap(B));
3137 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
3138 return CSI->setUnwindDest(unwrap(B));
3140 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
3143 LLVMBasicBlockRef LLVMGetCallBrDefaultDest(LLVMValueRef CallBr) {
3144 return wrap(unwrap<CallBrInst>(CallBr)->getDefaultDest());
3147 unsigned LLVMGetCallBrNumIndirectDests(LLVMValueRef CallBr) {
3148 return unwrap<CallBrInst>(CallBr)->getNumIndirectDests();
3151 LLVMBasicBlockRef LLVMGetCallBrIndirectDest(LLVMValueRef CallBr, unsigned Idx) {
3152 return wrap(unwrap<CallBrInst>(CallBr)->getIndirectDest(Idx));
3155 /*--.. Operations on terminators ...........................................--*/
3157 unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {
3158 return unwrap<Instruction>(Term)->getNumSuccessors();
3161 LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) {
3162 return wrap(unwrap<Instruction>(Term)->getSuccessor(i));
3165 void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
3166 return unwrap<Instruction>(Term)->setSuccessor(i, unwrap(block));
3169 /*--.. Operations on branch instructions (only) ............................--*/
3171 LLVMBool LLVMIsConditional(LLVMValueRef Branch) {
3172 return unwrap<BranchInst>(Branch)->isConditional();
3175 LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) {
3176 return wrap(unwrap<BranchInst>(Branch)->getCondition());
3179 void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) {
3180 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
3183 /*--.. Operations on switch instructions (only) ............................--*/
3185 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
3186 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
3189 /*--.. Operations on alloca instructions (only) ............................--*/
3191 LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) {
3192 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
3195 /*--.. Operations on gep instructions (only) ...............................--*/
3197 LLVMBool LLVMIsInBounds(LLVMValueRef GEP) {
3198 return unwrap<GEPOperator>(GEP)->isInBounds();
3201 void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) {
3202 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
3205 LLVMTypeRef LLVMGetGEPSourceElementType(LLVMValueRef GEP) {
3206 return wrap(unwrap<GEPOperator>(GEP)->getSourceElementType());
3209 LLVMGEPNoWrapFlags LLVMGEPGetNoWrapFlags(LLVMValueRef GEP) {
3210 GEPOperator *GEPOp = unwrap<GEPOperator>(GEP);
3211 return mapToLLVMGEPNoWrapFlags(GEPOp->getNoWrapFlags());
3214 void LLVMGEPSetNoWrapFlags(LLVMValueRef GEP, LLVMGEPNoWrapFlags NoWrapFlags) {
3215 GetElementPtrInst *GEPInst = unwrap<GetElementPtrInst>(GEP);
3216 GEPInst->setNoWrapFlags(mapFromLLVMGEPNoWrapFlags(NoWrapFlags));
3219 /*--.. Operations on phi nodes .............................................--*/
3221 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
3222 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
3223 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
3224 for (unsigned I = 0; I != Count; ++I)
3225 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
3228 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
3229 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
3232 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
3233 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
3236 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
3237 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
3240 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/
3242 unsigned LLVMGetNumIndices(LLVMValueRef Inst) {
3243 auto *I = unwrap(Inst);
3244 if (auto *GEP = dyn_cast<GEPOperator>(I))
3245 return GEP->getNumIndices();
3246 if (auto *EV = dyn_cast<ExtractValueInst>(I))
3247 return EV->getNumIndices();
3248 if (auto *IV = dyn_cast<InsertValueInst>(I))
3249 return IV->getNumIndices();
3250 llvm_unreachable(
3251 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
3254 const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
3255 auto *I = unwrap(Inst);
3256 if (auto *EV = dyn_cast<ExtractValueInst>(I))
3257 return EV->getIndices().data();
3258 if (auto *IV = dyn_cast<InsertValueInst>(I))
3259 return IV->getIndices().data();
3260 llvm_unreachable(
3261 "LLVMGetIndices applies only to extractvalue and insertvalue!");
3265 /*===-- Instruction builders ----------------------------------------------===*/
3267 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
3268 return wrap(new IRBuilder<>(*unwrap(C)));
3271 LLVMBuilderRef LLVMCreateBuilder(void) {
3272 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
3275 static void LLVMPositionBuilderImpl(IRBuilder<> *Builder, BasicBlock *Block,
3276 Instruction *Instr, bool BeforeDbgRecords) {
3277 BasicBlock::iterator I = Instr ? Instr->getIterator() : Block->end();
3278 I.setHeadBit(BeforeDbgRecords);
3279 Builder->SetInsertPoint(Block, I);
3282 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
3283 LLVMValueRef Instr) {
3284 return LLVMPositionBuilderImpl(unwrap(Builder), unwrap(Block),
3285 unwrap<Instruction>(Instr), false);
3288 void LLVMPositionBuilderBeforeDbgRecords(LLVMBuilderRef Builder,
3289 LLVMBasicBlockRef Block,
3290 LLVMValueRef Instr) {
3291 return LLVMPositionBuilderImpl(unwrap(Builder), unwrap(Block),
3292 unwrap<Instruction>(Instr), true);
3295 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
3296 Instruction *I = unwrap<Instruction>(Instr);
3297 return LLVMPositionBuilderImpl(unwrap(Builder), I->getParent(), I, false);
3300 void LLVMPositionBuilderBeforeInstrAndDbgRecords(LLVMBuilderRef Builder,
3301 LLVMValueRef Instr) {
3302 Instruction *I = unwrap<Instruction>(Instr);
3303 return LLVMPositionBuilderImpl(unwrap(Builder), I->getParent(), I, true);
3306 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
3307 BasicBlock *BB = unwrap(Block);
3308 unwrap(Builder)->SetInsertPoint(BB);
3311 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
3312 return wrap(unwrap(Builder)->GetInsertBlock());
3315 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
3316 unwrap(Builder)->ClearInsertionPoint();
3319 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
3320 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
3323 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
3324 const char *Name) {
3325 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
3328 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
3329 delete unwrap(Builder);
3332 /*--.. Metadata builders ...................................................--*/
3334 LLVMMetadataRef LLVMGetCurrentDebugLocation2(LLVMBuilderRef Builder) {
3335 return wrap(unwrap(Builder)->getCurrentDebugLocation().getAsMDNode());
3338 void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Builder, LLVMMetadataRef Loc) {
3339 if (Loc)
3340 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(unwrap<MDNode>(Loc)));
3341 else
3342 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc());
3345 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
3346 MDNode *Loc =
3347 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
3348 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
3351 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
3352 LLVMContext &Context = unwrap(Builder)->getContext();
3353 return wrap(MetadataAsValue::get(
3354 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
3357 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
3358 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
3361 void LLVMAddMetadataToInst(LLVMBuilderRef Builder, LLVMValueRef Inst) {
3362 unwrap(Builder)->AddMetadataToInst(unwrap<Instruction>(Inst));
3365 void LLVMBuilderSetDefaultFPMathTag(LLVMBuilderRef Builder,
3366 LLVMMetadataRef FPMathTag) {
3368 unwrap(Builder)->setDefaultFPMathTag(FPMathTag
3369 ? unwrap<MDNode>(FPMathTag)
3370 : nullptr);
3373 LLVMContextRef LLVMGetBuilderContext(LLVMBuilderRef Builder) {
3374 return wrap(&unwrap(Builder)->getContext());
3377 LLVMMetadataRef LLVMBuilderGetDefaultFPMathTag(LLVMBuilderRef Builder) {
3378 return wrap(unwrap(Builder)->getDefaultFPMathTag());
3381 /*--.. Instruction builders ................................................--*/
3383 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
3384 return wrap(unwrap(B)->CreateRetVoid());
3387 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
3388 return wrap(unwrap(B)->CreateRet(unwrap(V)));
3391 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
3392 unsigned N) {
3393 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
3396 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
3397 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
3400 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
3401 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
3402 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
3405 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
3406 LLVMBasicBlockRef Else, unsigned NumCases) {
3407 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
3410 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
3411 unsigned NumDests) {
3412 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
3415 LLVMValueRef LLVMBuildCallBr(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn,
3416 LLVMBasicBlockRef DefaultDest,
3417 LLVMBasicBlockRef *IndirectDests,
3418 unsigned NumIndirectDests, LLVMValueRef *Args,
3419 unsigned NumArgs, LLVMOperandBundleRef *Bundles,
3420 unsigned NumBundles, const char *Name) {
3422 SmallVector<OperandBundleDef, 8> OBs;
3423 for (auto *Bundle : ArrayRef(Bundles, NumBundles)) {
3424 OperandBundleDef *OB = unwrap(Bundle);
3425 OBs.push_back(*OB);
3428 return wrap(unwrap(B)->CreateCallBr(
3429 unwrap<FunctionType>(Ty), unwrap(Fn), unwrap(DefaultDest),
3430 ArrayRef(unwrap(IndirectDests), NumIndirectDests),
3431 ArrayRef<Value *>(unwrap(Args), NumArgs), OBs, Name));
3434 LLVMValueRef LLVMBuildInvoke2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn,
3435 LLVMValueRef *Args, unsigned NumArgs,
3436 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
3437 const char *Name) {
3438 return wrap(unwrap(B)->CreateInvoke(unwrap<FunctionType>(Ty), unwrap(Fn),
3439 unwrap(Then), unwrap(Catch),
3440 ArrayRef(unwrap(Args), NumArgs), Name));
3443 LLVMValueRef LLVMBuildInvokeWithOperandBundles(
3444 LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn, LLVMValueRef *Args,
3445 unsigned NumArgs, LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
3446 LLVMOperandBundleRef *Bundles, unsigned NumBundles, const char *Name) {
3447 SmallVector<OperandBundleDef, 8> OBs;
3448 for (auto *Bundle : ArrayRef(Bundles, NumBundles)) {
3449 OperandBundleDef *OB = unwrap(Bundle);
3450 OBs.push_back(*OB);
3452 return wrap(unwrap(B)->CreateInvoke(
3453 unwrap<FunctionType>(Ty), unwrap(Fn), unwrap(Then), unwrap(Catch),
3454 ArrayRef(unwrap(Args), NumArgs), OBs, Name));
3457 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
3458 LLVMValueRef PersFn, unsigned NumClauses,
3459 const char *Name) {
3460 // The personality used to live on the landingpad instruction, but now it
3461 // lives on the parent function. For compatibility, take the provided
3462 // personality and put it on the parent function.
3463 if (PersFn)
3464 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
3465 unwrap<Function>(PersFn));
3466 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
3469 LLVMValueRef LLVMBuildCatchPad(LLVMBuilderRef B, LLVMValueRef ParentPad,
3470 LLVMValueRef *Args, unsigned NumArgs,
3471 const char *Name) {
3472 return wrap(unwrap(B)->CreateCatchPad(unwrap(ParentPad),
3473 ArrayRef(unwrap(Args), NumArgs), Name));
3476 LLVMValueRef LLVMBuildCleanupPad(LLVMBuilderRef B, LLVMValueRef ParentPad,
3477 LLVMValueRef *Args, unsigned NumArgs,
3478 const char *Name) {
3479 if (ParentPad == nullptr) {
3480 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3481 ParentPad = wrap(Constant::getNullValue(Ty));
3483 return wrap(unwrap(B)->CreateCleanupPad(
3484 unwrap(ParentPad), ArrayRef(unwrap(Args), NumArgs), Name));
3487 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
3488 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
3491 LLVMValueRef LLVMBuildCatchSwitch(LLVMBuilderRef B, LLVMValueRef ParentPad,
3492 LLVMBasicBlockRef UnwindBB,
3493 unsigned NumHandlers, const char *Name) {
3494 if (ParentPad == nullptr) {
3495 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3496 ParentPad = wrap(Constant::getNullValue(Ty));
3498 return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(UnwindBB),
3499 NumHandlers, Name));
3502 LLVMValueRef LLVMBuildCatchRet(LLVMBuilderRef B, LLVMValueRef CatchPad,
3503 LLVMBasicBlockRef BB) {
3504 return wrap(unwrap(B)->CreateCatchRet(unwrap<CatchPadInst>(CatchPad),
3505 unwrap(BB)));
3508 LLVMValueRef LLVMBuildCleanupRet(LLVMBuilderRef B, LLVMValueRef CatchPad,
3509 LLVMBasicBlockRef BB) {
3510 return wrap(unwrap(B)->CreateCleanupRet(unwrap<CleanupPadInst>(CatchPad),
3511 unwrap(BB)));
3514 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
3515 return wrap(unwrap(B)->CreateUnreachable());
3518 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
3519 LLVMBasicBlockRef Dest) {
3520 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
3523 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
3524 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
3527 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
3528 return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
3531 LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
3532 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
3535 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
3536 unwrap<LandingPadInst>(LandingPad)->addClause(unwrap<Constant>(ClauseVal));
3539 LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) {
3540 return unwrap<LandingPadInst>(LandingPad)->isCleanup();
3543 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
3544 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
3547 void LLVMAddHandler(LLVMValueRef CatchSwitch, LLVMBasicBlockRef Dest) {
3548 unwrap<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Dest));
3551 unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) {
3552 return unwrap<CatchSwitchInst>(CatchSwitch)->getNumHandlers();
3555 void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) {
3556 CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(CatchSwitch);
3557 for (const BasicBlock *H : CSI->handlers())
3558 *Handlers++ = wrap(H);
3561 LLVMValueRef LLVMGetParentCatchSwitch(LLVMValueRef CatchPad) {
3562 return wrap(unwrap<CatchPadInst>(CatchPad)->getCatchSwitch());
3565 void LLVMSetParentCatchSwitch(LLVMValueRef CatchPad, LLVMValueRef CatchSwitch) {
3566 unwrap<CatchPadInst>(CatchPad)
3567 ->setCatchSwitch(unwrap<CatchSwitchInst>(CatchSwitch));
3570 /*--.. Funclets ...........................................................--*/
3572 LLVMValueRef LLVMGetArgOperand(LLVMValueRef Funclet, unsigned i) {
3573 return wrap(unwrap<FuncletPadInst>(Funclet)->getArgOperand(i));
3576 void LLVMSetArgOperand(LLVMValueRef Funclet, unsigned i, LLVMValueRef value) {
3577 unwrap<FuncletPadInst>(Funclet)->setArgOperand(i, unwrap(value));
3580 /*--.. Arithmetic ..........................................................--*/
3582 static FastMathFlags mapFromLLVMFastMathFlags(LLVMFastMathFlags FMF) {
3583 FastMathFlags NewFMF;
3584 NewFMF.setAllowReassoc((FMF & LLVMFastMathAllowReassoc) != 0);
3585 NewFMF.setNoNaNs((FMF & LLVMFastMathNoNaNs) != 0);
3586 NewFMF.setNoInfs((FMF & LLVMFastMathNoInfs) != 0);
3587 NewFMF.setNoSignedZeros((FMF & LLVMFastMathNoSignedZeros) != 0);
3588 NewFMF.setAllowReciprocal((FMF & LLVMFastMathAllowReciprocal) != 0);
3589 NewFMF.setAllowContract((FMF & LLVMFastMathAllowContract) != 0);
3590 NewFMF.setApproxFunc((FMF & LLVMFastMathApproxFunc) != 0);
3592 return NewFMF;
3595 static LLVMFastMathFlags mapToLLVMFastMathFlags(FastMathFlags FMF) {
3596 LLVMFastMathFlags NewFMF = LLVMFastMathNone;
3597 if (FMF.allowReassoc())
3598 NewFMF |= LLVMFastMathAllowReassoc;
3599 if (FMF.noNaNs())
3600 NewFMF |= LLVMFastMathNoNaNs;
3601 if (FMF.noInfs())
3602 NewFMF |= LLVMFastMathNoInfs;
3603 if (FMF.noSignedZeros())
3604 NewFMF |= LLVMFastMathNoSignedZeros;
3605 if (FMF.allowReciprocal())
3606 NewFMF |= LLVMFastMathAllowReciprocal;
3607 if (FMF.allowContract())
3608 NewFMF |= LLVMFastMathAllowContract;
3609 if (FMF.approxFunc())
3610 NewFMF |= LLVMFastMathApproxFunc;
3612 return NewFMF;
3615 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3616 const char *Name) {
3617 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
3620 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3621 const char *Name) {
3622 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
3625 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3626 const char *Name) {
3627 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
3630 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3631 const char *Name) {
3632 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
3635 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3636 const char *Name) {
3637 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
3640 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3641 const char *Name) {
3642 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
3645 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3646 const char *Name) {
3647 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
3650 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3651 const char *Name) {
3652 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
3655 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3656 const char *Name) {
3657 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
3660 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3661 const char *Name) {
3662 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
3665 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3666 const char *Name) {
3667 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
3670 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3671 const char *Name) {
3672 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
3675 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3676 const char *Name) {
3677 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
3680 LLVMValueRef LLVMBuildExactUDiv(LLVMBuilderRef B, LLVMValueRef LHS,
3681 LLVMValueRef RHS, const char *Name) {
3682 return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
3685 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3686 const char *Name) {
3687 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
3690 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
3691 LLVMValueRef RHS, const char *Name) {
3692 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
3695 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3696 const char *Name) {
3697 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
3700 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3701 const char *Name) {
3702 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
3705 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3706 const char *Name) {
3707 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
3710 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3711 const char *Name) {
3712 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
3715 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3716 const char *Name) {
3717 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
3720 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3721 const char *Name) {
3722 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
3725 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3726 const char *Name) {
3727 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
3730 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3731 const char *Name) {
3732 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
3735 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3736 const char *Name) {
3737 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
3740 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
3741 const char *Name) {
3742 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
3745 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
3746 LLVMValueRef LHS, LLVMValueRef RHS,
3747 const char *Name) {
3748 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
3749 unwrap(RHS), Name));
3752 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
3753 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
3756 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
3757 const char *Name) {
3758 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
3761 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
3762 const char *Name) {
3763 Value *Neg = unwrap(B)->CreateNeg(unwrap(V), Name);
3764 if (auto *I = dyn_cast<BinaryOperator>(Neg))
3765 I->setHasNoUnsignedWrap();
3766 return wrap(Neg);
3769 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
3770 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
3773 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
3774 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
3777 LLVMBool LLVMGetNUW(LLVMValueRef ArithInst) {
3778 Value *P = unwrap<Value>(ArithInst);
3779 return cast<Instruction>(P)->hasNoUnsignedWrap();
3782 void LLVMSetNUW(LLVMValueRef ArithInst, LLVMBool HasNUW) {
3783 Value *P = unwrap<Value>(ArithInst);
3784 cast<Instruction>(P)->setHasNoUnsignedWrap(HasNUW);
3787 LLVMBool LLVMGetNSW(LLVMValueRef ArithInst) {
3788 Value *P = unwrap<Value>(ArithInst);
3789 return cast<Instruction>(P)->hasNoSignedWrap();
3792 void LLVMSetNSW(LLVMValueRef ArithInst, LLVMBool HasNSW) {
3793 Value *P = unwrap<Value>(ArithInst);
3794 cast<Instruction>(P)->setHasNoSignedWrap(HasNSW);
3797 LLVMBool LLVMGetExact(LLVMValueRef DivOrShrInst) {
3798 Value *P = unwrap<Value>(DivOrShrInst);
3799 return cast<Instruction>(P)->isExact();
3802 void LLVMSetExact(LLVMValueRef DivOrShrInst, LLVMBool IsExact) {
3803 Value *P = unwrap<Value>(DivOrShrInst);
3804 cast<Instruction>(P)->setIsExact(IsExact);
3807 LLVMBool LLVMGetNNeg(LLVMValueRef NonNegInst) {
3808 Value *P = unwrap<Value>(NonNegInst);
3809 return cast<Instruction>(P)->hasNonNeg();
3812 void LLVMSetNNeg(LLVMValueRef NonNegInst, LLVMBool IsNonNeg) {
3813 Value *P = unwrap<Value>(NonNegInst);
3814 cast<Instruction>(P)->setNonNeg(IsNonNeg);
3817 LLVMFastMathFlags LLVMGetFastMathFlags(LLVMValueRef FPMathInst) {
3818 Value *P = unwrap<Value>(FPMathInst);
3819 FastMathFlags FMF = cast<Instruction>(P)->getFastMathFlags();
3820 return mapToLLVMFastMathFlags(FMF);
3823 void LLVMSetFastMathFlags(LLVMValueRef FPMathInst, LLVMFastMathFlags FMF) {
3824 Value *P = unwrap<Value>(FPMathInst);
3825 cast<Instruction>(P)->setFastMathFlags(mapFromLLVMFastMathFlags(FMF));
3828 LLVMBool LLVMCanValueUseFastMathFlags(LLVMValueRef V) {
3829 Value *Val = unwrap<Value>(V);
3830 return isa<FPMathOperator>(Val);
3833 LLVMBool LLVMGetIsDisjoint(LLVMValueRef Inst) {
3834 Value *P = unwrap<Value>(Inst);
3835 return cast<PossiblyDisjointInst>(P)->isDisjoint();
3838 void LLVMSetIsDisjoint(LLVMValueRef Inst, LLVMBool IsDisjoint) {
3839 Value *P = unwrap<Value>(Inst);
3840 cast<PossiblyDisjointInst>(P)->setIsDisjoint(IsDisjoint);
3843 /*--.. Memory ..............................................................--*/
3845 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
3846 const char *Name) {
3847 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3848 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3849 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3850 return wrap(unwrap(B)->CreateMalloc(ITy, unwrap(Ty), AllocSize, nullptr,
3851 nullptr, Name));
3854 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
3855 LLVMValueRef Val, const char *Name) {
3856 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3857 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3858 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3859 return wrap(unwrap(B)->CreateMalloc(ITy, unwrap(Ty), AllocSize, unwrap(Val),
3860 nullptr, Name));
3863 LLVMValueRef LLVMBuildMemSet(LLVMBuilderRef B, LLVMValueRef Ptr,
3864 LLVMValueRef Val, LLVMValueRef Len,
3865 unsigned Align) {
3866 return wrap(unwrap(B)->CreateMemSet(unwrap(Ptr), unwrap(Val), unwrap(Len),
3867 MaybeAlign(Align)));
3870 LLVMValueRef LLVMBuildMemCpy(LLVMBuilderRef B,
3871 LLVMValueRef Dst, unsigned DstAlign,
3872 LLVMValueRef Src, unsigned SrcAlign,
3873 LLVMValueRef Size) {
3874 return wrap(unwrap(B)->CreateMemCpy(unwrap(Dst), MaybeAlign(DstAlign),
3875 unwrap(Src), MaybeAlign(SrcAlign),
3876 unwrap(Size)));
3879 LLVMValueRef LLVMBuildMemMove(LLVMBuilderRef B,
3880 LLVMValueRef Dst, unsigned DstAlign,
3881 LLVMValueRef Src, unsigned SrcAlign,
3882 LLVMValueRef Size) {
3883 return wrap(unwrap(B)->CreateMemMove(unwrap(Dst), MaybeAlign(DstAlign),
3884 unwrap(Src), MaybeAlign(SrcAlign),
3885 unwrap(Size)));
3888 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
3889 const char *Name) {
3890 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
3893 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
3894 LLVMValueRef Val, const char *Name) {
3895 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
3898 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
3899 return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
3902 LLVMValueRef LLVMBuildLoad2(LLVMBuilderRef B, LLVMTypeRef Ty,
3903 LLVMValueRef PointerVal, const char *Name) {
3904 return wrap(unwrap(B)->CreateLoad(unwrap(Ty), unwrap(PointerVal), Name));
3907 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
3908 LLVMValueRef PointerVal) {
3909 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
3912 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
3913 switch (Ordering) {
3914 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic;
3915 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered;
3916 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic;
3917 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire;
3918 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release;
3919 case LLVMAtomicOrderingAcquireRelease:
3920 return AtomicOrdering::AcquireRelease;
3921 case LLVMAtomicOrderingSequentiallyConsistent:
3922 return AtomicOrdering::SequentiallyConsistent;
3925 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
3928 static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) {
3929 switch (Ordering) {
3930 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic;
3931 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered;
3932 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic;
3933 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire;
3934 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease;
3935 case AtomicOrdering::AcquireRelease:
3936 return LLVMAtomicOrderingAcquireRelease;
3937 case AtomicOrdering::SequentiallyConsistent:
3938 return LLVMAtomicOrderingSequentiallyConsistent;
3941 llvm_unreachable("Invalid AtomicOrdering value!");
3944 static AtomicRMWInst::BinOp mapFromLLVMRMWBinOp(LLVMAtomicRMWBinOp BinOp) {
3945 switch (BinOp) {
3946 case LLVMAtomicRMWBinOpXchg: return AtomicRMWInst::Xchg;
3947 case LLVMAtomicRMWBinOpAdd: return AtomicRMWInst::Add;
3948 case LLVMAtomicRMWBinOpSub: return AtomicRMWInst::Sub;
3949 case LLVMAtomicRMWBinOpAnd: return AtomicRMWInst::And;
3950 case LLVMAtomicRMWBinOpNand: return AtomicRMWInst::Nand;
3951 case LLVMAtomicRMWBinOpOr: return AtomicRMWInst::Or;
3952 case LLVMAtomicRMWBinOpXor: return AtomicRMWInst::Xor;
3953 case LLVMAtomicRMWBinOpMax: return AtomicRMWInst::Max;
3954 case LLVMAtomicRMWBinOpMin: return AtomicRMWInst::Min;
3955 case LLVMAtomicRMWBinOpUMax: return AtomicRMWInst::UMax;
3956 case LLVMAtomicRMWBinOpUMin: return AtomicRMWInst::UMin;
3957 case LLVMAtomicRMWBinOpFAdd: return AtomicRMWInst::FAdd;
3958 case LLVMAtomicRMWBinOpFSub: return AtomicRMWInst::FSub;
3959 case LLVMAtomicRMWBinOpFMax: return AtomicRMWInst::FMax;
3960 case LLVMAtomicRMWBinOpFMin: return AtomicRMWInst::FMin;
3961 case LLVMAtomicRMWBinOpUIncWrap:
3962 return AtomicRMWInst::UIncWrap;
3963 case LLVMAtomicRMWBinOpUDecWrap:
3964 return AtomicRMWInst::UDecWrap;
3965 case LLVMAtomicRMWBinOpUSubCond:
3966 return AtomicRMWInst::USubCond;
3967 case LLVMAtomicRMWBinOpUSubSat:
3968 return AtomicRMWInst::USubSat;
3971 llvm_unreachable("Invalid LLVMAtomicRMWBinOp value!");
3974 static LLVMAtomicRMWBinOp mapToLLVMRMWBinOp(AtomicRMWInst::BinOp BinOp) {
3975 switch (BinOp) {
3976 case AtomicRMWInst::Xchg: return LLVMAtomicRMWBinOpXchg;
3977 case AtomicRMWInst::Add: return LLVMAtomicRMWBinOpAdd;
3978 case AtomicRMWInst::Sub: return LLVMAtomicRMWBinOpSub;
3979 case AtomicRMWInst::And: return LLVMAtomicRMWBinOpAnd;
3980 case AtomicRMWInst::Nand: return LLVMAtomicRMWBinOpNand;
3981 case AtomicRMWInst::Or: return LLVMAtomicRMWBinOpOr;
3982 case AtomicRMWInst::Xor: return LLVMAtomicRMWBinOpXor;
3983 case AtomicRMWInst::Max: return LLVMAtomicRMWBinOpMax;
3984 case AtomicRMWInst::Min: return LLVMAtomicRMWBinOpMin;
3985 case AtomicRMWInst::UMax: return LLVMAtomicRMWBinOpUMax;
3986 case AtomicRMWInst::UMin: return LLVMAtomicRMWBinOpUMin;
3987 case AtomicRMWInst::FAdd: return LLVMAtomicRMWBinOpFAdd;
3988 case AtomicRMWInst::FSub: return LLVMAtomicRMWBinOpFSub;
3989 case AtomicRMWInst::FMax: return LLVMAtomicRMWBinOpFMax;
3990 case AtomicRMWInst::FMin: return LLVMAtomicRMWBinOpFMin;
3991 case AtomicRMWInst::UIncWrap:
3992 return LLVMAtomicRMWBinOpUIncWrap;
3993 case AtomicRMWInst::UDecWrap:
3994 return LLVMAtomicRMWBinOpUDecWrap;
3995 case AtomicRMWInst::USubCond:
3996 return LLVMAtomicRMWBinOpUSubCond;
3997 case AtomicRMWInst::USubSat:
3998 return LLVMAtomicRMWBinOpUSubSat;
3999 default: break;
4002 llvm_unreachable("Invalid AtomicRMWBinOp value!");
4005 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
4006 LLVMBool isSingleThread, const char *Name) {
4007 return wrap(
4008 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
4009 isSingleThread ? SyncScope::SingleThread
4010 : SyncScope::System,
4011 Name));
4014 LLVMValueRef LLVMBuildFenceSyncScope(LLVMBuilderRef B,
4015 LLVMAtomicOrdering Ordering, unsigned SSID,
4016 const char *Name) {
4017 return wrap(
4018 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering), SSID, Name));
4021 LLVMValueRef LLVMBuildGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,
4022 LLVMValueRef Pointer, LLVMValueRef *Indices,
4023 unsigned NumIndices, const char *Name) {
4024 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
4025 return wrap(unwrap(B)->CreateGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
4028 LLVMValueRef LLVMBuildInBoundsGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,
4029 LLVMValueRef Pointer, LLVMValueRef *Indices,
4030 unsigned NumIndices, const char *Name) {
4031 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
4032 return wrap(
4033 unwrap(B)->CreateInBoundsGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
4036 LLVMValueRef LLVMBuildGEPWithNoWrapFlags(LLVMBuilderRef B, LLVMTypeRef Ty,
4037 LLVMValueRef Pointer,
4038 LLVMValueRef *Indices,
4039 unsigned NumIndices, const char *Name,
4040 LLVMGEPNoWrapFlags NoWrapFlags) {
4041 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
4042 return wrap(unwrap(B)->CreateGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name,
4043 mapFromLLVMGEPNoWrapFlags(NoWrapFlags)));
4046 LLVMValueRef LLVMBuildStructGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,
4047 LLVMValueRef Pointer, unsigned Idx,
4048 const char *Name) {
4049 return wrap(
4050 unwrap(B)->CreateStructGEP(unwrap(Ty), unwrap(Pointer), Idx, Name));
4053 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
4054 const char *Name) {
4055 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
4058 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
4059 const char *Name) {
4060 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
4063 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
4064 Value *P = unwrap(MemAccessInst);
4065 if (LoadInst *LI = dyn_cast<LoadInst>(P))
4066 return LI->isVolatile();
4067 if (StoreInst *SI = dyn_cast<StoreInst>(P))
4068 return SI->isVolatile();
4069 if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
4070 return AI->isVolatile();
4071 return cast<AtomicCmpXchgInst>(P)->isVolatile();
4074 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
4075 Value *P = unwrap(MemAccessInst);
4076 if (LoadInst *LI = dyn_cast<LoadInst>(P))
4077 return LI->setVolatile(isVolatile);
4078 if (StoreInst *SI = dyn_cast<StoreInst>(P))
4079 return SI->setVolatile(isVolatile);
4080 if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
4081 return AI->setVolatile(isVolatile);
4082 return cast<AtomicCmpXchgInst>(P)->setVolatile(isVolatile);
4085 LLVMBool LLVMGetWeak(LLVMValueRef CmpXchgInst) {
4086 return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->isWeak();
4089 void LLVMSetWeak(LLVMValueRef CmpXchgInst, LLVMBool isWeak) {
4090 return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->setWeak(isWeak);
4093 LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) {
4094 Value *P = unwrap(MemAccessInst);
4095 AtomicOrdering O;
4096 if (LoadInst *LI = dyn_cast<LoadInst>(P))
4097 O = LI->getOrdering();
4098 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
4099 O = SI->getOrdering();
4100 else if (FenceInst *FI = dyn_cast<FenceInst>(P))
4101 O = FI->getOrdering();
4102 else
4103 O = cast<AtomicRMWInst>(P)->getOrdering();
4104 return mapToLLVMOrdering(O);
4107 void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
4108 Value *P = unwrap(MemAccessInst);
4109 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
4111 if (LoadInst *LI = dyn_cast<LoadInst>(P))
4112 return LI->setOrdering(O);
4113 else if (FenceInst *FI = dyn_cast<FenceInst>(P))
4114 return FI->setOrdering(O);
4115 else if (AtomicRMWInst *ARWI = dyn_cast<AtomicRMWInst>(P))
4116 return ARWI->setOrdering(O);
4117 return cast<StoreInst>(P)->setOrdering(O);
4120 LLVMAtomicRMWBinOp LLVMGetAtomicRMWBinOp(LLVMValueRef Inst) {
4121 return mapToLLVMRMWBinOp(unwrap<AtomicRMWInst>(Inst)->getOperation());
4124 void LLVMSetAtomicRMWBinOp(LLVMValueRef Inst, LLVMAtomicRMWBinOp BinOp) {
4125 unwrap<AtomicRMWInst>(Inst)->setOperation(mapFromLLVMRMWBinOp(BinOp));
4128 /*--.. Casts ...............................................................--*/
4130 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
4131 LLVMTypeRef DestTy, const char *Name) {
4132 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
4135 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
4136 LLVMTypeRef DestTy, const char *Name) {
4137 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
4140 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
4141 LLVMTypeRef DestTy, const char *Name) {
4142 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
4145 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
4146 LLVMTypeRef DestTy, const char *Name) {
4147 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
4150 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
4151 LLVMTypeRef DestTy, const char *Name) {
4152 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
4155 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
4156 LLVMTypeRef DestTy, const char *Name) {
4157 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
4160 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
4161 LLVMTypeRef DestTy, const char *Name) {
4162 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
4165 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
4166 LLVMTypeRef DestTy, const char *Name) {
4167 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
4170 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
4171 LLVMTypeRef DestTy, const char *Name) {
4172 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
4175 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
4176 LLVMTypeRef DestTy, const char *Name) {
4177 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
4180 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
4181 LLVMTypeRef DestTy, const char *Name) {
4182 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
4185 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
4186 LLVMTypeRef DestTy, const char *Name) {
4187 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
4190 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
4191 LLVMTypeRef DestTy, const char *Name) {
4192 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
4195 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
4196 LLVMTypeRef DestTy, const char *Name) {
4197 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
4198 Name));
4201 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
4202 LLVMTypeRef DestTy, const char *Name) {
4203 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
4204 Name));
4207 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
4208 LLVMTypeRef DestTy, const char *Name) {
4209 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
4210 Name));
4213 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
4214 LLVMTypeRef DestTy, const char *Name) {
4215 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
4216 unwrap(DestTy), Name));
4219 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
4220 LLVMTypeRef DestTy, const char *Name) {
4221 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
4224 LLVMValueRef LLVMBuildIntCast2(LLVMBuilderRef B, LLVMValueRef Val,
4225 LLVMTypeRef DestTy, LLVMBool IsSigned,
4226 const char *Name) {
4227 return wrap(
4228 unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy), IsSigned, Name));
4231 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
4232 LLVMTypeRef DestTy, const char *Name) {
4233 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
4234 /*isSigned*/true, Name));
4237 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
4238 LLVMTypeRef DestTy, const char *Name) {
4239 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
4242 LLVMOpcode LLVMGetCastOpcode(LLVMValueRef Src, LLVMBool SrcIsSigned,
4243 LLVMTypeRef DestTy, LLVMBool DestIsSigned) {
4244 return map_to_llvmopcode(CastInst::getCastOpcode(
4245 unwrap(Src), SrcIsSigned, unwrap(DestTy), DestIsSigned));
4248 /*--.. Comparisons .........................................................--*/
4250 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
4251 LLVMValueRef LHS, LLVMValueRef RHS,
4252 const char *Name) {
4253 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
4254 unwrap(LHS), unwrap(RHS), Name));
4257 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
4258 LLVMValueRef LHS, LLVMValueRef RHS,
4259 const char *Name) {
4260 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
4261 unwrap(LHS), unwrap(RHS), Name));
4264 /*--.. Miscellaneous instructions ..........................................--*/
4266 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
4267 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
4270 LLVMValueRef LLVMBuildCall2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn,
4271 LLVMValueRef *Args, unsigned NumArgs,
4272 const char *Name) {
4273 FunctionType *FTy = unwrap<FunctionType>(Ty);
4274 return wrap(unwrap(B)->CreateCall(FTy, unwrap(Fn),
4275 ArrayRef(unwrap(Args), NumArgs), Name));
4278 LLVMValueRef
4279 LLVMBuildCallWithOperandBundles(LLVMBuilderRef B, LLVMTypeRef Ty,
4280 LLVMValueRef Fn, LLVMValueRef *Args,
4281 unsigned NumArgs, LLVMOperandBundleRef *Bundles,
4282 unsigned NumBundles, const char *Name) {
4283 FunctionType *FTy = unwrap<FunctionType>(Ty);
4284 SmallVector<OperandBundleDef, 8> OBs;
4285 for (auto *Bundle : ArrayRef(Bundles, NumBundles)) {
4286 OperandBundleDef *OB = unwrap(Bundle);
4287 OBs.push_back(*OB);
4289 return wrap(unwrap(B)->CreateCall(
4290 FTy, unwrap(Fn), ArrayRef(unwrap(Args), NumArgs), OBs, Name));
4293 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
4294 LLVMValueRef Then, LLVMValueRef Else,
4295 const char *Name) {
4296 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
4297 Name));
4300 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
4301 LLVMTypeRef Ty, const char *Name) {
4302 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
4305 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
4306 LLVMValueRef Index, const char *Name) {
4307 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
4308 Name));
4311 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
4312 LLVMValueRef EltVal, LLVMValueRef Index,
4313 const char *Name) {
4314 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
4315 unwrap(Index), Name));
4318 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
4319 LLVMValueRef V2, LLVMValueRef Mask,
4320 const char *Name) {
4321 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
4322 unwrap(Mask), Name));
4325 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
4326 unsigned Index, const char *Name) {
4327 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
4330 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
4331 LLVMValueRef EltVal, unsigned Index,
4332 const char *Name) {
4333 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
4334 Index, Name));
4337 LLVMValueRef LLVMBuildFreeze(LLVMBuilderRef B, LLVMValueRef Val,
4338 const char *Name) {
4339 return wrap(unwrap(B)->CreateFreeze(unwrap(Val), Name));
4342 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
4343 const char *Name) {
4344 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
4347 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
4348 const char *Name) {
4349 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
4352 LLVMValueRef LLVMBuildPtrDiff2(LLVMBuilderRef B, LLVMTypeRef ElemTy,
4353 LLVMValueRef LHS, LLVMValueRef RHS,
4354 const char *Name) {
4355 return wrap(unwrap(B)->CreatePtrDiff(unwrap(ElemTy), unwrap(LHS),
4356 unwrap(RHS), Name));
4359 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
4360 LLVMValueRef PTR, LLVMValueRef Val,
4361 LLVMAtomicOrdering ordering,
4362 LLVMBool singleThread) {
4363 AtomicRMWInst::BinOp intop = mapFromLLVMRMWBinOp(op);
4364 return wrap(unwrap(B)->CreateAtomicRMW(
4365 intop, unwrap(PTR), unwrap(Val), MaybeAlign(),
4366 mapFromLLVMOrdering(ordering),
4367 singleThread ? SyncScope::SingleThread : SyncScope::System));
4370 LLVMValueRef LLVMBuildAtomicRMWSyncScope(LLVMBuilderRef B,
4371 LLVMAtomicRMWBinOp op,
4372 LLVMValueRef PTR, LLVMValueRef Val,
4373 LLVMAtomicOrdering ordering,
4374 unsigned SSID) {
4375 AtomicRMWInst::BinOp intop = mapFromLLVMRMWBinOp(op);
4376 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
4377 MaybeAlign(),
4378 mapFromLLVMOrdering(ordering), SSID));
4381 LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr,
4382 LLVMValueRef Cmp, LLVMValueRef New,
4383 LLVMAtomicOrdering SuccessOrdering,
4384 LLVMAtomicOrdering FailureOrdering,
4385 LLVMBool singleThread) {
4387 return wrap(unwrap(B)->CreateAtomicCmpXchg(
4388 unwrap(Ptr), unwrap(Cmp), unwrap(New), MaybeAlign(),
4389 mapFromLLVMOrdering(SuccessOrdering),
4390 mapFromLLVMOrdering(FailureOrdering),
4391 singleThread ? SyncScope::SingleThread : SyncScope::System));
4394 LLVMValueRef LLVMBuildAtomicCmpXchgSyncScope(LLVMBuilderRef B, LLVMValueRef Ptr,
4395 LLVMValueRef Cmp, LLVMValueRef New,
4396 LLVMAtomicOrdering SuccessOrdering,
4397 LLVMAtomicOrdering FailureOrdering,
4398 unsigned SSID) {
4399 return wrap(unwrap(B)->CreateAtomicCmpXchg(
4400 unwrap(Ptr), unwrap(Cmp), unwrap(New), MaybeAlign(),
4401 mapFromLLVMOrdering(SuccessOrdering),
4402 mapFromLLVMOrdering(FailureOrdering), SSID));
4405 unsigned LLVMGetNumMaskElements(LLVMValueRef SVInst) {
4406 Value *P = unwrap(SVInst);
4407 ShuffleVectorInst *I = cast<ShuffleVectorInst>(P);
4408 return I->getShuffleMask().size();
4411 int LLVMGetMaskValue(LLVMValueRef SVInst, unsigned Elt) {
4412 Value *P = unwrap(SVInst);
4413 ShuffleVectorInst *I = cast<ShuffleVectorInst>(P);
4414 return I->getMaskValue(Elt);
4417 int LLVMGetUndefMaskElem(void) { return PoisonMaskElem; }
4419 LLVMBool LLVMIsAtomic(LLVMValueRef Inst) {
4420 return unwrap<Instruction>(Inst)->isAtomic();
4423 LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) {
4424 // Backwards compatibility: return false for non-atomic instructions
4425 Instruction *I = unwrap<Instruction>(AtomicInst);
4426 if (!I->isAtomic())
4427 return 0;
4429 return *getAtomicSyncScopeID(I) == SyncScope::SingleThread;
4432 void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) {
4433 // Backwards compatibility: ignore non-atomic instructions
4434 Instruction *I = unwrap<Instruction>(AtomicInst);
4435 if (!I->isAtomic())
4436 return;
4438 SyncScope::ID SSID = NewValue ? SyncScope::SingleThread : SyncScope::System;
4439 setAtomicSyncScopeID(I, SSID);
4442 unsigned LLVMGetAtomicSyncScopeID(LLVMValueRef AtomicInst) {
4443 Instruction *I = unwrap<Instruction>(AtomicInst);
4444 assert(I->isAtomic() && "Expected an atomic instruction");
4445 return *getAtomicSyncScopeID(I);
4448 void LLVMSetAtomicSyncScopeID(LLVMValueRef AtomicInst, unsigned SSID) {
4449 Instruction *I = unwrap<Instruction>(AtomicInst);
4450 assert(I->isAtomic() && "Expected an atomic instruction");
4451 setAtomicSyncScopeID(I, SSID);
4454 LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) {
4455 Value *P = unwrap(CmpXchgInst);
4456 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering());
4459 void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst,
4460 LLVMAtomicOrdering Ordering) {
4461 Value *P = unwrap(CmpXchgInst);
4462 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
4464 return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O);
4467 LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst) {
4468 Value *P = unwrap(CmpXchgInst);
4469 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering());
4472 void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst,
4473 LLVMAtomicOrdering Ordering) {
4474 Value *P = unwrap(CmpXchgInst);
4475 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
4477 return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O);
4480 /*===-- Module providers --------------------------------------------------===*/
4482 LLVMModuleProviderRef
4483 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
4484 return reinterpret_cast<LLVMModuleProviderRef>(M);
4487 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
4488 delete unwrap(MP);
4492 /*===-- Memory buffers ----------------------------------------------------===*/
4494 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
4495 const char *Path,
4496 LLVMMemoryBufferRef *OutMemBuf,
4497 char **OutMessage) {
4499 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
4500 if (std::error_code EC = MBOrErr.getError()) {
4501 *OutMessage = strdup(EC.message().c_str());
4502 return 1;
4504 *OutMemBuf = wrap(MBOrErr.get().release());
4505 return 0;
4508 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
4509 char **OutMessage) {
4510 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
4511 if (std::error_code EC = MBOrErr.getError()) {
4512 *OutMessage = strdup(EC.message().c_str());
4513 return 1;
4515 *OutMemBuf = wrap(MBOrErr.get().release());
4516 return 0;
4519 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
4520 const char *InputData,
4521 size_t InputDataLength,
4522 const char *BufferName,
4523 LLVMBool RequiresNullTerminator) {
4525 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
4526 StringRef(BufferName),
4527 RequiresNullTerminator).release());
4530 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
4531 const char *InputData,
4532 size_t InputDataLength,
4533 const char *BufferName) {
4535 return wrap(
4536 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
4537 StringRef(BufferName)).release());
4540 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
4541 return unwrap(MemBuf)->getBufferStart();
4544 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
4545 return unwrap(MemBuf)->getBufferSize();
4548 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
4549 delete unwrap(MemBuf);
4552 /*===-- Pass Manager ------------------------------------------------------===*/
4554 LLVMPassManagerRef LLVMCreatePassManager() {
4555 return wrap(new legacy::PassManager());
4558 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
4559 return wrap(new legacy::FunctionPassManager(unwrap(M)));
4562 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
4563 return LLVMCreateFunctionPassManagerForModule(
4564 reinterpret_cast<LLVMModuleRef>(P));
4567 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
4568 return unwrap<legacy::PassManager>(PM)->run(*unwrap(M));
4571 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
4572 return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization();
4575 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
4576 return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
4579 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
4580 return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization();
4583 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
4584 delete unwrap(PM);
4587 /*===-- Threading ------------------------------------------------------===*/
4589 LLVMBool LLVMStartMultithreaded() {
4590 return LLVMIsMultithreaded();
4593 void LLVMStopMultithreaded() {
4596 LLVMBool LLVMIsMultithreaded() {
4597 return llvm_is_multithreaded();