LLVM 18.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"
17#include "llvm/IR/Constants.h"
22#include "llvm/IR/GlobalAlias.h"
24#include "llvm/IR/IRBuilder.h"
25#include "llvm/IR/InlineAsm.h"
27#include "llvm/IR/LLVMContext.h"
29#include "llvm/IR/Module.h"
31#include "llvm/PassRegistry.h"
32#include "llvm/Support/Debug.h"
39#include <cassert>
40#include <cstdlib>
41#include <cstring>
42#include <system_error>
43
44using namespace llvm;
45
46#define DEBUG_TYPE "ir"
47
54}
55
58}
59
60/*===-- Version query -----------------------------------------------------===*/
61
62void LLVMGetVersion(unsigned *Major, unsigned *Minor, unsigned *Patch) {
63 if (Major)
64 *Major = LLVM_VERSION_MAJOR;
65 if (Minor)
66 *Minor = LLVM_VERSION_MINOR;
67 if (Patch)
68 *Patch = LLVM_VERSION_PATCH;
69}
70
71/*===-- Error handling ----------------------------------------------------===*/
72
73char *LLVMCreateMessage(const char *Message) {
74 return strdup(Message);
75}
76
77void LLVMDisposeMessage(char *Message) {
78 free(Message);
79}
80
81
82/*===-- Operations on contexts --------------------------------------------===*/
83
85 static LLVMContext GlobalContext;
86 return GlobalContext;
87}
88
90 return wrap(new LLVMContext());
91}
92
94
97 void *DiagnosticContext) {
98 unwrap(C)->setDiagnosticHandlerCallBack(
100 Handler),
101 DiagnosticContext);
102}
103
105 return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>(
106 unwrap(C)->getDiagnosticHandlerCallBack());
107}
108
110 return unwrap(C)->getDiagnosticContext();
111}
112
114 void *OpaqueHandle) {
115 auto YieldCallback =
116 LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
117 unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
118}
119
121 return unwrap(C)->shouldDiscardValueNames();
122}
123
125 unwrap(C)->setDiscardValueNames(Discard);
126}
127
129 delete unwrap(C);
130}
131
133 unsigned SLen) {
134 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
135}
136
137unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) {
139}
140
141unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) {
143}
144
146 return Attribute::AttrKind::EndAttrKinds;
147}
148
150 uint64_t Val) {
151 auto &Ctx = *unwrap(C);
152 auto AttrKind = (Attribute::AttrKind)KindID;
153 return wrap(Attribute::get(Ctx, AttrKind, Val));
154}
155
157 return unwrap(A).getKindAsEnum();
158}
159
161 auto Attr = unwrap(A);
162 if (Attr.isEnumAttribute())
163 return 0;
164 return Attr.getValueAsInt();
165}
166
168 LLVMTypeRef type_ref) {
169 auto &Ctx = *unwrap(C);
170 auto AttrKind = (Attribute::AttrKind)KindID;
171 return wrap(Attribute::get(Ctx, AttrKind, unwrap(type_ref)));
172}
173
175 auto Attr = unwrap(A);
176 return wrap(Attr.getValueAsType());
177}
178
180 const char *K, unsigned KLength,
181 const char *V, unsigned VLength) {
182 return wrap(Attribute::get(*unwrap(C), StringRef(K, KLength),
183 StringRef(V, VLength)));
184}
185
187 unsigned *Length) {
188 auto S = unwrap(A).getKindAsString();
189 *Length = S.size();
190 return S.data();
191}
192
194 unsigned *Length) {
195 auto S = unwrap(A).getValueAsString();
196 *Length = S.size();
197 return S.data();
198}
199
201 auto Attr = unwrap(A);
202 return Attr.isEnumAttribute() || Attr.isIntAttribute();
203}
204
206 return unwrap(A).isStringAttribute();
207}
208
210 return unwrap(A).isTypeAttribute();
211}
212
214 std::string MsgStorage;
215 raw_string_ostream Stream(MsgStorage);
217
218 unwrap(DI)->print(DP);
219 Stream.flush();
220
221 return LLVMCreateMessage(MsgStorage.c_str());
222}
223
225 LLVMDiagnosticSeverity severity;
226
227 switch(unwrap(DI)->getSeverity()) {
228 default:
229 severity = LLVMDSError;
230 break;
231 case DS_Warning:
232 severity = LLVMDSWarning;
233 break;
234 case DS_Remark:
235 severity = LLVMDSRemark;
236 break;
237 case DS_Note:
238 severity = LLVMDSNote;
239 break;
240 }
241
242 return severity;
243}
244
245/*===-- Operations on modules ---------------------------------------------===*/
246
248 return wrap(new Module(ModuleID, getGlobalContext()));
249}
250
253 return wrap(new Module(ModuleID, *unwrap(C)));
254}
255
257 delete unwrap(M);
258}
259
260const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) {
261 auto &Str = unwrap(M)->getModuleIdentifier();
262 *Len = Str.length();
263 return Str.c_str();
264}
265
266void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) {
267 unwrap(M)->setModuleIdentifier(StringRef(Ident, Len));
268}
269
270const char *LLVMGetSourceFileName(LLVMModuleRef M, size_t *Len) {
271 auto &Str = unwrap(M)->getSourceFileName();
272 *Len = Str.length();
273 return Str.c_str();
274}
275
276void LLVMSetSourceFileName(LLVMModuleRef M, const char *Name, size_t Len) {
277 unwrap(M)->setSourceFileName(StringRef(Name, Len));
278}
279
280/*--.. Data layout .........................................................--*/
282 return unwrap(M)->getDataLayoutStr().c_str();
283}
284
286 return LLVMGetDataLayoutStr(M);
287}
288
289void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
290 unwrap(M)->setDataLayout(DataLayoutStr);
291}
292
293/*--.. Target triple .......................................................--*/
295 return unwrap(M)->getTargetTriple().c_str();
296}
297
298void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
299 unwrap(M)->setTargetTriple(Triple);
300}
301
302/*--.. Module flags ........................................................--*/
305 const char *Key;
306 size_t KeyLen;
308};
309
312 switch (Behavior) {
314 return Module::ModFlagBehavior::Error;
316 return Module::ModFlagBehavior::Warning;
318 return Module::ModFlagBehavior::Require;
320 return Module::ModFlagBehavior::Override;
322 return Module::ModFlagBehavior::Append;
324 return Module::ModFlagBehavior::AppendUnique;
325 }
326 llvm_unreachable("Unknown LLVMModuleFlagBehavior");
327}
328
331 switch (Behavior) {
332 case Module::ModFlagBehavior::Error:
334 case Module::ModFlagBehavior::Warning:
336 case Module::ModFlagBehavior::Require:
338 case Module::ModFlagBehavior::Override:
340 case Module::ModFlagBehavior::Append:
342 case Module::ModFlagBehavior::AppendUnique:
344 default:
345 llvm_unreachable("Unhandled Flag Behavior");
346 }
347}
348
351 unwrap(M)->getModuleFlagsMetadata(MFEs);
352
354 safe_malloc(MFEs.size() * sizeof(LLVMOpaqueModuleFlagEntry)));
355 for (unsigned i = 0; i < MFEs.size(); ++i) {
356 const auto &ModuleFlag = MFEs[i];
357 Result[i].Behavior = map_from_llvmModFlagBehavior(ModuleFlag.Behavior);
358 Result[i].Key = ModuleFlag.Key->getString().data();
359 Result[i].KeyLen = ModuleFlag.Key->getString().size();
360 Result[i].Metadata = wrap(ModuleFlag.Val);
361 }
362 *Len = MFEs.size();
363 return Result;
364}
365
367 free(Entries);
368}
369
372 unsigned Index) {
374 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
375 return MFE.Behavior;
376}
377
379 unsigned Index, size_t *Len) {
381 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
382 *Len = MFE.KeyLen;
383 return MFE.Key;
384}
385
387 unsigned Index) {
389 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
390 return MFE.Metadata;
391}
392
394 const char *Key, size_t KeyLen) {
395 return wrap(unwrap(M)->getModuleFlag({Key, KeyLen}));
396}
397
399 const char *Key, size_t KeyLen,
400 LLVMMetadataRef Val) {
401 unwrap(M)->addModuleFlag(map_to_llvmModFlagBehavior(Behavior),
402 {Key, KeyLen}, unwrap(Val));
403}
404
405/*--.. Printing modules ....................................................--*/
406
408 unwrap(M)->print(errs(), nullptr,
409 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
410}
411
413 char **ErrorMessage) {
414 std::error_code EC;
415 raw_fd_ostream dest(Filename, EC, sys::fs::OF_TextWithCRLF);
416 if (EC) {
417 *ErrorMessage = strdup(EC.message().c_str());
418 return true;
419 }
420
421 unwrap(M)->print(dest, nullptr);
422
423 dest.close();
424
425 if (dest.has_error()) {
426 std::string E = "Error printing to file: " + dest.error().message();
427 *ErrorMessage = strdup(E.c_str());
428 return true;
429 }
430
431 return false;
432}
433
435 std::string buf;
436 raw_string_ostream os(buf);
437
438 unwrap(M)->print(os, nullptr);
439 os.flush();
440
441 return strdup(buf.c_str());
442}
443
444/*--.. Operations on inline assembler ......................................--*/
445void LLVMSetModuleInlineAsm2(LLVMModuleRef M, const char *Asm, size_t Len) {
446 unwrap(M)->setModuleInlineAsm(StringRef(Asm, Len));
447}
448
449void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
450 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
451}
452
453void LLVMAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm, size_t Len) {
454 unwrap(M)->appendModuleInlineAsm(StringRef(Asm, Len));
455}
456
457const char *LLVMGetModuleInlineAsm(LLVMModuleRef M, size_t *Len) {
458 auto &Str = unwrap(M)->getModuleInlineAsm();
459 *Len = Str.length();
460 return Str.c_str();
461}
462
463LLVMValueRef LLVMGetInlineAsm(LLVMTypeRef Ty, const char *AsmString,
464 size_t AsmStringSize, const char *Constraints,
465 size_t ConstraintsSize, LLVMBool HasSideEffects,
466 LLVMBool IsAlignStack,
467 LLVMInlineAsmDialect Dialect, LLVMBool CanThrow) {
469 switch (Dialect) {
472 break;
475 break;
476 }
477 return wrap(InlineAsm::get(unwrap<FunctionType>(Ty),
478 StringRef(AsmString, AsmStringSize),
479 StringRef(Constraints, ConstraintsSize),
480 HasSideEffects, IsAlignStack, AD, CanThrow));
481}
482
483const char *LLVMGetInlineAsmAsmString(LLVMValueRef InlineAsmVal, size_t *Len) {
484
485 Value *Val = unwrap<Value>(InlineAsmVal);
486 const std::string &AsmString = cast<InlineAsm>(Val)->getAsmString();
487
488 *Len = AsmString.length();
489 return AsmString.c_str();
490}
491
493 size_t *Len) {
494 Value *Val = unwrap<Value>(InlineAsmVal);
495 const std::string &ConstraintString =
496 cast<InlineAsm>(Val)->getConstraintString();
497
498 *Len = ConstraintString.length();
499 return ConstraintString.c_str();
500}
501
503
504 Value *Val = unwrap<Value>(InlineAsmVal);
505 InlineAsm::AsmDialect Dialect = cast<InlineAsm>(Val)->getDialect();
506
507 switch (Dialect) {
512 }
513
514 llvm_unreachable("Unrecognized inline assembly dialect");
516}
517
519 Value *Val = unwrap<Value>(InlineAsmVal);
520 return (LLVMTypeRef)cast<InlineAsm>(Val)->getFunctionType();
521}
522
524 Value *Val = unwrap<Value>(InlineAsmVal);
525 return cast<InlineAsm>(Val)->hasSideEffects();
526}
527
529 Value *Val = unwrap<Value>(InlineAsmVal);
530 return cast<InlineAsm>(Val)->isAlignStack();
531}
532
534 Value *Val = unwrap<Value>(InlineAsmVal);
535 return cast<InlineAsm>(Val)->canThrow();
536}
537
538/*--.. Operations on module contexts ......................................--*/
540 return wrap(&unwrap(M)->getContext());
541}
542
543
544/*===-- Operations on types -----------------------------------------------===*/
545
546/*--.. Operations on all types (mostly) ....................................--*/
547
549 switch (unwrap(Ty)->getTypeID()) {
550 case Type::VoidTyID:
551 return LLVMVoidTypeKind;
552 case Type::HalfTyID:
553 return LLVMHalfTypeKind;
554 case Type::BFloatTyID:
555 return LLVMBFloatTypeKind;
556 case Type::FloatTyID:
557 return LLVMFloatTypeKind;
558 case Type::DoubleTyID:
559 return LLVMDoubleTypeKind;
562 case Type::FP128TyID:
563 return LLVMFP128TypeKind;
566 case Type::LabelTyID:
567 return LLVMLabelTypeKind;
571 return LLVMIntegerTypeKind;
574 case Type::StructTyID:
575 return LLVMStructTypeKind;
576 case Type::ArrayTyID:
577 return LLVMArrayTypeKind;
579 return LLVMPointerTypeKind;
581 return LLVMVectorTypeKind;
583 return LLVMX86_MMXTypeKind;
585 return LLVMX86_AMXTypeKind;
586 case Type::TokenTyID:
587 return LLVMTokenTypeKind;
593 llvm_unreachable("Typed pointers are unsupported via the C API");
594 }
595 llvm_unreachable("Unhandled TypeID.");
596}
597
599{
600 return unwrap(Ty)->isSized();
601}
602
604 return wrap(&unwrap(Ty)->getContext());
605}
606
608 return unwrap(Ty)->print(errs(), /*IsForDebug=*/true);
609}
610
612 std::string buf;
613 raw_string_ostream os(buf);
614
615 if (unwrap(Ty))
616 unwrap(Ty)->print(os);
617 else
618 os << "Printing <null> Type";
619
620 os.flush();
621
622 return strdup(buf.c_str());
623}
624
625/*--.. Operations on integer types .........................................--*/
626
629}
632}
635}
638}
641}
644}
646 return wrap(IntegerType::get(*unwrap(C), NumBits));
647}
648
651}
654}
657}
660}
663}
666}
667LLVMTypeRef LLVMIntType(unsigned NumBits) {
669}
670
671unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
672 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
673}
674
675/*--.. Operations on real types ............................................--*/
676
679}
682}
685}
688}
691}
694}
697}
700}
703}
704
707}
710}
713}
716}
719}
722}
725}
728}
731}
732
733/*--.. Operations on function types ........................................--*/
734
736 LLVMTypeRef *ParamTypes, unsigned ParamCount,
737 LLVMBool IsVarArg) {
738 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
739 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
740}
741
743 return unwrap<FunctionType>(FunctionTy)->isVarArg();
744}
745
747 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
748}
749
750unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
751 return unwrap<FunctionType>(FunctionTy)->getNumParams();
752}
753
755 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
756 for (Type *T : Ty->params())
757 *Dest++ = wrap(T);
758}
759
760/*--.. Operations on struct types ..........................................--*/
761
763 unsigned ElementCount, LLVMBool Packed) {
764 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
765 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
766}
767
769 unsigned ElementCount, LLVMBool Packed) {
770 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
771 ElementCount, Packed);
772}
773
775{
776 return wrap(StructType::create(*unwrap(C), Name));
777}
778
780{
781 StructType *Type = unwrap<StructType>(Ty);
782 if (!Type->hasName())
783 return nullptr;
784 return Type->getName().data();
785}
786
787void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
788 unsigned ElementCount, LLVMBool Packed) {
789 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
790 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
791}
792
794 return unwrap<StructType>(StructTy)->getNumElements();
795}
796
798 StructType *Ty = unwrap<StructType>(StructTy);
799 for (Type *T : Ty->elements())
800 *Dest++ = wrap(T);
801}
802
804 StructType *Ty = unwrap<StructType>(StructTy);
805 return wrap(Ty->getTypeAtIndex(i));
806}
807
809 return unwrap<StructType>(StructTy)->isPacked();
810}
811
813 return unwrap<StructType>(StructTy)->isOpaque();
814}
815
817 return unwrap<StructType>(StructTy)->isLiteral();
818}
819
821 return wrap(StructType::getTypeByName(unwrap(M)->getContext(), Name));
822}
823
826}
827
828/*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
829
831 int i = 0;
832 for (auto *T : unwrap(Tp)->subtypes()) {
833 Arr[i] = wrap(T);
834 i++;
835 }
836}
837
839 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
840}
841
843 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
844}
845
847 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
848}
849
851 return true;
852}
853
855 return wrap(FixedVectorType::get(unwrap(ElementType), ElementCount));
856}
857
859 unsigned ElementCount) {
860 return wrap(ScalableVectorType::get(unwrap(ElementType), ElementCount));
861}
862
864 auto *Ty = unwrap(WrappedTy);
865 if (auto *ATy = dyn_cast<ArrayType>(Ty))
866 return wrap(ATy->getElementType());
867 return wrap(cast<VectorType>(Ty)->getElementType());
868}
869
871 return unwrap(Tp)->getNumContainedTypes();
872}
873
875 return unwrap<ArrayType>(ArrayTy)->getNumElements();
876}
877
879 return unwrap<ArrayType>(ArrayTy)->getNumElements();
880}
881
883 return unwrap<PointerType>(PointerTy)->getAddressSpace();
884}
885
886unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
887 return unwrap<VectorType>(VectorTy)->getElementCount().getKnownMinValue();
888}
889
890/*--.. Operations on other types ...........................................--*/
891
893 return wrap(PointerType::get(*unwrap(C), AddressSpace));
894}
895
897 return wrap(Type::getVoidTy(*unwrap(C)));
898}
900 return wrap(Type::getLabelTy(*unwrap(C)));
901}
903 return wrap(Type::getTokenTy(*unwrap(C)));
904}
906 return wrap(Type::getMetadataTy(*unwrap(C)));
907}
908
911}
914}
915
917 LLVMTypeRef *TypeParams,
918 unsigned TypeParamCount,
919 unsigned *IntParams,
920 unsigned IntParamCount) {
921 ArrayRef<Type *> TypeParamArray(unwrap(TypeParams), TypeParamCount);
922 ArrayRef<unsigned> IntParamArray(IntParams, IntParamCount);
923 return wrap(
924 TargetExtType::get(*unwrap(C), Name, TypeParamArray, IntParamArray));
925}
926
927/*===-- Operations on values ----------------------------------------------===*/
928
929/*--.. Operations on all values ............................................--*/
930
932 return wrap(unwrap(Val)->getType());
933}
934
936 switch(unwrap(Val)->getValueID()) {
937#define LLVM_C_API 1
938#define HANDLE_VALUE(Name) \
939 case Value::Name##Val: \
940 return LLVM##Name##ValueKind;
941#include "llvm/IR/Value.def"
942 default:
944 }
945}
946
947const char *LLVMGetValueName2(LLVMValueRef Val, size_t *Length) {
948 auto *V = unwrap(Val);
949 *Length = V->getName().size();
950 return V->getName().data();
951}
952
953void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen) {
954 unwrap(Val)->setName(StringRef(Name, NameLen));
955}
956
958 return unwrap(Val)->getName().data();
959}
960
961void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
962 unwrap(Val)->setName(Name);
963}
964
966 unwrap(Val)->print(errs(), /*IsForDebug=*/true);
967}
968
970 std::string buf;
971 raw_string_ostream os(buf);
972
973 if (unwrap(Val))
974 unwrap(Val)->print(os);
975 else
976 os << "Printing <null> Value";
977
978 os.flush();
979
980 return strdup(buf.c_str());
981}
982
984 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
985}
986
988 return unwrap<Instruction>(Inst)->hasMetadata();
989}
990
992 auto *I = unwrap<Instruction>(Inst);
993 assert(I && "Expected instruction");
994 if (auto *MD = I->getMetadata(KindID))
995 return wrap(MetadataAsValue::get(I->getContext(), MD));
996 return nullptr;
997}
998
999// MetadataAsValue uses a canonical format which strips the actual MDNode for
1000// MDNode with just a single constant value, storing just a ConstantAsMetadata
1001// This undoes this canonicalization, reconstructing the MDNode.
1003 Metadata *MD = MAV->getMetadata();
1004 assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
1005 "Expected a metadata node or a canonicalized constant");
1006
1007 if (MDNode *N = dyn_cast<MDNode>(MD))
1008 return N;
1009
1010 return MDNode::get(MAV->getContext(), MD);
1011}
1012
1013void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
1014 MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
1015
1016 unwrap<Instruction>(Inst)->setMetadata(KindID, N);
1017}
1018
1020 unsigned Kind;
1022};
1023
1026llvm_getMetadata(size_t *NumEntries,
1027 llvm::function_ref<void(MetadataEntries &)> AccessMD) {
1029 AccessMD(MVEs);
1030
1032 static_cast<LLVMOpaqueValueMetadataEntry *>(
1034 for (unsigned i = 0; i < MVEs.size(); ++i) {
1035 const auto &ModuleFlag = MVEs[i];
1036 Result[i].Kind = ModuleFlag.first;
1037 Result[i].Metadata = wrap(ModuleFlag.second);
1038 }
1039 *NumEntries = MVEs.size();
1040 return Result;
1041}
1042
1045 size_t *NumEntries) {
1046 return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
1047 Entries.clear();
1048 unwrap<Instruction>(Value)->getAllMetadata(Entries);
1049 });
1050}
1051
1052/*--.. Conversion functions ................................................--*/
1053
1054#define LLVM_DEFINE_VALUE_CAST(name) \
1055 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
1056 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
1057 }
1058
1060
1062 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
1063 if (isa<MDNode>(MD->getMetadata()) ||
1064 isa<ValueAsMetadata>(MD->getMetadata()))
1065 return Val;
1066 return nullptr;
1067}
1068
1070 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
1071 if (isa<ValueAsMetadata>(MD->getMetadata()))
1072 return Val;
1073 return nullptr;
1074}
1075
1077 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
1078 if (isa<MDString>(MD->getMetadata()))
1079 return Val;
1080 return nullptr;
1081}
1082
1083/*--.. Operations on Uses ..................................................--*/
1085 Value *V = unwrap(Val);
1086 Value::use_iterator I = V->use_begin();
1087 if (I == V->use_end())
1088 return nullptr;
1089 return wrap(&*I);
1090}
1091
1093 Use *Next = unwrap(U)->getNext();
1094 if (Next)
1095 return wrap(Next);
1096 return nullptr;
1097}
1098
1100 return wrap(unwrap(U)->getUser());
1101}
1102
1104 return wrap(unwrap(U)->get());
1105}
1106
1107/*--.. Operations on Users .................................................--*/
1108
1110 unsigned Index) {
1111 Metadata *Op = N->getOperand(Index);
1112 if (!Op)
1113 return nullptr;
1114 if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
1115 return wrap(C->getValue());
1117}
1118
1120 Value *V = unwrap(Val);
1121 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
1122 if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1123 assert(Index == 0 && "Function-local metadata can only have one operand");
1124 return wrap(L->getValue());
1125 }
1126 return getMDNodeOperandImpl(V->getContext(),
1127 cast<MDNode>(MD->getMetadata()), Index);
1128 }
1129
1130 return wrap(cast<User>(V)->getOperand(Index));
1131}
1132
1134 Value *V = unwrap(Val);
1135 return wrap(&cast<User>(V)->getOperandUse(Index));
1136}
1137
1139 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
1140}
1141
1143 Value *V = unwrap(Val);
1144 if (isa<MetadataAsValue>(V))
1145 return LLVMGetMDNodeNumOperands(Val);
1146
1147 return cast<User>(V)->getNumOperands();
1148}
1149
1150/*--.. Operations on constants of any type .................................--*/
1151
1153 return wrap(Constant::getNullValue(unwrap(Ty)));
1154}
1155
1158}
1159
1161 return wrap(UndefValue::get(unwrap(Ty)));
1162}
1163
1165 return wrap(PoisonValue::get(unwrap(Ty)));
1166}
1167
1169 return isa<Constant>(unwrap(Ty));
1170}
1171
1173 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
1174 return C->isNullValue();
1175 return false;
1176}
1177
1179 return isa<UndefValue>(unwrap(Val));
1180}
1181
1183 return isa<PoisonValue>(unwrap(Val));
1184}
1185
1187 return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
1188}
1189
1190/*--.. Operations on metadata nodes ........................................--*/
1191
1193 size_t SLen) {
1194 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
1195}
1196
1198 size_t Count) {
1199 return wrap(MDNode::get(*unwrap(C), ArrayRef<Metadata*>(unwrap(MDs), Count)));
1200}
1201
1203 unsigned SLen) {
1206 Context, MDString::get(Context, StringRef(Str, SLen))));
1207}
1208
1209LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
1210 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
1211}
1212
1214 unsigned Count) {
1217 for (auto *OV : ArrayRef(Vals, Count)) {
1218 Value *V = unwrap(OV);
1219 Metadata *MD;
1220 if (!V)
1221 MD = nullptr;
1222 else if (auto *C = dyn_cast<Constant>(V))
1224 else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
1225 MD = MDV->getMetadata();
1226 assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
1227 "outside of direct argument to call");
1228 } else {
1229 // This is function-local metadata. Pretend to make an MDNode.
1230 assert(Count == 1 &&
1231 "Expected only one operand to function-local metadata");
1233 }
1234
1235 MDs.push_back(MD);
1236 }
1238}
1239
1240LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
1241 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
1242}
1243
1245 return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
1246}
1247
1249 auto *V = unwrap(Val);
1250 if (auto *C = dyn_cast<Constant>(V))
1252 if (auto *MAV = dyn_cast<MetadataAsValue>(V))
1253 return wrap(MAV->getMetadata());
1254 return wrap(ValueAsMetadata::get(V));
1255}
1256
1257const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
1258 if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
1259 if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
1260 *Length = S->getString().size();
1261 return S->getString().data();
1262 }
1263 *Length = 0;
1264 return nullptr;
1265}
1266
1268 auto *MD = unwrap<MetadataAsValue>(V);
1269 if (isa<ValueAsMetadata>(MD->getMetadata()))
1270 return 1;
1271 return cast<MDNode>(MD->getMetadata())->getNumOperands();
1272}
1273
1275 Module *Mod = unwrap(M);
1277 if (I == Mod->named_metadata_end())
1278 return nullptr;
1279 return wrap(&*I);
1280}
1281
1283 Module *Mod = unwrap(M);
1285 if (I == Mod->named_metadata_begin())
1286 return nullptr;
1287 return wrap(&*--I);
1288}
1289
1291 NamedMDNode *NamedNode = unwrap(NMD);
1293 if (++I == NamedNode->getParent()->named_metadata_end())
1294 return nullptr;
1295 return wrap(&*I);
1296}
1297
1299 NamedMDNode *NamedNode = unwrap(NMD);
1301 if (I == NamedNode->getParent()->named_metadata_begin())
1302 return nullptr;
1303 return wrap(&*--I);
1304}
1305
1307 const char *Name, size_t NameLen) {
1308 return wrap(unwrap(M)->getNamedMetadata(StringRef(Name, NameLen)));
1309}
1310
1312 const char *Name, size_t NameLen) {
1313 return wrap(unwrap(M)->getOrInsertNamedMetadata({Name, NameLen}));
1314}
1315
1316const char *LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NMD, size_t *NameLen) {
1317 NamedMDNode *NamedNode = unwrap(NMD);
1318 *NameLen = NamedNode->getName().size();
1319 return NamedNode->getName().data();
1320}
1321
1323 auto *MD = unwrap<MetadataAsValue>(V);
1324 if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1325 *Dest = wrap(MDV->getValue());
1326 return;
1327 }
1328 const auto *N = cast<MDNode>(MD->getMetadata());
1329 const unsigned numOperands = N->getNumOperands();
1330 LLVMContext &Context = unwrap(V)->getContext();
1331 for (unsigned i = 0; i < numOperands; i++)
1332 Dest[i] = getMDNodeOperandImpl(Context, N, i);
1333}
1334
1336 LLVMMetadataRef Replacement) {
1337 auto *MD = cast<MetadataAsValue>(unwrap(V));
1338 auto *N = cast<MDNode>(MD->getMetadata());
1339 N->replaceOperandWith(Index, unwrap<Metadata>(Replacement));
1340}
1341
1343 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
1344 return N->getNumOperands();
1345 }
1346 return 0;
1347}
1348
1350 LLVMValueRef *Dest) {
1351 NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
1352 if (!N)
1353 return;
1354 LLVMContext &Context = unwrap(M)->getContext();
1355 for (unsigned i=0;i<N->getNumOperands();i++)
1356 Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
1357}
1358
1360 LLVMValueRef Val) {
1361 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
1362 if (!N)
1363 return;
1364 if (!Val)
1365 return;
1366 N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
1367}
1368
1369const char *LLVMGetDebugLocDirectory(LLVMValueRef Val, unsigned *Length) {
1370 if (!Length) return nullptr;
1371 StringRef S;
1372 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1373 if (const auto &DL = I->getDebugLoc()) {
1374 S = DL->getDirectory();
1375 }
1376 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1378 GV->getDebugInfo(GVEs);
1379 if (GVEs.size())
1380 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1381 S = DGV->getDirectory();
1382 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1383 if (const DISubprogram *DSP = F->getSubprogram())
1384 S = DSP->getDirectory();
1385 } else {
1386 assert(0 && "Expected Instruction, GlobalVariable or Function");
1387 return nullptr;
1388 }
1389 *Length = S.size();
1390 return S.data();
1391}
1392
1393const char *LLVMGetDebugLocFilename(LLVMValueRef Val, unsigned *Length) {
1394 if (!Length) return nullptr;
1395 StringRef S;
1396 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1397 if (const auto &DL = I->getDebugLoc()) {
1398 S = DL->getFilename();
1399 }
1400 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1402 GV->getDebugInfo(GVEs);
1403 if (GVEs.size())
1404 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1405 S = DGV->getFilename();
1406 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1407 if (const DISubprogram *DSP = F->getSubprogram())
1408 S = DSP->getFilename();
1409 } else {
1410 assert(0 && "Expected Instruction, GlobalVariable or Function");
1411 return nullptr;
1412 }
1413 *Length = S.size();
1414 return S.data();
1415}
1416
1418 unsigned L = 0;
1419 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1420 if (const auto &DL = I->getDebugLoc()) {
1421 L = DL->getLine();
1422 }
1423 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1425 GV->getDebugInfo(GVEs);
1426 if (GVEs.size())
1427 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1428 L = DGV->getLine();
1429 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1430 if (const DISubprogram *DSP = F->getSubprogram())
1431 L = DSP->getLine();
1432 } else {
1433 assert(0 && "Expected Instruction, GlobalVariable or Function");
1434 return -1;
1435 }
1436 return L;
1437}
1438
1440 unsigned C = 0;
1441 if (const auto *I = dyn_cast<Instruction>(unwrap(Val)))
1442 if (const auto &DL = I->getDebugLoc())
1443 C = DL->getColumn();
1444 return C;
1445}
1446
1447/*--.. Operations on scalar constants ......................................--*/
1448
1449LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
1450 LLVMBool SignExtend) {
1451 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
1452}
1453
1455 unsigned NumWords,
1456 const uint64_t Words[]) {
1457 IntegerType *Ty = unwrap<IntegerType>(IntTy);
1458 return wrap(ConstantInt::get(
1459 Ty->getContext(), APInt(Ty->getBitWidth(), ArrayRef(Words, NumWords))));
1460}
1461
1463 uint8_t Radix) {
1464 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
1465 Radix));
1466}
1467
1469 unsigned SLen, uint8_t Radix) {
1470 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
1471 Radix));
1472}
1473
1475 return wrap(ConstantFP::get(unwrap(RealTy), N));
1476}
1477
1479 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
1480}
1481
1483 unsigned SLen) {
1484 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
1485}
1486
1487unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
1488 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
1489}
1490
1492 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
1493}
1494
1495double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
1496 ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
1497 Type *Ty = cFP->getType();
1498
1499 if (Ty->isHalfTy() || Ty->isBFloatTy() || Ty->isFloatTy() ||
1500 Ty->isDoubleTy()) {
1501 *LosesInfo = false;
1502 return cFP->getValueAPF().convertToDouble();
1503 }
1504
1505 bool APFLosesInfo;
1506 APFloat APF = cFP->getValueAPF();
1507 APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo);
1508 *LosesInfo = APFLosesInfo;
1509 return APF.convertToDouble();
1510}
1511
1512/*--.. Operations on composite constants ...................................--*/
1513
1515 unsigned Length,
1516 LLVMBool DontNullTerminate) {
1517 /* Inverted the sense of AddNull because ', 0)' is a
1518 better mnemonic for null termination than ', 1)'. */
1520 DontNullTerminate == 0));
1521}
1522
1523LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
1524 LLVMBool DontNullTerminate) {
1526 DontNullTerminate);
1527}
1528
1530 return wrap(unwrap<Constant>(C)->getAggregateElement(Idx));
1531}
1532
1534 return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
1535}
1536
1538 return unwrap<ConstantDataSequential>(C)->isString();
1539}
1540
1541const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1542 StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1543 *Length = Str.size();
1544 return Str.data();
1545}
1546
1548 LLVMValueRef *ConstantVals, unsigned Length) {
1549 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1550 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1551}
1552
1554 uint64_t Length) {
1555 ArrayRef<Constant *> V(unwrap<Constant>(ConstantVals, Length), Length);
1556 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1557}
1558
1560 LLVMValueRef *ConstantVals,
1561 unsigned Count, LLVMBool Packed) {
1562 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1563 return wrap(ConstantStruct::getAnon(*unwrap(C), ArrayRef(Elements, Count),
1564 Packed != 0));
1565}
1566
1567LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
1568 LLVMBool Packed) {
1569 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1570 Packed);
1571}
1572
1574 LLVMValueRef *ConstantVals,
1575 unsigned Count) {
1576 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1577 StructType *Ty = unwrap<StructType>(StructTy);
1578
1579 return wrap(ConstantStruct::get(Ty, ArrayRef(Elements, Count)));
1580}
1581
1582LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
1584 ArrayRef(unwrap<Constant>(ScalarConstantVals, Size), Size)));
1585}
1586
1587/*-- Opcode mapping */
1588
1590{
1591 switch (opcode) {
1592 default: llvm_unreachable("Unhandled Opcode.");
1593#define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
1594#include "llvm/IR/Instruction.def"
1595#undef HANDLE_INST
1596 }
1597}
1598
1600{
1601 switch (code) {
1602#define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
1603#include "llvm/IR/Instruction.def"
1604#undef HANDLE_INST
1605 }
1606 llvm_unreachable("Unhandled Opcode.");
1607}
1608
1609/*--.. Constant expressions ................................................--*/
1610
1612 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
1613}
1614
1617}
1618
1620 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
1621}
1622
1624 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1625}
1626
1628 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
1629}
1630
1632 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
1633}
1634
1635
1637 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
1638}
1639
1641 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
1642 unwrap<Constant>(RHSConstant)));
1643}
1644
1646 LLVMValueRef RHSConstant) {
1647 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
1648 unwrap<Constant>(RHSConstant)));
1649}
1650
1652 LLVMValueRef RHSConstant) {
1653 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
1654 unwrap<Constant>(RHSConstant)));
1655}
1656
1658 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
1659 unwrap<Constant>(RHSConstant)));
1660}
1661
1663 LLVMValueRef RHSConstant) {
1664 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
1665 unwrap<Constant>(RHSConstant)));
1666}
1667
1669 LLVMValueRef RHSConstant) {
1670 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
1671 unwrap<Constant>(RHSConstant)));
1672}
1673
1675 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
1676 unwrap<Constant>(RHSConstant)));
1677}
1678
1680 LLVMValueRef RHSConstant) {
1681 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
1682 unwrap<Constant>(RHSConstant)));
1683}
1684
1686 LLVMValueRef RHSConstant) {
1687 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
1688 unwrap<Constant>(RHSConstant)));
1689}
1690
1692 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
1693 unwrap<Constant>(RHSConstant)));
1694}
1695
1697 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1698 return wrap(ConstantExpr::getICmp(Predicate,
1699 unwrap<Constant>(LHSConstant),
1700 unwrap<Constant>(RHSConstant)));
1701}
1702
1704 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1705 return wrap(ConstantExpr::getFCmp(Predicate,
1706 unwrap<Constant>(LHSConstant),
1707 unwrap<Constant>(RHSConstant)));
1708}
1709
1711 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1712 unwrap<Constant>(RHSConstant)));
1713}
1714
1716 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
1717 unwrap<Constant>(RHSConstant)));
1718}
1719
1721 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1722 unwrap<Constant>(RHSConstant)));
1723}
1724
1726 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1727 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1728 NumIndices);
1729 Constant *Val = unwrap<Constant>(ConstantVal);
1730 return wrap(ConstantExpr::getGetElementPtr(unwrap(Ty), Val, IdxList));
1731}
1732
1734 LLVMValueRef *ConstantIndices,
1735 unsigned NumIndices) {
1736 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1737 NumIndices);
1738 Constant *Val = unwrap<Constant>(ConstantVal);
1739 return wrap(ConstantExpr::getInBoundsGetElementPtr(unwrap(Ty), Val, IdxList));
1740}
1741
1743 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1744 unwrap(ToType)));
1745}
1746
1748 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1749 unwrap(ToType)));
1750}
1751
1753 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1754 unwrap(ToType)));
1755}
1756
1758 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1759 unwrap(ToType)));
1760}
1761
1763 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1764 unwrap(ToType)));
1765}
1766
1768 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1769 unwrap(ToType)));
1770}
1771
1773 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1774 unwrap(ToType)));
1775}
1776
1778 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1779 unwrap(ToType)));
1780}
1781
1783 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1784 unwrap(ToType)));
1785}
1786
1788 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1789 unwrap(ToType)));
1790}
1791
1793 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1794 unwrap(ToType)));
1795}
1796
1798 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1799 unwrap(ToType)));
1800}
1801
1803 LLVMTypeRef ToType) {
1804 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1805 unwrap(ToType)));
1806}
1807
1809 LLVMTypeRef ToType) {
1810 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1811 unwrap(ToType)));
1812}
1813
1815 LLVMTypeRef ToType) {
1816 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1817 unwrap(ToType)));
1818}
1819
1821 LLVMTypeRef ToType) {
1822 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1823 unwrap(ToType)));
1824}
1825
1827 LLVMTypeRef ToType) {
1828 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1829 unwrap(ToType)));
1830}
1831
1834 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1835 unwrap(ToType), isSigned));
1836}
1837
1839 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1840 unwrap(ToType)));
1841}
1842
1844 LLVMValueRef IndexConstant) {
1845 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1846 unwrap<Constant>(IndexConstant)));
1847}
1848
1850 LLVMValueRef ElementValueConstant,
1851 LLVMValueRef IndexConstant) {
1852 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1853 unwrap<Constant>(ElementValueConstant),
1854 unwrap<Constant>(IndexConstant)));
1855}
1856
1858 LLVMValueRef VectorBConstant,
1859 LLVMValueRef MaskConstant) {
1860 SmallVector<int, 16> IntMask;
1861 ShuffleVectorInst::getShuffleMask(unwrap<Constant>(MaskConstant), IntMask);
1862 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1863 unwrap<Constant>(VectorBConstant),
1864 IntMask));
1865}
1866
1868 const char *Constraints,
1869 LLVMBool HasSideEffects,
1870 LLVMBool IsAlignStack) {
1871 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1872 Constraints, HasSideEffects, IsAlignStack));
1873}
1874
1876 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1877}
1878
1879/*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1880
1882 return wrap(unwrap<GlobalValue>(Global)->getParent());
1883}
1884
1886 return unwrap<GlobalValue>(Global)->isDeclaration();
1887}
1888
1890 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1892 return LLVMExternalLinkage;
1900 return LLVMWeakAnyLinkage;
1902 return LLVMWeakODRLinkage;
1904 return LLVMAppendingLinkage;
1906 return LLVMInternalLinkage;
1908 return LLVMPrivateLinkage;
1912 return LLVMCommonLinkage;
1913 }
1914
1915 llvm_unreachable("Invalid GlobalValue linkage!");
1916}
1917
1919 GlobalValue *GV = unwrap<GlobalValue>(Global);
1920
1921 switch (Linkage) {
1924 break;
1927 break;
1930 break;
1933 break;
1935 LLVM_DEBUG(
1936 errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1937 "longer supported.");
1938 break;
1939 case LLVMWeakAnyLinkage:
1941 break;
1942 case LLVMWeakODRLinkage:
1944 break;
1947 break;
1950 break;
1951 case LLVMPrivateLinkage:
1953 break;
1956 break;
1959 break;
1961 LLVM_DEBUG(
1962 errs()
1963 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1964 break;
1966 LLVM_DEBUG(
1967 errs()
1968 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1969 break;
1972 break;
1973 case LLVMGhostLinkage:
1974 LLVM_DEBUG(
1975 errs() << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1976 break;
1977 case LLVMCommonLinkage:
1979 break;
1980 }
1981}
1982
1984 // Using .data() is safe because of how GlobalObject::setSection is
1985 // implemented.
1986 return unwrap<GlobalValue>(Global)->getSection().data();
1987}
1988
1989void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1990 unwrap<GlobalObject>(Global)->setSection(Section);
1991}
1992
1994 return static_cast<LLVMVisibility>(
1995 unwrap<GlobalValue>(Global)->getVisibility());
1996}
1997
1999 unwrap<GlobalValue>(Global)
2000 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
2001}
2002
2004 return static_cast<LLVMDLLStorageClass>(
2005 unwrap<GlobalValue>(Global)->getDLLStorageClass());
2006}
2007
2009 unwrap<GlobalValue>(Global)->setDLLStorageClass(
2010 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
2011}
2012
2014 switch (unwrap<GlobalValue>(Global)->getUnnamedAddr()) {
2015 case GlobalVariable::UnnamedAddr::None:
2016 return LLVMNoUnnamedAddr;
2017 case GlobalVariable::UnnamedAddr::Local:
2018 return LLVMLocalUnnamedAddr;
2019 case GlobalVariable::UnnamedAddr::Global:
2020 return LLVMGlobalUnnamedAddr;
2021 }
2022 llvm_unreachable("Unknown UnnamedAddr kind!");
2023}
2024
2026 GlobalValue *GV = unwrap<GlobalValue>(Global);
2027
2028 switch (UnnamedAddr) {
2029 case LLVMNoUnnamedAddr:
2030 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::None);
2032 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Local);
2034 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Global);
2035 }
2036}
2037
2039 return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
2040}
2041
2043 unwrap<GlobalValue>(Global)->setUnnamedAddr(
2044 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
2045 : GlobalValue::UnnamedAddr::None);
2046}
2047
2049 return wrap(unwrap<GlobalValue>(Global)->getValueType());
2050}
2051
2052/*--.. Operations on global variables, load and store instructions .........--*/
2053
2055 Value *P = unwrap(V);
2056 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2057 return GV->getAlign() ? GV->getAlign()->value() : 0;
2058 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
2059 return AI->getAlign().value();
2060 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2061 return LI->getAlign().value();
2062 if (StoreInst *SI = dyn_cast<StoreInst>(P))
2063 return SI->getAlign().value();
2064 if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2065 return RMWI->getAlign().value();
2066 if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2067 return CXI->getAlign().value();
2068
2070 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, "
2071 "and AtomicCmpXchgInst have alignment");
2072}
2073
2074void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
2075 Value *P = unwrap(V);
2076 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2077 GV->setAlignment(MaybeAlign(Bytes));
2078 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
2079 AI->setAlignment(Align(Bytes));
2080 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
2081 LI->setAlignment(Align(Bytes));
2082 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
2083 SI->setAlignment(Align(Bytes));
2084 else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2085 RMWI->setAlignment(Align(Bytes));
2086 else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2087 CXI->setAlignment(Align(Bytes));
2088 else
2090 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, and "
2091 "and AtomicCmpXchgInst have alignment");
2092}
2093
2095 size_t *NumEntries) {
2096 return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
2097 Entries.clear();
2098 if (Instruction *Instr = dyn_cast<Instruction>(unwrap(Value))) {
2099 Instr->getAllMetadata(Entries);
2100 } else {
2101 unwrap<GlobalObject>(Value)->getAllMetadata(Entries);
2102 }
2103 });
2104}
2105
2107 unsigned Index) {
2109 static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2110 return MVE.Kind;
2111}
2112
2115 unsigned Index) {
2117 static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2118 return MVE.Metadata;
2119}
2120
2122 free(Entries);
2123}
2124
2126 LLVMMetadataRef MD) {
2127 unwrap<GlobalObject>(Global)->setMetadata(Kind, unwrap<MDNode>(MD));
2128}
2129
2131 unwrap<GlobalObject>(Global)->eraseMetadata(Kind);
2132}
2133
2135 unwrap<GlobalObject>(Global)->clearMetadata();
2136}
2137
2138/*--.. Operations on global variables ......................................--*/
2139
2141 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2143}
2144
2146 const char *Name,
2147 unsigned AddressSpace) {
2148 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2150 nullptr, GlobalVariable::NotThreadLocal,
2151 AddressSpace));
2152}
2153
2155 return wrap(unwrap(M)->getNamedGlobal(Name));
2156}
2157
2159 Module *Mod = unwrap(M);
2161 if (I == Mod->global_end())
2162 return nullptr;
2163 return wrap(&*I);
2164}
2165
2167 Module *Mod = unwrap(M);
2169 if (I == Mod->global_begin())
2170 return nullptr;
2171 return wrap(&*--I);
2172}
2173
2175 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2177 if (++I == GV->getParent()->global_end())
2178 return nullptr;
2179 return wrap(&*I);
2180}
2181
2183 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2185 if (I == GV->getParent()->global_begin())
2186 return nullptr;
2187 return wrap(&*--I);
2188}
2189
2191 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
2192}
2193
2195 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
2196 if ( !GV->hasInitializer() )
2197 return nullptr;
2198 return wrap(GV->getInitializer());
2199}
2200
2201void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
2202 unwrap<GlobalVariable>(GlobalVar)
2203 ->setInitializer(unwrap<Constant>(ConstantVal));
2204}
2205
2207 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
2208}
2209
2210void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
2211 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
2212}
2213
2215 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
2216}
2217
2218void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
2219 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
2220}
2221
2223 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
2224 case GlobalVariable::NotThreadLocal:
2225 return LLVMNotThreadLocal;
2226 case GlobalVariable::GeneralDynamicTLSModel:
2228 case GlobalVariable::LocalDynamicTLSModel:
2230 case GlobalVariable::InitialExecTLSModel:
2232 case GlobalVariable::LocalExecTLSModel:
2233 return LLVMLocalExecTLSModel;
2234 }
2235
2236 llvm_unreachable("Invalid GlobalVariable thread local mode");
2237}
2238
2240 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2241
2242 switch (Mode) {
2243 case LLVMNotThreadLocal:
2244 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
2245 break;
2247 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
2248 break;
2250 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
2251 break;
2253 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
2254 break;
2256 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
2257 break;
2258 }
2259}
2260
2262 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
2263}
2264
2266 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
2267}
2268
2269/*--.. Operations on aliases ......................................--*/
2270
2272 unsigned AddrSpace, LLVMValueRef Aliasee,
2273 const char *Name) {
2274 return wrap(GlobalAlias::create(unwrap(ValueTy), AddrSpace,
2276 unwrap<Constant>(Aliasee), unwrap(M)));
2277}
2278
2280 const char *Name, size_t NameLen) {
2281 return wrap(unwrap(M)->getNamedAlias(StringRef(Name, NameLen)));
2282}
2283
2285 Module *Mod = unwrap(M);
2287 if (I == Mod->alias_end())
2288 return nullptr;
2289 return wrap(&*I);
2290}
2291
2293 Module *Mod = unwrap(M);
2295 if (I == Mod->alias_begin())
2296 return nullptr;
2297 return wrap(&*--I);
2298}
2299
2301 GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2303 if (++I == Alias->getParent()->alias_end())
2304 return nullptr;
2305 return wrap(&*I);
2306}
2307
2309 GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2311 if (I == Alias->getParent()->alias_begin())
2312 return nullptr;
2313 return wrap(&*--I);
2314}
2315
2317 return wrap(unwrap<GlobalAlias>(Alias)->getAliasee());
2318}
2319
2321 unwrap<GlobalAlias>(Alias)->setAliasee(unwrap<Constant>(Aliasee));
2322}
2323
2324/*--.. Operations on functions .............................................--*/
2325
2327 LLVMTypeRef FunctionTy) {
2328 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
2330}
2331
2333 return wrap(unwrap(M)->getFunction(Name));
2334}
2335
2337 Module *Mod = unwrap(M);
2339 if (I == Mod->end())
2340 return nullptr;
2341 return wrap(&*I);
2342}
2343
2345 Module *Mod = unwrap(M);
2347 if (I == Mod->begin())
2348 return nullptr;
2349 return wrap(&*--I);
2350}
2351
2353 Function *Func = unwrap<Function>(Fn);
2354 Module::iterator I(Func);
2355 if (++I == Func->getParent()->end())
2356 return nullptr;
2357 return wrap(&*I);
2358}
2359
2361 Function *Func = unwrap<Function>(Fn);
2362 Module::iterator I(Func);
2363 if (I == Func->getParent()->begin())
2364 return nullptr;
2365 return wrap(&*--I);
2366}
2367
2369 unwrap<Function>(Fn)->eraseFromParent();
2370}
2371
2373 return unwrap<Function>(Fn)->hasPersonalityFn();
2374}
2375
2377 return wrap(unwrap<Function>(Fn)->getPersonalityFn());
2378}
2379
2381 unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
2382}
2383
2385 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
2386 return F->getIntrinsicID();
2387 return 0;
2388}
2389
2391 assert(ID < llvm::Intrinsic::num_intrinsics && "Intrinsic ID out of range");
2392 return llvm::Intrinsic::ID(ID);
2393}
2394
2396 unsigned ID,
2397 LLVMTypeRef *ParamTypes,
2398 size_t ParamCount) {
2399 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2400 auto IID = llvm_map_to_intrinsic_id(ID);
2401 return wrap(llvm::Intrinsic::getDeclaration(unwrap(Mod), IID, Tys));
2402}
2403
2404const char *LLVMIntrinsicGetName(unsigned ID, size_t *NameLength) {
2405 auto IID = llvm_map_to_intrinsic_id(ID);
2406 auto Str = llvm::Intrinsic::getName(IID);
2407 *NameLength = Str.size();
2408 return Str.data();
2409}
2410
2412 LLVMTypeRef *ParamTypes, size_t ParamCount) {
2413 auto IID = llvm_map_to_intrinsic_id(ID);
2414 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2415 return wrap(llvm::Intrinsic::getType(*unwrap(Ctx), IID, Tys));
2416}
2417
2419 LLVMTypeRef *ParamTypes,
2420 size_t ParamCount,
2421 size_t *NameLength) {
2422 auto IID = llvm_map_to_intrinsic_id(ID);
2423 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2424 auto Str = llvm::Intrinsic::getNameNoUnnamedTypes(IID, Tys);
2425 *NameLength = Str.length();
2426 return strdup(Str.c_str());
2427}
2428
2430 LLVMTypeRef *ParamTypes,
2431 size_t ParamCount,
2432 size_t *NameLength) {
2433 auto IID = llvm_map_to_intrinsic_id(ID);
2434 ArrayRef<Type *> Tys(unwrap(ParamTypes), ParamCount);
2435 auto Str = llvm::Intrinsic::getName(IID, Tys, unwrap(Mod));
2436 *NameLength = Str.length();
2437 return strdup(Str.c_str());
2438}
2439
2440unsigned LLVMLookupIntrinsicID(const char *Name, size_t NameLen) {
2441 return Function::lookupIntrinsicID({Name, NameLen});
2442}
2443
2445 auto IID = llvm_map_to_intrinsic_id(ID);
2447}
2448
2450 return unwrap<Function>(Fn)->getCallingConv();
2451}
2452
2454 return unwrap<Function>(Fn)->setCallingConv(
2455 static_cast<CallingConv::ID>(CC));
2456}
2457
2458const char *LLVMGetGC(LLVMValueRef Fn) {
2459 Function *F = unwrap<Function>(Fn);
2460 return F->hasGC()? F->getGC().c_str() : nullptr;
2461}
2462
2463void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
2464 Function *F = unwrap<Function>(Fn);
2465 if (GC)
2466 F->setGC(GC);
2467 else
2468 F->clearGC();
2469}
2470
2473 unwrap<Function>(F)->addAttributeAtIndex(Idx, unwrap(A));
2474}
2475
2477 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2478 return AS.getNumAttributes();
2479}
2480
2482 LLVMAttributeRef *Attrs) {
2483 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2484 for (auto A : AS)
2485 *Attrs++ = wrap(A);
2486}
2487
2490 unsigned KindID) {
2491 return wrap(unwrap<Function>(F)->getAttributeAtIndex(
2492 Idx, (Attribute::AttrKind)KindID));
2493}
2494
2497 const char *K, unsigned KLen) {
2498 return wrap(
2499 unwrap<Function>(F)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
2500}
2501
2503 unsigned KindID) {
2504 unwrap<Function>(F)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
2505}
2506
2508 const char *K, unsigned KLen) {
2509 unwrap<Function>(F)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
2510}
2511
2513 const char *V) {
2514 Function *Func = unwrap<Function>(Fn);
2515 Attribute Attr = Attribute::get(Func->getContext(), A, V);
2516 Func->addFnAttr(Attr);
2517}
2518
2519/*--.. Operations on parameters ............................................--*/
2520
2522 // This function is strictly redundant to
2523 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
2524 return unwrap<Function>(FnRef)->arg_size();
2525}
2526
2527void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
2528 Function *Fn = unwrap<Function>(FnRef);
2529 for (Argument &A : Fn->args())
2530 *ParamRefs++ = wrap(&A);
2531}
2532
2534 Function *Fn = unwrap<Function>(FnRef);
2535 return wrap(&Fn->arg_begin()[index]);
2536}
2537
2539 return wrap(unwrap<Argument>(V)->getParent());
2540}
2541
2543 Function *Func = unwrap<Function>(Fn);
2544 Function::arg_iterator I = Func->arg_begin();
2545 if (I == Func->arg_end())
2546 return nullptr;
2547 return wrap(&*I);
2548}
2549
2551 Function *Func = unwrap<Function>(Fn);
2552 Function::arg_iterator I = Func->arg_end();
2553 if (I == Func->arg_begin())
2554 return nullptr;
2555 return wrap(&*--I);
2556}
2557
2559 Argument *A = unwrap<Argument>(Arg);
2560 Function *Fn = A->getParent();
2561 if (A->getArgNo() + 1 >= Fn->arg_size())
2562 return nullptr;
2563 return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
2564}
2565
2567 Argument *A = unwrap<Argument>(Arg);
2568 if (A->getArgNo() == 0)
2569 return nullptr;
2570 return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
2571}
2572
2573void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
2574 Argument *A = unwrap<Argument>(Arg);
2575 A->addAttr(Attribute::getWithAlignment(A->getContext(), Align(align)));
2576}
2577
2578/*--.. Operations on ifuncs ................................................--*/
2579
2581 const char *Name, size_t NameLen,
2582 LLVMTypeRef Ty, unsigned AddrSpace,
2584 return wrap(GlobalIFunc::create(unwrap(Ty), AddrSpace,
2586 StringRef(Name, NameLen),
2587 unwrap<Constant>(Resolver), unwrap(M)));
2588}
2589
2591 const char *Name, size_t NameLen) {
2592 return wrap(unwrap(M)->getNamedIFunc(StringRef(Name, NameLen)));
2593}
2594
2596 Module *Mod = unwrap(M);
2598 if (I == Mod->ifunc_end())
2599 return nullptr;
2600 return wrap(&*I);
2601}
2602
2604 Module *Mod = unwrap(M);
2606 if (I == Mod->ifunc_begin())
2607 return nullptr;
2608 return wrap(&*--I);
2609}
2610
2612 GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2614 if (++I == GIF->getParent()->ifunc_end())
2615 return nullptr;
2616 return wrap(&*I);
2617}
2618
2620 GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2622 if (I == GIF->getParent()->ifunc_begin())
2623 return nullptr;
2624 return wrap(&*--I);
2625}
2626
2628 return wrap(unwrap<GlobalIFunc>(IFunc)->getResolver());
2629}
2630
2632 unwrap<GlobalIFunc>(IFunc)->setResolver(unwrap<Constant>(Resolver));
2633}
2634
2636 unwrap<GlobalIFunc>(IFunc)->eraseFromParent();
2637}
2638
2640 unwrap<GlobalIFunc>(IFunc)->removeFromParent();
2641}
2642
2643/*--.. Operations on basic blocks ..........................................--*/
2644
2646 return wrap(static_cast<Value*>(unwrap(BB)));
2647}
2648
2650 return isa<BasicBlock>(unwrap(Val));
2651}
2652
2654 return wrap(unwrap<BasicBlock>(Val));
2655}
2656
2658 return unwrap(BB)->getName().data();
2659}
2660
2662 return wrap(unwrap(BB)->getParent());
2663}
2664
2666 return wrap(unwrap(BB)->getTerminator());
2667}
2668
2670 return unwrap<Function>(FnRef)->size();
2671}
2672
2674 Function *Fn = unwrap<Function>(FnRef);
2675 for (BasicBlock &BB : *Fn)
2676 *BasicBlocksRefs++ = wrap(&BB);
2677}
2678
2680 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2681}
2682
2684 Function *Func = unwrap<Function>(Fn);
2685 Function::iterator I = Func->begin();
2686 if (I == Func->end())
2687 return nullptr;
2688 return wrap(&*I);
2689}
2690
2692 Function *Func = unwrap<Function>(Fn);
2693 Function::iterator I = Func->end();
2694 if (I == Func->begin())
2695 return nullptr;
2696 return wrap(&*--I);
2697}
2698
2700 BasicBlock *Block = unwrap(BB);
2702 if (++I == Block->getParent()->end())
2703 return nullptr;
2704 return wrap(&*I);
2705}
2706
2708 BasicBlock *Block = unwrap(BB);
2710 if (I == Block->getParent()->begin())
2711 return nullptr;
2712 return wrap(&*--I);
2713}
2714
2716 const char *Name) {
2718}
2719
2721 LLVMBasicBlockRef BB) {
2722 BasicBlock *ToInsert = unwrap(BB);
2723 BasicBlock *CurBB = unwrap(Builder)->GetInsertBlock();
2724 assert(CurBB && "current insertion point is invalid!");
2725 CurBB->getParent()->insert(std::next(CurBB->getIterator()), ToInsert);
2726}
2727
2729 LLVMBasicBlockRef BB) {
2730 unwrap<Function>(Fn)->insert(unwrap<Function>(Fn)->end(), unwrap(BB));
2731}
2732
2734 LLVMValueRef FnRef,
2735 const char *Name) {
2736 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
2737}
2738
2741}
2742
2744 LLVMBasicBlockRef BBRef,
2745 const char *Name) {
2746 BasicBlock *BB = unwrap(BBRef);
2747 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2748}
2749
2751 const char *Name) {
2753}
2754
2756 unwrap(BBRef)->eraseFromParent();
2757}
2758
2760 unwrap(BBRef)->removeFromParent();
2761}
2762
2764 unwrap(BB)->moveBefore(unwrap(MovePos));
2765}
2766
2768 unwrap(BB)->moveAfter(unwrap(MovePos));
2769}
2770
2771/*--.. Operations on instructions ..........................................--*/
2772
2774 return wrap(unwrap<Instruction>(Inst)->getParent());
2775}
2776
2778 BasicBlock *Block = unwrap(BB);
2779 BasicBlock::iterator I = Block->begin();
2780 if (I == Block->end())
2781 return nullptr;
2782 return wrap(&*I);
2783}
2784
2786 BasicBlock *Block = unwrap(BB);
2787 BasicBlock::iterator I = Block->end();
2788 if (I == Block->begin())
2789 return nullptr;
2790 return wrap(&*--I);
2791}
2792
2794 Instruction *Instr = unwrap<Instruction>(Inst);
2795 BasicBlock::iterator I(Instr);
2796 if (++I == Instr->getParent()->end())
2797 return nullptr;
2798 return wrap(&*I);
2799}
2800
2802 Instruction *Instr = unwrap<Instruction>(Inst);
2803 BasicBlock::iterator I(Instr);
2804 if (I == Instr->getParent()->begin())
2805 return nullptr;
2806 return wrap(&*--I);
2807}
2808
2810 unwrap<Instruction>(Inst)->removeFromParent();
2811}
2812
2814 unwrap<Instruction>(Inst)->eraseFromParent();
2815}
2816
2818 unwrap<Instruction>(Inst)->deleteValue();
2819}
2820
2822 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2823 return (LLVMIntPredicate)I->getPredicate();
2824 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2825 if (CE->getOpcode() == Instruction::ICmp)
2826 return (LLVMIntPredicate)CE->getPredicate();
2827 return (LLVMIntPredicate)0;
2828}
2829
2831 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2832 return (LLVMRealPredicate)I->getPredicate();
2833 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2834 if (CE->getOpcode() == Instruction::FCmp)
2835 return (LLVMRealPredicate)CE->getPredicate();
2836 return (LLVMRealPredicate)0;
2837}
2838
2840 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2841 return map_to_llvmopcode(C->getOpcode());
2842 return (LLVMOpcode)0;
2843}
2844
2846 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2847 return wrap(C->clone());
2848 return nullptr;
2849}
2850
2852 Instruction *I = dyn_cast<Instruction>(unwrap(Inst));
2853 return (I && I->isTerminator()) ? wrap(I) : nullptr;
2854}
2855
2857 if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) {
2858 return FPI->arg_size();
2859 }
2860 return unwrap<CallBase>(Instr)->arg_size();
2861}
2862
2863/*--.. Call and invoke instructions ........................................--*/
2864
2866 return unwrap<CallBase>(Instr)->getCallingConv();
2867}
2868
2870 return unwrap<CallBase>(Instr)->setCallingConv(
2871 static_cast<CallingConv::ID>(CC));
2872}
2873
2875 unsigned align) {
2876 auto *Call = unwrap<CallBase>(Instr);
2877 Attribute AlignAttr =
2878 Attribute::getWithAlignment(Call->getContext(), Align(align));
2879 Call->addAttributeAtIndex(Idx, AlignAttr);
2880}
2881
2884 unwrap<CallBase>(C)->addAttributeAtIndex(Idx, unwrap(A));
2885}
2886
2889 auto *Call = unwrap<CallBase>(C);
2890 auto AS = Call->getAttributes().getAttributes(Idx);
2891 return AS.getNumAttributes();
2892}
2893
2895 LLVMAttributeRef *Attrs) {
2896 auto *Call = unwrap<CallBase>(C);
2897 auto AS = Call->getAttributes().getAttributes(Idx);
2898 for (auto A : AS)
2899 *Attrs++ = wrap(A);
2900}
2901
2904 unsigned KindID) {
2905 return wrap(unwrap<CallBase>(C)->getAttributeAtIndex(
2906 Idx, (Attribute::AttrKind)KindID));
2907}
2908
2911 const char *K, unsigned KLen) {
2912 return wrap(
2913 unwrap<CallBase>(C)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
2914}
2915
2917 unsigned KindID) {
2918 unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
2919}
2920
2922 const char *K, unsigned KLen) {
2923 unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
2924}
2925
2927 return wrap(unwrap<CallBase>(Instr)->getCalledOperand());
2928}
2929
2931 return wrap(unwrap<CallBase>(Instr)->getFunctionType());
2932}
2933
2934/*--.. Operations on call instructions (only) ..............................--*/
2935
2937 return unwrap<CallInst>(Call)->isTailCall();
2938}
2939
2940void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
2941 unwrap<CallInst>(Call)->setTailCall(isTailCall);
2942}
2943
2945 return (LLVMTailCallKind)unwrap<CallInst>(Call)->getTailCallKind();
2946}
2947
2949 unwrap<CallInst>(Call)->setTailCallKind((CallInst::TailCallKind)kind);
2950}
2951
2952/*--.. Operations on invoke instructions (only) ............................--*/
2953
2955 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2956}
2957
2959 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2960 return wrap(CRI->getUnwindDest());
2961 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2962 return wrap(CSI->getUnwindDest());
2963 }
2964 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2965}
2966
2968 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2969}
2970
2972 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2973 return CRI->setUnwindDest(unwrap(B));
2974 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2975 return CSI->setUnwindDest(unwrap(B));
2976 }
2977 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2978}
2979
2980/*--.. Operations on terminators ...........................................--*/
2981
2983 return unwrap<Instruction>(Term)->getNumSuccessors();
2984}
2985
2987 return wrap(unwrap<Instruction>(Term)->getSuccessor(i));
2988}
2989
2991 return unwrap<Instruction>(Term)->setSuccessor(i, unwrap(block));
2992}
2993
2994/*--.. Operations on branch instructions (only) ............................--*/
2995
2997 return unwrap<BranchInst>(Branch)->isConditional();
2998}
2999
3001 return wrap(unwrap<BranchInst>(Branch)->getCondition());
3002}
3003
3005 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
3006}
3007
3008/*--.. Operations on switch instructions (only) ............................--*/
3009
3011 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
3012}
3013
3014/*--.. Operations on alloca instructions (only) ............................--*/
3015
3017 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
3018}
3019
3020/*--.. Operations on gep instructions (only) ...............................--*/
3021
3023 return unwrap<GEPOperator>(GEP)->isInBounds();
3024}
3025
3027 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
3028}
3029
3031 return wrap(unwrap<GEPOperator>(GEP)->getSourceElementType());
3032}
3033
3034/*--.. Operations on phi nodes .............................................--*/
3035
3036void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
3037 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
3038 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
3039 for (unsigned I = 0; I != Count; ++I)
3040 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
3041}
3042
3044 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
3045}
3046
3048 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
3049}
3050
3052 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
3053}
3054
3055/*--.. Operations on extractvalue and insertvalue nodes ....................--*/
3056
3058 auto *I = unwrap(Inst);
3059 if (auto *GEP = dyn_cast<GEPOperator>(I))
3060 return GEP->getNumIndices();
3061 if (auto *EV = dyn_cast<ExtractValueInst>(I))
3062 return EV->getNumIndices();
3063 if (auto *IV = dyn_cast<InsertValueInst>(I))
3064 return IV->getNumIndices();
3066 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
3067}
3068
3069const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
3070 auto *I = unwrap(Inst);
3071 if (auto *EV = dyn_cast<ExtractValueInst>(I))
3072 return EV->getIndices().data();
3073 if (auto *IV = dyn_cast<InsertValueInst>(I))
3074 return IV->getIndices().data();
3076 "LLVMGetIndices applies only to extractvalue and insertvalue!");
3077}
3078
3079
3080/*===-- Instruction builders ----------------------------------------------===*/
3081
3083 return wrap(new IRBuilder<>(*unwrap(C)));
3084}
3085
3088}
3089
3091 LLVMValueRef Instr) {
3092 BasicBlock *BB = unwrap(Block);
3093 auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
3094 unwrap(Builder)->SetInsertPoint(BB, I);
3095}
3096
3098 Instruction *I = unwrap<Instruction>(Instr);
3099 unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
3100}
3101
3103 BasicBlock *BB = unwrap(Block);
3104 unwrap(Builder)->SetInsertPoint(BB);
3105}
3106
3108 return wrap(unwrap(Builder)->GetInsertBlock());
3109}
3110
3112 unwrap(Builder)->ClearInsertionPoint();
3113}
3114
3116 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
3117}
3118
3120 const char *Name) {
3121 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
3122}
3123
3125 delete unwrap(Builder);
3126}
3127
3128/*--.. Metadata builders ...................................................--*/
3129
3131 return wrap(unwrap(Builder)->getCurrentDebugLocation().getAsMDNode());
3132}
3133
3135 if (Loc)
3136 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(unwrap<MDNode>(Loc)));
3137 else
3138 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc());
3139}
3140
3142 MDNode *Loc =
3143 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
3144 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
3145}
3146
3148 LLVMContext &Context = unwrap(Builder)->getContext();
3150 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
3151}
3152
3154 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
3155}
3156
3158 unwrap(Builder)->AddMetadataToInst(unwrap<Instruction>(Inst));
3159}
3160
3162 LLVMMetadataRef FPMathTag) {
3163
3164 unwrap(Builder)->setDefaultFPMathTag(FPMathTag
3165 ? unwrap<MDNode>(FPMathTag)
3166 : nullptr);
3167}
3168
3170 return wrap(unwrap(Builder)->getDefaultFPMathTag());
3171}
3172
3173/*--.. Instruction builders ................................................--*/
3174
3176 return wrap(unwrap(B)->CreateRetVoid());
3177}
3178
3180 return wrap(unwrap(B)->CreateRet(unwrap(V)));
3181}
3182
3184 unsigned N) {
3185 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
3186}
3187
3189 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
3190}
3191
3194 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
3195}
3196
3198 LLVMBasicBlockRef Else, unsigned NumCases) {
3199 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
3200}
3201
3203 unsigned NumDests) {
3204 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
3205}
3206
3208 LLVMValueRef *Args, unsigned NumArgs,
3210 const char *Name) {
3211 return wrap(unwrap(B)->CreateInvoke(unwrap<FunctionType>(Ty), unwrap(Fn),
3212 unwrap(Then), unwrap(Catch),
3213 ArrayRef(unwrap(Args), NumArgs), Name));
3214}
3215
3217 LLVMValueRef PersFn, unsigned NumClauses,
3218 const char *Name) {
3219 // The personality used to live on the landingpad instruction, but now it
3220 // lives on the parent function. For compatibility, take the provided
3221 // personality and put it on the parent function.
3222 if (PersFn)
3223 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
3224 unwrap<Function>(PersFn));
3225 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
3226}
3227
3229 LLVMValueRef *Args, unsigned NumArgs,
3230 const char *Name) {
3231 return wrap(unwrap(B)->CreateCatchPad(unwrap(ParentPad),
3232 ArrayRef(unwrap(Args), NumArgs), Name));
3233}
3234
3236 LLVMValueRef *Args, unsigned NumArgs,
3237 const char *Name) {
3238 if (ParentPad == nullptr) {
3239 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3240 ParentPad = wrap(Constant::getNullValue(Ty));
3241 }
3242 return wrap(unwrap(B)->CreateCleanupPad(
3243 unwrap(ParentPad), ArrayRef(unwrap(Args), NumArgs), Name));
3244}
3245
3247 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
3248}
3249
3251 LLVMBasicBlockRef UnwindBB,
3252 unsigned NumHandlers, const char *Name) {
3253 if (ParentPad == nullptr) {
3254 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3255 ParentPad = wrap(Constant::getNullValue(Ty));
3256 }
3257 return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(UnwindBB),
3258 NumHandlers, Name));
3259}
3260
3262 LLVMBasicBlockRef BB) {
3263 return wrap(unwrap(B)->CreateCatchRet(unwrap<CatchPadInst>(CatchPad),
3264 unwrap(BB)));
3265}
3266
3268 LLVMBasicBlockRef BB) {
3269 return wrap(unwrap(B)->CreateCleanupRet(unwrap<CleanupPadInst>(CatchPad),
3270 unwrap(BB)));
3271}
3272
3274 return wrap(unwrap(B)->CreateUnreachable());
3275}
3276
3278 LLVMBasicBlockRef Dest) {
3279 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
3280}
3281
3283 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
3284}
3285
3286unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
3287 return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
3288}
3289
3291 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
3292}
3293
3295 unwrap<LandingPadInst>(LandingPad)->addClause(unwrap<Constant>(ClauseVal));
3296}
3297
3299 return unwrap<LandingPadInst>(LandingPad)->isCleanup();
3300}
3301
3302void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
3303 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
3304}
3305
3307 unwrap<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Dest));
3308}
3309
3310unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) {
3311 return unwrap<CatchSwitchInst>(CatchSwitch)->getNumHandlers();
3312}
3313
3314void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) {
3315 CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(CatchSwitch);
3316 for (const BasicBlock *H : CSI->handlers())
3317 *Handlers++ = wrap(H);
3318}
3319
3321 return wrap(unwrap<CatchPadInst>(CatchPad)->getCatchSwitch());
3322}
3323
3325 unwrap<CatchPadInst>(CatchPad)
3326 ->setCatchSwitch(unwrap<CatchSwitchInst>(CatchSwitch));
3327}
3328
3329/*--.. Funclets ...........................................................--*/
3330
3332 return wrap(unwrap<FuncletPadInst>(Funclet)->getArgOperand(i));
3333}
3334
3336 unwrap<FuncletPadInst>(Funclet)->setArgOperand(i, unwrap(value));
3337}
3338
3339/*--.. Arithmetic ..........................................................--*/
3340
3342 const char *Name) {
3343 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
3344}
3345
3347 const char *Name) {
3348 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
3349}
3350
3352 const char *Name) {
3353 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
3354}
3355
3357 const char *Name) {
3358 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
3359}
3360
3362 const char *Name) {
3363 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
3364}
3365
3367 const char *Name) {
3368 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
3369}
3370
3372 const char *Name) {
3373 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
3374}
3375
3377 const char *Name) {
3378 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
3379}
3380
3382 const char *Name) {
3383 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
3384}
3385
3387 const char *Name) {
3388 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
3389}
3390
3392 const char *Name) {
3393 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
3394}
3395
3397 const char *Name) {
3398 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
3399}
3400
3402 const char *Name) {
3403 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
3404}
3405
3407 LLVMValueRef RHS, const char *Name) {
3408 return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
3409}
3410
3412 const char *Name) {
3413 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
3414}
3415
3417 LLVMValueRef RHS, const char *Name) {
3418 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
3419}
3420
3422 const char *Name) {
3423 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
3424}
3425
3427 const char *Name) {
3428 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
3429}
3430
3432 const char *Name) {
3433 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
3434}
3435
3437 const char *Name) {
3438 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
3439}
3440
3442 const char *Name) {
3443 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
3444}
3445
3447 const char *Name) {
3448 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
3449}
3450
3452 const char *Name) {
3453 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
3454}
3455
3457 const char *Name) {
3458 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
3459}
3460
3462 const char *Name) {
3463 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
3464}
3465
3467 const char *Name) {
3468 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
3469}
3470
3472 LLVMValueRef LHS, LLVMValueRef RHS,
3473 const char *Name) {
3475 unwrap(RHS), Name));
3476}
3477
3479 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
3480}
3481
3483 const char *Name) {
3484 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
3485}
3486
3488 const char *Name) {
3489 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
3490}
3491
3493 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
3494}
3495
3497 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
3498}
3499
3501 Value *P = unwrap<Value>(ArithInst);
3502 return cast<Instruction>(P)->hasNoUnsignedWrap();
3503}
3504
3505void LLVMSetNUW(LLVMValueRef ArithInst, LLVMBool HasNUW) {
3506 Value *P = unwrap<Value>(ArithInst);
3507 cast<Instruction>(P)->setHasNoUnsignedWrap(HasNUW);
3508}
3509
3511 Value *P = unwrap<Value>(ArithInst);
3512 return cast<Instruction>(P)->hasNoSignedWrap();
3513}
3514
3515void LLVMSetNSW(LLVMValueRef ArithInst, LLVMBool HasNSW) {
3516 Value *P = unwrap<Value>(ArithInst);
3517 cast<Instruction>(P)->setHasNoSignedWrap(HasNSW);
3518}
3519
3521 Value *P = unwrap<Value>(DivOrShrInst);
3522 return cast<Instruction>(P)->isExact();
3523}
3524
3525void LLVMSetExact(LLVMValueRef DivOrShrInst, LLVMBool IsExact) {
3526 Value *P = unwrap<Value>(DivOrShrInst);
3527 cast<Instruction>(P)->setIsExact(IsExact);
3528}
3529
3530/*--.. Memory ..............................................................--*/
3531
3533 const char *Name) {
3534 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3535 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3536 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3537 return wrap(unwrap(B)->CreateMalloc(ITy, unwrap(Ty), AllocSize, nullptr,
3538 nullptr, Name));
3539}
3540
3542 LLVMValueRef Val, const char *Name) {
3543 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3544 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3545 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3546 return wrap(unwrap(B)->CreateMalloc(ITy, unwrap(Ty), AllocSize, unwrap(Val),
3547 nullptr, Name));
3548}
3549
3551 LLVMValueRef Val, LLVMValueRef Len,
3552 unsigned Align) {
3553 return wrap(unwrap(B)->CreateMemSet(unwrap(Ptr), unwrap(Val), unwrap(Len),
3554 MaybeAlign(Align)));
3555}
3556
3558 LLVMValueRef Dst, unsigned DstAlign,
3559 LLVMValueRef Src, unsigned SrcAlign,
3561 return wrap(unwrap(B)->CreateMemCpy(unwrap(Dst), MaybeAlign(DstAlign),
3562 unwrap(Src), MaybeAlign(SrcAlign),
3563 unwrap(Size)));
3564}
3565
3567 LLVMValueRef Dst, unsigned DstAlign,
3568 LLVMValueRef Src, unsigned SrcAlign,
3570 return wrap(unwrap(B)->CreateMemMove(unwrap(Dst), MaybeAlign(DstAlign),
3571 unwrap(Src), MaybeAlign(SrcAlign),
3572 unwrap(Size)));
3573}
3574
3576 const char *Name) {
3577 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
3578}
3579
3581 LLVMValueRef Val, const char *Name) {
3582 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
3583}
3584
3586 return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
3587}
3588
3590 LLVMValueRef PointerVal, const char *Name) {
3591 return wrap(unwrap(B)->CreateLoad(unwrap(Ty), unwrap(PointerVal), Name));
3592}
3593
3595 LLVMValueRef PointerVal) {
3596 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
3597}
3598
3600 switch (Ordering) {
3601 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic;
3602 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered;
3603 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic;
3604 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire;
3605 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release;
3607 return AtomicOrdering::AcquireRelease;
3609 return AtomicOrdering::SequentiallyConsistent;
3610 }
3611
3612 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
3613}
3614
3616 switch (Ordering) {
3617 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic;
3618 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered;
3619 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic;
3620 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire;
3621 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease;
3622 case AtomicOrdering::AcquireRelease:
3624 case AtomicOrdering::SequentiallyConsistent:
3626 }
3627
3628 llvm_unreachable("Invalid AtomicOrdering value!");
3629}
3630
3632 switch (BinOp) {
3648 }
3649
3650 llvm_unreachable("Invalid LLVMAtomicRMWBinOp value!");
3651}
3652
3654 switch (BinOp) {
3670 default: break;
3671 }
3672
3673 llvm_unreachable("Invalid AtomicRMWBinOp value!");
3674}
3675
3676// TODO: Should this and other atomic instructions support building with
3677// "syncscope"?
3679 LLVMBool isSingleThread, const char *Name) {
3680 return wrap(
3681 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
3682 isSingleThread ? SyncScope::SingleThread
3684 Name));
3685}
3686
3688 LLVMValueRef Pointer, LLVMValueRef *Indices,
3689 unsigned NumIndices, const char *Name) {
3690 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3691 return wrap(unwrap(B)->CreateGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3692}
3693
3695 LLVMValueRef Pointer, LLVMValueRef *Indices,
3696 unsigned NumIndices, const char *Name) {
3697 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3698 return wrap(
3699 unwrap(B)->CreateInBoundsGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3700}
3701
3703 LLVMValueRef Pointer, unsigned