LLVM 17.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
464 size_t AsmStringSize, 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
483/*--.. Operations on module contexts ......................................--*/
485 return wrap(&unwrap(M)->getContext());
486}
487
488
489/*===-- Operations on types -----------------------------------------------===*/
490
491/*--.. Operations on all types (mostly) ....................................--*/
492
494 switch (unwrap(Ty)->getTypeID()) {
495 case Type::VoidTyID:
496 return LLVMVoidTypeKind;
497 case Type::HalfTyID:
498 return LLVMHalfTypeKind;
499 case Type::BFloatTyID:
500 return LLVMBFloatTypeKind;
501 case Type::FloatTyID:
502 return LLVMFloatTypeKind;
503 case Type::DoubleTyID:
504 return LLVMDoubleTypeKind;
507 case Type::FP128TyID:
508 return LLVMFP128TypeKind;
511 case Type::LabelTyID:
512 return LLVMLabelTypeKind;
516 return LLVMIntegerTypeKind;
519 case Type::StructTyID:
520 return LLVMStructTypeKind;
521 case Type::ArrayTyID:
522 return LLVMArrayTypeKind;
524 return LLVMPointerTypeKind;
526 return LLVMVectorTypeKind;
528 return LLVMX86_MMXTypeKind;
530 return LLVMX86_AMXTypeKind;
531 case Type::TokenTyID:
532 return LLVMTokenTypeKind;
538 llvm_unreachable("Typed pointers are unsupported via the C API");
539 }
540 llvm_unreachable("Unhandled TypeID.");
541}
542
544{
545 return unwrap(Ty)->isSized();
546}
547
549 return wrap(&unwrap(Ty)->getContext());
550}
551
553 return unwrap(Ty)->print(errs(), /*IsForDebug=*/true);
554}
555
557 std::string buf;
558 raw_string_ostream os(buf);
559
560 if (unwrap(Ty))
561 unwrap(Ty)->print(os);
562 else
563 os << "Printing <null> Type";
564
565 os.flush();
566
567 return strdup(buf.c_str());
568}
569
570/*--.. Operations on integer types .........................................--*/
571
574}
577}
580}
583}
586}
589}
591 return wrap(IntegerType::get(*unwrap(C), NumBits));
592}
593
596}
599}
602}
605}
608}
611}
612LLVMTypeRef LLVMIntType(unsigned NumBits) {
614}
615
616unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
617 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
618}
619
620/*--.. Operations on real types ............................................--*/
621
624}
627}
630}
633}
636}
639}
642}
645}
648}
649
652}
655}
658}
661}
664}
667}
670}
673}
676}
677
678/*--.. Operations on function types ........................................--*/
679
681 LLVMTypeRef *ParamTypes, unsigned ParamCount,
682 LLVMBool IsVarArg) {
683 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
684 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
685}
686
688 return unwrap<FunctionType>(FunctionTy)->isVarArg();
689}
690
692 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
693}
694
695unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
696 return unwrap<FunctionType>(FunctionTy)->getNumParams();
697}
698
700 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
701 for (Type *T : Ty->params())
702 *Dest++ = wrap(T);
703}
704
705/*--.. Operations on struct types ..........................................--*/
706
708 unsigned ElementCount, LLVMBool Packed) {
709 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
710 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
711}
712
714 unsigned ElementCount, LLVMBool Packed) {
715 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
716 ElementCount, Packed);
717}
718
720{
721 return wrap(StructType::create(*unwrap(C), Name));
722}
723
725{
726 StructType *Type = unwrap<StructType>(Ty);
727 if (!Type->hasName())
728 return nullptr;
729 return Type->getName().data();
730}
731
732void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
733 unsigned ElementCount, LLVMBool Packed) {
734 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
735 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
736}
737
739 return unwrap<StructType>(StructTy)->getNumElements();
740}
741
743 StructType *Ty = unwrap<StructType>(StructTy);
744 for (Type *T : Ty->elements())
745 *Dest++ = wrap(T);
746}
747
749 StructType *Ty = unwrap<StructType>(StructTy);
750 return wrap(Ty->getTypeAtIndex(i));
751}
752
754 return unwrap<StructType>(StructTy)->isPacked();
755}
756
758 return unwrap<StructType>(StructTy)->isOpaque();
759}
760
762 return unwrap<StructType>(StructTy)->isLiteral();
763}
764
766 return wrap(StructType::getTypeByName(unwrap(M)->getContext(), Name));
767}
768
771}
772
773/*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
774
776 int i = 0;
777 for (auto *T : unwrap(Tp)->subtypes()) {
778 Arr[i] = wrap(T);
779 i++;
780 }
781}
782
784 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
785}
786
788 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
789}
790
792 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
793}
794
796 return unwrap(Ty)->isOpaquePointerTy();
797}
798
800 return wrap(FixedVectorType::get(unwrap(ElementType), ElementCount));
801}
802
804 unsigned ElementCount) {
805 return wrap(ScalableVectorType::get(unwrap(ElementType), ElementCount));
806}
807
809 auto *Ty = unwrap(WrappedTy);
810 if (auto *ATy = dyn_cast<ArrayType>(Ty))
811 return wrap(ATy->getElementType());
812 return wrap(cast<VectorType>(Ty)->getElementType());
813}
814
816 return unwrap(Tp)->getNumContainedTypes();
817}
818
820 return unwrap<ArrayType>(ArrayTy)->getNumElements();
821}
822
824 return unwrap<ArrayType>(ArrayTy)->getNumElements();
825}
826
828 return unwrap<PointerType>(PointerTy)->getAddressSpace();
829}
830
831unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
832 return unwrap<VectorType>(VectorTy)->getElementCount().getKnownMinValue();
833}
834
835/*--.. Operations on other types ...........................................--*/
836
838 return wrap(PointerType::get(*unwrap(C), AddressSpace));
839}
840
842 return wrap(Type::getVoidTy(*unwrap(C)));
843}
845 return wrap(Type::getLabelTy(*unwrap(C)));
846}
848 return wrap(Type::getTokenTy(*unwrap(C)));
849}
851 return wrap(Type::getMetadataTy(*unwrap(C)));
852}
853
856}
859}
860
862 LLVMTypeRef *TypeParams,
863 unsigned TypeParamCount,
864 unsigned *IntParams,
865 unsigned IntParamCount) {
866 ArrayRef<Type *> TypeParamArray(unwrap(TypeParams), TypeParamCount);
867 ArrayRef<unsigned> IntParamArray(IntParams, IntParamCount);
868 return wrap(
869 TargetExtType::get(*unwrap(C), Name, TypeParamArray, IntParamArray));
870}
871
872/*===-- Operations on values ----------------------------------------------===*/
873
874/*--.. Operations on all values ............................................--*/
875
877 return wrap(unwrap(Val)->getType());
878}
879
881 switch(unwrap(Val)->getValueID()) {
882#define LLVM_C_API 1
883#define HANDLE_VALUE(Name) \
884 case Value::Name##Val: \
885 return LLVM##Name##ValueKind;
886#include "llvm/IR/Value.def"
887 default:
889 }
890}
891
892const char *LLVMGetValueName2(LLVMValueRef Val, size_t *Length) {
893 auto *V = unwrap(Val);
894 *Length = V->getName().size();
895 return V->getName().data();
896}
897
898void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen) {
899 unwrap(Val)->setName(StringRef(Name, NameLen));
900}
901
903 return unwrap(Val)->getName().data();
904}
905
906void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
907 unwrap(Val)->setName(Name);
908}
909
911 unwrap(Val)->print(errs(), /*IsForDebug=*/true);
912}
913
915 std::string buf;
916 raw_string_ostream os(buf);
917
918 if (unwrap(Val))
919 unwrap(Val)->print(os);
920 else
921 os << "Printing <null> Value";
922
923 os.flush();
924
925 return strdup(buf.c_str());
926}
927
929 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
930}
931
933 return unwrap<Instruction>(Inst)->hasMetadata();
934}
935
937 auto *I = unwrap<Instruction>(Inst);
938 assert(I && "Expected instruction");
939 if (auto *MD = I->getMetadata(KindID))
940 return wrap(MetadataAsValue::get(I->getContext(), MD));
941 return nullptr;
942}
943
944// MetadataAsValue uses a canonical format which strips the actual MDNode for
945// MDNode with just a single constant value, storing just a ConstantAsMetadata
946// This undoes this canonicalization, reconstructing the MDNode.
948 Metadata *MD = MAV->getMetadata();
949 assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
950 "Expected a metadata node or a canonicalized constant");
951
952 if (MDNode *N = dyn_cast<MDNode>(MD))
953 return N;
954
955 return MDNode::get(MAV->getContext(), MD);
956}
957
958void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
959 MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
960
961 unwrap<Instruction>(Inst)->setMetadata(KindID, N);
962}
963
965 unsigned Kind;
967};
968
971llvm_getMetadata(size_t *NumEntries,
972 llvm::function_ref<void(MetadataEntries &)> AccessMD) {
974 AccessMD(MVEs);
975
977 static_cast<LLVMOpaqueValueMetadataEntry *>(
979 for (unsigned i = 0; i < MVEs.size(); ++i) {
980 const auto &ModuleFlag = MVEs[i];
981 Result[i].Kind = ModuleFlag.first;
982 Result[i].Metadata = wrap(ModuleFlag.second);
983 }
984 *NumEntries = MVEs.size();
985 return Result;
986}
987
990 size_t *NumEntries) {
991 return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
992 Entries.clear();
993 unwrap<Instruction>(Value)->getAllMetadata(Entries);
994 });
995}
996
997/*--.. Conversion functions ................................................--*/
998
999#define LLVM_DEFINE_VALUE_CAST(name) \
1000 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
1001 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
1002 }
1003
1005
1007 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
1008 if (isa<MDNode>(MD->getMetadata()) ||
1009 isa<ValueAsMetadata>(MD->getMetadata()))
1010 return Val;
1011 return nullptr;
1012}
1013
1015 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
1016 if (isa<ValueAsMetadata>(MD->getMetadata()))
1017 return Val;
1018 return nullptr;
1019}
1020
1022 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
1023 if (isa<MDString>(MD->getMetadata()))
1024 return Val;
1025 return nullptr;
1026}
1027
1028/*--.. Operations on Uses ..................................................--*/
1030 Value *V = unwrap(Val);
1031 Value::use_iterator I = V->use_begin();
1032 if (I == V->use_end())
1033 return nullptr;
1034 return wrap(&*I);
1035}
1036
1038 Use *Next = unwrap(U)->getNext();
1039 if (Next)
1040 return wrap(Next);
1041 return nullptr;
1042}
1043
1045 return wrap(unwrap(U)->getUser());
1046}
1047
1049 return wrap(unwrap(U)->get());
1050}
1051
1052/*--.. Operations on Users .................................................--*/
1053
1055 unsigned Index) {
1056 Metadata *Op = N->getOperand(Index);
1057 if (!Op)
1058 return nullptr;
1059 if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
1060 return wrap(C->getValue());
1061 return wrap(MetadataAsValue::get(Context, Op));
1062}
1063
1065 Value *V = unwrap(Val);
1066 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
1067 if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1068 assert(Index == 0 && "Function-local metadata can only have one operand");
1069 return wrap(L->getValue());
1070 }
1071 return getMDNodeOperandImpl(V->getContext(),
1072 cast<MDNode>(MD->getMetadata()), Index);
1073 }
1074
1075 return wrap(cast<User>(V)->getOperand(Index));
1076}
1077
1079 Value *V = unwrap(Val);
1080 return wrap(&cast<User>(V)->getOperandUse(Index));
1081}
1082
1084 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
1085}
1086
1088 Value *V = unwrap(Val);
1089 if (isa<MetadataAsValue>(V))
1090 return LLVMGetMDNodeNumOperands(Val);
1091
1092 return cast<User>(V)->getNumOperands();
1093}
1094
1095/*--.. Operations on constants of any type .................................--*/
1096
1098 return wrap(Constant::getNullValue(unwrap(Ty)));
1099}
1100
1103}
1104
1106 return wrap(UndefValue::get(unwrap(Ty)));
1107}
1108
1110 return wrap(PoisonValue::get(unwrap(Ty)));
1111}
1112
1114 return isa<Constant>(unwrap(Ty));
1115}
1116
1118 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
1119 return C->isNullValue();
1120 return false;
1121}
1122
1124 return isa<UndefValue>(unwrap(Val));
1125}
1126
1128 return isa<PoisonValue>(unwrap(Val));
1129}
1130
1132 return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
1133}
1134
1135/*--.. Operations on metadata nodes ........................................--*/
1136
1138 size_t SLen) {
1139 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
1140}
1141
1143 size_t Count) {
1144 return wrap(MDNode::get(*unwrap(C), ArrayRef<Metadata*>(unwrap(MDs), Count)));
1145}
1146
1148 unsigned SLen) {
1151 Context, MDString::get(Context, StringRef(Str, SLen))));
1152}
1153
1154LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
1155 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
1156}
1157
1159 unsigned Count) {
1162 for (auto *OV : ArrayRef(Vals, Count)) {
1163 Value *V = unwrap(OV);
1164 Metadata *MD;
1165 if (!V)
1166 MD = nullptr;
1167 else if (auto *C = dyn_cast<Constant>(V))
1169 else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
1170 MD = MDV->getMetadata();
1171 assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
1172 "outside of direct argument to call");
1173 } else {
1174 // This is function-local metadata. Pretend to make an MDNode.
1175 assert(Count == 1 &&
1176 "Expected only one operand to function-local metadata");
1178 }
1179
1180 MDs.push_back(MD);
1181 }
1183}
1184
1185LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
1186 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
1187}
1188
1190 return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
1191}
1192
1194 auto *V = unwrap(Val);
1195 if (auto *C = dyn_cast<Constant>(V))
1197 if (auto *MAV = dyn_cast<MetadataAsValue>(V))
1198 return wrap(MAV->getMetadata());
1199 return wrap(ValueAsMetadata::get(V));
1200}
1201
1202const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
1203 if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
1204 if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
1205 *Length = S->getString().size();
1206 return S->getString().data();
1207 }
1208 *Length = 0;
1209 return nullptr;
1210}
1211
1213 auto *MD = unwrap<MetadataAsValue>(V);
1214 if (isa<ValueAsMetadata>(MD->getMetadata()))
1215 return 1;
1216 return cast<MDNode>(MD->getMetadata())->getNumOperands();
1217}
1218
1220 Module *Mod = unwrap(M);
1222 if (I == Mod->named_metadata_end())
1223 return nullptr;
1224 return wrap(&*I);
1225}
1226
1228 Module *Mod = unwrap(M);
1230 if (I == Mod->named_metadata_begin())
1231 return nullptr;
1232 return wrap(&*--I);
1233}
1234
1236 NamedMDNode *NamedNode = unwrap(NMD);
1238 if (++I == NamedNode->getParent()->named_metadata_end())
1239 return nullptr;
1240 return wrap(&*I);
1241}
1242
1244 NamedMDNode *NamedNode = unwrap(NMD);
1246 if (I == NamedNode->getParent()->named_metadata_begin())
1247 return nullptr;
1248 return wrap(&*--I);
1249}
1250
1252 const char *Name, size_t NameLen) {
1253 return wrap(unwrap(M)->getNamedMetadata(StringRef(Name, NameLen)));
1254}
1255
1257 const char *Name, size_t NameLen) {
1258 return wrap(unwrap(M)->getOrInsertNamedMetadata({Name, NameLen}));
1259}
1260
1261const char *LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NMD, size_t *NameLen) {
1262 NamedMDNode *NamedNode = unwrap(NMD);
1263 *NameLen = NamedNode->getName().size();
1264 return NamedNode->getName().data();
1265}
1266
1268 auto *MD = unwrap<MetadataAsValue>(V);
1269 if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1270 *Dest = wrap(MDV->getValue());
1271 return;
1272 }
1273 const auto *N = cast<MDNode>(MD->getMetadata());
1274 const unsigned numOperands = N->getNumOperands();
1275 LLVMContext &Context = unwrap(V)->getContext();
1276 for (unsigned i = 0; i < numOperands; i++)
1277 Dest[i] = getMDNodeOperandImpl(Context, N, i);
1278}
1279
1281 LLVMMetadataRef Replacement) {
1282 auto *MD = cast<MetadataAsValue>(unwrap(V));
1283 auto *N = cast<MDNode>(MD->getMetadata());
1284 N->replaceOperandWith(Index, unwrap<Metadata>(Replacement));
1285}
1286
1288 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
1289 return N->getNumOperands();
1290 }
1291 return 0;
1292}
1293
1295 LLVMValueRef *Dest) {
1296 NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
1297 if (!N)
1298 return;
1299 LLVMContext &Context = unwrap(M)->getContext();
1300 for (unsigned i=0;i<N->getNumOperands();i++)
1301 Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
1302}
1303
1305 LLVMValueRef Val) {
1306 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
1307 if (!N)
1308 return;
1309 if (!Val)
1310 return;
1311 N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
1312}
1313
1314const char *LLVMGetDebugLocDirectory(LLVMValueRef Val, unsigned *Length) {
1315 if (!Length) return nullptr;
1316 StringRef S;
1317 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1318 if (const auto &DL = I->getDebugLoc()) {
1319 S = DL->getDirectory();
1320 }
1321 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1323 GV->getDebugInfo(GVEs);
1324 if (GVEs.size())
1325 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1326 S = DGV->getDirectory();
1327 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1328 if (const DISubprogram *DSP = F->getSubprogram())
1329 S = DSP->getDirectory();
1330 } else {
1331 assert(0 && "Expected Instruction, GlobalVariable or Function");
1332 return nullptr;
1333 }
1334 *Length = S.size();
1335 return S.data();
1336}
1337
1338const char *LLVMGetDebugLocFilename(LLVMValueRef Val, unsigned *Length) {
1339 if (!Length) return nullptr;
1340 StringRef S;
1341 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1342 if (const auto &DL = I->getDebugLoc()) {
1343 S = DL->getFilename();
1344 }
1345 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1347 GV->getDebugInfo(GVEs);
1348 if (GVEs.size())
1349 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1350 S = DGV->getFilename();
1351 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1352 if (const DISubprogram *DSP = F->getSubprogram())
1353 S = DSP->getFilename();
1354 } else {
1355 assert(0 && "Expected Instruction, GlobalVariable or Function");
1356 return nullptr;
1357 }
1358 *Length = S.size();
1359 return S.data();
1360}
1361
1363 unsigned L = 0;
1364 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1365 if (const auto &DL = I->getDebugLoc()) {
1366 L = DL->getLine();
1367 }
1368 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1370 GV->getDebugInfo(GVEs);
1371 if (GVEs.size())
1372 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1373 L = DGV->getLine();
1374 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1375 if (const DISubprogram *DSP = F->getSubprogram())
1376 L = DSP->getLine();
1377 } else {
1378 assert(0 && "Expected Instruction, GlobalVariable or Function");
1379 return -1;
1380 }
1381 return L;
1382}
1383
1385 unsigned C = 0;
1386 if (const auto *I = dyn_cast<Instruction>(unwrap(Val)))
1387 if (const auto &DL = I->getDebugLoc())
1388 C = DL->getColumn();
1389 return C;
1390}
1391
1392/*--.. Operations on scalar constants ......................................--*/
1393
1394LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
1395 LLVMBool SignExtend) {
1396 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
1397}
1398
1400 unsigned NumWords,
1401 const uint64_t Words[]) {
1402 IntegerType *Ty = unwrap<IntegerType>(IntTy);
1403 return wrap(ConstantInt::get(
1404 Ty->getContext(), APInt(Ty->getBitWidth(), ArrayRef(Words, NumWords))));
1405}
1406
1408 uint8_t Radix) {
1409 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
1410 Radix));
1411}
1412
1414 unsigned SLen, uint8_t Radix) {
1415 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
1416 Radix));
1417}
1418
1420 return wrap(ConstantFP::get(unwrap(RealTy), N));
1421}
1422
1424 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
1425}
1426
1428 unsigned SLen) {
1429 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
1430}
1431
1432unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
1433 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
1434}
1435
1437 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
1438}
1439
1440double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
1441 ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
1442 Type *Ty = cFP->getType();
1443
1444 if (Ty->isHalfTy() || Ty->isBFloatTy() || Ty->isFloatTy() ||
1445 Ty->isDoubleTy()) {
1446 *LosesInfo = false;
1447 return cFP->getValueAPF().convertToDouble();
1448 }
1449
1450 bool APFLosesInfo;
1451 APFloat APF = cFP->getValueAPF();
1452 APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo);
1453 *LosesInfo = APFLosesInfo;
1454 return APF.convertToDouble();
1455}
1456
1457/*--.. Operations on composite constants ...................................--*/
1458
1460 unsigned Length,
1461 LLVMBool DontNullTerminate) {
1462 /* Inverted the sense of AddNull because ', 0)' is a
1463 better mnemonic for null termination than ', 1)'. */
1465 DontNullTerminate == 0));
1466}
1467
1468LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
1469 LLVMBool DontNullTerminate) {
1471 DontNullTerminate);
1472}
1473
1475 return wrap(unwrap<Constant>(C)->getAggregateElement(Idx));
1476}
1477
1479 return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
1480}
1481
1483 return unwrap<ConstantDataSequential>(C)->isString();
1484}
1485
1486const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1487 StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1488 *Length = Str.size();
1489 return Str.data();
1490}
1491
1493 LLVMValueRef *ConstantVals, unsigned Length) {
1494 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1495 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1496}
1497
1499 uint64_t Length) {
1500 ArrayRef<Constant *> V(unwrap<Constant>(ConstantVals, Length), Length);
1501 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1502}
1503
1505 LLVMValueRef *ConstantVals,
1506 unsigned Count, LLVMBool Packed) {
1507 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1508 return wrap(ConstantStruct::getAnon(*unwrap(C), ArrayRef(Elements, Count),
1509 Packed != 0));
1510}
1511
1512LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
1513 LLVMBool Packed) {
1514 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1515 Packed);
1516}
1517
1519 LLVMValueRef *ConstantVals,
1520 unsigned Count) {
1521 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1522 StructType *Ty = unwrap<StructType>(StructTy);
1523
1524 return wrap(ConstantStruct::get(Ty, ArrayRef(Elements, Count)));
1525}
1526
1527LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
1529 ArrayRef(unwrap<Constant>(ScalarConstantVals, Size), Size)));
1530}
1531
1532/*-- Opcode mapping */
1533
1535{
1536 switch (opcode) {
1537 default: llvm_unreachable("Unhandled Opcode.");
1538#define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
1539#include "llvm/IR/Instruction.def"
1540#undef HANDLE_INST
1541 }
1542}
1543
1545{
1546 switch (code) {
1547#define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
1548#include "llvm/IR/Instruction.def"
1549#undef HANDLE_INST
1550 }
1551 llvm_unreachable("Unhandled Opcode.");
1552}
1553
1554/*--.. Constant expressions ................................................--*/
1555
1557 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
1558}
1559
1562}
1563
1565 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
1566}
1567
1569 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1570}
1571
1573 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
1574}
1575
1577 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
1578}
1579
1580
1582 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
1583}
1584
1586 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
1587 unwrap<Constant>(RHSConstant)));
1588}
1589
1591 LLVMValueRef RHSConstant) {
1592 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
1593 unwrap<Constant>(RHSConstant)));
1594}
1595
1597 LLVMValueRef RHSConstant) {
1598 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
1599 unwrap<Constant>(RHSConstant)));
1600}
1601
1603 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
1604 unwrap<Constant>(RHSConstant)));
1605}
1606
1608 LLVMValueRef RHSConstant) {
1609 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
1610 unwrap<Constant>(RHSConstant)));
1611}
1612
1614 LLVMValueRef RHSConstant) {
1615 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
1616 unwrap<Constant>(RHSConstant)));
1617}
1618
1620 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
1621 unwrap<Constant>(RHSConstant)));
1622}
1623
1625 LLVMValueRef RHSConstant) {
1626 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
1627 unwrap<Constant>(RHSConstant)));
1628}
1629
1631 LLVMValueRef RHSConstant) {
1632 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
1633 unwrap<Constant>(RHSConstant)));
1634}
1635
1637 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
1638 unwrap<Constant>(RHSConstant)));
1639}
1640
1642 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
1643 unwrap<Constant>(RHSConstant)));
1644}
1645
1647 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
1648 unwrap<Constant>(RHSConstant)));
1649}
1650
1652 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1653 return wrap(ConstantExpr::getICmp(Predicate,
1654 unwrap<Constant>(LHSConstant),
1655 unwrap<Constant>(RHSConstant)));
1656}
1657
1659 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1660 return wrap(ConstantExpr::getFCmp(Predicate,
1661 unwrap<Constant>(LHSConstant),
1662 unwrap<Constant>(RHSConstant)));
1663}
1664
1666 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1667 unwrap<Constant>(RHSConstant)));
1668}
1669
1671 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
1672 unwrap<Constant>(RHSConstant)));
1673}
1674
1676 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1677 unwrap<Constant>(RHSConstant)));
1678}
1679
1681 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1682 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1683 NumIndices);
1684 Constant *Val = unwrap<Constant>(ConstantVal);
1685 return wrap(ConstantExpr::getGetElementPtr(unwrap(Ty), Val, IdxList));
1686}
1687
1689 LLVMValueRef *ConstantIndices,
1690 unsigned NumIndices) {
1691 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1692 NumIndices);
1693 Constant *Val = unwrap<Constant>(ConstantVal);
1694 return wrap(ConstantExpr::getInBoundsGetElementPtr(unwrap(Ty), Val, IdxList));
1695}
1696
1698 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1699 unwrap(ToType)));
1700}
1701
1703 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1704 unwrap(ToType)));
1705}
1706
1708 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1709 unwrap(ToType)));
1710}
1711
1713 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1714 unwrap(ToType)));
1715}
1716
1718 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1719 unwrap(ToType)));
1720}
1721
1723 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1724 unwrap(ToType)));
1725}
1726
1728 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1729 unwrap(ToType)));
1730}
1731
1733 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1734 unwrap(ToType)));
1735}
1736
1738 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1739 unwrap(ToType)));
1740}
1741
1743 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1744 unwrap(ToType)));
1745}
1746
1748 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1749 unwrap(ToType)));
1750}
1751
1753 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1754 unwrap(ToType)));
1755}
1756
1758 LLVMTypeRef ToType) {
1759 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1760 unwrap(ToType)));
1761}
1762
1764 LLVMTypeRef ToType) {
1765 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1766 unwrap(ToType)));
1767}
1768
1770 LLVMTypeRef ToType) {
1771 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1772 unwrap(ToType)));
1773}
1774
1776 LLVMTypeRef ToType) {
1777 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1778 unwrap(ToType)));
1779}
1780
1782 LLVMTypeRef ToType) {
1783 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1784 unwrap(ToType)));
1785}
1786
1789 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1790 unwrap(ToType), isSigned));
1791}
1792
1794 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1795 unwrap(ToType)));
1796}
1797
1799 LLVMValueRef IndexConstant) {
1800 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1801 unwrap<Constant>(IndexConstant)));
1802}
1803
1805 LLVMValueRef ElementValueConstant,
1806 LLVMValueRef IndexConstant) {
1807 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1808 unwrap<Constant>(ElementValueConstant),
1809 unwrap<Constant>(IndexConstant)));
1810}
1811
1813 LLVMValueRef VectorBConstant,
1814 LLVMValueRef MaskConstant) {
1815 SmallVector<int, 16> IntMask;
1816 ShuffleVectorInst::getShuffleMask(unwrap<Constant>(MaskConstant), IntMask);
1817 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1818 unwrap<Constant>(VectorBConstant),
1819 IntMask));
1820}
1821
1823 const char *Constraints,
1824 LLVMBool HasSideEffects,
1825 LLVMBool IsAlignStack) {
1826 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1827 Constraints, HasSideEffects, IsAlignStack));
1828}
1829
1831 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1832}
1833
1834/*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1835
1837 return wrap(unwrap<GlobalValue>(Global)->getParent());
1838}
1839
1841 return unwrap<GlobalValue>(Global)->isDeclaration();
1842}
1843
1845 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1847 return LLVMExternalLinkage;
1855 return LLVMWeakAnyLinkage;
1857 return LLVMWeakODRLinkage;
1859 return LLVMAppendingLinkage;
1861 return LLVMInternalLinkage;
1863 return LLVMPrivateLinkage;
1867 return LLVMCommonLinkage;
1868 }
1869
1870 llvm_unreachable("Invalid GlobalValue linkage!");
1871}
1872
1874 GlobalValue *GV = unwrap<GlobalValue>(Global);
1875
1876 switch (Linkage) {
1879 break;
1882 break;
1885 break;
1888 break;
1890 LLVM_DEBUG(
1891 errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1892 "longer supported.");
1893 break;
1894 case LLVMWeakAnyLinkage:
1896 break;
1897 case LLVMWeakODRLinkage:
1899 break;
1902 break;
1905 break;
1906 case LLVMPrivateLinkage:
1908 break;
1911 break;
1914 break;
1916 LLVM_DEBUG(
1917 errs()
1918 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1919 break;
1921 LLVM_DEBUG(
1922 errs()
1923 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1924 break;
1927 break;
1928 case LLVMGhostLinkage:
1929 LLVM_DEBUG(
1930 errs() << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1931 break;
1932 case LLVMCommonLinkage:
1934 break;
1935 }
1936}
1937
1939 // Using .data() is safe because of how GlobalObject::setSection is
1940 // implemented.
1941 return unwrap<GlobalValue>(Global)->getSection().data();
1942}
1943
1944void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1945 unwrap<GlobalObject>(Global)->setSection(Section);
1946}
1947
1949 return static_cast<LLVMVisibility>(
1950 unwrap<GlobalValue>(Global)->getVisibility());
1951}
1952
1954 unwrap<GlobalValue>(Global)
1955 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1956}
1957
1959 return static_cast<LLVMDLLStorageClass>(
1960 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1961}
1962
1964 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1965 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1966}
1967
1969 switch (unwrap<GlobalValue>(Global)->getUnnamedAddr()) {
1970 case GlobalVariable::UnnamedAddr::None:
1971 return LLVMNoUnnamedAddr;
1972 case GlobalVariable::UnnamedAddr::Local:
1973 return LLVMLocalUnnamedAddr;
1974 case GlobalVariable::UnnamedAddr::Global:
1975 return LLVMGlobalUnnamedAddr;
1976 }
1977 llvm_unreachable("Unknown UnnamedAddr kind!");
1978}
1979
1981 GlobalValue *GV = unwrap<GlobalValue>(Global);
1982
1983 switch (UnnamedAddr) {
1984 case LLVMNoUnnamedAddr:
1985 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::None);
1987 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Local);
1989 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Global);
1990 }
1991}
1992
1994 return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
1995}
1996
1998 unwrap<GlobalValue>(Global)->setUnnamedAddr(
1999 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
2000 : GlobalValue::UnnamedAddr::None);
2001}
2002
2004 return wrap(unwrap<GlobalValue>(Global)->getValueType());
2005}
2006
2007/*--.. Operations on global variables, load and store instructions .........--*/
2008
2010 Value *P = unwrap(V);
2011 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2012 return GV->getAlign() ? GV->getAlign()->value() : 0;
2013 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
2014 return AI->getAlign().value();
2015 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2016 return LI->getAlign().value();
2017 if (StoreInst *SI = dyn_cast<StoreInst>(P))
2018 return SI->getAlign().value();
2019 if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2020 return RMWI->getAlign().value();
2021 if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2022 return CXI->getAlign().value();
2023
2025 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, "
2026 "and AtomicCmpXchgInst have alignment");
2027}
2028
2029void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
2030 Value *P = unwrap(V);
2031 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2032 GV->setAlignment(MaybeAlign(Bytes));
2033 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
2034 AI->setAlignment(Align(Bytes));
2035 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
2036 LI->setAlignment(Align(Bytes));
2037 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
2038 SI->setAlignment(Align(Bytes));
2039 else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2040 RMWI->setAlignment(Align(Bytes));
2041 else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2042 CXI->setAlignment(Align(Bytes));
2043 else
2045 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, and "
2046 "and AtomicCmpXchgInst have alignment");
2047}
2048
2050 size_t *NumEntries) {
2051 return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
2052 Entries.clear();
2053 if (Instruction *Instr = dyn_cast<Instruction>(unwrap(Value))) {
2054 Instr->getAllMetadata(Entries);
2055 } else {
2056 unwrap<GlobalObject>(Value)->getAllMetadata(Entries);
2057 }
2058 });
2059}
2060
2062 unsigned Index) {
2064 static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2065 return MVE.Kind;
2066}
2067
2070 unsigned Index) {
2072 static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2073 return MVE.Metadata;
2074}
2075
2077 free(Entries);
2078}
2079
2081 LLVMMetadataRef MD) {
2082 unwrap<GlobalObject>(Global)->setMetadata(Kind, unwrap<MDNode>(MD));
2083}
2084
2086 unwrap<GlobalObject>(Global)->eraseMetadata(Kind);
2087}
2088
2090 unwrap<GlobalObject>(Global)->clearMetadata();
2091}
2092
2093/*--.. Operations on global variables ......................................--*/
2094
2096 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2098}
2099
2101 const char *Name,
2102 unsigned AddressSpace) {
2103 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2105 nullptr, GlobalVariable::NotThreadLocal,
2106 AddressSpace));
2107}
2108
2110 return wrap(unwrap(M)->getNamedGlobal(Name));
2111}
2112
2114 Module *Mod = unwrap(M);
2116 if (I == Mod->global_end())
2117 return nullptr;
2118 return wrap(&*I);
2119}
2120
2122 Module *Mod = unwrap(M);
2124 if (I == Mod->global_begin())
2125 return nullptr;
2126 return wrap(&*--I);
2127}
2128
2130 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2132 if (++I == GV->getParent()->global_end())
2133 return nullptr;
2134 return wrap(&*I);
2135}
2136
2138 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2140 if (I == GV->getParent()->global_begin())
2141 return nullptr;
2142 return wrap(&*--I);
2143}
2144
2146 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
2147}
2148
2150 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
2151 if ( !GV->hasInitializer() )
2152 return nullptr;
2153 return wrap(GV->getInitializer());
2154}
2155
2156void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
2157 unwrap<GlobalVariable>(GlobalVar)
2158 ->setInitializer(unwrap<Constant>(ConstantVal));
2159}
2160
2162 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
2163}
2164
2165void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
2166 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
2167}
2168
2170 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
2171}
2172
2173void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
2174 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
2175}
2176
2178 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
2179 case GlobalVariable::NotThreadLocal:
2180 return LLVMNotThreadLocal;
2181 case GlobalVariable::GeneralDynamicTLSModel:
2183 case GlobalVariable::LocalDynamicTLSModel:
2185 case GlobalVariable::InitialExecTLSModel:
2187 case GlobalVariable::LocalExecTLSModel:
2188 return LLVMLocalExecTLSModel;
2189 }
2190
2191 llvm_unreachable("Invalid GlobalVariable thread local mode");
2192}
2193
2195 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2196
2197 switch (Mode) {
2198 case LLVMNotThreadLocal:
2199 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
2200 break;
2202 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
2203 break;
2205 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
2206 break;
2208 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
2209 break;
2211 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
2212 break;
2213 }
2214}
2215
2217 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
2218}
2219
2221 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
2222}
2223
2224/*--.. Operations on aliases ......................................--*/
2225
2227 unsigned AddrSpace, LLVMValueRef Aliasee,
2228 const char *Name) {
2229 return wrap(GlobalAlias::create(unwrap(ValueTy), AddrSpace,
2231 unwrap<Constant>(Aliasee), unwrap(M)));
2232}
2233
2235 const char *Name, size_t NameLen) {
2236 return wrap(unwrap(M)->getNamedAlias(StringRef(Name, NameLen)));
2237}
2238
2240 Module *Mod = unwrap(M);
2242 if (I == Mod->alias_end())
2243 return nullptr;
2244 return wrap(&*I);
2245}
2246
2248 Module *Mod = unwrap(M);
2250 if (I == Mod->alias_begin())
2251 return nullptr;
2252 return wrap(&*--I);
2253}
2254
2256 GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2258 if (++I == Alias->getParent()->alias_end())
2259 return nullptr;
2260 return wrap(&*I);
2261}
2262
2264 GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2266 if (I == Alias->getParent()->alias_begin())
2267 return nullptr;
2268 return wrap(&*--I);
2269}
2270
2272 return wrap(unwrap<GlobalAlias>(Alias)->getAliasee());
2273}
2274
2276 unwrap<GlobalAlias>(Alias)->setAliasee(unwrap<Constant>(Aliasee));
2277}
2278
2279/*--.. Operations on functions .............................................--*/
2280
2282 LLVMTypeRef FunctionTy) {
2283 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
2285}
2286
2288 return wrap(unwrap(M)->getFunction(Name));
2289}
2290
2292 Module *Mod = unwrap(M);
2294 if (I == Mod->end())
2295 return nullptr;
2296 return wrap(&*I);
2297}
2298
2300 Module *Mod = unwrap(M);
2302 if (I == Mod->begin())
2303 return nullptr;
2304 return wrap(&*--I);
2305}
2306
2308 Function *Func = unwrap<Function>(Fn);
2309 Module::iterator I(Func);
2310 if (++I == Func->getParent()->end())
2311 return nullptr;
2312 return wrap(&*I);
2313}
2314
2316 Function *Func = unwrap<Function>(Fn);
2317 Module::iterator I(Func);
2318 if (I == Func->getParent()->begin())
2319 return nullptr;
2320 return wrap(&*--I);
2321}
2322
2324 unwrap<Function>(Fn)->eraseFromParent();
2325}
2326
2328 return unwrap<Function>(Fn)->hasPersonalityFn();
2329}
2330
2332 return wrap(unwrap<Function>(Fn)->getPersonalityFn());
2333}
2334
2336 unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
2337}
2338
2340 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
2341 return F->getIntrinsicID();
2342 return 0;
2343}
2344
2346 assert(ID < llvm::Intrinsic::num_intrinsics && "Intrinsic ID out of range");
2347 return llvm::Intrinsic::ID(ID);
2348}
2349
2351 unsigned ID,
2352 LLVMTypeRef *ParamTypes,
2353 size_t ParamCount) {
2354 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2355 auto IID = llvm_map_to_intrinsic_id(ID);
2356 return wrap(llvm::Intrinsic::getDeclaration(unwrap(Mod), IID, Tys));
2357}
2358
2359const char *LLVMIntrinsicGetName(unsigned ID, size_t *NameLength) {
2360 auto IID = llvm_map_to_intrinsic_id(ID);
2361 auto Str = llvm::Intrinsic::getName(IID);
2362 *NameLength = Str.size();
2363 return Str.data();
2364}
2365
2367 LLVMTypeRef *ParamTypes, size_t ParamCount) {
2368 auto IID = llvm_map_to_intrinsic_id(ID);
2369 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2370 return wrap(llvm::Intrinsic::getType(*unwrap(Ctx), IID, Tys));
2371}
2372
2374 LLVMTypeRef *ParamTypes,
2375 size_t ParamCount,
2376 size_t *NameLength) {
2377 auto IID = llvm_map_to_intrinsic_id(ID);
2378 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2379 auto Str = llvm::Intrinsic::getNameNoUnnamedTypes(IID, Tys);
2380 *NameLength = Str.length();
2381 return strdup(Str.c_str());
2382}
2383
2385 LLVMTypeRef *ParamTypes,
2386 size_t ParamCount,
2387 size_t *NameLength) {
2388 auto IID = llvm_map_to_intrinsic_id(ID);
2389 ArrayRef<Type *> Tys(unwrap(ParamTypes), ParamCount);
2390 auto Str = llvm::Intrinsic::getName(IID, Tys, unwrap(Mod));
2391 *NameLength = Str.length();
2392 return strdup(Str.c_str());
2393}
2394
2395unsigned LLVMLookupIntrinsicID(const char *Name, size_t NameLen) {
2396 return Function::lookupIntrinsicID({Name, NameLen});
2397}
2398
2400 auto IID = llvm_map_to_intrinsic_id(ID);
2402}
2403
2405 return unwrap<Function>(Fn)->getCallingConv();
2406}
2407
2409 return unwrap<Function>(Fn)->setCallingConv(
2410 static_cast<CallingConv::ID>(CC));
2411}
2412
2413const char *LLVMGetGC(LLVMValueRef Fn) {
2414 Function *F = unwrap<Function>(Fn);
2415 return F->hasGC()? F->getGC().c_str() : nullptr;
2416}
2417
2418void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
2419 Function *F = unwrap<Function>(Fn);
2420 if (GC)
2421 F->setGC(GC);
2422 else
2423 F->clearGC();
2424}
2425
2428 unwrap<Function>(F)->addAttributeAtIndex(Idx, unwrap(A));
2429}
2430
2432 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2433 return AS.getNumAttributes();
2434}
2435
2437 LLVMAttributeRef *Attrs) {
2438 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2439 for (auto A : AS)
2440 *Attrs++ = wrap(A);
2441}
2442
2445 unsigned KindID) {
2446 return wrap(unwrap<Function>(F)->getAttributeAtIndex(
2447 Idx, (Attribute::AttrKind)KindID));
2448}
2449
2452 const char *K, unsigned KLen) {
2453 return wrap(
2454 unwrap<Function>(F)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
2455}
2456
2458 unsigned KindID) {
2459 unwrap<Function>(F)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
2460}
2461
2463 const char *K, unsigned KLen) {
2464 unwrap<Function>(F)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
2465}
2466
2468 const char *V) {
2469 Function *Func = unwrap<Function>(Fn);
2470 Attribute Attr = Attribute::get(Func->getContext(), A, V);
2471 Func->addFnAttr(Attr);
2472}
2473
2474/*--.. Operations on parameters ............................................--*/
2475
2477 // This function is strictly redundant to
2478 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
2479 return unwrap<Function>(FnRef)->arg_size();
2480}
2481
2482void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
2483 Function *Fn = unwrap<Function>(FnRef);
2484 for (Argument &A : Fn->args())
2485 *ParamRefs++ = wrap(&A);
2486}
2487
2489 Function *Fn = unwrap<Function>(FnRef);
2490 return wrap(&Fn->arg_begin()[index]);
2491}
2492
2494 return wrap(unwrap<Argument>(V)->getParent());
2495}
2496
2498 Function *Func = unwrap<Function>(Fn);
2499 Function::arg_iterator I = Func->arg_begin();
2500 if (I == Func->arg_end())
2501 return nullptr;
2502 return wrap(&*I);
2503}
2504
2506 Function *Func = unwrap<Function>(Fn);
2507 Function::arg_iterator I = Func->arg_end();
2508 if (I == Func->arg_begin())
2509 return nullptr;
2510 return wrap(&*--I);
2511}
2512
2514 Argument *A = unwrap<Argument>(Arg);
2515 Function *Fn = A->getParent();
2516 if (A->getArgNo() + 1 >= Fn->arg_size())
2517 return nullptr;
2518 return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
2519}
2520
2522 Argument *A = unwrap<Argument>(Arg);
2523 if (A->getArgNo() == 0)
2524 return nullptr;
2525 return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
2526}
2527
2529 Argument *A = unwrap<Argument>(Arg);
2530 A->addAttr(Attribute::getWithAlignment(A->getContext(), Align(align)));
2531}
2532
2533/*--.. Operations on ifuncs ................................................--*/
2534
2536 const char *Name, size_t NameLen,
2537 LLVMTypeRef Ty, unsigned AddrSpace,
2539 return wrap(GlobalIFunc::create(unwrap(Ty), AddrSpace,
2541 StringRef(Name, NameLen),
2542 unwrap<Constant>(Resolver), unwrap(M)));
2543}
2544
2546 const char *Name, size_t NameLen) {
2547 return wrap(unwrap(M)->getNamedIFunc(StringRef(Name, NameLen)));
2548}
2549
2551 Module *Mod = unwrap(M);
2553 if (I == Mod->ifunc_end())
2554 return nullptr;
2555 return wrap(&*I);
2556}
2557
2559 Module *Mod = unwrap(M);
2561 if (I == Mod->ifunc_begin())
2562 return nullptr;
2563 return wrap(&*--I);
2564}
2565
2567 GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2569 if (++I == GIF->getParent()->ifunc_end())
2570 return nullptr;
2571 return wrap(&*I);
2572}
2573
2575 GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2577 if (I == GIF->getParent()->ifunc_begin())
2578 return nullptr;
2579 return wrap(&*--I);
2580}
2581
2583 return wrap(unwrap<GlobalIFunc>(IFunc)->getResolver());
2584}
2585
2587 unwrap<GlobalIFunc>(IFunc)->setResolver(unwrap<Constant>(Resolver));
2588}
2589
2591 unwrap<GlobalIFunc>(IFunc)->eraseFromParent();
2592}
2593
2595 unwrap<GlobalIFunc>(IFunc)->removeFromParent();
2596}
2597
2598/*--.. Operations on basic blocks ..........................................--*/
2599
2601 return wrap(static_cast<Value*>(unwrap(BB)));
2602}
2603
2605 return isa<BasicBlock>(unwrap(Val));
2606}
2607
2609 return wrap(unwrap<BasicBlock>(Val));
2610}
2611
2613 return unwrap(BB)->getName().data();
2614}
2615
2617 return wrap(unwrap(BB)->getParent());
2618}
2619
2621 return wrap(unwrap(BB)->getTerminator());
2622}
2623
2625 return unwrap<Function>(FnRef)->size();
2626}
2627
2629 Function *Fn = unwrap<Function>(FnRef);
2630 for (BasicBlock &BB : *Fn)
2631 *BasicBlocksRefs++ = wrap(&BB);
2632}
2633
2635 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2636}
2637
2639 Function *Func = unwrap<Function>(Fn);
2640 Function::iterator I = Func->begin();
2641 if (I == Func->end())
2642 return nullptr;
2643 return wrap(&*I);
2644}
2645
2647 Function *Func = unwrap<Function>(Fn);
2648 Function::iterator I = Func->end();
2649 if (I == Func->begin())
2650 return nullptr;
2651 return wrap(&*--I);
2652}
2653
2655 BasicBlock *Block = unwrap(BB);
2656 Function::iterator I(Block);
2657 if (++I == Block->getParent()->end())
2658 return nullptr;
2659 return wrap(&*I);
2660}
2661
2663 BasicBlock *Block = unwrap(BB);
2664 Function::iterator I(Block);
2665 if (I == Block->getParent()->begin())
2666 return nullptr;
2667 return wrap(&*--I);
2668}
2669
2671 const char *Name) {
2673}
2674
2676 LLVMBasicBlockRef BB) {
2677 BasicBlock *ToInsert = unwrap(BB);
2678 BasicBlock *CurBB = unwrap(Builder)->GetInsertBlock();
2679 assert(CurBB && "current insertion point is invalid!");
2680 CurBB->getParent()->insert(std::next(CurBB->getIterator()), ToInsert);
2681}
2682
2684 LLVMBasicBlockRef BB) {
2685 unwrap<Function>(Fn)->insert(unwrap<Function>(Fn)->end(), unwrap(BB));
2686}
2687
2689 LLVMValueRef FnRef,
2690 const char *Name) {
2691 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
2692}
2693
2696}
2697
2699 LLVMBasicBlockRef BBRef,
2700 const char *Name) {
2701 BasicBlock *BB = unwrap(BBRef);
2702 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2703}
2704
2706 const char *Name) {
2708}
2709
2711 unwrap(BBRef)->eraseFromParent();
2712}
2713
2715 unwrap(BBRef)->removeFromParent();
2716}
2717
2719 unwrap(BB)->moveBefore(unwrap(MovePos));
2720}
2721
2723 unwrap(BB)->moveAfter(unwrap(MovePos));
2724}
2725
2726/*--.. Operations on instructions ..........................................--*/
2727
2729 return wrap(unwrap<Instruction>(Inst)->getParent());
2730}
2731
2733 BasicBlock *Block = unwrap(BB);
2734 BasicBlock::iterator I = Block->begin();
2735 if (I == Block->end())
2736 return nullptr;
2737 return wrap(&*I);
2738}
2739
2741 BasicBlock *Block = unwrap(BB);
2742 BasicBlock::iterator I = Block->end();
2743 if (I == Block->begin())
2744 return nullptr;
2745 return wrap(&*--I);
2746}
2747
2749 Instruction *Instr = unwrap<Instruction>(Inst);
2750 BasicBlock::iterator I(Instr);
2751 if (++I == Instr->getParent()->end())
2752 return nullptr;
2753 return wrap(&*I);
2754}
2755
2757 Instruction *Instr = unwrap<Instruction>(Inst);
2758 BasicBlock::iterator I(Instr);
2759 if (I == Instr->getParent()->begin())
2760 return nullptr;
2761 return wrap(&*--I);
2762}
2763
2765 unwrap<Instruction>(Inst)->removeFromParent();
2766}
2767
2769 unwrap<Instruction>(Inst)->eraseFromParent();
2770}
2771
2773 unwrap<Instruction>(Inst)->deleteValue();
2774}
2775
2777 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2778 return (LLVMIntPredicate)I->getPredicate();
2779 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2780 if (CE->getOpcode() == Instruction::ICmp)
2781 return (LLVMIntPredicate)CE->getPredicate();
2782 return (LLVMIntPredicate)0;
2783}
2784
2786 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2787 return (LLVMRealPredicate)I->getPredicate();
2788 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2789 if (CE->getOpcode() == Instruction::FCmp)
2790 return (LLVMRealPredicate)CE->getPredicate();
2791 return (LLVMRealPredicate)0;
2792}
2793
2795 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2796 return map_to_llvmopcode(C->getOpcode());
2797 return (LLVMOpcode)0;
2798}
2799
2801 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2802 return wrap(C->clone());
2803 return nullptr;
2804}
2805
2807 Instruction *I = dyn_cast<Instruction>(unwrap(Inst));
2808 return (I && I->isTerminator()) ? wrap(I) : nullptr;
2809}
2810
2812 if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) {
2813 return FPI->arg_size();
2814 }
2815 return unwrap<CallBase>(Instr)->arg_size();
2816}
2817
2818/*--.. Call and invoke instructions ........................................--*/
2819
2821 return unwrap<CallBase>(Instr)->getCallingConv();
2822}
2823
2825 return unwrap<CallBase>(Instr)->setCallingConv(
2826 static_cast<CallingConv::ID>(CC));
2827}
2828
2830 unsigned align) {
2831 auto *Call = unwrap<CallBase>(Instr);
2832 Attribute AlignAttr =
2833 Attribute::getWithAlignment(Call->getContext(), Align(align));
2834 Call->addAttributeAtIndex(Idx, AlignAttr);
2835}
2836
2839 unwrap<CallBase>(C)->addAttributeAtIndex(Idx, unwrap(A));
2840}
2841
2844 auto *Call = unwrap<CallBase>(C);
2845 auto AS = Call->getAttributes().getAttributes(Idx);
2846 return AS.getNumAttributes();
2847}
2848
2850 LLVMAttributeRef *Attrs) {
2851 auto *Call = unwrap<CallBase>(C);
2852 auto AS = Call->getAttributes().getAttributes(Idx);
2853 for (auto A : AS)
2854 *Attrs++ = wrap(A);
2855}
2856
2859 unsigned KindID) {
2860 return wrap(unwrap<CallBase>(C)->getAttributeAtIndex(
2861 Idx, (Attribute::AttrKind)KindID));
2862}
2863
2866 const char *K, unsigned KLen) {
2867 return wrap(
2868 unwrap<CallBase>(C)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
2869}
2870
2872 unsigned KindID) {
2873 unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
2874}
2875
2877 const char *K, unsigned KLen) {
2878 unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
2879}
2880
2882 return wrap(unwrap<CallBase>(Instr)->getCalledOperand());
2883}
2884
2886 return wrap(unwrap<CallBase>(Instr)->getFunctionType());
2887}
2888
2889/*--.. Operations on call instructions (only) ..............................--*/
2890
2892 return unwrap<CallInst>(Call)->isTailCall();
2893}
2894
2895void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
2896 unwrap<CallInst>(Call)->setTailCall(isTailCall);
2897}
2898
2899/*--.. Operations on invoke instructions (only) ............................--*/
2900
2902 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2903}
2904
2906 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2907 return wrap(CRI->getUnwindDest());
2908 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2909 return wrap(CSI->getUnwindDest());
2910 }
2911 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2912}
2913
2915 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2916}
2917
2919 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2920 return CRI->setUnwindDest(unwrap(B));
2921 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2922 return CSI->setUnwindDest(unwrap(B));
2923 }
2924 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2925}
2926
2927/*--.. Operations on terminators ...........................................--*/
2928
2930 return unwrap<Instruction>(Term)->getNumSuccessors();
2931}
2932
2934 return wrap(unwrap<Instruction>(Term)->getSuccessor(i));
2935}
2936
2938 return unwrap<Instruction>(Term)->setSuccessor(i, unwrap(block));
2939}
2940
2941/*--.. Operations on branch instructions (only) ............................--*/
2942
2944 return unwrap<BranchInst>(Branch)->isConditional();
2945}
2946
2948 return wrap(unwrap<BranchInst>(Branch)->getCondition());
2949}
2950
2952 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2953}
2954
2955/*--.. Operations on switch instructions (only) ............................--*/
2956
2958 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2959}
2960
2961/*--.. Operations on alloca instructions (only) ............................--*/
2962
2964 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
2965}
2966
2967/*--.. Operations on gep instructions (only) ...............................--*/
2968
2970 return unwrap<GEPOperator>(GEP)->isInBounds();
2971}
2972
2974 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
2975}
2976
2978 return wrap(unwrap<GEPOperator>(GEP)->getSourceElementType());
2979}
2980
2981/*--.. Operations on phi nodes .............................................--*/
2982
2983void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
2984 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
2985 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
2986 for (unsigned I = 0; I != Count; ++I)
2987 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
2988}
2989
2991 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
2992}
2993
2995 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
2996}
2997
2999 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
3000}
3001
3002/*--.. Operations on extractvalue and insertvalue nodes ....................--*/
3003
3005 auto *I = unwrap(Inst);
3006 if (auto *GEP = dyn_cast<GEPOperator>(I))
3007 return GEP->getNumIndices();
3008 if (auto *EV = dyn_cast<ExtractValueInst>(I))
3009 return EV->getNumIndices();
3010 if (auto *IV = dyn_cast<InsertValueInst>(I))
3011 return IV->getNumIndices();
3013 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
3014}
3015
3016const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
3017 auto *I = unwrap(Inst);
3018 if (auto *EV = dyn_cast<ExtractValueInst>(I))
3019 return EV->getIndices().data();
3020 if (auto *IV = dyn_cast<InsertValueInst>(I))
3021 return IV->getIndices().data();
3023 "LLVMGetIndices applies only to extractvalue and insertvalue!");
3024}
3025
3026
3027/*===-- Instruction builders ----------------------------------------------===*/
3028
3030 return wrap(new IRBuilder<>(*unwrap(C)));
3031}
3032
3035}
3036
3038 LLVMValueRef Instr) {
3039 BasicBlock *BB = unwrap(Block);
3040 auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
3041 unwrap(Builder)->SetInsertPoint(BB, I);
3042}
3043
3045 Instruction *I = unwrap<Instruction>(Instr);
3046 unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
3047}
3048
3050 BasicBlock *BB = unwrap(Block);
3051 unwrap(Builder)->SetInsertPoint(BB);
3052}
3053
3055 return wrap(unwrap(Builder)->GetInsertBlock());
3056}
3057
3059 unwrap(Builder)->ClearInsertionPoint();
3060}
3061
3063 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
3064}
3065
3067 const char *Name) {
3068 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
3069}
3070
3072 delete unwrap(Builder);
3073}
3074
3075/*--.. Metadata builders ...................................................--*/
3076
3078 return wrap(unwrap(Builder)->getCurrentDebugLocation().getAsMDNode());
3079}
3080
3082 if (Loc)
3083 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(unwrap<MDNode>(Loc)));
3084 else
3085 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc());
3086}
3087
3089 MDNode *Loc =
3090 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
3091 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
3092}
3093
3095 LLVMContext &Context = unwrap(Builder)->getContext();
3097 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
3098}
3099
3101 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
3102}
3103
3105 unwrap(Builder)->AddMetadataToInst(unwrap<Instruction>(Inst));
3106}
3107
3109 LLVMMetadataRef FPMathTag) {
3110
3111 unwrap(Builder)->setDefaultFPMathTag(FPMathTag
3112 ? unwrap<MDNode>(FPMathTag)
3113 : nullptr);
3114}
3115
3117 return wrap(unwrap(Builder)->getDefaultFPMathTag());
3118}
3119
3120/*--.. Instruction builders ................................................--*/
3121
3123 return wrap(unwrap(B)->CreateRetVoid());
3124}
3125
3127 return wrap(unwrap(B)->CreateRet(unwrap(V)));
3128}
3129
3131 unsigned N) {
3132 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
3133}
3134
3136 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
3137}
3138
3141 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
3142}
3143
3145 LLVMBasicBlockRef Else, unsigned NumCases) {
3146 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
3147}
3148
3150 unsigned NumDests) {
3151 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
3152}
3153
3155 LLVMValueRef *Args, unsigned NumArgs,
3157 const char *Name) {
3158 return wrap(unwrap(B)->CreateInvoke(unwrap<FunctionType>(Ty), unwrap(Fn),
3159 unwrap(Then), unwrap(Catch),
3160 ArrayRef(unwrap(Args), NumArgs), Name));
3161}
3162
3164 LLVMValueRef PersFn, unsigned NumClauses,
3165 const char *Name) {
3166 // The personality used to live on the landingpad instruction, but now it
3167 // lives on the parent function. For compatibility, take the provided
3168 // personality and put it on the parent function.
3169 if (PersFn)
3170 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
3171 unwrap<Function>(PersFn));
3172 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
3173}
3174
3176 LLVMValueRef *Args, unsigned NumArgs,
3177 const char *Name) {
3178 return wrap(unwrap(B)->CreateCatchPad(unwrap(ParentPad),
3179 ArrayRef(unwrap(Args), NumArgs), Name));
3180}
3181
3183 LLVMValueRef *Args, unsigned NumArgs,
3184 const char *Name) {
3185 if (ParentPad == nullptr) {
3186 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3187 ParentPad = wrap(Constant::getNullValue(Ty));
3188 }
3189 return wrap(unwrap(B)->CreateCleanupPad(
3190 unwrap(ParentPad), ArrayRef(unwrap(Args), NumArgs), Name));
3191}
3192
3194 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
3195}
3196
3198 LLVMBasicBlockRef UnwindBB,
3199 unsigned NumHandlers, const char *Name) {
3200 if (ParentPad == nullptr) {
3201 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3202 ParentPad = wrap(Constant::getNullValue(Ty));
3203 }
3204 return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(UnwindBB),
3205 NumHandlers, Name));
3206}
3207
3209 LLVMBasicBlockRef BB) {
3210 return wrap(unwrap(B)->CreateCatchRet(unwrap<CatchPadInst>(CatchPad),
3211 unwrap(BB)));
3212}
3213
3215 LLVMBasicBlockRef BB) {
3216 return wrap(unwrap(B)->CreateCleanupRet(unwrap<CleanupPadInst>(CatchPad),
3217 unwrap(BB)));
3218}
3219
3221 return wrap(unwrap(B)->CreateUnreachable());
3222}
3223
3225 LLVMBasicBlockRef Dest) {
3226 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
3227}
3228
3230 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
3231}
3232
3233unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
3234 return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
3235}
3236
3238 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
3239}
3240
3242 unwrap<LandingPadInst>(LandingPad)->addClause(unwrap<Constant>(ClauseVal));
3243}
3244
3246 return unwrap<LandingPadInst>(LandingPad)->isCleanup();
3247}
3248
3249void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
3250 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
3251}
3252
3254 unwrap<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Dest));
3255}
3256
3257unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) {
3258 return unwrap<CatchSwitchInst>(CatchSwitch)->getNumHandlers();
3259}
3260
3261void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) {
3262 CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(CatchSwitch);
3263 for (const BasicBlock *H : CSI->handlers())
3264 *Handlers++ = wrap(H);
3265}
3266
3268 return wrap(unwrap<CatchPadInst>(CatchPad)->getCatchSwitch());
3269}
3270
3272 unwrap<CatchPadInst>(CatchPad)
3273 ->setCatchSwitch(unwrap<CatchSwitchInst>(CatchSwitch));
3274}
3275
3276/*--.. Funclets ...........................................................--*/
3277
3279 return wrap(unwrap<FuncletPadInst>(Funclet)->getArgOperand(i));
3280}
3281
3283 unwrap<FuncletPadInst>(Funclet)->setArgOperand(i, unwrap(value));
3284}
3285
3286/*--.. Arithmetic ..........................................................--*/
3287
3289 const char *Name) {
3290 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
3291}
3292
3294 const char *Name) {
3295 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
3296}
3297
3299 const char *Name) {
3300 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
3301}
3302
3304 const char *Name) {
3305 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
3306}
3307
3309 const char *Name) {
3310 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
3311}
3312
3314 const char *Name) {
3315 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
3316}
3317
3319 const char *Name) {
3320 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
3321}
3322
3324 const char *Name) {
3325 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
3326}
3327
3329 const char *Name) {
3330 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
3331}
3332
3334 const char *Name) {
3335 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
3336}
3337
3339 const char *Name) {
3340 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
3341}
3342
3344 const char *Name) {
3345 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
3346}
3347
3349 const char *Name) {
3350 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
3351}
3352
3354 LLVMValueRef RHS, const char *Name) {
3355 return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
3356}
3357
3359 const char *Name) {
3360 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
3361}
3362
3364 LLVMValueRef RHS, const char *Name) {
3365 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
3366}
3367
3369 const char *Name) {
3370 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
3371}
3372
3374 const char *Name) {
3375 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
3376}
3377
3379 const char *Name) {
3380 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
3381}
3382
3384 const char *Name) {
3385 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
3386}
3387
3389 const char *Name) {
3390 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
3391}
3392
3394 const char *Name) {
3395 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
3396}
3397
3399 const char *Name) {
3400 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
3401}
3402
3404 const char *Name) {
3405 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
3406}
3407
3409 const char *Name) {
3410 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
3411}
3412
3414 const char *Name) {
3415 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
3416}
3417
3419 LLVMValueRef LHS, LLVMValueRef RHS,
3420 const char *Name) {
3421 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
3422 unwrap(RHS), Name));
3423}
3424
3426 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
3427}
3428
3430 const char *Name) {
3431 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
3432}
3433
3435 const char *Name) {
3436 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
3437}
3438
3440 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
3441}
3442
3444 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
3445}
3446
3447/*--.. Memory ..............................................................--*/
3448
3450 const char *Name) {
3451 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3452 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3453 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3454 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3455 ITy, unwrap(Ty), AllocSize,
3456 nullptr, nullptr, "");
3457 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3458}
3459
3461 LLVMValueRef Val, const char *Name) {
3462 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3463 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3464 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3465 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3466 ITy, unwrap(Ty), AllocSize,
3467 unwrap(Val), nullptr, "");
3468 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3469}
3470
3472 LLVMValueRef Val, LLVMValueRef Len,
3473 unsigned Align) {
3474 return wrap(unwrap(B)->CreateMemSet(unwrap(Ptr), unwrap(Val), unwrap(Len),
3475 MaybeAlign(Align)));
3476}
3477
3479 LLVMValueRef Dst, unsigned DstAlign,
3480 LLVMValueRef Src, unsigned SrcAlign,
3482 return wrap(unwrap(B)->CreateMemCpy(unwrap(Dst), MaybeAlign(DstAlign),
3483 unwrap(Src), MaybeAlign(SrcAlign),
3484 unwrap(Size)));
3485}
3486
3488 LLVMValueRef Dst, unsigned DstAlign,
3489 LLVMValueRef Src, unsigned SrcAlign,
3491 return wrap(unwrap(B)->CreateMemMove(unwrap(Dst), MaybeAlign(DstAlign),
3492 unwrap(Src), MaybeAlign(SrcAlign),
3493 unwrap(Size)));
3494}
3495
3497 const char *Name) {
3498 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
3499}
3500
3502 LLVMValueRef Val, const char *Name) {
3503 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
3504}
3505
3507 return wrap(unwrap(B)->Insert(
3508 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
3509}
3510
3512 LLVMValueRef PointerVal, const char *Name) {
3513 return wrap(unwrap(B)->CreateLoad(unwrap(Ty), unwrap(PointerVal), Name));
3514}
3515
3517 LLVMValueRef PointerVal) {
3518 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
3519}
3520
3522 switch (Ordering) {
3523 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic;
3524 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered;
3525 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic;
3526 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire;
3527 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release;
3529 return AtomicOrdering::AcquireRelease;
3531 return AtomicOrdering::SequentiallyConsistent;
3532 }
3533
3534 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
3535}
3536
3538 switch (Ordering) {
3539 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic;
3540 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered;
3541 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic;
3542 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire;
3543 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease;
3544 case AtomicOrdering::AcquireRelease:
3546 case AtomicOrdering::SequentiallyConsistent:
3548 }
3549
3550 llvm_unreachable("Invalid AtomicOrdering value!");
3551}
3552
3554 switch (BinOp) {
3570 }
3571
3572 llvm_unreachable("Invalid LLVMAtomicRMWBinOp value!");
3573}
3574
3576 switch (BinOp) {
3592 default: break;
3593 }
3594
3595 llvm_unreachable("Invalid AtomicRMWBinOp value!");
3596}
3597
3598// TODO: Should this and other atomic instructions support building with
3599// "syncscope"?
3601 LLVMBool isSingleThread, const char *Name) {
3602 return wrap(
3603 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
3604 isSingleThread ? SyncScope::SingleThread
3606 Name));
3607}
3608
3610 LLVMValueRef Pointer, LLVMValueRef *Indices,
3611 unsigned NumIndices, const char *Name) {
3612 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3613 return wrap(unwrap(B)->CreateGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3614}
3615
3617 LLVMValueRef Pointer, LLVMValueRef *Indices,
3618 unsigned NumIndices, const char *Name) {
3619 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3620 return wrap(
3621 unwrap(B)->CreateInBoundsGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3622}
3623
3625 LLVMValueRef Pointer, unsigned Idx,
3626 const char *Name) {
3627 return wrap(
3628 unwrap(B)->CreateStructGEP(unwrap(Ty), unwrap(Pointer), Idx, Name));
3629}
3630
3632 const char *Name) {
3633 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
3634}
3635
3637 const char *Name) {
3638 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
3639}
3640
3642 Value *P = unwrap(MemAccessInst);
3643 if (LoadInst *LI = dyn_cast<LoadInst>(P))
3644 return LI->isVolatile();
3645 if (StoreInst *SI = dyn_cast<StoreInst>(P))
3646 return SI->isVolatile();
3647 if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
3648 return AI->isVolatile();
3649 return cast<AtomicCmpXchgInst>(P)->isVolatile();
3650}
3651
3652void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
3653 Value *P = unwrap(MemAccessInst);
3654 if (LoadInst *LI = dyn_cast<LoadInst>(P))
3655 return LI->setVolatile(isVolatile);
3656 if (StoreInst *SI = dyn_cast<StoreInst>(P))
3657 return SI->setVolatile(isVolatile);
3658 if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
3659 return AI->setVolatile(isVolatile);
3660 return cast<AtomicCmpXchgInst>(P)->setVolatile(isVolatile);
3661}
3662
3664 return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->isWeak();
3665}
3666
3667void LLVMSetWeak(LLVMValueRef CmpXchgInst, LLVMBool isWeak) {
3668 return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->setWeak(isWeak);
3669}
3670
3672 Value *P = unwrap(MemAccessInst);
3674 if (LoadInst *LI = dyn_cast<LoadInst>(P))
3675 O = LI->getOrdering();
3676 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
3677 O = SI->getOrdering();
3678 else
3679 O = cast<AtomicRMWInst>(P)->getOrdering();
3680 return mapToLLVMOrdering(O);
3681}
3682
3683void LLVMSetOrdering(LLVMValueRef MemAccessInst,