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, NULL));
146  }
147 
148  if (AttrKind == Attribute::AttrKind::StructRet) {
149  // Same as byval.
150  return wrap(Attribute::getWithStructRetType(Ctx, NULL));
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,
1701  unsigned NumIndices) {
1702  ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1703  NumIndices);
1704  Constant *Val = unwrap<Constant>(ConstantVal);
1705  Type *Ty =
1706  cast<PointerType>(Val->getType()->getScalarType())->getElementType();
1707  return wrap(ConstantExpr::getInBoundsGetElementPtr(Ty, Val, IdxList));
1708 }
1709 
1711  return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1712  unwrap(ToType)));
1713 }
1714 
1716  return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1717  unwrap(ToType)));
1718 }
1719 
1721  return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1722  unwrap(ToType)));
1723 }
1724 
1726  return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1727  unwrap(ToType)));
1728 }
1729 
1731  return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1732  unwrap(ToType)));
1733 }
1734 
1736  return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1737  unwrap(ToType)));
1738 }
1739 
1741  return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1742  unwrap(ToType)));
1743 }
1744 
1746  return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1747  unwrap(ToType)));
1748 }
1749 
1751  return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1752  unwrap(ToType)));
1753 }
1754 
1756  return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1757  unwrap(ToType)));
1758 }
1759 
1761  return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1762  unwrap(ToType)));
1763 }
1764 
1766  return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1767  unwrap(ToType)));
1768 }
1769 
1771  LLVMTypeRef ToType) {
1772  return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1773  unwrap(ToType)));
1774 }
1775 
1777  LLVMTypeRef ToType) {
1778  return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1779  unwrap(ToType)));
1780 }
1781 
1783  LLVMTypeRef ToType) {
1784  return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1785  unwrap(ToType)));
1786 }
1787 
1789  LLVMTypeRef ToType) {
1790  return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1791  unwrap(ToType)));
1792 }
1793 
1795  LLVMTypeRef ToType) {
1796  return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1797  unwrap(ToType)));
1798 }
1799 
1801  LLVMBool isSigned) {
1802  return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1803  unwrap(ToType), isSigned));
1804 }
1805 
1807  return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1808  unwrap(ToType)));
1809 }
1810 
1812  LLVMValueRef ConstantIfTrue,
1813  LLVMValueRef ConstantIfFalse) {
1814  return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1815  unwrap<Constant>(ConstantIfTrue),
1816  unwrap<Constant>(ConstantIfFalse)));
1817 }
1818 
1820  LLVMValueRef IndexConstant) {
1821  return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1822  unwrap<Constant>(IndexConstant)));
1823 }
1824 
1826  LLVMValueRef ElementValueConstant,
1827  LLVMValueRef IndexConstant) {
1828  return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1829  unwrap<Constant>(ElementValueConstant),
1830  unwrap<Constant>(IndexConstant)));
1831 }
1832 
1834  LLVMValueRef VectorBConstant,
1835  LLVMValueRef MaskConstant) {
1836  SmallVector<int, 16> IntMask;
1837  ShuffleVectorInst::getShuffleMask(unwrap<Constant>(MaskConstant), IntMask);
1838  return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1839  unwrap<Constant>(VectorBConstant),
1840  IntMask));
1841 }
1842 
1843 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1844  unsigned NumIdx) {
1845  return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1846  makeArrayRef(IdxList, NumIdx)));
1847 }
1848 
1850  LLVMValueRef ElementValueConstant,
1851  unsigned *IdxList, unsigned NumIdx) {
1852  return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1853  unwrap<Constant>(ElementValueConstant),
1854  makeArrayRef(IdxList, NumIdx)));
1855 }
1856 
1857 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1858  const char *Constraints,
1859  LLVMBool HasSideEffects,
1860  LLVMBool IsAlignStack) {
1861  return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1862  Constraints, HasSideEffects, IsAlignStack));
1863 }
1864 
1866  return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1867 }
1868 
1869 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1870 
1872  return wrap(unwrap<GlobalValue>(Global)->getParent());
1873 }
1874 
1876  return unwrap<GlobalValue>(Global)->isDeclaration();
1877 }
1878 
1880  switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1882  return LLVMExternalLinkage;
1886  return LLVMLinkOnceAnyLinkage;
1888  return LLVMLinkOnceODRLinkage;
1890  return LLVMWeakAnyLinkage;
1892  return LLVMWeakODRLinkage;
1894  return LLVMAppendingLinkage;
1896  return LLVMInternalLinkage;
1898  return LLVMPrivateLinkage;
1900  return LLVMExternalWeakLinkage;
1902  return LLVMCommonLinkage;
1903  }
1904 
1905  llvm_unreachable("Invalid GlobalValue linkage!");
1906 }
1907 
1909  GlobalValue *GV = unwrap<GlobalValue>(Global);
1910 
1911  switch (Linkage) {
1912  case LLVMExternalLinkage:
1914  break;
1917  break;
1920  break;
1923  break;
1925  LLVM_DEBUG(
1926  errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1927  "longer supported.");
1928  break;
1929  case LLVMWeakAnyLinkage:
1931  break;
1932  case LLVMWeakODRLinkage:
1934  break;
1935  case LLVMAppendingLinkage:
1937  break;
1938  case LLVMInternalLinkage:
1940  break;
1941  case LLVMPrivateLinkage:
1943  break;
1946  break;
1949  break;
1950  case LLVMDLLImportLinkage:
1951  LLVM_DEBUG(
1952  errs()
1953  << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1954  break;
1955  case LLVMDLLExportLinkage:
1956  LLVM_DEBUG(
1957  errs()
1958  << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1959  break;
1962  break;
1963  case LLVMGhostLinkage:
1964  LLVM_DEBUG(
1965  errs() << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1966  break;
1967  case LLVMCommonLinkage:
1969  break;
1970  }
1971 }
1972 
1973 const char *LLVMGetSection(LLVMValueRef Global) {
1974  // Using .data() is safe because of how GlobalObject::setSection is
1975  // implemented.
1976  return unwrap<GlobalValue>(Global)->getSection().data();
1977 }
1978 
1979 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1980  unwrap<GlobalObject>(Global)->setSection(Section);
1981 }
1982 
1984  return static_cast<LLVMVisibility>(
1985  unwrap<GlobalValue>(Global)->getVisibility());
1986 }
1987 
1989  unwrap<GlobalValue>(Global)
1990  ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1991 }
1992 
1994  return static_cast<LLVMDLLStorageClass>(
1995  unwrap<GlobalValue>(Global)->getDLLStorageClass());
1996 }
1997 
1999  unwrap<GlobalValue>(Global)->setDLLStorageClass(
2000  static_cast<GlobalValue::DLLStorageClassTypes>(Class));
2001 }
2002 
2004  switch (unwrap<GlobalValue>(Global)->getUnnamedAddr()) {
2006  return LLVMNoUnnamedAddr;
2008  return LLVMLocalUnnamedAddr;
2010  return LLVMGlobalUnnamedAddr;
2011  }
2012  llvm_unreachable("Unknown UnnamedAddr kind!");
2013 }
2014 
2016  GlobalValue *GV = unwrap<GlobalValue>(Global);
2017 
2018  switch (UnnamedAddr) {
2019  case LLVMNoUnnamedAddr:
2021  case LLVMLocalUnnamedAddr:
2023  case LLVMGlobalUnnamedAddr:
2025  }
2026 }
2027 
2029  return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
2030 }
2031 
2032 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
2033  unwrap<GlobalValue>(Global)->setUnnamedAddr(
2034  HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
2036 }
2037 
2039  return wrap(unwrap<GlobalValue>(Global)->getValueType());
2040 }
2041 
2042 /*--.. Operations on global variables, load and store instructions .........--*/
2043 
2045  Value *P = unwrap<Value>(V);
2046  if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2047  return GV->getAlignment();
2048  if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
2049  return AI->getAlignment();
2050  if (LoadInst *LI = dyn_cast<LoadInst>(P))
2051  return LI->getAlignment();
2052  if (StoreInst *SI = dyn_cast<StoreInst>(P))
2053  return SI->getAlignment();
2054  if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2055  return RMWI->getAlign().value();
2056  if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2057  return CXI->getAlign().value();
2058 
2060  "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, "
2061  "and AtomicCmpXchgInst have alignment");
2062 }
2063 
2064 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
2065  Value *P = unwrap<Value>(V);
2066  if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2067  GV->setAlignment(MaybeAlign(Bytes));
2068  else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
2069  AI->setAlignment(Align(Bytes));
2070  else if (LoadInst *LI = dyn_cast<LoadInst>(P))
2071  LI->setAlignment(Align(Bytes));
2072  else if (StoreInst *SI = dyn_cast<StoreInst>(P))
2073  SI->setAlignment(Align(Bytes));
2074  else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2075  RMWI->setAlignment(Align(Bytes));
2076  else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2077  CXI->setAlignment(Align(Bytes));
2078  else
2080  "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, and "
2081  "and AtomicCmpXchgInst have alignment");
2082 }
2083 
2085  size_t *NumEntries) {
2086  return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
2087  Entries.clear();
2088  if (Instruction *Instr = dyn_cast<Instruction>(unwrap(Value))) {
2089  Instr->getAllMetadata(Entries);
2090  } else {
2091  unwrap<GlobalObject>(Value)->getAllMetadata(Entries);
2092  }
2093  });
2094 }
2095 
2097  unsigned Index) {
2099  static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2100  return MVE.Kind;
2101 }
2102 
2105  unsigned Index) {
2107  static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2108  return MVE.Metadata;
2109 }
2110 
2112  free(Entries);
2113 }
2114 
2116  LLVMMetadataRef MD) {
2117  unwrap<GlobalObject>(Global)->setMetadata(Kind, unwrap<MDNode>(MD));
2118 }
2119 
2121  unwrap<GlobalObject>(Global)->eraseMetadata(Kind);
2122 }
2123 
2125  unwrap<GlobalObject>(Global)->clearMetadata();
2126 }
2127 
2128 /*--.. Operations on global variables ......................................--*/
2129 
2131  return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2132  GlobalValue::ExternalLinkage, nullptr, Name));
2133 }
2134 
2136  const char *Name,
2137  unsigned AddressSpace) {
2138  return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2141  AddressSpace));
2142 }
2143 
2145  return wrap(unwrap(M)->getNamedGlobal(Name));
2146 }
2147 
2149  Module *Mod = unwrap(M);
2151  if (I == Mod->global_end())
2152  return nullptr;
2153  return wrap(&*I);
2154 }
2155 
2157  Module *Mod = unwrap(M);
2159  if (I == Mod->global_begin())
2160  return nullptr;
2161  return wrap(&*--I);
2162 }
2163 
2165  GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2167  if (++I == GV->getParent()->global_end())
2168  return nullptr;
2169  return wrap(&*I);
2170 }
2171 
2173  GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2175  if (I == GV->getParent()->global_begin())
2176  return nullptr;
2177  return wrap(&*--I);
2178 }
2179 
2181  unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
2182 }
2183 
2185  GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
2186  if ( !GV->hasInitializer() )
2187  return nullptr;
2188  return wrap(GV->getInitializer());
2189 }
2190 
2192  unwrap<GlobalVariable>(GlobalVar)
2193  ->setInitializer(unwrap<Constant>(ConstantVal));
2194 }
2195 
2197  return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
2198 }
2199 
2201  unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
2202 }
2203 
2205  return unwrap<GlobalVariable>(GlobalVar)->isConstant();
2206 }
2207 
2209  unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
2210 }
2211 
2213  switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
2215  return LLVMNotThreadLocal;
2219  return LLVMLocalDynamicTLSModel;
2221  return LLVMInitialExecTLSModel;
2223  return LLVMLocalExecTLSModel;
2224  }
2225 
2226  llvm_unreachable("Invalid GlobalVariable thread local mode");
2227 }
2228 
2230  GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2231 
2232  switch (Mode) {
2233  case LLVMNotThreadLocal:
2235  break;
2238  break;
2241  break;
2244  break;
2245  case LLVMLocalExecTLSModel:
2247  break;
2248  }
2249 }
2250 
2252  return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
2253 }
2254 
2256  unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
2257 }
2258 
2259 /*--.. Operations on aliases ......................................--*/
2260 
2262  const char *Name) {
2263  auto *PTy = cast<PointerType>(unwrap(Ty));
2264  return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
2266  unwrap<Constant>(Aliasee), unwrap(M)));
2267 }
2268 
2270  const char *Name, size_t NameLen) {
2271  return wrap(unwrap(M)->getNamedAlias(Name));
2272 }
2273 
2275  Module *Mod = unwrap(M);
2277  if (I == Mod->alias_end())
2278  return nullptr;
2279  return wrap(&*I);
2280 }
2281 
2283  Module *Mod = unwrap(M);
2285  if (I == Mod->alias_begin())
2286  return nullptr;
2287  return wrap(&*--I);
2288 }
2289 
2291  GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2292  Module::alias_iterator I(Alias);
2293  if (++I == Alias->getParent()->alias_end())
2294  return nullptr;
2295  return wrap(&*I);
2296 }
2297 
2299  GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2300  Module::alias_iterator I(Alias);
2301  if (I == Alias->getParent()->alias_begin())
2302  return nullptr;
2303  return wrap(&*--I);
2304 }
2305 
2307  return wrap(unwrap<GlobalAlias>(Alias)->getAliasee());
2308 }
2309 
2311  unwrap<GlobalAlias>(Alias)->setAliasee(unwrap<Constant>(Aliasee));
2312 }
2313 
2314 /*--.. Operations on functions .............................................--*/
2315 
2317  LLVMTypeRef FunctionTy) {
2318  return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
2320 }
2321 
2323  return wrap(unwrap(M)->getFunction(Name));
2324 }
2325 
2327  Module *Mod = unwrap(M);
2328  Module::iterator I = Mod->begin();
2329  if (I == Mod->end())
2330  return nullptr;
2331  return wrap(&*I);
2332 }
2333 
2335  Module *Mod = unwrap(M);
2336  Module::iterator I = Mod->end();
2337  if (I == Mod->begin())
2338  return nullptr;
2339  return wrap(&*--I);
2340 }
2341 
2343  Function *Func = unwrap<Function>(Fn);
2344  Module::iterator I(Func);
2345  if (++I == Func->getParent()->end())
2346  return nullptr;
2347  return wrap(&*I);
2348 }
2349 
2351  Function *Func = unwrap<Function>(Fn);
2352  Module::iterator I(Func);
2353  if (I == Func->getParent()->begin())
2354  return nullptr;
2355  return wrap(&*--I);
2356 }
2357 
2359  unwrap<Function>(Fn)->eraseFromParent();
2360 }
2361 
2363  return unwrap<Function>(Fn)->hasPersonalityFn();
2364 }
2365 
2367  return wrap(unwrap<Function>(Fn)->getPersonalityFn());
2368 }
2369 
2371  unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
2372 }
2373 
2375  if (Function *F = dyn_cast<Function>(unwrap(Fn)))
2376  return F->getIntrinsicID();
2377  return 0;
2378 }
2379 
2381  assert(ID < llvm::Intrinsic::num_intrinsics && "Intrinsic ID out of range");
2382  return llvm::Intrinsic::ID(ID);
2383 }
2384 
2386  unsigned ID,
2387  LLVMTypeRef *ParamTypes,
2388  size_t ParamCount) {
2389  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2390  auto IID = llvm_map_to_intrinsic_id(ID);
2391  return wrap(llvm::Intrinsic::getDeclaration(unwrap(Mod), IID, Tys));
2392 }
2393 
2394 const char *LLVMIntrinsicGetName(unsigned ID, size_t *NameLength) {
2395  auto IID = llvm_map_to_intrinsic_id(ID);
2396  auto Str = llvm::Intrinsic::getName(IID);
2397  *NameLength = Str.size();
2398  return Str.data();
2399 }
2400 
2402  LLVMTypeRef *ParamTypes, size_t ParamCount) {
2403  auto IID = llvm_map_to_intrinsic_id(ID);
2404  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2405  return wrap(llvm::Intrinsic::getType(*unwrap(Ctx), IID, Tys));
2406 }
2407 
2409  LLVMTypeRef *ParamTypes,
2410  size_t ParamCount,
2411  size_t *NameLength) {
2412  auto IID = llvm_map_to_intrinsic_id(ID);
2413  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2414  auto Str = llvm::Intrinsic::getNameNoUnnamedTypes(IID, Tys);
2415  *NameLength = Str.length();
2416  return strdup(Str.c_str());
2417 }
2418 
2420  LLVMTypeRef *ParamTypes,
2421  size_t ParamCount,
2422  size_t *NameLength) {
2423  auto IID = llvm_map_to_intrinsic_id(ID);
2424  ArrayRef<Type *> Tys(unwrap(ParamTypes), ParamCount);
2425  auto Str = llvm::Intrinsic::getName(IID, Tys, unwrap(Mod));
2426  *NameLength = Str.length();
2427  return strdup(Str.c_str());
2428 }
2429 
2430 unsigned LLVMLookupIntrinsicID(const char *Name, size_t NameLen) {
2431  return Function::lookupIntrinsicID({Name, NameLen});
2432 }
2433 
2435  auto IID = llvm_map_to_intrinsic_id(ID);
2436  return llvm::Intrinsic::isOverloaded(IID);
2437 }
2438 
2440  return unwrap<Function>(Fn)->getCallingConv();
2441 }
2442 
2443 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
2444  return unwrap<Function>(Fn)->setCallingConv(
2445  static_cast<CallingConv::ID>(CC));
2446 }
2447 
2448 const char *LLVMGetGC(LLVMValueRef Fn) {
2449  Function *F = unwrap<Function>(Fn);
2450  return F->hasGC()? F->getGC().c_str() : nullptr;
2451 }
2452 
2453 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
2454  Function *F = unwrap<Function>(Fn);
2455  if (GC)
2456  F->setGC(GC);
2457  else
2458  F->clearGC();
2459 }
2460 
2462  LLVMAttributeRef A) {
2463  unwrap<Function>(F)->addAttributeAtIndex(Idx, unwrap(A));
2464 }
2465 
2467  auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2468  return AS.getNumAttributes();
2469 }
2470 
2473  auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2474  for (auto A : AS)
2475  *Attrs++ = wrap(A);
2476 }
2477 
2479  LLVMAttributeIndex Idx,
2480  unsigned KindID) {
2481  return wrap(unwrap<Function>(F)->getAttributeAtIndex(
2482  Idx, (Attribute::AttrKind)KindID));
2483 }
2484 
2486  LLVMAttributeIndex Idx,
2487  const char *K, unsigned KLen) {
2488  return wrap(
2489  unwrap<Function>(F)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
2490 }
2491 
2493  unsigned KindID) {
2494  unwrap<Function>(F)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
2495 }
2496 
2498  const char *K, unsigned KLen) {
2499  unwrap<Function>(F)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
2500 }
2501 
2503  const char *V) {
2504  Function *Func = unwrap<Function>(Fn);
2505  Attribute Attr = Attribute::get(Func->getContext(), A, V);
2506  Func->addFnAttr(Attr);
2507 }
2508 
2509 /*--.. Operations on parameters ............................................--*/
2510 
2512  // This function is strictly redundant to
2513  // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
2514  return unwrap<Function>(FnRef)->arg_size();
2515 }
2516 
2517 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
2518  Function *Fn = unwrap<Function>(FnRef);
2519  for (Argument &A : Fn->args())
2520  *ParamRefs++ = wrap(&A);
2521 }
2522 
2524  Function *Fn = unwrap<Function>(FnRef);
2525  return wrap(&Fn->arg_begin()[index]);
2526 }
2527 
2529  return wrap(unwrap<Argument>(V)->getParent());
2530 }
2531 
2533  Function *Func = unwrap<Function>(Fn);
2534  Function::arg_iterator I = Func->arg_begin();
2535  if (I == Func->arg_end())
2536  return nullptr;
2537  return wrap(&*I);
2538 }
2539 
2541  Function *Func = unwrap<Function>(Fn);
2542  Function::arg_iterator I = Func->arg_end();
2543  if (I == Func->arg_begin())
2544  return nullptr;
2545  return wrap(&*--I);
2546 }
2547 
2549  Argument *A = unwrap<Argument>(Arg);
2550  Function *Fn = A->getParent();
2551  if (A->getArgNo() + 1 >= Fn->arg_size())
2552  return nullptr;
2553  return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
2554 }
2555 
2557  Argument *A = unwrap<Argument>(Arg);
2558  if (A->getArgNo() == 0)
2559  return nullptr;
2560  return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
2561 }
2562 
2564  Argument *A = unwrap<Argument>(Arg);
2565  A->addAttr(Attribute::getWithAlignment(A->getContext(), Align(align)));
2566 }
2567 
2568 /*--.. Operations on ifuncs ................................................--*/
2569 
2571  const char *Name, size_t NameLen,
2572  LLVMTypeRef Ty, unsigned AddrSpace,
2574  return wrap(GlobalIFunc::create(unwrap(Ty), AddrSpace,
2576  StringRef(Name, NameLen),
2577  unwrap<Constant>(Resolver), unwrap(M)));
2578 }
2579 
2581  const char *Name, size_t NameLen) {
2582  return wrap(unwrap(M)->getNamedIFunc(StringRef(Name, NameLen)));
2583 }
2584 
2586  Module *Mod = unwrap(M);
2588  if (I == Mod->ifunc_end())
2589  return nullptr;
2590  return wrap(&*I);
2591 }
2592 
2594  Module *Mod = unwrap(M);
2596  if (I == Mod->ifunc_begin())
2597  return nullptr;
2598  return wrap(&*--I);
2599 }
2600 
2602  GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2604  if (++I == GIF->getParent()->ifunc_end())
2605  return nullptr;
2606  return wrap(&*I);
2607 }
2608 
2610  GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2612  if (I == GIF->getParent()->ifunc_begin())
2613  return nullptr;
2614  return wrap(&*--I);
2615 }
2616 
2618  return wrap(unwrap<GlobalIFunc>(IFunc)->getResolver());
2619 }
2620 
2622  unwrap<GlobalIFunc>(IFunc)->setResolver(unwrap<Constant>(Resolver));
2623 }
2624 
2626  unwrap<GlobalIFunc>(IFunc)->eraseFromParent();
2627 }
2628 
2630  unwrap<GlobalIFunc>(IFunc)->removeFromParent();
2631 }
2632 
2633 /*--.. Operations on basic blocks ..........................................--*/
2634 
2636  return wrap(static_cast<Value*>(unwrap(BB)));
2637 }
2638 
2640  return isa<BasicBlock>(unwrap(Val));
2641 }
2642 
2644  return wrap(unwrap<BasicBlock>(Val));
2645 }
2646 
2648  return unwrap(BB)->getName().data();
2649 }
2650 
2652  return wrap(unwrap(BB)->getParent());
2653 }
2654 
2656  return wrap(unwrap(BB)->getTerminator());
2657 }
2658 
2660  return unwrap<Function>(FnRef)->size();
2661 }
2662 
2663 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
2664  Function *Fn = unwrap<Function>(FnRef);
2665  for (BasicBlock &BB : *Fn)
2666  *BasicBlocksRefs++ = wrap(&BB);
2667 }
2668 
2670  return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2671 }
2672 
2674  Function *Func = unwrap<Function>(Fn);
2675  Function::iterator I = Func->begin();
2676  if (I == Func->end())
2677  return nullptr;
2678  return wrap(&*I);
2679 }
2680 
2682  Function *Func = unwrap<Function>(Fn);
2683  Function::iterator I = Func->end();
2684  if (I == Func->begin())
2685  return nullptr;
2686  return wrap(&*--I);
2687 }
2688 
2690  BasicBlock *Block = unwrap(BB);
2691  Function::iterator I(Block);
2692  if (++I == Block->getParent()->end())
2693  return nullptr;
2694  return wrap(&*I);
2695 }
2696 
2698  BasicBlock *Block = unwrap(BB);
2699  Function::iterator I(Block);
2700  if (I == Block->getParent()->begin())
2701  return nullptr;
2702  return wrap(&*--I);
2703 }
2704 
2706  const char *Name) {
2708 }
2709 
2712  BasicBlock *ToInsert = unwrap(BB);
2713  BasicBlock *CurBB = unwrap(Builder)->GetInsertBlock();
2714  assert(CurBB && "current insertion point is invalid!");
2715  CurBB->getParent()->getBasicBlockList().insertAfter(CurBB->getIterator(),
2716  ToInsert);
2717 }
2718 
2721  unwrap<Function>(Fn)->getBasicBlockList().push_back(unwrap(BB));
2722 }
2723 
2725  LLVMValueRef FnRef,
2726  const char *Name) {
2727  return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
2728 }
2729 
2732 }
2733 
2735  LLVMBasicBlockRef BBRef,
2736  const char *Name) {
2737  BasicBlock *BB = unwrap(BBRef);
2738  return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2739 }
2740 
2742  const char *Name) {
2744 }
2745 
2747  unwrap(BBRef)->eraseFromParent();
2748 }
2749 
2751  unwrap(BBRef)->removeFromParent();
2752 }
2753 
2755  unwrap(BB)->moveBefore(unwrap(MovePos));
2756 }
2757 
2759  unwrap(BB)->moveAfter(unwrap(MovePos));
2760 }
2761 
2762 /*--.. Operations on instructions ..........................................--*/
2763 
2765  return wrap(unwrap<Instruction>(Inst)->getParent());
2766 }
2767 
2769  BasicBlock *Block = unwrap(BB);
2770  BasicBlock::iterator I = Block->begin();
2771  if (I == Block->end())
2772  return nullptr;
2773  return wrap(&*I);
2774 }
2775 
2777  BasicBlock *Block = unwrap(BB);
2778  BasicBlock::iterator I = Block->end();
2779  if (I == Block->begin())
2780  return nullptr;
2781  return wrap(&*--I);
2782 }
2783 
2785  Instruction *Instr = unwrap<Instruction>(Inst);
2786  BasicBlock::iterator I(Instr);
2787  if (++I == Instr->getParent()->end())
2788  return nullptr;
2789  return wrap(&*I);
2790 }
2791 
2793  Instruction *Instr = unwrap<Instruction>(Inst);
2794  BasicBlock::iterator I(Instr);
2795  if (I == Instr->getParent()->begin())
2796  return nullptr;
2797  return wrap(&*--I);
2798 }
2799 
2801  unwrap<Instruction>(Inst)->removeFromParent();
2802 }
2803 
2805  unwrap<Instruction>(Inst)->eraseFromParent();
2806 }
2807 
2809  if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2810  return (LLVMIntPredicate)I->getPredicate();
2811  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2812  if (CE->getOpcode() == Instruction::ICmp)
2813  return (LLVMIntPredicate)CE->getPredicate();
2814  return (LLVMIntPredicate)0;
2815 }
2816 
2818  if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2819  return (LLVMRealPredicate)I->getPredicate();
2820  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2821  if (CE->getOpcode() == Instruction::FCmp)
2822  return (LLVMRealPredicate)CE->getPredicate();
2823  return (LLVMRealPredicate)0;
2824 }
2825 
2827  if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2828  return map_to_llvmopcode(C->getOpcode());
2829  return (LLVMOpcode)0;
2830 }
2831 
2833  if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2834  return wrap(C->clone());
2835  return nullptr;
2836 }
2837 
2839  Instruction *I = dyn_cast<Instruction>(unwrap(Inst));
2840  return (I && I->isTerminator()) ? wrap(I) : nullptr;
2841 }
2842 
2844  if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) {
2845  return FPI->getNumArgOperands();
2846  }
2847  return unwrap<CallBase>(Instr)->arg_size();
2848 }
2849 
2850 /*--.. Call and invoke instructions ........................................--*/
2851 
2853  return unwrap<CallBase>(Instr)->getCallingConv();
2854 }
2855 
2856 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
2857  return unwrap<CallBase>(Instr)->setCallingConv(
2858  static_cast<CallingConv::ID>(CC));
2859 }
2860 
2862  unsigned align) {
2863  auto *Call = unwrap<CallBase>(Instr);
2864  Attribute AlignAttr =
2865  Attribute::getWithAlignment(Call->getContext(), Align(align));
2866  Call->addAttributeAtIndex(Idx, AlignAttr);
2867 }
2868 
2870  LLVMAttributeRef A) {
2871  unwrap<CallBase>(C)->addAttributeAtIndex(Idx, unwrap(A));
2872 }
2873 
2875  LLVMAttributeIndex Idx) {
2876  auto *Call = unwrap<CallBase>(C);
2877  auto AS = Call->getAttributes().getAttributes(Idx);
2878  return AS.getNumAttributes();
2879 }
2880 
2883  auto *Call = unwrap<CallBase>(C);
2884  auto AS = Call->getAttributes().getAttributes(Idx);
2885  for (auto A : AS)
2886  *Attrs++ = wrap(A);
2887 }
2888 
2890  LLVMAttributeIndex Idx,
2891  unsigned KindID) {
2892  return wrap(unwrap<CallBase>(C)->getAttributeAtIndex(
2893  Idx, (Attribute::AttrKind)KindID));
2894 }
2895 
2897  LLVMAttributeIndex Idx,
2898  const char *K, unsigned KLen) {
2899  return wrap(
2900  unwrap<CallBase>(C)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
2901 }
2902 
2904  unsigned KindID) {
2905  unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
2906 }
2907 
2909  const char *K, unsigned KLen) {
2910  unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
2911 }
2912 
2914  return wrap(unwrap<CallBase>(Instr)->getCalledOperand());
2915 }
2916 
2918  return wrap(unwrap<CallBase>(Instr)->getFunctionType());
2919 }
2920 
2921 /*--.. Operations on call instructions (only) ..............................--*/
2922 
2924  return unwrap<CallInst>(Call)->isTailCall();
2925 }
2926 
2927 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
2928  unwrap<CallInst>(Call)->setTailCall(isTailCall);
2929 }
2930 
2931 /*--.. Operations on invoke instructions (only) ............................--*/
2932 
2934  return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2935 }
2936 
2938  if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2939  return wrap(CRI->getUnwindDest());
2940  } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2941  return wrap(CSI->getUnwindDest());
2942  }
2943  return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2944 }
2945 
2947  unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2948 }
2949 
2951  if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2952  return CRI->setUnwindDest(unwrap(B));
2953  } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2954  return CSI->setUnwindDest(unwrap(B));
2955  }
2956  unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2957 }
2958 
2959 /*--.. Operations on terminators ...........................................--*/
2960 
2962  return unwrap<Instruction>(Term)->getNumSuccessors();
2963 }
2964 
2966  return wrap(unwrap<Instruction>(Term)->getSuccessor(i));
2967 }
2968 
2970  return unwrap<Instruction>(Term)->setSuccessor(i, unwrap(block));
2971 }
2972 
2973 /*--.. Operations on branch instructions (only) ............................--*/
2974 
2976  return unwrap<BranchInst>(Branch)->isConditional();
2977 }
2978 
2980  return wrap(unwrap<BranchInst>(Branch)->getCondition());
2981 }
2982 
2984  return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2985 }
2986 
2987 /*--.. Operations on switch instructions (only) ............................--*/
2988 
2990  return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2991 }
2992 
2993 /*--.. Operations on alloca instructions (only) ............................--*/
2994 
2996  return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
2997 }
2998 
2999 /*--.. Operations on gep instructions (only) ...............................--*/
3000 
3002  return unwrap<GetElementPtrInst>(GEP)->isInBounds();
3003 }
3004 
3006  return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
3007 }
3008 
3009 /*--.. Operations on phi nodes .............................................--*/
3010 
3011 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
3012  LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
3013  PHINode *PhiVal = unwrap<PHINode>(PhiNode);
3014  for (unsigned I = 0; I != Count; ++I)
3015  PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
3016 }
3017 
3018 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
3019  return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
3020 }
3021 
3023  return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
3024 }
3025 
3027  return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
3028 }
3029 
3030 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/
3031 
3033  auto *I = unwrap(Inst);
3034  if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
3035  return GEP->getNumIndices();
3036  if (auto *EV = dyn_cast<ExtractValueInst>(I))
3037  return EV->getNumIndices();
3038  if (auto *IV = dyn_cast<InsertValueInst>(I))
3039  return IV->getNumIndices();
3040  if (auto *CE = dyn_cast<ConstantExpr>(I))
3041  return CE->getIndices().size();
3043  "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
3044 }
3045 
3046 const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
3047  auto *I = unwrap(Inst);
3048  if (auto *EV = dyn_cast<ExtractValueInst>(I))
3049  return EV->getIndices().data();
3050  if (auto *IV = dyn_cast<InsertValueInst>(I))
3051  return IV->getIndices().data();
3052  if (auto *CE = dyn_cast<ConstantExpr>(I))
3053  return CE->getIndices().data();
3055  "LLVMGetIndices applies only to extractvalue and insertvalue!");
3056 }
3057 
3058 
3059 /*===-- Instruction builders ----------------------------------------------===*/
3060 
3062  return wrap(new IRBuilder<>(*unwrap(C)));
3063 }
3064 
3067 }
3068 
3070  LLVMValueRef Instr) {
3071  BasicBlock *BB = unwrap(Block);
3072  auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
3073  unwrap(Builder)->SetInsertPoint(BB, I);
3074 }
3075 
3077  Instruction *I = unwrap<Instruction>(Instr);
3078  unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
3079 }
3080 
3082  BasicBlock *BB = unwrap(Block);
3083  unwrap(Builder)->SetInsertPoint(BB);
3084 }
3085 
3087  return wrap(unwrap(Builder)->GetInsertBlock());
3088 }
3089 
3091  unwrap(Builder)->ClearInsertionPoint();
3092 }
3093 
3095  unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
3096 }
3097 
3099  const char *Name) {
3100  unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
3101 }
3102 
3104  delete unwrap(Builder);
3105 }
3106 
3107 /*--.. Metadata builders ...................................................--*/
3108 
3110  return wrap(unwrap(Builder)->getCurrentDebugLocation().getAsMDNode());
3111 }
3112 
3114  if (Loc)
3115  unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(unwrap<MDNode>(Loc)));
3116  else
3117  unwrap(Builder)->SetCurrentDebugLocation(DebugLoc());
3118 }
3119 
3121  MDNode *Loc =
3122  L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
3123  unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
3124 }
3125 
3127  LLVMContext &Context = unwrap(Builder)->getContext();
3128  return wrap(MetadataAsValue::get(
3129  Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
3130 }
3131 
3133  unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
3134 }
3135 
3137  unwrap(Builder)->AddMetadataToInst(unwrap<Instruction>(Inst));
3138 }
3139 
3141  LLVMMetadataRef FPMathTag) {
3142 
3143  unwrap(Builder)->setDefaultFPMathTag(FPMathTag
3144  ? unwrap<MDNode>(FPMathTag)
3145  : nullptr);
3146 }
3147 
3149  return wrap(unwrap(Builder)->getDefaultFPMathTag());
3150 }
3151 
3152 /*--.. Instruction builders ................................................--*/
3153 
3155  return wrap(unwrap(B)->CreateRetVoid());
3156 }
3157 
3159  return wrap(unwrap(B)->CreateRet(unwrap(V)));
3160 }
3161 
3163  unsigned N) {
3164  return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
3165 }
3166 
3168  return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
3169 }
3170 
3173  return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
3174 }
3175 
3177  LLVMBasicBlockRef Else, unsigned NumCases) {
3178  return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
3179 }
3180 
3182  unsigned NumDests) {
3183  return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
3184 }
3185 
3187  LLVMValueRef *Args, unsigned NumArgs,
3189  const char *Name) {
3190  Value *V = unwrap(Fn);
3191  FunctionType *FnT =
3192  cast<FunctionType>(cast<PointerType>(V->getType())->getElementType());
3193 
3194  return wrap(
3195  unwrap(B)->CreateInvoke(FnT, unwrap(Fn), unwrap(Then), unwrap(Catch),
3196  makeArrayRef(unwrap(Args), NumArgs), Name));
3197 }
3198 
3200  LLVMValueRef *Args, unsigned NumArgs,
3202  const char *Name) {
3203  return wrap(unwrap(B)->CreateInvoke(
3204  unwrap<FunctionType>(Ty), unwrap(Fn), unwrap(Then), unwrap(Catch),
3205  makeArrayRef(unwrap(Args), NumArgs), Name));
3206 }
3207 
3209  LLVMValueRef PersFn, unsigned NumClauses,
3210  const char *Name) {
3211  // The personality used to live on the landingpad instruction, but now it
3212  // lives on the parent function. For compatibility, take the provided
3213  // personality and put it on the parent function.
3214  if (PersFn)
3215  unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
3216  cast<Function>(unwrap(PersFn)));
3217  return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
3218 }
3219 
3221  LLVMValueRef *Args, unsigned NumArgs,
3222  const char *Name) {
3223  return wrap(unwrap(B)->CreateCatchPad(unwrap(ParentPad),
3224  makeArrayRef(unwrap(Args), NumArgs),
3225  Name));
3226 }
3227 
3229  LLVMValueRef *Args, unsigned NumArgs,
3230  const char *Name) {
3231  if (ParentPad == nullptr) {
3232  Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3233  ParentPad = wrap(Constant::getNullValue(Ty));
3234  }
3235  return wrap(unwrap(B)->CreateCleanupPad(unwrap(ParentPad),
3236  makeArrayRef(unwrap(Args), NumArgs),
3237  Name));
3238 }
3239 
3241  return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
3242 }
3243 
3245  LLVMBasicBlockRef UnwindBB,
3246  unsigned NumHandlers, const char *Name) {
3247  if (ParentPad == nullptr) {
3248  Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3249  ParentPad = wrap(Constant::getNullValue(Ty));
3250  }
3251  return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(UnwindBB),
3252  NumHandlers, Name));
3253 }
3254 
3257  return wrap(unwrap(B)->CreateCatchRet(unwrap<CatchPadInst>(CatchPad),
3258  unwrap(BB)));
3259 }
3260 
3263  return wrap(unwrap(B)->CreateCleanupRet(unwrap<CleanupPadInst>(CatchPad),
3264  unwrap(BB)));
3265 }
3266 
3268  return wrap(unwrap(B)->CreateUnreachable());
3269 }
3270 
3272  LLVMBasicBlockRef Dest) {
3273  unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
3274 }
3275 
3277  unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
3278 }
3279 
3280 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
3281  return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
3282 }
3283 
3284 LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
3285  return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
3286 }
3287 
3289  unwrap<LandingPadInst>(LandingPad)->
3290  addClause(cast<Constant>(unwrap(ClauseVal)));
3291 }
3292 
3294  return unwrap<LandingPadInst>(LandingPad)->isCleanup();
3295 }
3296 
3297 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
3298  unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
3299 }
3300 
3302  unwrap<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Dest));
3303 }
3304 
3305 unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) {
3306  return unwrap<CatchSwitchInst>(CatchSwitch)->getNumHandlers();
3307 }
3308 
3309 void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) {
3310  CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(CatchSwitch);
3311  for (const BasicBlock *H : CSI->handlers())
3312  *Handlers++ = wrap(H);
3313 }
3314 
3316  return wrap(unwrap<CatchPadInst>(CatchPad)->getCatchSwitch());
3317 }
3318 
3320  unwrap<CatchPadInst>(CatchPad)
3321  ->setCatchSwitch(unwrap<CatchSwitchInst>(CatchSwitch));
3322 }
3323 
3324 /*--.. Funclets ...........................................................--*/
3325 
3327  return wrap(unwrap<FuncletPadInst>(Funclet)->getArgOperand(i));
3328 }
3329 
3330 void LLVMSetArgOperand(LLVMValueRef Funclet, unsigned i, LLVMValueRef value) {
3331  unwrap<FuncletPadInst>(Funclet)->setArgOperand(i, unwrap(value));
3332 }
3333 
3334 /*--.. Arithmetic ..........................................................--*/
3335 
3337  const char *Name) {
3338  return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
3339 }
3340 
3342  const char *Name) {
3343  return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
3344 }
3345 
3347  const char *Name) {
3348  return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
3349 }
3350 
3352  const char *Name) {
3353  return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
3354 }
3355 
3357  const char *Name) {
3358  return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
3359 }
3360 
3362  const char *Name) {
3363  return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
3364 }
3365 
3367  const char *Name) {
3368  return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
3369 }
3370 
3372  const char *Name) {
3373  return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
3374 }
3375 
3377  const char *Name) {
3378  return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
3379 }
3380 
3382  const char *Name) {
3383  return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
3384 }
3385 
3387  const char *Name) {
3388  return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
3389 }
3390 
3392  const char *Name) {
3393  return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
3394 }
3395 
3397  const char *Name) {
3398  return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
3399 }
3400 
3402  LLVMValueRef RHS, const char *Name) {
3403  return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
3404 }
3405 
3407  const char *Name) {
3408  return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
3409 }
3410 
3412  LLVMValueRef RHS, const char *Name) {
3413  return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
3414 }
3415 
3417  const char *Name) {
3418  return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
3419 }
3420 
3422  const char *Name) {
3423  return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
3424 }
3425 
3427  const char *Name) {
3428  return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
3429 }
3430 
3432  const char *Name) {
3433  return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
3434 }
3435 
3437  const char *Name) {
3438  return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
3439 }
3440 
3442  const char *Name) {
3443  return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
3444 }
3445 
3447  const char *Name) {
3448  return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
3449 }
3450 
3452  const char *Name) {
3453  return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
3454 }
3455 
3457  const char *Name) {
3458  return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
3459 }
3460 
3462  const char *Name) {
3463  return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
3464 }
3465 
3467  LLVMValueRef LHS, LLVMValueRef RHS,
3468  const char *Name) {
3469  return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
3470  unwrap(RHS), Name));
3471 }
3472 
3474  return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
3475 }
3476 
3478  const char *Name) {
3479  return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
3480 }
3481 
3483  const char *Name) {
3484  return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
3485 }
3486 
3488  return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
3489 }
3490 
3492  return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
3493 }
3494 
3495 /*--.. Memory ..............................................................--*/
3496 
3498  const char *Name) {
3499  Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3500  Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3501  AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3502  Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3503  ITy, unwrap(Ty), AllocSize,
3504  nullptr, nullptr, "");
3505  return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3506 }
3507 
3509  LLVMValueRef Val, const char *Name) {
3510  Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3511  Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3512  AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3513  Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3514  ITy, unwrap(Ty), AllocSize,
3515  unwrap(Val), nullptr, "");
3516  return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3517 }
3518 
3520  LLVMValueRef Val, LLVMValueRef Len,
3521  unsigned Align) {
3522  return wrap(unwrap(B)->CreateMemSet(unwrap(Ptr), unwrap(Val), unwrap(Len),
3523  MaybeAlign(Align)));
3524 }
3525 
3527  LLVMValueRef Dst, unsigned DstAlign,
3528  LLVMValueRef Src, unsigned SrcAlign,
3529  LLVMValueRef Size) {
3530  return wrap(unwrap(B)->CreateMemCpy(unwrap(Dst), MaybeAlign(DstAlign),
3531  unwrap(Src), MaybeAlign(SrcAlign),
3532  unwrap(Size)));
3533 }
3534 
3536  LLVMValueRef Dst, unsigned DstAlign,
3537  LLVMValueRef Src, unsigned SrcAlign,
3538  LLVMValueRef Size) {
3539  return wrap(unwrap(B)->CreateMemMove(unwrap(Dst), MaybeAlign(DstAlign),
3540  unwrap(Src), MaybeAlign(SrcAlign),
3541  unwrap(Size)));
3542 }
3543 
3545  const char *Name) {
3546  return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
3547 }
3548 
3550  LLVMValueRef Val, const char *Name) {
3551  return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
3552 }
3553 
3555  return wrap(unwrap(B)->Insert(
3556  CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
3557 }
3558 
3560  const char *Name) {
3561  Value *V = unwrap(PointerVal);
3562  PointerType *Ty = cast<PointerType>(V->getType());
3563 
3564  return wrap(unwrap(B)->CreateLoad(Ty->getElementType(), V, Name));
3565 }
3566 
3568  LLVMValueRef PointerVal, const char *Name) {
3569  return wrap(unwrap(B)->CreateLoad(unwrap(Ty), unwrap(PointerVal), Name));
3570 }
3571 
3573  LLVMValueRef PointerVal) {
3574  return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
3575 }
3576 
3578  switch (Ordering) {
3588  }
3589 
3590  llvm_unreachable("Invalid LLVMAtomicOrdering value!");
3591 }
3592 
3594  switch (Ordering) {
3604  }
3605 
3606  llvm_unreachable("Invalid AtomicOrdering value!");
3607 }
3608 
3610  switch (BinOp) {
3624  }
3625 
3626  llvm_unreachable("Invalid LLVMAtomicRMWBinOp value!");
3627 }
3628 
3630  switch (BinOp) {
3644  default: break;
3645  }
3646 
3647  llvm_unreachable("Invalid AtomicRMWBinOp value!");
3648 }
3649 
3650 // TODO: Should this and other atomic instructions support building with
3651 // "syncscope"?
3653  LLVMBool isSingleThread, const char *Name) {
3654  return wrap(
3655  unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
3656  isSingleThread ? SyncScope::SingleThread
3658  Name));
3659 }
3660 
3662  LLVMValueRef *Indices, unsigned NumIndices,
3663  const char *Name) {
3664  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3665  Value *Val = unwrap(Pointer);
3666  Type *Ty =
3667  cast<PointerType>(Val->getType()->getScalarType())->getElementType();
3668  return wrap(unwrap(B)->CreateGEP(Ty, Val, IdxList, Name));
3669 }
3670 
3672  LLVMValueRef Pointer, LLVMValueRef *Indices,
3673  unsigned NumIndices, const char *Name) {
3674  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3675  return wrap(unwrap(B)->CreateGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3676 }
3677 
3679  LLVMValueRef *Indices, unsigned NumIndices,
3680  const char *Name) {
3681  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3682  Value *Val = unwrap(Pointer);
3683  Type *Ty =
3684  cast<PointerType>(Val->getType()->getScalarType())->getElementType();
3685  return wrap(unwrap(B)->CreateInBoundsGEP(Ty, Val, IdxList, Name));
3686 }
3687 
3689  LLVMValueRef Pointer, LLVMValueRef *Indices,
3690  unsigned NumIndices, const char *Name) {
3691  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3692  return wrap(
3693  unwrap(B)->CreateInBoundsGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3694 }
3695 
3697  unsigned Idx, const char *Name) {
3698  Value *Val = unwrap(Pointer);
3699  Type *Ty =
3700  cast<PointerType>(Val->getType()->getScalarType())->getElementType();
3701  return wrap(unwrap(B)->CreateStructGEP(Ty, Val, Idx, Name));
3702 }
3703 
3705  LLVMValueRef Pointer, unsigned Idx,
3706  const char *Name) {
3707  return wrap(
3708  unwrap(B)->CreateStructGEP(unwrap(Ty), unwrap(Pointer), Idx, Name));
3709 }
3710 
3712  const char *Name) {
3713  return wrap(unwrap(B)->CreateGlobalString(Str, Name));
3714 }
3715 
3717  const char *Name) {
3718  return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
3719 }
3720 
3722  Value *P = unwrap<Value>(MemAccessInst);
3723  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3724  return LI->isVolatile();
3725  if (StoreInst *SI = dyn_cast<StoreInst>(P))
3726  return SI->isVolatile();
3727  if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
3728  return AI->isVolatile();
3729  return cast<AtomicCmpXchgInst>(P)->isVolatile();
3730 }
3731 
3732 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
3733  Value *P = unwrap<Value>(MemAccessInst);
3734  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3735  return LI->setVolatile(isVolatile);
3736  if (StoreInst *SI = dyn_cast<StoreInst>(P))
3737  return SI->setVolatile(isVolatile);
3738  if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
3739  return AI->setVolatile(isVolatile);
3740  return cast<AtomicCmpXchgInst>(P)->setVolatile(isVolatile);
3741 }
3742 
3744  return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->isWeak();
3745 }
3746 
3747 void LLVMSetWeak(LLVMValueRef CmpXchgInst, LLVMBool isWeak) {
3748  return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->setWeak(isWeak);
3749 }
3750 
3752  Value *P = unwrap<Value>(MemAccessInst);
3753  AtomicOrdering O;
3754  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3755  O = LI->getOrdering();
3756  else if (StoreInst *SI = dyn_cast<StoreInst>(P))
3757  O = SI->getOrdering();
3758  else
3759  O = cast<AtomicRMWInst>(P)->getOrdering();
3760  return mapToLLVMOrdering(O);
3761 }
3762 
3763 void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
3764  Value *P = unwrap<Value>(MemAccessInst);
3765  AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3766 
3767  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3768  return LI->setOrdering(O);
3769  return cast<StoreInst>(P)->setOrdering(O);
3770 }
3771 
3773  return mapToLLVMRMWBinOp(unwrap<AtomicRMWInst>(Inst)->getOperation());
3774 }
3775 
3777  unwrap<AtomicRMWInst>(Inst)->setOperation(mapFromLLVMRMWBinOp(BinOp));
3778 }
3779 
3780 /*--.. Casts ...............................................................--*/
3781 
3783  LLVMTypeRef DestTy, const char *Name) {
3784  return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
3785 }
3786 
3788  LLVMTypeRef DestTy, const char *Name) {
3789  return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
3790 }
3791 
3793  LLVMTypeRef DestTy, const char *Name) {
3794  return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
3795 }
3796 
3798  LLVMTypeRef DestTy, const char *Name) {
3799  return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
3800 }
3801 
3803  LLVMTypeRef DestTy, const char *Name) {
3804  return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
3805 }
3806 
3808  LLVMTypeRef DestTy, const char *Name) {
3809  return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
3810 }
3811 
3813  LLVMTypeRef DestTy, const char *Name) {
3814  return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
3815 }
3816 
3818  LLVMTypeRef DestTy, const char *Name) {
3819  return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
3820 }
3821 
3823  LLVMTypeRef DestTy, const char *Name) {
3824  return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
3825 }
3826 
3828  LLVMTypeRef DestTy, const char *Name) {
3829  return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
3830 }
3831 
3833  LLVMTypeRef DestTy, const char *Name) {
3834  return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
3835 }
3836 
3838  LLVMTypeRef DestTy, const char *Name) {
3839  return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
3840 }
3841 
3843  LLVMTypeRef DestTy, const char *Name) {
3844  return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
3845 }
3846 
3848  LLVMTypeRef DestTy, const char *Name) {
3849  return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
3850  Name));
3851 }
3852 
3854  LLVMTypeRef DestTy, const char *Name) {
3855  return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
3856  Name));
3857 }
3858 
3860  LLVMTypeRef DestTy, const char *Name) {
3861  return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
3862  Name));
3863 }
3864 
3866  LLVMTypeRef DestTy, const char *Name) {
3867  return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
3868  unwrap(DestTy), Name));
3869 }
3870 
3872  LLVMTypeRef DestTy, const char *Name) {
3873  return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
3874 }
3875 
3877  LLVMTypeRef DestTy, LLVMBool IsSigned,
3878  const char *Name) {
3879  return wrap(
3880  unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy), IsSigned, Name));
3881 }
3882 
3884  LLVMTypeRef DestTy, const char *Name) {
3885  return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
3886  /*isSigned*/true, Name));
3887 }
3888