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-c/Types.h"
16#include "llvm/IR/Attributes.h"
17#include "llvm/IR/BasicBlock.h"
19#include "llvm/IR/Constants.h"
25#include "llvm/IR/GlobalAlias.h"
27#include "llvm/IR/IRBuilder.h"
28#include "llvm/IR/InlineAsm.h"
31#include "llvm/IR/LLVMContext.h"
33#include "llvm/IR/Module.h"
35#include "llvm/PassRegistry.h"
36#include "llvm/Support/Debug.h"
44#include <cassert>
45#include <cstdlib>
46#include <cstring>
47#include <system_error>
48
49using namespace llvm;
50
52
54 return reinterpret_cast<BasicBlock **>(BBs);
55}
56
57#define DEBUG_TYPE "ir"
58
65}
66
69}
70
71/*===-- Version query -----------------------------------------------------===*/
72
73void LLVMGetVersion(unsigned *Major, unsigned *Minor, unsigned *Patch) {
74 if (Major)
75 *Major = LLVM_VERSION_MAJOR;
76 if (Minor)
77 *Minor = LLVM_VERSION_MINOR;
78 if (Patch)
79 *Patch = LLVM_VERSION_PATCH;
80}
81
82/*===-- Error handling ----------------------------------------------------===*/
83
84char *LLVMCreateMessage(const char *Message) {
85 return strdup(Message);
86}
87
88void LLVMDisposeMessage(char *Message) {
89 free(Message);
90}
91
92
93/*===-- Operations on contexts --------------------------------------------===*/
94
96 static LLVMContext GlobalContext;
97 return GlobalContext;
98}
99
101 return wrap(new LLVMContext());
102}
103
105
107 LLVMDiagnosticHandler Handler,
108 void *DiagnosticContext) {
109 unwrap(C)->setDiagnosticHandlerCallBack(
111 Handler),
112 DiagnosticContext);
113}
114
116 return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>(
117 unwrap(C)->getDiagnosticHandlerCallBack());
118}
119
121 return unwrap(C)->getDiagnosticContext();
122}
123
125 void *OpaqueHandle) {
126 auto YieldCallback =
127 LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
128 unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
129}
130
132 return unwrap(C)->shouldDiscardValueNames();
133}
134
136 unwrap(C)->setDiscardValueNames(Discard);
137}
138
140 delete unwrap(C);
141}
142
144 unsigned SLen) {
145 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
146}
147
148unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) {
150}
151
152unsigned LLVMGetSyncScopeID(LLVMContextRef C, const char *Name, size_t SLen) {
153 return unwrap(C)->getOrInsertSyncScopeID(StringRef(Name, SLen));
154}
155
156unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) {
158}
159
161 return Attribute::AttrKind::EndAttrKinds;
162}
163
165 uint64_t Val) {
166 auto &Ctx = *unwrap(C);
167 auto AttrKind = (Attribute::AttrKind)KindID;
168 return wrap(Attribute::get(Ctx, AttrKind, Val));
169}
170
172 return unwrap(A).getKindAsEnum();
173}
174
176 auto Attr = unwrap(A);
177 if (Attr.isEnumAttribute())
178 return 0;
179 return Attr.getValueAsInt();
180}
181
183 LLVMTypeRef type_ref) {
184 auto &Ctx = *unwrap(C);
185 auto AttrKind = (Attribute::AttrKind)KindID;
186 return wrap(Attribute::get(Ctx, AttrKind, unwrap(type_ref)));
187}
188
190 auto Attr = unwrap(A);
191 return wrap(Attr.getValueAsType());
192}
193
195 unsigned KindID,
196 unsigned NumBits,
197 const uint64_t LowerWords[],
198 const uint64_t UpperWords[]) {
199 auto &Ctx = *unwrap(C);
200 auto AttrKind = (Attribute::AttrKind)KindID;
201 unsigned NumWords = divideCeil(NumBits, 64);
202 return wrap(Attribute::get(
203 Ctx, AttrKind,
204 ConstantRange(APInt(NumBits, ArrayRef(LowerWords, NumWords)),
205 APInt(NumBits, ArrayRef(UpperWords, NumWords)))));
206}
207
209 const char *K, unsigned KLength,
210 const char *V, unsigned VLength) {
211 return wrap(Attribute::get(*unwrap(C), StringRef(K, KLength),
212 StringRef(V, VLength)));
213}
214
216 unsigned *Length) {
217 auto S = unwrap(A).getKindAsString();
218 *Length = S.size();
219 return S.data();
220}
221
223 unsigned *Length) {
224 auto S = unwrap(A).getValueAsString();
225 *Length = S.size();
226 return S.data();
227}
228
230 auto Attr = unwrap(A);
231 return Attr.isEnumAttribute() || Attr.isIntAttribute();
232}
233
235 return unwrap(A).isStringAttribute();
236}
237
239 return unwrap(A).isTypeAttribute();
240}
241
243 std::string MsgStorage;
244 raw_string_ostream Stream(MsgStorage);
246
247 unwrap(DI)->print(DP);
248 Stream.flush();
249
250 return LLVMCreateMessage(MsgStorage.c_str());
251}
252
254 LLVMDiagnosticSeverity severity;
255
256 switch(unwrap(DI)->getSeverity()) {
257 default:
258 severity = LLVMDSError;
259 break;
260 case DS_Warning:
261 severity = LLVMDSWarning;
262 break;
263 case DS_Remark:
264 severity = LLVMDSRemark;
265 break;
266 case DS_Note:
267 severity = LLVMDSNote;
268 break;
269 }
270
271 return severity;
272}
273
274/*===-- Operations on modules ---------------------------------------------===*/
275
277 return wrap(new Module(ModuleID, getGlobalContext()));
278}
279
282 return wrap(new Module(ModuleID, *unwrap(C)));
283}
284
286 delete unwrap(M);
287}
288
289const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) {
290 auto &Str = unwrap(M)->getModuleIdentifier();
291 *Len = Str.length();
292 return Str.c_str();
293}
294
295void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) {
296 unwrap(M)->setModuleIdentifier(StringRef(Ident, Len));
297}
298
299const char *LLVMGetSourceFileName(LLVMModuleRef M, size_t *Len) {
300 auto &Str = unwrap(M)->getSourceFileName();
301 *Len = Str.length();
302 return Str.c_str();
303}
304
305void LLVMSetSourceFileName(LLVMModuleRef M, const char *Name, size_t Len) {
306 unwrap(M)->setSourceFileName(StringRef(Name, Len));
307}
308
309/*--.. Data layout .........................................................--*/
311 return unwrap(M)->getDataLayoutStr().c_str();
312}
313
315 return LLVMGetDataLayoutStr(M);
316}
317
318void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
319 unwrap(M)->setDataLayout(DataLayoutStr);
320}
321
322/*--.. Target triple .......................................................--*/
324 return unwrap(M)->getTargetTriple().c_str();
325}
326
327void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
328 unwrap(M)->setTargetTriple(Triple);
329}
330
331/*--.. Module flags ........................................................--*/
334 const char *Key;
335 size_t KeyLen;
337};
338
341 switch (Behavior) {
343 return Module::ModFlagBehavior::Error;
345 return Module::ModFlagBehavior::Warning;
347 return Module::ModFlagBehavior::Require;
349 return Module::ModFlagBehavior::Override;
351 return Module::ModFlagBehavior::Append;
353 return Module::ModFlagBehavior::AppendUnique;
354 }
355 llvm_unreachable("Unknown LLVMModuleFlagBehavior");
356}
357
360 switch (Behavior) {
361 case Module::ModFlagBehavior::Error:
363 case Module::ModFlagBehavior::Warning:
365 case Module::ModFlagBehavior::Require:
367 case Module::ModFlagBehavior::Override:
369 case Module::ModFlagBehavior::Append:
371 case Module::ModFlagBehavior::AppendUnique:
373 default:
374 llvm_unreachable("Unhandled Flag Behavior");
375 }
376}
377
380 unwrap(M)->getModuleFlagsMetadata(MFEs);
381
383 safe_malloc(MFEs.size() * sizeof(LLVMOpaqueModuleFlagEntry)));
384 for (unsigned i = 0; i < MFEs.size(); ++i) {
385 const auto &ModuleFlag = MFEs[i];
386 Result[i].Behavior = map_from_llvmModFlagBehavior(ModuleFlag.Behavior);
387 Result[i].Key = ModuleFlag.Key->getString().data();
388 Result[i].KeyLen = ModuleFlag.Key->getString().size();
389 Result[i].Metadata = wrap(ModuleFlag.Val);
390 }
391 *Len = MFEs.size();
392 return Result;
393}
394
396 free(Entries);
397}
398
401 unsigned Index) {
403 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
404 return MFE.Behavior;
405}
406
408 unsigned Index, size_t *Len) {
410 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
411 *Len = MFE.KeyLen;
412 return MFE.Key;
413}
414
416 unsigned Index) {
418 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
419 return MFE.Metadata;
420}
421
423 const char *Key, size_t KeyLen) {
424 return wrap(unwrap(M)->getModuleFlag({Key, KeyLen}));
425}
426
428 const char *Key, size_t KeyLen,
429 LLVMMetadataRef Val) {
430 unwrap(M)->addModuleFlag(map_to_llvmModFlagBehavior(Behavior),
431 {Key, KeyLen}, unwrap(Val));
432}
433
435 return unwrap(M)->IsNewDbgInfoFormat;
436}
437
439 unwrap(M)->setIsNewDbgInfoFormat(UseNewFormat);
440}
441
442/*--.. Printing modules ....................................................--*/
443
445 unwrap(M)->print(errs(), nullptr,
446 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
447}
448
450 char **ErrorMessage) {
451 std::error_code EC;
452 raw_fd_ostream dest(Filename, EC, sys::fs::OF_TextWithCRLF);
453 if (EC) {
454 *ErrorMessage = strdup(EC.message().c_str());
455 return true;
456 }
457
458 unwrap(M)->print(dest, nullptr);
459
460 dest.close();
461
462 if (dest.has_error()) {
463 std::string E = "Error printing to file: " + dest.error().message();
464 *ErrorMessage = strdup(E.c_str());
465 return true;
466 }
467
468 return false;
469}
470
472 std::string buf;
473 raw_string_ostream os(buf);
474
475 unwrap(M)->print(os, nullptr);
476 os.flush();
477
478 return strdup(buf.c_str());
479}
480
481/*--.. Operations on inline assembler ......................................--*/
482void LLVMSetModuleInlineAsm2(LLVMModuleRef M, const char *Asm, size_t Len) {
483 unwrap(M)->setModuleInlineAsm(StringRef(Asm, Len));
484}
485
486void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
487 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
488}
489
490void LLVMAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm, size_t Len) {
491 unwrap(M)->appendModuleInlineAsm(StringRef(Asm, Len));
492}
493
494const char *LLVMGetModuleInlineAsm(LLVMModuleRef M, size_t *Len) {
495 auto &Str = unwrap(M)->getModuleInlineAsm();
496 *Len = Str.length();
497 return Str.c_str();
498}
499
500LLVMValueRef LLVMGetInlineAsm(LLVMTypeRef Ty, const char *AsmString,
501 size_t AsmStringSize, const char *Constraints,
502 size_t ConstraintsSize, LLVMBool HasSideEffects,
503 LLVMBool IsAlignStack,
504 LLVMInlineAsmDialect Dialect, LLVMBool CanThrow) {
506 switch (Dialect) {
509 break;
512 break;
513 }
514 return wrap(InlineAsm::get(unwrap<FunctionType>(Ty),
515 StringRef(AsmString, AsmStringSize),
516 StringRef(Constraints, ConstraintsSize),
517 HasSideEffects, IsAlignStack, AD, CanThrow));
518}
519
520const char *LLVMGetInlineAsmAsmString(LLVMValueRef InlineAsmVal, size_t *Len) {
521
522 Value *Val = unwrap<Value>(InlineAsmVal);
523 const std::string &AsmString = cast<InlineAsm>(Val)->getAsmString();
524
525 *Len = AsmString.length();
526 return AsmString.c_str();
527}
528
530 size_t *Len) {
531 Value *Val = unwrap<Value>(InlineAsmVal);
532 const std::string &ConstraintString =
533 cast<InlineAsm>(Val)->getConstraintString();
534
535 *Len = ConstraintString.length();
536 return ConstraintString.c_str();
537}
538
540
541 Value *Val = unwrap<Value>(InlineAsmVal);
542 InlineAsm::AsmDialect Dialect = cast<InlineAsm>(Val)->getDialect();
543
544 switch (Dialect) {
549 }
550
551 llvm_unreachable("Unrecognized inline assembly dialect");
553}
554
556 Value *Val = unwrap<Value>(InlineAsmVal);
557 return (LLVMTypeRef)cast<InlineAsm>(Val)->getFunctionType();
558}
559
561 Value *Val = unwrap<Value>(InlineAsmVal);
562 return cast<InlineAsm>(Val)->hasSideEffects();
563}
564
566 Value *Val = unwrap<Value>(InlineAsmVal);
567 return cast<InlineAsm>(Val)->isAlignStack();
568}
569
571 Value *Val = unwrap<Value>(InlineAsmVal);
572 return cast<InlineAsm>(Val)->canThrow();
573}
574
575/*--.. Operations on module contexts ......................................--*/
577 return wrap(&unwrap(M)->getContext());
578}
579
580
581/*===-- Operations on types -----------------------------------------------===*/
582
583/*--.. Operations on all types (mostly) ....................................--*/
584
586 switch (unwrap(Ty)->getTypeID()) {
587 case Type::VoidTyID:
588 return LLVMVoidTypeKind;
589 case Type::HalfTyID:
590 return LLVMHalfTypeKind;
591 case Type::BFloatTyID:
592 return LLVMBFloatTypeKind;
593 case Type::FloatTyID:
594 return LLVMFloatTypeKind;
595 case Type::DoubleTyID:
596 return LLVMDoubleTypeKind;
599 case Type::FP128TyID:
600 return LLVMFP128TypeKind;
603 case Type::LabelTyID:
604 return LLVMLabelTypeKind;
608 return LLVMIntegerTypeKind;
611 case Type::StructTyID:
612 return LLVMStructTypeKind;
613 case Type::ArrayTyID:
614 return LLVMArrayTypeKind;
616 return LLVMPointerTypeKind;
618 return LLVMVectorTypeKind;
620 return LLVMX86_AMXTypeKind;
621 case Type::TokenTyID:
622 return LLVMTokenTypeKind;
628 llvm_unreachable("Typed pointers are unsupported via the C API");
629 }
630 llvm_unreachable("Unhandled TypeID.");
631}
632
634{
635 return unwrap(Ty)->isSized();
636}
637
639 return wrap(&unwrap(Ty)->getContext());
640}
641
643 return unwrap(Ty)->print(errs(), /*IsForDebug=*/true);
644}
645
647 std::string buf;
648 raw_string_ostream os(buf);
649
650 if (unwrap(Ty))
651 unwrap(Ty)->print(os);
652 else
653 os << "Printing <null> Type";
654
655 os.flush();
656
657 return strdup(buf.c_str());
658}
659
660/*--.. Operations on integer types .........................................--*/
661
664}
667}
670}
673}
676}
679}
681 return wrap(IntegerType::get(*unwrap(C), NumBits));
682}
683
686}
689}
692}
695}
698}
701}
702LLVMTypeRef LLVMIntType(unsigned NumBits) {
704}
705
706unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
707 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
708}
709
710/*--.. Operations on real types ............................................--*/
711
714}
717}
720}
723}
726}
729}
732}
735}
736
739}
742}
745}
748}
751}
754}
757}
760}
761
762/*--.. Operations on function types ........................................--*/
763
765 LLVMTypeRef *ParamTypes, unsigned ParamCount,
766 LLVMBool IsVarArg) {
767 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
768 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
769}
770
772 return unwrap<FunctionType>(FunctionTy)->isVarArg();
773}
774
776 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
777}
778
779unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
780 return unwrap<FunctionType>(FunctionTy)->getNumParams();
781}
782
784 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
785 for (Type *T : Ty->params())
786 *Dest++ = wrap(T);
787}
788
789/*--.. Operations on struct types ..........................................--*/
790
792 unsigned ElementCount, LLVMBool Packed) {
793 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
794 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
795}
796
798 unsigned ElementCount, LLVMBool Packed) {
799 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
800 ElementCount, Packed);
801}
802
804{
805 return wrap(StructType::create(*unwrap(C), Name));
806}
807
809{
810 StructType *Type = unwrap<StructType>(Ty);
811 if (!Type->hasName())
812 return nullptr;
813 return Type->getName().data();
814}
815
816void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
817 unsigned ElementCount, LLVMBool Packed) {
818 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
819 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
820}
821
823 return unwrap<StructType>(StructTy)->getNumElements();
824}
825
827 StructType *Ty = unwrap<StructType>(StructTy);
828 for (Type *T : Ty->elements())
829 *Dest++ = wrap(T);
830}
831
833 StructType *Ty = unwrap<StructType>(StructTy);
834 return wrap(Ty->getTypeAtIndex(i));
835}
836
838 return unwrap<StructType>(StructTy)->isPacked();
839}
840
842 return unwrap<StructType>(StructTy)->isOpaque();
843}
844
846 return unwrap<StructType>(StructTy)->isLiteral();
847}
848
850 return wrap(StructType::getTypeByName(unwrap(M)->getContext(), Name));
851}
852
855}
856
857/*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
858
860 int i = 0;
861 for (auto *T : unwrap(Tp)->subtypes()) {
862 Arr[i] = wrap(T);
863 i++;
864 }
865}
866
868 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
869}
870
872 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
873}
874
876 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
877}
878
880 return true;
881}
882
884 return wrap(FixedVectorType::get(unwrap(ElementType), ElementCount));
885}
886
888 unsigned ElementCount) {
889 return wrap(ScalableVectorType::get(unwrap(ElementType), ElementCount));
890}
891
893 auto *Ty = unwrap(WrappedTy);
894 if (auto *ATy = dyn_cast<ArrayType>(Ty))
895 return wrap(ATy->getElementType());
896 return wrap(cast<VectorType>(Ty)->getElementType());
897}
898
900 return unwrap(Tp)->getNumContainedTypes();
901}
902
904 return unwrap<ArrayType>(ArrayTy)->getNumElements();
905}
906
908 return unwrap<ArrayType>(ArrayTy)->getNumElements();
909}
910
912 return unwrap<PointerType>(PointerTy)->getAddressSpace();
913}
914
915unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
916 return unwrap<VectorType>(VectorTy)->getElementCount().getKnownMinValue();
917}
918
920 return wrap(unwrap<ConstantPtrAuth>(PtrAuth)->getPointer());
921}
922
924 return wrap(unwrap<ConstantPtrAuth>(PtrAuth)->getKey());
925}
926
928 return wrap(unwrap<ConstantPtrAuth>(PtrAuth)->getDiscriminator());
929}
930
932 return wrap(unwrap<ConstantPtrAuth>(PtrAuth)->getAddrDiscriminator());
933}
934
935/*--.. Operations on other types ...........................................--*/
936
938 return wrap(PointerType::get(*unwrap(C), AddressSpace));
939}
940
942 return wrap(Type::getVoidTy(*unwrap(C)));
943}
945 return wrap(Type::getLabelTy(*unwrap(C)));
946}
948 return wrap(Type::getTokenTy(*unwrap(C)));
949}
951 return wrap(Type::getMetadataTy(*unwrap(C)));
952}
953
956}
959}
960
962 LLVMTypeRef *TypeParams,
963 unsigned TypeParamCount,
964 unsigned *IntParams,
965 unsigned IntParamCount) {
966 ArrayRef<Type *> TypeParamArray(unwrap(TypeParams), TypeParamCount);
967 ArrayRef<unsigned> IntParamArray(IntParams, IntParamCount);
968 return wrap(
969 TargetExtType::get(*unwrap(C), Name, TypeParamArray, IntParamArray));
970}
971
972const char *LLVMGetTargetExtTypeName(LLVMTypeRef TargetExtTy) {
973 TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);
974 return Type->getName().data();
975}
976
978 TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);
979 return Type->getNumTypeParameters();
980}
981
983 unsigned Idx) {
984 TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);
985 return wrap(Type->getTypeParameter(Idx));
986}
987
989 TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);
990 return Type->getNumIntParameters();
991}
992
993unsigned LLVMGetTargetExtTypeIntParam(LLVMTypeRef TargetExtTy, unsigned Idx) {
994 TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);
995 return Type->getIntParameter(Idx);
996}
997
998/*===-- Operations on values ----------------------------------------------===*/
999
1000/*--.. Operations on all values ............................................--*/
1001
1003 return wrap(unwrap(Val)->getType());
1004}
1005
1007 switch(unwrap(Val)->getValueID()) {
1008#define LLVM_C_API 1
1009#define HANDLE_VALUE(Name) \
1010 case Value::Name##Val: \
1011 return LLVM##Name##ValueKind;
1012#include "llvm/IR/Value.def"
1013 default:
1015 }
1016}
1017
1018const char *LLVMGetValueName2(LLVMValueRef Val, size_t *Length) {
1019 auto *V = unwrap(Val);
1020 *Length = V->getName().size();
1021 return V->getName().data();
1022}
1023
1024void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen) {
1025 unwrap(Val)->setName(StringRef(Name, NameLen));
1026}
1027
1029 return unwrap(Val)->getName().data();
1030}
1031
1032void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
1033 unwrap(Val)->setName(Name);
1034}
1035
1037 unwrap(Val)->print(errs(), /*IsForDebug=*/true);
1038}
1039
1041 std::string buf;
1042 raw_string_ostream os(buf);
1043
1044 if (unwrap(Val))
1045 unwrap(Val)->print(os);
1046 else
1047 os << "Printing <null> Value";
1048
1049 os.flush();
1050
1051 return strdup(buf.c_str());
1052}
1053
1055 return wrap(&unwrap(Val)->getContext());
1056}
1057
1059 std::string buf;
1060 raw_string_ostream os(buf);
1061
1062 if (unwrap(Record))
1063 unwrap(Record)->print(os);
1064 else
1065 os << "Printing <null> DbgRecord";
1066
1067 os.flush();
1068
1069 return strdup(buf.c_str());
1070}
1071
1073 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
1074}
1075
1077 return unwrap<Instruction>(Inst)->hasMetadata();
1078}
1079
1081 auto *I = unwrap<Instruction>(Inst);
1082 assert(I && "Expected instruction");
1083 if (auto *MD = I->getMetadata(KindID))
1084 return wrap(MetadataAsValue::get(I->getContext(), MD));
1085 return nullptr;
1086}
1087
1088// MetadataAsValue uses a canonical format which strips the actual MDNode for
1089// MDNode with just a single constant value, storing just a ConstantAsMetadata
1090// This undoes this canonicalization, reconstructing the MDNode.
1092 Metadata *MD = MAV->getMetadata();
1093 assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
1094 "Expected a metadata node or a canonicalized constant");
1095
1096 if (MDNode *N = dyn_cast<MDNode>(MD))
1097 return N;
1098
1099 return MDNode::get(MAV->getContext(), MD);
1100}
1101
1102void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
1103 MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
1104
1105 unwrap<Instruction>(Inst)->setMetadata(KindID, N);
1106}
1107
1109 unsigned Kind;
1111};
1112
1115llvm_getMetadata(size_t *NumEntries,
1116 llvm::function_ref<void(MetadataEntries &)> AccessMD) {
1118 AccessMD(MVEs);
1119
1121 static_cast<LLVMOpaqueValueMetadataEntry *>(
1123 for (unsigned i = 0; i < MVEs.size(); ++i) {
1124 const auto &ModuleFlag = MVEs[i];
1125 Result[i].Kind = ModuleFlag.first;
1126 Result[i].Metadata = wrap(ModuleFlag.second);
1127 }
1128 *NumEntries = MVEs.size();
1129 return Result;
1130}
1131
1134 size_t *NumEntries) {
1135 return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
1136 Entries.clear();
1137 unwrap<Instruction>(Value)->getAllMetadata(Entries);
1138 });
1139}
1140
1141/*--.. Conversion functions ................................................--*/
1142
1143#define LLVM_DEFINE_VALUE_CAST(name) \
1144 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
1145 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
1146 }
1147
1149
1151 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
1152 if (isa<MDNode>(MD->getMetadata()) ||
1153 isa<ValueAsMetadata>(MD->getMetadata()))
1154 return Val;
1155 return nullptr;
1156}
1157
1159 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
1160 if (isa<ValueAsMetadata>(MD->getMetadata()))
1161 return Val;
1162 return nullptr;
1163}
1164
1166 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
1167 if (isa<MDString>(MD->getMetadata()))
1168 return Val;
1169 return nullptr;
1170}
1171
1172/*--.. Operations on Uses ..................................................--*/
1174 Value *V = unwrap(Val);
1175 Value::use_iterator I = V->use_begin();
1176 if (I == V->use_end())
1177 return nullptr;
1178 return wrap(&*I);
1179}
1180
1182 Use *Next = unwrap(U)->getNext();
1183 if (Next)
1184 return wrap(Next);
1185 return nullptr;
1186}
1187
1189 return wrap(unwrap(U)->getUser());
1190}
1191
1193 return wrap(unwrap(U)->get());
1194}
1195
1196/*--.. Operations on Users .................................................--*/
1197
1199 unsigned Index) {
1200 Metadata *Op = N->getOperand(Index);
1201 if (!Op)
1202 return nullptr;
1203 if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
1204 return wrap(C->getValue());
1205 return wrap(MetadataAsValue::get(Context, Op));
1206}
1207
1209 Value *V = unwrap(Val);
1210 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
1211 if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1212 assert(Index == 0 && "Function-local metadata can only have one operand");
1213 return wrap(L->getValue());
1214 }
1215 return getMDNodeOperandImpl(V->getContext(),
1216 cast<MDNode>(MD->getMetadata()), Index);
1217 }
1218
1219 return wrap(cast<User>(V)->getOperand(Index));
1220}
1221
1223 Value *V = unwrap(Val);
1224 return wrap(&cast<User>(V)->getOperandUse(Index));
1225}
1226
1227void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
1228 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
1229}
1230
1232 Value *V = unwrap(Val);
1233 if (isa<MetadataAsValue>(V))
1234 return LLVMGetMDNodeNumOperands(Val);
1235
1236 return cast<User>(V)->getNumOperands();
1237}
1238
1239/*--.. Operations on constants of any type .................................--*/
1240
1242 return wrap(Constant::getNullValue(unwrap(Ty)));
1243}
1244
1247}
1248
1250 return wrap(UndefValue::get(unwrap(Ty)));
1251}
1252
1254 return wrap(PoisonValue::get(unwrap(Ty)));
1255}
1256
1258 return isa<Constant>(unwrap(Ty));
1259}
1260
1262 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
1263 return C->isNullValue();
1264 return false;
1265}
1266
1268 return isa<UndefValue>(unwrap(Val));
1269}
1270
1272 return isa<PoisonValue>(unwrap(Val));
1273}
1274
1276 return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
1277}
1278
1279/*--.. Operations on metadata nodes ........................................--*/
1280
1282 size_t SLen) {
1283 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
1284}
1285
1287 size_t Count) {
1288 return wrap(MDNode::get(*unwrap(C), ArrayRef<Metadata*>(unwrap(MDs), Count)));
1289}
1290
1292 unsigned SLen) {
1293 LLVMContext &Context = *unwrap(C);
1295 Context, MDString::get(Context, StringRef(Str, SLen))));
1296}
1297
1298LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
1299 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
1300}
1301
1303 unsigned Count) {
1304 LLVMContext &Context = *unwrap(C);
1306 for (auto *OV : ArrayRef(Vals, Count)) {
1307 Value *V = unwrap(OV);
1308 Metadata *MD;
1309 if (!V)
1310 MD = nullptr;
1311 else if (auto *C = dyn_cast<Constant>(V))
1313 else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
1314 MD = MDV->getMetadata();
1315 assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
1316 "outside of direct argument to call");
1317 } else {
1318 // This is function-local metadata. Pretend to make an MDNode.
1319 assert(Count == 1 &&
1320 "Expected only one operand to function-local metadata");
1321 return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V)));
1322 }
1323
1324 MDs.push_back(MD);
1325 }
1326 return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs)));
1327}
1328
1329LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
1330 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
1331}
1332
1334 return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
1335}
1336
1338 auto *V = unwrap(Val);
1339 if (auto *C = dyn_cast<Constant>(V))
1341 if (auto *MAV = dyn_cast<MetadataAsValue>(V))
1342 return wrap(MAV->getMetadata());
1343 return wrap(ValueAsMetadata::get(V));
1344}
1345
1346const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
1347 if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
1348 if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
1349 *Length = S->getString().size();
1350 return S->getString().data();
1351 }
1352 *Length = 0;
1353 return nullptr;
1354}
1355
1357 auto *MD = unwrap<MetadataAsValue>(V);
1358 if (isa<ValueAsMetadata>(MD->getMetadata()))
1359 return 1;
1360 return cast<MDNode>(MD->getMetadata())->getNumOperands();
1361}
1362
1364 Module *Mod = unwrap(M);
1365 Module::named_metadata_iterator I = Mod->named_metadata_begin();
1366 if (I == Mod->named_metadata_end())
1367 return nullptr;
1368 return wrap(&*I);
1369}
1370
1372 Module *Mod = unwrap(M);
1373 Module::named_metadata_iterator I = Mod->named_metadata_end();
1374 if (I == Mod->named_metadata_begin())
1375 return nullptr;
1376 return wrap(&*--I);
1377}
1378
1380 NamedMDNode *NamedNode = unwrap(NMD);
1382 if (++I == NamedNode->getParent()->named_metadata_end())
1383 return nullptr;
1384 return wrap(&*I);
1385}
1386
1388 NamedMDNode *NamedNode = unwrap(NMD);
1390 if (I == NamedNode->getParent()->named_metadata_begin())
1391 return nullptr;
1392 return wrap(&*--I);
1393}
1394
1396 const char *Name, size_t NameLen) {
1397 return wrap(unwrap(M)->getNamedMetadata(StringRef(Name, NameLen)));
1398}
1399
1401 const char *Name, size_t NameLen) {
1402 return wrap(unwrap(M)->getOrInsertNamedMetadata({Name, NameLen}));
1403}
1404
1405const char *LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NMD, size_t *NameLen) {
1406 NamedMDNode *NamedNode = unwrap(NMD);
1407 *NameLen = NamedNode->getName().size();
1408 return NamedNode->getName().data();
1409}
1410
1412 auto *MD = unwrap<MetadataAsValue>(V);
1413 if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1414 *Dest = wrap(MDV->getValue());
1415 return;
1416 }
1417 const auto *N = cast<MDNode>(MD->getMetadata());
1418 const unsigned numOperands = N->getNumOperands();
1419 LLVMContext &Context = unwrap(V)->getContext();
1420 for (unsigned i = 0; i < numOperands; i++)
1421 Dest[i] = getMDNodeOperandImpl(Context, N, i);
1422}
1423
1425 LLVMMetadataRef Replacement) {
1426 auto *MD = cast<MetadataAsValue>(unwrap(V));
1427 auto *N = cast<MDNode>(MD->getMetadata());
1428 N->replaceOperandWith(Index, unwrap<Metadata>(Replacement));
1429}
1430
1432 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
1433 return N->getNumOperands();
1434 }
1435 return 0;
1436}
1437
1439 LLVMValueRef *Dest) {
1440 NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
1441 if (!N)
1442 return;
1443 LLVMContext &Context = unwrap(M)->getContext();
1444 for (unsigned i=0;i<N->getNumOperands();i++)
1445 Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
1446}
1447
1449 LLVMValueRef Val) {
1450 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
1451 if (!N)
1452 return;
1453 if (!Val)
1454 return;
1455 N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
1456}
1457
1458const char *LLVMGetDebugLocDirectory(LLVMValueRef Val, unsigned *Length) {
1459 if (!Length) return nullptr;
1460 StringRef S;
1461 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1462 if (const auto &DL = I->getDebugLoc()) {
1463 S = DL->getDirectory();
1464 }
1465 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1467 GV->getDebugInfo(GVEs);
1468 if (GVEs.size())
1469 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1470 S = DGV->getDirectory();
1471 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1472 if (const DISubprogram *DSP = F->getSubprogram())
1473 S = DSP->getDirectory();
1474 } else {
1475 assert(0 && "Expected Instruction, GlobalVariable or Function");
1476 return nullptr;
1477 }
1478 *Length = S.size();
1479 return S.data();
1480}
1481
1482const char *LLVMGetDebugLocFilename(LLVMValueRef Val, unsigned *Length) {
1483 if (!Length) return nullptr;
1484 StringRef S;
1485 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1486 if (const auto &DL = I->getDebugLoc()) {
1487 S = DL->getFilename();
1488 }
1489 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1491 GV->getDebugInfo(GVEs);
1492 if (GVEs.size())
1493 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1494 S = DGV->getFilename();
1495 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1496 if (const DISubprogram *DSP = F->getSubprogram())
1497 S = DSP->getFilename();
1498 } else {
1499 assert(0 && "Expected Instruction, GlobalVariable or Function");
1500 return nullptr;
1501 }
1502 *Length = S.size();
1503 return S.data();
1504}
1505
1507 unsigned L = 0;
1508 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1509 if (const auto &DL = I->getDebugLoc()) {
1510 L = DL->getLine();
1511 }
1512 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1514 GV->getDebugInfo(GVEs);
1515 if (GVEs.size())
1516 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1517 L = DGV->getLine();
1518 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1519 if (const DISubprogram *DSP = F->getSubprogram())
1520 L = DSP->getLine();
1521 } else {
1522 assert(0 && "Expected Instruction, GlobalVariable or Function");
1523 return -1;
1524 }
1525 return L;
1526}
1527
1529 unsigned C = 0;
1530 if (const auto *I = dyn_cast<Instruction>(unwrap(Val)))
1531 if (const auto &DL = I->getDebugLoc())
1532 C = DL->getColumn();
1533 return C;
1534}
1535
1536/*--.. Operations on scalar constants ......................................--*/
1537
1538LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
1539 LLVMBool SignExtend) {
1540 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
1541}
1542
1544 unsigned NumWords,
1545 const uint64_t Words[]) {
1546 IntegerType *Ty = unwrap<IntegerType>(IntTy);
1547 return wrap(ConstantInt::get(
1548 Ty->getContext(), APInt(Ty->getBitWidth(), ArrayRef(Words, NumWords))));
1549}
1550
1552 uint8_t Radix) {
1553 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
1554 Radix));
1555}
1556
1558 unsigned SLen, uint8_t Radix) {
1559 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
1560 Radix));
1561}
1562
1564 return wrap(ConstantFP::get(unwrap(RealTy), N));
1565}
1566
1568 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
1569}
1570
1572 unsigned SLen) {
1573 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
1574}
1575
1576unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
1577 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
1578}
1579
1581 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
1582}
1583
1584double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
1585 ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
1586 Type *Ty = cFP->getType();
1587
1588 if (Ty->isHalfTy() || Ty->isBFloatTy() || Ty->isFloatTy() ||
1589 Ty->isDoubleTy()) {
1590 *LosesInfo = false;
1591 return cFP->getValueAPF().convertToDouble();
1592 }
1593
1594 bool APFLosesInfo;
1595 APFloat APF = cFP->getValueAPF();
1596 APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo);
1597 *LosesInfo = APFLosesInfo;
1598 return APF.convertToDouble();
1599}
1600
1601/*--.. Operations on composite constants ...................................--*/
1602
1604 unsigned Length,
1605 LLVMBool DontNullTerminate) {
1606 /* Inverted the sense of AddNull because ', 0)' is a
1607 better mnemonic for null termination than ', 1)'. */
1609 DontNullTerminate == 0));
1610}
1611
1613 size_t Length,
1614 LLVMBool DontNullTerminate) {
1615 /* Inverted the sense of AddNull because ', 0)' is a
1616 better mnemonic for null termination than ', 1)'. */
1618 DontNullTerminate == 0));
1619}
1620
1621LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
1622 LLVMBool DontNullTerminate) {
1624 DontNullTerminate);
1625}
1626
1628 return wrap(unwrap<Constant>(C)->getAggregateElement(Idx));
1629}
1630
1632 return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
1633}
1634
1636 return unwrap<ConstantDataSequential>(C)->isString();
1637}
1638
1639const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1640 StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1641 *Length = Str.size();
1642 return Str.data();
1643}
1644
1646 LLVMValueRef *ConstantVals, unsigned Length) {
1647 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1648 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1649}
1650
1652 uint64_t Length) {
1653 ArrayRef<Constant *> V(unwrap<Constant>(ConstantVals, Length), Length);
1654 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1655}
1656
1658 LLVMValueRef *ConstantVals,
1659 unsigned Count, LLVMBool Packed) {
1660 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1661 return wrap(ConstantStruct::getAnon(*unwrap(C), ArrayRef(Elements, Count),
1662 Packed != 0));
1663}
1664
1665LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
1666 LLVMBool Packed) {
1667 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1668 Packed);
1669}
1670
1672 LLVMValueRef *ConstantVals,
1673 unsigned Count) {
1674 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1675 StructType *Ty = unwrap<StructType>(StructTy);
1676
1677 return wrap(ConstantStruct::get(Ty, ArrayRef(Elements, Count)));
1678}
1679
1680LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
1682 ArrayRef(unwrap<Constant>(ScalarConstantVals, Size), Size)));
1683}
1684
1686 LLVMValueRef Disc, LLVMValueRef AddrDisc) {
1688 unwrap<Constant>(Ptr), unwrap<ConstantInt>(Key),
1689 unwrap<ConstantInt>(Disc), unwrap<Constant>(AddrDisc)));
1690}
1691
1692/*-- Opcode mapping */
1693
1695{
1696 switch (opcode) {
1697 default: llvm_unreachable("Unhandled Opcode.");
1698#define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
1699#include "llvm/IR/Instruction.def"
1700#undef HANDLE_INST
1701 }
1702}
1703
1705{
1706 switch (code) {
1707#define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
1708#include "llvm/IR/Instruction.def"
1709#undef HANDLE_INST
1710 }
1711 llvm_unreachable("Unhandled Opcode.");
1712}
1713
1714/*-- GEP wrap flag conversions */
1715
1717 GEPNoWrapFlags NewGEPFlags;
1718 if ((GEPFlags & LLVMGEPFlagInBounds) != 0)
1719 NewGEPFlags |= GEPNoWrapFlags::inBounds();
1720 if ((GEPFlags & LLVMGEPFlagNUSW) != 0)
1721 NewGEPFlags |= GEPNoWrapFlags::noUnsignedSignedWrap();
1722 if ((GEPFlags & LLVMGEPFlagNUW) != 0)
1723 NewGEPFlags |= GEPNoWrapFlags::noUnsignedWrap();
1724
1725 return NewGEPFlags;
1726}
1727
1729 LLVMGEPNoWrapFlags NewGEPFlags = 0;
1730 if (GEPFlags.isInBounds())
1731 NewGEPFlags |= LLVMGEPFlagInBounds;
1732 if (GEPFlags.hasNoUnsignedSignedWrap())
1733 NewGEPFlags |= LLVMGEPFlagNUSW;
1734 if (GEPFlags.hasNoUnsignedWrap())
1735 NewGEPFlags |= LLVMGEPFlagNUW;
1736
1737 return NewGEPFlags;
1738}
1739
1740/*--.. Constant expressions ................................................--*/
1741
1743 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
1744}
1745
1748}
1749
1751 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
1752}
1753
1755 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1756}
1757
1759 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
1760}
1761
1763 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1764}
1765
1766
1768 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
1769}
1770
1772 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
1773 unwrap<Constant>(RHSConstant)));
1774}
1775
1777 LLVMValueRef RHSConstant) {
1778 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
1779 unwrap<Constant>(RHSConstant)));
1780}
1781
1783 LLVMValueRef RHSConstant) {
1784 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
1785 unwrap<Constant>(RHSConstant)));
1786}
1787
1789 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
1790 unwrap<Constant>(RHSConstant)));
1791}
1792
1794 LLVMValueRef RHSConstant) {
1795 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
1796 unwrap<Constant>(RHSConstant)));
1797}
1798
1800 LLVMValueRef RHSConstant) {
1801 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
1802 unwrap<Constant>(RHSConstant)));
1803}
1804
1806 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
1807 unwrap<Constant>(RHSConstant)));
1808}
1809
1811 LLVMValueRef RHSConstant) {
1812 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
1813 unwrap<Constant>(RHSConstant)));
1814}
1815
1817 LLVMValueRef RHSConstant) {
1818 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
1819 unwrap<Constant>(RHSConstant)));
1820}
1821
1823 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
1824 unwrap<Constant>(RHSConstant)));
1825}
1826
1828 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1829 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1830 NumIndices);
1831 Constant *Val = unwrap<Constant>(ConstantVal);
1832 return wrap(ConstantExpr::getGetElementPtr(unwrap(Ty), Val, IdxList));
1833}
1834
1836 LLVMValueRef *ConstantIndices,
1837 unsigned NumIndices) {
1838 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1839 NumIndices);
1840 Constant *Val = unwrap<Constant>(ConstantVal);
1841 return wrap(ConstantExpr::getInBoundsGetElementPtr(unwrap(Ty), Val, IdxList));
1842}
1843
1845 LLVMValueRef ConstantVal,
1846 LLVMValueRef *ConstantIndices,
1847 unsigned NumIndices,
1848 LLVMGEPNoWrapFlags NoWrapFlags) {
1849 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1850 NumIndices);
1851 Constant *Val = unwrap<Constant>(ConstantVal);
1853 unwrap(Ty), Val, IdxList, mapFromLLVMGEPNoWrapFlags(NoWrapFlags)));
1854}
1855
1857 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1858 unwrap(ToType)));
1859}
1860
1862 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1863 unwrap(ToType)));
1864}
1865
1867 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1868 unwrap(ToType)));
1869}
1870
1872 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1873 unwrap(ToType)));
1874}
1875
1877 LLVMTypeRef ToType) {
1878 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1879 unwrap(ToType)));
1880}
1881
1883 LLVMTypeRef ToType) {
1884 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1885 unwrap(ToType)));
1886}
1887
1889 LLVMTypeRef ToType) {
1890 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1891 unwrap(ToType)));
1892}
1893
1895 LLVMValueRef IndexConstant) {
1896 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1897 unwrap<Constant>(IndexConstant)));
1898}
1899
1901 LLVMValueRef ElementValueConstant,
1902 LLVMValueRef IndexConstant) {
1903 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1904 unwrap<Constant>(ElementValueConstant),
1905 unwrap<Constant>(IndexConstant)));
1906}
1907
1909 LLVMValueRef VectorBConstant,
1910 LLVMValueRef MaskConstant) {
1911 SmallVector<int, 16> IntMask;
1912 ShuffleVectorInst::getShuffleMask(unwrap<Constant>(MaskConstant), IntMask);
1913 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1914 unwrap<Constant>(VectorBConstant),
1915 IntMask));
1916}
1917
1919 const char *Constraints,
1920 LLVMBool HasSideEffects,
1921 LLVMBool IsAlignStack) {
1922 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1923 Constraints, HasSideEffects, IsAlignStack));
1924}
1925
1927 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1928}
1929
1931 return wrap(unwrap<BlockAddress>(BlockAddr)->getFunction());
1932}
1933
1935 return wrap(unwrap<BlockAddress>(BlockAddr)->getBasicBlock());
1936}
1937
1938/*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1939
1941 return wrap(unwrap<GlobalValue>(Global)->getParent());
1942}
1943
1945 return unwrap<GlobalValue>(Global)->isDeclaration();
1946}
1947
1949 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1951 return LLVMExternalLinkage;
1959 return LLVMWeakAnyLinkage;
1961 return LLVMWeakODRLinkage;
1963 return LLVMAppendingLinkage;
1965 return LLVMInternalLinkage;
1967 return LLVMPrivateLinkage;
1971 return LLVMCommonLinkage;
1972 }
1973
1974 llvm_unreachable("Invalid GlobalValue linkage!");
1975}
1976
1978 GlobalValue *GV = unwrap<GlobalValue>(Global);
1979
1980 switch (Linkage) {
1983 break;
1986 break;
1989 break;
1992 break;
1994 LLVM_DEBUG(
1995 errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1996 "longer supported.");
1997 break;
1998 case LLVMWeakAnyLinkage:
2000 break;
2001 case LLVMWeakODRLinkage:
2003 break;
2006 break;
2009 break;
2010 case LLVMPrivateLinkage:
2012 break;
2015 break;
2018 break;
2020 LLVM_DEBUG(
2021 errs()
2022 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
2023 break;
2025 LLVM_DEBUG(
2026 errs()
2027 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
2028 break;
2031 break;
2032 case LLVMGhostLinkage:
2033 LLVM_DEBUG(
2034 errs() << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
2035 break;
2036 case LLVMCommonLinkage:
2038 break;
2039 }
2040}
2041
2043 // Using .data() is safe because of how GlobalObject::setSection is
2044 // implemented.
2045 return unwrap<GlobalValue>(Global)->getSection().data();
2046}
2047
2048void LLVMSetSection(LLVMValueRef Global, const char *Section) {
2049 unwrap<GlobalObject>(Global)->setSection(Section);
2050}
2051
2053 return static_cast<LLVMVisibility>(
2054 unwrap<GlobalValue>(Global)->getVisibility());
2055}
2056
2058 unwrap<GlobalValue>(Global)
2059 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
2060}
2061
2063 return static_cast<LLVMDLLStorageClass>(
2064 unwrap<GlobalValue>(Global)->getDLLStorageClass());
2065}
2066
2068 unwrap<GlobalValue>(Global)->setDLLStorageClass(
2069 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
2070}
2071
2073 switch (unwrap<GlobalValue>(Global)->getUnnamedAddr()) {
2074 case GlobalVariable::UnnamedAddr::None:
2075 return LLVMNoUnnamedAddr;
2076 case GlobalVariable::UnnamedAddr::Local:
2077 return LLVMLocalUnnamedAddr;
2078 case GlobalVariable::UnnamedAddr::Global:
2079 return LLVMGlobalUnnamedAddr;
2080 }
2081 llvm_unreachable("Unknown UnnamedAddr kind!");
2082}
2083
2085 GlobalValue *GV = unwrap<GlobalValue>(Global);
2086
2087 switch (UnnamedAddr) {
2088 case LLVMNoUnnamedAddr:
2089 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::None);
2091 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Local);
2093 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Global);
2094 }
2095}
2096
2098 return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
2099}
2100
2102 unwrap<GlobalValue>(Global)->setUnnamedAddr(
2103 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
2104 : GlobalValue::UnnamedAddr::None);
2105}
2106
2108 return wrap(unwrap<GlobalValue>(Global)->getValueType());
2109}
2110
2111/*--.. Operations on global variables, load and store instructions .........--*/
2112
2114 Value *P = unwrap(V);
2115 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2116 return GV->getAlign() ? GV->getAlign()->value() : 0;
2117 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
2118 return AI->getAlign().value();
2119 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2120 return LI->getAlign().value();
2121 if (StoreInst *SI = dyn_cast<StoreInst>(P))
2122 return SI->getAlign().value();
2123 if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2124 return RMWI->getAlign().value();
2125 if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2126 return CXI->getAlign().value();
2127
2129 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, "
2130 "and AtomicCmpXchgInst have alignment");
2131}
2132
2133void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
2134 Value *P = unwrap(V);
2135 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2136 GV->setAlignment(MaybeAlign(Bytes));
2137 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
2138 AI->setAlignment(Align(Bytes));
2139 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
2140 LI->setAlignment(Align(Bytes));
2141 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
2142 SI->setAlignment(Align(Bytes));
2143 else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2144 RMWI->setAlignment(Align(Bytes));
2145 else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2146 CXI->setAlignment(Align(Bytes));
2147 else
2149 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, and "
2150 "and AtomicCmpXchgInst have alignment");
2151}
2152
2154 size_t *NumEntries) {
2155 return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
2156 Entries.clear();
2157 if (Instruction *Instr = dyn_cast<Instruction>(unwrap(Value))) {
2158 Instr->getAllMetadata(Entries);
2159 } else {
2160 unwrap<GlobalObject>(Value)->getAllMetadata(Entries);
2161 }
2162 });
2163}
2164
2166 unsigned Index) {
2168 static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2169 return MVE.Kind;
2170}
2171
2174 unsigned Index) {
2176 static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2177 return MVE.Metadata;
2178}
2179
2181 free(Entries);
2182}
2183
2185 LLVMMetadataRef MD) {
2186 unwrap<GlobalObject>(Global)->setMetadata(Kind, unwrap<MDNode>(MD));
2187}
2188
2190 unwrap<GlobalObject>(Global)->eraseMetadata(Kind);
2191}
2192
2194 unwrap<GlobalObject>(Global)->clearMetadata();
2195}
2196
2197/*--.. Operations on global variables ......................................--*/
2198
2200 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2202}
2203
2205 const char *Name,
2206 unsigned AddressSpace) {
2207 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2209 nullptr, GlobalVariable::NotThreadLocal,
2210 AddressSpace));
2211}
2212
2214 return wrap(unwrap(M)->getNamedGlobal(Name));
2215}
2216
2218 size_t Length) {
2219 return wrap(unwrap(M)->getNamedGlobal(StringRef(Name, Length)));
2220}
2221
2223 Module *Mod = unwrap(M);
2224 Module::global_iterator I = Mod->global_begin();
2225 if (I == Mod->global_end())
2226 return nullptr;
2227 return wrap(&*I);
2228}
2229
2231 Module *Mod = unwrap(M);
2232 Module::global_iterator I = Mod->global_end();
2233 if (I == Mod->global_begin())
2234 return nullptr;
2235 return wrap(&*--I);
2236}
2237
2239 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2241 if (++I == GV->getParent()->global_end())
2242 return nullptr;
2243 return wrap(&*I);
2244}
2245
2247 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2249 if (I == GV->getParent()->global_begin())
2250 return nullptr;
2251 return wrap(&*--I);
2252}
2253
2255 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
2256}
2257
2259 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
2260 if ( !GV->hasInitializer() )
2261 return nullptr;
2262 return wrap(GV->getInitializer());
2263}
2264
2265void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
2266 unwrap<GlobalVariable>(GlobalVar)->setInitializer(
2267 ConstantVal ? unwrap<Constant>(ConstantVal) : nullptr);
2268}
2269
2271 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
2272}
2273
2274void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
2275 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
2276}
2277
2279 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
2280}
2281
2282void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
2283 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
2284}
2285
2287 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
2288 case GlobalVariable::NotThreadLocal:
2289 return LLVMNotThreadLocal;
2290 case GlobalVariable::GeneralDynamicTLSModel:
2292 case GlobalVariable::LocalDynamicTLSModel:
2294 case GlobalVariable::InitialExecTLSModel:
2296 case GlobalVariable::LocalExecTLSModel:
2297 return LLVMLocalExecTLSModel;
2298 }
2299
2300 llvm_unreachable("Invalid GlobalVariable thread local mode");
2301}
2302
2304 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2305
2306 switch (Mode) {
2307 case LLVMNotThreadLocal:
2308 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
2309 break;
2311 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
2312 break;
2314 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
2315 break;
2317 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
2318 break;
2320 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
2321 break;
2322 }
2323}
2324
2326 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
2327}
2328
2330 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
2331}
2332
2333/*--.. Operations on aliases ......................................--*/
2334
2336 unsigned AddrSpace, LLVMValueRef Aliasee,
2337 const char *Name) {
2338 return wrap(GlobalAlias::create(unwrap(ValueTy), AddrSpace,
2340 unwrap<Constant>(Aliasee), unwrap(M)));
2341}
2342
2344 const char *Name, size_t NameLen) {
2345 return wrap(unwrap(M)->getNamedAlias(StringRef(Name, NameLen)));
2346}
2347
2349 Module *Mod = unwrap(M);
2350 Module::alias_iterator I = Mod->alias_begin();
2351 if (I == Mod->alias_end())
2352 return nullptr;
2353 return wrap(&*I);
2354}
2355
2357 Module *Mod = unwrap(M);
2358 Module::alias_iterator I = Mod->alias_end();
2359 if (I == Mod->alias_begin())
2360 return nullptr;
2361 return wrap(&*--I);
2362}
2363
2365 GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2367 if (++I == Alias->getParent()->alias_end())
2368 return nullptr;
2369 return wrap(&*I);
2370}
2371
2373 GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2375 if (I == Alias->getParent()->alias_begin())
2376 return nullptr;
2377 return wrap(&*--I);
2378}
2379
2381 return wrap(unwrap<GlobalAlias>(Alias)->getAliasee());
2382}
2383
2385 unwrap<GlobalAlias>(Alias)->setAliasee(unwrap<Constant>(Aliasee));
2386}
2387
2388/*--.. Operations on functions .............................................--*/
2389
2391 LLVMTypeRef FunctionTy) {
2392 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
2394}
2395
2397 return wrap(unwrap(M)->getFunction(Name));
2398}
2399
2401 size_t Length) {
2402 return wrap(unwrap(M)->getFunction(StringRef(Name, Length)));
2403}
2404
2406 Module *Mod = unwrap(M);
2407 Module::iterator I = Mod->begin();
2408 if (I == Mod->end())
2409 return nullptr;
2410 return wrap(&*I);
2411}
2412
2414 Module *Mod = unwrap(M);
2415 Module::iterator I = Mod->end();
2416 if (I == Mod->begin())
2417 return nullptr;
2418 return wrap(&*--I);
2419}
2420
2422 Function *Func = unwrap<Function>(Fn);
2423 Module::iterator I(Func);
2424 if (++I == Func->getParent()->end())
2425 return nullptr;
2426 return wrap(&*I);
2427}
2428
2430 Function *Func = unwrap<Function>(Fn);
2431 Module::iterator I(Func);
2432 if (I == Func->getParent()->begin())
2433 return nullptr;
2434 return wrap(&*--I);
2435}
2436
2438 unwrap<Function>(Fn)->eraseFromParent();
2439}
2440
2442 return unwrap<Function>(Fn)->hasPersonalityFn();
2443}
2444
2446 return wrap(unwrap<Function>(Fn)->getPersonalityFn());
2447}
2448
2450 unwrap<Function>(Fn)->setPersonalityFn(
2451 PersonalityFn ? unwrap<Constant>(PersonalityFn) : nullptr);
2452}
2453
2455 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
2456 return F->getIntrinsicID();
2457 return 0;
2458}
2459
2461 assert(ID < llvm::Intrinsic::num_intrinsics && "Intrinsic ID out of range");
2462 return llvm::Intrinsic::ID(ID);
2463}
2464
2466 unsigned ID,
2467 LLVMTypeRef *ParamTypes,
2468 size_t ParamCount) {
2469 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2470 auto IID = llvm_map_to_intrinsic_id(ID);
2472}
2473
2474const char *LLVMIntrinsicGetName(unsigned ID, size_t *NameLength) {
2475 auto IID = llvm_map_to_intrinsic_id(ID);
2476 auto Str = llvm::Intrinsic::getName(IID);
2477 *NameLength = Str.size();
2478 return Str.data();
2479}
2480
2482 LLVMTypeRef *ParamTypes, size_t ParamCount) {
2483 auto IID = llvm_map_to_intrinsic_id(ID);
2484 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2485 return wrap(llvm::Intrinsic::getType(*unwrap(Ctx), IID, Tys));
2486}
2487
2489 size_t ParamCount, size_t *NameLength) {
2490 auto IID = llvm_map_to_intrinsic_id(ID);
2491 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2492 auto Str = llvm::Intrinsic::getNameNoUnnamedTypes(IID, Tys);
2493 *NameLength = Str.length();
2494 return strdup(Str.c_str());
2495}
2496
2498 LLVMTypeRef *ParamTypes,
2499 size_t ParamCount, size_t *NameLength) {
2500 auto IID = llvm_map_to_intrinsic_id(ID);
2501 ArrayRef<Type *> Tys(unwrap(ParamTypes), ParamCount);
2502 auto Str = llvm::Intrinsic::getName(IID, Tys, unwrap(Mod));
2503 *NameLength = Str.length();
2504 return strdup(Str.c_str());
2505}
2506
2507unsigned LLVMLookupIntrinsicID(const char *Name, size_t NameLen) {
2508 return Intrinsic::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
2744LLVMOperandBundleRef LLVMCreateOperandBundle(const char *Tag, size_t TagLen,
2745 LLVMValueRef *Args,
2746 unsigned NumArgs) {
2747 return wrap(new OperandBundleDef(std::string(Tag, TagLen),
2748 ArrayRef(unwrap(Args), NumArgs)));
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 Instruction *Instr = unwrap<Instruction>(Inst);
2979 auto I = Instr->DebugMarker->StoredDbgRecords.begin();
2980 if (I == Instr->DebugMarker->StoredDbgRecords.end())
2981 return nullptr;
2982 return wrap(&*I);
2983}
2984
2986 Instruction *Instr = unwrap<Instruction>(Inst);
2987 auto I = Instr->DebugMarker->StoredDbgRecords.rbegin();
2988 if (I == Instr->DebugMarker->StoredDbgRecords.rend())
2989 return nullptr;
2990 return wrap(&*I);
2991}
2992
2994 DbgRecord *Record = unwrap<DbgRecord>(Rec);
2996 if (++I == Record->getInstruction()->DebugMarker->StoredDbgRecords.end())
2997 return nullptr;
2998 return wrap(&*I);
2999}
3000
3002 DbgRecord *Record = unwrap<DbgRecord>(Rec);
3004 if (I == Record->getInstruction()->DebugMarker->StoredDbgRecords.begin())
3005 return nullptr;
3006 return wrap(&*--I);
3007}
3008
3010 if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) {
3011 return FPI->arg_size();
3012 }
3013 return unwrap<CallBase>(Instr)->arg_size();
3014}
3015
3016/*--.. Call and invoke instructions ........................................--*/
3017
3019 return unwrap<CallBase>(Instr)->getCallingConv();
3020}
3021
3023 return unwrap<CallBase>(Instr)->setCallingConv(
3024 static_cast<CallingConv::ID>(CC));
3025}
3026
3028 unsigned align) {
3029 auto *Call = unwrap<CallBase>(Instr);
3030 Attribute AlignAttr =
3031 Attribute::getWithAlignment(Call->getContext(), Align(align));
3032 Call->addAttributeAtIndex(Idx, AlignAttr);
3033}
3034
3037 unwrap<CallBase>(C)->addAttributeAtIndex(Idx, unwrap(A));
3038}
3039
3042 auto *Call = unwrap<CallBase>(C);
3043 auto AS = Call->getAttributes().getAttributes(Idx);
3044 return AS.getNumAttributes();
3045}
3046
3048 LLVMAttributeRef *Attrs) {
3049 auto *Call = unwrap<CallBase>(C);
3050 auto AS = Call->getAttributes().getAttributes(Idx);
3051 for (auto A : AS)
3052 *Attrs++ = wrap(A);
3053}
3054
3057 unsigned KindID) {
3058 return wrap(unwrap<CallBase>(C)->getAttributeAtIndex(
3059 Idx, (Attribute::AttrKind)KindID));
3060}
3061
3064 const char *K, unsigned KLen) {
3065 return wrap(
3066 unwrap<CallBase>(C)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
3067}
3068
3070 unsigned KindID) {
3071 unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
3072}
3073
3075 const char *K, unsigned KLen) {
3076 unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
3077}
3078
3080 return wrap(unwrap<CallBase>(Instr)->getCalledOperand());
3081}
3082
3084 return wrap(unwrap<CallBase>(Instr)->getFunctionType());
3085}
3086
3088 return unwrap<CallBase>(C)->getNumOperandBundles();
3089}
3090
3092 unsigned Index) {
3093 return wrap(
3094 new OperandBundleDef(unwrap<CallBase>(C)->getOperandBundleAt(Index)));
3095}
3096
3097/*--.. Operations on call instructions (only) ..............................--*/
3098
3100 return unwrap<CallInst>(Call)->isTailCall();
3101}
3102
3103void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
3104 unwrap<CallInst>(Call)->setTailCall(isTailCall);
3105}
3106
3108 return (LLVMTailCallKind)unwrap<CallInst>(Call)->getTailCallKind();
3109}
3110
3112 unwrap<CallInst>(Call)->setTailCallKind((CallInst::TailCallKind)kind);
3113}
3114
3115/*--.. Operations on invoke instructions (only) ............................--*/
3116
3118 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
3119}
3120
3122 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
3123 return wrap(CRI->getUnwindDest());
3124 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
3125 return wrap(CSI->getUnwindDest());
3126 }
3127 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
3128}
3129
3131 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
3132}
3133
3135 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
3136 return CRI->setUnwindDest(unwrap(B));
3137 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
3138 return CSI->setUnwindDest(unwrap(B));
3139 }
3140 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
3141}
3142
3144 return wrap(unwrap<CallBrInst>(CallBr)->getDefaultDest());
3145}
3146
3148 return unwrap<CallBrInst>(CallBr)->getNumIndirectDests();
3149}
3150
3152 return wrap(unwrap<CallBrInst>(CallBr)->getIndirectDest(Idx));
3153}
3154
3155/*--.. Operations on terminators ...........................................--*/
3156
3158 return unwrap<Instruction>(Term)->getNumSuccessors();
3159}
3160
3162 return wrap(unwrap<Instruction>(Term)->getSuccessor(i));
3163}
3164
3166 return unwrap<Instruction>(Term)->setSuccessor(i, unwrap(block));
3167}
3168
3169/*--.. Operations on branch instructions (only) ............................--*/
3170
3172 return unwrap<BranchInst>(Branch)->isConditional();
3173}
3174
3176 return wrap(unwrap<BranchInst>(Branch)->getCondition());
3177}
3178
3180 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
3181}
3182
3183/*--.. Operations on switch instructions (only) ............................--*/
3184
3186 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
3187}
3188
3189/*--.. Operations on alloca instructions (only) ............................--*/
3190
3192 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
3193}
3194
3195/*--.. Operations on gep instructions (only) ...............................--*/
3196
3198 return unwrap<GEPOperator>(GEP)->isInBounds();
3199}
3200
3202 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
3203}
3204
3206 return wrap(unwrap<GEPOperator>(GEP)->getSourceElementType());
3207}
3208
3210 GEPOperator *GEPOp = unwrap<GEPOperator>(GEP);
3211 return mapToLLVMGEPNoWrapFlags(GEPOp->getNoWrapFlags());
3212}
3213
3215 GetElementPtrInst *GEPInst = unwrap<GetElementPtrInst>(GEP);
3216 GEPInst->setNoWrapFlags(mapFromLLVMGEPNoWrapFlags(NoWrapFlags));
3217}
3218
3219/*--.. Operations on phi nodes .............................................--*/
3220
3221void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
3222 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
3223 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
3224 for (unsigned I = 0; I != Count; ++I)
3225 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
3226}
3227
3229 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
3230}
3231
3233 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
3234}
3235
3237 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
3238}
3239
3240/*--.. Operations on extractvalue and insertvalue nodes ....................--*/
3241
3243 auto *I = unwrap(Inst);
3244 if (auto *GEP = dyn_cast<GEPOperator>(I))
3245 return GEP->getNumIndices();
3246 if (auto *EV = dyn_cast<ExtractValueInst>(I))
3247 return EV->getNumIndices();
3248 if (auto *IV = dyn_cast<InsertValueInst>(I))
3249 return IV->getNumIndices();
3251 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
3252}
3253
3254const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
3255 auto *I = unwrap(Inst);
3256 if (auto *EV = dyn_cast<ExtractValueInst>(I))
3257 return EV->getIndices().data();
3258 if (auto *IV = dyn_cast<InsertValueInst>(I))
3259 return IV->getIndices().data();
3261 "LLVMGetIndices applies only to extractvalue and insertvalue!");
3262}
3263
3264
3265/*===-- Instruction builders ----------------------------------------------===*/
3266
3268 return wrap(new IRBuilder<>(*unwrap(C)));
3269}
3270
3273}
3274
3276 Instruction *Instr, bool BeforeDbgRecords) {
3277 BasicBlock::iterator I = Instr ? Instr->getIterator() : Block->end();
3278 I.setHeadBit(BeforeDbgRecords);
3279 Builder->SetInsertPoint(Block, I);
3280}
3281
3283 LLVMValueRef Instr) {
3284 return LLVMPositionBuilderImpl(unwrap(Builder), unwrap(Block),
3285 unwrap<Instruction>(Instr), false);
3286}
3287
3290 LLVMValueRef Instr) {
3291 return LLVMPositionBuilderImpl(unwrap(Builder), unwrap(Block),
3292 unwrap<Instruction>(Instr), true);
3293}
3294
3296 Instruction *I = unwrap<Instruction>(Instr);
3297 return LLVMPositionBuilderImpl(unwrap(Builder), I->getParent(), I, false);
3298}
3299
3301 LLVMValueRef Instr) {
3302 Instruction *I = unwrap<Instruction>(Instr);
3303 return LLVMPositionBuilderImpl(unwrap(Builder), I->getParent(), I, true);
3304}
3305
3307 BasicBlock *BB = unwrap(Block);
3308 unwrap(Builder)->SetInsertPoint(BB);
3309}
3310
3312 return wrap(unwrap(Builder)->GetInsertBlock());
3313}
3314
3316 unwrap(Builder)->ClearInsertionPoint();
3317}
3318
3320 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
3321}
3322
3324 const char *Name) {
3325 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
3326}
3327
3329 delete unwrap(Builder);
3330}
3331
3332/*--.. Metadata builders ...................................................--*/
3333
3335 return wrap(unwrap(Builder)->getCurrentDebugLocation().getAsMDNode());
3336}
3337
3339 if (Loc)
3340 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(unwrap<MDNode>(Loc)));
3341 else
3342 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc());
3343}
3344
3346 MDNode *Loc =
3347 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
3348 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
3349}
3350
3352 LLVMContext &Context = unwrap(Builder)->getContext();
3354 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
3355}
3356
3358 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
3359}
3360
3362 unwrap(Builder)->AddMetadataToInst(unwrap<Instruction>(Inst));
3363}
3364
3366 LLVMMetadataRef FPMathTag) {
3367
3368 unwrap(Builder)->setDefaultFPMathTag(FPMathTag
3369 ? unwrap<MDNode>(FPMathTag)
3370 : nullptr);
3371}
3372
3374 return wrap(&unwrap(Builder)->getContext());
3375}
3376
3378 return wrap(unwrap(Builder)->getDefaultFPMathTag());
3379}
3380
3381/*--.. Instruction builders ................................................--*/
3382
3384 return wrap(unwrap(B)->CreateRetVoid());
3385}
3386
3388 return wrap(unwrap(B)->CreateRet(unwrap(V)));
3389}
3390
3392 unsigned N) {
3393 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
3394}
3395
3397 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
3398}
3399
3402 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
3403}
3404
3406 LLVMBasicBlockRef Else, unsigned NumCases) {
3407 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
3408}
3409
3411 unsigned NumDests) {
3412 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
3413}
3414
3416 LLVMBasicBlockRef DefaultDest,
3417 LLVMBasicBlockRef *IndirectDests,
3418 unsigned NumIndirectDests, LLVMValueRef *Args,
3419 unsigned NumArgs, LLVMOperandBundleRef *Bundles,
3420 unsigned NumBundles, const char *Name) {
3421
3423 for (auto *Bundle : ArrayRef(Bundles, NumBundles)) {
3424 OperandBundleDef *OB = unwrap(Bundle);
3425 OBs.push_back(*OB);
3426 }
3427
3428 return wrap(unwrap(B)->CreateCallBr(
3429 unwrap<FunctionType>(Ty), unwrap(Fn), unwrap(DefaultDest),
3430 ArrayRef(unwrap(IndirectDests), NumIndirectDests),
3431 ArrayRef<Value *>(unwrap(Args), NumArgs), OBs, Name));
3432}
3433
3435 LLVMValueRef *Args, unsigned NumArgs,
3437 const char *Name) {
3438 return wrap(unwrap(B)->CreateInvoke(unwrap<FunctionType>(Ty), unwrap(Fn),
3439 unwrap(Then), unwrap(Catch),
3440 ArrayRef(unwrap(Args), NumArgs), Name));
3441}
3442
3445 unsigned NumArgs, LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
3446 LLVMOperandBundleRef *Bundles, unsigned NumBundles, const char *Name) {
3448 for (auto *Bundle : ArrayRef(Bundles, NumBundles)) {
3449 OperandBundleDef *OB = unwrap(Bundle);
3450 OBs.push_back(*OB);
3451 }
3452 return wrap(unwrap(B)->CreateInvoke(
3453 unwrap<FunctionType>(Ty), unwrap(Fn), unwrap(Then), unwrap(Catch),
3454 ArrayRef(unwrap(Args), NumArgs), OBs, Name));
3455}
3456
3458 LLVMValueRef PersFn, unsigned NumClauses,
3459 const char *Name) {
3460 // The personality used to live on the landingpad instruction, but now it
3461 // lives on the parent function. For compatibility, take the provided
3462 // personality and put it on the parent function.
3463 if (PersFn)
3464 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
3465 unwrap<Function>(PersFn));
3466 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
3467}
3468
3470 LLVMValueRef *Args, unsigned NumArgs,
3471 const char *Name) {
3472 return wrap(unwrap(B)->CreateCatchPad(unwrap(ParentPad),
3473 ArrayRef(unwrap(Args), NumArgs), Name));
3474}
3475
3477 LLVMValueRef *Args, unsigned NumArgs,
3478 const char *Name) {
3479 if (ParentPad == nullptr) {
3480 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3481 ParentPad = wrap(Constant::getNullValue(Ty));
3482 }
3483 return wrap(unwrap(B)->CreateCleanupPad(
3484 unwrap(ParentPad), ArrayRef(unwrap(Args), NumArgs), Name));
3485}
3486
3488 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
3489}
3490
3492 LLVMBasicBlockRef UnwindBB,
3493 unsigned NumHandlers, const char *Name) {
3494 if (ParentPad == nullptr) {
3495 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3496 ParentPad = wrap(Constant::getNullValue(Ty));
3497 }
3498 return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(UnwindBB),
3499 NumHandlers, Name));
3500}
3501
3503 LLVMBasicBlockRef BB) {
3504 return wrap(unwrap(B)->CreateCatchRet(unwrap<CatchPadInst>(CatchPad),
3505 unwrap(BB)));
3506}
3507
3509 LLVMBasicBlockRef BB) {
3510 return wrap(unwrap(B)->CreateCleanupRet(unwrap<CleanupPadInst>(CatchPad),
3511 unwrap(BB)));
3512}
3513
3515 return wrap(unwrap(B)->CreateUnreachable());
3516}
3517
3519 LLVMBasicBlockRef Dest) {
3520 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
3521}
3522
3524 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
3525}
3526
3527unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
3528 return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
3529}
3530
3532 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
3533}
3534
3536 unwrap<LandingPadInst>(LandingPad)->addClause(unwrap<Constant>(ClauseVal));
3537}
3538
3540 return unwrap<LandingPadInst>(LandingPad)->isCleanup();
3541}
3542
3543void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
3544 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
3545}
3546
3548 unwrap<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Dest));
3549}
3550
3551unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) {
3552 return unwrap<CatchSwitchInst>(CatchSwitch)->getNumHandlers();
3553}
3554
3555void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) {
3556 CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(CatchSwitch);
3557 for (const BasicBlock *H : CSI->handlers())
3558 *Handlers++ = wrap(H);
3559}
3560
3562 return wrap(unwrap<CatchPadInst>(CatchPad)->getCatchSwitch());
3563}
3564
3566 unwrap<CatchPadInst>(CatchPad)
3567 ->setCatchSwitch(unwrap<CatchSwitchInst>(CatchSwitch));
3568}
3569
3570/*--.. Funclets ...........................................................--*/
3571
3573 return wrap(unwrap<FuncletPadInst>(Funclet)->getArgOperand(i));
3574}
3575
3577 unwrap<FuncletPadInst>(Funclet)->setArgOperand(i, unwrap(value));
3578}
3579
3580/*--.. Arithmetic ..........................................................--*/
3581
3583 FastMathFlags NewFMF;
3584 NewFMF.setAllowReassoc((FMF & LLVMFastMathAllowReassoc) != 0);
3585 NewFMF.setNoNaNs((FMF & LLVMFastMathNoNaNs) != 0);
3586 NewFMF.setNoInfs((FMF & LLVMFastMathNoInfs) != 0);
3587 NewFMF.setNoSignedZeros((FMF & LLVMFastMathNoSignedZeros) != 0);
3589 NewFMF.setAllowContract((FMF & LLVMFastMathAllowContract) != 0);
3590 NewFMF.setApproxFunc((FMF & LLVMFastMathApproxFunc) != 0);
3591
3592 return NewFMF;
3593}
3594
3597 if (FMF.allowReassoc())
3598 NewFMF |= LLVMFastMathAllowReassoc;
3599 if (FMF.noNaNs())
3600 NewFMF |= LLVMFastMathNoNaNs;
3601 if (FMF.noInfs())
3602 NewFMF |= LLVMFastMathNoInfs;
3603 if (FMF.noSignedZeros())
3604 NewFMF |= LLVMFastMathNoSignedZeros;
3605 if (FMF.allowReciprocal())
3607 if (FMF.allowContract())
3608 NewFMF |= LLVMFastMathAllowContract;
3609 if (FMF.approxFunc())
3610 NewFMF |= LLVMFastMathApproxFunc;
3611
3612 return NewFMF;
3613}
3614
3616 const char *Name) {
3617 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
3618}
3619
3621 const char *Name) {
3622 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
3623}
3624
3626 const char *Name) {
3627 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
3628}
3629
3631 const char *Name) {
3632 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
3633}
3634
3636 const char *Name) {
3637 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
3638}
3639
3641 const char *Name) {
3642 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
3643}
3644
3646 const char *Name) {
3647 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
3648}
3649
3651 const char *Name) {
3652 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
3653}
3654
3656 const char *Name) {
3657 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
3658}
3659
3661 const char *Name) {
3662 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
3663}
3664
3666 const char *Name) {
3667 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
3668}
3669
3671 const char *Name) {
3672 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
3673}
3674
3676 const char *Name) {
3677 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
3678}
3679
3681 LLVMValueRef RHS, const char *Name) {
3682 return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
3683}
3684
3686 const char *Name) {
3687 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
3688}
3689
3691 LLVMValueRef RHS, const char *Name) {
3692 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
3693}
3694
3696 const char *Name) {
3697 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
3698}
3699
3701 const char *Name) {
3702 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
3703}
3704
3706 const char *Name) {
3707 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
3708}
3709
3711 const char *Name) {
3712 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
3713}
3714
3716 const char *Name) {
3717 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
3718}
3719
3721 const char *Name) {
3722 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
3723}
3724
3726 const char *Name) {
3727 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
3728}
3729
3731 const char *Name) {
3732 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
3733}
3734
3736 const char *Name) {
3737 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
3738}
3739
3741 const char *Name) {
3742 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
3743}
3744
3746 LLVMValueRef LHS, LLVMValueRef RHS,
3747 const char *Name) {
3749 unwrap(RHS), Name));
3750}
3751
3753 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
3754}
3755
3757 const char *Name) {
3758 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
3759}
3760
3762 const char *Name) {
3763 Value *Neg = unwrap(B)->CreateNeg(unwrap(V), Name);
3764 if (auto *I = dyn_cast<BinaryOperator>(Neg))
3765 I->setHasNoUnsignedWrap();
3766 return wrap(Neg);
3767}
3768
3770 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
3771}
3772
3774 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
3775}
3776
3778 Value *P = unwrap<Value>(ArithInst);
3779 return cast<Instruction>(P)->hasNoUnsignedWrap();
3780}
3781
3782void LLVMSetNUW(LLVMValueRef ArithInst, LLVMBool HasNUW) {
3783 Value *P = unwrap<Value>(ArithInst);
3784 cast<Instruction>(P)->setHasNoUnsignedWrap(HasNUW);
3785}
3786