LLVM 20.0.0git
Core.cpp
Go to the documentation of this file.
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.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm-c/Core.h"
15#include "llvm/IR/Attributes.h"
16#include "llvm/IR/BasicBlock.h"
18#include "llvm/IR/Constants.h"
23#include "llvm/IR/GlobalAlias.h"
25#include "llvm/IR/IRBuilder.h"
26#include "llvm/IR/InlineAsm.h"
29#include "llvm/IR/LLVMContext.h"
31#include "llvm/IR/Module.h"
33#include "llvm/PassRegistry.h"
34#include "llvm/Support/Debug.h"
42#include <cassert>
43#include <cstdlib>
44#include <cstring>
45#include <system_error>
46
47using namespace llvm;
48
50
52 return reinterpret_cast<BasicBlock **>(BBs);
53}
54
55#define DEBUG_TYPE "ir"
56
63}
64
67}
68
69/*===-- Version query -----------------------------------------------------===*/
70
71void LLVMGetVersion(unsigned *Major, unsigned *Minor, unsigned *Patch) {
72 if (Major)
73 *Major = LLVM_VERSION_MAJOR;
74 if (Minor)
75 *Minor = LLVM_VERSION_MINOR;
76 if (Patch)
77 *Patch = LLVM_VERSION_PATCH;
78}
79
80/*===-- Error handling ----------------------------------------------------===*/
81
82char *LLVMCreateMessage(const char *Message) {
83 return strdup(Message);
84}
85
86void LLVMDisposeMessage(char *Message) {
87 free(Message);
88}
89
90
91/*===-- Operations on contexts --------------------------------------------===*/
92
94 static LLVMContext GlobalContext;
95 return GlobalContext;
96}
97
99 return wrap(new LLVMContext());
100}
101
103
105 LLVMDiagnosticHandler Handler,
106 void *DiagnosticContext) {
107 unwrap(C)->setDiagnosticHandlerCallBack(
109 Handler),
110 DiagnosticContext);
111}
112
114 return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>(
115 unwrap(C)->getDiagnosticHandlerCallBack());
116}
117
119 return unwrap(C)->getDiagnosticContext();
120}
121
123 void *OpaqueHandle) {
124 auto YieldCallback =
125 LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
126 unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
127}
128
130 return unwrap(C)->shouldDiscardValueNames();
131}
132
134 unwrap(C)->setDiscardValueNames(Discard);
135}
136
138 delete unwrap(C);
139}
140
142 unsigned SLen) {
143 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
144}
145
146unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) {
148}
149
150unsigned LLVMGetSyncScopeID(LLVMContextRef C, const char *Name, size_t SLen) {
151 return unwrap(C)->getOrInsertSyncScopeID(StringRef(Name, SLen));
152}
153
154unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) {
156}
157
159 return Attribute::AttrKind::EndAttrKinds;
160}
161
163 uint64_t Val) {
164 auto &Ctx = *unwrap(C);
165 auto AttrKind = (Attribute::AttrKind)KindID;
166 return wrap(Attribute::get(Ctx, AttrKind, Val));
167}
168
170 return unwrap(A).getKindAsEnum();
171}
172
174 auto Attr = unwrap(A);
175 if (Attr.isEnumAttribute())
176 return 0;
177 return Attr.getValueAsInt();
178}
179
181 LLVMTypeRef type_ref) {
182 auto &Ctx = *unwrap(C);
183 auto AttrKind = (Attribute::AttrKind)KindID;
184 return wrap(Attribute::get(Ctx, AttrKind, unwrap(type_ref)));
185}
186
188 auto Attr = unwrap(A);
189 return wrap(Attr.getValueAsType());
190}
191
193 unsigned KindID,
194 unsigned NumBits,
195 const uint64_t LowerWords[],
196 const uint64_t UpperWords[]) {
197 auto &Ctx = *unwrap(C);
198 auto AttrKind = (Attribute::AttrKind)KindID;
199 unsigned NumWords = divideCeil(NumBits, 64);
200 return wrap(Attribute::get(
201 Ctx, AttrKind,
202 ConstantRange(APInt(NumBits, ArrayRef(LowerWords, NumWords)),
203 APInt(NumBits, ArrayRef(UpperWords, NumWords)))));
204}
205
207 const char *K, unsigned KLength,
208 const char *V, unsigned VLength) {
209 return wrap(Attribute::get(*unwrap(C), StringRef(K, KLength),
210 StringRef(V, VLength)));
211}
212
214 unsigned *Length) {
215 auto S = unwrap(A).getKindAsString();
216 *Length = S.size();
217 return S.data();
218}
219
221 unsigned *Length) {
222 auto S = unwrap(A).getValueAsString();
223 *Length = S.size();
224 return S.data();
225}
226
228 auto Attr = unwrap(A);
229 return Attr.isEnumAttribute() || Attr.isIntAttribute();
230}
231
233 return unwrap(A).isStringAttribute();
234}
235
237 return unwrap(A).isTypeAttribute();
238}
239
241 std::string MsgStorage;
242 raw_string_ostream Stream(MsgStorage);
244
245 unwrap(DI)->print(DP);
246 Stream.flush();
247
248 return LLVMCreateMessage(MsgStorage.c_str());
249}
250
252 LLVMDiagnosticSeverity severity;
253
254 switch(unwrap(DI)->getSeverity()) {
255 default:
256 severity = LLVMDSError;
257 break;
258 case DS_Warning:
259 severity = LLVMDSWarning;
260 break;
261 case DS_Remark:
262 severity = LLVMDSRemark;
263 break;
264 case DS_Note:
265 severity = LLVMDSNote;
266 break;
267 }
268
269 return severity;
270}
271
272/*===-- Operations on modules ---------------------------------------------===*/
273
275 return wrap(new Module(ModuleID, getGlobalContext()));
276}
277
280 return wrap(new Module(ModuleID, *unwrap(C)));
281}
282
284 delete unwrap(M);
285}
286
287const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) {
288 auto &Str = unwrap(M)->getModuleIdentifier();
289 *Len = Str.length();
290 return Str.c_str();
291}
292
293void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) {
294 unwrap(M)->setModuleIdentifier(StringRef(Ident, Len));
295}
296
297const char *LLVMGetSourceFileName(LLVMModuleRef M, size_t *Len) {
298 auto &Str = unwrap(M)->getSourceFileName();
299 *Len = Str.length();
300 return Str.c_str();
301}
302
303void LLVMSetSourceFileName(LLVMModuleRef M, const char *Name, size_t Len) {
304 unwrap(M)->setSourceFileName(StringRef(Name, Len));
305}
306
307/*--.. Data layout .........................................................--*/
309 return unwrap(M)->getDataLayoutStr().c_str();
310}
311
313 return LLVMGetDataLayoutStr(M);
314}
315
316void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
317 unwrap(M)->setDataLayout(DataLayoutStr);
318}
319
320/*--.. Target triple .......................................................--*/
322 return unwrap(M)->getTargetTriple().c_str();
323}
324
325void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
326 unwrap(M)->setTargetTriple(Triple);
327}
328
329/*--.. Module flags ........................................................--*/
332 const char *Key;
333 size_t KeyLen;
335};
336
339 switch (Behavior) {
341 return Module::ModFlagBehavior::Error;
343 return Module::ModFlagBehavior::Warning;
345 return Module::ModFlagBehavior::Require;
347 return Module::ModFlagBehavior::Override;
349 return Module::ModFlagBehavior::Append;
351 return Module::ModFlagBehavior::AppendUnique;
352 }
353 llvm_unreachable("Unknown LLVMModuleFlagBehavior");
354}
355
358 switch (Behavior) {
359 case Module::ModFlagBehavior::Error:
361 case Module::ModFlagBehavior::Warning:
363 case Module::ModFlagBehavior::Require:
365 case Module::ModFlagBehavior::Override:
367 case Module::ModFlagBehavior::Append:
369 case Module::ModFlagBehavior::AppendUnique:
371 default:
372 llvm_unreachable("Unhandled Flag Behavior");
373 }
374}
375
378 unwrap(M)->getModuleFlagsMetadata(MFEs);
379
381 safe_malloc(MFEs.size() * sizeof(LLVMOpaqueModuleFlagEntry)));
382 for (unsigned i = 0; i < MFEs.size(); ++i) {
383 const auto &ModuleFlag = MFEs[i];
384 Result[i].Behavior = map_from_llvmModFlagBehavior(ModuleFlag.Behavior);
385 Result[i].Key = ModuleFlag.Key->getString().data();
386 Result[i].KeyLen = ModuleFlag.Key->getString().size();
387 Result[i].Metadata = wrap(ModuleFlag.Val);
388 }
389 *Len = MFEs.size();
390 return Result;
391}
392
394 free(Entries);
395}
396
399 unsigned Index) {
401 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
402 return MFE.Behavior;
403}
404
406 unsigned Index, size_t *Len) {
408 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
409 *Len = MFE.KeyLen;
410 return MFE.Key;
411}
412
414 unsigned Index) {
416 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
417 return MFE.Metadata;
418}
419
421 const char *Key, size_t KeyLen) {
422 return wrap(unwrap(M)->getModuleFlag({Key, KeyLen}));
423}
424
426 const char *Key, size_t KeyLen,
427 LLVMMetadataRef Val) {
428 unwrap(M)->addModuleFlag(map_to_llvmModFlagBehavior(Behavior),
429 {Key, KeyLen}, unwrap(Val));
430}
431
433 return unwrap(M)->IsNewDbgInfoFormat;
434}
435
437 unwrap(M)->setIsNewDbgInfoFormat(UseNewFormat);
438}
439
440/*--.. Printing modules ....................................................--*/
441
443 unwrap(M)->print(errs(), nullptr,
444 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
445}
446
448 char **ErrorMessage) {
449 std::error_code EC;
450 raw_fd_ostream dest(Filename, EC, sys::fs::OF_TextWithCRLF);
451 if (EC) {
452 *ErrorMessage = strdup(EC.message().c_str());
453 return true;
454 }
455
456 unwrap(M)->print(dest, nullptr);
457
458 dest.close();
459
460 if (dest.has_error()) {
461 std::string E = "Error printing to file: " + dest.error().message();
462 *ErrorMessage = strdup(E.c_str());
463 return true;
464 }
465
466 return false;
467}
468
470 std::string buf;
471 raw_string_ostream os(buf);
472
473 unwrap(M)->print(os, nullptr);
474 os.flush();
475
476 return strdup(buf.c_str());
477}
478
479/*--.. Operations on inline assembler ......................................--*/
480void LLVMSetModuleInlineAsm2(LLVMModuleRef M, const char *Asm, size_t Len) {
481 unwrap(M)->setModuleInlineAsm(StringRef(Asm, Len));
482}
483
484void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
485 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
486}
487
488void LLVMAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm, size_t Len) {
489 unwrap(M)->appendModuleInlineAsm(StringRef(Asm, Len));
490}
491
492const char *LLVMGetModuleInlineAsm(LLVMModuleRef M, size_t *Len) {
493 auto &Str = unwrap(M)->getModuleInlineAsm();
494 *Len = Str.length();
495 return Str.c_str();
496}
497
498LLVMValueRef LLVMGetInlineAsm(LLVMTypeRef Ty, const char *AsmString,
499 size_t AsmStringSize, const char *Constraints,
500 size_t ConstraintsSize, LLVMBool HasSideEffects,
501 LLVMBool IsAlignStack,
502 LLVMInlineAsmDialect Dialect, LLVMBool CanThrow) {
504 switch (Dialect) {
507 break;
510 break;
511 }
512 return wrap(InlineAsm::get(unwrap<FunctionType>(Ty),
513 StringRef(AsmString, AsmStringSize),
514 StringRef(Constraints, ConstraintsSize),
515 HasSideEffects, IsAlignStack, AD, CanThrow));
516}
517
518const char *LLVMGetInlineAsmAsmString(LLVMValueRef InlineAsmVal, size_t *Len) {
519
520 Value *Val = unwrap<Value>(InlineAsmVal);
521 const std::string &AsmString = cast<InlineAsm>(Val)->getAsmString();
522
523 *Len = AsmString.length();
524 return AsmString.c_str();
525}
526
528 size_t *Len) {
529 Value *Val = unwrap<Value>(InlineAsmVal);
530 const std::string &ConstraintString =
531 cast<InlineAsm>(Val)->getConstraintString();
532
533 *Len = ConstraintString.length();
534 return ConstraintString.c_str();
535}
536
538
539 Value *Val = unwrap<Value>(InlineAsmVal);
540 InlineAsm::AsmDialect Dialect = cast<InlineAsm>(Val)->getDialect();
541
542 switch (Dialect) {
547 }
548
549 llvm_unreachable("Unrecognized inline assembly dialect");
551}
552
554 Value *Val = unwrap<Value>(InlineAsmVal);
555 return (LLVMTypeRef)cast<InlineAsm>(Val)->getFunctionType();
556}
557
559 Value *Val = unwrap<Value>(InlineAsmVal);
560 return cast<InlineAsm>(Val)->hasSideEffects();
561}
562
564 Value *Val = unwrap<Value>(InlineAsmVal);
565 return cast<InlineAsm>(Val)->isAlignStack();
566}
567
569 Value *Val = unwrap<Value>(InlineAsmVal);
570 return cast<InlineAsm>(Val)->canThrow();
571}
572
573/*--.. Operations on module contexts ......................................--*/
575 return wrap(&unwrap(M)->getContext());
576}
577
578
579/*===-- Operations on types -----------------------------------------------===*/
580
581/*--.. Operations on all types (mostly) ....................................--*/
582
584 switch (unwrap(Ty)->getTypeID()) {
585 case Type::VoidTyID:
586 return LLVMVoidTypeKind;
587 case Type::HalfTyID:
588 return LLVMHalfTypeKind;
589 case Type::BFloatTyID:
590 return LLVMBFloatTypeKind;
591 case Type::FloatTyID:
592 return LLVMFloatTypeKind;
593 case Type::DoubleTyID:
594 return LLVMDoubleTypeKind;
597 case Type::FP128TyID:
598 return LLVMFP128TypeKind;
601 case Type::LabelTyID:
602 return LLVMLabelTypeKind;
606 return LLVMIntegerTypeKind;
609 case Type::StructTyID:
610 return LLVMStructTypeKind;
611 case Type::ArrayTyID:
612 return LLVMArrayTypeKind;
614 return LLVMPointerTypeKind;
616 return LLVMVectorTypeKind;
618 return LLVMX86_AMXTypeKind;
619 case Type::TokenTyID:
620 return LLVMTokenTypeKind;
626 llvm_unreachable("Typed pointers are unsupported via the C API");
627 }
628 llvm_unreachable("Unhandled TypeID.");
629}
630
632{
633 return unwrap(Ty)->isSized();
634}
635
637 return wrap(&unwrap(Ty)->getContext());
638}
639
641 return unwrap(Ty)->print(errs(), /*IsForDebug=*/true);
642}
643
645 std::string buf;
646 raw_string_ostream os(buf);
647
648 if (unwrap(Ty))
649 unwrap(Ty)->print(os);
650 else
651 os << "Printing <null> Type";
652
653 os.flush();
654
655 return strdup(buf.c_str());
656}
657
658/*--.. Operations on integer types .........................................--*/
659
662}
665}
668}
671}
674}
677}
679 return wrap(IntegerType::get(*unwrap(C), NumBits));
680}
681
684}
687}
690}
693}
696}
699}
700LLVMTypeRef LLVMIntType(unsigned NumBits) {
702}
703
704unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
705 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
706}
707
708/*--.. Operations on real types ............................................--*/
709
712}
715}
718}
721}
724}
727}
730}
733}
734
737}
740}
743}
746}
749}
752}
755}
758}
759
760/*--.. Operations on function types ........................................--*/
761
763 LLVMTypeRef *ParamTypes, unsigned ParamCount,
764 LLVMBool IsVarArg) {
765 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
766 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
767}
768
770 return unwrap<FunctionType>(FunctionTy)->isVarArg();
771}
772
774 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
775}
776
777unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
778 return unwrap<FunctionType>(FunctionTy)->getNumParams();
779}
780
782 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
783 for (Type *T : Ty->params())
784 *Dest++ = wrap(T);
785}
786
787/*--.. Operations on struct types ..........................................--*/
788
790 unsigned ElementCount, LLVMBool Packed) {
791 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
792 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
793}
794
796 unsigned ElementCount, LLVMBool Packed) {
797 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
798 ElementCount, Packed);
799}
800
802{
803 return wrap(StructType::create(*unwrap(C), Name));
804}
805
807{
808 StructType *Type = unwrap<StructType>(Ty);
809 if (!Type->hasName())
810 return nullptr;
811 return Type->getName().data();
812}
813
814void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
815 unsigned ElementCount, LLVMBool Packed) {
816 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
817 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
818}
819
821 return unwrap<StructType>(StructTy)->getNumElements();
822}
823
825 StructType *Ty = unwrap<StructType>(StructTy);
826 for (Type *T : Ty->elements())
827 *Dest++ = wrap(T);
828}
829
831 StructType *Ty = unwrap<StructType>(StructTy);
832 return wrap(Ty->getTypeAtIndex(i));
833}
834
836 return unwrap<StructType>(StructTy)->isPacked();
837}
838
840 return unwrap<StructType>(StructTy)->isOpaque();
841}
842
844 return unwrap<StructType>(StructTy)->isLiteral();
845}
846
848 return wrap(StructType::getTypeByName(unwrap(M)->getContext(), Name));
849}
850
853}
854
855/*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
856
858 int i = 0;
859 for (auto *T : unwrap(Tp)->subtypes()) {
860 Arr[i] = wrap(T);
861 i++;
862 }
863}
864
866 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
867}
868
870 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
871}
872
874 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
875}
876
878 return true;
879}
880
882 return wrap(FixedVectorType::get(unwrap(ElementType), ElementCount));
883}
884
886 unsigned ElementCount) {
887 return wrap(ScalableVectorType::get(unwrap(ElementType), ElementCount));
888}
889
891 auto *Ty = unwrap(WrappedTy);
892 if (auto *ATy = dyn_cast<ArrayType>(Ty))
893 return wrap(ATy->getElementType());
894 return wrap(cast<VectorType>(Ty)->getElementType());
895}
896
898 return unwrap(Tp)->getNumContainedTypes();
899}
900
902 return unwrap<ArrayType>(ArrayTy)->getNumElements();
903}
904
906 return unwrap<ArrayType>(ArrayTy)->getNumElements();
907}
908
910 return unwrap<PointerType>(PointerTy)->getAddressSpace();
911}
912
913unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
914 return unwrap<VectorType>(VectorTy)->getElementCount().getKnownMinValue();
915}
916
918 return wrap(unwrap<ConstantPtrAuth>(PtrAuth)->getPointer());
919}
920
922 return wrap(unwrap<ConstantPtrAuth>(PtrAuth)->getKey());
923}
924
926 return wrap(unwrap<ConstantPtrAuth>(PtrAuth)->getDiscriminator());
927}
928
930 return wrap(unwrap<ConstantPtrAuth>(PtrAuth)->getAddrDiscriminator());
931}
932
933/*--.. Operations on other types ...........................................--*/
934
936 return wrap(PointerType::get(*unwrap(C), AddressSpace));
937}
938
940 return wrap(Type::getVoidTy(*unwrap(C)));
941}
943 return wrap(Type::getLabelTy(*unwrap(C)));
944}
946 return wrap(Type::getTokenTy(*unwrap(C)));
947}
949 return wrap(Type::getMetadataTy(*unwrap(C)));
950}
951
954}
957}
958
960 LLVMTypeRef *TypeParams,
961 unsigned TypeParamCount,
962 unsigned *IntParams,
963 unsigned IntParamCount) {
964 ArrayRef<Type *> TypeParamArray(unwrap(TypeParams), TypeParamCount);
965 ArrayRef<unsigned> IntParamArray(IntParams, IntParamCount);
966 return wrap(
967 TargetExtType::get(*unwrap(C), Name, TypeParamArray, IntParamArray));
968}
969
970const char *LLVMGetTargetExtTypeName(LLVMTypeRef TargetExtTy) {
971 TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);
972 return Type->getName().data();
973}
974
976 TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);
977 return Type->getNumTypeParameters();
978}
979
981 unsigned Idx) {
982 TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);
983 return wrap(Type->getTypeParameter(Idx));
984}
985
987 TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);
988 return Type->getNumIntParameters();
989}
990
991unsigned LLVMGetTargetExtTypeIntParam(LLVMTypeRef TargetExtTy, unsigned Idx) {
992 TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);
993 return Type->getIntParameter(Idx);
994}
995
996/*===-- Operations on values ----------------------------------------------===*/
997
998/*--.. Operations on all values ............................................--*/
999
1001 return wrap(unwrap(Val)->getType());
1002}
1003
1005 switch(unwrap(Val)->getValueID()) {
1006#define LLVM_C_API 1
1007#define HANDLE_VALUE(Name) \
1008 case Value::Name##Val: \
1009 return LLVM##Name##ValueKind;
1010#include "llvm/IR/Value.def"
1011 default:
1013 }
1014}
1015
1016const char *LLVMGetValueName2(LLVMValueRef Val, size_t *Length) {
1017 auto *V = unwrap(Val);
1018 *Length = V->getName().size();
1019 return V->getName().data();
1020}
1021
1022void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen) {
1023 unwrap(Val)->setName(StringRef(Name, NameLen));
1024}
1025
1027 return unwrap(Val)->getName().data();
1028}
1029
1030void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
1031 unwrap(Val)->setName(Name);
1032}
1033
1035 unwrap(Val)->print(errs(), /*IsForDebug=*/true);
1036}
1037
1039 std::string buf;
1040 raw_string_ostream os(buf);
1041
1042 if (unwrap(Val))
1043 unwrap(Val)->print(os);
1044 else
1045 os << "Printing <null> Value";
1046
1047 os.flush();
1048
1049 return strdup(buf.c_str());
1050}
1051
1053 return wrap(&unwrap(Val)->getContext());
1054}
1055
1057 std::string buf;
1058 raw_string_ostream os(buf);
1059
1060 if (unwrap(Record))
1061 unwrap(Record)->print(os);
1062 else
1063 os << "Printing <null> DbgRecord";
1064
1065 os.flush();
1066
1067 return strdup(buf.c_str());
1068}
1069
1071 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
1072}
1073
1075 return unwrap<Instruction>(Inst)->hasMetadata();
1076}
1077
1079 auto *I = unwrap<Instruction>(Inst);
1080 assert(I && "Expected instruction");
1081 if (auto *MD = I->getMetadata(KindID))
1082 return wrap(MetadataAsValue::get(I->getContext(), MD));
1083 return nullptr;
1084}
1085
1086// MetadataAsValue uses a canonical format which strips the actual MDNode for
1087// MDNode with just a single constant value, storing just a ConstantAsMetadata
1088// This undoes this canonicalization, reconstructing the MDNode.
1090 Metadata *MD = MAV->getMetadata();
1091 assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
1092 "Expected a metadata node or a canonicalized constant");
1093
1094 if (MDNode *N = dyn_cast<MDNode>(MD))
1095 return N;
1096
1097 return MDNode::get(MAV->getContext(), MD);
1098}
1099
1100void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
1101 MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
1102
1103 unwrap<Instruction>(Inst)->setMetadata(KindID, N);
1104}
1105
1107 unsigned Kind;
1109};
1110
1113llvm_getMetadata(size_t *NumEntries,
1114 llvm::function_ref<void(MetadataEntries &)> AccessMD) {
1116 AccessMD(MVEs);
1117
1119 static_cast<LLVMOpaqueValueMetadataEntry *>(
1121 for (unsigned i = 0; i < MVEs.size(); ++i) {
1122 const auto &ModuleFlag = MVEs[i];
1123 Result[i].Kind = ModuleFlag.first;
1124 Result[i].Metadata = wrap(ModuleFlag.second);
1125 }
1126 *NumEntries = MVEs.size();
1127 return Result;
1128}
1129
1132 size_t *NumEntries) {
1133 return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
1134 Entries.clear();
1135 unwrap<Instruction>(Value)->getAllMetadata(Entries);
1136 });
1137}
1138
1139/*--.. Conversion functions ................................................--*/
1140
1141#define LLVM_DEFINE_VALUE_CAST(name) \
1142 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
1143 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
1144 }
1145
1147
1149 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
1150 if (isa<MDNode>(MD->getMetadata()) ||
1151 isa<ValueAsMetadata>(MD->getMetadata()))
1152 return Val;
1153 return nullptr;
1154}
1155
1157 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
1158 if (isa<ValueAsMetadata>(MD->getMetadata()))
1159 return Val;
1160 return nullptr;
1161}
1162
1164 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
1165 if (isa<MDString>(MD->getMetadata()))
1166 return Val;
1167 return nullptr;
1168}
1169
1170/*--.. Operations on Uses ..................................................--*/
1172 Value *V = unwrap(Val);
1173 Value::use_iterator I = V->use_begin();
1174 if (I == V->use_end())
1175 return nullptr;
1176 return wrap(&*I);
1177}
1178
1180 Use *Next = unwrap(U)->getNext();
1181 if (Next)
1182 return wrap(Next);
1183 return nullptr;
1184}
1185
1187 return wrap(unwrap(U)->getUser());
1188}
1189
1191 return wrap(unwrap(U)->get());
1192}
1193
1194/*--.. Operations on Users .................................................--*/
1195
1197 unsigned Index) {
1198 Metadata *Op = N->getOperand(Index);
1199 if (!Op)
1200 return nullptr;
1201 if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
1202 return wrap(C->getValue());
1203 return wrap(MetadataAsValue::get(Context, Op));
1204}
1205
1207 Value *V = unwrap(Val);
1208 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
1209 if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1210 assert(Index == 0 && "Function-local metadata can only have one operand");
1211 return wrap(L->getValue());
1212 }
1213 return getMDNodeOperandImpl(V->getContext(),
1214 cast<MDNode>(MD->getMetadata()), Index);
1215 }
1216
1217 return wrap(cast<User>(V)->getOperand(Index));
1218}
1219
1221 Value *V = unwrap(Val);
1222 return wrap(&cast<User>(V)->getOperandUse(Index));
1223}
1224
1226 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
1227}
1228
1230 Value *V = unwrap(Val);
1231 if (isa<MetadataAsValue>(V))
1232 return LLVMGetMDNodeNumOperands(Val);
1233
1234 return cast<User>(V)->getNumOperands();
1235}
1236
1237/*--.. Operations on constants of any type .................................--*/
1238
1240 return wrap(Constant::getNullValue(unwrap(Ty)));
1241}
1242
1245}
1246
1248 return wrap(UndefValue::get(unwrap(Ty)));
1249}
1250
1252 return wrap(PoisonValue::get(unwrap(Ty)));
1253}
1254
1256 return isa<Constant>(unwrap(Ty));
1257}
1258
1260 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
1261 return C->isNullValue();
1262 return false;
1263}
1264
1266 return isa<UndefValue>(unwrap(Val));
1267}
1268
1270 return isa<PoisonValue>(unwrap(Val));
1271}
1272
1274 return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
1275}
1276
1277/*--.. Operations on metadata nodes ........................................--*/
1278
1280 size_t SLen) {
1281 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
1282}
1283
1285 size_t Count) {
1286 return wrap(MDNode::get(*unwrap(C), ArrayRef<Metadata*>(unwrap(MDs), Count)));
1287}
1288
1290 unsigned SLen) {
1291 LLVMContext &Context = *unwrap(C);
1293 Context, MDString::get(Context, StringRef(Str, SLen))));
1294}
1295
1296LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
1297 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
1298}
1299
1301 unsigned Count) {
1302 LLVMContext &Context = *unwrap(C);
1304 for (auto *OV : ArrayRef(Vals, Count)) {
1305 Value *V = unwrap(OV);
1306 Metadata *MD;
1307 if (!V)
1308 MD = nullptr;
1309 else if (auto *C = dyn_cast<Constant>(V))
1311 else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
1312 MD = MDV->getMetadata();
1313 assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
1314 "outside of direct argument to call");
1315 } else {
1316 // This is function-local metadata. Pretend to make an MDNode.
1317 assert(Count == 1 &&
1318 "Expected only one operand to function-local metadata");
1319 return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V)));
1320 }
1321
1322 MDs.push_back(MD);
1323 }
1324 return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs)));
1325}
1326
1327LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
1328 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
1329}
1330
1332 return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
1333}
1334
1336 auto *V = unwrap(Val);
1337 if (auto *C = dyn_cast<Constant>(V))
1339 if (auto *MAV = dyn_cast<MetadataAsValue>(V))
1340 return wrap(MAV->getMetadata());
1341 return wrap(ValueAsMetadata::get(V));
1342}
1343
1344const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
1345 if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
1346 if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
1347 *Length = S->getString().size();
1348 return S->getString().data();
1349 }
1350 *Length = 0;
1351 return nullptr;
1352}
1353
1355 auto *MD = unwrap<MetadataAsValue>(V);
1356 if (isa<ValueAsMetadata>(MD->getMetadata()))
1357 return 1;
1358 return cast<MDNode>(MD->getMetadata())->getNumOperands();
1359}
1360
1362 Module *Mod = unwrap(M);
1363 Module::named_metadata_iterator I = Mod->named_metadata_begin();
1364 if (I == Mod->named_metadata_end())
1365 return nullptr;
1366 return wrap(&*I);
1367}
1368
1370 Module *Mod = unwrap(M);
1371 Module::named_metadata_iterator I = Mod->named_metadata_end();
1372 if (I == Mod->named_metadata_begin())
1373 return nullptr;
1374 return wrap(&*--I);
1375}
1376
1378 NamedMDNode *NamedNode = unwrap(NMD);
1380 if (++I == NamedNode->getParent()->named_metadata_end())
1381 return nullptr;
1382 return wrap(&*I);
1383}
1384
1386 NamedMDNode *NamedNode = unwrap(NMD);
1388 if (I == NamedNode->getParent()->named_metadata_begin())
1389 return nullptr;
1390 return wrap(&*--I);
1391}
1392
1394 const char *Name, size_t NameLen) {
1395 return wrap(unwrap(M)->getNamedMetadata(StringRef(Name, NameLen)));
1396}
1397
1399 const char *Name, size_t NameLen) {
1400 return wrap(unwrap(M)->getOrInsertNamedMetadata({Name, NameLen}));
1401}
1402
1403const char *LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NMD, size_t *NameLen) {
1404 NamedMDNode *NamedNode = unwrap(NMD);
1405 *NameLen = NamedNode->getName().size();
1406 return NamedNode->getName().data();
1407}
1408
1410 auto *MD = unwrap<MetadataAsValue>(V);
1411 if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1412 *Dest = wrap(MDV->getValue());
1413 return;
1414 }
1415 const auto *N = cast<MDNode>(MD->getMetadata());
1416 const unsigned numOperands = N->getNumOperands();
1417 LLVMContext &Context = unwrap(V)->getContext();
1418 for (unsigned i = 0; i < numOperands; i++)
1419 Dest[i] = getMDNodeOperandImpl(Context, N, i);
1420}
1421
1423 LLVMMetadataRef Replacement) {
1424 auto *MD = cast<MetadataAsValue>(unwrap(V));
1425 auto *N = cast<MDNode>(MD->getMetadata());
1426 N->replaceOperandWith(Index, unwrap<Metadata>(Replacement));
1427}
1428
1430 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
1431 return N->getNumOperands();
1432 }
1433 return 0;
1434}
1435
1437 LLVMValueRef *Dest) {
1438 NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
1439 if (!N)
1440 return;
1441 LLVMContext &Context = unwrap(M)->getContext();
1442 for (unsigned i=0;i<N->getNumOperands();i++)
1443 Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
1444}
1445
1447 LLVMValueRef Val) {
1448 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
1449 if (!N)
1450 return;
1451 if (!Val)
1452 return;
1453 N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
1454}
1455
1456const char *LLVMGetDebugLocDirectory(LLVMValueRef Val, unsigned *Length) {
1457 if (!Length) return nullptr;
1458 StringRef S;
1459 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1460 if (const auto &DL = I->getDebugLoc()) {
1461 S = DL->getDirectory();
1462 }
1463 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1465 GV->getDebugInfo(GVEs);
1466 if (GVEs.size())
1467 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1468 S = DGV->getDirectory();
1469 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1470 if (const DISubprogram *DSP = F->getSubprogram())
1471 S = DSP->getDirectory();
1472 } else {
1473 assert(0 && "Expected Instruction, GlobalVariable or Function");
1474 return nullptr;
1475 }
1476 *Length = S.size();
1477 return S.data();
1478}
1479
1480const char *LLVMGetDebugLocFilename(LLVMValueRef Val, unsigned *Length) {
1481 if (!Length) return nullptr;
1482 StringRef S;
1483 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1484 if (const auto &DL = I->getDebugLoc()) {
1485 S = DL->getFilename();
1486 }
1487 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1489 GV->getDebugInfo(GVEs);
1490 if (GVEs.size())
1491 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1492 S = DGV->getFilename();
1493 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1494 if (const DISubprogram *DSP = F->getSubprogram())
1495 S = DSP->getFilename();
1496 } else {
1497 assert(0 && "Expected Instruction, GlobalVariable or Function");
1498 return nullptr;
1499 }
1500 *Length = S.size();
1501 return S.data();
1502}
1503
1505 unsigned L = 0;
1506 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1507 if (const auto &DL = I->getDebugLoc()) {
1508 L = DL->getLine();
1509 }
1510 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1512 GV->getDebugInfo(GVEs);
1513 if (GVEs.size())
1514 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1515 L = DGV->getLine();
1516 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1517 if (const DISubprogram *DSP = F->getSubprogram())
1518 L = DSP->getLine();
1519 } else {
1520 assert(0 && "Expected Instruction, GlobalVariable or Function");
1521 return -1;
1522 }
1523 return L;
1524}
1525
1527 unsigned C = 0;
1528 if (const auto *I = dyn_cast<Instruction>(unwrap(Val)))
1529 if (const auto &DL = I->getDebugLoc())
1530 C = DL->getColumn();
1531 return C;
1532}
1533
1534/*--.. Operations on scalar constants ......................................--*/
1535
1536LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
1537 LLVMBool SignExtend) {
1538 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
1539}
1540
1542 unsigned NumWords,
1543 const uint64_t Words[]) {
1544 IntegerType *Ty = unwrap<IntegerType>(IntTy);
1545 return wrap(ConstantInt::get(
1546 Ty->getContext(), APInt(Ty->getBitWidth(), ArrayRef(Words, NumWords))));
1547}
1548
1550 uint8_t Radix) {
1551 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
1552 Radix));
1553}
1554
1556 unsigned SLen, uint8_t Radix) {
1557 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
1558 Radix));
1559}
1560
1562 return wrap(ConstantFP::get(unwrap(RealTy), N));
1563}
1564
1566 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
1567}
1568
1570 unsigned SLen) {
1571 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
1572}
1573
1574unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
1575 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
1576}
1577
1579 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
1580}
1581
1582double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
1583 ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
1584 Type *Ty = cFP->getType();
1585
1586 if (Ty->isHalfTy() || Ty->isBFloatTy() || Ty->isFloatTy() ||
1587 Ty->isDoubleTy()) {
1588 *LosesInfo = false;
1589 return cFP->getValueAPF().convertToDouble();
1590 }
1591
1592 bool APFLosesInfo;
1593 APFloat APF = cFP->getValueAPF();
1594 APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo);
1595 *LosesInfo = APFLosesInfo;
1596 return APF.convertToDouble();
1597}
1598
1599/*--.. Operations on composite constants ...................................--*/
1600
1602 unsigned Length,
1603 LLVMBool DontNullTerminate) {
1604 /* Inverted the sense of AddNull because ', 0)' is a
1605 better mnemonic for null termination than ', 1)'. */
1607 DontNullTerminate == 0));
1608}
1609
1611 size_t Length,
1612 LLVMBool DontNullTerminate) {
1613 /* Inverted the sense of AddNull because ', 0)' is a
1614 better mnemonic for null termination than ', 1)'. */
1616 DontNullTerminate == 0));
1617}
1618
1619LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
1620 LLVMBool DontNullTerminate) {
1622 DontNullTerminate);
1623}
1624
1626 return wrap(unwrap<Constant>(C)->getAggregateElement(Idx));
1627}
1628
1630 return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
1631}
1632
1634 return unwrap<ConstantDataSequential>(C)->isString();
1635}
1636
1637const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1638 StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1639 *Length = Str.size();
1640 return Str.data();
1641}
1642
1644 LLVMValueRef *ConstantVals, unsigned Length) {
1645 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1646 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1647}
1648
1650 uint64_t Length) {
1651 ArrayRef<Constant *> V(unwrap<Constant>(ConstantVals, Length), Length);
1652 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1653}
1654
1656 LLVMValueRef *ConstantVals,
1657 unsigned Count, LLVMBool Packed) {
1658 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1659 return wrap(ConstantStruct::getAnon(*unwrap(C), ArrayRef(Elements, Count),
1660 Packed != 0));
1661}
1662
1663LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
1664 LLVMBool Packed) {
1665 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1666 Packed);
1667}
1668
1670 LLVMValueRef *ConstantVals,
1671 unsigned Count) {
1672 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1673 StructType *Ty = unwrap<StructType>(StructTy);
1674
1675 return wrap(ConstantStruct::get(Ty, ArrayRef(Elements, Count)));
1676}
1677
1678LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
1680 ArrayRef(unwrap<Constant>(ScalarConstantVals, Size), Size)));
1681}
1682
1684 LLVMValueRef Disc, LLVMValueRef AddrDisc) {
1686 unwrap<Constant>(Ptr), unwrap<ConstantInt>(Key),
1687 unwrap<ConstantInt>(Disc), unwrap<Constant>(AddrDisc)));
1688}
1689
1690/*-- Opcode mapping */
1691
1693{
1694 switch (opcode) {
1695 default: llvm_unreachable("Unhandled Opcode.");
1696#define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
1697#include "llvm/IR/Instruction.def"
1698#undef HANDLE_INST
1699 }
1700}
1701
1703{
1704 switch (code) {
1705#define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
1706#include "llvm/IR/Instruction.def"
1707#undef HANDLE_INST
1708 }
1709 llvm_unreachable("Unhandled Opcode.");
1710}
1711
1712/*-- GEP wrap flag conversions */
1713
1715 GEPNoWrapFlags NewGEPFlags;
1716 if ((GEPFlags & LLVMGEPFlagInBounds) != 0)
1717 NewGEPFlags |= GEPNoWrapFlags::inBounds();
1718 if ((GEPFlags & LLVMGEPFlagNUSW) != 0)
1719 NewGEPFlags |= GEPNoWrapFlags::noUnsignedSignedWrap();
1720 if ((GEPFlags & LLVMGEPFlagNUW) != 0)
1721 NewGEPFlags |= GEPNoWrapFlags::noUnsignedWrap();
1722
1723 return NewGEPFlags;
1724}
1725
1727 LLVMGEPNoWrapFlags NewGEPFlags = 0;
1728 if (GEPFlags.isInBounds())
1729 NewGEPFlags |= LLVMGEPFlagInBounds;
1730 if (GEPFlags.hasNoUnsignedSignedWrap())
1731 NewGEPFlags |= LLVMGEPFlagNUSW;
1732 if (GEPFlags.hasNoUnsignedWrap())
1733 NewGEPFlags |= LLVMGEPFlagNUW;
1734
1735 return NewGEPFlags;
1736}
1737
1738/*--.. Constant expressions ................................................--*/
1739
1741 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
1742}
1743
1746}
1747
1749 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
1750}
1751
1753 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1754}
1755
1757 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
1758}
1759
1761 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1762}
1763
1764
1766 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
1767}
1768
1770 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
1771 unwrap<Constant>(RHSConstant)));
1772}
1773
1775 LLVMValueRef RHSConstant) {
1776 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
1777 unwrap<Constant>(RHSConstant)));
1778}
1779
1781 LLVMValueRef RHSConstant) {
1782 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
1783 unwrap<Constant>(RHSConstant)));
1784}
1785
1787 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
1788 unwrap<Constant>(RHSConstant)));
1789}
1790
1792 LLVMValueRef RHSConstant) {
1793 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
1794 unwrap<Constant>(RHSConstant)));
1795}
1796
1798 LLVMValueRef RHSConstant) {
1799 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
1800 unwrap<Constant>(RHSConstant)));
1801}
1802
1804 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
1805 unwrap<Constant>(RHSConstant)));
1806}
1807
1809 LLVMValueRef RHSConstant) {
1810 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
1811 unwrap<Constant>(RHSConstant)));
1812}
1813
1815 LLVMValueRef RHSConstant) {
1816 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
1817 unwrap<Constant>(RHSConstant)));
1818}
1819
1821 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
1822 unwrap<Constant>(RHSConstant)));
1823}
1824
1826 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1827 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1828 NumIndices);
1829 Constant *Val = unwrap<Constant>(ConstantVal);
1830 return wrap(ConstantExpr::getGetElementPtr(unwrap(Ty), Val, IdxList));
1831}
1832
1834 LLVMValueRef *ConstantIndices,
1835 unsigned NumIndices) {
1836 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1837 NumIndices);
1838 Constant *Val = unwrap<Constant>(ConstantVal);
1839 return wrap(ConstantExpr::getInBoundsGetElementPtr(unwrap(Ty), Val, IdxList));
1840}
1841
1843 LLVMValueRef ConstantVal,
1844 LLVMValueRef *ConstantIndices,
1845 unsigned NumIndices,
1846 LLVMGEPNoWrapFlags NoWrapFlags) {
1847 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1848 NumIndices);
1849 Constant *Val = unwrap<Constant>(ConstantVal);
1851 unwrap(Ty), Val, IdxList, mapFromLLVMGEPNoWrapFlags(NoWrapFlags)));
1852}
1853
1855 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1856 unwrap(ToType)));
1857}
1858
1860 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1861 unwrap(ToType)));
1862}
1863
1865 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1866 unwrap(ToType)));
1867}
1868
1870 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1871 unwrap(ToType)));
1872}
1873
1875 LLVMTypeRef ToType) {
1876 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1877 unwrap(ToType)));
1878}
1879
1881 LLVMTypeRef ToType) {
1882 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1883 unwrap(ToType)));
1884}
1885
1887 LLVMTypeRef ToType) {
1888 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1889 unwrap(ToType)));
1890}
1891
1893 LLVMValueRef IndexConstant) {
1894 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1895 unwrap<Constant>(IndexConstant)));
1896}
1897
1899 LLVMValueRef ElementValueConstant,
1900 LLVMValueRef IndexConstant) {
1901 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1902 unwrap<Constant>(ElementValueConstant),
1903 unwrap<Constant>(IndexConstant)));
1904}
1905
1907 LLVMValueRef VectorBConstant,
1908 LLVMValueRef MaskConstant) {
1909 SmallVector<int, 16> IntMask;
1910 ShuffleVectorInst::getShuffleMask(unwrap<Constant>(MaskConstant), IntMask);
1911 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1912 unwrap<Constant>(VectorBConstant),
1913 IntMask));
1914}
1915
1917 const char *Constraints,
1918 LLVMBool HasSideEffects,
1919 LLVMBool IsAlignStack) {
1920 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1921 Constraints, HasSideEffects, IsAlignStack));
1922}
1923
1925 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1926}
1927
1929 return wrap(unwrap<BlockAddress>(BlockAddr)->getFunction());
1930}
1931
1933 return wrap(unwrap<BlockAddress>(BlockAddr)->getBasicBlock());
1934}
1935
1936/*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1937
1939 return wrap(unwrap<GlobalValue>(Global)->getParent());
1940}
1941
1943 return unwrap<GlobalValue>(Global)->isDeclaration();
1944}
1945
1947 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1949 return LLVMExternalLinkage;
1957 return LLVMWeakAnyLinkage;
1959 return LLVMWeakODRLinkage;
1961 return LLVMAppendingLinkage;
1963 return LLVMInternalLinkage;
1965 return LLVMPrivateLinkage;
1969 return LLVMCommonLinkage;
1970 }
1971
1972 llvm_unreachable("Invalid GlobalValue linkage!");
1973}
1974
1976 GlobalValue *GV = unwrap<GlobalValue>(Global);
1977
1978 switch (Linkage) {
1981 break;
1984 break;
1987 break;
1990 break;
1992 LLVM_DEBUG(
1993 errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1994 "longer supported.");
1995 break;
1996 case LLVMWeakAnyLinkage:
1998 break;
1999 case LLVMWeakODRLinkage:
2001 break;
2004 break;
2007 break;
2008 case LLVMPrivateLinkage:
2010 break;
2013 break;
2016 break;
2018 LLVM_DEBUG(
2019 errs()
2020 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
2021 break;
2023 LLVM_DEBUG(
2024 errs()
2025 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
2026 break;
2029 break;
2030 case LLVMGhostLinkage:
2031 LLVM_DEBUG(
2032 errs() << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
2033 break;
2034 case LLVMCommonLinkage:
2036 break;
2037 }
2038}
2039
2041 // Using .data() is safe because of how GlobalObject::setSection is
2042 // implemented.
2043 return unwrap<GlobalValue>(Global)->getSection().data();
2044}
2045
2046void LLVMSetSection(LLVMValueRef Global, const char *Section) {
2047 unwrap<GlobalObject>(Global)->setSection(Section);
2048}
2049
2051 return static_cast<LLVMVisibility>(
2052 unwrap<GlobalValue>(Global)->getVisibility());
2053}
2054
2056 unwrap<GlobalValue>(Global)
2057 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
2058}
2059
2061 return static_cast<LLVMDLLStorageClass>(
2062 unwrap<GlobalValue>(Global)->getDLLStorageClass());
2063}
2064
2066 unwrap<GlobalValue>(Global)->setDLLStorageClass(
2067 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
2068}
2069
2071 switch (unwrap<GlobalValue>(Global)->getUnnamedAddr()) {
2072 case GlobalVariable::UnnamedAddr::None:
2073 return LLVMNoUnnamedAddr;
2074 case GlobalVariable::UnnamedAddr::Local:
2075 return LLVMLocalUnnamedAddr;
2076 case GlobalVariable::UnnamedAddr::Global:
2077 return LLVMGlobalUnnamedAddr;
2078 }
2079 llvm_unreachable("Unknown UnnamedAddr kind!");
2080}
2081
2083 GlobalValue *GV = unwrap<GlobalValue>(Global);
2084
2085 switch (UnnamedAddr) {
2086 case LLVMNoUnnamedAddr:
2087 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::None);
2089 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Local);
2091 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Global);
2092 }
2093}
2094
2096 return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
2097}
2098
2100 unwrap<GlobalValue>(Global)->setUnnamedAddr(
2101 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
2102 : GlobalValue::UnnamedAddr::None);
2103}
2104
2106 return wrap(unwrap<GlobalValue>(Global)->getValueType());
2107}
2108
2109/*--.. Operations on global variables, load and store instructions .........--*/
2110
2112 Value *P = unwrap(V);
2113 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2114 return GV->getAlign() ? GV->getAlign()->value() : 0;
2115 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
2116 return AI->getAlign().value();
2117 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2118 return LI->getAlign().value();
2119 if (StoreInst *SI = dyn_cast<StoreInst>(P))
2120 return SI->getAlign().value();
2121 if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2122 return RMWI->getAlign().value();
2123 if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2124 return CXI->getAlign().value();
2125
2127 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, "
2128 "and AtomicCmpXchgInst have alignment");
2129}
2130
2131void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
2132 Value *P = unwrap(V);
2133 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2134 GV->setAlignment(MaybeAlign(Bytes));
2135 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
2136 AI->setAlignment(Align(Bytes));
2137 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
2138 LI->setAlignment(Align(Bytes));
2139 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
2140 SI->setAlignment(Align(Bytes));
2141 else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2142 RMWI->setAlignment(Align(Bytes));
2143 else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2144 CXI->setAlignment(Align(Bytes));
2145 else
2147 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, and "
2148 "and AtomicCmpXchgInst have alignment");
2149}
2150
2152 size_t *NumEntries) {
2153 return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
2154 Entries.clear();
2155 if (Instruction *Instr = dyn_cast<Instruction>(unwrap(Value))) {
2156 Instr->getAllMetadata(Entries);
2157 } else {
2158 unwrap<GlobalObject>(Value)->getAllMetadata(Entries);
2159 }
2160 });
2161}
2162
2164 unsigned Index) {
2166 static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2167 return MVE.Kind;
2168}
2169
2172 unsigned Index) {
2174 static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2175 return MVE.Metadata;
2176}
2177
2179 free(Entries);
2180}
2181
2183 LLVMMetadataRef MD) {
2184 unwrap<GlobalObject>(Global)->setMetadata(Kind, unwrap<MDNode>(MD));
2185}
2186
2188 unwrap<GlobalObject>(Global)->eraseMetadata(Kind);
2189}
2190
2192 unwrap<GlobalObject>(Global)->clearMetadata();
2193}
2194
2195/*--.. Operations on global variables ......................................--*/
2196
2198 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2200}
2201
2203 const char *Name,
2204 unsigned AddressSpace) {
2205 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2207 nullptr, GlobalVariable::NotThreadLocal,
2208 AddressSpace));
2209}
2210
2212 return wrap(unwrap(M)->getNamedGlobal(Name));
2213}
2214
2216 size_t Length) {
2217 return wrap(unwrap(M)->getNamedGlobal(StringRef(Name, Length)));
2218}
2219
2221 Module *Mod = unwrap(M);
2222 Module::global_iterator I = Mod->global_begin();
2223 if (I == Mod->global_end())
2224 return nullptr;
2225 return wrap(&*I);
2226}
2227
2229 Module *Mod = unwrap(M);
2230 Module::global_iterator I = Mod->global_end();
2231 if (I == Mod->global_begin())
2232 return nullptr;
2233 return wrap(&*--I);
2234}
2235
2237 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2239 if (++I == GV->getParent()->global_end())
2240 return nullptr;
2241 return wrap(&*I);
2242}
2243
2245 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2247 if (I == GV->getParent()->global_begin())
2248 return nullptr;
2249 return wrap(&*--I);
2250}
2251
2253 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
2254}
2255
2257 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
2258 if ( !GV->hasInitializer() )
2259 return nullptr;
2260 return wrap(GV->getInitializer());
2261}
2262
2263void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
2264 unwrap<GlobalVariable>(GlobalVar)
2265 ->setInitializer(unwrap<Constant>(ConstantVal));
2266}
2267
2269 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
2270}
2271
2272void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
2273 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
2274}
2275
2277 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
2278}
2279
2280void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
2281 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
2282}
2283
2285 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
2286 case GlobalVariable::NotThreadLocal:
2287 return LLVMNotThreadLocal;
2288 case GlobalVariable::GeneralDynamicTLSModel:
2290 case GlobalVariable::LocalDynamicTLSModel:
2292 case GlobalVariable::InitialExecTLSModel:
2294 case GlobalVariable::LocalExecTLSModel:
2295 return LLVMLocalExecTLSModel;
2296 }
2297
2298 llvm_unreachable("Invalid GlobalVariable thread local mode");
2299}
2300
2302 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2303
2304 switch (Mode) {
2305 case LLVMNotThreadLocal:
2306 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
2307 break;
2309 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
2310 break;
2312 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
2313 break;
2315 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
2316 break;
2318 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
2319 break;
2320 }
2321}
2322
2324 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
2325}
2326
2328 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
2329}
2330
2331/*--.. Operations on aliases ......................................--*/
2332
2334 unsigned AddrSpace, LLVMValueRef Aliasee,
2335 const char *Name) {
2336 return wrap(GlobalAlias::create(unwrap(ValueTy), AddrSpace,
2338 unwrap<Constant>(Aliasee), unwrap(M)));
2339}
2340
2342 const char *Name, size_t NameLen) {
2343 return wrap(unwrap(M)->getNamedAlias(StringRef(Name, NameLen)));
2344}
2345
2347 Module *Mod = unwrap(M);
2348 Module::alias_iterator I = Mod->alias_begin();
2349 if (I == Mod->alias_end())
2350 return nullptr;
2351 return wrap(&*I);
2352}
2353
2355 Module *Mod = unwrap(M);
2356 Module::alias_iterator I = Mod->alias_end();
2357 if (I == Mod->alias_begin())
2358 return nullptr;
2359 return wrap(&*--I);
2360}
2361
2363 GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2365 if (++I == Alias->getParent()->alias_end())
2366 return nullptr;
2367 return wrap(&*I);
2368}
2369
2371 GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2373 if (I == Alias->getParent()->alias_begin())
2374 return nullptr;
2375 return wrap(&*--I);
2376}
2377
2379 return wrap(unwrap<GlobalAlias>(Alias)->getAliasee());
2380}
2381
2383 unwrap<GlobalAlias>(Alias)->setAliasee(unwrap<Constant>(Aliasee));
2384}
2385
2386/*--.. Operations on functions .............................................--*/
2387
2389 LLVMTypeRef FunctionTy) {
2390 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
2392}
2393
2395 return wrap(unwrap(M)->getFunction(Name));
2396}
2397
2399 size_t Length) {
2400 return wrap(unwrap(M)->getFunction(StringRef(Name, Length)));
2401}
2402
2404 Module *Mod = unwrap(M);
2405 Module::iterator I = Mod->begin();
2406 if (I == Mod->end())
2407 return nullptr;
2408 return wrap(&*I);
2409}
2410
2412 Module *Mod = unwrap(M);
2413 Module::iterator I = Mod->end();
2414 if (I == Mod->begin())
2415 return nullptr;
2416 return wrap(&*--I);
2417}
2418
2420 Function *Func = unwrap<Function>(Fn);
2421 Module::iterator I(Func);
2422 if (++I == Func->getParent()->end())
2423 return nullptr;
2424 return wrap(&*I);
2425}
2426
2428 Function *Func = unwrap<Function>(Fn);
2429 Module::iterator I(Func);
2430 if (I == Func->getParent()->begin())
2431 return nullptr;
2432 return wrap(&*--I);
2433}
2434
2436 unwrap<Function>(Fn)->eraseFromParent();
2437}
2438
2440 return unwrap<Function>(Fn)->hasPersonalityFn();
2441}
2442
2444 return wrap(unwrap<Function>(Fn)->getPersonalityFn());
2445}
2446
2448 unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
2449}
2450
2452 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
2453 return F->getIntrinsicID();
2454 return 0;
2455}
2456
2458 assert(ID < llvm::Intrinsic::num_intrinsics && "Intrinsic ID out of range");
2459 return llvm::Intrinsic::ID(ID);
2460}
2461
2463 unsigned ID,
2464 LLVMTypeRef *ParamTypes,
2465 size_t ParamCount) {
2466 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2467 auto IID = llvm_map_to_intrinsic_id(ID);
2468 return wrap(llvm::Intrinsic::getDeclaration(unwrap(Mod), IID, Tys));
2469}
2470
2471const char *LLVMIntrinsicGetName(unsigned ID, size_t *NameLength) {
2472 auto IID = llvm_map_to_intrinsic_id(ID);
2473 auto Str = llvm::Intrinsic::getName(IID);
2474 *NameLength = Str.size();
2475 return Str.data();
2476}
2477
2479 LLVMTypeRef *ParamTypes, size_t ParamCount) {
2480 auto IID = llvm_map_to_intrinsic_id(ID);
2481 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2482 return wrap(llvm::Intrinsic::getType(*unwrap(Ctx), IID, Tys));
2483}
2484
2486 LLVMTypeRef *ParamTypes,
2487 size_t ParamCount,
2488 size_t *NameLength) {
2489 auto IID = llvm_map_to_intrinsic_id(ID);
2490 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2491 auto Str = llvm::Intrinsic::getNameNoUnnamedTypes(IID, Tys);
2492 *NameLength = Str.length();
2493 return strdup(Str.c_str());
2494}
2495
2497 LLVMTypeRef *ParamTypes,
2498 size_t ParamCount,
2499 size_t *NameLength) {
2500 auto IID = llvm_map_to_intrinsic_id(ID);
2501 ArrayRef<Type *> Tys(unwrap(ParamTypes), ParamCount);
2502 auto Str = llvm::Intrinsic::getName(IID, Tys, unwrap(Mod));
2503 *NameLength = Str.length();
2504 return strdup(Str.c_str());
2505}
2506
2507unsigned LLVMLookupIntrinsicID(const char *Name, size_t NameLen) {
2508 return Function::lookupIntrinsicID({Name, NameLen});
2509}
2510
2512 auto IID = llvm_map_to_intrinsic_id(ID);
2514}
2515
2517 return unwrap<Function>(Fn)->getCallingConv();
2518}
2519
2521 return unwrap<Function>(Fn)->setCallingConv(
2522 static_cast<CallingConv::ID>(CC));
2523}
2524
2525const char *LLVMGetGC(LLVMValueRef Fn) {
2526 Function *F = unwrap<Function>(Fn);
2527 return F->hasGC()? F->getGC().c_str() : nullptr;
2528}
2529
2530void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
2531 Function *F = unwrap<Function>(Fn);
2532 if (GC)
2533 F->setGC(GC);
2534 else
2535 F->clearGC();
2536}
2537
2539 Function *F = unwrap<Function>(Fn);
2540 return wrap(F->getPrefixData());
2541}
2542
2544 Function *F = unwrap<Function>(Fn);
2545 return F->hasPrefixData();
2546}
2547
2549 Function *F = unwrap<Function>(Fn);
2550 Constant *prefix = unwrap<Constant>(prefixData);
2551 F->setPrefixData(prefix);
2552}
2553
2555 Function *F = unwrap<Function>(Fn);
2556 return wrap(F->getPrologueData());
2557}
2558
2560 Function *F = unwrap<Function>(Fn);
2561 return F->hasPrologueData();
2562}
2563
2565 Function *F = unwrap<Function>(Fn);
2566 Constant *prologue = unwrap<Constant>(prologueData);
2567 F->setPrologueData(prologue);
2568}
2569
2572 unwrap<Function>(F)->addAttributeAtIndex(Idx, unwrap(A));
2573}
2574
2576 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2577 return AS.getNumAttributes();
2578}
2579
2581 LLVMAttributeRef *Attrs) {
2582 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2583 for (auto A : AS)
2584 *Attrs++ = wrap(A);
2585}
2586
2589 unsigned KindID) {
2590 return wrap(unwrap<Function>(F)->getAttributeAtIndex(
2591 Idx, (Attribute::AttrKind)KindID));
2592}
2593
2596 const char *K, unsigned KLen) {
2597 return wrap(
2598 unwrap<Function>(F)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
2599}
2600
2602 unsigned KindID) {
2603 unwrap<Function>(F)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
2604}
2605
2607 const char *K, unsigned KLen) {
2608 unwrap<Function>(F)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
2609}
2610
2612 const char *V) {
2613 Function *Func = unwrap<Function>(Fn);
2614 Attribute Attr = Attribute::get(Func->getContext(), A, V);
2615 Func->addFnAttr(Attr);
2616}
2617
2618/*--.. Operations on parameters ............................................--*/
2619
2621 // This function is strictly redundant to
2622 // LLVMCountParamTypes(LLVMGlobalGetValueType(FnRef))
2623 return unwrap<Function>(FnRef)->arg_size();
2624}
2625
2626void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
2627 Function *Fn = unwrap<Function>(FnRef);
2628 for (Argument &A : Fn->args())
2629 *ParamRefs++ = wrap(&A);
2630}
2631
2633 Function *Fn = unwrap<Function>(FnRef);
2634 return wrap(&Fn->arg_begin()[index]);
2635}
2636
2638 return wrap(unwrap<Argument>(V)->getParent());
2639}
2640
2642 Function *Func = unwrap<Function>(Fn);
2643 Function::arg_iterator I = Func->arg_begin();
2644 if (I == Func->arg_end())
2645 return nullptr;
2646 return wrap(&*I);
2647}
2648
2650 Function *Func = unwrap<Function>(Fn);
2651 Function::arg_iterator I = Func->arg_end();
2652 if (I == Func->arg_begin())
2653 return nullptr;
2654 return wrap(&*--I);
2655}
2656
2658 Argument *A = unwrap<Argument>(Arg);
2659 Function *Fn = A->getParent();
2660 if (A->getArgNo() + 1 >= Fn->arg_size())
2661 return nullptr;
2662 return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
2663}
2664
2666 Argument *A = unwrap<Argument>(Arg);
2667 if (A->getArgNo() == 0)
2668 return nullptr;
2669 return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
2670}
2671
2672void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
2673 Argument *A = unwrap<Argument>(Arg);
2674 A->addAttr(Attribute::getWithAlignment(A->getContext(), Align(align)));
2675}
2676
2677/*--.. Operations on ifuncs ................................................--*/
2678
2680 const char *Name, size_t NameLen,
2681 LLVMTypeRef Ty, unsigned AddrSpace,
2683 return wrap(GlobalIFunc::create(unwrap(Ty), AddrSpace,
2685 StringRef(Name, NameLen),
2686 unwrap<Constant>(Resolver), unwrap(M)));
2687}
2688
2690 const char *Name, size_t NameLen) {
2691 return wrap(unwrap(M)->getNamedIFunc(StringRef(Name, NameLen)));
2692}
2693
2695 Module *Mod = unwrap(M);
2696 Module::ifunc_iterator I = Mod->ifunc_begin();
2697 if (I == Mod->ifunc_end())
2698 return nullptr;
2699 return wrap(&*I);
2700}
2701
2703 Module *Mod = unwrap(M);
2704 Module::ifunc_iterator I = Mod->ifunc_end();
2705 if (I == Mod->ifunc_begin())
2706 return nullptr;
2707 return wrap(&*--I);
2708}
2709
2711 GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2713 if (++I == GIF->getParent()->ifunc_end())
2714 return nullptr;
2715 return wrap(&*I);
2716}
2717
2719 GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2721 if (I == GIF->getParent()->ifunc_begin())
2722 return nullptr;
2723 return wrap(&*--I);
2724}
2725
2727 return wrap(unwrap<GlobalIFunc>(IFunc)->getResolver());
2728}
2729
2731 unwrap<GlobalIFunc>(IFunc)->setResolver(unwrap<Constant>(Resolver));
2732}
2733
2735 unwrap<GlobalIFunc>(IFunc)->eraseFromParent();
2736}
2737
2739 unwrap<GlobalIFunc>(IFunc)->removeFromParent();
2740}
2741
2742/*--.. Operations on operand bundles........................................--*/
2743
2745 LLVMValueRef *Args,
2746 unsigned NumArgs) {
2747 return wrap(new OperandBundleDef(std::string(Tag, TagLen),
2748 ArrayRef(unwrap(Args), NumArgs)));
2749}
2750
2752 delete unwrap(Bundle);
2753}
2754
2755const char *LLVMGetOperandBundleTag(LLVMOperandBundleRef Bundle, size_t *Len) {
2756 StringRef Str = unwrap(Bundle)->getTag();
2757 *Len = Str.size();
2758 return Str.data();
2759}
2760
2762 return unwrap(Bundle)->inputs().size();
2763}
2764
2766 unsigned Index) {
2767 return wrap(unwrap(Bundle)->inputs()[Index]);
2768}
2769
2770/*--.. Operations on basic blocks ..........................................--*/
2771
2773 return wrap(static_cast<Value*>(unwrap(BB)));
2774}
2775
2777 return isa<BasicBlock>(unwrap(Val));
2778}
2779
2781 return wrap(unwrap<BasicBlock>(Val));
2782}
2783
2785 return unwrap(BB)->getName().data();
2786}
2787
2789 return wrap(unwrap(BB)->getParent());
2790}
2791
2793 return wrap(unwrap(BB)->getTerminator());
2794}
2795
2797 return unwrap<Function>(FnRef)->size();
2798}
2799
2801 Function *Fn = unwrap<Function>(FnRef);
2802 for (BasicBlock &BB : *Fn)
2803 *BasicBlocksRefs++ = wrap(&BB);
2804}
2805
2807 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2808}
2809
2811 Function *Func = unwrap<Function>(Fn);
2812 Function::iterator I = Func->begin();
2813 if (I == Func->end())
2814 return nullptr;
2815 return wrap(&*I);
2816}
2817
2819 Function *Func = unwrap<Function>(Fn);
2820 Function::iterator I = Func->end();
2821 if (I == Func->begin())
2822 return nullptr;
2823 return wrap(&*--I);
2824}
2825
2827 BasicBlock *Block = unwrap(BB);
2829 if (++I == Block->getParent()->end())
2830 return nullptr;
2831 return wrap(&*I);
2832}
2833
2835 BasicBlock *Block = unwrap(BB);
2837 if (I == Block->getParent()->begin())
2838 return nullptr;
2839 return wrap(&*--I);
2840}
2841
2843 const char *Name) {
2845}
2846
2848 LLVMBasicBlockRef BB) {
2849 BasicBlock *ToInsert = unwrap(BB);
2850 BasicBlock *CurBB = unwrap(Builder)->GetInsertBlock();
2851 assert(CurBB && "current insertion point is invalid!");
2852 CurBB->getParent()->insert(std::next(CurBB->getIterator()), ToInsert);
2853}
2854
2856 LLVMBasicBlockRef BB) {
2857 unwrap<Function>(Fn)->insert(unwrap<Function>(Fn)->end(), unwrap(BB));
2858}
2859
2861 LLVMValueRef FnRef,
2862 const char *Name) {
2863 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
2864}
2865
2868}
2869
2871 LLVMBasicBlockRef BBRef,
2872 const char *Name) {
2873 BasicBlock *BB = unwrap(BBRef);
2874 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2875}
2876
2878 const char *Name) {
2880}
2881
2883 unwrap(BBRef)->eraseFromParent();
2884}
2885
2887 unwrap(BBRef)->removeFromParent();
2888}
2889
2891 unwrap(BB)->moveBefore(unwrap(MovePos));
2892}
2893
2895 unwrap(BB)->moveAfter(unwrap(MovePos));
2896}
2897
2898/*--.. Operations on instructions ..........................................--*/
2899
2901 return wrap(unwrap<Instruction>(Inst)->getParent());
2902}
2903
2905 BasicBlock *Block = unwrap(BB);
2906 BasicBlock::iterator I = Block->begin();
2907 if (I == Block->end())
2908 return nullptr;
2909 return wrap(&*I);
2910}
2911
2913 BasicBlock *Block = unwrap(BB);
2914 BasicBlock::iterator I = Block->end();
2915 if (I == Block->begin())
2916 return nullptr;
2917 return wrap(&*--I);
2918}
2919
2921 Instruction *Instr = unwrap<Instruction>(Inst);
2922 BasicBlock::iterator I(Instr);
2923 if (++I == Instr->getParent()->end())
2924 return nullptr;
2925 return wrap(&*I);
2926}
2927
2929 Instruction *Instr = unwrap<Instruction>(Inst);
2930 BasicBlock::iterator I(Instr);
2931 if (I == Instr->getParent()->begin())
2932 return nullptr;
2933 return wrap(&*--I);
2934}
2935
2937 unwrap<Instruction>(Inst)->removeFromParent();
2938}
2939
2941 unwrap<Instruction>(Inst)->eraseFromParent();
2942}
2943
2945 unwrap<Instruction>(Inst)->deleteValue();
2946}
2947
2949 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2950 return (LLVMIntPredicate)I->getPredicate();
2951 return (LLVMIntPredicate)0;
2952}
2953
2955 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2956 return (LLVMRealPredicate)I->getPredicate();
2957 return (LLVMRealPredicate)0;
2958}
2959
2961 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2962 return map_to_llvmopcode(C->getOpcode());
2963 return (LLVMOpcode)0;
2964}
2965
2967 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2968 return wrap(C->clone());
2969 return nullptr;
2970}
2971
2973 Instruction *I = dyn_cast<Instruction>(unwrap(Inst));
2974 return (I && I->isTerminator()) ? wrap(I) : nullptr;
2975}
2976
2978 if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) {
2979 return FPI->arg_size();
2980 }
2981 return unwrap<CallBase>(Instr)->arg_size();
2982}
2983
2984/*--.. Call and invoke instructions ........................................--*/
2985
2987 return unwrap<CallBase>(Instr)->getCallingConv();
2988}
2989
2991 return unwrap<CallBase>(Instr)->setCallingConv(
2992 static_cast<CallingConv::ID>(CC));
2993}
2994
2996 unsigned align) {
2997 auto *Call = unwrap<CallBase>(Instr);
2998 Attribute AlignAttr =
2999 Attribute::getWithAlignment(Call->getContext(), Align(align));
3000 Call->addAttributeAtIndex(Idx, AlignAttr);
3001}
3002
3005 unwrap<CallBase>(C)->addAttributeAtIndex(Idx, unwrap(A));
3006}
3007
3010 auto *Call = unwrap<CallBase>(C);
3011 auto AS = Call->getAttributes().getAttributes(Idx);
3012 return AS.getNumAttributes();
3013}
3014
3016 LLVMAttributeRef *Attrs) {
3017 auto *Call = unwrap<CallBase>(C);
3018 auto AS = Call->getAttributes().getAttributes(Idx);
3019 for (auto A : AS)
3020 *Attrs++ = wrap(A);
3021}
3022
3025 unsigned KindID) {
3026 return wrap(unwrap<CallBase>(C)->getAttributeAtIndex(
3027 Idx, (Attribute::AttrKind)KindID));
3028}
3029
3032 const char *K, unsigned KLen) {
3033 return wrap(
3034 unwrap<CallBase>(C)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
3035}
3036
3038 unsigned KindID) {
3039 unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
3040}
3041
3043 const char *K, unsigned KLen) {
3044 unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
3045}
3046
3048 return wrap(unwrap<CallBase>(Instr)->getCalledOperand());
3049}
3050
3052 return wrap(unwrap<CallBase>(Instr)->getFunctionType());
3053}
3054
3056 return unwrap<CallBase>(C)->getNumOperandBundles();
3057}
3058
3060 unsigned Index) {
3061 return wrap(
3062 new OperandBundleDef(unwrap<CallBase>(C)->getOperandBundleAt(Index)));
3063}
3064
3065/*--.. Operations on call instructions (only) ..............................--*/
3066
3068 return unwrap<CallInst>(Call)->isTailCall();
3069}
3070
3071void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
3072 unwrap<CallInst>(Call)->setTailCall(isTailCall);
3073}
3074
3076 return (LLVMTailCallKind)unwrap<CallInst>(Call)->getTailCallKind();
3077}
3078
3080 unwrap<CallInst>(Call)->setTailCallKind((CallInst::TailCallKind)kind);
3081}
3082
3083/*--.. Operations on invoke instructions (only) ............................--*/
3084
3086 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
3087}
3088
3090 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
3091 return wrap(CRI->getUnwindDest());
3092 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
3093 return wrap(CSI->getUnwindDest());
3094 }
3095 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
3096}
3097
3099 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
3100}
3101
3103 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
3104 return CRI->setUnwindDest(unwrap(B));
3105 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
3106 return CSI->setUnwindDest(unwrap(B));
3107 }
3108 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
3109}
3110
3112 return wrap(unwrap<CallBrInst>(CallBr)->getDefaultDest());
3113}
3114
3116 return unwrap<CallBrInst>(CallBr)->getNumIndirectDests();
3117}
3118
3120 return wrap(unwrap<CallBrInst>(CallBr)->getIndirectDest(Idx));
3121}
3122
3123/*--.. Operations on terminators ...........................................--*/
3124
3126 return unwrap<Instruction>(Term)->getNumSuccessors();
3127}
3128
3130 return wrap(unwrap<Instruction>(Term)->getSuccessor(i));
3131}
3132
3134 return unwrap<Instruction>(Term)->setSuccessor(i, unwrap(block));
3135}
3136
3137/*--.. Operations on branch instructions (only) ............................--*/
3138
3140 return unwrap<BranchInst>(Branch)->isConditional();
3141}
3142
3144 return wrap(unwrap<BranchInst>(Branch)->getCondition());
3145}
3146
3148 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
3149}
3150
3151/*--.. Operations on switch instructions (only) ............................--*/
3152
3154 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
3155}
3156
3157/*--.. Operations on alloca instructions (only) ............................--*/
3158
3160 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
3161}
3162
3163/*--.. Operations on gep instructions (only) ...............................--*/
3164
3166 return unwrap<GEPOperator>(GEP)->isInBounds();
3167}
3168
3170 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
3171}
3172
3174 return wrap(unwrap<GEPOperator>(GEP)->getSourceElementType());
3175}
3176
3178 GEPOperator *GEPOp = unwrap<GEPOperator>(GEP);
3179 return mapToLLVMGEPNoWrapFlags(GEPOp->getNoWrapFlags());
3180}
3181
3183 GetElementPtrInst *GEPInst = unwrap<GetElementPtrInst>(GEP);
3184 GEPInst->setNoWrapFlags(mapFromLLVMGEPNoWrapFlags(NoWrapFlags));
3185}
3186
3187/*--.. Operations on phi nodes .............................................--*/
3188
3189void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
3190 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
3191 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
3192 for (unsigned I = 0; I != Count; ++I)
3193 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
3194}
3195
3197 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
3198}
3199
3201 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
3202}
3203
3205 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
3206}
3207
3208/*--.. Operations on extractvalue and insertvalue nodes ....................--*/
3209
3211 auto *I = unwrap(Inst);
3212 if (auto *GEP = dyn_cast<GEPOperator>(I))
3213 return GEP->getNumIndices();
3214 if (auto *EV = dyn_cast<ExtractValueInst>(I))
3215 return EV->getNumIndices();
3216 if (auto *IV = dyn_cast<InsertValueInst>(I))
3217 return IV->getNumIndices();
3219 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
3220}
3221
3222const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
3223 auto *I = unwrap(Inst);
3224 if (auto *EV = dyn_cast<ExtractValueInst>(I))
3225 return EV->getIndices().data();
3226 if (auto *IV = dyn_cast<InsertValueInst>(I))
3227 return IV->getIndices().data();
3229 "LLVMGetIndices applies only to extractvalue and insertvalue!");
3230}
3231
3232
3233/*===-- Instruction builders ----------------------------------------------===*/
3234
3236 return wrap(new IRBuilder<>(*unwrap(C)));
3237}
3238
3241}
3242
3244 Instruction *Instr, bool BeforeDbgRecords) {
3245 BasicBlock::iterator I = Instr ? Instr->getIterator() : Block->end();
3246 I.setHeadBit(BeforeDbgRecords);
3247 Builder->SetInsertPoint(Block, I);
3248}
3249
3251 LLVMValueRef Instr) {
3252 return LLVMPositionBuilderImpl(unwrap(Builder), unwrap(Block),
3253 unwrap<Instruction>(Instr), false);
3254}
3255
3258 LLVMValueRef Instr) {
3259 return LLVMPositionBuilderImpl(unwrap(Builder), unwrap(Block),
3260 unwrap<Instruction>(Instr), true);
3261}
3262
3264 Instruction *I = unwrap<Instruction>(Instr);
3265 return LLVMPositionBuilderImpl(unwrap(Builder), I->getParent(), I, false);
3266}
3267
3269 LLVMValueRef Instr) {
3270 Instruction *I = unwrap<Instruction>(Instr);
3271 return LLVMPositionBuilderImpl(unwrap(Builder), I->getParent(), I, true);
3272}
3273
3275 BasicBlock *BB = unwrap(Block);
3276 unwrap(Builder)->SetInsertPoint(BB);
3277}
3278
3280 return wrap(unwrap(Builder)->GetInsertBlock());
3281}
3282
3284 unwrap(Builder)->ClearInsertionPoint();
3285}
3286
3288 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
3289}
3290
3292 const char *Name) {
3293 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
3294}
3295
3297 delete unwrap(Builder);
3298}
3299
3300/*--.. Metadata builders ...................................................--*/
3301
3303 return wrap(unwrap(Builder)->getCurrentDebugLocation().getAsMDNode());
3304}
3305
3307 if (Loc)
3308 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(unwrap<MDNode>(Loc)));
3309 else
3310 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc());
3311}
3312
3314 MDNode *Loc =
3315 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
3316 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
3317}
3318
3320 LLVMContext &Context = unwrap(Builder)->getContext();
3322 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
3323}
3324
3326 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
3327}
3328
3330 unwrap(Builder)->AddMetadataToInst(unwrap<Instruction>(Inst));
3331}
3332
3334 LLVMMetadataRef FPMathTag) {
3335
3336 unwrap(Builder)->setDefaultFPMathTag(FPMathTag
3337 ? unwrap<MDNode>(FPMathTag)
3338 : nullptr);
3339}
3340
3342 return wrap(&unwrap(Builder)->getContext());
3343}
3344
3346 return wrap(unwrap(Builder)->getDefaultFPMathTag());
3347}
3348
3349/*--.. Instruction builders ................................................--*/
3350
3352 return wrap(unwrap(B)->CreateRetVoid());
3353}
3354
3356 return wrap(unwrap(B)->CreateRet(unwrap(V)));
3357}
3358
3360 unsigned N) {
3361 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
3362}
3363
3365 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
3366}
3367
3370 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
3371}
3372
3374 LLVMBasicBlockRef Else, unsigned NumCases) {
3375 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
3376}
3377
3379 unsigned NumDests) {
3380 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
3381}
3382
3384 LLVMBasicBlockRef DefaultDest,
3385 LLVMBasicBlockRef *IndirectDests,
3386 unsigned NumIndirectDests, LLVMValueRef *Args,
3387 unsigned NumArgs, LLVMOperandBundleRef *Bundles,
3388 unsigned NumBundles, const char *Name) {
3389
3391 for (auto *Bundle : ArrayRef(Bundles, NumBundles)) {
3392 OperandBundleDef *OB = unwrap(Bundle);
3393 OBs.push_back(*OB);
3394 }
3395
3396 return wrap(unwrap(B)->CreateCallBr(
3397 unwrap<FunctionType>(Ty), unwrap(Fn), unwrap(DefaultDest),
3398 ArrayRef(unwrap(IndirectDests), NumIndirectDests),
3399 ArrayRef<Value *>(unwrap(Args), NumArgs), OBs, Name));
3400}
3401
3403 LLVMValueRef *Args, unsigned NumArgs,
3405 const char *Name) {
3406 return wrap(unwrap(B)->CreateInvoke(unwrap<FunctionType>(Ty), unwrap(Fn),
3407 unwrap(Then), unwrap(Catch),
3408 ArrayRef(unwrap(Args), NumArgs), Name));
3409}
3410
3413 unsigned NumArgs, LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
3414 LLVMOperandBundleRef *Bundles, unsigned NumBundles, const char *Name) {
3416 for (auto *Bundle : ArrayRef(Bundles, NumBundles)) {
3417 OperandBundleDef *OB = unwrap(Bundle);
3418 OBs.push_back(*OB);
3419 }
3420 return wrap(unwrap(B)->CreateInvoke(
3421 unwrap<FunctionType>(Ty), unwrap(Fn), unwrap(Then), unwrap(Catch),
3422 ArrayRef(unwrap(Args), NumArgs), OBs, Name));
3423}
3424
3426 LLVMValueRef PersFn, unsigned NumClauses,
3427 const char *Name) {
3428 // The personality used to live on the landingpad instruction, but now it
3429 // lives on the parent function. For compatibility, take the provided
3430 // personality and put it on the parent function.
3431 if (PersFn)
3432 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
3433 unwrap<Function>(PersFn));
3434 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
3435}
3436
3438 LLVMValueRef *Args, unsigned NumArgs,
3439 const char *Name) {
3440 return wrap(unwrap(B)->CreateCatchPad(unwrap(ParentPad),
3441 ArrayRef(unwrap(Args), NumArgs), Name));
3442}
3443
3445 LLVMValueRef *Args, unsigned NumArgs,
3446 const char *Name) {
3447 if (ParentPad == nullptr) {
3448 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3449 ParentPad = wrap(Constant::getNullValue(Ty));
3450 }
3451 return wrap(unwrap(B)->CreateCleanupPad(
3452 unwrap(ParentPad), ArrayRef(unwrap(Args), NumArgs), Name));
3453}
3454
3456 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
3457}
3458
3460 LLVMBasicBlockRef UnwindBB,
3461 unsigned NumHandlers, const char *Name) {
3462 if (ParentPad == nullptr) {
3463 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3464 ParentPad = wrap(Constant::getNullValue(Ty));
3465 }
3466 return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(UnwindBB),
3467 NumHandlers, Name));
3468}
3469
3471 LLVMBasicBlockRef BB) {
3472 return wrap(unwrap(B)->CreateCatchRet(unwrap<CatchPadInst>(CatchPad),
3473 unwrap(BB)));
3474}
3475
3477 LLVMBasicBlockRef BB) {
3478 return wrap(unwrap(B)->CreateCleanupRet(unwrap<CleanupPadInst>(CatchPad),
3479 unwrap(BB)));
3480}
3481
3483 return wrap(unwrap(B)->CreateUnreachable());
3484}
3485
3487 LLVMBasicBlockRef Dest) {
3488 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
3489}
3490
3492 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
3493}
3494
3495unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
3496 return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
3497}
3498
3500 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
3501}
3502
3504 unwrap<LandingPadInst>(LandingPad)->addClause(unwrap<Constant>(ClauseVal));
3505}
3506
3508 return unwrap<LandingPadInst>(LandingPad)->isCleanup();
3509}
3510
3511void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
3512 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
3513}
3514
3516 unwrap<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Dest));
3517}
3518
3519unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) {
3520 return unwrap<CatchSwitchInst>(CatchSwitch)->getNumHandlers();
3521}
3522
3523void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) {
3524 CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(CatchSwitch);
3525 for (const BasicBlock *H : CSI->handlers())
3526 *Handlers++ = wrap(H);
3527}
3528
3530 return wrap(unwrap<CatchPadInst>(CatchPad)->getCatchSwitch());
3531}
3532
3534 unwrap<CatchPadInst>(CatchPad)
3535 ->setCatchSwitch(unwrap<CatchSwitchInst>(CatchSwitch));
3536}
3537
3538/*--.. Funclets ...........................................................--*/
3539
3541 return wrap(unwrap<FuncletPadInst>(Funclet)->getArgOperand(i));
3542}
3543
3545 unwrap<FuncletPadInst>(Funclet)->setArgOperand(i, unwrap(value));
3546}
3547
3548/*--.. Arithmetic ..........................................................--*/
3549
3551 FastMathFlags NewFMF;
3552 NewFMF.setAllowReassoc((FMF & LLVMFastMathAllowReassoc) != 0);
3553 NewFMF.setNoNaNs((FMF & LLVMFastMathNoNaNs) != 0);
3554 NewFMF.setNoInfs((FMF & LLVMFastMathNoInfs) != 0);
3555 NewFMF.setNoSignedZeros((FMF & LLVMFastMathNoSignedZeros) != 0);
3557 NewFMF.setAllowContract((FMF & LLVMFastMathAllowContract) != 0);
3558 NewFMF.setApproxFunc((FMF & LLVMFastMathApproxFunc) != 0);
3559
3560 return NewFMF;
3561}
3562
3565 if (FMF.allowReassoc())
3566 NewFMF |= LLVMFastMathAllowReassoc;
3567 if (FMF.noNaNs())
3568 NewFMF |= LLVMFastMathNoNaNs;
3569 if (FMF.noInfs())
3570 NewFMF |= LLVMFastMathNoInfs;
3571 if (FMF.noSignedZeros())
3572 NewFMF |= LLVMFastMathNoSignedZeros;
3573 if (FMF.allowReciprocal())
3575 if (FMF.allowContract())
3576 NewFMF |= LLVMFastMathAllowContract;
3577 if (FMF.approxFunc())
3578 NewFMF |= LLVMFastMathApproxFunc;
3579
3580 return NewFMF;
3581}
3582
3584 const char *Name) {
3585 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
3586}
3587
3589 const char *Name) {
3590 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
3591}
3592
3594 const char *Name) {
3595 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
3596}
3597
3599 const char *Name) {
3600 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
3601}
3602
3604 const char *Name) {
3605 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
3606}
3607
3609 const char *Name) {
3610 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
3611}
3612
3614 const char *Name) {
3615 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
3616}
3617
3619 const char *Name) {
3620 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
3621}
3622
3624 const char *Name) {
3625 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
3626}
3627
3629 const char *Name) {
3630 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
3631}
3632
3634 const char *Name) {
3635 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
3636}
3637
3639 const char *Name) {
3640 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
3641}
3642
3644 const char *Name) {
3645 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
3646}
3647
3649 LLVMValueRef RHS, const char *Name) {
3650 return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
3651}
3652
3654 const char *Name) {
3655 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
3656}
3657
3659 LLVMValueRef RHS, const char *Name) {
3660 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
3661}
3662
3664 const char *Name) {
3665 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
3666}
3667
3669 const char *Name) {
3670 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
3671}
3672
3674 const char *Name) {
3675 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
3676}
3677
3679 const char *Name) {
3680 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
3681}
3682
3684 const char *Name) {
3685 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
3686}
3687
3689 const char *Name) {
3690 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
3691}
3692
3694 const char *Name) {
3695 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
3696}
3697
3699 const char *Name) {
3700 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
3701}
3702
3704 const char *Name) {
3705 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
3706}
3707
3709 const char *Name) {
3710 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
3711}
3712
3714 LLVMValueRef LHS, LLVMValueRef RHS,
3715 const char *Name) {
3717 unwrap(RHS), Name));
3718}
3719
3721 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
3722}
3723
3725 const char *Name) {
3726 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
3727}
3728
3730 const char *Name) {
3731 Value *Neg = unwrap(B)->CreateNeg(unwrap(V), Name);
3732 if (auto *I = dyn_cast<BinaryOperator>(Neg))
3733 I->setHasNoUnsignedWrap();
3734 return wrap(Neg);
3735}
3736
3738 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
3739}
3740
3742 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
3743}
3744
3746 Value *P = unwrap<Value>(ArithInst);
3747 return cast<Instruction>(P)->hasNoUnsignedWrap();
3748}
3749
3750void LLVMSetNUW(LLVMValueRef ArithInst, LLVMBool HasNUW) {
3751 Value *P = unwrap<Value>(ArithInst);
3752 cast<Instruction>(P)->setHasNoUnsignedWrap(HasNUW);
3753}
3754
3756 Value *P = unwrap<Value>(ArithInst);
3757 return cast<Instruction>(P)->hasNoSignedWrap();
3758}
3759
3760void LLVMSetNSW(LLVMValueRef ArithInst, LLVMBool HasNSW) {
3761 Value *P = unwrap<Value>(ArithInst);
3762 cast<Instruction>(P)->setHasNoSignedWrap(HasNSW);
3763}
3764
3766 Value *P = unwrap<Value>(DivOrShrInst);
3767 return cast<Instruction>(P)->isExact();
3768}
3769
3770void LLVMSetExact(LLVMValueRef DivOrShrInst, LLVMBool IsExact) {
3771 Value *P = unwrap<Value>(DivOrShrInst);
3772 cast<Instruction>(P)->setIsExact(IsExact);
3773}
3774
3776 Value *P = unwrap<Value>(NonNegInst);
3777 return cast<Instruction>(