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