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 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1717 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1718 NumIndices);
1719 Constant *Val = unwrap<Constant>(ConstantVal);
1720 return wrap(ConstantExpr::getGetElementPtr(unwrap(Ty), Val, IdxList));
1721}
1722
1724 LLVMValueRef *ConstantIndices,
1725 unsigned NumIndices) {
1726 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1727 NumIndices);
1728 Constant *Val = unwrap<Constant>(ConstantVal);
1729 return wrap(ConstantExpr::getInBoundsGetElementPtr(unwrap(Ty), Val, IdxList));
1730}
1731
1733 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1734 unwrap(ToType)));
1735}
1736
1738 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1739 unwrap(ToType)));
1740}
1741
1743 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1744 unwrap(ToType)));
1745}
1746
1748 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1749 unwrap(ToType)));
1750}
1751
1753 LLVMTypeRef ToType) {
1754 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1755 unwrap(ToType)));
1756}
1757
1759 LLVMTypeRef ToType) {
1760 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1761 unwrap(ToType)));
1762}
1763
1765 LLVMTypeRef ToType) {
1766 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1767 unwrap(ToType)));
1768}
1769
1771 LLVMValueRef IndexConstant) {
1772 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1773 unwrap<Constant>(IndexConstant)));
1774}
1775
1777 LLVMValueRef ElementValueConstant,
1778 LLVMValueRef IndexConstant) {
1779 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1780 unwrap<Constant>(ElementValueConstant),
1781 unwrap<Constant>(IndexConstant)));
1782}
1783
1785 LLVMValueRef VectorBConstant,
1786 LLVMValueRef MaskConstant) {
1787 SmallVector<int, 16> IntMask;
1788 ShuffleVectorInst::getShuffleMask(unwrap<Constant>(MaskConstant), IntMask);
1789 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1790 unwrap<Constant>(VectorBConstant),
1791 IntMask));
1792}
1793
1795 const char *Constraints,
1796 LLVMBool HasSideEffects,
1797 LLVMBool IsAlignStack) {
1798 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1799 Constraints, HasSideEffects, IsAlignStack));
1800}
1801
1803 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1804}
1805
1806/*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1807
1809 return wrap(unwrap<GlobalValue>(Global)->getParent());
1810}
1811
1813 return unwrap<GlobalValue>(Global)->isDeclaration();
1814}
1815
1817 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1819 return LLVMExternalLinkage;
1827 return LLVMWeakAnyLinkage;
1829 return LLVMWeakODRLinkage;
1831 return LLVMAppendingLinkage;
1833 return LLVMInternalLinkage;
1835 return LLVMPrivateLinkage;
1839 return LLVMCommonLinkage;
1840 }
1841
1842 llvm_unreachable("Invalid GlobalValue linkage!");
1843}
1844
1846 GlobalValue *GV = unwrap<GlobalValue>(Global);
1847
1848 switch (Linkage) {
1851 break;
1854 break;
1857 break;
1860 break;
1862 LLVM_DEBUG(
1863 errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1864 "longer supported.");
1865 break;
1866 case LLVMWeakAnyLinkage:
1868 break;
1869 case LLVMWeakODRLinkage:
1871 break;
1874 break;
1877 break;
1878 case LLVMPrivateLinkage:
1880 break;
1883 break;
1886 break;
1888 LLVM_DEBUG(
1889 errs()
1890 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1891 break;
1893 LLVM_DEBUG(
1894 errs()
1895 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1896 break;
1899 break;
1900 case LLVMGhostLinkage:
1901 LLVM_DEBUG(
1902 errs() << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1903 break;
1904 case LLVMCommonLinkage:
1906 break;
1907 }
1908}
1909
1911 // Using .data() is safe because of how GlobalObject::setSection is
1912 // implemented.
1913 return unwrap<GlobalValue>(Global)->getSection().data();
1914}
1915
1916void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1917 unwrap<GlobalObject>(Global)->setSection(Section);
1918}
1919
1921 return static_cast<LLVMVisibility>(
1922 unwrap<GlobalValue>(Global)->getVisibility());
1923}
1924
1926 unwrap<GlobalValue>(Global)
1927 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1928}
1929
1931 return static_cast<LLVMDLLStorageClass>(
1932 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1933}
1934
1936 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1937 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1938}
1939
1941 switch (unwrap<GlobalValue>(Global)->getUnnamedAddr()) {
1942 case GlobalVariable::UnnamedAddr::None:
1943 return LLVMNoUnnamedAddr;
1944 case GlobalVariable::UnnamedAddr::Local:
1945 return LLVMLocalUnnamedAddr;
1946 case GlobalVariable::UnnamedAddr::Global:
1947 return LLVMGlobalUnnamedAddr;
1948 }
1949 llvm_unreachable("Unknown UnnamedAddr kind!");
1950}
1951
1953 GlobalValue *GV = unwrap<GlobalValue>(Global);
1954
1955 switch (UnnamedAddr) {
1956 case LLVMNoUnnamedAddr:
1957 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::None);
1959 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Local);
1961 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Global);
1962 }
1963}
1964
1966 return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
1967}
1968
1970 unwrap<GlobalValue>(Global)->setUnnamedAddr(
1971 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
1972 : GlobalValue::UnnamedAddr::None);
1973}
1974
1976 return wrap(unwrap<GlobalValue>(Global)->getValueType());
1977}
1978
1979/*--.. Operations on global variables, load and store instructions .........--*/
1980
1982 Value *P = unwrap(V);
1983 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
1984 return GV->getAlign() ? GV->getAlign()->value() : 0;
1985 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1986 return AI->getAlign().value();
1987 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1988 return LI->getAlign().value();
1989 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1990 return SI->getAlign().value();
1991 if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
1992 return RMWI->getAlign().value();
1993 if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
1994 return CXI->getAlign().value();
1995
1997 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, "
1998 "and AtomicCmpXchgInst have alignment");
1999}
2000
2001void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
2002 Value *P = unwrap(V);
2003 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2004 GV->setAlignment(MaybeAlign(Bytes));
2005 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
2006 AI->setAlignment(Align(Bytes));
2007 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
2008 LI->setAlignment(Align(Bytes));
2009 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
2010 SI->setAlignment(Align(Bytes));
2011 else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2012 RMWI->setAlignment(Align(Bytes));
2013 else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2014 CXI->setAlignment(Align(Bytes));
2015 else
2017 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, and "
2018 "and AtomicCmpXchgInst have alignment");
2019}
2020
2022 size_t *NumEntries) {
2023 return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
2024 Entries.clear();
2025 if (Instruction *Instr = dyn_cast<Instruction>(unwrap(Value))) {
2026 Instr->getAllMetadata(Entries);
2027 } else {
2028 unwrap<GlobalObject>(Value)->getAllMetadata(Entries);
2029 }
2030 });
2031}
2032
2034 unsigned Index) {
2036 static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2037 return MVE.Kind;
2038}
2039
2042 unsigned Index) {
2044 static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2045 return MVE.Metadata;
2046}
2047
2049 free(Entries);
2050}
2051
2053 LLVMMetadataRef MD) {
2054 unwrap<GlobalObject>(Global)->setMetadata(Kind, unwrap<MDNode>(MD));
2055}
2056
2058 unwrap<GlobalObject>(Global)->eraseMetadata(Kind);
2059}
2060
2062 unwrap<GlobalObject>(Global)->clearMetadata();
2063}
2064
2065/*--.. Operations on global variables ......................................--*/
2066
2068 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2070}
2071
2073 const char *Name,
2074 unsigned AddressSpace) {
2075 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2077 nullptr, GlobalVariable::NotThreadLocal,
2078 AddressSpace));
2079}
2080
2082 return wrap(unwrap(M)->getNamedGlobal(Name));
2083}
2084
2086 Module *Mod = unwrap(M);
2088 if (I == Mod->global_end())
2089 return nullptr;
2090 return wrap(&*I);
2091}
2092
2094 Module *Mod = unwrap(M);
2096 if (I == Mod->global_begin())
2097 return nullptr;
2098 return wrap(&*--I);
2099}
2100
2102 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2104 if (++I == GV->getParent()->global_end())
2105 return nullptr;
2106 return wrap(&*I);
2107}
2108
2110 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2112 if (I == GV->getParent()->global_begin())
2113 return nullptr;
2114 return wrap(&*--I);
2115}
2116
2118 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
2119}
2120
2122 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
2123 if ( !GV->hasInitializer() )
2124 return nullptr;
2125 return wrap(GV->getInitializer());
2126}
2127
2128void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
2129 unwrap<GlobalVariable>(GlobalVar)
2130 ->setInitializer(unwrap<Constant>(ConstantVal));
2131}
2132
2134 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
2135}
2136
2137void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
2138 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
2139}
2140
2142 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
2143}
2144
2145void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
2146 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
2147}
2148
2150 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
2151 case GlobalVariable::NotThreadLocal:
2152 return LLVMNotThreadLocal;
2153 case GlobalVariable::GeneralDynamicTLSModel:
2155 case GlobalVariable::LocalDynamicTLSModel:
2157 case GlobalVariable::InitialExecTLSModel:
2159 case GlobalVariable::LocalExecTLSModel:
2160 return LLVMLocalExecTLSModel;
2161 }
2162
2163 llvm_unreachable("Invalid GlobalVariable thread local mode");
2164}
2165
2167 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2168
2169 switch (Mode) {
2170 case LLVMNotThreadLocal:
2171 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
2172 break;
2174 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
2175 break;
2177 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
2178 break;
2180 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
2181 break;
2183 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
2184 break;
2185 }
2186}
2187
2189 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
2190}
2191
2193 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
2194}
2195
2196/*--.. Operations on aliases ......................................--*/
2197
2199 unsigned AddrSpace, LLVMValueRef Aliasee,
2200 const char *Name) {
2201 return wrap(GlobalAlias::create(unwrap(ValueTy), AddrSpace,
2203 unwrap<Constant>(Aliasee), unwrap(M)));
2204}
2205
2207 const char *Name, size_t NameLen) {
2208 return wrap(unwrap(M)->getNamedAlias(StringRef(Name, NameLen)));
2209}
2210
2212 Module *Mod = unwrap(M);
2214 if (I == Mod->alias_end())
2215 return nullptr;
2216 return wrap(&*I);
2217}
2218
2220 Module *Mod = unwrap(M);
2222 if (I == Mod->alias_begin())
2223 return nullptr;
2224 return wrap(&*--I);
2225}
2226
2228 GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2230 if (++I == Alias->getParent()->alias_end())
2231 return nullptr;
2232 return wrap(&*I);
2233}
2234
2236 GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2238 if (I == Alias->getParent()->alias_begin())
2239 return nullptr;
2240 return wrap(&*--I);
2241}
2242
2244 return wrap(unwrap<GlobalAlias>(Alias)->getAliasee());
2245}
2246
2248 unwrap<GlobalAlias>(Alias)->setAliasee(unwrap<Constant>(Aliasee));
2249}
2250
2251/*--.. Operations on functions .............................................--*/
2252
2254 LLVMTypeRef FunctionTy) {
2255 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
2257}
2258
2260 return wrap(unwrap(M)->getFunction(Name));
2261}
2262
2264 Module *Mod = unwrap(M);
2266 if (I == Mod->end())
2267 return nullptr;
2268 return wrap(&*I);
2269}
2270
2272 Module *Mod = unwrap(M);
2274 if (I == Mod->begin())
2275 return nullptr;
2276 return wrap(&*--I);
2277}
2278
2280 Function *Func = unwrap<Function>(Fn);
2281 Module::iterator I(Func);
2282 if (++I == Func->getParent()->end())
2283 return nullptr;
2284 return wrap(&*I);
2285}
2286
2288 Function *Func = unwrap<Function>(Fn);
2289 Module::iterator I(Func);
2290 if (I == Func->getParent()->begin())
2291 return nullptr;
2292 return wrap(&*--I);
2293}
2294
2296 unwrap<Function>(Fn)->eraseFromParent();
2297}
2298
2300 return unwrap<Function>(Fn)->hasPersonalityFn();
2301}
2302
2304 return wrap(unwrap<Function>(Fn)->getPersonalityFn());
2305}
2306
2308 unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
2309}
2310
2312 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
2313 return F->getIntrinsicID();
2314 return 0;
2315}
2316
2318 assert(ID < llvm::Intrinsic::num_intrinsics && "Intrinsic ID out of range");
2319 return llvm::Intrinsic::ID(ID);
2320}
2321
2323 unsigned ID,
2324 LLVMTypeRef *ParamTypes,
2325 size_t ParamCount) {
2326 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2327 auto IID = llvm_map_to_intrinsic_id(ID);
2328 return wrap(llvm::Intrinsic::getDeclaration(unwrap(Mod), IID, Tys));
2329}
2330
2331const char *LLVMIntrinsicGetName(unsigned ID, size_t *NameLength) {
2332 auto IID = llvm_map_to_intrinsic_id(ID);
2333 auto Str = llvm::Intrinsic::getName(IID);
2334 *NameLength = Str.size();
2335 return Str.data();
2336}
2337
2339 LLVMTypeRef *ParamTypes, size_t ParamCount) {
2340 auto IID = llvm_map_to_intrinsic_id(ID);
2341 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2342 return wrap(llvm::Intrinsic::getType(*unwrap(Ctx), IID, Tys));
2343}
2344
2346 LLVMTypeRef *ParamTypes,
2347 size_t ParamCount,
2348 size_t *NameLength) {
2349 auto IID = llvm_map_to_intrinsic_id(ID);
2350 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2351 auto Str = llvm::Intrinsic::getNameNoUnnamedTypes(IID, Tys);
2352 *NameLength = Str.length();
2353 return strdup(Str.c_str());
2354}
2355
2357 LLVMTypeRef *ParamTypes,
2358 size_t ParamCount,
2359 size_t *NameLength) {
2360 auto IID = llvm_map_to_intrinsic_id(ID);
2361 ArrayRef<Type *> Tys(unwrap(ParamTypes), ParamCount);
2362 auto Str = llvm::Intrinsic::getName(IID, Tys, unwrap(Mod));
2363 *NameLength = Str.length();
2364 return strdup(Str.c_str());
2365}
2366
2367unsigned LLVMLookupIntrinsicID(const char *Name, size_t NameLen) {
2368 return Function::lookupIntrinsicID({Name, NameLen});
2369}
2370
2372 auto IID = llvm_map_to_intrinsic_id(ID);
2374}
2375
2377 return unwrap<Function>(Fn)->getCallingConv();
2378}
2379
2381 return unwrap<Function>(Fn)->setCallingConv(
2382 static_cast<CallingConv::ID>(CC));
2383}
2384
2385const char *LLVMGetGC(LLVMValueRef Fn) {
2386 Function *F = unwrap<Function>(Fn);
2387 return F->hasGC()? F->getGC().c_str() : nullptr;
2388}
2389
2390void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
2391 Function *F = unwrap<Function>(Fn);
2392 if (GC)
2393 F->setGC(GC);
2394 else
2395 F->clearGC();
2396}
2397
2400 unwrap<Function>(F)->addAttributeAtIndex(Idx, unwrap(A));
2401}
2402
2404 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2405 return AS.getNumAttributes();
2406}
2407
2409 LLVMAttributeRef *Attrs) {
2410 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2411 for (auto A : AS)
2412 *Attrs++ = wrap(A);
2413}
2414
2417 unsigned KindID) {
2418 return wrap(unwrap<Function>(F)->getAttributeAtIndex(
2419 Idx, (Attribute::AttrKind)KindID));
2420}
2421
2424 const char *K, unsigned KLen) {
2425 return wrap(
2426 unwrap<Function>(F)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
2427}
2428
2430 unsigned KindID) {
2431 unwrap<Function>(F)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
2432}
2433
2435 const char *K, unsigned KLen) {
2436 unwrap<Function>(F)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
2437}
2438
2440 const char *V) {
2441 Function *Func = unwrap<Function>(Fn);
2442 Attribute Attr = Attribute::get(Func->getContext(), A, V);
2443 Func->addFnAttr(Attr);
2444}
2445
2446/*--.. Operations on parameters ............................................--*/
2447
2449 // This function is strictly redundant to
2450 // LLVMCountParamTypes(LLVMGlobalGetValueType(FnRef))
2451 return unwrap<Function>(FnRef)->arg_size();
2452}
2453
2454void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
2455 Function *Fn = unwrap<Function>(FnRef);
2456 for (Argument &A : Fn->args())
2457 *ParamRefs++ = wrap(&A);
2458}
2459
2461 Function *Fn = unwrap<Function>(FnRef);
2462 return wrap(&Fn->arg_begin()[index]);
2463}
2464
2466 return wrap(unwrap<Argument>(V)->getParent());
2467}
2468
2470 Function *Func = unwrap<Function>(Fn);
2471 Function::arg_iterator I = Func->arg_begin();
2472 if (I == Func->arg_end())
2473 return nullptr;
2474 return wrap(&*I);
2475}
2476
2478 Function *Func = unwrap<Function>(Fn);
2479 Function::arg_iterator I = Func->arg_end();
2480 if (I == Func->arg_begin())
2481 return nullptr;
2482 return wrap(&*--I);
2483}
2484
2486 Argument *A = unwrap<Argument>(Arg);
2487 Function *Fn = A->getParent();
2488 if (A->getArgNo() + 1 >= Fn->arg_size())
2489 return nullptr;
2490 return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
2491}
2492
2494 Argument *A = unwrap<Argument>(Arg);
2495 if (A->getArgNo() == 0)
2496 return nullptr;
2497 return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
2498}
2499
2500void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
2501 Argument *A = unwrap<Argument>(Arg);
2502 A->addAttr(Attribute::getWithAlignment(A->getContext(), Align(align)));
2503}
2504
2505/*--.. Operations on ifuncs ................................................--*/
2506
2508 const char *Name, size_t NameLen,
2509 LLVMTypeRef Ty, unsigned AddrSpace,
2511 return wrap(GlobalIFunc::create(unwrap(Ty), AddrSpace,
2513 StringRef(Name, NameLen),
2514 unwrap<Constant>(Resolver), unwrap(M)));
2515}
2516
2518 const char *Name, size_t NameLen) {
2519 return wrap(unwrap(M)->getNamedIFunc(StringRef(Name, NameLen)));
2520}
2521
2523 Module *Mod = unwrap(M);
2525 if (I == Mod->ifunc_end())
2526 return nullptr;
2527 return wrap(&*I);
2528}
2529
2531 Module *Mod = unwrap(M);
2533 if (I == Mod->ifunc_begin())
2534 return nullptr;
2535 return wrap(&*--I);
2536}
2537
2539 GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2541 if (++I == GIF->getParent()->ifunc_end())
2542 return nullptr;
2543 return wrap(&*I);
2544}
2545
2547 GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2549 if (I == GIF->getParent()->ifunc_begin())
2550 return nullptr;
2551 return wrap(&*--I);
2552}
2553
2555 return wrap(unwrap<GlobalIFunc>(IFunc)->getResolver());
2556}
2557
2559 unwrap<GlobalIFunc>(IFunc)->setResolver(unwrap<Constant>(Resolver));
2560}
2561
2563 unwrap<GlobalIFunc>(IFunc)->eraseFromParent();
2564}
2565
2567 unwrap<GlobalIFunc>(IFunc)->removeFromParent();
2568}
2569
2570/*--.. Operations on basic blocks ..........................................--*/
2571
2573 return wrap(static_cast<Value*>(unwrap(BB)));
2574}
2575
2577 return isa<BasicBlock>(unwrap(Val));
2578}
2579
2581 return wrap(unwrap<BasicBlock>(Val));
2582}
2583
2585 return unwrap(BB)->getName().data();
2586}
2587
2589 return wrap(unwrap(BB)->getParent());
2590}
2591
2593 return wrap(unwrap(BB)->getTerminator());
2594}
2595
2597 return unwrap<Function>(FnRef)->size();
2598}
2599
2601 Function *Fn = unwrap<Function>(FnRef);
2602 for (BasicBlock &BB : *Fn)
2603 *BasicBlocksRefs++ = wrap(&BB);
2604}
2605
2607 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2608}
2609
2611 Function *Func = unwrap<Function>(Fn);
2612 Function::iterator I = Func->begin();
2613 if (I == Func->end())
2614 return nullptr;
2615 return wrap(&*I);
2616}
2617
2619 Function *Func = unwrap<Function>(Fn);
2620 Function::iterator I = Func->end();
2621 if (I == Func->begin())
2622 return nullptr;
2623 return wrap(&*--I);
2624}
2625
2627 BasicBlock *Block = unwrap(BB);
2629 if (++I == Block->getParent()->end())
2630 return nullptr;
2631 return wrap(&*I);
2632}
2633
2635 BasicBlock *Block = unwrap(BB);
2637 if (I == Block->getParent()->begin())
2638 return nullptr;
2639 return wrap(&*--I);
2640}
2641
2643 const char *Name) {
2645}
2646
2648 LLVMBasicBlockRef BB) {
2649 BasicBlock *ToInsert = unwrap(BB);
2650 BasicBlock *CurBB = unwrap(Builder)->GetInsertBlock();
2651 assert(CurBB && "current insertion point is invalid!");
2652 CurBB->getParent()->insert(std::next(CurBB->getIterator()), ToInsert);
2653}
2654
2656 LLVMBasicBlockRef BB) {
2657 unwrap<Function>(Fn)->insert(unwrap<Function>(Fn)->end(), unwrap(BB));
2658}
2659
2661 LLVMValueRef FnRef,
2662 const char *Name) {
2663 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
2664}
2665
2668}
2669
2671 LLVMBasicBlockRef BBRef,
2672 const char *Name) {
2673 BasicBlock *BB = unwrap(BBRef);
2674 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2675}
2676
2678 const char *Name) {
2680}
2681
2683 unwrap(BBRef)->eraseFromParent();
2684}
2685
2687 unwrap(BBRef)->removeFromParent();
2688}
2689
2691 unwrap(BB)->moveBefore(unwrap(MovePos));
2692}
2693
2695 unwrap(BB)->moveAfter(unwrap(MovePos));
2696}
2697
2698/*--.. Operations on instructions ..........................................--*/
2699
2701 return wrap(unwrap<Instruction>(Inst)->getParent());
2702}
2703
2705 BasicBlock *Block = unwrap(BB);
2706 BasicBlock::iterator I = Block->begin();
2707 if (I == Block->end())
2708 return nullptr;
2709 return wrap(&*I);
2710}
2711
2713 BasicBlock *Block = unwrap(BB);
2714 BasicBlock::iterator I = Block->end();
2715 if (I == Block->begin())
2716 return nullptr;
2717 return wrap(&*--I);
2718}
2719
2721 Instruction *Instr = unwrap<Instruction>(Inst);
2722 BasicBlock::iterator I(Instr);
2723 if (++I == Instr->getParent()->end())
2724 return nullptr;
2725 return wrap(&*I);
2726}
2727
2729 Instruction *Instr = unwrap<Instruction>(Inst);
2730 BasicBlock::iterator I(Instr);
2731 if (I == Instr->getParent()->begin())
2732 return nullptr;
2733 return wrap(&*--I);
2734}
2735
2737 unwrap<Instruction>(Inst)->removeFromParent();
2738}
2739
2741 unwrap<Instruction>(Inst)->eraseFromParent();
2742}
2743
2745 unwrap<Instruction>(Inst)->deleteValue();
2746}
2747
2749 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2750 return (LLVMIntPredicate)I->getPredicate();
2751 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2752 if (CE->getOpcode() == Instruction::ICmp)
2753 return (LLVMIntPredicate)CE->getPredicate();
2754 return (LLVMIntPredicate)0;
2755}
2756
2758 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2759 return (LLVMRealPredicate)I->getPredicate();
2760 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2761 if (CE->getOpcode() == Instruction::FCmp)
2762 return (LLVMRealPredicate)CE->getPredicate();
2763 return (LLVMRealPredicate)0;
2764}
2765
2767 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2768 return map_to_llvmopcode(C->getOpcode());
2769 return (LLVMOpcode)0;
2770}
2771
2773 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2774 return wrap(C->clone());
2775 return nullptr;
2776}
2777
2779 Instruction *I = dyn_cast<Instruction>(unwrap(Inst));
2780 return (I && I->isTerminator()) ? wrap(I) : nullptr;
2781}
2782
2784 if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) {
2785 return FPI->arg_size();
2786 }
2787 return unwrap<CallBase>(Instr)->arg_size();
2788}
2789
2790/*--.. Call and invoke instructions ........................................--*/
2791
2793 return unwrap<CallBase>(Instr)->getCallingConv();
2794}
2795
2797 return unwrap<CallBase>(Instr)->setCallingConv(
2798 static_cast<CallingConv::ID>(CC));
2799}
2800
2802 unsigned align) {
2803 auto *Call = unwrap<CallBase>(Instr);
2804 Attribute AlignAttr =
2805 Attribute::getWithAlignment(Call->getContext(), Align(align));
2806 Call->addAttributeAtIndex(Idx, AlignAttr);
2807}
2808
2811 unwrap<CallBase>(C)->addAttributeAtIndex(Idx, unwrap(A));
2812}
2813
2816 auto *Call = unwrap<CallBase>(C);
2817 auto AS = Call->getAttributes().getAttributes(Idx);
2818 return AS.getNumAttributes();
2819}
2820
2822 LLVMAttributeRef *Attrs) {
2823 auto *Call = unwrap<CallBase>(C);
2824 auto AS = Call->getAttributes().getAttributes(Idx);
2825 for (auto A : AS)
2826 *Attrs++ = wrap(A);
2827}
2828
2831 unsigned KindID) {
2832 return wrap(unwrap<CallBase>(C)->getAttributeAtIndex(
2833 Idx, (Attribute::AttrKind)KindID));
2834}
2835
2838 const char *K, unsigned KLen) {
2839 return wrap(
2840 unwrap<CallBase>(C)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
2841}
2842
2844 unsigned KindID) {
2845 unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
2846}
2847
2849 const char *K, unsigned KLen) {
2850 unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
2851}
2852
2854 return wrap(unwrap<CallBase>(Instr)->getCalledOperand());
2855}
2856
2858 return wrap(unwrap<CallBase>(Instr)->getFunctionType());
2859}
2860
2861/*--.. Operations on call instructions (only) ..............................--*/
2862
2864 return unwrap<CallInst>(Call)->isTailCall();
2865}
2866
2867void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
2868 unwrap<CallInst>(Call)->setTailCall(isTailCall);
2869}
2870
2872 return (LLVMTailCallKind)unwrap<CallInst>(Call)->getTailCallKind();
2873}
2874
2876 unwrap<CallInst>(Call)->setTailCallKind((CallInst::TailCallKind)kind);
2877}
2878
2879/*--.. Operations on invoke instructions (only) ............................--*/
2880
2882 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2883}
2884
2886 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2887 return wrap(CRI->getUnwindDest());
2888 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2889 return wrap(CSI->getUnwindDest());
2890 }
2891 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2892}
2893
2895 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2896}
2897
2899 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2900 return CRI->setUnwindDest(unwrap(B));
2901 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2902 return CSI->setUnwindDest(unwrap(B));
2903 }
2904 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2905}
2906
2907/*--.. Operations on terminators ...........................................--*/
2908
2910 return unwrap<Instruction>(Term)->getNumSuccessors();
2911}
2912
2914 return wrap(unwrap<Instruction>(Term)->getSuccessor(i));
2915}
2916
2918 return unwrap<Instruction>(Term)->setSuccessor(i, unwrap(block));
2919}
2920
2921/*--.. Operations on branch instructions (only) ............................--*/
2922
2924 return unwrap<BranchInst>(Branch)->isConditional();
2925}
2926
2928 return wrap(unwrap<BranchInst>(Branch)->getCondition());
2929}
2930
2932 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2933}
2934
2935/*--.. Operations on switch instructions (only) ............................--*/
2936
2938 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2939}
2940
2941/*--.. Operations on alloca instructions (only) ............................--*/
2942
2944 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
2945}
2946
2947/*--.. Operations on gep instructions (only) ...............................--*/
2948
2950 return unwrap<GEPOperator>(GEP)->isInBounds();
2951}
2952
2954 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
2955}
2956
2958 return wrap(unwrap<GEPOperator>(GEP)->getSourceElementType());
2959}
2960
2961/*--.. Operations on phi nodes .............................................--*/
2962
2963void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
2964 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
2965 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
2966 for (unsigned I = 0; I != Count; ++I)
2967 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
2968}
2969
2971 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
2972}
2973
2975 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
2976}
2977
2979 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
2980}
2981
2982/*--.. Operations on extractvalue and insertvalue nodes ....................--*/
2983
2985 auto *I = unwrap(Inst);
2986 if (auto *GEP = dyn_cast<GEPOperator>(I))
2987 return GEP->getNumIndices();
2988 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2989 return EV->getNumIndices();
2990 if (auto *IV = dyn_cast<InsertValueInst>(I))
2991 return IV->getNumIndices();
2993 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
2994}
2995
2996const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
2997 auto *I = unwrap(Inst);
2998 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2999 return EV->getIndices().data();
3000 if (auto *IV = dyn_cast<InsertValueInst>(I))
3001 return IV->getIndices().data();
3003 "LLVMGetIndices applies only to extractvalue and insertvalue!");
3004}
3005
3006
3007/*===-- Instruction builders ----------------------------------------------===*/
3008
3010 return wrap(new IRBuilder<>(*unwrap(C)));
3011}
3012
3015}
3016
3018 LLVMValueRef Instr) {
3019 BasicBlock *BB = unwrap(Block);
3020 auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
3021 unwrap(Builder)->SetInsertPoint(BB, I);
3022}
3023
3025 Instruction *I = unwrap<Instruction>(Instr);
3026 unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
3027}
3028
3030 BasicBlock *BB = unwrap(Block);
3031 unwrap(Builder)->SetInsertPoint(BB);
3032}
3033
3035 return wrap(unwrap(Builder)->GetInsertBlock());
3036}
3037
3039 unwrap(Builder)->ClearInsertionPoint();
3040}
3041
3043 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
3044}
3045
3047 const char *Name) {
3048 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
3049}
3050
3052 delete unwrap(Builder);
3053}
3054
3055/*--.. Metadata builders ...................................................--*/
3056
3058 return wrap(unwrap(Builder)->getCurrentDebugLocation().getAsMDNode());
3059}
3060
3062 if (Loc)
3063 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(unwrap<MDNode>(Loc)));
3064 else
3065 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc());
3066}
3067
3069 MDNode *Loc =
3070 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
3071 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
3072}
3073
3075 LLVMContext &Context = unwrap(Builder)->getContext();
3077 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
3078}
3079
3081 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
3082}
3083
3085 unwrap(Builder)->AddMetadataToInst(unwrap<Instruction>(Inst));
3086}
3087
3089 LLVMMetadataRef FPMathTag) {
3090
3091 unwrap(Builder)->setDefaultFPMathTag(FPMathTag
3092 ? unwrap<MDNode>(FPMathTag)
3093 : nullptr);
3094}
3095
3097 return wrap(unwrap(Builder)->getDefaultFPMathTag());
3098}
3099
3100/*--.. Instruction builders ................................................--*/
3101
3103 return wrap(unwrap(B)->CreateRetVoid());
3104}
3105
3107 return wrap(unwrap(B)->CreateRet(unwrap(V)));
3108}
3109
3111 unsigned N) {
3112 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
3113}
3114
3116 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
3117}
3118
3121 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
3122}
3123
3125 LLVMBasicBlockRef Else, unsigned NumCases) {
3126 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
3127}
3128
3130 unsigned NumDests) {
3131 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
3132}
3133
3135 LLVMValueRef *Args, unsigned NumArgs,
3137 const char *Name) {
3138 return wrap(unwrap(B)->CreateInvoke(unwrap<FunctionType>(Ty), unwrap(Fn),
3139 unwrap(Then), unwrap(Catch),
3140 ArrayRef(unwrap(Args), NumArgs), Name));
3141}
3142
3144 LLVMValueRef PersFn, unsigned NumClauses,
3145 const char *Name) {
3146 // The personality used to live on the landingpad instruction, but now it
3147 // lives on the parent function. For compatibility, take the provided
3148 // personality and put it on the parent function.
3149 if (PersFn)
3150 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
3151 unwrap<Function>(PersFn));
3152 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
3153}
3154
3156 LLVMValueRef *Args, unsigned NumArgs,
3157 const char *Name) {
3158 return wrap(unwrap(B)->CreateCatchPad(unwrap(ParentPad),
3159 ArrayRef(unwrap(Args), NumArgs), Name));
3160}
3161
3163 LLVMValueRef *Args, unsigned NumArgs,
3164 const char *Name) {
3165 if (ParentPad == nullptr) {
3166 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3167 ParentPad = wrap(Constant::getNullValue(Ty));
3168 }
3169 return wrap(unwrap(B)->CreateCleanupPad(
3170 unwrap(ParentPad), ArrayRef(unwrap(Args), NumArgs), Name));
3171}
3172
3174 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
3175}
3176
3178 LLVMBasicBlockRef UnwindBB,
3179 unsigned NumHandlers, const char *Name) {
3180 if (ParentPad == nullptr) {
3181 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3182 ParentPad = wrap(Constant::getNullValue(Ty));
3183 }
3184 return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(UnwindBB),
3185 NumHandlers, Name));
3186}
3187
3189 LLVMBasicBlockRef BB) {
3190 return wrap(unwrap(B)->CreateCatchRet(unwrap<CatchPadInst>(CatchPad),
3191 unwrap(BB)));
3192}
3193
3195 LLVMBasicBlockRef BB) {
3196 return wrap(unwrap(B)->CreateCleanupRet(unwrap<CleanupPadInst>(CatchPad),
3197 unwrap(BB)));
3198}
3199
3201 return wrap(unwrap(B)->CreateUnreachable());
3202}
3203
3205 LLVMBasicBlockRef Dest) {
3206 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
3207}
3208
3210 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
3211}
3212
3213unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
3214 return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
3215}
3216
3218 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
3219}
3220
3222 unwrap<LandingPadInst>(LandingPad)->addClause(unwrap<Constant>(ClauseVal));
3223}
3224
3226 return unwrap<LandingPadInst>(LandingPad)->isCleanup();
3227}
3228
3229void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
3230 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
3231}
3232
3234 unwrap<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Dest));
3235}
3236
3237unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) {
3238 return unwrap<CatchSwitchInst>(CatchSwitch)->getNumHandlers();
3239}
3240
3241void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) {
3242 CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(CatchSwitch);
3243 for (const BasicBlock *H : CSI->handlers())
3244 *Handlers++ = wrap(H);
3245}
3246
3248 return wrap(unwrap<CatchPadInst>(CatchPad)->getCatchSwitch());
3249}
3250
3252 unwrap<CatchPadInst>(CatchPad)
3253 ->setCatchSwitch(unwrap<CatchSwitchInst>(CatchSwitch));
3254}
3255
3256/*--.. Funclets ...........................................................--*/
3257
3259 return wrap(unwrap<FuncletPadInst>(Funclet)->getArgOperand(i));
3260}
3261
3263 unwrap<FuncletPadInst>(Funclet)->setArgOperand(i, unwrap(value));
3264}
3265
3266/*--.. Arithmetic ..........................................................--*/
3267
3269 const char *Name) {
3270 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
3271}
3272
3274 const char *Name) {
3275 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
3276}
3277
3279 const char *Name) {
3280 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
3281}
3282
3284 const char *Name) {
3285 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
3286}
3287
3289 const char *Name) {
3290 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
3291}
3292
3294 const char *Name) {
3295 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
3296}
3297
3299 const char *Name) {
3300 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
3301}
3302
3304 const char *Name) {
3305 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
3306}
3307
3309 const char *Name) {
3310 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
3311}
3312
3314 const char *Name) {
3315 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
3316}
3317
3319 const char *Name) {
3320 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
3321}
3322
3324 const char *Name) {
3325 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
3326}
3327
3329 const char *Name) {
3330 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
3331}
3332
3334 LLVMValueRef RHS, const char *Name) {
3335 return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
3336}
3337
3339 const char *Name) {
3340 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
3341}
3342
3344 LLVMValueRef RHS, const char *Name) {
3345 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
3346}
3347
3349 const char *Name) {
3350 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
3351}
3352
3354 const char *Name) {
3355 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
3356}
3357
3359 const char *Name) {
3360 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
3361}
3362
3364 const char *Name) {
3365 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
3366}
3367
3369 const char *Name) {
3370 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
3371}
3372
3374 const char *Name) {
3375 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
3376}
3377
3379 const char *Name) {
3380 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
3381}
3382
3384 const char *Name) {
3385 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
3386}
3387
3389 const char *Name) {
3390 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
3391}
3392
3394 const char *Name) {
3395 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
3396}
3397
3399 LLVMValueRef LHS, LLVMValueRef RHS,
3400 const char *Name) {
3402 unwrap(RHS), Name));
3403}
3404
3406 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
3407}
3408
3410 const char *Name) {
3411 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
3412}
3413
3415 const char *Name) {
3416 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
3417}
3418
3420 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
3421}
3422
3424 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
3425}
3426
3428 Value *P = unwrap<Value>(ArithInst);
3429 return cast<Instruction>(P)->hasNoUnsignedWrap();
3430}
3431
3432void LLVMSetNUW(LLVMValueRef ArithInst, LLVMBool HasNUW) {
3433 Value *P = unwrap<Value>(ArithInst);
3434 cast<Instruction>(P)->setHasNoUnsignedWrap(HasNUW);
3435}
3436
3438 Value *P = unwrap<Value>(ArithInst);
3439 return cast<Instruction>(P)->hasNoSignedWrap();
3440}
3441
3442void LLVMSetNSW(LLVMValueRef ArithInst, LLVMBool HasNSW) {
3443 Value *P = unwrap<Value>(ArithInst);
3444 cast<Instruction>(P)->setHasNoSignedWrap(HasNSW);
3445}
3446
3448 Value *P = unwrap<Value>(DivOrShrInst);
3449 return cast<Instruction>(P)->isExact();
3450}
3451
3452void LLVMSetExact(LLVMValueRef DivOrShrInst, LLVMBool IsExact) {
3453 Value *P = unwrap<Value>(DivOrShrInst);
3454 cast<Instruction>(P)->setIsExact(IsExact);
3455}
3456
3458 Value *P = unwrap<Value>(NonNegInst);
3459 return cast<Instruction>(P)->hasNonNeg();
3460}
3461
3462void LLVMSetNNeg(LLVMValueRef NonNegInst, LLVMBool IsNonNeg) {
3463 Value *P = unwrap<Value>(NonNegInst);
3464 cast<Instruction>(P)->setNonNeg(IsNonNeg);
3465}
3466
3467/*--.. Memory ..............................................................--*/
3468
3470 const char *Name) {
3471 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3472 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3473 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3474 return wrap(unwrap(B)->CreateMalloc(ITy, unwrap(Ty), AllocSize, nullptr,
3475 nullptr, Name));
3476}
3477
3479 LLVMValueRef Val, const char *Name) {
3480 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3481 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3482 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3483 return wrap(unwrap(B)->CreateMalloc(ITy, unwrap(Ty), AllocSize, unwrap(Val),
3484 nullptr, Name));
3485}
3486
3488 LLVMValueRef Val, LLVMValueRef Len,
3489 unsigned Align) {
3490 return wrap(unwrap(B)->CreateMemSet(unwrap(Ptr), unwrap(Val), unwrap(Len),
3491 MaybeAlign(Align)));
3492}
3493
3495 LLVMValueRef Dst, unsigned DstAlign,
3496 LLVMValueRef Src, unsigned SrcAlign,
3498 return wrap(unwrap(B)->CreateMemCpy(unwrap(Dst), MaybeAlign(DstAlign),
3499 unwrap(Src), MaybeAlign(SrcAlign),
3500 unwrap(Size)));
3501}
3502
3504 LLVMValueRef Dst, unsigned DstAlign,
3505 LLVMValueRef Src, unsigned SrcAlign,
3507 return wrap(unwrap(B)->CreateMemMove(unwrap(Dst), MaybeAlign(DstAlign),
3508 unwrap(Src), MaybeAlign(SrcAlign),
3509 unwrap(Size)));
3510}
3511
3513 const char *Name) {
3514 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
3515}
3516
3518 LLVMValueRef Val, const char *Name) {
3519 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
3520}
3521
3523 return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
3524}
3525
3527 LLVMValueRef PointerVal, const char *Name) {
3528 return wrap(unwrap(B)->CreateLoad(unwrap(Ty), unwrap(PointerVal), Name));
3529}
3530
3532 LLVMValueRef PointerVal) {
3533 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
3534}
3535
3537 switch (Ordering) {
3538 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic;
3539 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered;
3540 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic;
3541 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire;
3542 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release;
3544 return AtomicOrdering::AcquireRelease;
3546 return AtomicOrdering::SequentiallyConsistent;
3547 }
3548
3549 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
3550}
3551
3553 switch (Ordering) {
3554 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic;
3555 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered;
3556 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic;
3557 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire;
3558 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease;
3559 case AtomicOrdering::AcquireRelease:
3561 case AtomicOrdering::SequentiallyConsistent:
3563 }
3564
3565 llvm_unreachable("Invalid AtomicOrdering value!");
3566}
3567
3569 switch (BinOp) {
3585 }
3586
3587 llvm_unreachable("Invalid LLVMAtomicRMWBinOp value!");
3588}
3589
3591 switch (BinOp) {
3607 default: break;
3608 }
3609
3610 llvm_unreachable("Invalid AtomicRMWBinOp value!");
3611}
3612
3613// TODO: Should this and other atomic instructions support building with
3614// "syncscope"?
3616 LLVMBool isSingleThread, const char *Name) {
3617 return wrap(
3618 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
3619 isSingleThread ? SyncScope::SingleThread
3621 Name));
3622}
3623
3625 LLVMValueRef Pointer, LLVMValueRef *Indices,
3626 unsigned NumIndices, const char *Name) {
3627 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3628 return wrap(unwrap(B)->CreateGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3629}
3630
3632 LLVMValueRef Pointer, LLVMValueRef *Indices,
3633 unsigned NumIndices, const char *Name) {
3634 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3635 return wrap(
3636 unwrap(B)->CreateInBoundsGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3637}
3638
3640 LLVMValueRef Pointer, unsigned Idx,
3641 const char *Name) {
3642 return wrap(
3643 unwrap(B)->CreateStructGEP(unwrap(Ty), unwrap(Pointer), Idx, Name));
3644}
3645
3647 const char *Name) {
3648 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
3649}
3650
3652 const char *Name) {
3653 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
3654}
3655
3657 Value *P = unwrap(MemAccessInst);
3658 if (LoadInst *LI = dyn_cast<LoadInst>(P))
3659 return LI->isVolatile();
3660 if (StoreInst *SI = dyn_cast<StoreInst>(P))
3661 return SI->isVolatile();
3662 if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
3663 return AI->isVolatile();
3664 return cast<AtomicCmpXchgInst>(P)->isVolatile();
3665}
3666
3667void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
3668 Value *P = unwrap(MemAccessInst);
3669 if (LoadInst *LI = dyn_cast<LoadInst>(P))
3670 return LI->setVolatile(isVolatile);
3671 if (StoreInst *SI = dyn_cast<StoreInst>(P))
3672 return SI->setVolatile(isVolatile);
3673 if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
3674 return AI->setVolatile(isVolatile);
3675 return cast<AtomicCmpXchgInst>(P)->setVolatile(isVolatile);
3676}
3677
3679 return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->isWeak();
3680}
3681
3682void LLVMSetWeak(LLVMValueRef CmpXchgInst, LLVMBool isWeak) {
3683 return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->setWeak(isWeak);
3684}
3685
3687 Value *P = unwrap(MemAccessInst);
3689 if (LoadInst *LI = dyn_cast<LoadInst>(P))
3690 O = LI->getOrdering();
3691 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
3692 O = SI->getOrdering();
3693 else if (FenceInst *FI = dyn_cast<FenceInst>(P))
3694 O = FI->getOrdering();
3695 else
3696 O = cast<AtomicRMWInst>(P)->getOrdering();
3697 return mapToLLVMOrdering(O);
3698}
3699
3700void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
3701 Value *P = unwrap(MemAccessInst);
3702 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3703
3704 if (LoadInst *LI = dyn_cast<LoadInst>(P))
3705 return LI->setOrdering(O);
3706 else if (FenceInst *FI = dyn_cast<FenceInst>(P))
3707 return FI->setOrdering(O);
3708 else if (AtomicRMWInst *ARWI = dyn_cast<AtomicRMWInst>(P))
3709 return ARWI->setOrdering(O);
3710 return cast<StoreInst>(P)->setOrdering(O);
3711}
3712
3714 return mapToLLVMRMWBinOp(unwrap<AtomicRMWInst>(Inst)->getOperation());
3715}
3716