LLVM  13.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  char *AsmString, size_t AsmStringSize,
465  char *Constraints, size_t ConstraintsSize,
466  LLVMBool HasSideEffects, LLVMBool IsAlignStack,
467  LLVMInlineAsmDialect Dialect) {
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));
481 }
482 
483 
484 /*--.. Operations on module contexts ......................................--*/
486  return wrap(&unwrap(M)->getContext());
487 }
488 
489 
490 /*===-- Operations on types -----------------------------------------------===*/
491 
492 /*--.. Operations on all types (mostly) ....................................--*/
493 
495  switch (unwrap(Ty)->getTypeID()) {
496  case Type::VoidTyID:
497  return LLVMVoidTypeKind;
498  case Type::HalfTyID:
499  return LLVMHalfTypeKind;
500  case Type::BFloatTyID:
501  return LLVMBFloatTypeKind;
502  case Type::FloatTyID:
503  return LLVMFloatTypeKind;
504  case Type::DoubleTyID:
505  return LLVMDoubleTypeKind;
506  case Type::X86_FP80TyID:
507  return LLVMX86_FP80TypeKind;
508  case Type::FP128TyID:
509  return LLVMFP128TypeKind;
510  case Type::PPC_FP128TyID:
511  return LLVMPPC_FP128TypeKind;
512  case Type::LabelTyID:
513  return LLVMLabelTypeKind;
514  case Type::MetadataTyID:
515  return LLVMMetadataTypeKind;
516  case Type::IntegerTyID:
517  return LLVMIntegerTypeKind;
518  case Type::FunctionTyID:
519  return LLVMFunctionTypeKind;
520  case Type::StructTyID:
521  return LLVMStructTypeKind;
522  case Type::ArrayTyID:
523  return LLVMArrayTypeKind;
524  case Type::PointerTyID:
525  return LLVMPointerTypeKind;
527  return LLVMVectorTypeKind;
528  case Type::X86_MMXTyID:
529  return LLVMX86_MMXTypeKind;
530  case Type::X86_AMXTyID:
531  return LLVMX86_AMXTypeKind;
532  case Type::TokenTyID:
533  return LLVMTokenTypeKind;
536  }
537  llvm_unreachable("Unhandled TypeID.");
538 }
539 
541 {
542  return unwrap(Ty)->isSized();
543 }
544 
546  return wrap(&unwrap(Ty)->getContext());
547 }
548 
550  return unwrap(Ty)->print(errs(), /*IsForDebug=*/true);
551 }
552 
554  std::string buf;
555  raw_string_ostream os(buf);
556 
557  if (unwrap(Ty))
558  unwrap(Ty)->print(os);
559  else
560  os << "Printing <null> Type";
561 
562  os.flush();
563 
564  return strdup(buf.c_str());
565 }
566 
567 /*--.. Operations on integer types .........................................--*/
568 
570  return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
571 }
573  return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
574 }
576  return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
577 }
579  return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
580 }
582  return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
583 }
586 }
588  return wrap(IntegerType::get(*unwrap(C), NumBits));
589 }
590 
593 }
596 }
599 }
602 }
605 }
608 }
609 LLVMTypeRef LLVMIntType(unsigned NumBits) {
610  return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
611 }
612 
613 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
614  return unwrap<IntegerType>(IntegerTy)->getBitWidth();
615 }
616 
617 /*--.. Operations on real types ............................................--*/
618 
620  return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
621 }
624 }
626  return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
627 }
630 }
633 }
635  return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
636 }
639 }
642 }
645 }
646 
649 }
652 }
655 }
658 }
661 }
664 }
667 }
670 }
673 }
674 
675 /*--.. Operations on function types ........................................--*/
676 
678  LLVMTypeRef *ParamTypes, unsigned ParamCount,
679  LLVMBool IsVarArg) {
680  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
681  return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
682 }
683 
685  return unwrap<FunctionType>(FunctionTy)->isVarArg();
686 }
687 
689  return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
690 }
691 
692 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
693  return unwrap<FunctionType>(FunctionTy)->getNumParams();
694 }
695 
696 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
697  FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
698  for (Type *T : Ty->params())
699  *Dest++ = wrap(T);
700 }
701 
702 /*--.. Operations on struct types ..........................................--*/
703 
705  unsigned ElementCount, LLVMBool Packed) {
706  ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
707  return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
708 }
709 
711  unsigned ElementCount, LLVMBool Packed) {
712  return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
713  ElementCount, Packed);
714 }
715 
717 {
718  return wrap(StructType::create(*unwrap(C), Name));
719 }
720 
722 {
723  StructType *Type = unwrap<StructType>(Ty);
724  if (!Type->hasName())
725  return nullptr;
726  return Type->getName().data();
727 }
728 
729 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
730  unsigned ElementCount, LLVMBool Packed) {
731  ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
732  unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
733 }
734 
736  return unwrap<StructType>(StructTy)->getNumElements();
737 }
738 
740  StructType *Ty = unwrap<StructType>(StructTy);
741  for (Type *T : Ty->elements())
742  *Dest++ = wrap(T);
743 }
744 
746  StructType *Ty = unwrap<StructType>(StructTy);
747  return wrap(Ty->getTypeAtIndex(i));
748 }
749 
751  return unwrap<StructType>(StructTy)->isPacked();
752 }
753 
755  return unwrap<StructType>(StructTy)->isOpaque();
756 }
757 
759  return unwrap<StructType>(StructTy)->isLiteral();
760 }
761 
763  return wrap(StructType::getTypeByName(unwrap(M)->getContext(), Name));
764 }
765 
768 }
769 
770 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
771 
773  int i = 0;
774  for (auto *T : unwrap(Tp)->subtypes()) {
775  Arr[i] = wrap(T);
776  i++;
777  }
778 }
779 
781  return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
782 }
783 
785  return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
786 }
787 
789  return wrap(FixedVectorType::get(unwrap(ElementType), ElementCount));
790 }
791 
793  unsigned ElementCount) {
794  return wrap(ScalableVectorType::get(unwrap(ElementType), ElementCount));
795 }
796 
798  auto *Ty = unwrap<Type>(WrappedTy);
799  if (auto *PTy = dyn_cast<PointerType>(Ty))
800  return wrap(PTy->getElementType());
801  if (auto *ATy = dyn_cast<ArrayType>(Ty))
802  return wrap(ATy->getElementType());
803  return wrap(cast<VectorType>(Ty)->getElementType());
804 }
805 
807  return unwrap(Tp)->getNumContainedTypes();
808 }
809 
810 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
811  return unwrap<ArrayType>(ArrayTy)->getNumElements();
812 }
813 
815  return unwrap<PointerType>(PointerTy)->getAddressSpace();
816 }
817 
818 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
819  return unwrap<VectorType>(VectorTy)->getElementCount().getKnownMinValue();
820 }
821 
822 /*--.. Operations on other types ...........................................--*/
823 
825  return wrap(Type::getVoidTy(*unwrap(C)));
826 }
828  return wrap(Type::getLabelTy(*unwrap(C)));
829 }
831  return wrap(Type::getTokenTy(*unwrap(C)));
832 }
834  return wrap(Type::getMetadataTy(*unwrap(C)));
835 }
836 
839 }
842 }
843 
844 /*===-- Operations on values ----------------------------------------------===*/
845 
846 /*--.. Operations on all values ............................................--*/
847 
849  return wrap(unwrap(Val)->getType());
850 }
851 
853  switch(unwrap(Val)->getValueID()) {
854 #define LLVM_C_API 1
855 #define HANDLE_VALUE(Name) \
856  case Value::Name##Val: \
857  return LLVM##Name##ValueKind;
858 #include "llvm/IR/Value.def"
859  default:
861  }
862 }
863 
864 const char *LLVMGetValueName2(LLVMValueRef Val, size_t *Length) {
865  auto *V = unwrap(Val);
866  *Length = V->getName().size();
867  return V->getName().data();
868 }
869 
870 void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen) {
871  unwrap(Val)->setName(StringRef(Name, NameLen));
872 }
873 
874 const char *LLVMGetValueName(LLVMValueRef Val) {
875  return unwrap(Val)->getName().data();
876 }
877 
878 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
879  unwrap(Val)->setName(Name);
880 }
881 
883  unwrap(Val)->print(errs(), /*IsForDebug=*/true);
884 }
885 
887  std::string buf;
888  raw_string_ostream os(buf);
889 
890  if (unwrap(Val))
891  unwrap(Val)->print(os);
892  else
893  os << "Printing <null> Value";
894 
895  os.flush();
896 
897  return strdup(buf.c_str());
898 }
899 
901  unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
902 }
903 
905  return unwrap<Instruction>(Inst)->hasMetadata();
906 }
907 
908 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
909  auto *I = unwrap<Instruction>(Inst);
910  assert(I && "Expected instruction");
911  if (auto *MD = I->getMetadata(KindID))
912  return wrap(MetadataAsValue::get(I->getContext(), MD));
913  return nullptr;
914 }
915 
916 // MetadataAsValue uses a canonical format which strips the actual MDNode for
917 // MDNode with just a single constant value, storing just a ConstantAsMetadata
918 // This undoes this canonicalization, reconstructing the MDNode.
920  Metadata *MD = MAV->getMetadata();
921  assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
922  "Expected a metadata node or a canonicalized constant");
923 
924  if (MDNode *N = dyn_cast<MDNode>(MD))
925  return N;
926 
927  return MDNode::get(MAV->getContext(), MD);
928 }
929 
930 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
931  MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
932 
933  unwrap<Instruction>(Inst)->setMetadata(KindID, N);
934 }
935 
937  unsigned Kind;
939 };
940 
942 static LLVMValueMetadataEntry *
943 llvm_getMetadata(size_t *NumEntries,
944  llvm::function_ref<void(MetadataEntries &)> AccessMD) {
946  AccessMD(MVEs);
947 
949  static_cast<LLVMOpaqueValueMetadataEntry *>(
950  safe_malloc(MVEs.size() * sizeof(LLVMOpaqueValueMetadataEntry)));
951  for (unsigned i = 0; i < MVEs.size(); ++i) {
952  const auto &ModuleFlag = MVEs[i];
953  Result[i].Kind = ModuleFlag.first;
954  Result[i].Metadata = wrap(ModuleFlag.second);
955  }
956  *NumEntries = MVEs.size();
957  return Result;
958 }
959 
962  size_t *NumEntries) {
963  return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
964  Entries.clear();
965  unwrap<Instruction>(Value)->getAllMetadata(Entries);
966  });
967 }
968 
969 /*--.. Conversion functions ................................................--*/
970 
971 #define LLVM_DEFINE_VALUE_CAST(name) \
972  LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
973  return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
974  }
975 
977 
979  if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
980  if (isa<MDNode>(MD->getMetadata()) ||
981  isa<ValueAsMetadata>(MD->getMetadata()))
982  return Val;
983  return nullptr;
984 }
985 
987  if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
988  if (isa<MDString>(MD->getMetadata()))
989  return Val;
990  return nullptr;
991 }
992 
993 /*--.. Operations on Uses ..................................................--*/
995  Value *V = unwrap(Val);
997  if (I == V->use_end())
998  return nullptr;
999  return wrap(&*I);
1000 }
1001 
1003  Use *Next = unwrap(U)->getNext();
1004  if (Next)
1005  return wrap(Next);
1006  return nullptr;
1007 }
1008 
1010  return wrap(unwrap(U)->getUser());
1011 }
1012 
1014  return wrap(unwrap(U)->get());
1015 }
1016 
1017 /*--.. Operations on Users .................................................--*/
1018 
1020  unsigned Index) {
1021  Metadata *Op = N->getOperand(Index);
1022  if (!Op)
1023  return nullptr;
1024  if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
1025  return wrap(C->getValue());
1026  return wrap(MetadataAsValue::get(Context, Op));
1027 }
1028 
1030  Value *V = unwrap(Val);
1031  if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
1032  if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1033  assert(Index == 0 && "Function-local metadata can only have one operand");
1034  return wrap(L->getValue());
1035  }
1036  return getMDNodeOperandImpl(V->getContext(),
1037  cast<MDNode>(MD->getMetadata()), Index);
1038  }
1039 
1040  return wrap(cast<User>(V)->getOperand(Index));
1041 }
1042 
1044  Value *V = unwrap(Val);
1045  return wrap(&cast<User>(V)->getOperandUse(Index));
1046 }
1047 
1049  unwrap<User>(Val)->setOperand(Index, unwrap(Op));
1050 }
1051 
1053  Value *V = unwrap(Val);
1054  if (isa<MetadataAsValue>(V))
1055  return LLVMGetMDNodeNumOperands(Val);
1056 
1057  return cast<User>(V)->getNumOperands();
1058 }
1059 
1060 /*--.. Operations on constants of any type .................................--*/
1061 
1063  return wrap(Constant::getNullValue(unwrap(Ty)));
1064 }
1065 
1067  return wrap(Constant::getAllOnesValue(unwrap(Ty)));
1068 }
1069 
1071  return wrap(UndefValue::get(unwrap(Ty)));
1072 }
1073 
1075  return wrap(PoisonValue::get(unwrap(Ty)));
1076 }
1077 
1079  return isa<Constant>(unwrap(Ty));
1080 }
1081 
1083  if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
1084  return C->isNullValue();
1085  return false;
1086 }
1087 
1089  return isa<UndefValue>(unwrap(Val));
1090 }
1091 
1093  return isa<PoisonValue>(unwrap(Val));
1094 }
1095 
1097  return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
1098 }
1099 
1100 /*--.. Operations on metadata nodes ........................................--*/
1101 
1103  size_t SLen) {
1104  return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
1105 }
1106 
1108  size_t Count) {
1109  return wrap(MDNode::get(*unwrap(C), ArrayRef<Metadata*>(unwrap(MDs), Count)));
1110 }
1111 
1113  unsigned SLen) {
1114  LLVMContext &Context = *unwrap(C);
1115  return wrap(MetadataAsValue::get(
1116  Context, MDString::get(Context, StringRef(Str, SLen))));
1117 }
1118 
1119 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
1120  return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
1121 }
1122 
1124  unsigned Count) {
1125  LLVMContext &Context = *unwrap(C);
1127  for (auto *OV : makeArrayRef(Vals, Count)) {
1128  Value *V = unwrap(OV);
1129  Metadata *MD;
1130  if (!V)
1131  MD = nullptr;
1132  else if (auto *C = dyn_cast<Constant>(V))
1133  MD = ConstantAsMetadata::get(C);
1134  else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
1135  MD = MDV->getMetadata();
1136  assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
1137  "outside of direct argument to call");
1138  } else {
1139  // This is function-local metadata. Pretend to make an MDNode.
1140  assert(Count == 1 &&
1141  "Expected only one operand to function-local metadata");
1143  }
1144 
1145  MDs.push_back(MD);
1146  }
1148 }
1149 
1150 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
1151  return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
1152 }
1153 
1155  return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
1156 }
1157 
1159  auto *V = unwrap(Val);
1160  if (auto *C = dyn_cast<Constant>(V))
1161  return wrap(ConstantAsMetadata::get(C));
1162  if (auto *MAV = dyn_cast<MetadataAsValue>(V))
1163  return wrap(MAV->getMetadata());
1164  return wrap(ValueAsMetadata::get(V));
1165 }
1166 
1167 const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
1168  if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
1169  if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
1170  *Length = S->getString().size();
1171  return S->getString().data();
1172  }
1173  *Length = 0;
1174  return nullptr;
1175 }
1176 
1178  auto *MD = cast<MetadataAsValue>(unwrap(V));
1179  if (isa<ValueAsMetadata>(MD->getMetadata()))
1180  return 1;
1181  return cast<MDNode>(MD->getMetadata())->getNumOperands();
1182 }
1183 
1185  Module *Mod = unwrap(M);
1186  Module::named_metadata_iterator I = Mod->named_metadata_begin();
1187  if (I == Mod->named_metadata_end())
1188  return nullptr;
1189  return wrap(&*I);
1190 }
1191 
1193  Module *Mod = unwrap(M);
1194  Module::named_metadata_iterator I = Mod->named_metadata_end();
1195  if (I == Mod->named_metadata_begin())
1196  return nullptr;
1197  return wrap(&*--I);
1198 }
1199 
1201  NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD);
1203  if (++I == NamedNode->getParent()->named_metadata_end())
1204  return nullptr;
1205  return wrap(&*I);
1206 }
1207 
1209  NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD);
1211  if (I == NamedNode->getParent()->named_metadata_begin())
1212  return nullptr;
1213  return wrap(&*--I);
1214 }
1215 
1217  const char *Name, size_t NameLen) {
1218  return wrap(unwrap(M)->getNamedMetadata(StringRef(Name, NameLen)));
1219 }
1220 
1222  const char *Name, size_t NameLen) {
1223  return wrap(unwrap(M)->getOrInsertNamedMetadata({Name, NameLen}));
1224 }
1225 
1226 const char *LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NMD, size_t *NameLen) {
1227  NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD);
1228  *NameLen = NamedNode->getName().size();
1229  return NamedNode->getName().data();
1230 }
1231 
1233  auto *MD = cast<MetadataAsValue>(unwrap(V));
1234  if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1235  *Dest = wrap(MDV->getValue());
1236  return;
1237  }
1238  const auto *N = cast<MDNode>(MD->getMetadata());
1239  const unsigned numOperands = N->getNumOperands();
1240  LLVMContext &Context = unwrap(V)->getContext();
1241  for (unsigned i = 0; i < numOperands; i++)
1242  Dest[i] = getMDNodeOperandImpl(Context, N, i);
1243 }
1244 
1246  if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
1247  return N->getNumOperands();
1248  }
1249  return 0;
1250 }
1251 
1253  LLVMValueRef *Dest) {
1254  NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
1255  if (!N)
1256  return;
1257  LLVMContext &Context = unwrap(M)->getContext();
1258  for (unsigned i=0;i<N->getNumOperands();i++)
1259  Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
1260 }
1261 
1263  LLVMValueRef Val) {
1264  NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
1265  if (!N)
1266  return;
1267  if (!Val)
1268  return;
1269  N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
1270 }
1271 
1272 const char *LLVMGetDebugLocDirectory(LLVMValueRef Val, unsigned *Length) {
1273  if (!Length) return nullptr;
1274  StringRef S;
1275  if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1276  if (const auto &DL = I->getDebugLoc()) {
1277  S = DL->getDirectory();
1278  }
1279  } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1281  GV->getDebugInfo(GVEs);
1282  if (GVEs.size())
1283  if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1284  S = DGV->getDirectory();
1285  } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1286  if (const DISubprogram *DSP = F->getSubprogram())
1287  S = DSP->getDirectory();
1288  } else {
1289  assert(0 && "Expected Instruction, GlobalVariable or Function");
1290  return nullptr;
1291  }
1292  *Length = S.size();
1293  return S.data();
1294 }
1295 
1296 const char *LLVMGetDebugLocFilename(LLVMValueRef Val, unsigned *Length) {
1297  if (!Length) return nullptr;
1298  StringRef S;
1299  if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1300  if (const auto &DL = I->getDebugLoc()) {
1301  S = DL->getFilename();
1302  }
1303  } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1305  GV->getDebugInfo(GVEs);
1306  if (GVEs.size())
1307  if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1308  S = DGV->getFilename();
1309  } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1310  if (const DISubprogram *DSP = F->getSubprogram())
1311  S = DSP->getFilename();
1312  } else {
1313  assert(0 && "Expected Instruction, GlobalVariable or Function");
1314  return nullptr;
1315  }
1316  *Length = S.size();
1317  return S.data();
1318 }
1319 
1321  unsigned L = 0;
1322  if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1323  if (const auto &DL = I->getDebugLoc()) {
1324  L = DL->getLine();
1325  }
1326  } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1328  GV->getDebugInfo(GVEs);
1329  if (GVEs.size())
1330  if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1331  L = DGV->getLine();
1332  } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1333  if (const DISubprogram *DSP = F->getSubprogram())
1334  L = DSP->getLine();
1335  } else {
1336  assert(0 && "Expected Instruction, GlobalVariable or Function");
1337  return -1;
1338  }
1339  return L;
1340 }
1341 
1343  unsigned C = 0;
1344  if (const auto *I = dyn_cast<Instruction>(unwrap(Val)))
1345  if (const auto &DL = I->getDebugLoc())
1346  C = DL->getColumn();
1347  return C;
1348 }
1349 
1350 /*--.. Operations on scalar constants ......................................--*/
1351 
1352 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
1353  LLVMBool SignExtend) {
1354  return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
1355 }
1356 
1358  unsigned NumWords,
1359  const uint64_t Words[]) {
1360  IntegerType *Ty = unwrap<IntegerType>(IntTy);
1361  return wrap(ConstantInt::get(Ty->getContext(),
1362  APInt(Ty->getBitWidth(),
1363  makeArrayRef(Words, NumWords))));
1364 }
1365 
1367  uint8_t Radix) {
1368  return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
1369  Radix));
1370 }
1371 
1373  unsigned SLen, uint8_t Radix) {
1374  return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
1375  Radix));
1376 }
1377 
1379  return wrap(ConstantFP::get(unwrap(RealTy), N));
1380 }
1381 
1383  return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
1384 }
1385 
1387  unsigned SLen) {
1388  return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
1389 }
1390 
1391 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
1392  return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
1393 }
1394 
1395 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
1396  return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
1397 }
1398 
1399 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
1400  ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
1401  Type *Ty = cFP->getType();
1402 
1403  if (Ty->isFloatTy()) {
1404  *LosesInfo = false;
1405  return cFP->getValueAPF().convertToFloat();
1406  }
1407 
1408  if (Ty->isDoubleTy()) {
1409  *LosesInfo = false;
1410  return cFP->getValueAPF().convertToDouble();
1411  }
1412 
1413  bool APFLosesInfo;
1414  APFloat APF = cFP->getValueAPF();
1416  *LosesInfo = APFLosesInfo;
1417  return APF.convertToDouble();
1418 }
1419 
1420 /*--.. Operations on composite constants ...................................--*/
1421 
1423  unsigned Length,
1424  LLVMBool DontNullTerminate) {
1425  /* Inverted the sense of AddNull because ', 0)' is a
1426  better mnemonic for null termination than ', 1)'. */
1427  return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
1428  DontNullTerminate == 0));
1429 }
1430 
1431 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
1432  LLVMBool DontNullTerminate) {
1433  return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
1434  DontNullTerminate);
1435 }
1436 
1438  return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
1439 }
1440 
1442  return unwrap<ConstantDataSequential>(C)->isString();
1443 }
1444 
1445 const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1446  StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1447  *Length = Str.size();
1448  return Str.data();
1449 }
1450 
1452  LLVMValueRef *ConstantVals, unsigned Length) {
1453  ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1454  return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1455 }
1456 
1458  LLVMValueRef *ConstantVals,
1459  unsigned Count, LLVMBool Packed) {
1460  Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1461  return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
1462  Packed != 0));
1463 }
1464 
1465 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
1466  LLVMBool Packed) {
1467  return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1468  Packed);
1469 }
1470 
1472  LLVMValueRef *ConstantVals,
1473  unsigned Count) {
1474  Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1475  StructType *Ty = cast<StructType>(unwrap(StructTy));
1476 
1477  return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
1478 }
1479 
1480 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
1482  unwrap<Constant>(ScalarConstantVals, Size), Size)));
1483 }
1484 
1485 /*-- Opcode mapping */
1486 
1487 static LLVMOpcode map_to_llvmopcode(int opcode)
1488 {
1489  switch (opcode) {
1490  default: llvm_unreachable("Unhandled Opcode.");
1491 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
1492 #include "llvm/IR/Instruction.def"
1493 #undef HANDLE_INST
1494  }
1495 }
1496 
1498 {
1499  switch (code) {
1500 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
1501 #include "llvm/IR/Instruction.def"
1502 #undef HANDLE_INST
1503  }
1504  llvm_unreachable("Unhandled Opcode.");
1505 }
1506 
1507 /*--.. Constant expressions ................................................--*/
1508 
1510  return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
1511 }
1512 
1514  return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
1515 }
1516 
1518  return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
1519 }
1520 
1522  return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1523 }
1524 
1526  return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
1527 }
1528 
1530  return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
1531 }
1532 
1533 
1535  return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
1536 }
1537 
1539  return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
1540 }
1541 
1543  return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
1544  unwrap<Constant>(RHSConstant)));
1545 }
1546 
1548  LLVMValueRef RHSConstant) {
1549  return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
1550  unwrap<Constant>(RHSConstant)));
1551 }
1552 
1554  LLVMValueRef RHSConstant) {
1555  return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
1556  unwrap<Constant>(RHSConstant)));
1557 }
1558 
1560  return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
1561  unwrap<Constant>(RHSConstant)));
1562 }
1563 
1565  return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
1566  unwrap<Constant>(RHSConstant)));
1567 }
1568 
1570  LLVMValueRef RHSConstant) {
1571  return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
1572  unwrap<Constant>(RHSConstant)));
1573 }
1574 
1576  LLVMValueRef RHSConstant) {
1577  return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
1578  unwrap<Constant>(RHSConstant)));
1579 }
1580 
1582  return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
1583  unwrap<Constant>(RHSConstant)));
1584 }
1585 
1587  return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
1588  unwrap<Constant>(RHSConstant)));
1589 }
1590 
1592  LLVMValueRef RHSConstant) {
1593  return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
1594  unwrap<Constant>(RHSConstant)));
1595 }
1596 
1598  LLVMValueRef RHSConstant) {
1599  return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
1600  unwrap<Constant>(RHSConstant)));
1601 }
1602 
1604  return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
1605  unwrap<Constant>(RHSConstant)));
1606 }
1607 
1609  return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
1610  unwrap<Constant>(RHSConstant)));
1611 }
1612 
1614  LLVMValueRef RHSConstant) {
1615  return wrap(ConstantExpr::getExactUDiv(unwrap<Constant>(LHSConstant),
1616  unwrap<Constant>(RHSConstant)));
1617 }
1618 
1620  return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
1621  unwrap<Constant>(RHSConstant)));
1622 }
1623 
1625  LLVMValueRef RHSConstant) {
1626  return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
1627  unwrap<Constant>(RHSConstant)));
1628 }
1629 
1631  return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
1632  unwrap<Constant>(RHSConstant)));
1633 }
1634 
1636  return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
1637  unwrap<Constant>(RHSConstant)));
1638 }
1639 
1641  return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
1642  unwrap<Constant>(RHSConstant)));
1643 }
1644 
1646  return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
1647  unwrap<Constant>(RHSConstant)));
1648 }
1649 
1651  return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
1652  unwrap<Constant>(RHSConstant)));
1653 }
1654 
1656  return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
1657  unwrap<Constant>(RHSConstant)));
1658 }
1659 
1661  return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
1662  unwrap<Constant>(RHSConstant)));
1663 }
1664 
1666  LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1668  unwrap<Constant>(LHSConstant),
1669  unwrap<Constant>(RHSConstant)));
1670 }
1671 
1673  LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1675  unwrap<Constant>(LHSConstant),
1676  unwrap<Constant>(RHSConstant)));
1677 }
1678 
1680  return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1681  unwrap<Constant>(RHSConstant)));
1682 }
1683 
1685  return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
1686  unwrap<Constant>(RHSConstant)));
1687 }
1688 
1690  return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1691  unwrap<Constant>(RHSConstant)));
1692 }
1693 
1695  LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1696  ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1697  NumIndices);
1698  Constant *Val = unwrap<Constant>(ConstantVal);
1699  Type *Ty =
1700  cast<PointerType>(Val->getType()->getScalarType())->getElementType();
1701  return wrap(ConstantExpr::getGetElementPtr(Ty, Val, IdxList));
1702 }
1703 
1705  LLVMValueRef *ConstantIndices,
1706  unsigned NumIndices) {
1707  ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1708  NumIndices);
1709  Constant *Val = unwrap<Constant>(ConstantVal);
1710  Type *Ty =
1711  cast<PointerType>(Val->getType()->getScalarType())->getElementType();
1712  return wrap(ConstantExpr::getInBoundsGetElementPtr(Ty, Val, IdxList));
1713 }
1714 
1716  return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1717  unwrap(ToType)));
1718 }
1719 
1721  return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1722  unwrap(ToType)));
1723 }
1724 
1726  return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1727  unwrap(ToType)));
1728 }
1729 
1731  return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1732  unwrap(ToType)));
1733 }
1734 
1736  return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1737  unwrap(ToType)));
1738 }
1739 
1741  return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1742  unwrap(ToType)));
1743 }
1744 
1746  return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1747  unwrap(ToType)));
1748 }
1749 
1751  return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1752  unwrap(ToType)));
1753 }
1754 
1756  return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1757  unwrap(ToType)));
1758 }
1759 
1761  return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1762  unwrap(ToType)));
1763 }
1764 
1766  return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1767  unwrap(ToType)));
1768 }
1769 
1771  return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1772  unwrap(ToType)));
1773 }
1774 
1776  LLVMTypeRef ToType) {
1777  return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1778  unwrap(ToType)));
1779 }
1780 
1782  LLVMTypeRef ToType) {
1783  return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1784  unwrap(ToType)));
1785 }
1786 
1788  LLVMTypeRef ToType) {
1789  return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1790  unwrap(ToType)));
1791 }
1792 
1794  LLVMTypeRef ToType) {
1795  return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1796  unwrap(ToType)));
1797 }
1798 
1800  LLVMTypeRef ToType) {
1801  return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1802  unwrap(ToType)));
1803 }
1804 
1806  LLVMBool isSigned) {
1807  return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1808  unwrap(ToType), isSigned));
1809 }
1810 
1812  return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1813  unwrap(ToType)));
1814 }
1815 
1817  LLVMValueRef ConstantIfTrue,
1818  LLVMValueRef ConstantIfFalse) {
1819  return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1820  unwrap<Constant>(ConstantIfTrue),
1821  unwrap<Constant>(ConstantIfFalse)));
1822 }
1823 
1825  LLVMValueRef IndexConstant) {
1826  return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1827  unwrap<Constant>(IndexConstant)));
1828 }
1829 
1831  LLVMValueRef ElementValueConstant,
1832  LLVMValueRef IndexConstant) {
1833  return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1834  unwrap<Constant>(ElementValueConstant),
1835  unwrap<Constant>(IndexConstant)));
1836 }
1837 
1839  LLVMValueRef VectorBConstant,
1840  LLVMValueRef MaskConstant) {
1841  SmallVector<int, 16> IntMask;
1842  ShuffleVectorInst::getShuffleMask(unwrap<Constant>(MaskConstant), IntMask);
1843  return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1844  unwrap<Constant>(VectorBConstant),
1845  IntMask));
1846 }
1847 
1848 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1849  unsigned NumIdx) {
1850  return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1851  makeArrayRef(IdxList, NumIdx)));
1852 }
1853 
1855  LLVMValueRef ElementValueConstant,
1856  unsigned *IdxList, unsigned NumIdx) {
1857  return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1858  unwrap<Constant>(ElementValueConstant),
1859  makeArrayRef(IdxList, NumIdx)));
1860 }
1861 
1862 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1863  const char *Constraints,
1864  LLVMBool HasSideEffects,
1865  LLVMBool IsAlignStack) {
1866  return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1867  Constraints, HasSideEffects, IsAlignStack));
1868 }
1869 
1871  return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1872 }
1873 
1874 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1875 
1877  return wrap(unwrap<GlobalValue>(Global)->getParent());
1878 }
1879 
1881  return unwrap<GlobalValue>(Global)->isDeclaration();
1882 }
1883 
1885  switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1887  return LLVMExternalLinkage;
1891  return LLVMLinkOnceAnyLinkage;
1893  return LLVMLinkOnceODRLinkage;
1895  return LLVMWeakAnyLinkage;
1897  return LLVMWeakODRLinkage;
1899  return LLVMAppendingLinkage;
1901  return LLVMInternalLinkage;
1903  return LLVMPrivateLinkage;
1905  return LLVMExternalWeakLinkage;
1907  return LLVMCommonLinkage;
1908  }
1909 
1910  llvm_unreachable("Invalid GlobalValue linkage!");
1911 }
1912 
1914  GlobalValue *GV = unwrap<GlobalValue>(Global);
1915 
1916  switch (Linkage) {
1917  case LLVMExternalLinkage:
1919  break;
1922  break;
1925  break;
1928  break;
1930  LLVM_DEBUG(
1931  errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1932  "longer supported.");
1933  break;
1934  case LLVMWeakAnyLinkage:
1936  break;
1937  case LLVMWeakODRLinkage:
1939  break;
1940  case LLVMAppendingLinkage:
1942  break;
1943  case LLVMInternalLinkage:
1945  break;
1946  case LLVMPrivateLinkage:
1948  break;
1951  break;
1954  break;
1955  case LLVMDLLImportLinkage:
1956  LLVM_DEBUG(
1957  errs()
1958  << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1959  break;
1960  case LLVMDLLExportLinkage:
1961  LLVM_DEBUG(
1962  errs()
1963  << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1964  break;
1967  break;
1968  case LLVMGhostLinkage:
1969  LLVM_DEBUG(
1970  errs() << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1971  break;
1972  case LLVMCommonLinkage:
1974  break;
1975  }
1976 }
1977 
1978 const char *LLVMGetSection(LLVMValueRef Global) {
1979  // Using .data() is safe because of how GlobalObject::setSection is
1980  // implemented.
1981  return unwrap<GlobalValue>(Global)->getSection().data();
1982 }
1983 
1984 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1985  unwrap<GlobalObject>(Global)->setSection(Section);
1986 }
1987 
1989  return static_cast<LLVMVisibility>(
1990  unwrap<GlobalValue>(Global)->getVisibility());
1991 }
1992 
1994  unwrap<GlobalValue>(Global)
1995  ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1996 }
1997 
1999  return static_cast<LLVMDLLStorageClass>(
2000  unwrap<GlobalValue>(Global)->getDLLStorageClass());
2001 }
2002 
2004  unwrap<GlobalValue>(Global)->setDLLStorageClass(
2005  static_cast<GlobalValue::DLLStorageClassTypes>(Class));
2006 }
2007 
2009  switch (unwrap<GlobalValue>(Global)->getUnnamedAddr()) {
2011  return LLVMNoUnnamedAddr;
2013  return LLVMLocalUnnamedAddr;
2015  return LLVMGlobalUnnamedAddr;
2016  }
2017  llvm_unreachable("Unknown UnnamedAddr kind!");
2018 }
2019 
2021  GlobalValue *GV = unwrap<GlobalValue>(Global);
2022 
2023  switch (UnnamedAddr) {
2024  case LLVMNoUnnamedAddr:
2026  case LLVMLocalUnnamedAddr:
2028  case LLVMGlobalUnnamedAddr:
2030  }
2031 }
2032 
2034  return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
2035 }
2036 
2037 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
2038  unwrap<GlobalValue>(Global)->setUnnamedAddr(
2039  HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
2041 }
2042 
2044  return wrap(unwrap<GlobalValue>(Global)->getValueType());
2045 }
2046 
2047 /*--.. Operations on global variables, load and store instructions .........--*/
2048 
2050  Value *P = unwrap<Value>(V);
2051  if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2052  return GV->getAlignment();
2053  if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
2054  return AI->getAlignment();
2055  if (LoadInst *LI = dyn_cast<LoadInst>(P))
2056  return LI->getAlignment();
2057  if (StoreInst *SI = dyn_cast<StoreInst>(P))
2058  return SI->getAlignment();
2059  if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2060  return RMWI->getAlign().value();
2061  if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2062  return CXI->getAlign().value();
2063 
2065  "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, "
2066  "and AtomicCmpXchgInst have alignment");
2067 }
2068 
2069 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
2070  Value *P = unwrap<Value>(V);
2071  if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2072  GV->setAlignment(MaybeAlign(Bytes));
2073  else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
2074  AI->setAlignment(Align(Bytes));
2075  else if (LoadInst *LI = dyn_cast<LoadInst>(P))
2076  LI->setAlignment(Align(Bytes));
2077  else if (StoreInst *SI = dyn_cast<StoreInst>(P))
2078  SI->setAlignment(Align(Bytes));
2079  else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2080  RMWI->setAlignment(Align(Bytes));
2081  else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2082  CXI->setAlignment(Align(Bytes));
2083  else
2085  "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, and "
2086  "and AtomicCmpXchgInst have alignment");
2087 }
2088 
2090  size_t *NumEntries) {
2091  return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
2092  Entries.clear();
2093  if (Instruction *Instr = dyn_cast<Instruction>(unwrap(Value))) {
2094  Instr->getAllMetadata(Entries);
2095  } else {
2096  unwrap<GlobalObject>(Value)->getAllMetadata(Entries);
2097  }
2098  });
2099 }
2100 
2102  unsigned Index) {
2104  static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2105  return MVE.Kind;
2106 }
2107 
2110  unsigned Index) {
2112  static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2113  return MVE.Metadata;
2114 }
2115 
2117  free(Entries);
2118 }
2119 
2121  LLVMMetadataRef MD) {
2122  unwrap<GlobalObject>(Global)->setMetadata(Kind, unwrap<MDNode>(MD));
2123 }
2124 
2126  unwrap<GlobalObject>(Global)->eraseMetadata(Kind);
2127 }
2128 
2130  unwrap<GlobalObject>(Global)->clearMetadata();
2131 }
2132 
2133 /*--.. Operations on global variables ......................................--*/
2134 
2136  return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2137  GlobalValue::ExternalLinkage, nullptr, Name));
2138 }
2139 
2141  const char *Name,
2142  unsigned AddressSpace) {
2143  return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2146  AddressSpace));
2147 }
2148 
2150  return wrap(unwrap(M)->getNamedGlobal(Name));
2151 }
2152 
2154  Module *Mod = unwrap(M);
2155  Module::global_iterator I = Mod->global_begin();
2156  if (I == Mod->global_end())
2157  return nullptr;
2158  return wrap(&*I);
2159 }
2160 
2162  Module *Mod = unwrap(M);
2163  Module::global_iterator I = Mod->global_end();
2164  if (I == Mod->global_begin())
2165  return nullptr;
2166  return wrap(&*--I);
2167 }
2168 
2170  GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2172  if (++I == GV->getParent()->global_end())
2173  return nullptr;
2174  return wrap(&*I);
2175 }
2176 
2178  GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2180  if (I == GV->getParent()->global_begin())
2181  return nullptr;
2182  return wrap(&*--I);
2183 }
2184 
2186  unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
2187 }
2188 
2190  GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
2191  if ( !GV->hasInitializer() )
2192  return nullptr;
2193  return wrap(GV->getInitializer());
2194 }
2195 
2197  unwrap<GlobalVariable>(GlobalVar)
2198  ->setInitializer(unwrap<Constant>(ConstantVal));
2199 }
2200 
2202  return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
2203 }
2204 
2206  unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
2207 }
2208 
2210  return unwrap<GlobalVariable>(GlobalVar)->isConstant();
2211 }
2212 
2214  unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
2215 }
2216 
2218  switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
2220  return LLVMNotThreadLocal;
2224  return LLVMLocalDynamicTLSModel;
2226  return LLVMInitialExecTLSModel;
2228  return LLVMLocalExecTLSModel;
2229  }
2230 
2231  llvm_unreachable("Invalid GlobalVariable thread local mode");
2232 }
2233 
2235  GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2236 
2237  switch (Mode) {
2238  case LLVMNotThreadLocal:
2240  break;
2243  break;
2246  break;
2249  break;
2250  case LLVMLocalExecTLSModel:
2252  break;
2253  }
2254 }
2255 
2257  return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
2258 }
2259 
2261  unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
2262 }
2263 
2264 /*--.. Operations on aliases ......................................--*/
2265 
2267  const char *Name) {
2268  auto *PTy = cast<PointerType>(unwrap(Ty));
2269  return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
2271  unwrap<Constant>(Aliasee), unwrap(M)));
2272 }
2273 
2275  const char *Name, size_t NameLen) {
2276  return wrap(unwrap(M)->getNamedAlias(Name));
2277 }
2278 
2280  Module *Mod = unwrap(M);
2281  Module::alias_iterator I = Mod->alias_begin();
2282  if (I == Mod->alias_end())
2283  return nullptr;
2284  return wrap(&*I);
2285 }
2286 
2288  Module *Mod = unwrap(M);
2289  Module::alias_iterator I = Mod->alias_end();
2290  if (I == Mod->alias_begin())
2291  return nullptr;
2292  return wrap(&*--I);
2293 }
2294 
2296  GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2297  Module::alias_iterator I(Alias);
2298  if (++I == Alias->getParent()->alias_end())
2299  return nullptr;
2300  return wrap(&*I);
2301 }
2302 
2304  GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2305  Module::alias_iterator I(Alias);
2306  if (I == Alias->getParent()->alias_begin())
2307  return nullptr;
2308  return wrap(&*--I);
2309 }
2310 
2312  return wrap(unwrap<GlobalAlias>(Alias)->getAliasee());
2313 }
2314 
2316  unwrap<GlobalAlias>(Alias)->setAliasee(unwrap<Constant>(Aliasee));
2317 }
2318 
2319 /*--.. Operations on functions .............................................--*/
2320 
2322  LLVMTypeRef FunctionTy) {
2323  return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
2325 }
2326 
2328  return wrap(unwrap(M)->getFunction(Name));
2329 }
2330 
2332  Module *Mod = unwrap(M);
2333  Module::iterator I = Mod->begin();
2334  if (I == Mod->end())
2335  return nullptr;
2336  return wrap(&*I);
2337 }
2338 
2340  Module *Mod = unwrap(M);
2341  Module::iterator I = Mod->end();
2342  if (I == Mod->begin())
2343  return nullptr;
2344  return wrap(&*--I);
2345 }
2346 
2348  Function *Func = unwrap<Function>(Fn);
2349  Module::iterator I(Func);
2350  if (++I == Func->getParent()->end())
2351  return nullptr;
2352  return wrap(&*I);
2353 }
2354 
2356  Function *Func = unwrap<Function>(Fn);
2357  Module::iterator I(Func);
2358  if (I == Func->getParent()->begin())
2359  return nullptr;
2360  return wrap(&*--I);
2361 }
2362 
2364  unwrap<Function>(Fn)->eraseFromParent();
2365 }
2366 
2368  return unwrap<Function>(Fn)->hasPersonalityFn();
2369 }
2370 
2372  return wrap(unwrap<Function>(Fn)->getPersonalityFn());
2373 }
2374 
2376  unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
2377 }
2378 
2380  if (Function *F = dyn_cast<Function>(unwrap(Fn)))
2381  return F->getIntrinsicID();
2382  return 0;
2383 }
2384 
2386  assert(ID < llvm::Intrinsic::num_intrinsics && "Intrinsic ID out of range");
2387  return llvm::Intrinsic::ID(ID);
2388 }
2389 
2391  unsigned ID,
2392  LLVMTypeRef *ParamTypes,
2393  size_t ParamCount) {
2394  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2395  auto IID = llvm_map_to_intrinsic_id(ID);
2396  return wrap(llvm::Intrinsic::getDeclaration(unwrap(Mod), IID, Tys));
2397 }
2398 
2399 const char *LLVMIntrinsicGetName(unsigned ID, size_t *NameLength) {
2400  auto IID = llvm_map_to_intrinsic_id(ID);
2401  auto Str = llvm::Intrinsic::getName(IID);
2402  *NameLength = Str.size();
2403  return Str.data();
2404 }
2405 
2407  LLVMTypeRef *ParamTypes, size_t ParamCount) {
2408  auto IID = llvm_map_to_intrinsic_id(ID);
2409  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2410  return wrap(llvm::Intrinsic::getType(*unwrap(Ctx), IID, Tys));
2411 }
2412 
2414  LLVMTypeRef *ParamTypes,
2415  size_t ParamCount,
2416  size_t *NameLength) {
2417  auto IID = llvm_map_to_intrinsic_id(ID);
2418  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2419  auto Str = llvm::Intrinsic::getName(IID, Tys);
2420  *NameLength = Str.length();
2421  return strdup(Str.c_str());
2422 }
2423 
2424 unsigned LLVMLookupIntrinsicID(const char *Name, size_t NameLen) {
2425  return Function::lookupIntrinsicID({Name, NameLen});
2426 }
2427 
2429  auto IID = llvm_map_to_intrinsic_id(ID);
2430  return llvm::Intrinsic::isOverloaded(IID);
2431 }
2432 
2434  return unwrap<Function>(Fn)->getCallingConv();
2435 }
2436 
2437 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
2438  return unwrap<Function>(Fn)->setCallingConv(
2439  static_cast<CallingConv::ID>(CC));
2440 }
2441 
2442 const char *LLVMGetGC(LLVMValueRef Fn) {
2443  Function *F = unwrap<Function>(Fn);
2444  return F->hasGC()? F->getGC().c_str() : nullptr;
2445 }
2446 
2447 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
2448  Function *F = unwrap<Function>(Fn);
2449  if (GC)
2450  F->setGC(GC);
2451  else
2452  F->clearGC();
2453 }
2454 
2456  LLVMAttributeRef A) {
2457  unwrap<Function>(F)->addAttribute(Idx, unwrap(A));
2458 }
2459 
2461  auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2462  return AS.getNumAttributes();
2463 }
2464 
2467  auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2468  for (auto A : AS)
2469  *Attrs++ = wrap(A);
2470 }
2471 
2473  LLVMAttributeIndex Idx,
2474  unsigned KindID) {
2475  return wrap(unwrap<Function>(F)->getAttribute(Idx,
2476  (Attribute::AttrKind)KindID));
2477 }
2478 
2480  LLVMAttributeIndex Idx,
2481  const char *K, unsigned KLen) {
2482  return wrap(unwrap<Function>(F)->getAttribute(Idx, StringRef(K, KLen)));
2483 }
2484 
2486  unsigned KindID) {
2487  unwrap<Function>(F)->removeAttribute(Idx, (Attribute::AttrKind)KindID);
2488 }
2489 
2491  const char *K, unsigned KLen) {
2492  unwrap<Function>(F)->removeAttribute(Idx, StringRef(K, KLen));
2493 }
2494 
2496  const char *V) {
2497  Function *Func = unwrap<Function>(Fn);
2498  Attribute Attr = Attribute::get(Func->getContext(), A, V);
2499  Func->addAttribute(AttributeList::FunctionIndex, Attr);
2500 }
2501 
2502 /*--.. Operations on parameters ............................................--*/
2503 
2505  // This function is strictly redundant to
2506  // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
2507  return unwrap<Function>(FnRef)->arg_size();
2508 }
2509 
2510 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
2511  Function *Fn = unwrap<Function>(FnRef);
2512  for (Argument &A : Fn->args())
2513  *ParamRefs++ = wrap(&A);
2514 }
2515 
2517  Function *Fn = unwrap<Function>(FnRef);
2518  return wrap(&Fn->arg_begin()[index]);
2519 }
2520 
2522  return wrap(unwrap<Argument>(V)->getParent());
2523 }
2524 
2526  Function *Func = unwrap<Function>(Fn);
2527  Function::arg_iterator I = Func->arg_begin();
2528  if (I == Func->arg_end())
2529  return nullptr;
2530  return wrap(&*I);
2531 }
2532 
2534  Function *Func = unwrap<Function>(Fn);
2535  Function::arg_iterator I = Func->arg_end();
2536  if (I == Func->arg_begin())
2537  return nullptr;
2538  return wrap(&*--I);
2539 }
2540 
2542  Argument *A = unwrap<Argument>(Arg);
2543  Function *Fn = A->getParent();
2544  if (A->getArgNo() + 1 >= Fn->arg_size())
2545  return nullptr;
2546  return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
2547 }
2548 
2550  Argument *A = unwrap<Argument>(Arg);
2551  if (A->getArgNo() == 0)
2552  return nullptr;
2553  return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
2554 }
2555 
2557  Argument *A = unwrap<Argument>(Arg);
2558  A->addAttr(Attribute::getWithAlignment(A->getContext(), Align(align)));
2559 }
2560 
2561 /*--.. Operations on ifuncs ................................................--*/
2562 
2564  const char *Name, size_t NameLen,
2565  LLVMTypeRef Ty, unsigned AddrSpace,
2567  return wrap(GlobalIFunc::create(unwrap(Ty), AddrSpace,
2569  StringRef(Name, NameLen),
2570  unwrap<Constant>(Resolver), unwrap(M)));
2571 }
2572 
2574  const char *Name, size_t NameLen) {
2575  return wrap(unwrap(M)->getNamedIFunc(StringRef(Name, NameLen)));
2576 }
2577 
2579  Module *Mod = unwrap(M);
2580  Module::ifunc_iterator I = Mod->ifunc_begin();
2581  if (I == Mod->ifunc_end())
2582  return nullptr;
2583  return wrap(&*I);
2584 }
2585 
2587  Module *Mod = unwrap(M);
2588  Module::ifunc_iterator I = Mod->ifunc_end();
2589  if (I == Mod->ifunc_begin())
2590  return nullptr;
2591  return wrap(&*--I);
2592 }
2593 
2595  GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2597  if (++I == GIF->getParent()->ifunc_end())
2598  return nullptr;
2599  return wrap(&*I);
2600 }
2601 
2603  GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2605  if (I == GIF->getParent()->ifunc_begin())
2606  return nullptr;
2607  return wrap(&*--I);
2608 }
2609 
2611  return wrap(unwrap<GlobalIFunc>(IFunc)->getResolver());
2612 }
2613 
2615  unwrap<GlobalIFunc>(IFunc)->setResolver(unwrap<Constant>(Resolver));
2616 }
2617 
2619  unwrap<GlobalIFunc>(IFunc)->eraseFromParent();
2620 }
2621 
2623  unwrap<GlobalIFunc>(IFunc)->removeFromParent();
2624 }
2625 
2626 /*--.. Operations on basic blocks ..........................................--*/
2627 
2629  return wrap(static_cast<Value*>(unwrap(BB)));
2630 }
2631 
2633  return isa<BasicBlock>(unwrap(Val));
2634 }
2635 
2637  return wrap(unwrap<BasicBlock>(Val));
2638 }
2639 
2641  return unwrap(BB)->getName().data();
2642 }
2643 
2645  return wrap(unwrap(BB)->getParent());
2646 }
2647 
2649  return wrap(unwrap(BB)->getTerminator());
2650 }
2651 
2653  return unwrap<Function>(FnRef)->size();
2654 }
2655 
2656 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
2657  Function *Fn = unwrap<Function>(FnRef);
2658  for (BasicBlock &BB : *Fn)
2659  *BasicBlocksRefs++ = wrap(&BB);
2660 }
2661 
2663  return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2664 }
2665 
2667  Function *Func = unwrap<Function>(Fn);
2668  Function::iterator I = Func->begin();
2669  if (I == Func->end())
2670  return nullptr;
2671  return wrap(&*I);
2672 }
2673 
2675  Function *Func = unwrap<Function>(Fn);
2676  Function::iterator I = Func->end();
2677  if (I == Func->begin())
2678  return nullptr;
2679  return wrap(&*--I);
2680 }
2681 
2683  BasicBlock *Block = unwrap(BB);
2684  Function::iterator I(Block);
2685  if (++I == Block->getParent()->end())
2686  return nullptr;
2687  return wrap(&*I);
2688 }
2689 
2691  BasicBlock *Block = unwrap(BB);
2692  Function::iterator I(Block);
2693  if (I == Block->getParent()->begin())
2694  return nullptr;
2695  return wrap(&*--I);
2696 }
2697 
2699  const char *Name) {
2701 }
2702 
2705  BasicBlock *ToInsert = unwrap(BB);
2706  BasicBlock *CurBB = unwrap(Builder)->GetInsertBlock();
2707  assert(CurBB && "current insertion point is invalid!");
2708  CurBB->getParent()->getBasicBlockList().insertAfter(CurBB->getIterator(),
2709  ToInsert);
2710 }
2711 
2714  unwrap<Function>(Fn)->getBasicBlockList().push_back(unwrap(BB));
2715 }
2716 
2718  LLVMValueRef FnRef,
2719  const char *Name) {
2720  return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
2721 }
2722 
2725 }
2726 
2728  LLVMBasicBlockRef BBRef,
2729  const char *Name) {
2730  BasicBlock *BB = unwrap(BBRef);
2731  return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2732 }
2733 
2735  const char *Name) {
2737 }
2738 
2740  unwrap(BBRef)->eraseFromParent();
2741 }
2742 
2744  unwrap(BBRef)->removeFromParent();
2745 }
2746 
2748  unwrap(BB)->moveBefore(unwrap(MovePos));
2749 }
2750 
2752  unwrap(BB)->moveAfter(unwrap(MovePos));
2753 }
2754 
2755 /*--.. Operations on instructions ..........................................--*/
2756 
2758  return wrap(unwrap<Instruction>(Inst)->getParent());
2759 }
2760 
2762  BasicBlock *Block = unwrap(BB);
2763  BasicBlock::iterator I = Block->begin();
2764  if (I == Block->end())
2765  return nullptr;
2766  return wrap(&*I);
2767 }
2768 
2770  BasicBlock *Block = unwrap(BB);
2771  BasicBlock::iterator I = Block->end();
2772  if (I == Block->begin())
2773  return nullptr;
2774  return wrap(&*--I);
2775 }
2776 
2778  Instruction *Instr = unwrap<Instruction>(Inst);
2779  BasicBlock::iterator I(Instr);
2780  if (++I == Instr->getParent()->end())
2781  return nullptr;
2782  return wrap(&*I);
2783 }
2784 
2786  Instruction *Instr = unwrap<Instruction>(Inst);
2787  BasicBlock::iterator I(Instr);
2788  if (I == Instr->getParent()->begin())
2789  return nullptr;
2790  return wrap(&*--I);
2791 }
2792 
2794  unwrap<Instruction>(Inst)->removeFromParent();
2795 }
2796 
2798  unwrap<Instruction>(Inst)->eraseFromParent();
2799 }
2800 
2802  if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2803  return (LLVMIntPredicate)I->getPredicate();
2804  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2805  if (CE->getOpcode() == Instruction::ICmp)
2806  return (LLVMIntPredicate)CE->getPredicate();
2807  return (LLVMIntPredicate)0;
2808 }
2809 
2811  if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2812  return (LLVMRealPredicate)I->getPredicate();
2813  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2814  if (CE->getOpcode() == Instruction::FCmp)
2815  return (LLVMRealPredicate)CE->getPredicate();
2816  return (LLVMRealPredicate)0;
2817 }
2818 
2820  if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2821  return map_to_llvmopcode(C->getOpcode());
2822  return (LLVMOpcode)0;
2823 }
2824 
2826  if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2827  return wrap(C->clone());
2828  return nullptr;
2829 }
2830 
2832  Instruction *I = dyn_cast<Instruction>(unwrap(Inst));
2833  return (I && I->isTerminator()) ? wrap(I) : nullptr;
2834 }
2835 
2837  if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) {
2838  return FPI->getNumArgOperands();
2839  }
2840  return unwrap<CallBase>(Instr)->getNumArgOperands();
2841 }
2842 
2843 /*--.. Call and invoke instructions ........................................--*/
2844 
2846  return unwrap<CallBase>(Instr)->getCallingConv();
2847 }
2848 
2849 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
2850  return unwrap<CallBase>(Instr)->setCallingConv(
2851  static_cast<CallingConv::ID>(CC));
2852 }
2853 
2855  unsigned align) {
2856  auto *Call = unwrap<CallBase>(Instr);
2857  Attribute AlignAttr =
2858  Attribute::getWithAlignment(Call->getContext(), Align(align));
2859  Call->addAttribute(index, AlignAttr);
2860 }
2861 
2863  LLVMAttributeRef A) {
2864  unwrap<CallBase>(C)->addAttribute(Idx, unwrap(A));
2865 }
2866 
2868  LLVMAttributeIndex Idx) {
2869  auto *Call = unwrap<CallBase>(C);
2870  auto AS = Call->getAttributes().getAttributes(Idx);
2871  return AS.getNumAttributes();
2872 }
2873 
2876  auto *Call = unwrap<CallBase>(C);
2877  auto AS = Call->getAttributes().getAttributes(Idx);
2878  for (auto A : AS)
2879  *Attrs++ = wrap(A);
2880 }
2881 
2883  LLVMAttributeIndex Idx,
2884  unsigned KindID) {
2885  return wrap(
2886  unwrap<CallBase>(C)->getAttribute(Idx, (Attribute::AttrKind)KindID));
2887 }
2888 
2890  LLVMAttributeIndex Idx,
2891  const char *K, unsigned KLen) {
2892  return wrap(unwrap<CallBase>(C)->getAttribute(Idx, StringRef(K, KLen)));
2893 }
2894 
2896  unsigned KindID) {
2897  unwrap<CallBase>(C)->removeAttribute(Idx, (Attribute::AttrKind)KindID);
2898 }
2899 
2901  const char *K, unsigned KLen) {
2902  unwrap<CallBase>(C)->removeAttribute(Idx, StringRef(K, KLen));
2903 }
2904 
2906  return wrap(unwrap<CallBase>(Instr)->getCalledOperand());
2907 }
2908 
2910  return wrap(unwrap<CallBase>(Instr)->getFunctionType());
2911 }
2912 
2913 /*--.. Operations on call instructions (only) ..............................--*/
2914 
2916  return unwrap<CallInst>(Call)->isTailCall();
2917 }
2918 
2919 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
2920  unwrap<CallInst>(Call)->setTailCall(isTailCall);
2921 }
2922 
2923 /*--.. Operations on invoke instructions (only) ............................--*/
2924 
2926  return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2927 }
2928 
2930  if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2931  return wrap(CRI->getUnwindDest());
2932  } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2933  return wrap(CSI->getUnwindDest());
2934  }
2935  return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2936 }
2937 
2939  unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2940 }
2941 
2943  if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2944  return CRI->setUnwindDest(unwrap(B));
2945  } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2946  return CSI->setUnwindDest(unwrap(B));
2947  }
2948  unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2949 }
2950 
2951 /*--.. Operations on terminators ...........................................--*/
2952 
2954  return unwrap<Instruction>(Term)->getNumSuccessors();
2955 }
2956 
2958  return wrap(unwrap<Instruction>(Term)->getSuccessor(i));
2959 }
2960 
2962  return unwrap<Instruction>(Term)->setSuccessor(i, unwrap(block));
2963 }
2964 
2965 /*--.. Operations on branch instructions (only) ............................--*/
2966 
2968  return unwrap<BranchInst>(Branch)->isConditional();
2969 }
2970 
2972  return wrap(unwrap<BranchInst>(Branch)->getCondition());
2973 }
2974 
2976  return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2977 }
2978 
2979 /*--.. Operations on switch instructions (only) ............................--*/
2980 
2982  return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2983 }
2984 
2985 /*--.. Operations on alloca instructions (only) ............................--*/
2986 
2988  return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
2989 }
2990 
2991 /*--.. Operations on gep instructions (only) ...............................--*/
2992 
2994  return unwrap<GetElementPtrInst>(GEP)->isInBounds();
2995 }
2996 
2998  return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
2999 }
3000 
3001 /*--.. Operations on phi nodes .............................................--*/
3002 
3003 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
3004  LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
3005  PHINode *PhiVal = unwrap<PHINode>(PhiNode);
3006  for (unsigned I = 0; I != Count; ++I)
3007  PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
3008 }
3009 
3010 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
3011  return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
3012 }
3013 
3015  return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
3016 }
3017 
3019  return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
3020 }
3021 
3022 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/
3023 
3025  auto *I = unwrap(Inst);
3026  if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
3027  return GEP->getNumIndices();
3028  if (auto *EV = dyn_cast<ExtractValueInst>(I))
3029  return EV->getNumIndices();
3030  if (auto *IV = dyn_cast<InsertValueInst>(I))
3031  return IV->getNumIndices();
3032  if (auto *CE = dyn_cast<ConstantExpr>(I))
3033  return CE->getIndices().size();
3035  "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
3036 }
3037 
3038 const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
3039  auto *I = unwrap(Inst);
3040  if (auto *EV = dyn_cast<ExtractValueInst>(I))
3041  return EV->getIndices().data();
3042  if (auto *IV = dyn_cast<InsertValueInst>(I))
3043  return IV->getIndices().data();
3044  if (auto *CE = dyn_cast<ConstantExpr>(I))
3045  return CE->getIndices().data();
3047  "LLVMGetIndices applies only to extractvalue and insertvalue!");
3048 }
3049 
3050 
3051 /*===-- Instruction builders ----------------------------------------------===*/
3052 
3054  return wrap(new IRBuilder<>(*unwrap(C)));
3055 }
3056 
3059 }
3060 
3062  LLVMValueRef Instr) {
3063  BasicBlock *BB = unwrap(Block);
3064  auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
3065  unwrap(Builder)->SetInsertPoint(BB, I);
3066 }
3067 
3069  Instruction *I = unwrap<Instruction>(Instr);
3070  unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
3071 }
3072 
3074  BasicBlock *BB = unwrap(Block);
3075  unwrap(Builder)->SetInsertPoint(BB);
3076 }
3077 
3079  return wrap(unwrap(Builder)->GetInsertBlock());
3080 }
3081 
3083  unwrap(Builder)->ClearInsertionPoint();
3084 }
3085 
3087  unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
3088 }
3089 
3091  const char *Name) {
3092  unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
3093 }
3094 
3096  delete unwrap(Builder);
3097 }
3098 
3099 /*--.. Metadata builders ...................................................--*/
3100 
3102  return wrap(unwrap(Builder)->getCurrentDebugLocation().getAsMDNode());
3103 }
3104 
3106  if (Loc)
3107  unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(unwrap<MDNode>(Loc)));
3108  else
3109  unwrap(Builder)->SetCurrentDebugLocation(DebugLoc());
3110 }
3111 
3113  MDNode *Loc =
3114  L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
3115  unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
3116 }
3117 
3119  LLVMContext &Context = unwrap(Builder)->getContext();
3120  return wrap(MetadataAsValue::get(
3121  Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
3122 }
3123 
3125  unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
3126 }
3127 
3129  LLVMMetadataRef FPMathTag) {
3130 
3131  unwrap(Builder)->setDefaultFPMathTag(FPMathTag
3132  ? unwrap<MDNode>(FPMathTag)
3133  : nullptr);
3134 }
3135 
3137  return wrap(unwrap(Builder)->getDefaultFPMathTag());
3138 }
3139 
3140 /*--.. Instruction builders ................................................--*/
3141 
3143  return wrap(unwrap(B)->CreateRetVoid());
3144 }
3145 
3147  return wrap(unwrap(B)->CreateRet(unwrap(V)));
3148 }
3149 
3151  unsigned N) {
3152  return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
3153 }
3154 
3156  return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
3157 }
3158 
3161  return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
3162 }
3163 
3165  LLVMBasicBlockRef Else, unsigned NumCases) {
3166  return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
3167 }
3168 
3170  unsigned NumDests) {
3171  return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
3172 }
3173 
3175  LLVMValueRef *Args, unsigned NumArgs,
3177  const char *Name) {
3178  Value *V = unwrap(Fn);
3179  FunctionType *FnT =
3180  cast<FunctionType>(cast<PointerType>(V->getType())->getElementType());
3181 
3182  return wrap(
3183  unwrap(B)->CreateInvoke(FnT, unwrap(Fn), unwrap(Then), unwrap(Catch),
3184  makeArrayRef(unwrap(Args), NumArgs), Name));
3185 }
3186 
3188  LLVMValueRef *Args, unsigned NumArgs,
3190  const char *Name) {
3191  return wrap(unwrap(B)->CreateInvoke(
3192  unwrap<FunctionType>(Ty), unwrap(Fn), unwrap(Then), unwrap(Catch),
3193  makeArrayRef(unwrap(Args), NumArgs), Name));
3194 }
3195 
3197  LLVMValueRef PersFn, unsigned NumClauses,
3198  const char *Name) {
3199  // The personality used to live on the landingpad instruction, but now it
3200  // lives on the parent function. For compatibility, take the provided
3201  // personality and put it on the parent function.
3202  if (PersFn)
3203  unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
3204  cast<Function>(unwrap(PersFn)));
3205  return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
3206 }
3207 
3209  LLVMValueRef *Args, unsigned NumArgs,
3210  const char *Name) {
3211  return wrap(unwrap(B)->CreateCatchPad(unwrap(ParentPad),
3212  makeArrayRef(unwrap(Args), NumArgs),
3213  Name));
3214 }
3215 
3217  LLVMValueRef *Args, unsigned NumArgs,
3218  const char *Name) {
3219  if (ParentPad == nullptr) {
3220  Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3221  ParentPad = wrap(Constant::getNullValue(Ty));
3222  }
3223  return wrap(unwrap(B)->CreateCleanupPad(unwrap(ParentPad),
3224  makeArrayRef(unwrap(Args), NumArgs),
3225  Name));
3226 }
3227 
3229  return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
3230 }
3231 
3233  LLVMBasicBlockRef UnwindBB,
3234  unsigned NumHandlers, const char *Name) {
3235  if (ParentPad == nullptr) {
3236  Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3237  ParentPad = wrap(Constant::getNullValue(Ty));
3238  }
3239  return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(UnwindBB),
3240  NumHandlers, Name));
3241 }
3242 
3245  return wrap(unwrap(B)->CreateCatchRet(unwrap<CatchPadInst>(CatchPad),
3246  unwrap(BB)));
3247 }
3248 
3251  return wrap(unwrap(B)->CreateCleanupRet(unwrap<CleanupPadInst>(CatchPad),
3252  unwrap(BB)));
3253 }
3254 
3256  return wrap(unwrap(B)->CreateUnreachable());
3257 }
3258 
3260  LLVMBasicBlockRef Dest) {
3261  unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
3262 }
3263 
3265  unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
3266 }
3267 
3268 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
3269  return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
3270 }
3271 
3272 LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
3273  return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
3274 }
3275 
3277  unwrap<LandingPadInst>(LandingPad)->
3278  addClause(cast<Constant>(unwrap(ClauseVal)));
3279 }
3280 
3282  return unwrap<LandingPadInst>(LandingPad)->isCleanup();
3283 }
3284 
3285 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
3286  unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
3287 }
3288 
3290  unwrap<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Dest));
3291 }
3292 
3293 unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) {
3294  return unwrap<CatchSwitchInst>(CatchSwitch)->getNumHandlers();
3295 }
3296 
3297 void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) {
3298  CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(CatchSwitch);
3299  for (const BasicBlock *H : CSI->handlers())
3300  *Handlers++ = wrap(H);
3301 }
3302 
3304  return wrap(unwrap<CatchPadInst>(CatchPad)->getCatchSwitch());
3305 }
3306 
3308  unwrap<CatchPadInst>(CatchPad)
3309  ->setCatchSwitch(unwrap<CatchSwitchInst>(CatchSwitch));
3310 }
3311 
3312 /*--.. Funclets ...........................................................--*/
3313 
3315  return wrap(unwrap<FuncletPadInst>(Funclet)->getArgOperand(i));
3316 }
3317 
3318 void LLVMSetArgOperand(LLVMValueRef Funclet, unsigned i, LLVMValueRef value) {
3319  unwrap<FuncletPadInst>(Funclet)->setArgOperand(i, unwrap(value));
3320 }
3321 
3322 /*--.. Arithmetic ..........................................................--*/
3323 
3325  const char *Name) {
3326  return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
3327 }
3328 
3330  const char *Name) {
3331  return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
3332 }
3333 
3335  const char *Name) {
3336  return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
3337 }
3338 
3340  const char *Name) {
3341  return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
3342 }
3343 
3345  const char *Name) {
3346  return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
3347 }
3348 
3350  const char *Name) {
3351  return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
3352 }
3353 
3355  const char *Name) {
3356  return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
3357 }
3358 
3360  const char *Name) {
3361  return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
3362 }
3363 
3365  const char *Name) {
3366  return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
3367 }
3368 
3370  const char *Name) {
3371  return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
3372 }
3373 
3375  const char *Name) {
3376  return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
3377 }
3378 
3380  const char *Name) {
3381  return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
3382 }
3383 
3385  const char *Name) {
3386  return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
3387 }
3388 
3390  LLVMValueRef RHS, const char *Name) {
3391  return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
3392 }
3393 
3395  const char *Name) {
3396  return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
3397 }
3398 
3400  LLVMValueRef RHS, const char *Name) {
3401  return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
3402 }
3403 
3405  const char *Name) {
3406  return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
3407 }
3408 
3410  const char *Name) {
3411  return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
3412 }
3413 
3415  const char *Name) {
3416  return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
3417 }
3418 
3420  const char *Name) {
3421  return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
3422 }
3423 
3425  const char *Name) {
3426  return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
3427 }
3428 
3430  const char *Name) {
3431  return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
3432 }
3433 
3435  const char *Name) {
3436  return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
3437 }
3438 
3440  const char *Name) {
3441  return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
3442 }
3443 
3445  const char *Name) {
3446  return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
3447 }
3448 
3450  const char *Name) {
3451  return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
3452 }
3453 
3455  LLVMValueRef LHS, LLVMValueRef RHS,
3456  const char *Name) {
3457  return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
3458  unwrap(RHS), Name));
3459 }
3460 
3462  return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
3463 }
3464 
3466  const char *Name) {
3467  return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
3468 }
3469 
3471  const char *Name) {
3472  return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
3473 }
3474 
3476  return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
3477 }
3478 
3480  return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
3481 }
3482 
3483 /*--.. Memory ..............................................................--*/
3484 
3486  const char *Name) {
3487  Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3488  Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3489  AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3490  Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3491  ITy, unwrap(Ty), AllocSize,
3492  nullptr, nullptr, "");
3493  return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3494 }
3495 
3497  LLVMValueRef Val, const char *Name) {
3498  Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3499  Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3500  AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3501  Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3502  ITy, unwrap(Ty), AllocSize,
3503  unwrap(Val), nullptr, "");
3504  return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3505 }
3506 
3508  LLVMValueRef Val, LLVMValueRef Len,
3509  unsigned Align) {
3510  return wrap(unwrap(B)->CreateMemSet(unwrap(Ptr), unwrap(Val), unwrap(Len),
3511  MaybeAlign(Align)));
3512 }
3513 
3515  LLVMValueRef Dst, unsigned DstAlign,
3516  LLVMValueRef Src, unsigned SrcAlign,
3517  LLVMValueRef Size) {
3518  return wrap(unwrap(B)->CreateMemCpy(unwrap(Dst), MaybeAlign(DstAlign),
3519  unwrap(Src), MaybeAlign(SrcAlign),
3520  unwrap(Size)));
3521 }
3522 
3524  LLVMValueRef Dst, unsigned DstAlign,
3525  LLVMValueRef Src, unsigned SrcAlign,
3526  LLVMValueRef Size) {
3527  return wrap(unwrap(B)->CreateMemMove(unwrap(Dst), MaybeAlign(DstAlign),
3528  unwrap(Src), MaybeAlign(SrcAlign),
3529  unwrap(Size)));
3530 }
3531 
3533  const char *Name) {
3534  return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
3535 }
3536 
3538  LLVMValueRef Val, const char *Name) {
3539  return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
3540 }
3541 
3543  return wrap(unwrap(B)->Insert(
3544  CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
3545 }
3546 
3548  const char *Name) {
3549  Value *V = unwrap(PointerVal);
3550  PointerType *Ty = cast<PointerType>(V->getType());
3551 
3552  return wrap(unwrap(B)->CreateLoad(Ty->getElementType(), V, Name));
3553 }
3554 
3556  LLVMValueRef PointerVal, const char *Name) {
3557  return wrap(unwrap(B)->CreateLoad(unwrap(Ty), unwrap(PointerVal), Name));
3558 }
3559 
3561  LLVMValueRef PointerVal) {
3562  return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
3563 }
3564 
3566  switch (Ordering) {
3576  }
3577 
3578  llvm_unreachable("Invalid LLVMAtomicOrdering value!");
3579 }
3580 
3582  switch (Ordering) {
3592  }
3593 
3594  llvm_unreachable("Invalid AtomicOrdering value!");
3595 }
3596 
3598  switch (BinOp) {
3612  }
3613 
3614  llvm_unreachable("Invalid LLVMAtomicRMWBinOp value!");
3615 }
3616 
3618  switch (BinOp) {
3632  default: break;
3633  }
3634 
3635  llvm_unreachable("Invalid AtomicRMWBinOp value!");
3636 }
3637 
3638 // TODO: Should this and other atomic instructions support building with
3639 // "syncscope"?
3641  LLVMBool isSingleThread, const char *Name) {
3642  return wrap(
3643  unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
3644  isSingleThread ? SyncScope::SingleThread
3646  Name));
3647 }
3648 
3650  LLVMValueRef *Indices, unsigned NumIndices,
3651  const char *Name) {
3652  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3653  Value *Val = unwrap(Pointer);
3654  Type *Ty =
3655  cast<PointerType>(Val->getType()->getScalarType())->getElementType();
3656  return wrap(unwrap(B)->CreateGEP(Ty, Val, IdxList, Name));
3657 }
3658 
3660  LLVMValueRef Pointer, LLVMValueRef *Indices,
3661  unsigned NumIndices, const char *Name) {
3662  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3663  return wrap(unwrap(B)->CreateGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3664 }
3665 
3667  LLVMValueRef *Indices, unsigned NumIndices,
3668  const char *Name) {
3669  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3670  Value *Val = unwrap(Pointer);
3671  Type *Ty =
3672  cast<PointerType>(Val->getType()->getScalarType())->getElementType();
3673  return wrap(unwrap(B)->CreateInBoundsGEP(Ty, Val, IdxList, Name));
3674 }
3675 
3677  LLVMValueRef Pointer, LLVMValueRef *Indices,
3678  unsigned NumIndices, const char *Name) {
3679  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3680  return wrap(
3681  unwrap(B)->CreateInBoundsGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3682 }
3683 
3685  unsigned Idx, const char *Name) {
3686  Value *Val = unwrap(Pointer);
3687  Type *Ty =
3688  cast<PointerType>(Val->getType()->getScalarType())->getElementType();
3689  return wrap(unwrap(B)->CreateStructGEP(Ty, Val, Idx, Name));
3690 }
3691 
3693  LLVMValueRef Pointer, unsigned Idx,
3694  const char *Name) {
3695  return wrap(
3696  unwrap(B)->CreateStructGEP(unwrap(Ty), unwrap(Pointer), Idx, Name));
3697 }
3698 
3700  const char *Name) {
3701  return wrap(unwrap(B)->CreateGlobalString(Str, Name));
3702 }
3703 
3705  const char *Name) {
3706  return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
3707 }
3708 
3710  Value *P = unwrap<Value>(MemAccessInst);
3711  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3712  return LI->isVolatile();
3713  if (StoreInst *SI = dyn_cast<StoreInst>(P))
3714  return SI->isVolatile();
3715  if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
3716  return AI->isVolatile();
3717  return cast<AtomicCmpXchgInst>(P)->isVolatile();
3718 }
3719 
3720 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
3721  Value *P = unwrap<Value>(MemAccessInst);
3722  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3723  return LI->setVolatile(isVolatile);
3724  if (StoreInst *SI = dyn_cast<StoreInst>(P))
3725  return SI->setVolatile(isVolatile);
3726  if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
3727  return AI->setVolatile(isVolatile);
3728  return cast<AtomicCmpXchgInst>(P)->setVolatile(isVolatile);
3729 }
3730 
3732  return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->isWeak();
3733 }
3734 
3735 void LLVMSetWeak(LLVMValueRef CmpXchgInst, LLVMBool isWeak) {
3736  return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->setWeak(isWeak);
3737 }
3738 
3740  Value *P = unwrap<Value>(MemAccessInst);
3741  AtomicOrdering O;
3742  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3743  O = LI->getOrdering();
3744  else if (StoreInst *SI = dyn_cast<StoreInst>(P))
3745  O = SI->getOrdering();
3746  else
3747  O = cast<AtomicRMWInst>(P)->getOrdering();
3748  return mapToLLVMOrdering(O);
3749 }
3750 
3751 void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
3752  Value *P = unwrap<Value>(MemAccessInst);
3753  AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3754 
3755  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3756  return LI->setOrdering(O);
3757  return cast<StoreInst>(P)->setOrdering(O);
3758 }
3759 
3761  return mapToLLVMRMWBinOp(unwrap<AtomicRMWInst>(Inst)->getOperation());
3762 }
3763 
3765  unwrap<AtomicRMWInst>(Inst)->setOperation(mapFromLLVMRMWBinOp(BinOp));
3766 }
3767 
3768 /*--.. Casts ...............................................................--*/
3769 
3771  LLVMTypeRef DestTy, const char *Name) {
3772  return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
3773 }
3774 
3776  LLVMTypeRef DestTy, const char *Name) {
3777  return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
3778 }
3779 
3781  LLVMTypeRef DestTy, const char *Name) {
3782  return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
3783 }
3784 
3786  LLVMTypeRef DestTy, const char *Name) {
3787  return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
3788 }
3789 
3791  LLVMTypeRef DestTy, const char *Name) {
3792  return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
3793 }
3794 
3796  LLVMTypeRef DestTy, const char *Name) {
3797  return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
3798 }
3799 
3801  LLVMTypeRef DestTy, const char *Name) {
3802  return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
3803 }
3804 
3806  LLVMTypeRef DestTy, const char *Name) {
3807  return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
3808 }
3809 
3811  LLVMTypeRef DestTy, const char *Name) {
3812  return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
3813 }
3814 
3816  LLVMTypeRef DestTy, const char *Name) {
3817  return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
3818 }
3819 
3821  LLVMTypeRef DestTy, const char *Name) {
3822  return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
3823 }
3824 
3826  LLVMTypeRef DestTy, const char *Name) {
3827  return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
3828 }
3829 
3831  LLVMTypeRef DestTy, const char *Name) {
3832  return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
3833 }
3834 
3836  LLVMTypeRef DestTy, const char *Name) {
3837  return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
3838  Name));
3839 }
3840 
3842  LLVMTypeRef DestTy, const char *Name) {
3843  return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
3844  Name));
3845 }
3846 
3848  LLVMTypeRef DestTy, const char *Name) {
3849  return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
3850  Name));
3851 }
3852 
3854  LLVMTypeRef DestTy, const char *Name) {
3855  return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
3856  unwrap(DestTy), Name));
3857 }
3858 
3860  LLVMTypeRef DestTy, const char *Name) {
3861  return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
3862 }
3863 
3865  LLVMTypeRef DestTy, LLVMBool IsSigned,
3866  const char *Name) {
3867  return wrap(
3868  unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy), IsSigned, Name));
3869 }
3870 
3872  LLVMTypeRef DestTy, const char *Name) {
3873  return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
3874  /*isSigned*/true, Name));
3875 }
3876 
3878  LLVMTypeRef DestTy, const char *Name) {
3879  return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
3880 }
3881 
3882 /*--.. Comparisons .........................................................--*/
3883 
3885  LLVMValueRef LHS, LLVMValueRef RHS,
3886  const char *Name) {
3887  return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
3888