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
62}
63
64/*===-- Version query -----------------------------------------------------===*/
65
66void LLVMGetVersion(unsigned *Major, unsigned *Minor, unsigned *Patch) {
67 if (Major)
68 *Major = LLVM_VERSION_MAJOR;
69 if (Minor)
70 *Minor = LLVM_VERSION_MINOR;
71 if (Patch)
72 *Patch = LLVM_VERSION_PATCH;
73}
74
75/*===-- Error handling ----------------------------------------------------===*/
76
77char *LLVMCreateMessage(const char *Message) {
78 return strdup(Message);
79}
80
81void LLVMDisposeMessage(char *Message) {
82 free(Message);
83}
84
85
86/*===-- Operations on contexts --------------------------------------------===*/
87
89 static LLVMContext GlobalContext;
90 return GlobalContext;
91}
92
94 return wrap(new LLVMContext());
95}
96
98
100 LLVMDiagnosticHandler Handler,
101 void *DiagnosticContext) {
102 unwrap(C)->setDiagnosticHandlerCallBack(
104 Handler),
105 DiagnosticContext);
106}
107
109 return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>(
110 unwrap(C)->getDiagnosticHandlerCallBack());
111}
112
114 return unwrap(C)->getDiagnosticContext();
115}
116
118 void *OpaqueHandle) {
119 auto YieldCallback =
120 LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
121 unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
122}
123
125 return unwrap(C)->shouldDiscardValueNames();
126}
127
129 unwrap(C)->setDiscardValueNames(Discard);
130}
131
133 delete unwrap(C);
134}
135
137 unsigned SLen) {
138 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
139}
140
141unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) {
143}
144
145unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) {
147}
148
150 return Attribute::AttrKind::EndAttrKinds;
151}
152
154 uint64_t Val) {
155 auto &Ctx = *unwrap(C);
156 auto AttrKind = (Attribute::AttrKind)KindID;
157 return wrap(Attribute::get(Ctx, AttrKind, Val));
158}
159
161 return unwrap(A).getKindAsEnum();
162}
163
165 auto Attr = unwrap(A);
166 if (Attr.isEnumAttribute())
167 return 0;
168 return Attr.getValueAsInt();
169}
170
172 LLVMTypeRef type_ref) {
173 auto &Ctx = *unwrap(C);
174 auto AttrKind = (Attribute::AttrKind)KindID;
175 return wrap(Attribute::get(Ctx, AttrKind, unwrap(type_ref)));
176}
177
179 auto Attr = unwrap(A);
180 return wrap(Attr.getValueAsType());
181}
182
184 const char *K, unsigned KLength,
185 const char *V, unsigned VLength) {
186 return wrap(Attribute::get(*unwrap(C), StringRef(K, KLength),
187 StringRef(V, VLength)));
188}
189
191 unsigned *Length) {
192 auto S = unwrap(A).getKindAsString();
193 *Length = S.size();
194 return S.data();
195}
196
198 unsigned *Length) {
199 auto S = unwrap(A).getValueAsString();
200 *Length = S.size();
201 return S.data();
202}
203
205 auto Attr = unwrap(A);
206 return Attr.isEnumAttribute() || Attr.isIntAttribute();
207}
208
210 return unwrap(A).isStringAttribute();
211}
212
214 return unwrap(A).isTypeAttribute();
215}
216
218 std::string MsgStorage;
219 raw_string_ostream Stream(MsgStorage);
221
222 unwrap(DI)->print(DP);
223 Stream.flush();
224
225 return LLVMCreateMessage(MsgStorage.c_str());
226}
227
229 LLVMDiagnosticSeverity severity;
230
231 switch(unwrap(DI)->getSeverity()) {
232 default:
233 severity = LLVMDSError;
234 break;
235 case DS_Warning:
236 severity = LLVMDSWarning;
237 break;
238 case DS_Remark:
239 severity = LLVMDSRemark;
240 break;
241 case DS_Note:
242 severity = LLVMDSNote;
243 break;
244 }
245
246 return severity;
247}
248
249/*===-- Operations on modules ---------------------------------------------===*/
250
252 return wrap(new Module(ModuleID, getGlobalContext()));
253}
254
257 return wrap(new Module(ModuleID, *unwrap(C)));
258}
259
261 delete unwrap(M);
262}
263
264const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) {
265 auto &Str = unwrap(M)->getModuleIdentifier();
266 *Len = Str.length();
267 return Str.c_str();
268}
269
270void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) {
271 unwrap(M)->setModuleIdentifier(StringRef(Ident, Len));
272}
273
274const char *LLVMGetSourceFileName(LLVMModuleRef M, size_t *Len) {
275 auto &Str = unwrap(M)->getSourceFileName();
276 *Len = Str.length();
277 return Str.c_str();
278}
279
280void LLVMSetSourceFileName(LLVMModuleRef M, const char *Name, size_t Len) {
281 unwrap(M)->setSourceFileName(StringRef(Name, Len));
282}
283
284/*--.. Data layout .........................................................--*/
286 return unwrap(M)->getDataLayoutStr().c_str();
287}
288
290 return LLVMGetDataLayoutStr(M);
291}
292
293void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
294 unwrap(M)->setDataLayout(DataLayoutStr);
295}
296
297/*--.. Target triple .......................................................--*/
299 return unwrap(M)->getTargetTriple().c_str();
300}
301
302void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
303 unwrap(M)->setTargetTriple(Triple);
304}
305
306/*--.. Module flags ........................................................--*/
309 const char *Key;
310 size_t KeyLen;
312};
313
316 switch (Behavior) {
318 return Module::ModFlagBehavior::Error;
320 return Module::ModFlagBehavior::Warning;
322 return Module::ModFlagBehavior::Require;
324 return Module::ModFlagBehavior::Override;
326 return Module::ModFlagBehavior::Append;
328 return Module::ModFlagBehavior::AppendUnique;
329 }
330 llvm_unreachable("Unknown LLVMModuleFlagBehavior");
331}
332
335 switch (Behavior) {
336 case Module::ModFlagBehavior::Error:
338 case Module::ModFlagBehavior::Warning:
340 case Module::ModFlagBehavior::Require:
342 case Module::ModFlagBehavior::Override:
344 case Module::ModFlagBehavior::Append:
346 case Module::ModFlagBehavior::AppendUnique:
348 default:
349 llvm_unreachable("Unhandled Flag Behavior");
350 }
351}
352
355 unwrap(M)->getModuleFlagsMetadata(MFEs);
356
358 safe_malloc(MFEs.size() * sizeof(LLVMOpaqueModuleFlagEntry)));
359 for (unsigned i = 0; i < MFEs.size(); ++i) {
360 const auto &ModuleFlag = MFEs[i];
361 Result[i].Behavior = map_from_llvmModFlagBehavior(ModuleFlag.Behavior);
362 Result[i].Key = ModuleFlag.Key->getString().data();
363 Result[i].KeyLen = ModuleFlag.Key->getString().size();
364 Result[i].Metadata = wrap(ModuleFlag.Val);
365 }
366 *Len = MFEs.size();
367 return Result;
368}
369
371 free(Entries);
372}
373
376 unsigned Index) {
378 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
379 return MFE.Behavior;
380}
381
383 unsigned Index, size_t *Len) {
385 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
386 *Len = MFE.KeyLen;
387 return MFE.Key;
388}
389
391 unsigned Index) {
393 static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
394 return MFE.Metadata;
395}
396
398 const char *Key, size_t KeyLen) {
399 return wrap(unwrap(M)->getModuleFlag({Key, KeyLen}));
400}
401
403 const char *Key, size_t KeyLen,
404 LLVMMetadataRef Val) {
405 unwrap(M)->addModuleFlag(map_to_llvmModFlagBehavior(Behavior),
406 {Key, KeyLen}, unwrap(Val));
407}
408
409/*--.. Printing modules ....................................................--*/
410
412 unwrap(M)->print(errs(), nullptr,
413 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
414}
415
417 char **ErrorMessage) {
418 std::error_code EC;
419 raw_fd_ostream dest(Filename, EC, sys::fs::OF_TextWithCRLF);
420 if (EC) {
421 *ErrorMessage = strdup(EC.message().c_str());
422 return true;
423 }
424
425 unwrap(M)->print(dest, nullptr);
426
427 dest.close();
428
429 if (dest.has_error()) {
430 std::string E = "Error printing to file: " + dest.error().message();
431 *ErrorMessage = strdup(E.c_str());
432 return true;
433 }
434
435 return false;
436}
437
439 std::string buf;
440 raw_string_ostream os(buf);
441
442 unwrap(M)->print(os, nullptr);
443 os.flush();
444
445 return strdup(buf.c_str());
446}
447
448/*--.. Operations on inline assembler ......................................--*/
449void LLVMSetModuleInlineAsm2(LLVMModuleRef M, const char *Asm, size_t Len) {
450 unwrap(M)->setModuleInlineAsm(StringRef(Asm, Len));
451}
452
453void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
454 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
455}
456
457void LLVMAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm, size_t Len) {
458 unwrap(M)->appendModuleInlineAsm(StringRef(Asm, Len));
459}
460
461const char *LLVMGetModuleInlineAsm(LLVMModuleRef M, size_t *Len) {
462 auto &Str = unwrap(M)->getModuleInlineAsm();
463 *Len = Str.length();
464 return Str.c_str();
465}
466
468 size_t AsmStringSize, char *Constraints,
469 size_t ConstraintsSize, LLVMBool HasSideEffects,
470 LLVMBool IsAlignStack,
471 LLVMInlineAsmDialect Dialect, LLVMBool CanThrow) {
473 switch (Dialect) {
476 break;
479 break;
480 }
481 return wrap(InlineAsm::get(unwrap<FunctionType>(Ty),
482 StringRef(AsmString, AsmStringSize),
483 StringRef(Constraints, ConstraintsSize),
484 HasSideEffects, IsAlignStack, AD, CanThrow));
485}
486
487/*--.. Operations on module contexts ......................................--*/
489 return wrap(&unwrap(M)->getContext());
490}
491
492
493/*===-- Operations on types -----------------------------------------------===*/
494
495/*--.. Operations on all types (mostly) ....................................--*/
496
498 switch (unwrap(Ty)->getTypeID()) {
499 case Type::VoidTyID:
500 return LLVMVoidTypeKind;
501 case Type::HalfTyID:
502 return LLVMHalfTypeKind;
503 case Type::BFloatTyID:
504 return LLVMBFloatTypeKind;
505 case Type::FloatTyID:
506 return LLVMFloatTypeKind;
507 case Type::DoubleTyID:
508 return LLVMDoubleTypeKind;
511 case Type::FP128TyID:
512 return LLVMFP128TypeKind;
515 case Type::LabelTyID:
516 return LLVMLabelTypeKind;
520 return LLVMIntegerTypeKind;
523 case Type::StructTyID:
524 return LLVMStructTypeKind;
525 case Type::ArrayTyID:
526 return LLVMArrayTypeKind;
528 return LLVMPointerTypeKind;
530 return LLVMVectorTypeKind;
532 return LLVMX86_MMXTypeKind;
534 return LLVMX86_AMXTypeKind;
535 case Type::TokenTyID:
536 return LLVMTokenTypeKind;
542 llvm_unreachable("Typed pointers are unsupported via the C API");
543 }
544 llvm_unreachable("Unhandled TypeID.");
545}
546
548{
549 return unwrap(Ty)->isSized();
550}
551
553 return wrap(&unwrap(Ty)->getContext());
554}
555
557 return unwrap(Ty)->print(errs(), /*IsForDebug=*/true);
558}
559
561 std::string buf;
562 raw_string_ostream os(buf);
563
564 if (unwrap(Ty))
565 unwrap(Ty)->print(os);
566 else
567 os << "Printing <null> Type";
568
569 os.flush();
570
571 return strdup(buf.c_str());
572}
573
574/*--.. Operations on integer types .........................................--*/
575
578}
581}
584}
587}
590}
593}
595 return wrap(IntegerType::get(*unwrap(C), NumBits));
596}
597
600}
603}
606}
609}
612}
615}
616LLVMTypeRef LLVMIntType(unsigned NumBits) {
618}
619
620unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
621 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
622}
623
624/*--.. Operations on real types ............................................--*/
625
628}
631}
634}
637}
640}
643}
646}
649}
652}
653
656}
659}
662}
665}
668}
671}
674}
677}
680}
681
682/*--.. Operations on function types ........................................--*/
683
685 LLVMTypeRef *ParamTypes, unsigned ParamCount,
686 LLVMBool IsVarArg) {
687 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
688 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
689}
690
692 return unwrap<FunctionType>(FunctionTy)->isVarArg();
693}
694
696 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
697}
698
699unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
700 return unwrap<FunctionType>(FunctionTy)->getNumParams();
701}
702
704 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
705 for (Type *T : Ty->params())
706 *Dest++ = wrap(T);
707}
708
709/*--.. Operations on struct types ..........................................--*/
710
712 unsigned ElementCount, LLVMBool Packed) {
713 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
714 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
715}
716
718 unsigned ElementCount, LLVMBool Packed) {
719 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
720 ElementCount, Packed);
721}
722
724{
725 return wrap(StructType::create(*unwrap(C), Name));
726}
727
729{
730 StructType *Type = unwrap<StructType>(Ty);
731 if (!Type->hasName())
732 return nullptr;
733 return Type->getName().data();
734}
735
736void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
737 unsigned ElementCount, LLVMBool Packed) {
738 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
739 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
740}
741
743 return unwrap<StructType>(StructTy)->getNumElements();
744}
745
747 StructType *Ty = unwrap<StructType>(StructTy);
748 for (Type *T : Ty->elements())
749 *Dest++ = wrap(T);
750}
751
753 StructType *Ty = unwrap<StructType>(StructTy);
754 return wrap(Ty->getTypeAtIndex(i));
755}
756
758 return unwrap<StructType>(StructTy)->isPacked();
759}
760
762 return unwrap<StructType>(StructTy)->isOpaque();
763}
764
766 return unwrap<StructType>(StructTy)->isLiteral();
767}
768
770 return wrap(StructType::getTypeByName(unwrap(M)->getContext(), Name));
771}
772
775}
776
777/*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
778
780 int i = 0;
781 for (auto *T : unwrap(Tp)->subtypes()) {
782 Arr[i] = wrap(T);
783 i++;
784 }
785}
786
788 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
789}
790
792 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
793}
794
796 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
797}
798
800 return unwrap(Ty)->isOpaquePointerTy();
801}
802
804 return wrap(FixedVectorType::get(unwrap(ElementType), ElementCount));
805}
806
808 unsigned ElementCount) {
809 return wrap(ScalableVectorType::get(unwrap(ElementType), ElementCount));
810}
811
813 auto *Ty = unwrap(WrappedTy);
814 if (auto *ATy = dyn_cast<ArrayType>(Ty))
815 return wrap(ATy->getElementType());
816 return wrap(cast<VectorType>(Ty)->getElementType());
817}
818
820 return unwrap(Tp)->getNumContainedTypes();
821}
822
824 return unwrap<ArrayType>(ArrayTy)->getNumElements();
825}
826
828 return unwrap<ArrayType>(ArrayTy)->getNumElements();
829}
830
832 return unwrap<PointerType>(PointerTy)->getAddressSpace();
833}
834
835unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
836 return unwrap<VectorType>(VectorTy)->getElementCount().getKnownMinValue();
837}
838
839/*--.. Operations on other types ...........................................--*/
840
842 return wrap(PointerType::get(*unwrap(C), AddressSpace));
843}
844
846 return wrap(Type::getVoidTy(*unwrap(C)));
847}
849 return wrap(Type::getLabelTy(*unwrap(C)));
850}
852 return wrap(Type::getTokenTy(*unwrap(C)));
853}
855 return wrap(Type::getMetadataTy(*unwrap(C)));
856}
857
860}
863}
864
866 LLVMTypeRef *TypeParams,
867 unsigned TypeParamCount,
868 unsigned *IntParams,
869 unsigned IntParamCount) {
870 ArrayRef<Type *> TypeParamArray(unwrap(TypeParams), TypeParamCount);
871 ArrayRef<unsigned> IntParamArray(IntParams, IntParamCount);
872 return wrap(
873 TargetExtType::get(*unwrap(C), Name, TypeParamArray, IntParamArray));
874}
875
876/*===-- Operations on values ----------------------------------------------===*/
877
878/*--.. Operations on all values ............................................--*/
879
881 return wrap(unwrap(Val)->getType());
882}
883
885 switch(unwrap(Val)->getValueID()) {
886#define LLVM_C_API 1
887#define HANDLE_VALUE(Name) \
888 case Value::Name##Val: \
889 return LLVM##Name##ValueKind;
890#include "llvm/IR/Value.def"
891 default:
893 }
894}
895
896const char *LLVMGetValueName2(LLVMValueRef Val, size_t *Length) {
897 auto *V = unwrap(Val);
898 *Length = V->getName().size();
899 return V->getName().data();
900}
901
902void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen) {
903 unwrap(Val)->setName(StringRef(Name, NameLen));
904}
905
907 return unwrap(Val)->getName().data();
908}
909
910void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
911 unwrap(Val)->setName(Name);
912}
913
915 unwrap(Val)->print(errs(), /*IsForDebug=*/true);
916}
917
919 std::string buf;
920 raw_string_ostream os(buf);
921
922 if (unwrap(Val))
923 unwrap(Val)->print(os);
924 else
925 os << "Printing <null> Value";
926
927 os.flush();
928
929 return strdup(buf.c_str());
930}
931
933 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
934}
935
937 return unwrap<Instruction>(Inst)->hasMetadata();
938}
939
941 auto *I = unwrap<Instruction>(Inst);
942 assert(I && "Expected instruction");
943 if (auto *MD = I->getMetadata(KindID))
944 return wrap(MetadataAsValue::get(I->getContext(), MD));
945 return nullptr;
946}
947
948// MetadataAsValue uses a canonical format which strips the actual MDNode for
949// MDNode with just a single constant value, storing just a ConstantAsMetadata
950// This undoes this canonicalization, reconstructing the MDNode.
952 Metadata *MD = MAV->getMetadata();
953 assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
954 "Expected a metadata node or a canonicalized constant");
955
956 if (MDNode *N = dyn_cast<MDNode>(MD))
957 return N;
958
959 return MDNode::get(MAV->getContext(), MD);
960}
961
962void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
963 MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
964
965 unwrap<Instruction>(Inst)->setMetadata(KindID, N);
966}
967
969 unsigned Kind;
971};
972
975llvm_getMetadata(size_t *NumEntries,
976 llvm::function_ref<void(MetadataEntries &)> AccessMD) {
978 AccessMD(MVEs);
979
981 static_cast<LLVMOpaqueValueMetadataEntry *>(
983 for (unsigned i = 0; i < MVEs.size(); ++i) {
984 const auto &ModuleFlag = MVEs[i];
985 Result[i].Kind = ModuleFlag.first;
986 Result[i].Metadata = wrap(ModuleFlag.second);
987 }
988 *NumEntries = MVEs.size();
989 return Result;
990}
991
994 size_t *NumEntries) {
995 return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
996 Entries.clear();
997 unwrap<Instruction>(Value)->getAllMetadata(Entries);
998 });
999}
1000
1001/*--.. Conversion functions ................................................--*/
1002
1003#define LLVM_DEFINE_VALUE_CAST(name) \
1004 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
1005 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
1006 }
1007
1009
1011 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
1012 if (isa<MDNode>(MD->getMetadata()) ||
1013 isa<ValueAsMetadata>(MD->getMetadata()))
1014 return Val;
1015 return nullptr;
1016}
1017
1019 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
1020 if (isa<ValueAsMetadata>(MD->getMetadata()))
1021 return Val;
1022 return nullptr;
1023}
1024
1026 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
1027 if (isa<MDString>(MD->getMetadata()))
1028 return Val;
1029 return nullptr;
1030}
1031
1032/*--.. Operations on Uses ..................................................--*/
1034 Value *V = unwrap(Val);
1035 Value::use_iterator I = V->use_begin();
1036 if (I == V->use_end())
1037 return nullptr;
1038 return wrap(&*I);
1039}
1040
1042 Use *Next = unwrap(U)->getNext();
1043 if (Next)
1044 return wrap(Next);
1045 return nullptr;
1046}
1047
1049 return wrap(unwrap(U)->getUser());
1050}
1051
1053 return wrap(unwrap(U)->get());
1054}
1055
1056/*--.. Operations on Users .................................................--*/
1057
1059 unsigned Index) {
1060 Metadata *Op = N->getOperand(Index);
1061 if (!Op)
1062 return nullptr;
1063 if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
1064 return wrap(C->getValue());
1065 return wrap(MetadataAsValue::get(Context, Op));
1066}
1067
1069 Value *V = unwrap(Val);
1070 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
1071 if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1072 assert(Index == 0 && "Function-local metadata can only have one operand");
1073 return wrap(L->getValue());
1074 }
1075 return getMDNodeOperandImpl(V->getContext(),
1076 cast<MDNode>(MD->getMetadata()), Index);
1077 }
1078
1079 return wrap(cast<User>(V)->getOperand(Index));
1080}
1081
1083 Value *V = unwrap(Val);
1084 return wrap(&cast<User>(V)->getOperandUse(Index));
1085}
1086
1088 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
1089}
1090
1092 Value *V = unwrap(Val);
1093 if (isa<MetadataAsValue>(V))
1094 return LLVMGetMDNodeNumOperands(Val);
1095
1096 return cast<User>(V)->getNumOperands();
1097}
1098
1099/*--.. Operations on constants of any type .................................--*/
1100
1102 return wrap(Constant::getNullValue(unwrap(Ty)));
1103}
1104
1107}
1108
1110 return wrap(UndefValue::get(unwrap(Ty)));
1111}
1112
1114 return wrap(PoisonValue::get(unwrap(Ty)));
1115}
1116
1118 return isa<Constant>(unwrap(Ty));
1119}
1120
1122 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
1123 return C->isNullValue();
1124 return false;
1125}
1126
1128 return isa<UndefValue>(unwrap(Val));
1129}
1130
1132 return isa<PoisonValue>(unwrap(Val));
1133}
1134
1136 return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
1137}
1138
1139/*--.. Operations on metadata nodes ........................................--*/
1140
1142 size_t SLen) {
1143 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
1144}
1145
1147 size_t Count) {
1148 return wrap(MDNode::get(*unwrap(C), ArrayRef<Metadata*>(unwrap(MDs), Count)));
1149}
1150
1152 unsigned SLen) {
1155 Context, MDString::get(Context, StringRef(Str, SLen))));
1156}
1157
1158LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
1159 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
1160}
1161
1163 unsigned Count) {
1166 for (auto *OV : ArrayRef(Vals, Count)) {
1167 Value *V = unwrap(OV);
1168 Metadata *MD;
1169 if (!V)
1170 MD = nullptr;
1171 else if (auto *C = dyn_cast<Constant>(V))
1173 else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
1174 MD = MDV->getMetadata();
1175 assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
1176 "outside of direct argument to call");
1177 } else {
1178 // This is function-local metadata. Pretend to make an MDNode.
1179 assert(Count == 1 &&
1180 "Expected only one operand to function-local metadata");
1182 }
1183
1184 MDs.push_back(MD);
1185 }
1187}
1188
1189LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
1190 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
1191}
1192
1194 return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
1195}
1196
1198 auto *V = unwrap(Val);
1199 if (auto *C = dyn_cast<Constant>(V))
1201 if (auto *MAV = dyn_cast<MetadataAsValue>(V))
1202 return wrap(MAV->getMetadata());
1203 return wrap(ValueAsMetadata::get(V));
1204}
1205
1206const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
1207 if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
1208 if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
1209 *Length = S->getString().size();
1210 return S->getString().data();
1211 }
1212 *Length = 0;
1213 return nullptr;
1214}
1215
1217 auto *MD = unwrap<MetadataAsValue>(V);
1218 if (isa<ValueAsMetadata>(MD->getMetadata()))
1219 return 1;
1220 return cast<MDNode>(MD->getMetadata())->getNumOperands();
1221}
1222
1224 Module *Mod = unwrap(M);
1226 if (I == Mod->named_metadata_end())
1227 return nullptr;
1228 return wrap(&*I);
1229}
1230
1232 Module *Mod = unwrap(M);
1234 if (I == Mod->named_metadata_begin())
1235 return nullptr;
1236 return wrap(&*--I);
1237}
1238
1240 NamedMDNode *NamedNode = unwrap(NMD);
1242 if (++I == NamedNode->getParent()->named_metadata_end())
1243 return nullptr;
1244 return wrap(&*I);
1245}
1246
1248 NamedMDNode *NamedNode = unwrap(NMD);
1250 if (I == NamedNode->getParent()->named_metadata_begin())
1251 return nullptr;
1252 return wrap(&*--I);
1253}
1254
1256 const char *Name, size_t NameLen) {
1257 return wrap(unwrap(M)->getNamedMetadata(StringRef(Name, NameLen)));
1258}
1259
1261 const char *Name, size_t NameLen) {
1262 return wrap(unwrap(M)->getOrInsertNamedMetadata({Name, NameLen}));
1263}
1264
1265const char *LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NMD, size_t *NameLen) {
1266 NamedMDNode *NamedNode = unwrap(NMD);
1267 *NameLen = NamedNode->getName().size();
1268 return NamedNode->getName().data();
1269}
1270
1272 auto *MD = unwrap<MetadataAsValue>(V);
1273 if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1274 *Dest = wrap(MDV->getValue());
1275 return;
1276 }
1277 const auto *N = cast<MDNode>(MD->getMetadata());
1278 const unsigned numOperands = N->getNumOperands();
1279 LLVMContext &Context = unwrap(V)->getContext();
1280 for (unsigned i = 0; i < numOperands; i++)
1281 Dest[i] = getMDNodeOperandImpl(Context, N, i);
1282}
1283
1285 LLVMMetadataRef Replacement) {
1286 auto *MD = cast<MetadataAsValue>(unwrap(V));
1287 auto *N = cast<MDNode>(MD->getMetadata());
1288 N->replaceOperandWith(Index, unwrap<Metadata>(Replacement));
1289}
1290
1292 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
1293 return N->getNumOperands();
1294 }
1295 return 0;
1296}
1297
1299 LLVMValueRef *Dest) {
1300 NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
1301 if (!N)
1302 return;
1303 LLVMContext &Context = unwrap(M)->getContext();
1304 for (unsigned i=0;i<N->getNumOperands();i++)
1305 Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
1306}
1307
1309 LLVMValueRef Val) {
1310 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
1311 if (!N)
1312 return;
1313 if (!Val)
1314 return;
1315 N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
1316}
1317
1318const char *LLVMGetDebugLocDirectory(LLVMValueRef Val, unsigned *Length) {
1319 if (!Length) return nullptr;
1320 StringRef S;
1321 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1322 if (const auto &DL = I->getDebugLoc()) {
1323 S = DL->getDirectory();
1324 }
1325 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1327 GV->getDebugInfo(GVEs);
1328 if (GVEs.size())
1329 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1330 S = DGV->getDirectory();
1331 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1332 if (const DISubprogram *DSP = F->getSubprogram())
1333 S = DSP->getDirectory();
1334 } else {
1335 assert(0 && "Expected Instruction, GlobalVariable or Function");
1336 return nullptr;
1337 }
1338 *Length = S.size();
1339 return S.data();
1340}
1341
1342const char *LLVMGetDebugLocFilename(LLVMValueRef Val, unsigned *Length) {
1343 if (!Length) return nullptr;
1344 StringRef S;
1345 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1346 if (const auto &DL = I->getDebugLoc()) {
1347 S = DL->getFilename();
1348 }
1349 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1351 GV->getDebugInfo(GVEs);
1352 if (GVEs.size())
1353 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1354 S = DGV->getFilename();
1355 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1356 if (const DISubprogram *DSP = F->getSubprogram())
1357 S = DSP->getFilename();
1358 } else {
1359 assert(0 && "Expected Instruction, GlobalVariable or Function");
1360 return nullptr;
1361 }
1362 *Length = S.size();
1363 return S.data();
1364}
1365
1367 unsigned L = 0;
1368 if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1369 if (const auto &DL = I->getDebugLoc()) {
1370 L = DL->getLine();
1371 }
1372 } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1374 GV->getDebugInfo(GVEs);
1375 if (GVEs.size())
1376 if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1377 L = DGV->getLine();
1378 } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1379 if (const DISubprogram *DSP = F->getSubprogram())
1380 L = DSP->getLine();
1381 } else {
1382 assert(0 && "Expected Instruction, GlobalVariable or Function");
1383 return -1;
1384 }
1385 return L;
1386}
1387
1389 unsigned C = 0;
1390 if (const auto *I = dyn_cast<Instruction>(unwrap(Val)))
1391 if (const auto &DL = I->getDebugLoc())
1392 C = DL->getColumn();
1393 return C;
1394}
1395
1396/*--.. Operations on scalar constants ......................................--*/
1397
1398LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
1399 LLVMBool SignExtend) {
1400 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
1401}
1402
1404 unsigned NumWords,
1405 const uint64_t Words[]) {
1406 IntegerType *Ty = unwrap<IntegerType>(IntTy);
1407 return wrap(ConstantInt::get(
1408 Ty->getContext(), APInt(Ty->getBitWidth(), ArrayRef(Words, NumWords))));
1409}
1410
1412 uint8_t Radix) {
1413 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
1414 Radix));
1415}
1416
1418 unsigned SLen, uint8_t Radix) {
1419 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
1420 Radix));
1421}
1422
1424 return wrap(ConstantFP::get(unwrap(RealTy), N));
1425}
1426
1428 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
1429}
1430
1432 unsigned SLen) {
1433 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
1434}
1435
1436unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
1437 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
1438}
1439
1441 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
1442}
1443
1444double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
1445 ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
1446 Type *Ty = cFP->getType();
1447
1448 if (Ty->isHalfTy() || Ty->isBFloatTy() || Ty->isFloatTy() ||
1449 Ty->isDoubleTy()) {
1450 *LosesInfo = false;
1451 return cFP->getValueAPF().convertToDouble();
1452 }
1453
1454 bool APFLosesInfo;
1455 APFloat APF = cFP->getValueAPF();
1456 APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo);
1457 *LosesInfo = APFLosesInfo;
1458 return APF.convertToDouble();
1459}
1460
1461/*--.. Operations on composite constants ...................................--*/
1462
1464 unsigned Length,
1465 LLVMBool DontNullTerminate) {
1466 /* Inverted the sense of AddNull because ', 0)' is a
1467 better mnemonic for null termination than ', 1)'. */
1469 DontNullTerminate == 0));
1470}
1471
1472LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
1473 LLVMBool DontNullTerminate) {
1475 DontNullTerminate);
1476}
1477
1479 return wrap(unwrap<Constant>(C)->getAggregateElement(Idx));
1480}
1481
1483 return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
1484}
1485
1487 return unwrap<ConstantDataSequential>(C)->isString();
1488}
1489
1490const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1491 StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1492 *Length = Str.size();
1493 return Str.data();
1494}
1495
1497 LLVMValueRef *ConstantVals, unsigned Length) {
1498 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1499 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1500}
1501
1503 uint64_t Length) {
1504 ArrayRef<Constant *> V(unwrap<Constant>(ConstantVals, Length), Length);
1505 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1506}
1507
1509 LLVMValueRef *ConstantVals,
1510 unsigned Count, LLVMBool Packed) {
1511 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1512 return wrap(ConstantStruct::getAnon(*unwrap(C), ArrayRef(Elements, Count),
1513 Packed != 0));
1514}
1515
1516LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
1517 LLVMBool Packed) {
1518 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1519 Packed);
1520}
1521
1523 LLVMValueRef *ConstantVals,
1524 unsigned Count) {
1525 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1526 StructType *Ty = unwrap<StructType>(StructTy);
1527
1528 return wrap(ConstantStruct::get(Ty, ArrayRef(Elements, Count)));
1529}
1530
1531LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
1533 ArrayRef(unwrap<Constant>(ScalarConstantVals, Size), Size)));
1534}
1535
1536/*-- Opcode mapping */
1537
1539{
1540 switch (opcode) {
1541 default: llvm_unreachable("Unhandled Opcode.");
1542#define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
1543#include "llvm/IR/Instruction.def"
1544#undef HANDLE_INST
1545 }
1546}
1547
1549{
1550 switch (code) {
1551#define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
1552#include "llvm/IR/Instruction.def"
1553#undef HANDLE_INST
1554 }
1555 llvm_unreachable("Unhandled Opcode.");
1556}
1557
1558/*--.. Constant expressions ................................................--*/
1559
1561 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
1562}
1563
1566}
1567
1569 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
1570}
1571
1573 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1574}
1575
1577 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
1578}
1579
1581 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
1582}
1583
1584
1586 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
1587}
1588
1590 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
1591 unwrap<Constant>(RHSConstant)));
1592}
1593
1595 LLVMValueRef RHSConstant) {
1596 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
1597 unwrap<Constant>(RHSConstant)));
1598}
1599
1601 LLVMValueRef RHSConstant) {
1602 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
1603 unwrap<Constant>(RHSConstant)));
1604}
1605
1607 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
1608 unwrap<Constant>(RHSConstant)));
1609}
1610
1612 LLVMValueRef RHSConstant) {
1613 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
1614 unwrap<Constant>(RHSConstant)));
1615}
1616
1618 LLVMValueRef RHSConstant) {
1619 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
1620 unwrap<Constant>(RHSConstant)));
1621}
1622
1624 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
1625 unwrap<Constant>(RHSConstant)));
1626}
1627
1629 LLVMValueRef RHSConstant) {
1630 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
1631 unwrap<Constant>(RHSConstant)));
1632}
1633
1635 LLVMValueRef RHSConstant) {
1636 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
1637 unwrap<Constant>(RHSConstant)));
1638}
1639
1641 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
1642 unwrap<Constant>(RHSConstant)));
1643}
1644
1646 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
1647 unwrap<Constant>(RHSConstant)));
1648}
1649
1651 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
1652 unwrap<Constant>(RHSConstant)));
1653}
1654
1656 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1657 return wrap(ConstantExpr::getICmp(Predicate,
1658 unwrap<Constant>(LHSConstant),
1659 unwrap<Constant>(RHSConstant)));
1660}
1661
1663 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1664 return wrap(ConstantExpr::getFCmp(Predicate,
1665 unwrap<Constant>(LHSConstant),
1666 unwrap<Constant>(RHSConstant)));
1667}
1668
1670 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1671 unwrap<Constant>(RHSConstant)));
1672}
1673
1675 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
1676 unwrap<Constant>(RHSConstant)));
1677}
1678
1680 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1681 unwrap<Constant>(RHSConstant)));
1682}
1683
1685 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1686 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1687 NumIndices);
1688 Constant *Val = unwrap<Constant>(ConstantVal);
1689 return wrap(ConstantExpr::getGetElementPtr(unwrap(Ty), Val, IdxList));
1690}
1691
1693 LLVMValueRef *ConstantIndices,
1694 unsigned NumIndices) {
1695 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1696 NumIndices);
1697 Constant *Val = unwrap<Constant>(ConstantVal);
1698 return wrap(ConstantExpr::getInBoundsGetElementPtr(unwrap(Ty), Val, IdxList));
1699}
1700
1702 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1703 unwrap(ToType)));
1704}
1705
1707 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1708 unwrap(ToType)));
1709}
1710
1712 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1713 unwrap(ToType)));
1714}
1715
1717 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1718 unwrap(ToType)));
1719}
1720
1722 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1723 unwrap(ToType)));
1724}
1725
1727 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1728 unwrap(ToType)));
1729}
1730
1732 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1733 unwrap(ToType)));
1734}
1735
1737 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1738 unwrap(ToType)));
1739}
1740
1742 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1743 unwrap(ToType)));
1744}
1745
1747 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1748 unwrap(ToType)));
1749}
1750
1752 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1753 unwrap(ToType)));
1754}
1755
1757 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1758 unwrap(ToType)));
1759}
1760
1762 LLVMTypeRef ToType) {
1763 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1764 unwrap(ToType)));
1765}
1766
1768 LLVMTypeRef ToType) {
1769 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1770 unwrap(ToType)));
1771}
1772
1774 LLVMTypeRef ToType) {
1775 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1776 unwrap(ToType)));
1777}
1778
1780 LLVMTypeRef ToType) {
1781 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1782 unwrap(ToType)));
1783}
1784
1786 LLVMTypeRef ToType) {
1787 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1788 unwrap(ToType)));
1789}
1790
1793 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1794 unwrap(ToType), isSigned));
1795}
1796
1798 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1799 unwrap(ToType)));
1800}
1801
1803 LLVMValueRef IndexConstant) {
1804 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1805 unwrap<Constant>(IndexConstant)));
1806}
1807
1809 LLVMValueRef ElementValueConstant,
1810 LLVMValueRef IndexConstant) {
1811 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1812 unwrap<Constant>(ElementValueConstant),
1813 unwrap<Constant>(IndexConstant)));
1814}
1815
1817 LLVMValueRef VectorBConstant,
1818 LLVMValueRef MaskConstant) {
1819 SmallVector<int, 16> IntMask;
1820 ShuffleVectorInst::getShuffleMask(unwrap<Constant>(MaskConstant), IntMask);
1821 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1822 unwrap<Constant>(VectorBConstant),
1823 IntMask));
1824}
1825
1827 const char *Constraints,
1828 LLVMBool HasSideEffects,
1829 LLVMBool IsAlignStack) {
1830 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1831 Constraints, HasSideEffects, IsAlignStack));
1832}
1833
1835 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1836}
1837
1838/*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1839
1841 return wrap(unwrap<GlobalValue>(Global)->getParent());
1842}
1843
1845 return unwrap<GlobalValue>(Global)->isDeclaration();
1846}
1847
1849 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1851 return LLVMExternalLinkage;
1859 return LLVMWeakAnyLinkage;
1861 return LLVMWeakODRLinkage;
1863 return LLVMAppendingLinkage;
1865 return LLVMInternalLinkage;
1867 return LLVMPrivateLinkage;
1871 return LLVMCommonLinkage;
1872 }
1873
1874 llvm_unreachable("Invalid GlobalValue linkage!");
1875}
1876
1878 GlobalValue *GV = unwrap<GlobalValue>(Global);
1879
1880 switch (Linkage) {
1883 break;
1886 break;
1889 break;
1892 break;
1894 LLVM_DEBUG(
1895 errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1896 "longer supported.");
1897 break;
1898 case LLVMWeakAnyLinkage:
1900 break;
1901 case LLVMWeakODRLinkage:
1903 break;
1906 break;
1909 break;
1910 case LLVMPrivateLinkage:
1912 break;
1915 break;
1918 break;
1920 LLVM_DEBUG(
1921 errs()
1922 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1923 break;
1925 LLVM_DEBUG(
1926 errs()
1927 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1928 break;
1931 break;
1932 case LLVMGhostLinkage:
1933 LLVM_DEBUG(
1934 errs() << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1935 break;
1936 case LLVMCommonLinkage:
1938 break;
1939 }
1940}
1941
1943 // Using .data() is safe because of how GlobalObject::setSection is
1944 // implemented.
1945 return unwrap<GlobalValue>(Global)->getSection().data();
1946}
1947
1948void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1949 unwrap<GlobalObject>(Global)->setSection(Section);
1950}
1951
1953 return static_cast<LLVMVisibility>(
1954 unwrap<GlobalValue>(Global)->getVisibility());
1955}
1956
1958 unwrap<GlobalValue>(Global)
1959 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1960}
1961
1963 return static_cast<LLVMDLLStorageClass>(
1964 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1965}
1966
1968 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1969 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1970}
1971
1973 switch (unwrap<GlobalValue>(Global)->getUnnamedAddr()) {
1974 case GlobalVariable::UnnamedAddr::None:
1975 return LLVMNoUnnamedAddr;
1976 case GlobalVariable::UnnamedAddr::Local:
1977 return LLVMLocalUnnamedAddr;
1978 case GlobalVariable::UnnamedAddr::Global:
1979 return LLVMGlobalUnnamedAddr;
1980 }
1981 llvm_unreachable("Unknown UnnamedAddr kind!");
1982}
1983
1985 GlobalValue *GV = unwrap<GlobalValue>(Global);
1986
1987 switch (UnnamedAddr) {
1988 case LLVMNoUnnamedAddr:
1989 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::None);
1991 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Local);
1993 return GV->setUnnamedAddr(GlobalVariable::UnnamedAddr::Global);
1994 }
1995}
1996
1998 return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
1999}
2000
2002 unwrap<GlobalValue>(Global)->setUnnamedAddr(
2003 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
2004 : GlobalValue::UnnamedAddr::None);
2005}
2006
2008 return wrap(unwrap<GlobalValue>(Global)->getValueType());
2009}
2010
2011/*--.. Operations on global variables, load and store instructions .........--*/
2012
2014 Value *P = unwrap(V);
2015 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2016 return GV->getAlign() ? GV->getAlign()->value() : 0;
2017 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
2018 return AI->getAlign().value();
2019 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2020 return LI->getAlign().value();
2021 if (StoreInst *SI = dyn_cast<StoreInst>(P))
2022 return SI->getAlign().value();
2023 if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2024 return RMWI->getAlign().value();
2025 if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2026 return CXI->getAlign().value();
2027
2029 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, "
2030 "and AtomicCmpXchgInst have alignment");
2031}
2032
2033void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
2034 Value *P = unwrap(V);
2035 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2036 GV->setAlignment(MaybeAlign(Bytes));
2037 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
2038 AI->setAlignment(Align(Bytes));
2039 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
2040 LI->setAlignment(Align(Bytes));
2041 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
2042 SI->setAlignment(Align(Bytes));
2043 else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2044 RMWI->setAlignment(Align(Bytes));
2045 else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2046 CXI->setAlignment(Align(Bytes));
2047 else
2049 "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, and "
2050 "and AtomicCmpXchgInst have alignment");
2051}
2052
2054 size_t *NumEntries) {
2055 return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
2056 Entries.clear();
2057 if (Instruction *Instr = dyn_cast<Instruction>(unwrap(Value))) {
2058 Instr->getAllMetadata(Entries);
2059 } else {
2060 unwrap<GlobalObject>(Value)->getAllMetadata(Entries);
2061 }
2062 });
2063}
2064
2066 unsigned Index) {
2068 static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2069 return MVE.Kind;
2070}
2071
2074 unsigned Index) {
2076 static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2077 return MVE.Metadata;
2078}
2079
2081 free(Entries);
2082}
2083
2085 LLVMMetadataRef MD) {
2086 unwrap<GlobalObject>(Global)->setMetadata(Kind, unwrap<MDNode>(MD));
2087}
2088
2090 unwrap<GlobalObject>(Global)->eraseMetadata(Kind);
2091}
2092
2094 unwrap<GlobalObject>(Global)->clearMetadata();
2095}
2096
2097/*--.. Operations on global variables ......................................--*/
2098
2100 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2102}
2103
2105 const char *Name,
2106 unsigned AddressSpace) {
2107 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2109 nullptr, GlobalVariable::NotThreadLocal,
2110 AddressSpace));
2111}
2112
2114 return wrap(unwrap(M)->getNamedGlobal(Name));
2115}
2116
2118 Module *Mod = unwrap(M);
2120 if (I == Mod->global_end())
2121 return nullptr;
2122 return wrap(&*I);
2123}
2124
2126 Module *Mod = unwrap(M);
2128 if (I == Mod->global_begin())
2129 return nullptr;
2130 return wrap(&*--I);
2131}
2132
2134 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2136 if (++I == GV->getParent()->global_end())
2137 return nullptr;
2138 return wrap(&*I);
2139}
2140
2142 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2144 if (I == GV->getParent()->global_begin())
2145 return nullptr;
2146 return wrap(&*--I);
2147}
2148
2150 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
2151}
2152
2154 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
2155 if ( !GV->hasInitializer() )
2156 return nullptr;
2157 return wrap(GV->getInitializer());
2158}
2159
2160void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
2161 unwrap<GlobalVariable>(GlobalVar)
2162 ->setInitializer(unwrap<Constant>(ConstantVal));
2163}
2164
2166 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
2167}
2168
2169void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
2170 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
2171}
2172
2174 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
2175}
2176
2177void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
2178 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
2179}
2180
2182 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
2183 case GlobalVariable::NotThreadLocal:
2184 return LLVMNotThreadLocal;
2185 case GlobalVariable::GeneralDynamicTLSModel:
2187 case GlobalVariable::LocalDynamicTLSModel:
2189 case GlobalVariable::InitialExecTLSModel:
2191 case GlobalVariable::LocalExecTLSModel:
2192 return LLVMLocalExecTLSModel;
2193 }
2194
2195 llvm_unreachable("Invalid GlobalVariable thread local mode");
2196}
2197
2199 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2200
2201 switch (Mode) {
2202 case LLVMNotThreadLocal:
2203 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
2204 break;
2206 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
2207 break;
2209 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
2210 break;
2212 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
2213 break;
2215 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
2216 break;
2217 }
2218}
2219
2221 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
2222}
2223
2225 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
2226}
2227
2228/*--.. Operations on aliases ......................................--*/
2229
2231 unsigned AddrSpace, LLVMValueRef Aliasee,
2232 const char *Name) {
2233 return wrap(GlobalAlias::create(unwrap(ValueTy), AddrSpace,
2235 unwrap<Constant>(Aliasee), unwrap(M)));
2236}
2237
2239 const char *Name, size_t NameLen) {
2240 return wrap(unwrap(M)->getNamedAlias(StringRef(Name, NameLen)));
2241}
2242
2244 Module *Mod = unwrap(M);
2246 if (I == Mod->alias_end())
2247 return nullptr;
2248 return wrap(&*I);
2249}
2250
2252 Module *Mod = unwrap(M);
2254 if (I == Mod->alias_begin())
2255 return nullptr;
2256 return wrap(&*--I);
2257}
2258
2260 GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2262 if (++I == Alias->getParent()->alias_end())
2263 return nullptr;
2264 return wrap(&*I);
2265}
2266
2268 GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2270 if (I == Alias->getParent()->alias_begin())
2271 return nullptr;
2272 return wrap(&*--I);
2273}
2274
2276 return wrap(unwrap<GlobalAlias>(Alias)->getAliasee());
2277}
2278
2280 unwrap<GlobalAlias>(Alias)->setAliasee(unwrap<Constant>(Aliasee));
2281}
2282
2283/*--.. Operations on functions .............................................--*/
2284
2286 LLVMTypeRef FunctionTy) {
2287 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
2289}
2290
2292 return wrap(unwrap(M)->getFunction(Name));
2293}
2294
2296 Module *Mod = unwrap(M);
2298 if (I == Mod->end())
2299 return nullptr;
2300 return wrap(&*I);
2301}
2302
2304 Module *Mod = unwrap(M);
2306 if (I == Mod->begin())
2307 return nullptr;
2308 return wrap(&*--I);
2309}
2310
2312 Function *Func = unwrap<Function>(Fn);
2313 Module::iterator I(Func);
2314 if (++I == Func->getParent()->end())
2315 return nullptr;
2316 return wrap(&*I);
2317}
2318
2320 Function *Func = unwrap<Function>(Fn);
2321 Module::iterator I(Func);
2322 if (I == Func->getParent()->begin())
2323 return nullptr;
2324 return wrap(&*--I);
2325}
2326
2328 unwrap<Function>(Fn)->eraseFromParent();
2329}
2330
2332 return unwrap<Function>(Fn)->hasPersonalityFn();
2333}
2334
2336 return wrap(unwrap<Function>(Fn)->getPersonalityFn());
2337}
2338
2340 unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
2341}
2342
2344 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
2345 return F->getIntrinsicID();
2346 return 0;
2347}
2348
2350 assert(ID < llvm::Intrinsic::num_intrinsics && "Intrinsic ID out of range");
2351 return llvm::Intrinsic::ID(ID);
2352}
2353
2355 unsigned ID,
2356 LLVMTypeRef *ParamTypes,
2357 size_t ParamCount) {
2358 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2359 auto IID = llvm_map_to_intrinsic_id(ID);
2360 return wrap(llvm::Intrinsic::getDeclaration(unwrap(Mod), IID, Tys));
2361}
2362
2363const char *LLVMIntrinsicGetName(unsigned ID, size_t *NameLength) {
2364 auto IID = llvm_map_to_intrinsic_id(ID);
2365 auto Str = llvm::Intrinsic::getName(IID);
2366 *NameLength = Str.size();
2367 return Str.data();
2368}
2369
2371 LLVMTypeRef *ParamTypes, size_t ParamCount) {
2372 auto IID = llvm_map_to_intrinsic_id(ID);
2373 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2374 return wrap(llvm::Intrinsic::getType(*unwrap(Ctx), IID, Tys));
2375}
2376
2378 LLVMTypeRef *ParamTypes,
2379 size_t ParamCount,
2380 size_t *NameLength) {
2381 auto IID = llvm_map_to_intrinsic_id(ID);
2382 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2383 auto Str = llvm::Intrinsic::getNameNoUnnamedTypes(IID, Tys);
2384 *NameLength = Str.length();
2385 return strdup(Str.c_str());
2386}
2387
2389 LLVMTypeRef *ParamTypes,
2390 size_t ParamCount,
2391 size_t *NameLength) {
2392 auto IID = llvm_map_to_intrinsic_id(ID);
2393 ArrayRef<Type *> Tys(unwrap(ParamTypes), ParamCount);
2394 auto Str = llvm::Intrinsic::getName(IID, Tys, unwrap(Mod));
2395 *NameLength = Str.length();
2396 return strdup(Str.c_str());
2397}
2398
2399unsigned LLVMLookupIntrinsicID(const char *Name, size_t NameLen) {
2400 return Function::lookupIntrinsicID({Name, NameLen});
2401}
2402
2404 auto IID = llvm_map_to_intrinsic_id(ID);
2406}
2407
2409 return unwrap<Function>(Fn)->getCallingConv();
2410}
2411
2413 return unwrap<Function>(Fn)->setCallingConv(
2414 static_cast<CallingConv::ID>(CC));
2415}
2416
2417const char *LLVMGetGC(LLVMValueRef Fn) {
2418 Function *F = unwrap<Function>(Fn);
2419 return F->hasGC()? F->getGC().c_str() : nullptr;
2420}
2421
2422void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
2423 Function *F = unwrap<Function>(Fn);
2424 if (GC)
2425 F->setGC(GC);
2426 else
2427 F->clearGC();
2428}
2429
2432 unwrap<Function>(F)->addAttributeAtIndex(Idx, unwrap(A));
2433}
2434
2436 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2437 return AS.getNumAttributes();
2438}
2439
2441 LLVMAttributeRef *Attrs) {
2442 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2443 for (auto A : AS)
2444 *Attrs++ = wrap(A);
2445}
2446
2449 unsigned KindID) {
2450 return wrap(unwrap<Function>(F)->getAttributeAtIndex(
2451 Idx, (Attribute::AttrKind)KindID));
2452}
2453
2456 const char *K, unsigned KLen) {
2457 return wrap(
2458 unwrap<Function>(F)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
2459}
2460
2462 unsigned KindID) {
2463 unwrap<Function>(F)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
2464}
2465
2467 const char *K, unsigned KLen) {
2468 unwrap<Function>(F)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
2469}
2470
2472 const char *V) {
2473 Function *Func = unwrap<Function>(Fn);
2474 Attribute Attr = Attribute::get(Func->getContext(), A, V);
2475 Func->addFnAttr(Attr);
2476}
2477
2478/*--.. Operations on parameters ............................................--*/
2479
2481 // This function is strictly redundant to
2482 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
2483 return unwrap<Function>(FnRef)->arg_size();
2484}
2485
2486void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
2487 Function *Fn = unwrap<Function>(FnRef);
2488 for (Argument &A : Fn->args())
2489 *ParamRefs++ = wrap(&A);
2490}
2491
2493 Function *Fn = unwrap<Function>(FnRef);
2494 return wrap(&Fn->arg_begin()[index]);
2495}
2496
2498 return wrap(unwrap<Argument>(V)->getParent());
2499}
2500
2502 Function *Func = unwrap<Function>(Fn);
2503 Function::arg_iterator I = Func->arg_begin();
2504 if (I == Func->arg_end())
2505 return nullptr;
2506 return wrap(&*I);
2507}
2508
2510 Function *Func = unwrap<Function>(Fn);
2511 Function::arg_iterator I = Func->arg_end();
2512 if (I == Func->arg_begin())
2513 return nullptr;
2514 return wrap(&*--I);
2515}
2516
2518 Argument *A = unwrap<Argument>(Arg);
2519 Function *Fn = A->getParent();
2520 if (A->getArgNo() + 1 >= Fn->arg_size())
2521 return nullptr;
2522 return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
2523}
2524
2526 Argument *A = unwrap<Argument>(Arg);
2527 if (A->getArgNo() == 0)
2528 return nullptr;
2529 return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
2530}
2531
2533 Argument *A = unwrap<Argument>(Arg);
2534 A->addAttr(Attribute::getWithAlignment(A->getContext(), Align(align)));
2535}
2536
2537/*--.. Operations on ifuncs ................................................--*/
2538
2540 const char *Name, size_t NameLen,
2541 LLVMTypeRef Ty, unsigned AddrSpace,
2543 return wrap(GlobalIFunc::create(unwrap(Ty), AddrSpace,
2545 StringRef(Name, NameLen),
2546 unwrap<Constant>(Resolver), unwrap(M)));
2547}
2548
2550 const char *Name, size_t NameLen) {
2551 return wrap(unwrap(M)->getNamedIFunc(StringRef(Name, NameLen)));
2552}
2553
2555 Module *Mod = unwrap(M);
2557 if (I == Mod->ifunc_end())
2558 return nullptr;
2559 return wrap(&*I);
2560}
2561
2563 Module *Mod = unwrap(M);
2565 if (I == Mod->ifunc_begin())
2566 return nullptr;
2567 return wrap(&*--I);
2568}
2569
2571 GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2573 if (++I == GIF->getParent()->ifunc_end())
2574 return nullptr;
2575 return wrap(&*I);
2576}
2577
2579 GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2581 if (I == GIF->getParent()->ifunc_begin())
2582 return nullptr;
2583 return wrap(&*--I);
2584}
2585
2587 return wrap(unwrap<GlobalIFunc>(IFunc)->getResolver());
2588}
2589
2591 unwrap<GlobalIFunc>(IFunc)->setResolver(unwrap<Constant>(Resolver));
2592}
2593
2595 unwrap<GlobalIFunc>(IFunc)->eraseFromParent();
2596}
2597
2599 unwrap<GlobalIFunc>(IFunc)->removeFromParent();
2600}
2601
2602/*--.. Operations on basic blocks ..........................................--*/
2603
2605 return wrap(static_cast<Value*>(unwrap(BB)));
2606}
2607
2609 return isa<BasicBlock>(unwrap(Val));
2610}
2611
2613 return wrap(unwrap<BasicBlock>(Val));
2614}
2615
2617 return unwrap(BB)->getName().data();
2618}
2619
2621 return wrap(unwrap(BB)->getParent());
2622}
2623
2625 return wrap(unwrap(BB)->getTerminator());
2626}
2627
2629 return unwrap<Function>(FnRef)->size();
2630}
2631
2633 Function *Fn = unwrap<Function>(FnRef);
2634 for (BasicBlock &BB : *Fn)
2635 *BasicBlocksRefs++ = wrap(&BB);
2636}
2637
2639 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2640}
2641
2643 Function *Func = unwrap<Function>(Fn);
2644 Function::iterator I = Func->begin();
2645 if (I == Func->end())
2646 return nullptr;
2647 return wrap(&*I);
2648}
2649
2651 Function *Func = unwrap<Function>(Fn);
2652 Function::iterator I = Func->end();
2653 if (I == Func->begin())
2654 return nullptr;
2655 return wrap(&*--I);
2656}
2657
2659 BasicBlock *Block = unwrap(BB);
2660 Function::iterator I(Block);
2661 if (++I == Block->getParent()->end())
2662 return nullptr;
2663 return wrap(&*I);
2664}
2665
2667 BasicBlock *Block = unwrap(BB);
2668 Function::iterator I(Block);
2669 if (I == Block->getParent()->begin())
2670 return nullptr;
2671 return wrap(&*--I);
2672}
2673
2675 const char *Name) {
2677}
2678
2680 LLVMBasicBlockRef BB) {
2681 BasicBlock *ToInsert = unwrap(BB);
2682 BasicBlock *CurBB = unwrap(Builder)->GetInsertBlock();
2683 assert(CurBB && "current insertion point is invalid!");
2684 CurBB->getParent()->insert(std::next(CurBB->getIterator()), ToInsert);
2685}
2686
2688 LLVMBasicBlockRef BB) {
2689 unwrap<Function>(Fn)->insert(unwrap<Function>(Fn)->end(), unwrap(BB));
2690}
2691
2693 LLVMValueRef FnRef,
2694 const char *Name) {
2695 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
2696}
2697
2700}
2701
2703 LLVMBasicBlockRef BBRef,
2704 const char *Name) {
2705 BasicBlock *BB = unwrap(BBRef);
2706 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2707}
2708
2710 const char *Name) {
2712}
2713
2715 unwrap(BBRef)->eraseFromParent();
2716}
2717
2719 unwrap(BBRef)->removeFromParent();
2720}
2721
2723 unwrap(BB)->moveBefore(unwrap(MovePos));
2724}
2725
2727 unwrap(BB)->moveAfter(unwrap(MovePos));
2728}
2729
2730/*--.. Operations on instructions ..........................................--*/
2731
2733 return wrap(unwrap<Instruction>(Inst)->getParent());
2734}
2735
2737 BasicBlock *Block = unwrap(BB);
2738 BasicBlock::iterator I = Block->begin();
2739 if (I == Block->end())
2740 return nullptr;
2741 return wrap(&*I);
2742}
2743
2745 BasicBlock *Block = unwrap(BB);
2746 BasicBlock::iterator I = Block->end();
2747 if (I == Block->begin())
2748 return nullptr;
2749 return wrap(&*--I);
2750}
2751
2753 Instruction *Instr = unwrap<Instruction>(Inst);
2754 BasicBlock::iterator I(Instr);
2755 if (++I == Instr->getParent()->end())
2756 return nullptr;
2757 return wrap(&*I);
2758}
2759
2761 Instruction *Instr = unwrap<Instruction>(Inst);
2762 BasicBlock::iterator I(Instr);
2763 if (I == Instr->getParent()->begin())
2764 return nullptr;
2765 return wrap(&*--I);
2766}
2767
2769 unwrap<Instruction>(Inst)->removeFromParent();
2770}
2771
2773 unwrap<Instruction>(Inst)->eraseFromParent();
2774}
2775
2777 unwrap<Instruction>(Inst)->deleteValue();
2778}
2779
2781 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2782 return (LLVMIntPredicate)I->getPredicate();
2783 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2784 if (CE->getOpcode() == Instruction::ICmp)
2785 return (LLVMIntPredicate)CE->getPredicate();
2786 return (LLVMIntPredicate)0;
2787}
2788
2790 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2791 return (LLVMRealPredicate)I->getPredicate();
2792 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2793 if (CE->getOpcode() == Instruction::FCmp)
2794 return (LLVMRealPredicate)CE->getPredicate();
2795 return (LLVMRealPredicate)0;
2796}
2797
2799 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2800 return map_to_llvmopcode(C->getOpcode());
2801 return (LLVMOpcode)0;
2802}
2803
2805 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2806 return wrap(C->clone());
2807 return nullptr;
2808}
2809
2811 Instruction *I = dyn_cast<Instruction>(unwrap(Inst));
2812 return (I && I->isTerminator()) ? wrap(I) : nullptr;
2813}
2814
2816 if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) {
2817 return FPI->arg_size();
2818 }
2819 return unwrap<CallBase>(Instr)->arg_size();
2820}
2821
2822/*--.. Call and invoke instructions ........................................--*/
2823
2825 return unwrap<CallBase>(Instr)->getCallingConv();
2826}
2827
2829 return unwrap<CallBase>(Instr)->setCallingConv(
2830 static_cast<CallingConv::ID>(CC));
2831}
2832
2834 unsigned align) {
2835 auto *Call = unwrap<CallBase>(Instr);
2836 Attribute AlignAttr =
2837 Attribute::getWithAlignment(Call->getContext(), Align(align));
2838 Call->addAttributeAtIndex(Idx, AlignAttr);
2839}
2840
2843 unwrap<CallBase>(C)->addAttributeAtIndex(Idx, unwrap(A));
2844}
2845
2848 auto *Call = unwrap<CallBase>(C);
2849 auto AS = Call->getAttributes().getAttributes(Idx);
2850 return AS.getNumAttributes();
2851}
2852
2854 LLVMAttributeRef *Attrs) {
2855 auto *Call = unwrap<CallBase>(C);
2856 auto AS = Call->getAttributes().getAttributes(Idx);
2857 for (auto A : AS)
2858 *Attrs++ = wrap(A);
2859}
2860
2863 unsigned KindID) {
2864 return wrap(unwrap<CallBase>(C)->getAttributeAtIndex(
2865 Idx, (Attribute::AttrKind)KindID));
2866}
2867
2870 const char *K, unsigned KLen) {
2871 return wrap(
2872 unwrap<CallBase>(C)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
2873}
2874
2876 unsigned KindID) {
2877 unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
2878}
2879
2881 const char *K, unsigned KLen) {
2882 unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
2883}
2884
2886 return wrap(unwrap<CallBase>(Instr)->getCalledOperand());
2887}
2888
2890 return wrap(unwrap<CallBase>(Instr)->getFunctionType());
2891}
2892
2893/*--.. Operations on call instructions (only) ..............................--*/
2894
2896 return unwrap<CallInst>(Call)->isTailCall();
2897}
2898
2899void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
2900 unwrap<CallInst>(Call)->setTailCall(isTailCall);
2901}
2902
2903/*--.. Operations on invoke instructions (only) ............................--*/
2904
2906 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2907}
2908
2910 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2911 return wrap(CRI->getUnwindDest());
2912 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2913 return wrap(CSI->getUnwindDest());
2914 }
2915 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2916}
2917
2919 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2920}
2921
2923 if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2924 return CRI->setUnwindDest(unwrap(B));
2925 } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2926 return CSI->setUnwindDest(unwrap(B));
2927 }
2928 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2929}
2930
2931/*--.. Operations on terminators ...........................................--*/
2932
2934 return unwrap<Instruction>(Term)->getNumSuccessors();
2935}
2936
2938 return wrap(unwrap<Instruction>(Term)->getSuccessor(i));
2939}
2940
2942 return unwrap<Instruction>(Term)->setSuccessor(i, unwrap(block));
2943}
2944
2945/*--.. Operations on branch instructions (only) ............................--*/
2946
2948 return unwrap<BranchInst>(Branch)->isConditional();
2949}
2950
2952 return wrap(unwrap<BranchInst>(Branch)->getCondition());
2953}
2954
2956 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2957}
2958
2959/*--.. Operations on switch instructions (only) ............................--*/
2960
2962 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2963}
2964
2965/*--.. Operations on alloca instructions (only) ............................--*/
2966
2968 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
2969}
2970
2971/*--.. Operations on gep instructions (only) ...............................--*/
2972
2974 return unwrap<GEPOperator>(GEP)->isInBounds();
2975}
2976
2978 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
2979}
2980
2982 return wrap(unwrap<GEPOperator>(GEP)->getSourceElementType());
2983}
2984
2985/*--.. Operations on phi nodes .............................................--*/
2986
2987void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
2988 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
2989 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
2990 for (unsigned I = 0; I != Count; ++I)
2991 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
2992}
2993
2995 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
2996}
2997
2999 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
3000}
3001
3003 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
3004}
3005
3006/*--.. Operations on extractvalue and insertvalue nodes ....................--*/
3007
3009 auto *I = unwrap(Inst);
3010 if (auto *GEP = dyn_cast<GEPOperator>(I))
3011 return GEP->getNumIndices();
3012 if (auto *EV = dyn_cast<ExtractValueInst>(I))
3013 return EV->getNumIndices();
3014 if (auto *IV = dyn_cast<InsertValueInst>(I))
3015 return IV->getNumIndices();
3017 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
3018}
3019
3020const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
3021 auto *I = unwrap(Inst);
3022 if (auto *EV = dyn_cast<ExtractValueInst>(I))
3023 return EV->getIndices().data();
3024 if (auto *IV = dyn_cast<InsertValueInst>(I))
3025 return IV->getIndices().data();
3027 "LLVMGetIndices applies only to extractvalue and insertvalue!");
3028}
3029
3030
3031/*===-- Instruction builders ----------------------------------------------===*/
3032
3034 return wrap(new IRBuilder<>(*unwrap(C)));
3035}
3036
3039}
3040
3042 LLVMValueRef Instr) {
3043 BasicBlock *BB = unwrap(Block);
3044 auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
3045 unwrap(Builder)->SetInsertPoint(BB, I);
3046}
3047
3049 Instruction *I = unwrap<Instruction>(Instr);
3050 unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
3051}
3052
3054 BasicBlock *BB = unwrap(Block);
3055 unwrap(Builder)->SetInsertPoint(BB);
3056}
3057
3059 return wrap(unwrap(Builder)->GetInsertBlock());
3060}
3061
3063 unwrap(Builder)->ClearInsertionPoint();
3064}
3065
3067 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
3068}
3069
3071 const char *Name) {
3072 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
3073}
3074
3076 delete unwrap(Builder);
3077}
3078
3079/*--.. Metadata builders ...................................................--*/
3080
3082 return wrap(unwrap(Builder)->getCurrentDebugLocation().getAsMDNode());
3083}
3084
3086 if (Loc)
3087 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(unwrap<MDNode>(Loc)));
3088 else
3089 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc());
3090}
3091
3093 MDNode *Loc =
3094 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
3095 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
3096}
3097
3099 LLVMContext &Context = unwrap(Builder)->getContext();
3101 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
3102}
3103
3105 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
3106}
3107
3109 unwrap(Builder)->AddMetadataToInst(unwrap<Instruction>(Inst));
3110}
3111
3113 LLVMMetadataRef FPMathTag) {
3114
3115 unwrap(Builder)->setDefaultFPMathTag(FPMathTag
3116 ? unwrap<MDNode>(FPMathTag)
3117 : nullptr);
3118}
3119
3121 return wrap(unwrap(Builder)->getDefaultFPMathTag());
3122}
3123
3124/*--.. Instruction builders ................................................--*/
3125
3127 return wrap(unwrap(B)->CreateRetVoid());
3128}
3129
3131 return wrap(unwrap(B)->CreateRet(unwrap(V)));
3132}
3133
3135 unsigned N) {
3136 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
3137}
3138
3140 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
3141}
3142
3145 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
3146}
3147
3149 LLVMBasicBlockRef Else, unsigned NumCases) {
3150 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
3151}
3152
3154 unsigned NumDests) {
3155 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
3156}
3157
3159 LLVMValueRef *Args, unsigned NumArgs,
3161 const char *Name) {
3162 return wrap(unwrap(B)->CreateInvoke(unwrap<FunctionType>(Ty), unwrap(Fn),
3163 unwrap(Then), unwrap(Catch),
3164 ArrayRef(unwrap(Args), NumArgs), Name));
3165}
3166
3168 LLVMValueRef PersFn, unsigned NumClauses,
3169 const char *Name) {
3170 // The personality used to live on the landingpad instruction, but now it
3171 // lives on the parent function. For compatibility, take the provided
3172 // personality and put it on the parent function.
3173 if (PersFn)
3174 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
3175 unwrap<Function>(PersFn));
3176 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
3177}
3178
3180 LLVMValueRef *Args, unsigned NumArgs,
3181 const char *Name) {
3182 return wrap(unwrap(B)->CreateCatchPad(unwrap(ParentPad),
3183 ArrayRef(unwrap(Args), NumArgs), Name));
3184}
3185
3187 LLVMValueRef *Args, unsigned NumArgs,
3188 const char *Name) {
3189 if (ParentPad == nullptr) {
3190 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3191 ParentPad = wrap(Constant::getNullValue(Ty));
3192 }
3193 return wrap(unwrap(B)->CreateCleanupPad(
3194 unwrap(ParentPad), ArrayRef(unwrap(Args), NumArgs), Name));
3195}
3196
3198 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
3199}
3200
3202 LLVMBasicBlockRef UnwindBB,
3203 unsigned NumHandlers, const char *Name) {
3204 if (ParentPad == nullptr) {
3205 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3206 ParentPad = wrap(Constant::getNullValue(Ty));
3207 }
3208 return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(UnwindBB),
3209 NumHandlers, Name));
3210}
3211
3213 LLVMBasicBlockRef BB) {
3214 return wrap(unwrap(B)->CreateCatchRet(unwrap<CatchPadInst>(CatchPad),
3215 unwrap(BB)));
3216}
3217
3219 LLVMBasicBlockRef BB) {
3220 return wrap(unwrap(B)->CreateCleanupRet(unwrap<CleanupPadInst>(CatchPad),
3221 unwrap(BB)));
3222}
3223
3225 return wrap(unwrap(B)->CreateUnreachable());
3226}
3227
3229 LLVMBasicBlockRef Dest) {
3230 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
3231}
3232
3234 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
3235}
3236
3237unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
3238 return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
3239}
3240
3242 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
3243}
3244
3246 unwrap<LandingPadInst>(LandingPad)->addClause(unwrap<Constant>(ClauseVal));
3247}
3248
3250 return unwrap<LandingPadInst>(LandingPad)->isCleanup();
3251}
3252
3253void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
3254 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
3255}
3256
3258 unwrap<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Dest));
3259}
3260
3261unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) {
3262 return unwrap<CatchSwitchInst>(CatchSwitch)->getNumHandlers();
3263}
3264
3265void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) {
3266 CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(CatchSwitch);
3267 for (const BasicBlock *H : CSI->handlers())
3268 *Handlers++ = wrap(H);
3269}
3270
3272 return wrap(unwrap<CatchPadInst>(CatchPad)->getCatchSwitch());
3273}
3274
3276 unwrap<CatchPadInst>(CatchPad)
3277 ->setCatchSwitch(unwrap<CatchSwitchInst>(CatchSwitch));
3278}
3279
3280/*--.. Funclets ...........................................................--*/
3281
3283 return wrap(unwrap<FuncletPadInst>(Funclet)->getArgOperand(i));
3284}
3285
3287 unwrap<FuncletPadInst>(Funclet)->setArgOperand(i, unwrap(value));
3288}
3289
3290/*--.. Arithmetic ..........................................................--*/
3291
3293 const char *Name) {
3294 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
3295}
3296
3298 const char *Name) {
3299 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
3300}
3301
3303 const char *Name) {
3304 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
3305}
3306
3308 const char *Name) {
3309 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
3310}
3311
3313 const char *Name) {
3314 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
3315}
3316
3318 const char *Name) {
3319 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
3320}
3321
3323 const char *Name) {
3324 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
3325}
3326
3328 const char *Name) {
3329 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
3330}
3331
3333 const char *Name) {
3334 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
3335}
3336
3338 const char *Name) {
3339 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
3340}
3341
3343 const char *Name) {
3344 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
3345}
3346
3348 const char *Name) {
3349 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
3350}
3351
3353 const char *Name) {
3354 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
3355}
3356
3358 LLVMValueRef RHS, const char *Name) {
3359 return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
3360}
3361
3363 const char *Name) {
3364 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
3365}
3366
3368 LLVMValueRef RHS, const char *Name) {
3369 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
3370}
3371
3373 const char *Name) {
3374 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
3375}
3376
3378 const char *Name) {
3379 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
3380}
3381
3383 const char *Name) {
3384 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
3385}
3386
3388 const char *Name) {
3389 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
3390}
3391
3393 const char *Name) {
3394 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
3395}
3396
3398 const char *Name) {
3399 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
3400}
3401
3403 const char *Name) {
3404 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
3405}
3406
3408 const char *Name) {
3409 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
3410}
3411
3413 const char *Name) {
3414 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
3415}
3416
3418 const char *Name) {
3419 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
3420}
3421
3423 LLVMValueRef LHS, LLVMValueRef RHS,
3424 const char *Name) {
3425 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
3426 unwrap(RHS), Name));
3427}
3428
3430 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
3431}
3432
3434 const char *Name) {
3435 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
3436}
3437
3439 const char *Name) {
3440 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
3441}
3442
3444 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
3445}
3446
3448 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
3449}
3450
3451/*--.. Memory ..............................................................--*/
3452
3454 const char *Name) {
3455 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3456 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3457 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3458 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3459 ITy, unwrap(Ty), AllocSize,
3460 nullptr, nullptr, "");
3461 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3462}
3463
3465 LLVMValueRef Val, const char *Name) {
3466 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3467 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3468 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3469 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3470 ITy, unwrap(Ty), AllocSize,
3471 unwrap(Val), nullptr, "");
3472 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3473}
3474
3476 LLVMValueRef Val, LLVMValueRef Len,
3477 unsigned Align) {
3478 return wrap(unwrap(B)->CreateMemSet(unwrap(Ptr), unwrap(Val), unwrap(Len),
3479 MaybeAlign(Align)));
3480}
3481
3483 LLVMValueRef Dst, unsigned DstAlign,
3484 LLVMValueRef Src, unsigned SrcAlign,
3486 return wrap(unwrap(B)->CreateMemCpy(unwrap(Dst), MaybeAlign(DstAlign),
3487 unwrap(Src), MaybeAlign(SrcAlign),
3488 unwrap(Size)));
3489}
3490
3492 LLVMValueRef Dst, unsigned DstAlign,
3493 LLVMValueRef Src, unsigned SrcAlign,
3495 return wrap(unwrap(B)->CreateMemMove(unwrap(Dst), MaybeAlign(DstAlign),
3496 unwrap(Src), MaybeAlign(SrcAlign),
3497 unwrap(Size)));
3498}
3499
3501 const char *Name) {
3502 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
3503}
3504
3506 LLVMValueRef Val, const char *Name) {
3507 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
3508}
3509
3511 return wrap(unwrap(B)->Insert(
3512 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
3513}
3514
3516 LLVMValueRef PointerVal, const char *Name) {
3517 return wrap(unwrap(B)->CreateLoad(unwrap(Ty), unwrap(PointerVal), Name));
3518}
3519
3521 LLVMValueRef PointerVal) {
3522 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
3523}
3524
3526 switch (Ordering) {
3527 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic;
3528 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered;
3529 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic;
3530 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire;
3531 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release;
3533 return AtomicOrdering::AcquireRelease;
3535 return AtomicOrdering::SequentiallyConsistent;
3536 }
3537
3538 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
3539}
3540
3542 switch (Ordering) {
3543 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic;
3544 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered;
3545 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic;
3546 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire;
3547 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease;
3548 case AtomicOrdering::AcquireRelease:
3550 case AtomicOrdering::SequentiallyConsistent:
3552 }
3553
3554 llvm_unreachable("Invalid AtomicOrdering value!");
3555}
3556
3558 switch (BinOp) {
3574 }
3575
3576 llvm_unreachable("Invalid LLVMAtomicRMWBinOp value!");
3577}
3578
3580 switch (BinOp) {
3596 default: break;
3597 }
3598
3599 llvm_unreachable("Invalid AtomicRMWBinOp value!");
3600}
3601
3602// TODO: Should this and other atomic instructions support building with
3603// "syncscope"?
3605 LLVMBool isSingleThread, const char *Name) {
3606 return wrap(
3607 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
3608 isSingleThread ? SyncScope::SingleThread
3610 Name));
3611}
3612
3614 LLVMValueRef Pointer, LLVMValueRef *Indices,
3615 unsigned NumIndices, const char *Name) {
3616 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3617 return wrap(unwrap(B)->CreateGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3618}
3619
3621 LLVMValueRef Pointer, LLVMValueRef *Indices,
3622 unsigned NumIndices, const char *Name) {
3623 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3624 return wrap(
3625 unwrap(B)->CreateInBoundsGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3626}
3627
3629 LLVMValueRef Pointer, unsigned Idx,
3630 const char *Name) {
3631 return wrap(
3632 unwrap(B)->CreateStructGEP(unwrap(Ty), unwrap(Pointer), Idx, Name));
3633}
3634
3636 const char *Name) {
3637 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
3638}
3639
3641 const char *Name) {
3642 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
3643}
3644
3646 Value *P = unwrap(MemAccessInst);
3647 if (LoadInst *LI = dyn_cast<LoadInst>(P))
3648 return LI->isVolatile();
3649 if (StoreInst *SI = dyn_cast<StoreInst>(P))
3650 return SI->isVolatile();
3651 if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
3652 return AI->isVolatile();
3653 return cast<AtomicCmpXchgInst>(P)->isVolatile();
3654}
3655
3656void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
3657 Value *P = unwrap(MemAccessInst);
3658 if (LoadInst *LI = dyn_cast<LoadInst>(P))
3659 return LI->setVolatile(isVolatile);
3660 if (StoreInst *SI = dyn_cast<StoreInst>(P))
3661 return SI->setVolatile(isVolatile);
3662 if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
3663 return AI->setVolatile(isVolatile);
3664 return cast<AtomicCmpXchgInst>(P)->setVolatile(isVolatile);
3665}
3666
3668 return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->isWeak();
3669}
3670
3671void LLVMSetWeak(LLVMValueRef CmpXchgInst, LLVMBool isWeak) {
3672 return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->setWeak(isWeak);
3673}
3674
3676 Value *P = unwrap(MemAccessInst);
3678 if (LoadInst *LI = dyn_cast<LoadInst>(P))
3679 O = LI->getOrdering();
3680 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
3681 O = SI->getOrdering();
3682 else
3683 O = cast<AtomicRMWInst>(P)->