LLVM  14.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/Constants.h"
18 #include "llvm/IR/DerivedTypes.h"
19 #include "llvm/IR/DiagnosticInfo.h"
21 #include "llvm/IR/GlobalAlias.h"
22 #include "llvm/IR/GlobalVariable.h"
23 #include "llvm/IR/IRBuilder.h"
24 #include "llvm/IR/InlineAsm.h"
25 #include "llvm/IR/IntrinsicInst.h"
26 #include "llvm/IR/LLVMContext.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/InitializePasses.h"
30 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/Threading.h"
37 #include <cassert>
38 #include <cstdlib>
39 #include <cstring>
40 #include <system_error>
41 
42 using namespace llvm;
43 
44 #define DEBUG_TYPE "ir"
45 
52 }
53 
56 }
57 
58 void LLVMShutdown() {
59  llvm_shutdown();
60 }
61 
62 /*===-- Error handling ----------------------------------------------------===*/
63 
64 char *LLVMCreateMessage(const char *Message) {
65  return strdup(Message);
66 }
67 
68 void LLVMDisposeMessage(char *Message) {
69  free(Message);
70 }
71 
72 
73 /*===-- Operations on contexts --------------------------------------------===*/
74 
76 
78  return wrap(new LLVMContext());
79 }
80 
82 
84  LLVMDiagnosticHandler Handler,
85  void *DiagnosticContext) {
86  unwrap(C)->setDiagnosticHandlerCallBack(
88  Handler),
89  DiagnosticContext);
90 }
91 
93  return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>(
94  unwrap(C)->getDiagnosticHandlerCallBack());
95 }
96 
98  return unwrap(C)->getDiagnosticContext();
99 }
100 
102  void *OpaqueHandle) {
103  auto YieldCallback =
104  LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
105  unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
106 }
107 
109  return unwrap(C)->shouldDiscardValueNames();
110 }
111 
113  unwrap(C)->setDiscardValueNames(Discard);
114 }
115 
117  delete unwrap(C);
118 }
119 
121  unsigned SLen) {
122  return unwrap(C)->getMDKindID(StringRef(Name, SLen));
123 }
124 
125 unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) {
127 }
128 
129 unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) {
131 }
132 
134  return Attribute::AttrKind::EndAttrKinds;
135 }
136 
138  uint64_t Val) {
139  auto &Ctx = *unwrap(C);
140  auto AttrKind = (Attribute::AttrKind)KindID;
141 
142  if (AttrKind == Attribute::AttrKind::ByVal) {
143  // After r362128, byval attributes need to have a type attribute. Provide a
144  // NULL one until a proper API is added for this.
145  return wrap(Attribute::getWithByValType(Ctx, nullptr));
146  }
147 
148  if (AttrKind == Attribute::AttrKind::StructRet) {
149  // Same as byval.
150  return wrap(Attribute::getWithStructRetType(Ctx, nullptr));
151  }
152 
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, *GlobalContext));
249 }
250 
252  LLVMContextRef C) {
253  return wrap(new Module(ModuleID, *unwrap(C)));
254 }
255 
257  delete unwrap(M);
258 }
259 
260 const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) {
261  auto &Str = unwrap(M)->getModuleIdentifier();
262  *Len = Str.length();
263  return Str.c_str();
264 }
265 
266 void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) {
267  unwrap(M)->setModuleIdentifier(StringRef(Ident, Len));
268 }
269 
270 const char *LLVMGetSourceFileName(LLVMModuleRef M, size_t *Len) {
271  auto &Str = unwrap(M)->getSourceFileName();
272  *Len = Str.length();
273  return Str.c_str();
274 }
275 
276 void 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 
289 void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
290  unwrap(M)->setDataLayout(DataLayoutStr);
291 }
292 
293 /*--.. Target triple .......................................................--*/
294 const char * LLVMGetTarget(LLVMModuleRef M) {
295  return unwrap(M)->getTargetTriple().c_str();
296 }
297 
298 void 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) {
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) {
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 
353  LLVMOpaqueModuleFlagEntry *Result = static_cast<LLVMOpaqueModuleFlagEntry *>(
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 ......................................--*/
445 void LLVMSetModuleInlineAsm2(LLVMModuleRef M, const char *Asm, size_t Len) {
446  unwrap(M)->setModuleInlineAsm(StringRef(Asm, Len));
447 }
448 
450  unwrap(M)->setModuleInlineAsm(StringRef(Asm));
451 }
452 
453 void LLVMAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm, size_t Len) {
454  unwrap(M)->appendModuleInlineAsm(StringRef(Asm, Len));
455 }
456 
457 const 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) {
471  AD = InlineAsm::AD_ATT;
472  break;
474  AD = InlineAsm::AD_Intel;
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;
505  case Type::X86_FP80TyID:
506  return LLVMX86_FP80TypeKind;
507  case Type::FP128TyID:
508  return LLVMFP128TypeKind;
509  case Type::PPC_FP128TyID:
510  return LLVMPPC_FP128TypeKind;
511  case Type::LabelTyID:
512  return LLVMLabelTypeKind;
513  case Type::MetadataTyID:
514  return LLVMMetadataTypeKind;
515  case Type::IntegerTyID:
516  return LLVMIntegerTypeKind;
517  case Type::FunctionTyID:
518  return LLVMFunctionTypeKind;
519  case Type::StructTyID:
520  return LLVMStructTypeKind;
521  case Type::ArrayTyID:
522  return LLVMArrayTypeKind;
523  case Type::PointerTyID:
524  return LLVMPointerTypeKind;
526  return LLVMVectorTypeKind;
527  case Type::X86_MMXTyID:
528  return LLVMX86_MMXTypeKind;
529  case Type::X86_AMXTyID:
530  return LLVMX86_AMXTypeKind;
531  case Type::TokenTyID:
532  return LLVMTokenTypeKind;
535  }
536  llvm_unreachable("Unhandled TypeID.");
537 }
538 
540 {
541  return unwrap(Ty)->isSized();
542 }
543 
545  return wrap(&unwrap(Ty)->getContext());
546 }
547 
549  return unwrap(Ty)->print(errs(), /*IsForDebug=*/true);
550 }
551 
553  std::string buf;
554  raw_string_ostream os(buf);
555 
556  if (unwrap(Ty))
557  unwrap(Ty)->print(os);
558  else
559  os << "Printing <null> Type";
560 
561  os.flush();
562 
563  return strdup(buf.c_str());
564 }
565 
566 /*--.. Operations on integer types .........................................--*/
567 
569  return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
570 }
572  return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
573 }
575  return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
576 }
578  return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
579 }
581  return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
582 }
585 }
587  return wrap(IntegerType::get(*unwrap(C), NumBits));
588 }
589 
592 }
595 }
598 }
601 }
604 }
607 }
608 LLVMTypeRef LLVMIntType(unsigned NumBits) {
609  return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
610 }
611 
612 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
613  return unwrap<IntegerType>(IntegerTy)->getBitWidth();
614 }
615 
616 /*--.. Operations on real types ............................................--*/
617 
619  return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
620 }
623 }
625  return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
626 }
629 }
632 }
634  return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
635 }
638 }
641 }
644 }
645 
648 }
651 }
654 }
657 }
660 }
663 }
666 }
669 }
672 }
673 
674 /*--.. Operations on function types ........................................--*/
675 
677  LLVMTypeRef *ParamTypes, unsigned ParamCount,
678  LLVMBool IsVarArg) {
679  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
680  return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
681 }
682 
684  return unwrap<FunctionType>(FunctionTy)->isVarArg();
685 }
686 
688  return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
689 }
690 
691 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
692  return unwrap<FunctionType>(FunctionTy)->getNumParams();
693 }
694 
695 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
696  FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
697  for (Type *T : Ty->params())
698  *Dest++ = wrap(T);
699 }
700 
701 /*--.. Operations on struct types ..........................................--*/
702 
704  unsigned ElementCount, LLVMBool Packed) {
705  ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
706  return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
707 }
708 
710  unsigned ElementCount, LLVMBool Packed) {
711  return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
712  ElementCount, Packed);
713 }
714 
716 {
717  return wrap(StructType::create(*unwrap(C), Name));
718 }
719 
721 {
722  StructType *Type = unwrap<StructType>(Ty);
723  if (!Type->hasName())
724  return nullptr;
725  return Type->getName().data();
726 }
727 
728 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
729  unsigned ElementCount, LLVMBool Packed) {
730  ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
731  unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
732 }
733 
735  return unwrap<StructType>(StructTy)->getNumElements();
736 }
737 
739  StructType *Ty = unwrap<StructType>(StructTy);
740  for (Type *T : Ty->elements())
741  *Dest++ = wrap(T);
742 }
743 
745  StructType *Ty = unwrap<StructType>(StructTy);
746  return wrap(Ty->getTypeAtIndex(i));
747 }
748 
750  return unwrap<StructType>(StructTy)->isPacked();
751 }
752 
754  return unwrap<StructType>(StructTy)->isOpaque();
755 }
756 
758  return unwrap<StructType>(StructTy)->isLiteral();
759 }
760 
762  return wrap(StructType::getTypeByName(unwrap(M)->getContext(), Name));
763 }
764 
767 }
768 
769 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
770 
772  int i = 0;
773  for (auto *T : unwrap(Tp)->subtypes()) {
774  Arr[i] = wrap(T);
775  i++;
776  }
777 }
778 
780  return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
781 }
782 
784  return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
785 }
786 
788  return wrap(FixedVectorType::get(unwrap(ElementType), ElementCount));
789 }
790 
792  unsigned ElementCount) {
793  return wrap(ScalableVectorType::get(unwrap(ElementType), ElementCount));
794 }
795 
797  auto *Ty = unwrap<Type>(WrappedTy);
798  if (auto *PTy = dyn_cast<PointerType>(Ty))
799  return wrap(PTy->getElementType());
800  if (auto *ATy = dyn_cast<ArrayType>(Ty))
801  return wrap(ATy->getElementType());
802  return wrap(cast<VectorType>(Ty)->getElementType());
803 }
804 
806  return unwrap(Tp)->getNumContainedTypes();
807 }
808 
809 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
810  return unwrap<ArrayType>(ArrayTy)->getNumElements();
811 }
812 
814  return unwrap<PointerType>(PointerTy)->getAddressSpace();
815 }
816 
817 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
818  return unwrap<VectorType>(VectorTy)->getElementCount().getKnownMinValue();
819 }
820 
821 /*--.. Operations on other types ...........................................--*/
822 
824  return wrap(Type::getVoidTy(*unwrap(C)));
825 }
827  return wrap(Type::getLabelTy(*unwrap(C)));
828 }
830  return wrap(Type::getTokenTy(*unwrap(C)));
831 }
833  return wrap(Type::getMetadataTy(*unwrap(C)));
834 }
835 
838 }
841 }
842 
843 /*===-- Operations on values ----------------------------------------------===*/
844 
845 /*--.. Operations on all values ............................................--*/
846 
848  return wrap(unwrap(Val)->getType());
849 }
850 
852  switch(unwrap(Val)->getValueID()) {
853 #define LLVM_C_API 1
854 #define HANDLE_VALUE(Name) \
855  case Value::Name##Val: \
856  return LLVM##Name##ValueKind;
857 #include "llvm/IR/Value.def"
858  default:
860  }
861 }
862 
863 const char *LLVMGetValueName2(LLVMValueRef Val, size_t *Length) {
864  auto *V = unwrap(Val);
865  *Length = V->getName().size();
866  return V->getName().data();
867 }
868 
869 void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen) {
870  unwrap(Val)->setName(StringRef(Name, NameLen));
871 }
872 
873 const char *LLVMGetValueName(LLVMValueRef Val) {
874  return unwrap(Val)->getName().data();
875 }
876 
877 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
878  unwrap(Val)->setName(Name);
879 }
880 
882  unwrap(Val)->print(errs(), /*IsForDebug=*/true);
883 }
884 
886  std::string buf;
887  raw_string_ostream os(buf);
888 
889  if (unwrap(Val))
890  unwrap(Val)->print(os);
891  else
892  os << "Printing <null> Value";
893 
894  os.flush();
895 
896  return strdup(buf.c_str());
897 }
898 
900  unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
901 }
902 
904  return unwrap<Instruction>(Inst)->hasMetadata();
905 }
906 
907 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
908  auto *I = unwrap<Instruction>(Inst);
909  assert(I && "Expected instruction");
910  if (auto *MD = I->getMetadata(KindID))
911  return wrap(MetadataAsValue::get(I->getContext(), MD));
912  return nullptr;
913 }
914 
915 // MetadataAsValue uses a canonical format which strips the actual MDNode for
916 // MDNode with just a single constant value, storing just a ConstantAsMetadata
917 // This undoes this canonicalization, reconstructing the MDNode.
919  Metadata *MD = MAV->getMetadata();
920  assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
921  "Expected a metadata node or a canonicalized constant");
922 
923  if (MDNode *N = dyn_cast<MDNode>(MD))
924  return N;
925 
926  return MDNode::get(MAV->getContext(), MD);
927 }
928 
929 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
930  MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
931 
932  unwrap<Instruction>(Inst)->setMetadata(KindID, N);
933 }
934 
936  unsigned Kind;
938 };
939 
941 static LLVMValueMetadataEntry *
942 llvm_getMetadata(size_t *NumEntries,
943  llvm::function_ref<void(MetadataEntries &)> AccessMD) {
945  AccessMD(MVEs);
946 
948  static_cast<LLVMOpaqueValueMetadataEntry *>(
949  safe_malloc(MVEs.size() * sizeof(LLVMOpaqueValueMetadataEntry)));
950  for (unsigned i = 0; i < MVEs.size(); ++i) {
951  const auto &ModuleFlag = MVEs[i];
952  Result[i].Kind = ModuleFlag.first;
953  Result[i].Metadata = wrap(ModuleFlag.second);
954  }
955  *NumEntries = MVEs.size();
956  return Result;
957 }
958 
961  size_t *NumEntries) {
962  return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
963  Entries.clear();
964  unwrap<Instruction>(Value)->getAllMetadata(Entries);
965  });
966 }
967 
968 /*--.. Conversion functions ................................................--*/
969 
970 #define LLVM_DEFINE_VALUE_CAST(name) \
971  LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
972  return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
973  }
974 
976 
978  if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
979  if (isa<MDNode>(MD->getMetadata()) ||
980  isa<ValueAsMetadata>(MD->getMetadata()))
981  return Val;
982  return nullptr;
983 }
984 
986  if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
987  if (isa<MDString>(MD->getMetadata()))
988  return Val;
989  return nullptr;
990 }
991 
992 /*--.. Operations on Uses ..................................................--*/
994  Value *V = unwrap(Val);
996  if (I == V->use_end())
997  return nullptr;
998  return wrap(&*I);
999 }
1000 
1002  Use *Next = unwrap(U)->getNext();
1003  if (Next)
1004  return wrap(Next);
1005  return nullptr;
1006 }
1007 
1009  return wrap(unwrap(U)->getUser());
1010 }
1011 
1013  return wrap(unwrap(U)->get());
1014 }
1015 
1016 /*--.. Operations on Users .................................................--*/
1017 
1019  unsigned Index) {
1020  Metadata *Op = N->getOperand(Index);
1021  if (!Op)
1022  return nullptr;
1023  if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
1024  return wrap(C->getValue());
1025  return wrap(MetadataAsValue::get(Context, Op));
1026 }
1027 
1029  Value *V = unwrap(Val);
1030  if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
1031  if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1032  assert(Index == 0 && "Function-local metadata can only have one operand");
1033  return wrap(L->getValue());
1034  }
1035  return getMDNodeOperandImpl(V->getContext(),
1036  cast<MDNode>(MD->getMetadata()), Index);
1037  }
1038 
1039  return wrap(cast<User>(V)->getOperand(Index));
1040 }
1041 
1043  Value *V = unwrap(Val);
1044  return wrap(&cast<User>(V)->getOperandUse(Index));
1045 }
1046 
1048  unwrap<User>(Val)->setOperand(Index, unwrap(Op));
1049 }
1050 
1052  Value *V = unwrap(Val);
1053  if (isa<MetadataAsValue>(V))
1054  return LLVMGetMDNodeNumOperands(Val);
1055 
1056  return cast<User>(V)->getNumOperands();
1057 }
1058 
1059 /*--.. Operations on constants of any type .................................--*/
1060 
1062  return wrap(Constant::getNullValue(unwrap(Ty)));
1063 }
1064 
1066  return wrap(Constant::getAllOnesValue(unwrap(Ty)));
1067 }
1068 
1070  return wrap(UndefValue::get(unwrap(Ty)));
1071 }
1072 
1074  return wrap(PoisonValue::get(unwrap(Ty)));
1075 }
1076 
1078  return isa<Constant>(unwrap(Ty));
1079 }
1080 
1082  if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
1083  return C->isNullValue();
1084  return false;
1085 }
1086 
1088  return isa<UndefValue>(unwrap(Val));
1089 }
1090 
1092  return isa<PoisonValue>(unwrap(Val));
1093 }
1094 
1096  return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
1097 }
1098 
1099 /*--.. Operations on metadata nodes ........................................--*/
1100 
1102  size_t SLen) {
1103  return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
1104 }
1105 
1107  size_t Count) {
1108  return wrap(MDNode::get(*unwrap(C), ArrayRef<Metadata*>(unwrap(MDs), Count)));
1109 }
1110 
1112  unsigned SLen) {
1113  LLVMContext &Context = *unwrap(C);
1114  return wrap(MetadataAsValue::get(
1115  Context, MDString::get(Context, StringRef(Str, SLen))));
1116 }
1117 
1118 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
1119  return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
1120 }
1121 
1123  unsigned Count) {
1124  LLVMContext &Context = *unwrap(C);
1126  for (auto *OV : makeArrayRef(Vals, Count)) {
1127  Value *V = unwrap(OV);
1128  Metadata *MD;
1129  if (!V)
1130  MD = nullptr;
1131  else if (auto *C = dyn_cast<Constant>(V))
1132  MD = ConstantAsMetadata::get(C);
1133  else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
1134  MD = MDV->getMetadata();
1135  assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
1136  "outside of direct argument to call");
1137  } else {
1138  // This is function-local metadata. Pretend to make an MDNode.
1139  assert(Count == 1 &&
1140  "Expected only one operand to function-local metadata");
1142  }
1143 
1144  MDs.push_back(MD);
1145  }
1147 }
1148 
1149 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
1150  return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
1151 }
1152 
1154  return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
1155 }
1156 
1158  auto *V = unwrap(Val);
1159  if (auto *C = dyn_cast<Constant>(V))
1160  return wrap(ConstantAsMetadata::get(C));
1161  if (auto *MAV = dyn_cast<MetadataAsValue>(V))
1162  return wrap(MAV->getMetadata());
1163  return wrap(ValueAsMetadata::get(V));
1164 }
1165 
1166 const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
1167  if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
1168  if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
1169  *Length = S->getString().size();
1170  return S->getString().data();
1171  }
1172  *Length = 0;
1173  return nullptr;
1174 }
1175 
1177  auto *MD = cast<MetadataAsValue>(unwrap(V));
1178  if (isa<ValueAsMetadata>(MD->getMetadata()))
1179  return 1;
1180  return cast<MDNode>(MD->getMetadata())->getNumOperands();
1181 }
1182 
1184  Module *Mod = unwrap(M);
1186  if (I == Mod->named_metadata_end())
1187  return nullptr;
1188  return wrap(&*I);
1189 }
1190 
1192  Module *Mod = unwrap(M);
1194  if (I == Mod->named_metadata_begin())
1195  return nullptr;
1196  return wrap(&*--I);
1197 }
1198 
1200  NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD);
1202  if (++I == NamedNode->getParent()->named_metadata_end())
1203  return nullptr;
1204  return wrap(&*I);
1205 }
1206 
1208  NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD);
1210  if (I == NamedNode->getParent()->named_metadata_begin())
1211  return nullptr;
1212  return wrap(&*--I);
1213 }
1214 
1216  const char *Name, size_t NameLen) {
1217  return wrap(unwrap(M)->getNamedMetadata(StringRef(Name, NameLen)));
1218 }
1219 
1221  const char *Name, size_t NameLen) {
1222  return wrap(unwrap(M)->getOrInsertNamedMetadata({Name, NameLen}));
1223 }
1224 
1225 const char *LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NMD, size_t *NameLen) {
1226  NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD);
1227  *NameLen = NamedNode->getName().size();
1228  return NamedNode->getName().data();
1229 }
1230 
1232  auto *MD = cast<MetadataAsValue>(unwrap(V));
1233  if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1234  *Dest = wrap(MDV->getValue());
1235  return;
1236  }
1237  const auto *N = cast<MDNode>(MD->getMetadata());
1238  const unsigned numOperands = N->getNumOperands();
1239  LLVMContext &Context = unwrap(V)->getContext();
1240  for (unsigned i = 0; i < numOperands; i++)
1241  Dest[i] = getMDNodeOperandImpl(Context, N, i);
1242 }
1243 
1245  if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
1246  return N->getNumOperands();
1247  }
1248  return 0;
1249 }
1250 
1252  LLVMValueRef *Dest) {
1253  NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
1254  if (!N)
1255  return;
1256  LLVMContext &Context = unwrap(M)->getContext();
1257  for (unsigned i=0;i<N->getNumOperands();i++)
1258  Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
1259 }
1260 
1262  LLVMValueRef Val) {
1263  NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
1264  if (!N)
1265  return;
1266  if (!Val)
1267  return;
1268  N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
1269 }
1270 
1271 const char *LLVMGetDebugLocDirectory(LLVMValueRef Val, unsigned *Length) {
1272  if (!Length) return nullptr;
1273  StringRef S;
1274  if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1275  if (const auto &DL = I->getDebugLoc()) {
1276  S = DL->getDirectory();
1277  }
1278  } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1280  GV->getDebugInfo(GVEs);
1281  if (GVEs.size())
1282  if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1283  S = DGV->getDirectory();
1284  } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1285  if (const DISubprogram *DSP = F->getSubprogram())
1286  S = DSP->getDirectory();
1287  } else {
1288  assert(0 && "Expected Instruction, GlobalVariable or Function");
1289  return nullptr;
1290  }
1291  *Length = S.size();
1292  return S.data();
1293 }
1294 
1295 const char *LLVMGetDebugLocFilename(LLVMValueRef Val, unsigned *Length) {
1296  if (!Length) return nullptr;
1297  StringRef S;
1298  if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1299  if (const auto &DL = I->getDebugLoc()) {
1300  S = DL->getFilename();
1301  }
1302  } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1304  GV->getDebugInfo(GVEs);
1305  if (GVEs.size())
1306  if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1307  S = DGV->getFilename();
1308  } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1309  if (const DISubprogram *DSP = F->getSubprogram())
1310  S = DSP->getFilename();
1311  } else {
1312  assert(0 && "Expected Instruction, GlobalVariable or Function");
1313  return nullptr;
1314  }
1315  *Length = S.size();
1316  return S.data();
1317 }
1318 
1320  unsigned L = 0;
1321  if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1322  if (const auto &DL = I->getDebugLoc()) {
1323  L = DL->getLine();
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  L = DGV->getLine();
1331  } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1332  if (const DISubprogram *DSP = F->getSubprogram())
1333  L = DSP->getLine();
1334  } else {
1335  assert(0 && "Expected Instruction, GlobalVariable or Function");
1336  return -1;
1337  }
1338  return L;
1339 }
1340 
1342  unsigned C = 0;
1343  if (const auto *I = dyn_cast<Instruction>(unwrap(Val)))
1344  if (const auto &DL = I->getDebugLoc())
1345  C = DL->getColumn();
1346  return C;
1347 }
1348 
1349 /*--.. Operations on scalar constants ......................................--*/
1350 
1351 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
1352  LLVMBool SignExtend) {
1353  return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
1354 }
1355 
1357  unsigned NumWords,
1358  const uint64_t Words[]) {
1359  IntegerType *Ty = unwrap<IntegerType>(IntTy);
1360  return wrap(ConstantInt::get(Ty->getContext(),
1361  APInt(Ty->getBitWidth(),
1362  makeArrayRef(Words, NumWords))));
1363 }
1364 
1366  uint8_t Radix) {
1367  return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
1368  Radix));
1369 }
1370 
1372  unsigned SLen, uint8_t Radix) {
1373  return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
1374  Radix));
1375 }
1376 
1378  return wrap(ConstantFP::get(unwrap(RealTy), N));
1379 }
1380 
1382  return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
1383 }
1384 
1386  unsigned SLen) {
1387  return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
1388 }
1389 
1390 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
1391  return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
1392 }
1393 
1394 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
1395  return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
1396 }
1397 
1398 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
1399  ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
1400  Type *Ty = cFP->getType();
1401 
1402  if (Ty->isHalfTy() || Ty->isBFloatTy() || Ty->isFloatTy() ||
1403  Ty->isDoubleTy()) {
1404  *LosesInfo = false;
1405  return cFP->getValueAPF().convertToDouble();
1406  }
1407 
1408  bool APFLosesInfo;
1409  APFloat APF = cFP->getValueAPF();
1411  *LosesInfo = APFLosesInfo;
1412  return APF.convertToDouble();
1413 }
1414 
1415 /*--.. Operations on composite constants ...................................--*/
1416 
1418  unsigned Length,
1419  LLVMBool DontNullTerminate) {
1420  /* Inverted the sense of AddNull because ', 0)' is a
1421  better mnemonic for null termination than ', 1)'. */
1422  return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
1423  DontNullTerminate == 0));
1424 }
1425 
1426 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
1427  LLVMBool DontNullTerminate) {
1428  return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
1429  DontNullTerminate);
1430 }
1431 
1433  return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
1434 }
1435 
1437  return unwrap<ConstantDataSequential>(C)->isString();
1438 }
1439 
1440 const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1441  StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1442  *Length = Str.size();
1443  return Str.data();
1444 }
1445 
1447  LLVMValueRef *ConstantVals, unsigned Length) {
1448  ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1449  return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1450 }
1451 
1453  LLVMValueRef *ConstantVals,
1454  unsigned Count, LLVMBool Packed) {
1455  Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1456  return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
1457  Packed != 0));
1458 }
1459 
1460 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
1461  LLVMBool Packed) {
1462  return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1463  Packed);
1464 }
1465 
1467  LLVMValueRef *ConstantVals,
1468  unsigned Count) {
1469  Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1470  StructType *Ty = cast<StructType>(unwrap(StructTy));
1471 
1472  return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
1473 }
1474 
1475 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
1477  unwrap<Constant>(ScalarConstantVals, Size), Size)));
1478 }
1479 
1480 /*-- Opcode mapping */
1481 
1482 static LLVMOpcode map_to_llvmopcode(int opcode)
1483 {
1484  switch (opcode) {
1485  default: llvm_unreachable("Unhandled Opcode.");
1486 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
1487 #include "llvm/IR/Instruction.def"
1488 #undef HANDLE_INST
1489  }
1490 }
1491 
1493 {
1494  switch (code) {
1495 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
1496 #include "llvm/IR/Instruction.def"
1497 #undef HANDLE_INST
1498  }
1499  llvm_unreachable("Unhandled Opcode.");
1500 }
1501 
1502 /*--.. Constant expressions ................................................--*/
1503 
1505  return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
1506 }
1507 
1509  return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
1510 }
1511 
1513  return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
1514 }
1515 
1517  return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1518 }
1519 
1521  return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
1522 }
1523 
1525  return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
1526 }
1527 
1528 
1530  return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
1531 }
1532 
1534  return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
1535 }
1536 
1538  return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
1539  unwrap<Constant>(RHSConstant)));
1540 }
1541 
1543  LLVMValueRef RHSConstant) {
1544  return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
1545  unwrap<Constant>(RHSConstant)));
1546 }
1547 
1549  LLVMValueRef RHSConstant) {
1550  return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
1551  unwrap<Constant>(RHSConstant)));
1552 }
1553 
1555  return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
1556  unwrap<Constant>(RHSConstant)));
1557 }
1558 
1560  return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
1561  unwrap<Constant>(RHSConstant)));
1562 }
1563 
1565  LLVMValueRef RHSConstant) {
1566  return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
1567  unwrap<Constant>(RHSConstant)));
1568 }
1569 
1571  LLVMValueRef RHSConstant) {
1572  return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
1573  unwrap<Constant>(RHSConstant)));
1574 }
1575 
1577  return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
1578  unwrap<Constant>(RHSConstant)));
1579 }
1580 
1582  return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
1583  unwrap<Constant>(RHSConstant)));
1584 }
1585 
1587  LLVMValueRef RHSConstant) {
1588  return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
1589  unwrap<Constant>(RHSConstant)));
1590 }
1591 
1593  LLVMValueRef RHSConstant) {
1594  return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
1595  unwrap<Constant>(RHSConstant)));
1596 }
1597 
1599  return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
1600  unwrap<Constant>(RHSConstant)));
1601 }
1602 
1604  return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
1605  unwrap<Constant>(RHSConstant)));
1606 }
1607 
1609  LLVMValueRef RHSConstant) {
1610  return wrap(ConstantExpr::getExactUDiv(unwrap<Constant>(LHSConstant),
1611  unwrap<Constant>(RHSConstant)));
1612 }
1613 
1615  return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
1616  unwrap<Constant>(RHSConstant)));
1617 }
1618 
1620  LLVMValueRef RHSConstant) {
1621  return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
1622  unwrap<Constant>(RHSConstant)));
1623 }
1624 
1626  return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
1627  unwrap<Constant>(RHSConstant)));
1628 }
1629 
1631  return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
1632  unwrap<Constant>(RHSConstant)));
1633 }
1634 
1636  return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
1637  unwrap<Constant>(RHSConstant)));
1638 }
1639 
1641  return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
1642  unwrap<Constant>(RHSConstant)));
1643 }
1644 
1646  return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
1647  unwrap<Constant>(RHSConstant)));
1648 }
1649 
1651  return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
1652  unwrap<Constant>(RHSConstant)));
1653 }
1654 
1656  return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
1657  unwrap<Constant>(RHSConstant)));
1658 }
1659 
1661  LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1663  unwrap<Constant>(LHSConstant),
1664  unwrap<Constant>(RHSConstant)));
1665 }
1666 
1668  LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1670  unwrap<Constant>(LHSConstant),
1671  unwrap<Constant>(RHSConstant)));
1672 }
1673 
1675  return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1676  unwrap<Constant>(RHSConstant)));
1677 }
1678 
1680  return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
1681  unwrap<Constant>(RHSConstant)));
1682 }
1683 
1685  return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1686  unwrap<Constant>(RHSConstant)));
1687 }
1688 
1690  LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1691  ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1692  NumIndices);
1693  Constant *Val = unwrap<Constant>(ConstantVal);
1694  Type *Ty =
1695  cast<PointerType>(Val->getType()->getScalarType())->getElementType();
1696  return wrap(ConstantExpr::getGetElementPtr(Ty, Val, IdxList));
1697 }
1698 
1700  LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1701  ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1702  NumIndices);
1703  Constant *Val = unwrap<Constant>(ConstantVal);
1704  return wrap(ConstantExpr::getGetElementPtr(unwrap(Ty), Val, IdxList));
1705 }
1706 
1708  LLVMValueRef *ConstantIndices,
1709  unsigned NumIndices) {
1710  ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1711  NumIndices);
1712  Constant *Val = unwrap<Constant>(ConstantVal);
1713  Type *Ty =
1714  cast<PointerType>(Val->getType()->getScalarType())->getElementType();
1715  return wrap(ConstantExpr::getInBoundsGetElementPtr(Ty, Val, IdxList));
1716 }
1717 
1719  LLVMValueRef *ConstantIndices,
1720  unsigned NumIndices) {
1721  ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1722  NumIndices);
1723  Constant *Val = unwrap<Constant>(ConstantVal);
1724  return wrap(ConstantExpr::getInBoundsGetElementPtr(unwrap(Ty), Val, IdxList));
1725 }
1726 
1728  return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1729  unwrap(ToType)));
1730 }
1731 
1733  return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1734  unwrap(ToType)));
1735 }
1736 
1738  return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1739  unwrap(ToType)));
1740 }
1741 
1743  return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1744  unwrap(ToType)));
1745 }
1746 
1748  return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1749  unwrap(ToType)));
1750 }
1751 
1753  return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1754  unwrap(ToType)));
1755 }
1756 
1758  return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1759  unwrap(ToType)));
1760 }
1761 
1763  return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1764  unwrap(ToType)));
1765 }
1766 
1768  return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1769  unwrap(ToType)));
1770 }
1771 
1773  return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1774  unwrap(ToType)));
1775 }
1776 
1778  return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1779  unwrap(ToType)));
1780 }
1781 
1783  return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1784  unwrap(ToType)));
1785 }
1786 
1788  LLVMTypeRef ToType) {
1789  return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1790  unwrap(ToType)));
1791 }
1792 
1794  LLVMTypeRef ToType) {
1795  return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1796  unwrap(ToType)));
1797 }
1798 
1800  LLVMTypeRef ToType) {
1801  return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1802  unwrap(ToType)));
1803 }
1804 
1806  LLVMTypeRef ToType) {
1807  return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1808  unwrap(ToType)));
1809 }
1810 
1812  LLVMTypeRef ToType) {
1813  return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1814  unwrap(ToType)));
1815 }
1816 
1818  LLVMBool isSigned) {
1819  return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1820  unwrap(ToType), isSigned));
1821 }
1822 
1824  return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1825  unwrap(ToType)));
1826 }
1827 
1829  LLVMValueRef ConstantIfTrue,
1830  LLVMValueRef ConstantIfFalse) {
1831  return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1832  unwrap<Constant>(ConstantIfTrue),
1833  unwrap<Constant>(ConstantIfFalse)));
1834 }
1835 
1837  LLVMValueRef IndexConstant) {
1838  return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1839  unwrap<Constant>(IndexConstant)));
1840 }
1841 
1843  LLVMValueRef ElementValueConstant,
1844  LLVMValueRef IndexConstant) {
1845  return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1846  unwrap<Constant>(ElementValueConstant),
1847  unwrap<Constant>(IndexConstant)));
1848 }
1849 
1851  LLVMValueRef VectorBConstant,
1852  LLVMValueRef MaskConstant) {
1853  SmallVector<int, 16> IntMask;
1854  ShuffleVectorInst::getShuffleMask(unwrap<Constant>(MaskConstant), IntMask);
1855  return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1856  unwrap<Constant>(VectorBConstant),
1857  IntMask));
1858 }
1859 
1860 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1861  unsigned NumIdx) {
1862  return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1863  makeArrayRef(IdxList, NumIdx)));
1864 }
1865 
1867  LLVMValueRef ElementValueConstant,
1868  unsigned *IdxList, unsigned NumIdx) {
1869  return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1870  unwrap<Constant>(ElementValueConstant),
1871  makeArrayRef(IdxList, NumIdx)));
1872 }
1873 
1874 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1875  const char *Constraints,
1876  LLVMBool HasSideEffects,
1877  LLVMBool IsAlignStack) {
1878  return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1879  Constraints, HasSideEffects, IsAlignStack));
1880 }
1881 
1883  return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1884 }
1885 
1886 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1887 
1889  return wrap(unwrap<GlobalValue>(Global)->getParent());
1890 }
1891 
1893  return unwrap<GlobalValue>(Global)->isDeclaration();
1894 }
1895 
1897  switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1899  return LLVMExternalLinkage;
1903  return LLVMLinkOnceAnyLinkage;
1905  return LLVMLinkOnceODRLinkage;
1907  return LLVMWeakAnyLinkage;
1909  return LLVMWeakODRLinkage;
1911  return LLVMAppendingLinkage;
1913  return LLVMInternalLinkage;
1915  return LLVMPrivateLinkage;
1917  return LLVMExternalWeakLinkage;
1919  return LLVMCommonLinkage;
1920  }
1921 
1922  llvm_unreachable("Invalid GlobalValue linkage!");
1923 }
1924 
1926  GlobalValue *GV = unwrap<GlobalValue>(Global);
1927 
1928  switch (Linkage) {
1929  case LLVMExternalLinkage:
1931  break;
1934  break;
1937  break;
1940  break;
1942  LLVM_DEBUG(
1943  errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1944  "longer supported.");
1945  break;
1946  case LLVMWeakAnyLinkage:
1948  break;
1949  case LLVMWeakODRLinkage:
1951  break;
1952  case LLVMAppendingLinkage:
1954  break;
1955  case LLVMInternalLinkage:
1957  break;
1958  case LLVMPrivateLinkage:
1960  break;
1963  break;
1966  break;
1967  case LLVMDLLImportLinkage:
1968  LLVM_DEBUG(
1969  errs()
1970  << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1971  break;
1972  case LLVMDLLExportLinkage:
1973  LLVM_DEBUG(
1974  errs()
1975  << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1976  break;
1979  break;
1980  case LLVMGhostLinkage:
1981  LLVM_DEBUG(
1982  errs() << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1983  break;
1984  case LLVMCommonLinkage:
1986  break;
1987  }
1988 }
1989 
1990 const char *LLVMGetSection(LLVMValueRef Global) {
1991  // Using .data() is safe because of how GlobalObject::setSection is
1992  // implemented.
1993  return unwrap<GlobalValue>(Global)->getSection().data();
1994 }
1995 
1996 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1997  unwrap<GlobalObject>(Global)->setSection(Section);
1998 }
1999 
2001  return static_cast<LLVMVisibility>(
2002  unwrap<GlobalValue>(Global)->getVisibility());
2003 }
2004 
2006  unwrap<GlobalValue>(Global)
2007  ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
2008 }
2009 
2011  return static_cast<LLVMDLLStorageClass>(
2012  unwrap<GlobalValue>(Global)->getDLLStorageClass());
2013 }
2014 
2016  unwrap<GlobalValue>(Global)->setDLLStorageClass(
2017  static_cast<GlobalValue::DLLStorageClassTypes>(Class));
2018 }
2019 
2021  switch (unwrap<GlobalValue>(Global)->getUnnamedAddr()) {
2023  return LLVMNoUnnamedAddr;
2025  return LLVMLocalUnnamedAddr;
2027  return LLVMGlobalUnnamedAddr;
2028  }
2029  llvm_unreachable("Unknown UnnamedAddr kind!");
2030 }
2031 
2033  GlobalValue *GV = unwrap<GlobalValue>(Global);
2034 
2035  switch (UnnamedAddr) {
2036  case LLVMNoUnnamedAddr:
2038  case LLVMLocalUnnamedAddr:
2040  case LLVMGlobalUnnamedAddr:
2042  }
2043 }
2044 
2046  return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
2047 }
2048 
2049 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
2050  unwrap<GlobalValue>(Global)->setUnnamedAddr(
2051  HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
2053 }
2054 
2056  return wrap(unwrap<GlobalValue>(Global)->getValueType());
2057 }
2058 
2059 /*--.. Operations on global variables, load and store instructions .........--*/
2060 
2062  Value *P = unwrap<Value>(V);
2063  if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2064  return GV->getAlignment();
2065  if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
2066  return AI->getAlignment();
2067  if (LoadInst *LI = dyn_cast<LoadInst>(P))
2068  return LI->getAlignment();
2069  if (StoreInst *SI = dyn_cast<StoreInst>(P))
2070  return SI->getAlignment();
2071  if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2072  return RMWI->getAlign().value();
2073  if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2074  return CXI->getAlign().value();
2075 
2077  "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, "
2078  "and AtomicCmpXchgInst have alignment");
2079 }
2080 
2081 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
2082  Value *P = unwrap<Value>(V);
2083  if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2084  GV->setAlignment(MaybeAlign(Bytes));
2085  else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
2086  AI->setAlignment(Align(Bytes));
2087  else if (LoadInst *LI = dyn_cast<LoadInst>(P))
2088  LI->setAlignment(Align(Bytes));
2089  else if (StoreInst *SI = dyn_cast<StoreInst>(P))
2090  SI->setAlignment(Align(Bytes));
2091  else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2092  RMWI->setAlignment(Align(Bytes));
2093  else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2094  CXI->setAlignment(Align(Bytes));
2095  else
2097  "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, and "
2098  "and AtomicCmpXchgInst have alignment");
2099 }
2100 
2102  size_t *NumEntries) {
2103  return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
2104  Entries.clear();
2105  if (Instruction *Instr = dyn_cast<Instruction>(unwrap(Value))) {
2106  Instr->getAllMetadata(Entries);
2107  } else {
2108  unwrap<GlobalObject>(Value)->getAllMetadata(Entries);
2109  }
2110  });
2111 }
2112 
2114  unsigned Index) {
2116  static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2117  return MVE.Kind;
2118 }
2119 
2122  unsigned Index) {
2124  static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2125  return MVE.Metadata;
2126 }
2127 
2129  free(Entries);
2130 }
2131 
2133  LLVMMetadataRef MD) {
2134  unwrap<GlobalObject>(Global)->setMetadata(Kind, unwrap<MDNode>(MD));
2135 }
2136 
2138  unwrap<GlobalObject>(Global)->eraseMetadata(Kind);
2139 }
2140 
2142  unwrap<GlobalObject>(Global)->clearMetadata();
2143 }
2144 
2145 /*--.. Operations on global variables ......................................--*/
2146 
2148  return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2149  GlobalValue::ExternalLinkage, nullptr, Name));
2150 }
2151 
2153  const char *Name,
2154  unsigned AddressSpace) {
2155  return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2158  AddressSpace));
2159 }
2160 
2162  return wrap(unwrap(M)->getNamedGlobal(Name));
2163 }
2164 
2166  Module *Mod = unwrap(M);
2168  if (I == Mod->global_end())
2169  return nullptr;
2170  return wrap(&*I);
2171 }
2172 
2174  Module *Mod = unwrap(M);
2176  if (I == Mod->global_begin())
2177  return nullptr;
2178  return wrap(&*--I);
2179 }
2180 
2182  GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2184  if (++I == GV->getParent()->global_end())
2185  return nullptr;
2186  return wrap(&*I);
2187 }
2188 
2190  GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2192  if (I == GV->getParent()->global_begin())
2193  return nullptr;
2194  return wrap(&*--I);
2195 }
2196 
2198  unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
2199 }
2200 
2202  GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
2203  if ( !GV->hasInitializer() )
2204  return nullptr;
2205  return wrap(GV->getInitializer());
2206 }
2207 
2209  unwrap<GlobalVariable>(GlobalVar)
2210  ->setInitializer(unwrap<Constant>(ConstantVal));
2211 }
2212 
2214  return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
2215 }
2216 
2218  unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
2219 }
2220 
2222  return unwrap<GlobalVariable>(GlobalVar)->isConstant();
2223 }
2224 
2226  unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
2227 }
2228 
2230  switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
2232  return LLVMNotThreadLocal;
2236  return LLVMLocalDynamicTLSModel;
2238  return LLVMInitialExecTLSModel;
2240  return LLVMLocalExecTLSModel;
2241  }
2242 
2243  llvm_unreachable("Invalid GlobalVariable thread local mode");
2244 }
2245 
2247  GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2248 
2249  switch (Mode) {
2250  case LLVMNotThreadLocal:
2252  break;
2255  break;
2258  break;
2261  break;
2262  case LLVMLocalExecTLSModel:
2264  break;
2265  }
2266 }
2267 
2269  return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
2270 }
2271 
2273  unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
2274 }
2275 
2276 /*--.. Operations on aliases ......................................--*/
2277 
2279  const char *Name) {
2280  auto *PTy = cast<PointerType>(unwrap(Ty));
2281  return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
2283  unwrap<Constant>(Aliasee), unwrap(M)));
2284 }
2285 
2287  unsigned AddrSpace, LLVMValueRef Aliasee,
2288  const char *Name) {
2289  return wrap(GlobalAlias::create(unwrap(ValueTy), AddrSpace,
2291  unwrap<Constant>(Aliasee), unwrap(M)));
2292 }
2293 
2295  const char *Name, size_t NameLen) {
2296  return wrap(unwrap(M)->getNamedAlias(Name));
2297 }
2298 
2300  Module *Mod = unwrap(M);
2302  if (I == Mod->alias_end())
2303  return nullptr;
2304  return wrap(&*I);
2305 }
2306 
2308  Module *Mod = unwrap(M);
2310  if (I == Mod->alias_begin())
2311  return nullptr;
2312  return wrap(&*--I);
2313 }
2314 
2316  GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2317  Module::alias_iterator I(Alias);
2318  if (++I == Alias->getParent()->alias_end())
2319  return nullptr;
2320  return wrap(&*I);
2321 }
2322 
2324  GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2325  Module::alias_iterator I(Alias);
2326  if (I == Alias->getParent()->alias_begin())
2327  return nullptr;
2328  return wrap(&*--I);
2329 }
2330 
2332  return wrap(unwrap<GlobalAlias>(Alias)->getAliasee());
2333 }
2334 
2336  unwrap<GlobalAlias>(Alias)->setAliasee(unwrap<Constant>(Aliasee));
2337 }
2338 
2339 /*--.. Operations on functions .............................................--*/
2340 
2342  LLVMTypeRef FunctionTy) {
2343  return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
2345 }
2346 
2348  return wrap(unwrap(M)->getFunction(Name));
2349 }
2350 
2352  Module *Mod = unwrap(M);
2353  Module::iterator I = Mod->begin();
2354  if (I == Mod->end())
2355  return nullptr;
2356  return wrap(&*I);
2357 }
2358 
2360  Module *Mod = unwrap(M);
2361  Module::iterator I = Mod->end();
2362  if (I == Mod->begin())
2363  return nullptr;
2364  return wrap(&*--I);
2365 }
2366 
2368  Function *Func = unwrap<Function>(Fn);
2369  Module::iterator I(Func);
2370  if (++I == Func->getParent()->end())
2371  return nullptr;
2372  return wrap(&*I);
2373 }
2374 
2376  Function *Func = unwrap<Function>(Fn);
2377  Module::iterator I(Func);
2378  if (I == Func->getParent()->begin())
2379  return nullptr;
2380  return wrap(&*--I);
2381 }
2382 
2384  unwrap<Function>(Fn)->eraseFromParent();
2385 }
2386 
2388  return unwrap<Function>(Fn)->hasPersonalityFn();
2389 }
2390 
2392  return wrap(unwrap<Function>(Fn)->getPersonalityFn());
2393 }
2394 
2396  unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
2397 }
2398 
2400  if (Function *F = dyn_cast<Function>(unwrap(Fn)))
2401  return F->getIntrinsicID();
2402  return 0;
2403 }
2404 
2406  assert(ID < llvm::Intrinsic::num_intrinsics && "Intrinsic ID out of range");
2407  return llvm::Intrinsic::ID(ID);
2408 }
2409 
2411  unsigned ID,
2412  LLVMTypeRef *ParamTypes,
2413  size_t ParamCount) {
2414  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2415  auto IID = llvm_map_to_intrinsic_id(ID);
2416  return wrap(llvm::Intrinsic::getDeclaration(unwrap(Mod), IID, Tys));
2417 }
2418 
2419 const char *LLVMIntrinsicGetName(unsigned ID, size_t *NameLength) {
2420  auto IID = llvm_map_to_intrinsic_id(ID);
2421  auto Str = llvm::Intrinsic::getName(IID);
2422  *NameLength = Str.size();
2423  return Str.data();
2424 }
2425 
2427  LLVMTypeRef *ParamTypes, size_t ParamCount) {
2428  auto IID = llvm_map_to_intrinsic_id(ID);
2429  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2430  return wrap(llvm::Intrinsic::getType(*unwrap(Ctx), IID, Tys));
2431 }
2432 
2434  LLVMTypeRef *ParamTypes,
2435  size_t ParamCount,
2436  size_t *NameLength) {
2437  auto IID = llvm_map_to_intrinsic_id(ID);
2438  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2439  auto Str = llvm::Intrinsic::getNameNoUnnamedTypes(IID, Tys);
2440  *NameLength = Str.length();
2441  return strdup(Str.c_str());
2442 }
2443 
2445  LLVMTypeRef *ParamTypes,
2446  size_t ParamCount,
2447  size_t *NameLength) {
2448  auto IID = llvm_map_to_intrinsic_id(ID);
2449  ArrayRef<Type *> Tys(unwrap(ParamTypes), ParamCount);
2450  auto Str = llvm::Intrinsic::getName(IID, Tys, unwrap(Mod));
2451  *NameLength = Str.length();
2452  return strdup(Str.c_str());
2453 }
2454 
2455 unsigned LLVMLookupIntrinsicID(const char *Name, size_t NameLen) {
2456  return Function::lookupIntrinsicID({Name, NameLen});
2457 }
2458 
2460  auto IID = llvm_map_to_intrinsic_id(ID);
2461  return llvm::Intrinsic::isOverloaded(IID);
2462 }
2463 
2465  return unwrap<Function>(Fn)->getCallingConv();
2466 }
2467 
2468 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
2469  return unwrap<Function>(Fn)->setCallingConv(
2470  static_cast<CallingConv::ID>(CC));
2471 }
2472 
2473 const char *LLVMGetGC(LLVMValueRef Fn) {
2474  Function *F = unwrap<Function>(Fn);
2475  return F->hasGC()? F->getGC().c_str() : nullptr;
2476 }
2477 
2478 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
2479  Function *F = unwrap<Function>(Fn);
2480  if (GC)
2481  F->setGC(GC);
2482  else
2483  F->clearGC();
2484 }
2485 
2487  LLVMAttributeRef A) {
2488  unwrap<Function>(F)->addAttributeAtIndex(Idx, unwrap(A));
2489 }
2490 
2492  auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2493  return AS.getNumAttributes();
2494 }
2495 
2498  auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2499  for (auto A : AS)
2500  *Attrs++ = wrap(A);
2501 }
2502 
2504  LLVMAttributeIndex Idx,
2505  unsigned KindID) {
2506  return wrap(unwrap<Function>(F)->getAttributeAtIndex(
2507  Idx, (Attribute::AttrKind)KindID));
2508 }
2509 
2511  LLVMAttributeIndex Idx,
2512  const char *K, unsigned KLen) {
2513  return wrap(
2514  unwrap<Function>(F)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
2515 }
2516 
2518  unsigned KindID) {
2519  unwrap<Function>(F)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
2520 }
2521 
2523  const char *K, unsigned KLen) {
2524  unwrap<Function>(F)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
2525 }
2526 
2528  const char *V) {
2529  Function *Func = unwrap<Function>(Fn);
2530  Attribute Attr = Attribute::get(Func->getContext(), A, V);
2531  Func->addFnAttr(Attr);
2532 }
2533 
2534 /*--.. Operations on parameters ............................................--*/
2535 
2537  // This function is strictly redundant to
2538  // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
2539  return unwrap<Function>(FnRef)->arg_size();
2540 }
2541 
2542 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
2543  Function *Fn = unwrap<Function>(FnRef);
2544  for (Argument &A : Fn->args())
2545  *ParamRefs++ = wrap(&A);
2546 }
2547 
2549  Function *Fn = unwrap<Function>(FnRef);
2550  return wrap(&Fn->arg_begin()[index]);
2551 }
2552 
2554  return wrap(unwrap<Argument>(V)->getParent());
2555 }
2556 
2558  Function *Func = unwrap<Function>(Fn);
2559  Function::arg_iterator I = Func->arg_begin();
2560  if (I == Func->arg_end())
2561  return nullptr;
2562  return wrap(&*I);
2563 }
2564 
2566  Function *Func = unwrap<Function>(Fn);
2567  Function::arg_iterator I = Func->arg_end();
2568  if (I == Func->arg_begin())
2569  return nullptr;
2570  return wrap(&*--I);
2571 }
2572 
2574  Argument *A = unwrap<Argument>(Arg);
2575  Function *Fn = A->getParent();
2576  if (A->getArgNo() + 1 >= Fn->arg_size())
2577  return nullptr;
2578  return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
2579 }
2580 
2582  Argument *A = unwrap<Argument>(Arg);
2583  if (A->getArgNo() == 0)
2584  return nullptr;
2585  return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
2586 }
2587 
2589  Argument *A = unwrap<Argument>(Arg);
2590  A->addAttr(Attribute::getWithAlignment(A->getContext(), Align(align)));
2591 }
2592 
2593 /*--.. Operations on ifuncs ................................................--*/
2594 
2596  const char *Name, size_t NameLen,
2597  LLVMTypeRef Ty, unsigned AddrSpace,
2599  return wrap(GlobalIFunc::create(unwrap(Ty), AddrSpace,
2601  StringRef(Name, NameLen),
2602  unwrap<Constant>(Resolver), unwrap(M)));
2603 }
2604 
2606  const char *Name, size_t NameLen) {
2607  return wrap(unwrap(M)->getNamedIFunc(StringRef(Name, NameLen)));
2608 }
2609 
2611  Module *Mod = unwrap(M);
2613  if (I == Mod->ifunc_end())
2614  return nullptr;
2615  return wrap(&*I);
2616 }
2617 
2619  Module *Mod = unwrap(M);
2621  if (I == Mod->ifunc_begin())
2622  return nullptr;
2623  return wrap(&*--I);
2624 }
2625 
2627  GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2629  if (++I == GIF->getParent()->ifunc_end())
2630  return nullptr;
2631  return wrap(&*I);
2632 }
2633 
2635  GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2637  if (I == GIF->getParent()->ifunc_begin())
2638  return nullptr;
2639  return wrap(&*--I);
2640 }
2641 
2643  return wrap(unwrap<GlobalIFunc>(IFunc)->getResolver());
2644 }
2645 
2647  unwrap<GlobalIFunc>(IFunc)->setResolver(unwrap<Constant>(Resolver));
2648 }
2649 
2651  unwrap<GlobalIFunc>(IFunc)->eraseFromParent();
2652 }
2653 
2655  unwrap<GlobalIFunc>(IFunc)->removeFromParent();
2656 }
2657 
2658 /*--.. Operations on basic blocks ..........................................--*/
2659 
2661  return wrap(static_cast<Value*>(unwrap(BB)));
2662 }
2663 
2665  return isa<BasicBlock>(unwrap(Val));
2666 }
2667 
2669  return wrap(unwrap<BasicBlock>(Val));
2670 }
2671 
2673  return unwrap(BB)->getName().data();
2674 }
2675 
2677  return wrap(unwrap(BB)->getParent());
2678 }
2679 
2681  return wrap(unwrap(BB)->getTerminator());
2682 }
2683 
2685  return unwrap<Function>(FnRef)->size();
2686 }
2687 
2688 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
2689  Function *Fn = unwrap<Function>(FnRef);
2690  for (BasicBlock &BB : *Fn)
2691  *BasicBlocksRefs++ = wrap(&BB);
2692 }
2693 
2695  return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2696 }
2697 
2699  Function *Func = unwrap<Function>(Fn);
2700  Function::iterator I = Func->begin();
2701  if (I == Func->end())
2702  return nullptr;
2703  return wrap(&*I);
2704 }
2705 
2707  Function *Func = unwrap<Function>(Fn);
2708  Function::iterator I = Func->end();
2709  if (I == Func->begin())
2710  return nullptr;
2711  return wrap(&*--I);
2712 }
2713 
2715  BasicBlock *Block = unwrap(BB);
2716  Function::iterator I(Block);
2717  if (++I == Block->getParent()->end())
2718  return nullptr;
2719  return wrap(&*I);
2720 }
2721 
2723  BasicBlock *Block = unwrap(BB);
2724  Function::iterator I(Block);
2725  if (I == Block->getParent()->begin())
2726  return nullptr;
2727  return wrap(&*--I);
2728 }
2729 
2731  const char *Name) {
2733 }
2734 
2737  BasicBlock *ToInsert = unwrap(BB);
2738  BasicBlock *CurBB = unwrap(Builder)->GetInsertBlock();
2739  assert(CurBB && "current insertion point is invalid!");
2740  CurBB->getParent()->getBasicBlockList().insertAfter(CurBB->getIterator(),
2741  ToInsert);
2742 }
2743 
2746  unwrap<Function>(Fn)->getBasicBlockList().push_back(unwrap(BB));
2747 }
2748 
2750  LLVMValueRef FnRef,
2751  const char *Name) {
2752  return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
2753 }
2754 
2757 }
2758 
2760  LLVMBasicBlockRef BBRef,
2761  const char *Name) {
2762  BasicBlock *BB = unwrap(BBRef);
2763  return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2764 }
2765 
2767  const char *Name) {
2769 }
2770 
2772  unwrap(BBRef)->eraseFromParent();
2773 }
2774 
2776  unwrap(BBRef)->removeFromParent();
2777 }
2778 
2780  unwrap(BB)->moveBefore(unwrap(MovePos));
2781 }
2782 
2784  unwrap(BB)->moveAfter(unwrap(MovePos));
2785 }
2786 
2787 /*--.. Operations on instructions ..........................................--*/
2788 
2790  return wrap(unwrap<Instruction>(Inst)->getParent());
2791 }
2792 
2794  BasicBlock *Block = unwrap(BB);
2795  BasicBlock::iterator I = Block->begin();
2796  if (I == Block->end())
2797  return nullptr;
2798  return wrap(&*I);
2799 }
2800 
2802  BasicBlock *Block = unwrap(BB);
2803  BasicBlock::iterator I = Block->end();
2804  if (I == Block->begin())
2805  return nullptr;
2806  return wrap(&*--I);
2807 }
2808 
2810  Instruction *Instr = unwrap<Instruction>(Inst);
2811  BasicBlock::iterator I(Instr);
2812  if (++I == Instr->getParent()->end())
2813  return nullptr;
2814  return wrap(&*I);
2815 }
2816 
2818  Instruction *Instr = unwrap<Instruction>(Inst);
2819  BasicBlock::iterator I(Instr);
2820  if (I == Instr->getParent()->begin())
2821  return nullptr;
2822  return wrap(&*--I);
2823 }
2824 
2826  unwrap<Instruction>(Inst)->removeFromParent();
2827 }
2828 
2830  unwrap<Instruction>(Inst)->eraseFromParent();
2831 }
2832 
2834  if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2835  return (LLVMIntPredicate)I->getPredicate();
2836  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2837  if (CE->getOpcode() == Instruction::ICmp)
2838  return (LLVMIntPredicate)CE->getPredicate();
2839  return (LLVMIntPredicate)0;
2840 }
2841 
2843  if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2844  return (LLVMRealPredicate)I->getPredicate();
2845  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2846  if (CE->getOpcode() == Instruction::FCmp)
2847  return (LLVMRealPredicate)CE->getPredicate();
2848  return (LLVMRealPredicate)0;
2849 }
2850 
2852  if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2853  return map_to_llvmopcode(C->getOpcode());
2854  return (LLVMOpcode)0;
2855 }
2856 
2858  if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2859  return wrap(C->clone());
2860  return nullptr;
2861 }
2862 
2864  Instruction *I = dyn_cast<Instruction>(unwrap(Inst));
2865  return (I && I->isTerminator()) ? wrap(I) : nullptr;
2866 }
2867 
2869  if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) {
2870  return FPI->getNumArgOperands();
2871  }
2872  return unwrap<CallBase>(Instr)->arg_size();
2873 }
2874 
2875 /*--.. Call and invoke instructions ........................................--*/
2876 
2878  return unwrap<CallBase>(Instr)->getCallingConv();
2879 }
2880 
2881 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
2882  return unwrap<CallBase>(Instr)->setCallingConv(
2883  static_cast<CallingConv::ID>(CC));
2884 }
2885 
2887  unsigned align) {
2888  auto *Call = unwrap<CallBase>(Instr);
2889  Attribute AlignAttr =
2890  Attribute::getWithAlignment(Call->getContext(), Align(align));
2891  Call->addAttributeAtIndex(Idx, AlignAttr);
2892 }
2893 
2895  LLVMAttributeRef A) {
2896  unwrap<CallBase>(C)->addAttributeAtIndex(Idx, unwrap(A));
2897 }
2898 
2900  LLVMAttributeIndex Idx) {
2901  auto *Call = unwrap<CallBase>(C);
2902  auto AS = Call->getAttributes().getAttributes(Idx);
2903  return AS.getNumAttributes();
2904 }
2905 
2908  auto *Call = unwrap<CallBase>(C);
2909  auto AS = Call->getAttributes().getAttributes(Idx);
2910  for (auto A : AS)
2911  *Attrs++ = wrap(A);
2912 }
2913 
2915  LLVMAttributeIndex Idx,
2916  unsigned KindID) {
2917  return wrap(unwrap<CallBase>(C)->getAttributeAtIndex(
2918  Idx, (Attribute::AttrKind)KindID));
2919 }
2920 
2922  LLVMAttributeIndex Idx,
2923  const char *K, unsigned KLen) {
2924  return wrap(
2925  unwrap<CallBase>(C)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
2926 }
2927 
2929  unsigned KindID) {
2930  unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
2931 }
2932 
2934  const char *K, unsigned KLen) {
2935  unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
2936 }
2937 
2939  return wrap(unwrap<CallBase>(Instr)->getCalledOperand());
2940 }
2941 
2943  return wrap(unwrap<CallBase>(Instr)->getFunctionType());
2944 }
2945 
2946 /*--.. Operations on call instructions (only) ..............................--*/
2947 
2949  return unwrap<CallInst>(Call)->isTailCall();
2950 }
2951 
2952 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
2953  unwrap<CallInst>(Call)->setTailCall(isTailCall);
2954 }
2955 
2956 /*--.. Operations on invoke instructions (only) ............................--*/
2957 
2959  return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2960 }
2961 
2963  if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2964  return wrap(CRI->getUnwindDest());
2965  } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2966  return wrap(CSI->getUnwindDest());
2967  }
2968  return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2969 }
2970 
2972  unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2973 }
2974 
2976  if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2977  return CRI->setUnwindDest(unwrap(B));
2978  } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2979  return CSI->setUnwindDest(unwrap(B));
2980  }
2981  unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2982 }
2983 
2984 /*--.. Operations on terminators ...........................................--*/
2985 
2987  return unwrap<Instruction>(Term)->getNumSuccessors();
2988 }
2989 
2991  return wrap(unwrap<Instruction>(Term)->getSuccessor(i));
2992 }
2993 
2995  return unwrap<Instruction>(Term)->setSuccessor(i, unwrap(block));
2996 }
2997 
2998 /*--.. Operations on branch instructions (only) ............................--*/
2999 
3001  return unwrap<BranchInst>(Branch)->isConditional();
3002 }
3003 
3005  return wrap(unwrap<BranchInst>(Branch)->getCondition());
3006 }
3007 
3009  return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
3010 }
3011 
3012 /*--.. Operations on switch instructions (only) ............................--*/
3013 
3015  return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
3016 }
3017 
3018 /*--.. Operations on alloca instructions (only) ............................--*/
3019 
3021  return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
3022 }
3023 
3024 /*--.. Operations on gep instructions (only) ...............................--*/
3025 
3027  return unwrap<GEPOperator>(GEP)->isInBounds();
3028 }
3029 
3031  return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
3032 }
3033 
3035  return wrap(unwrap<GEPOperator>(GEP)->getSourceElementType());
3036 }
3037 
3038 /*--.. Operations on phi nodes .............................................--*/
3039 
3040 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
3041  LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
3042  PHINode *PhiVal = unwrap<PHINode>(PhiNode);
3043  for (unsigned I = 0; I != Count; ++I)
3044  PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
3045 }
3046 
3047 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
3048  return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
3049 }
3050 
3052  return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
3053 }
3054 
3056  return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
3057 }
3058 
3059 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/
3060 
3062  auto *I = unwrap(Inst);
3063  if (auto *GEP = dyn_cast<GEPOperator>(I))
3064  return GEP->getNumIndices();
3065  if (auto *EV = dyn_cast<ExtractValueInst>(I))
3066  return EV->getNumIndices();
3067  if (auto *IV = dyn_cast<InsertValueInst>(I))
3068  return IV->getNumIndices();
3069  if (auto *CE = dyn_cast<ConstantExpr>(I))
3070  return CE->getIndices().size();
3072  "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
3073 }
3074 
3075 const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
3076  auto *I = unwrap(Inst);
3077  if (auto *EV = dyn_cast<ExtractValueInst>(I))
3078  return EV->getIndices().data();
3079  if (auto *IV = dyn_cast<InsertValueInst>(I))
3080  return IV->getIndices().data();
3081  if (auto *CE = dyn_cast<ConstantExpr>(I))
3082  return CE->getIndices().data();
3084  "LLVMGetIndices applies only to extractvalue and insertvalue!");
3085 }
3086 
3087 
3088 /*===-- Instruction builders ----------------------------------------------===*/
3089 
3091  return wrap(new IRBuilder<>(*unwrap(C)));
3092 }
3093 
3096 }
3097 
3099  LLVMValueRef Instr) {
3100  BasicBlock *BB = unwrap(Block);
3101  auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
3102  unwrap(Builder)->SetInsertPoint(BB, I);
3103 }
3104 
3106  Instruction *I = unwrap<Instruction>(Instr);
3107  unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
3108 }
3109 
3111  BasicBlock *BB = unwrap(Block);
3112  unwrap(Builder)->SetInsertPoint(BB);
3113 }
3114 
3116  return wrap(unwrap(Builder)->GetInsertBlock());
3117 }
3118 
3120  unwrap(Builder)->ClearInsertionPoint();
3121 }
3122 
3124  unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
3125 }
3126 
3128  const char *Name) {
3129  unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
3130 }
3131 
3133  delete unwrap(Builder);
3134 }
3135 
3136 /*--.. Metadata builders ...................................................--*/
3137 
3139  return wrap(unwrap(Builder)->getCurrentDebugLocation().getAsMDNode());
3140 }
3141 
3143  if (Loc)
3144  unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(unwrap<MDNode>(Loc)));
3145  else
3146  unwrap(Builder)->SetCurrentDebugLocation(DebugLoc());
3147 }
3148 
3150  MDNode *Loc =
3151  L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
3152  unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
3153 }
3154 
3156  LLVMContext &Context = unwrap(Builder)->getContext();
3157  return wrap(MetadataAsValue::get(
3158  Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
3159 }
3160 
3162  unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
3163 }
3164 
3166  unwrap(Builder)->AddMetadataToInst(unwrap<Instruction>(Inst));
3167 }
3168 
3170  LLVMMetadataRef FPMathTag) {
3171 
3172  unwrap(Builder)->setDefaultFPMathTag(FPMathTag
3173  ? unwrap<MDNode>(FPMathTag)
3174  : nullptr);
3175 }
3176 
3178  return wrap(unwrap(Builder)->getDefaultFPMathTag());
3179 }
3180 
3181 /*--.. Instruction builders ................................................--*/
3182 
3184  return wrap(unwrap(B)->CreateRetVoid());
3185 }
3186 
3188  return wrap(unwrap(B)->CreateRet(unwrap(V)));
3189 }
3190 
3192  unsigned N) {
3193  return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
3194 }
3195 
3197  return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
3198 }
3199 
3202  return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
3203 }
3204 
3206  LLVMBasicBlockRef Else, unsigned NumCases) {
3207  return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
3208 }
3209 
3211  unsigned NumDests) {
3212  return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
3213 }
3214 
3216  LLVMValueRef *Args, unsigned NumArgs,
3218  const char *Name) {
3219  Value *V = unwrap(Fn);
3220  FunctionType *FnT =
3221  cast<FunctionType>(cast<PointerType>(V->getType())->getElementType());
3222 
3223  return wrap(
3224  unwrap(B)->CreateInvoke(FnT, unwrap(Fn), unwrap(Then), unwrap(Catch),
3225  makeArrayRef(unwrap(Args), NumArgs), Name));
3226 }
3227 
3229  LLVMValueRef *Args, unsigned NumArgs,
3231  const char *Name) {
3232  return wrap(unwrap(B)->CreateInvoke(
3233  unwrap<FunctionType>(Ty), unwrap(Fn), unwrap(Then), unwrap(Catch),
3234  makeArrayRef(unwrap(Args), NumArgs), Name));
3235 }
3236 
3238  LLVMValueRef PersFn, unsigned NumClauses,
3239  const char *Name) {
3240  // The personality used to live on the landingpad instruction, but now it
3241  // lives on the parent function. For compatibility, take the provided
3242  // personality and put it on the parent function.
3243  if (PersFn)
3244  unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
3245  cast<Function>(unwrap(PersFn)));
3246  return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
3247 }
3248 
3250  LLVMValueRef *Args, unsigned NumArgs,
3251  const char *Name) {
3252  return wrap(unwrap(B)->CreateCatchPad(unwrap(ParentPad),
3253  makeArrayRef(unwrap(Args), NumArgs),
3254  Name));
3255 }
3256 
3258  LLVMValueRef *Args, unsigned NumArgs,
3259  const char *Name) {
3260  if (ParentPad == nullptr) {
3261  Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3262  ParentPad = wrap(Constant::getNullValue(Ty));
3263  }
3264  return wrap(unwrap(B)->CreateCleanupPad(unwrap(ParentPad),
3265  makeArrayRef(unwrap(Args), NumArgs),
3266  Name));
3267 }
3268 
3270  return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
3271 }
3272 
3274  LLVMBasicBlockRef UnwindBB,
3275  unsigned NumHandlers, const char *Name) {
3276  if (ParentPad == nullptr) {
3277  Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3278  ParentPad = wrap(Constant::getNullValue(Ty));
3279  }
3280  return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(UnwindBB),
3281  NumHandlers, Name));
3282 }
3283 
3286  return wrap(unwrap(B)->CreateCatchRet(unwrap<CatchPadInst>(CatchPad),
3287  unwrap(BB)));
3288 }
3289 
3292  return wrap(unwrap(B)->CreateCleanupRet(unwrap<CleanupPadInst>(CatchPad),
3293  unwrap(BB)));
3294 }
3295 
3297  return wrap(unwrap(B)->CreateUnreachable());
3298 }
3299 
3301  LLVMBasicBlockRef Dest) {
3302  unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
3303 }
3304 
3306  unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
3307 }
3308 
3309 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
3310  return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
3311 }
3312 
3313 LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
3314  return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
3315 }
3316 
3318  unwrap<LandingPadInst>(LandingPad)->
3319  addClause(cast<Constant>(unwrap(ClauseVal)));
3320 }
3321 
3323  return unwrap<LandingPadInst>(LandingPad)->isCleanup();
3324 }
3325 
3326 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
3327  unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
3328 }
3329 
3331  unwrap<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Dest));
3332 }
3333 
3334 unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) {
3335  return unwrap<CatchSwitchInst>(CatchSwitch)->getNumHandlers();
3336 }
3337 
3338 void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) {
3339  CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(CatchSwitch);
3340  for (const BasicBlock *H : CSI->handlers())
3341  *Handlers++ = wrap(H);
3342 }
3343 
3345  return wrap(unwrap<CatchPadInst>(CatchPad)->getCatchSwitch());
3346 }
3347 
3349  unwrap<CatchPadInst>(CatchPad)
3350  ->setCatchSwitch(unwrap<CatchSwitchInst>(CatchSwitch));
3351 }
3352 
3353 /*--.. Funclets ...........................................................--*/
3354 
3356  return wrap(unwrap<FuncletPadInst>(Funclet)->getArgOperand(i));
3357 }
3358 
3359 void LLVMSetArgOperand(LLVMValueRef Funclet, unsigned i, LLVMValueRef value) {
3360  unwrap<FuncletPadInst>(Funclet)->setArgOperand(i, unwrap(value));
3361 }
3362 
3363 /*--.. Arithmetic ..........................................................--*/
3364 
3366  const char *Name) {
3367  return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
3368 }
3369 
3371  const char *Name) {
3372  return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
3373 }
3374 
3376  const char *Name) {
3377  return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
3378 }
3379 
3381  const char *Name) {
3382  return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
3383 }
3384 
3386  const char *Name) {
3387  return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
3388 }
3389 
3391  const char *Name) {
3392  return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
3393 }
3394 
3396  const char *Name) {
3397  return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
3398 }
3399 
3401  const char *Name) {
3402  return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
3403 }
3404 
3406  const char *Name) {
3407  return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
3408 }
3409 
3411  const char *Name) {
3412  return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
3413 }
3414 
3416  const char *Name) {
3417  return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
3418 }
3419 
3421  const char *Name) {
3422  return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
3423 }
3424 
3426  const char *Name) {
3427  return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
3428 }
3429 
3431  LLVMValueRef RHS, const char *Name) {
3432  return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
3433 }
3434 
3436  const char *Name) {
3437  return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
3438 }
3439 
3441  LLVMValueRef RHS, const char *Name) {
3442  return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
3443 }
3444 
3446  const char *Name) {
3447  return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
3448 }
3449 
3451  const char *Name) {
3452  return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
3453 }
3454 
3456  const char *Name) {
3457  return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
3458 }
3459 
3461  const char *Name) {
3462  return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
3463 }
3464 
3466  const char *Name) {
3467  return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
3468 }
3469 
3471  const char *Name) {
3472  return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
3473 }
3474 
3476  const char *Name) {
3477  return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
3478 }
3479 
3481  const char *Name) {
3482  return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
3483 }
3484 
3486  const char *Name) {
3487  return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
3488 }
3489 
3491  const char *Name) {
3492  return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
3493 }
3494 
3497  const char *Name) {
3499  unwrap(RHS), Name));
3500 }
3501 
3503  return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
3504 }
3505 
3507  const char *Name) {
3508  return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
3509 }
3510 
3512  const char *Name) {
3513  return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
3514 }
3515 
3517  return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
3518 }
3519 
3521  return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
3522 }
3523 
3524 /*--.. Memory ..............................................................--*/
3525 
3527  const char *Name) {
3528  Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3529  Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3530  AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3531  Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3532  ITy, unwrap(Ty), AllocSize,
3533  nullptr, nullptr, "");
3534  return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3535 }
3536 
3538  LLVMValueRef Val, const char *Name) {
3539  Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3540  Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3541  AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3542  Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3543  ITy, unwrap(Ty), AllocSize,
3544  unwrap(Val), nullptr, "");
3545  return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3546 }
3547 
3549  LLVMValueRef Val, LLVMValueRef Len,
3550  unsigned Align) {
3551  return wrap(unwrap(B)->CreateMemSet(unwrap(Ptr), unwrap(Val), unwrap(Len),
3552  MaybeAlign(Align)));
3553 }
3554 
3556  LLVMValueRef Dst, unsigned DstAlign,
3557  LLVMValueRef Src, unsigned SrcAlign,
3558  LLVMValueRef Size) {
3559  return wrap(unwrap(B)->CreateMemCpy(unwrap(Dst), MaybeAlign(DstAlign),
3560  unwrap(Src), MaybeAlign(SrcAlign),
3561  unwrap(Size)));
3562 }
3563 
3565  LLVMValueRef Dst, unsigned DstAlign,
3566  LLVMValueRef Src, unsigned SrcAlign,
3567  LLVMValueRef Size) {
3568  return wrap(unwrap(B)->CreateMemMove(unwrap(Dst), MaybeAlign(DstAlign),
3569  unwrap(Src), MaybeAlign(SrcAlign),
3570  unwrap(Size)));
3571 }
3572 
3574  const char *Name) {
3575  return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
3576 }
3577 
3579  LLVMValueRef Val, const char *Name) {
3580  return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
3581 }
3582 
3584  return wrap(unwrap(B)->Insert(
3585  CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
3586 }
3587 
3589  const char *Name) {
3590  Value *V = unwrap(PointerVal);
3591  PointerType *Ty = cast<PointerType>(V->getType());
3592 
3593  return wrap(unwrap(B)->CreateLoad(Ty->getElementType(), V, Name));
3594 }
3595 
3597  LLVMValueRef PointerVal, const char *Name) {
3598  return wrap(unwrap(B)->CreateLoad(unwrap(Ty), unwrap(PointerVal), Name));
3599 }
3600 
3602  LLVMValueRef PointerVal) {
3603  return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
3604 }
3605 
3607  switch (Ordering) {
3617  }
3618 
3619  llvm_unreachable("Invalid LLVMAtomicOrdering value!");
3620 }
3621 
3623  switch (Ordering) {
3633  }
3634 
3635  llvm_unreachable("Invalid AtomicOrdering value!");
3636 }
3637 
3639  switch (BinOp) {
3653  }
3654 
3655  llvm_unreachable("Invalid LLVMAtomicRMWBinOp value!");
3656 }
3657 
3659  switch (BinOp) {
3673  default: break;
3674  }
3675 
3676  llvm_unreachable("Invalid AtomicRMWBinOp value!");
3677 }
3678 
3679 // TODO: Should this and other atomic instructions support building with
3680 // "syncscope"?
3682  LLVMBool isSingleThread, const char *Name) {
3683  return wrap(
3684  unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
3685  isSingleThread ? SyncScope::SingleThread
3687  Name));
3688 }
3689 
3691  LLVMValueRef *Indices, unsigned NumIndices,
3692  const char *Name) {
3693  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3694  Value *Val = unwrap(Pointer);
3695  Type *Ty =
3696  cast<PointerType>(Val->getType()->getScalarType())->getElementType();
3697  return wrap(unwrap(B)->CreateGEP(Ty, Val, IdxList, Name));
3698 }
3699 
3701  LLVMValueRef Pointer, LLVMValueRef *Indices,
3702  unsigned NumIndices, const char *Name) {
3703  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3704  return wrap(unwrap(B)->CreateGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3705 }
3706 
3708  LLVMValueRef *Indices, unsigned NumIndices,
3709  const char *Name) {
3710  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3711  Value *Val = unwrap(Pointer);
3712  Type *Ty =
3713  cast<PointerType>(Val->getType()->getScalarType())->getElementType();
3714  return wrap(unwrap(B)->CreateInBoundsGEP(Ty, Val, IdxList, Name));
3715 }
3716 
3718  LLVMValueRef Pointer, LLVMValueRef *Indices,
3719  unsigned NumIndices, const char *Name) {
3720  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3721  return wrap(
3722  unwrap(B)->CreateInBoundsGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3723 }
3724 
3726  unsigned Idx, const char *Name) {
3727  Value *Val = unwrap(Pointer);
3728  Type *Ty =
3729  cast<PointerType>(Val->getType()->getScalarType())->getElementType();
3730  return wrap(unwrap(B)->CreateStructGEP(Ty, Val, Idx, Name));
3731 }
3732 
3734  LLVMValueRef Pointer, unsigned Idx,
3735  const char *Name) {
3736  return wrap(
3737  unwrap(B)->CreateStructGEP(unwrap(Ty), unwrap(Pointer), Idx, Name));
3738 }
3739 
3741  const char *Name) {
3742  return wrap(unwrap(B)->CreateGlobalString(Str, Name));
3743 }
3744 
3746  const char *Name) {
3747  return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
3748 }
3749 
3751  Value *P = unwrap<Value>(MemAccessInst);
3752  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3753  return LI->isVolatile();
3754  if (StoreInst *SI = dyn_cast<StoreInst>(P))
3755  return SI->isVolatile();
3756  if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
3757  return AI->isVolatile();
3758  return cast<AtomicCmpXchgInst>(P)->isVolatile();
3759 }
3760 
3761 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
3762  Value *P = unwrap<Value>(MemAccessInst);
3763  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3764  return LI->setVolatile(isVolatile);
3765  if (StoreInst *SI = dyn_cast<StoreInst>(P))
3766  return SI->setVolatile(isVolatile);
3767  if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
3768  return AI->setVolatile(isVolatile);
3769  return cast<AtomicCmpXchgInst>(P)->setVolatile(isVolatile);
3770 }
3771 
3773  return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->isWeak();
3774 }
3775 
3776 void LLVMSetWeak(LLVMValueRef CmpXchgInst, LLVMBool isWeak) {
3777  return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->setWeak(isWeak);
3778 }
3779 
3781  Value *P = unwrap<Value>(MemAccessInst);
3782  AtomicOrdering O;
3783  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3784  O = LI->getOrdering();
3785  else if (StoreInst *SI = dyn_cast<StoreInst>(P))
3786  O = SI->getOrdering();
3787  else
3788  O = cast<AtomicRMWInst>(P)->getOrdering();
3789  return mapToLLVMOrdering(O);
3790 }
3791 
3792 void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
3793  Value *P = unwrap<Value>(MemAccessInst);
3794  AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3795 
3796  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3797  return LI->setOrdering(O);
3798  return cast<StoreInst>(P)->setOrdering(O);
3799 }
3800 
3802  return mapToLLVMRMWBinOp(unwrap<AtomicRMWInst>(Inst)->getOperation());
3803 }
3804 
3806  unwrap<AtomicRMWInst>(Inst)->setOperation(mapFromLLVMRMWBinOp(BinOp));
3807 }
3808 
3809 /*--.. Casts ...............................................................--*/
3810 
3812  LLVMTypeRef DestTy, const char *Name) {
3813  return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
3814 }
3815 
3817  LLVMTypeRef DestTy, const char *Name) {
3818  return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
3819 }
3820 
3822  LLVMTypeRef DestTy, const char *Name) {
3823  return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
3824 }
3825 
3827  LLVMTypeRef DestTy, const char *Name) {
3828  return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
3829 }
3830 
3832  LLVMTypeRef DestTy, const char *Name) {
3833  return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
3834 }
3835 
3837  LLVMTypeRef DestTy, const char *Name) {
3838  return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
3839 }
3840 
3842  LLVMTypeRef DestTy, const char *Name) {
3843  return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
3844 }
3845 
3847  LLVMTypeRef DestTy, const char *Name) {
3848  return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
3849 }
3850 
3852  LLVMTypeRef DestTy, const char *Name) {
3853  return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
3854 }
3855 
3857  LLVMTypeRef DestTy, const char *Name) {
3858  return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
3859 }
3860 
3862  LLVMTypeRef DestTy, const char *Name) {
3863  return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
3864 }
3865 
3867  LLVMTypeRef DestTy, const char *Name) {
3868  return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
3869 }
3870