LLVM  16.0.0git
Core.cpp
Go to the documentation of this file.
1 //===-- Core.cpp ----------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the common infrastructure (including the C bindings)
10 // for libLLVMCore.a, which implements the LLVM intermediate representation.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm-c/Core.h"
15 #include "llvm/IR/Attributes.h"
16 #include "llvm/IR/BasicBlock.h"
17 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/DerivedTypes.h"
20 #include "llvm/IR/DiagnosticInfo.h"
22 #include "llvm/IR/GlobalAlias.h"
23 #include "llvm/IR/GlobalVariable.h"
24 #include "llvm/IR/IRBuilder.h"
25 #include "llvm/IR/InlineAsm.h"
26 #include "llvm/IR/IntrinsicInst.h"
27 #include "llvm/IR/LLVMContext.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/InitializePasses.h"
31 #include "llvm/PassRegistry.h"
32 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/Threading.h"
39 #include <cassert>
40 #include <cstdlib>
41 #include <cstring>
42 #include <system_error>
43 
44 using namespace llvm;
45 
46 #define DEBUG_TYPE "ir"
47 
54 }
55 
58 }
59 
60 void LLVMShutdown() {
61  llvm_shutdown();
62 }
63 
64 /*===-- Error handling ----------------------------------------------------===*/
65 
66 char *LLVMCreateMessage(const char *Message) {
67  return strdup(Message);
68 }
69 
70 void LLVMDisposeMessage(char *Message) {
71  free(Message);
72 }
73 
74 
75 /*===-- Operations on contexts --------------------------------------------===*/
76 
78  static LLVMContext GlobalContext;
79  return GlobalContext;
80 }
81 
83  return wrap(new LLVMContext());
84 }
85 
87 
89  LLVMDiagnosticHandler Handler,
90  void *DiagnosticContext) {
91  unwrap(C)->setDiagnosticHandlerCallBack(
93  Handler),
94  DiagnosticContext);
95 }
96 
98  return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>(
99  unwrap(C)->getDiagnosticHandlerCallBack());
100 }
101 
103  return unwrap(C)->getDiagnosticContext();
104 }
105 
107  void *OpaqueHandle) {
108  auto YieldCallback =
109  LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
110  unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
111 }
112 
114  return unwrap(C)->shouldDiscardValueNames();
115 }
116 
118  unwrap(C)->setDiscardValueNames(Discard);
119 }
120 
122  unwrap(C)->setOpaquePointers(OpaquePointers);
123 }
124 
126  delete unwrap(C);
127 }
128 
130  unsigned SLen) {
131  return unwrap(C)->getMDKindID(StringRef(Name, SLen));
132 }
133 
134 unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) {
136 }
137 
138 unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) {
140 }
141 
143  return Attribute::AttrKind::EndAttrKinds;
144 }
145 
147  uint64_t Val) {
148  auto &Ctx = *unwrap(C);
149  auto AttrKind = (Attribute::AttrKind)KindID;
150 
151  if (AttrKind == Attribute::AttrKind::ByVal) {
152  // After r362128, byval attributes need to have a type attribute. Provide a
153  // NULL one until a proper API is added for this.
154  return wrap(Attribute::getWithByValType(Ctx, nullptr));
155  }
156 
157  if (AttrKind == Attribute::AttrKind::StructRet) {
158  // Same as byval.
159  return wrap(Attribute::getWithStructRetType(Ctx, nullptr));
160  }
161 
162  return wrap(Attribute::get(Ctx, AttrKind, Val));
163 }
164 
166  return unwrap(A).getKindAsEnum();
167 }
168 
170  auto Attr = unwrap(A);
171  if (Attr.isEnumAttribute())
172  return 0;
173  return Attr.getValueAsInt();
174 }
175 
177  LLVMTypeRef type_ref) {
178  auto &Ctx = *unwrap(C);
179  auto AttrKind = (Attribute::AttrKind)KindID;
180  return wrap(Attribute::get(Ctx, AttrKind, unwrap(type_ref)));
181 }
182 
184  auto Attr = unwrap(A);
185  return wrap(Attr.getValueAsType());
186 }
187 
189  const char *K, unsigned KLength,
190  const char *V, unsigned VLength) {
191  return wrap(Attribute::get(*unwrap(C), StringRef(K, KLength),
192  StringRef(V, VLength)));
193 }
194 
196  unsigned *Length) {
197  auto S = unwrap(A).getKindAsString();
198  *Length = S.size();
199  return S.data();
200 }
201 
203  unsigned *Length) {
204  auto S = unwrap(A).getValueAsString();
205  *Length = S.size();
206  return S.data();
207 }
208 
210  auto Attr = unwrap(A);
211  return Attr.isEnumAttribute() || Attr.isIntAttribute();
212 }
213 
215  return unwrap(A).isStringAttribute();
216 }
217 
219  return unwrap(A).isTypeAttribute();
220 }
221 
223  std::string MsgStorage;
224  raw_string_ostream Stream(MsgStorage);
226 
227  unwrap(DI)->print(DP);
228  Stream.flush();
229 
230  return LLVMCreateMessage(MsgStorage.c_str());
231 }
232 
234  LLVMDiagnosticSeverity severity;
235 
236  switch(unwrap(DI)->getSeverity()) {
237  default:
238  severity = LLVMDSError;
239  break;
240  case DS_Warning:
241  severity = LLVMDSWarning;
242  break;
243  case DS_Remark:
244  severity = LLVMDSRemark;
245  break;
246  case DS_Note:
247  severity = LLVMDSNote;
248  break;
249  }
250 
251  return severity;
252 }
253 
254 /*===-- Operations on modules ---------------------------------------------===*/
255 
257  return wrap(new Module(ModuleID, getGlobalContext()));
258 }
259 
261  LLVMContextRef C) {
262  return wrap(new Module(ModuleID, *unwrap(C)));
263 }
264 
266  delete unwrap(M);
267 }
268 
269 const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) {
270  auto &Str = unwrap(M)->getModuleIdentifier();
271  *Len = Str.length();
272  return Str.c_str();
273 }
274 
275 void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) {
276  unwrap(M)->setModuleIdentifier(StringRef(Ident, Len));
277 }
278 
279 const char *LLVMGetSourceFileName(LLVMModuleRef M, size_t *Len) {
280  auto &Str = unwrap(M)->getSourceFileName();
281  *Len = Str.length();
282  return Str.c_str();
283 }
284 
285 void LLVMSetSourceFileName(LLVMModuleRef M, const char *Name, size_t Len) {
286  unwrap(M)->setSourceFileName(StringRef(Name, Len));
287 }
288 
289 /*--.. Data layout .........................................................--*/
291  return unwrap(M)->getDataLayoutStr().c_str();
292 }
293 
295  return LLVMGetDataLayoutStr(M);
296 }
297 
298 void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
299  unwrap(M)->setDataLayout(DataLayoutStr);
300 }
301 
302 /*--.. Target triple .......................................................--*/
303 const char * LLVMGetTarget(LLVMModuleRef M) {
304  return unwrap(M)->getTargetTriple().c_str();
305 }
306 
307 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
308  unwrap(M)->setTargetTriple(Triple);
309 }
310 
311 /*--.. Module flags ........................................................--*/
314  const char *Key;
315  size_t KeyLen;
317 };
318 
321  switch (Behavior) {
325  return Module::ModFlagBehavior::Warning;
327  return Module::ModFlagBehavior::Require;
329  return Module::ModFlagBehavior::Override;
331  return Module::ModFlagBehavior::Append;
333  return Module::ModFlagBehavior::AppendUnique;
334  }
335  llvm_unreachable("Unknown LLVMModuleFlagBehavior");
336 }
337 
340  switch (Behavior) {
343  case Module::ModFlagBehavior::Warning:
345  case Module::ModFlagBehavior::Require:
347  case Module::ModFlagBehavior::Override:
349  case Module::ModFlagBehavior::Append:
351  case Module::ModFlagBehavior::AppendUnique:
353  default:
354  llvm_unreachable("Unhandled Flag Behavior");
355  }
356 }
357 
360  unwrap(M)->getModuleFlagsMetadata(MFEs);
361 
362  LLVMOpaqueModuleFlagEntry *Result = static_cast<LLVMOpaqueModuleFlagEntry *>(
363  safe_malloc(MFEs.size() * sizeof(LLVMOpaqueModuleFlagEntry)));
364  for (unsigned i = 0; i < MFEs.size(); ++i) {
365  const auto &ModuleFlag = MFEs[i];
366  Result[i].Behavior = map_from_llvmModFlagBehavior(ModuleFlag.Behavior);
367  Result[i].Key = ModuleFlag.Key->getString().data();
368  Result[i].KeyLen = ModuleFlag.Key->getString().size();
369  Result[i].Metadata = wrap(ModuleFlag.Val);
370  }
371  *Len = MFEs.size();
372  return Result;
373 }
374 
376  free(Entries);
377 }
378 
381  unsigned Index) {
383  static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
384  return MFE.Behavior;
385 }
386 
388  unsigned Index, size_t *Len) {
390  static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
391  *Len = MFE.KeyLen;
392  return MFE.Key;
393 }
394 
396  unsigned Index) {
398  static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
399  return MFE.Metadata;
400 }
401 
403  const char *Key, size_t KeyLen) {
404  return wrap(unwrap(M)->getModuleFlag({Key, KeyLen}));
405 }
406 
408  const char *Key, size_t KeyLen,
409  LLVMMetadataRef Val) {
410  unwrap(M)->addModuleFlag(map_to_llvmModFlagBehavior(Behavior),
411  {Key, KeyLen}, unwrap(Val));
412 }
413 
414 /*--.. Printing modules ....................................................--*/
415 
417  unwrap(M)->print(errs(), nullptr,
418  /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
419 }
420 
422  char **ErrorMessage) {
423  std::error_code EC;
424  raw_fd_ostream dest(Filename, EC, sys::fs::OF_TextWithCRLF);
425  if (EC) {
426  *ErrorMessage = strdup(EC.message().c_str());
427  return true;
428  }
429 
430  unwrap(M)->print(dest, nullptr);
431 
432  dest.close();
433 
434  if (dest.has_error()) {
435  std::string E = "Error printing to file: " + dest.error().message();
436  *ErrorMessage = strdup(E.c_str());
437  return true;
438  }
439 
440  return false;
441 }
442 
444  std::string buf;
445  raw_string_ostream os(buf);
446 
447  unwrap(M)->print(os, nullptr);
448  os.flush();
449 
450  return strdup(buf.c_str());
451 }
452 
453 /*--.. Operations on inline assembler ......................................--*/
454 void LLVMSetModuleInlineAsm2(LLVMModuleRef M, const char *Asm, size_t Len) {
455  unwrap(M)->setModuleInlineAsm(StringRef(Asm, Len));
456 }
457 
459  unwrap(M)->setModuleInlineAsm(StringRef(Asm));
460 }
461 
462 void LLVMAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm, size_t Len) {
463  unwrap(M)->appendModuleInlineAsm(StringRef(Asm, Len));
464 }
465 
466 const char *LLVMGetModuleInlineAsm(LLVMModuleRef M, size_t *Len) {
467  auto &Str = unwrap(M)->getModuleInlineAsm();
468  *Len = Str.length();
469  return Str.c_str();
470 }
471 
473  size_t AsmStringSize, char *Constraints,
474  size_t ConstraintsSize, LLVMBool HasSideEffects,
475  LLVMBool IsAlignStack,
476  LLVMInlineAsmDialect Dialect, LLVMBool CanThrow) {
478  switch (Dialect) {
480  AD = InlineAsm::AD_ATT;
481  break;
483  AD = InlineAsm::AD_Intel;
484  break;
485  }
486  return wrap(InlineAsm::get(unwrap<FunctionType>(Ty),
487  StringRef(AsmString, AsmStringSize),
488  StringRef(Constraints, ConstraintsSize),
489  HasSideEffects, IsAlignStack, AD, CanThrow));
490 }
491 
492 /*--.. Operations on module contexts ......................................--*/
494  return wrap(&unwrap(M)->getContext());
495 }
496 
497 
498 /*===-- Operations on types -----------------------------------------------===*/
499 
500 /*--.. Operations on all types (mostly) ....................................--*/
501 
503  switch (unwrap(Ty)->getTypeID()) {
504  case Type::VoidTyID:
505  return LLVMVoidTypeKind;
506  case Type::HalfTyID:
507  return LLVMHalfTypeKind;
508  case Type::BFloatTyID:
509  return LLVMBFloatTypeKind;
510  case Type::FloatTyID:
511  return LLVMFloatTypeKind;
512  case Type::DoubleTyID:
513  return LLVMDoubleTypeKind;
514  case Type::X86_FP80TyID:
515  return LLVMX86_FP80TypeKind;
516  case Type::FP128TyID:
517  return LLVMFP128TypeKind;
518  case Type::PPC_FP128TyID:
519  return LLVMPPC_FP128TypeKind;
520  case Type::LabelTyID:
521  return LLVMLabelTypeKind;
522  case Type::MetadataTyID:
523  return LLVMMetadataTypeKind;
524  case Type::IntegerTyID:
525  return LLVMIntegerTypeKind;
526  case Type::FunctionTyID:
527  return LLVMFunctionTypeKind;
528  case Type::StructTyID:
529  return LLVMStructTypeKind;
530  case Type::ArrayTyID:
531  return LLVMArrayTypeKind;
532  case Type::PointerTyID:
533  return LLVMPointerTypeKind;
535  return LLVMVectorTypeKind;
536  case Type::X86_MMXTyID:
537  return LLVMX86_MMXTypeKind;
538  case Type::X86_AMXTyID:
539  return LLVMX86_AMXTypeKind;
540  case Type::TokenTyID:
541  return LLVMTokenTypeKind;
545  llvm_unreachable("Typed pointers are unsupported via the C API");
546  }
547  llvm_unreachable("Unhandled TypeID.");
548 }
549 
551 {
552  return unwrap(Ty)->isSized();
553 }
554 
556  return wrap(&unwrap(Ty)->getContext());
557 }
558 
560  return unwrap(Ty)->print(errs(), /*IsForDebug=*/true);
561 }
562 
564  std::string buf;
565  raw_string_ostream os(buf);
566 
567  if (unwrap(Ty))
568  unwrap(Ty)->print(os);
569  else
570  os << "Printing <null> Type";
571 
572  os.flush();
573 
574  return strdup(buf.c_str());
575 }
576 
577 /*--.. Operations on integer types .........................................--*/
578 
580  return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
581 }
583  return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
584 }
586  return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
587 }
589  return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
590 }
592  return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
593 }
596 }
598  return wrap(IntegerType::get(*unwrap(C), NumBits));
599 }
600 
603 }
606 }
609 }
612 }
615 }
618 }
619 LLVMTypeRef LLVMIntType(unsigned NumBits) {
620  return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
621 }
622 
623 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
624  return unwrap<IntegerType>(IntegerTy)->getBitWidth();
625 }
626 
627 /*--.. Operations on real types ............................................--*/
628 
630  return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
631 }
634 }
636  return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
637 }
640 }
643 }
645  return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
646 }
649 }
652 }
655 }
656 
659 }
662 }
665 }
668 }
671 }
674 }
677 }
680 }
683 }
684 
685 /*--.. Operations on function types ........................................--*/
686 
688  LLVMTypeRef *ParamTypes, unsigned ParamCount,
689  LLVMBool IsVarArg) {
690  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
691  return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
692 }
693 
695  return unwrap<FunctionType>(FunctionTy)->isVarArg();
696 }
697 
699  return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
700 }
701 
702 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
703  return unwrap<FunctionType>(FunctionTy)->getNumParams();
704 }
705 
706 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
707  FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
708  for (Type *T : Ty->params())
709  *Dest++ = wrap(T);
710 }
711 
712 /*--.. Operations on struct types ..........................................--*/
713 
715  unsigned ElementCount, LLVMBool Packed) {
716  ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
717  return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
718 }
719 
721  unsigned ElementCount, LLVMBool Packed) {
722  return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
723  ElementCount, Packed);
724 }
725 
727 {
728  return wrap(StructType::create(*unwrap(C), Name));
729 }
730 
732 {
733  StructType *Type = unwrap<StructType>(Ty);
734  if (!Type->hasName())
735  return nullptr;
736  return Type->getName().data();
737 }
738 
739 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
740  unsigned ElementCount, LLVMBool Packed) {
741  ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
742  unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
743 }
744 
746  return unwrap<StructType>(StructTy)->getNumElements();
747 }
748 
750  StructType *Ty = unwrap<StructType>(StructTy);
751  for (Type *T : Ty->elements())
752  *Dest++ = wrap(T);
753 }
754 
756  StructType *Ty = unwrap<StructType>(StructTy);
757  return wrap(Ty->getTypeAtIndex(i));
758 }
759 
761  return unwrap<StructType>(StructTy)->isPacked();
762 }
763 
765  return unwrap<StructType>(StructTy)->isOpaque();
766 }
767 
769  return unwrap<StructType>(StructTy)->isLiteral();
770 }
771 
773  return wrap(StructType::getTypeByName(unwrap(M)->getContext(), Name));
774 }
775 
778 }
779 
780 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
781 
783  int i = 0;
784  for (auto *T : unwrap(Tp)->subtypes()) {
785  Arr[i] = wrap(T);
786  i++;
787  }
788 }
789 
791  return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
792 }
793 
795  return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
796 }
797 
799  return unwrap(Ty)->isOpaquePointerTy();
800 }
801 
803  return wrap(FixedVectorType::get(unwrap(ElementType), ElementCount));
804 }
805 
807  unsigned ElementCount) {
808  return wrap(ScalableVectorType::get(unwrap(ElementType), ElementCount));
809 }
810 
812  auto *Ty = unwrap<Type>(WrappedTy);
813  if (auto *PTy = dyn_cast<PointerType>(Ty))
814  return wrap(PTy->getNonOpaquePointerElementType());
815  if (auto *ATy = dyn_cast<ArrayType>(Ty))
816  return wrap(ATy->getElementType());
817  return wrap(cast<VectorType>(Ty)->getElementType());
818 }
819 
821  return unwrap(Tp)->getNumContainedTypes();
822 }
823 
824 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
825  return unwrap<ArrayType>(ArrayTy)->getNumElements();
826 }
827 
829  return unwrap<PointerType>(PointerTy)->getAddressSpace();
830 }
831 
832 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
833  return unwrap<VectorType>(VectorTy)->getElementCount().getKnownMinValue();
834 }
835 
836 /*--.. Operations on other types ...........................................--*/
837 
840 }
841 
843  return wrap(Type::getVoidTy(*unwrap(C)));
844 }
846  return wrap(Type::getLabelTy(*unwrap(C)));
847 }
849  return wrap(Type::getTokenTy(*unwrap(C)));
850 }
852  return wrap(Type::getMetadataTy(*unwrap(C)));
853 }
854 
857 }
860 }
861 
862 /*===-- Operations on values ----------------------------------------------===*/
863 
864 /*--.. Operations on all values ............................................--*/
865 
867  return wrap(unwrap(Val)->getType());
868 }
869 
871  switch(unwrap(Val)->getValueID()) {
872 #define LLVM_C_API 1
873 #define HANDLE_VALUE(Name) \
874  case Value::Name##Val: \
875  return LLVM##Name##ValueKind;
876 #include "llvm/IR/Value.def"
877  default:
879  }
880 }
881 
882 const char *LLVMGetValueName2(LLVMValueRef Val, size_t *Length) {
883  auto *V = unwrap(Val);
884  *Length = V->getName().size();
885  return V->getName().data();
886 }
887 
888 void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen) {
889  unwrap(Val)->setName(StringRef(Name, NameLen));
890 }
891 
892 const char *LLVMGetValueName(LLVMValueRef Val) {
893  return unwrap(Val)->getName().data();
894 }
895 
896 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
897  unwrap(Val)->setName(Name);
898 }
899 
901  unwrap(Val)->print(errs(), /*IsForDebug=*/true);
902 }
903 
905  std::string buf;
906  raw_string_ostream os(buf);
907 
908  if (unwrap(Val))
909  unwrap(Val)->print(os);
910  else
911  os << "Printing <null> Value";
912 
913  os.flush();
914 
915  return strdup(buf.c_str());
916 }
917 
919  unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
920 }
921 
923  return unwrap<Instruction>(Inst)->hasMetadata();
924 }
925 
926 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
927  auto *I = unwrap<Instruction>(Inst);
928  assert(I && "Expected instruction");
929  if (auto *MD = I->getMetadata(KindID))
930  return wrap(MetadataAsValue::get(I->getContext(), MD));
931  return nullptr;
932 }
933 
934 // MetadataAsValue uses a canonical format which strips the actual MDNode for
935 // MDNode with just a single constant value, storing just a ConstantAsMetadata
936 // This undoes this canonicalization, reconstructing the MDNode.
938  Metadata *MD = MAV->getMetadata();
939  assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
940  "Expected a metadata node or a canonicalized constant");
941 
942  if (MDNode *N = dyn_cast<MDNode>(MD))
943  return N;
944 
945  return MDNode::get(MAV->getContext(), MD);
946 }
947 
948 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
949  MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
950 
951  unwrap<Instruction>(Inst)->setMetadata(KindID, N);
952 }
953 
955  unsigned Kind;
957 };
958 
960 static LLVMValueMetadataEntry *
961 llvm_getMetadata(size_t *NumEntries,
962  llvm::function_ref<void(MetadataEntries &)> AccessMD) {
964  AccessMD(MVEs);
965 
967  static_cast<LLVMOpaqueValueMetadataEntry *>(
968  safe_malloc(MVEs.size() * sizeof(LLVMOpaqueValueMetadataEntry)));
969  for (unsigned i = 0; i < MVEs.size(); ++i) {
970  const auto &ModuleFlag = MVEs[i];
971  Result[i].Kind = ModuleFlag.first;
972  Result[i].Metadata = wrap(ModuleFlag.second);
973  }
974  *NumEntries = MVEs.size();
975  return Result;
976 }
977 
980  size_t *NumEntries) {
981  return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
982  Entries.clear();
983  unwrap<Instruction>(Value)->getAllMetadata(Entries);
984  });
985 }
986 
987 /*--.. Conversion functions ................................................--*/
988 
989 #define LLVM_DEFINE_VALUE_CAST(name) \
990  LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
991  return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
992  }
993 
995 
997  if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
998  if (isa<MDNode>(MD->getMetadata()) ||
999  isa<ValueAsMetadata>(MD->getMetadata()))
1000  return Val;
1001  return nullptr;
1002 }
1003 
1005  if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
1006  if (isa<MDString>(MD->getMetadata()))
1007  return Val;
1008  return nullptr;
1009 }
1010 
1011 /*--.. Operations on Uses ..................................................--*/
1013  Value *V = unwrap(Val);
1015  if (I == V->use_end())
1016  return nullptr;
1017  return wrap(&*I);
1018 }
1019 
1021  Use *Next = unwrap(U)->getNext();
1022  if (Next)
1023  return wrap(Next);
1024  return nullptr;
1025 }
1026 
1028  return wrap(unwrap(U)->getUser());
1029 }
1030 
1032  return wrap(unwrap(U)->get());
1033 }
1034 
1035 /*--.. Operations on Users .................................................--*/
1036 
1038  unsigned Index) {
1039  Metadata *Op = N->getOperand(Index);
1040  if (!Op)
1041  return nullptr;
1042  if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
1043  return wrap(C->getValue());
1044  return wrap(MetadataAsValue::get(Context, Op));
1045 }
1046 
1048  Value *V = unwrap(Val);
1049  if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
1050  if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1051  assert(Index == 0 && "Function-local metadata can only have one operand");
1052  return wrap(L->getValue());
1053  }
1054  return getMDNodeOperandImpl(V->getContext(),
1055  cast<MDNode>(MD->getMetadata()), Index);
1056  }
1057 
1058  return wrap(cast<User>(V)->getOperand(Index));
1059 }
1060 
1062  Value *V = unwrap(Val);
1063  return wrap(&cast<User>(V)->getOperandUse(Index));
1064 }
1065 
1067  unwrap<User>(Val)->setOperand(Index, unwrap(Op));
1068 }
1069 
1071  Value *V = unwrap(Val);
1072  if (isa<MetadataAsValue>(V))
1073  return LLVMGetMDNodeNumOperands(Val);
1074 
1075  return cast<User>(V)->getNumOperands();
1076 }
1077 
1078 /*--.. Operations on constants of any type .................................--*/
1079 
1081  return wrap(Constant::getNullValue(unwrap(Ty)));
1082 }
1083 
1085  return wrap(Constant::getAllOnesValue(unwrap(Ty)));
1086 }
1087 
1089  return wrap(UndefValue::get(unwrap(Ty)));
1090 }
1091 
1093  return wrap(PoisonValue::get(unwrap(Ty)));
1094 }
1095 
1097  return isa<Constant>(unwrap(Ty));
1098 }
1099 
1101  if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
1102  return C->isNullValue();
1103  return false;
1104 }
1105 
1107  return isa<UndefValue>(unwrap(Val));
1108 }
1109 
1111  return isa<PoisonValue>(unwrap(Val));
1112 }
1113 
1115  return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
1116 }
1117 
1118 /*--.. Operations on metadata nodes ........................................--*/
1119 
1121  size_t SLen) {
1122  return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
1123 }
1124 
1126  size_t Count) {
1127  return wrap(MDNode::get(*unwrap(C), ArrayRef<Metadata*>(unwrap(MDs), Count)));
1128 }
1129 
1131  unsigned SLen) {
1132  LLVMContext &Context = *unwrap(C);
1133  return wrap(MetadataAsValue::get(
1134  Context, MDString::get(Context, StringRef(Str, SLen))));
1135 }
1136 
1137 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
1138  return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
1139 }
1140 
1142  unsigned Count) {
1143  LLVMContext &Context = *unwrap(C);
1145  for (auto *OV : makeArrayRef(Vals, Count)) {
1146  Value *V = unwrap(OV);
1147  Metadata *MD;
1148  if (!V)
1149  MD = nullptr;
1150  else if (auto *C = dyn_cast<Constant>(V))
1151  MD = ConstantAsMetadata::get(C);
1152  else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
1153  MD = MDV->getMetadata();
1154  assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
1155  "outside of direct argument to call");
1156  } else {
1157  // This is function-local metadata. Pretend to make an MDNode.
1158  assert(Count == 1 &&
1159  "Expected only one operand to function-local metadata");
1161  }
1162 
1163  MDs.push_back(MD);
1164  }
1166 }
1167 
1168 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
1169  return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
1170 }
1171 
1173  return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
1174 }
1175 
1177  auto *V = unwrap(Val);
1178  if (auto *C = dyn_cast<Constant>(V))
1179  return wrap(ConstantAsMetadata::get(C));
1180  if (auto *MAV = dyn_cast<MetadataAsValue>(V))
1181  return wrap(MAV->getMetadata());
1182  return wrap(ValueAsMetadata::get(V));
1183 }
1184 
1185 const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
1186  if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
1187  if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
1188  *Length = S->getString().size();
1189  return S->getString().data();
1190  }
1191  *Length = 0;
1192  return nullptr;
1193 }
1194 
1196  auto *MD = cast<MetadataAsValue>(unwrap(V));
1197  if (isa<ValueAsMetadata>(MD->getMetadata()))
1198  return 1;
1199  return cast<MDNode>(MD->getMetadata())->getNumOperands();
1200 }
1201 
1203  Module *Mod = unwrap(M);
1205  if (I == Mod->named_metadata_end())
1206  return nullptr;
1207  return wrap(&*I);
1208 }
1209 
1211  Module *Mod = unwrap(M);
1213  if (I == Mod->named_metadata_begin())
1214  return nullptr;
1215  return wrap(&*--I);
1216 }
1217 
1219  NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD);
1221  if (++I == NamedNode->getParent()->named_metadata_end())
1222  return nullptr;
1223  return wrap(&*I);
1224 }
1225 
1227  NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD);
1229  if (I == NamedNode->getParent()->named_metadata_begin())
1230  return nullptr;
1231  return wrap(&*--I);
1232 }
1233 
1235  const char *Name, size_t NameLen) {
1236  return wrap(unwrap(M)->getNamedMetadata(StringRef(Name, NameLen)));
1237 }
1238 
1240  const char *Name, size_t NameLen) {
1241  return wrap(unwrap(M)->getOrInsertNamedMetadata({Name, NameLen}));
1242 }
1243 
1244 const char *LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NMD, size_t *NameLen) {
1245  NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD);
1246  *NameLen = NamedNode->getName().size();
1247  return NamedNode->getName().data();
1248 }
1249 
1251  auto *MD = cast<MetadataAsValue>(unwrap(V));
1252  if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1253  *Dest = wrap(MDV->getValue());
1254  return;
1255  }
1256  const auto *N = cast<MDNode>(MD->getMetadata());
1257  const unsigned numOperands = N->getNumOperands();
1258  LLVMContext &Context = unwrap(V)->getContext();
1259  for (unsigned i = 0; i < numOperands; i++)
1260  Dest[i] = getMDNodeOperandImpl(Context, N, i);
1261 }
1262 
1264  if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
1265  return N->getNumOperands();
1266  }
1267  return 0;
1268 }
1269 
1271  LLVMValueRef *Dest) {
1272  NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
1273  if (!N)
1274  return;
1275  LLVMContext &Context = unwrap(M)->getContext();
1276  for (unsigned i=0;i<N->getNumOperands();i++)
1277  Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
1278 }
1279 
1281  LLVMValueRef Val) {
1282  NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
1283  if (!N)
1284  return;
1285  if (!Val)
1286  return;
1287  N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
1288 }
1289 
1290 const char *LLVMGetDebugLocDirectory(LLVMValueRef Val, unsigned *Length) {
1291  if (!Length) return nullptr;
1292  StringRef S;
1293  if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1294  if (const auto &DL = I->getDebugLoc()) {
1295  S = DL->getDirectory();
1296  }
1297  } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1299  GV->getDebugInfo(GVEs);
1300  if (GVEs.size())
1301  if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1302  S = DGV->getDirectory();
1303  } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1304  if (const DISubprogram *DSP = F->getSubprogram())
1305  S = DSP->getDirectory();
1306  } else {
1307  assert(0 && "Expected Instruction, GlobalVariable or Function");
1308  return nullptr;
1309  }
1310  *Length = S.size();
1311  return S.data();
1312 }
1313 
1314 const char *LLVMGetDebugLocFilename(LLVMValueRef Val, unsigned *Length) {
1315  if (!Length) return nullptr;
1316  StringRef S;
1317  if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1318  if (const auto &DL = I->getDebugLoc()) {
1319  S = DL->getFilename();
1320  }
1321  } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1323  GV->getDebugInfo(GVEs);
1324  if (GVEs.size())
1325  if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1326  S = DGV->getFilename();
1327  } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1328  if (const DISubprogram *DSP = F->getSubprogram())
1329  S = DSP->getFilename();
1330  } else {
1331  assert(0 && "Expected Instruction, GlobalVariable or Function");
1332  return nullptr;
1333  }
1334  *Length = S.size();
1335  return S.data();
1336 }
1337 
1339  unsigned L = 0;
1340  if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1341  if (const auto &DL = I->getDebugLoc()) {
1342  L = DL->getLine();
1343  }
1344  } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1346  GV->getDebugInfo(GVEs);
1347  if (GVEs.size())
1348  if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1349  L = DGV->getLine();
1350  } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1351  if (const DISubprogram *DSP = F->getSubprogram())
1352  L = DSP->getLine();
1353  } else {
1354  assert(0 && "Expected Instruction, GlobalVariable or Function");
1355  return -1;
1356  }
1357  return L;
1358 }
1359 
1361  unsigned C = 0;
1362  if (const auto *I = dyn_cast<Instruction>(unwrap(Val)))
1363  if (const auto &DL = I->getDebugLoc())
1364  C = DL->getColumn();
1365  return C;
1366 }
1367 
1368 /*--.. Operations on scalar constants ......................................--*/
1369 
1370 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
1371  LLVMBool SignExtend) {
1372  return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
1373 }
1374 
1376  unsigned NumWords,
1377  const uint64_t Words[]) {
1378  IntegerType *Ty = unwrap<IntegerType>(IntTy);
1379  return wrap(ConstantInt::get(Ty->getContext(),
1380  APInt(Ty->getBitWidth(),
1381  makeArrayRef(Words, NumWords))));
1382 }
1383 
1385  uint8_t Radix) {
1386  return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
1387  Radix));
1388 }
1389 
1391  unsigned SLen, uint8_t Radix) {
1392  return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
1393  Radix));
1394 }
1395 
1397  return wrap(ConstantFP::get(unwrap(RealTy), N));
1398 }
1399 
1401  return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
1402 }
1403 
1405  unsigned SLen) {
1406  return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
1407 }
1408 
1409 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
1410  return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
1411 }
1412 
1413 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
1414  return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
1415 }
1416 
1417 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
1418  ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
1419  Type *Ty = cFP->getType();
1420 
1421  if (Ty->isHalfTy() || Ty->isBFloatTy() || Ty->isFloatTy() ||
1422  Ty->isDoubleTy()) {
1423  *LosesInfo = false;
1424  return cFP->getValueAPF().convertToDouble();
1425  }
1426 
1427  bool APFLosesInfo;
1428  APFloat APF = cFP->getValueAPF();
1430  *LosesInfo = APFLosesInfo;
1431  return APF.convertToDouble();
1432 }
1433 
1434 /*--.. Operations on composite constants ...................................--*/
1435 
1437  unsigned Length,
1438  LLVMBool DontNullTerminate) {
1439  /* Inverted the sense of AddNull because ', 0)' is a
1440  better mnemonic for null termination than ', 1)'. */
1441  return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
1442  DontNullTerminate == 0));
1443 }
1444 
1445 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
1446  LLVMBool DontNullTerminate) {
1447  return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
1448  DontNullTerminate);
1449 }
1450 
1452  return wrap(unwrap<Constant>(C)->getAggregateElement(Idx));
1453 }
1454 
1456  return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
1457 }
1458 
1460  return unwrap<ConstantDataSequential>(C)->isString();
1461 }
1462 
1463 const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1464  StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1465  *Length = Str.size();
1466  return Str.data();
1467 }
1468 
1470  LLVMValueRef *ConstantVals, unsigned Length) {
1471  ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1472  return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1473 }
1474 
1476  LLVMValueRef *ConstantVals,
1477  unsigned Count, LLVMBool Packed) {
1478  Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1479  return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
1480  Packed != 0));
1481 }
1482 
1483 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
1484  LLVMBool Packed) {
1485  return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1486  Packed);
1487 }
1488 
1490  LLVMValueRef *ConstantVals,
1491  unsigned Count) {
1492  Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1493  StructType *Ty = cast<StructType>(unwrap(StructTy));
1494 
1495  return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
1496 }
1497 
1498 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
1500  unwrap<Constant>(ScalarConstantVals, Size), Size)));
1501 }
1502 
1503 /*-- Opcode mapping */
1504 
1505 static LLVMOpcode map_to_llvmopcode(int opcode)
1506 {
1507  switch (opcode) {
1508  default: llvm_unreachable("Unhandled Opcode.");
1509 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
1510 #include "llvm/IR/Instruction.def"
1511 #undef HANDLE_INST
1512  }
1513 }
1514 
1516 {
1517  switch (code) {
1518 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
1519 #include "llvm/IR/Instruction.def"
1520 #undef HANDLE_INST
1521  }
1522  llvm_unreachable("Unhandled Opcode.");
1523 }
1524 
1525 /*--.. Constant expressions ................................................--*/
1526 
1528  return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
1529 }
1530 
1532  return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
1533 }
1534 
1536  return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
1537 }
1538 
1540  return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1541 }
1542 
1544  return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
1545 }
1546 
1548  return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
1549 }
1550 
1551 
1553  return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
1554 }
1555 
1557  return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
1558  unwrap<Constant>(RHSConstant)));
1559 }
1560 
1562  LLVMValueRef RHSConstant) {
1563  return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
1564  unwrap<Constant>(RHSConstant)));
1565 }
1566 
1568  LLVMValueRef RHSConstant) {
1569  return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
1570  unwrap<Constant>(RHSConstant)));
1571 }
1572 
1574  return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
1575  unwrap<Constant>(RHSConstant)));
1576 }
1577 
1579  LLVMValueRef RHSConstant) {
1580  return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
1581  unwrap<Constant>(RHSConstant)));
1582 }
1583 
1585  LLVMValueRef RHSConstant) {
1586  return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
1587  unwrap<Constant>(RHSConstant)));
1588 }
1589 
1591  return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
1592  unwrap<Constant>(RHSConstant)));
1593 }
1594 
1596  LLVMValueRef RHSConstant) {
1597  return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
1598  unwrap<Constant>(RHSConstant)));
1599 }
1600 
1602  LLVMValueRef RHSConstant) {
1603  return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
1604  unwrap<Constant>(RHSConstant)));
1605 }
1606 
1608  return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
1609  unwrap<Constant>(RHSConstant)));
1610 }
1611 
1613  return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
1614  unwrap<Constant>(RHSConstant)));
1615 }
1616 
1618  return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
1619  unwrap<Constant>(RHSConstant)));
1620 }
1621 
1623  LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1625  unwrap<Constant>(LHSConstant),
1626  unwrap<Constant>(RHSConstant)));
1627 }
1628 
1630  LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1632  unwrap<Constant>(LHSConstant),
1633  unwrap<Constant>(RHSConstant)));
1634 }
1635 
1637  return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1638  unwrap<Constant>(RHSConstant)));
1639 }
1640 
1642  return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
1643  unwrap<Constant>(RHSConstant)));
1644 }
1645 
1647  return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1648  unwrap<Constant>(RHSConstant)));
1649 }
1650 
1652  LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1653  ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1654  NumIndices);
1655  Constant *Val = unwrap<Constant>(ConstantVal);
1657  return wrap(ConstantExpr::getGetElementPtr(Ty, Val, IdxList));
1658 }
1659 
1661  LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1662  ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1663  NumIndices);
1664  Constant *Val = unwrap<Constant>(ConstantVal);
1665  return wrap(ConstantExpr::getGetElementPtr(unwrap(Ty), Val, IdxList));
1666 }
1667 
1669  LLVMValueRef *ConstantIndices,
1670  unsigned NumIndices) {
1671  ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1672  NumIndices);
1673  Constant *Val = unwrap<Constant>(ConstantVal);
1675  return wrap(ConstantExpr::getInBoundsGetElementPtr(Ty, Val, IdxList));
1676 }
1677 
1679  LLVMValueRef *ConstantIndices,
1680  unsigned NumIndices) {
1681  ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1682  NumIndices);
1683  Constant *Val = unwrap<Constant>(ConstantVal);
1684  return wrap(ConstantExpr::getInBoundsGetElementPtr(unwrap(Ty), Val, IdxList));
1685 }
1686 
1688  return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1689  unwrap(ToType)));
1690 }
1691 
1693  return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1694  unwrap(ToType)));
1695 }
1696 
1698  return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1699  unwrap(ToType)));
1700 }
1701 
1703  return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1704  unwrap(ToType)));
1705 }
1706 
1708  return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1709  unwrap(ToType)));
1710 }
1711 
1713  return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1714  unwrap(ToType)));
1715 }
1716 
1718  return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1719  unwrap(ToType)));
1720 }
1721 
1723  return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1724  unwrap(ToType)));
1725 }
1726 
1728  return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1729  unwrap(ToType)));
1730 }
1731 
1733  return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1734  unwrap(ToType)));
1735 }
1736 
1738  return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1739  unwrap(ToType)));
1740 }
1741 
1743  return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1744  unwrap(ToType)));
1745 }
1746 
1748  LLVMTypeRef ToType) {
1749  return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1750  unwrap(ToType)));
1751 }
1752 
1754  LLVMTypeRef ToType) {
1755  return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1756  unwrap(ToType)));
1757 }
1758 
1760  LLVMTypeRef ToType) {
1761  return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1762  unwrap(ToType)));
1763 }
1764 
1766  LLVMTypeRef ToType) {
1767  return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1768  unwrap(ToType)));
1769 }
1770 
1772  LLVMTypeRef ToType) {
1773  return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1774  unwrap(ToType)));
1775 }
1776 
1778  LLVMBool isSigned) {
1779  return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1780  unwrap(ToType), isSigned));
1781 }
1782 
1784  return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1785  unwrap(ToType)));
1786 }
1787 
1789  LLVMValueRef ConstantIfTrue,
1790  LLVMValueRef ConstantIfFalse) {
1791  return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1792  unwrap<Constant>(ConstantIfTrue),
1793  unwrap<Constant>(ConstantIfFalse)));
1794 }
1795 
1797  LLVMValueRef IndexConstant) {
1798  return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1799  unwrap<Constant>(IndexConstant)));
1800 }
1801 
1803  LLVMValueRef ElementValueConstant,
1804  LLVMValueRef IndexConstant) {
1805  return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1806  unwrap<Constant>(ElementValueConstant),
1807  unwrap<Constant>(IndexConstant)));
1808 }
1809 
1811  LLVMValueRef VectorBConstant,
1812  LLVMValueRef MaskConstant) {
1813  SmallVector<int, 16> IntMask;
1814  ShuffleVectorInst::getShuffleMask(unwrap<Constant>(MaskConstant), IntMask);
1815  return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1816  unwrap<Constant>(VectorBConstant),
1817  IntMask));
1818 }
1819 
1820 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1821  const char *Constraints,
1822  LLVMBool HasSideEffects,
1823  LLVMBool IsAlignStack) {
1824  return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1825  Constraints, HasSideEffects, IsAlignStack));
1826 }
1827 
1829  return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1830 }
1831 
1832 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1833 
1835  return wrap(unwrap<GlobalValue>(Global)->getParent());
1836 }
1837 
1839  return unwrap<GlobalValue>(Global)->isDeclaration();
1840 }
1841 
1843  switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1845  return LLVMExternalLinkage;
1849  return LLVMLinkOnceAnyLinkage;
1851  return LLVMLinkOnceODRLinkage;
1853  return LLVMWeakAnyLinkage;
1855  return LLVMWeakODRLinkage;
1857  return LLVMAppendingLinkage;
1859  return LLVMInternalLinkage;
1861  return LLVMPrivateLinkage;
1863  return LLVMExternalWeakLinkage;
1865  return LLVMCommonLinkage;
1866  }
1867 
1868  llvm_unreachable("Invalid GlobalValue linkage!");
1869 }
1870 
1872  GlobalValue *GV = unwrap<GlobalValue>(Global);
1873 
1874  switch (Linkage) {
1875  case LLVMExternalLinkage:
1877  break;
1880  break;
1883  break;
1886  break;
1888  LLVM_DEBUG(
1889  errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1890  "longer supported.");
1891  break;
1892  case LLVMWeakAnyLinkage:
1894  break;
1895  case LLVMWeakODRLinkage:
1897  break;
1898  case LLVMAppendingLinkage:
1900  break;
1901  case LLVMInternalLinkage:
1903  break;
1904  case LLVMPrivateLinkage:
1906  break;
1909  break;
1912  break;
1913  case LLVMDLLImportLinkage:
1914  LLVM_DEBUG(
1915  errs()
1916  << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1917  break;
1918  case LLVMDLLExportLinkage:
1919  LLVM_DEBUG(
1920  errs()
1921  << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1922  break;
1925  break;
1926  case LLVMGhostLinkage:
1927  LLVM_DEBUG(
1928  errs() << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1929  break;
1930  case LLVMCommonLinkage:
1932  break;
1933  }
1934 }
1935 
1936 const char *LLVMGetSection(LLVMValueRef Global) {
1937  // Using .data() is safe because of how GlobalObject::setSection is
1938  // implemented.
1939  return unwrap<GlobalValue>(Global)->getSection().data();
1940 }
1941 
1942 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1943  unwrap<GlobalObject>(Global)->setSection(Section);
1944 }
1945 
1947  return static_cast<LLVMVisibility>(
1948  unwrap<GlobalValue>(Global)->getVisibility());
1949 }
1950 
1952  unwrap<GlobalValue>(Global)
1953  ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1954 }
1955 
1957  return static_cast<LLVMDLLStorageClass>(
1958  unwrap<GlobalValue>(Global)->getDLLStorageClass());
1959 }
1960 
1962  unwrap<GlobalValue>(Global)->setDLLStorageClass(
1963  static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1964 }
1965 
1967  switch (unwrap<GlobalValue>(Global)->getUnnamedAddr()) {
1969  return LLVMNoUnnamedAddr;
1971  return LLVMLocalUnnamedAddr;
1973  return LLVMGlobalUnnamedAddr;
1974  }
1975  llvm_unreachable("Unknown UnnamedAddr kind!");
1976 }
1977 
1979  GlobalValue *GV = unwrap<GlobalValue>(Global);
1980 
1981  switch (UnnamedAddr) {
1982  case LLVMNoUnnamedAddr:
1984  case LLVMLocalUnnamedAddr:
1986  case LLVMGlobalUnnamedAddr:
1988  }
1989 }
1990 
1992  return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
1993 }
1994 
1995 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
1996  unwrap<GlobalValue>(Global)->setUnnamedAddr(
1997  HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
1999 }
2000 
2002  return wrap(unwrap<GlobalValue>(Global)->getValueType());
2003 }
2004 
2005 /*--.. Operations on global variables, load and store instructions .........--*/
2006 
2008  Value *P = unwrap<Value>(V);
2009  if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2010  return GV->getAlign() ? GV->getAlign()->value() : 0;
2011  if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
2012  return AI->getAlign().value();
2013  if (LoadInst *LI = dyn_cast<LoadInst>(P))
2014  return LI->getAlign().value();
2015  if (StoreInst *SI = dyn_cast<StoreInst>(P))
2016  return SI->getAlign().value();
2017  if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2018  return RMWI->getAlign().value();
2019  if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2020  return CXI->getAlign().value();
2021 
2023  "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, "
2024  "and AtomicCmpXchgInst have alignment");
2025 }
2026 
2027 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
2028  Value *P = unwrap<Value>(V);
2029  if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2030  GV->setAlignment(MaybeAlign(Bytes));
2031  else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
2032  AI->setAlignment(Align(Bytes));
2033  else if (LoadInst *LI = dyn_cast<LoadInst>(P))
2034  LI->setAlignment(Align(Bytes));
2035  else if (StoreInst *SI = dyn_cast<StoreInst>(P))
2036  SI->setAlignment(Align(Bytes));
2037  else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2038  RMWI->setAlignment(Align(Bytes));
2039  else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2040  CXI->setAlignment(Align(Bytes));
2041  else
2043  "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, and "
2044  "and AtomicCmpXchgInst have alignment");
2045 }
2046 
2048  size_t *NumEntries) {
2049  return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
2050  Entries.clear();
2051  if (Instruction *Instr = dyn_cast<Instruction>(unwrap(Value))) {
2052  Instr->getAllMetadata(Entries);
2053  } else {
2054  unwrap<GlobalObject>(Value)->getAllMetadata(Entries);
2055  }
2056  });
2057 }
2058 
2060  unsigned Index) {
2062  static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2063  return MVE.Kind;
2064 }
2065 
2068  unsigned Index) {
2070  static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2071  return MVE.Metadata;
2072 }
2073 
2075  free(Entries);
2076 }
2077 
2079  LLVMMetadataRef MD) {
2080  unwrap<GlobalObject>(Global)->setMetadata(Kind, unwrap<MDNode>(MD));
2081 }
2082 
2084  unwrap<GlobalObject>(Global)->eraseMetadata(Kind);
2085 }
2086 
2088  unwrap<GlobalObject>(Global)->clearMetadata();
2089 }
2090 
2091 /*--.. Operations on global variables ......................................--*/
2092 
2094  return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2095  GlobalValue::ExternalLinkage, nullptr, Name));
2096 }
2097 
2099  const char *Name,
2100  unsigned AddressSpace) {
2101  return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2104  AddressSpace));
2105 }
2106 
2108  return wrap(unwrap(M)->getNamedGlobal(Name));
2109 }
2110 
2112  Module *Mod = unwrap(M);
2114  if (I == Mod->global_end())
2115  return nullptr;
2116  return wrap(&*I);
2117 }
2118 
2120  Module *Mod = unwrap(M);
2122  if (I == Mod->global_begin())
2123  return nullptr;
2124  return wrap(&*--I);
2125 }
2126 
2128  GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2130  if (++I == GV->getParent()->global_end())
2131  return nullptr;
2132  return wrap(&*I);
2133 }
2134 
2136  GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2138  if (I == GV->getParent()->global_begin())
2139  return nullptr;
2140  return wrap(&*--I);
2141 }
2142 
2144  unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
2145 }
2146 
2148  GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
2149  if ( !GV->hasInitializer() )
2150  return nullptr;
2151  return wrap(GV->getInitializer());
2152 }
2153 
2155  unwrap<GlobalVariable>(GlobalVar)
2156  ->setInitializer(unwrap<Constant>(ConstantVal));
2157 }
2158 
2160  return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
2161 }
2162 
2164  unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
2165 }
2166 
2168  return unwrap<GlobalVariable>(GlobalVar)->isConstant();
2169 }
2170 
2172  unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
2173 }
2174 
2176  switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
2178  return LLVMNotThreadLocal;
2182  return LLVMLocalDynamicTLSModel;
2184  return LLVMInitialExecTLSModel;
2186  return LLVMLocalExecTLSModel;
2187  }
2188 
2189  llvm_unreachable("Invalid GlobalVariable thread local mode");
2190 }
2191 
2193  GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2194 
2195  switch (Mode) {
2196  case LLVMNotThreadLocal:
2198  break;
2201  break;
2204  break;
2207  break;
2208  case LLVMLocalExecTLSModel:
2210  break;
2211  }
2212 }
2213 
2215  return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
2216 }
2217 
2219  unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
2220 }
2221 
2222 /*--.. Operations on aliases ......................................--*/
2223 
2225  const char *Name) {
2226  auto *PTy = cast<PointerType>(unwrap(Ty));
2227  return wrap(GlobalAlias::create(PTy->getNonOpaquePointerElementType(),
2228  PTy->getAddressSpace(),
2230  unwrap<Constant>(Aliasee), unwrap(M)));
2231 }
2232 
2234  unsigned AddrSpace, LLVMValueRef Aliasee,
2235  const char *Name) {
2236  return wrap(GlobalAlias::create(unwrap(ValueTy), AddrSpace,
2238  unwrap<Constant>(Aliasee), unwrap(M)));
2239 }
2240 
2242  const char *Name, size_t NameLen) {
2243  return wrap(unwrap(M)->getNamedAlias(StringRef(Name, NameLen)));
2244 }
2245 
2247  Module *Mod = unwrap(M);
2249  if (I == Mod->alias_end())
2250  return nullptr;
2251  return wrap(&*I);
2252 }
2253 
2255  Module *Mod = unwrap(M);
2257  if (I == Mod->alias_begin())
2258  return nullptr;
2259  return wrap(&*--I);
2260 }
2261 
2263  GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2264  Module::alias_iterator I(Alias);
2265  if (++I == Alias->getParent()->alias_end())
2266  return nullptr;
2267  return wrap(&*I);
2268 }
2269 
2271  GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2272  Module::alias_iterator I(Alias);
2273  if (I == Alias->getParent()->alias_begin())
2274  return nullptr;
2275  return wrap(&*--I);
2276 }
2277 
2279  return wrap(unwrap<GlobalAlias>(Alias)->getAliasee());
2280 }
2281 
2283  unwrap<GlobalAlias>(Alias)->setAliasee(unwrap<Constant>(Aliasee));
2284 }
2285 
2286 /*--.. Operations on functions .............................................--*/
2287 
2289  LLVMTypeRef FunctionTy) {
2290  return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
2292 }
2293 
2295  return wrap(unwrap(M)->getFunction(Name));
2296 }
2297 
2299  Module *Mod = unwrap(M);
2300  Module::iterator I = Mod->begin();
2301  if (I == Mod->end())
2302  return nullptr;
2303  return wrap(&*I);
2304 }
2305 
2307  Module *Mod = unwrap(M);
2308  Module::iterator I = Mod->end();
2309  if (I == Mod->begin())
2310  return nullptr;
2311  return wrap(&*--I);
2312 }
2313 
2315  Function *Func = unwrap<Function>(Fn);
2316  Module::iterator I(Func);
2317  if (++I == Func->getParent()->end())
2318  return nullptr;
2319  return wrap(&*I);
2320 }
2321 
2323  Function *Func = unwrap<Function>(Fn);
2324  Module::iterator I(Func);
2325  if (I == Func->getParent()->begin())
2326  return nullptr;
2327  return wrap(&*--I);
2328 }
2329 
2331  unwrap<Function>(Fn)->eraseFromParent();
2332 }
2333 
2335  return unwrap<Function>(Fn)->hasPersonalityFn();
2336 }
2337 
2339  return wrap(unwrap<Function>(Fn)->getPersonalityFn());
2340 }
2341 
2343  unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
2344 }
2345 
2347  if (Function *F = dyn_cast<Function>(unwrap(Fn)))
2348  return F->getIntrinsicID();
2349  return 0;
2350 }
2351 
2353  assert(ID < llvm::Intrinsic::num_intrinsics && "Intrinsic ID out of range");
2354  return llvm::Intrinsic::ID(ID);
2355 }
2356 
2358  unsigned ID,
2359  LLVMTypeRef *ParamTypes,
2360  size_t ParamCount) {
2361  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2362  auto IID = llvm_map_to_intrinsic_id(ID);
2363  return wrap(llvm::Intrinsic::getDeclaration(unwrap(Mod), IID, Tys));
2364 }
2365 
2366 const char *LLVMIntrinsicGetName(unsigned ID, size_t *NameLength) {
2367  auto IID = llvm_map_to_intrinsic_id(ID);
2368  auto Str = llvm::Intrinsic::getName(IID);
2369  *NameLength = Str.size();
2370  return Str.data();
2371 }
2372 
2374  LLVMTypeRef *ParamTypes, size_t ParamCount) {
2375  auto IID = llvm_map_to_intrinsic_id(ID);
2376  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2377  return wrap(llvm::Intrinsic::getType(*unwrap(Ctx), IID, Tys));
2378 }
2379 
2381  LLVMTypeRef *ParamTypes,
2382  size_t ParamCount,
2383  size_t *NameLength) {
2384  auto IID = llvm_map_to_intrinsic_id(ID);
2385  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2386  auto Str = llvm::Intrinsic::getNameNoUnnamedTypes(IID, Tys);
2387  *NameLength = Str.length();
2388  return strdup(Str.c_str());
2389 }
2390 
2392  LLVMTypeRef *ParamTypes,
2393  size_t ParamCount,
2394  size_t *NameLength) {
2395  auto IID = llvm_map_to_intrinsic_id(ID);
2396  ArrayRef<Type *> Tys(unwrap(ParamTypes), ParamCount);
2397  auto Str = llvm::Intrinsic::getName(IID, Tys, unwrap(Mod));
2398  *NameLength = Str.length();
2399  return strdup(Str.c_str());
2400 }
2401 
2402 unsigned LLVMLookupIntrinsicID(const char *Name, size_t NameLen) {
2403  return Function::lookupIntrinsicID({Name, NameLen});
2404 }
2405 
2407  auto IID = llvm_map_to_intrinsic_id(ID);
2408  return llvm::Intrinsic::isOverloaded(IID);
2409 }
2410 
2412  return unwrap<Function>(Fn)->getCallingConv();
2413 }
2414 
2416  return unwrap<Function>(Fn)->setCallingConv(
2417  static_cast<CallingConv::ID>(CC));
2418 }
2419 
2420 const char *LLVMGetGC(LLVMValueRef Fn) {
2421  Function *F = unwrap<Function>(Fn);
2422  return F->hasGC()? F->getGC().c_str() : nullptr;
2423 }
2424 
2425 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
2426  Function *F = unwrap<Function>(Fn);
2427  if (GC)
2428  F->setGC(GC);
2429  else
2430  F->clearGC();
2431 }
2432 
2434  LLVMAttributeRef A) {
2435  unwrap<Function>(F)->addAttributeAtIndex(Idx, unwrap(A));
2436 }
2437 
2439  auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2440  return AS.getNumAttributes();
2441 }
2442 
2445  auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2446  for (auto A : AS)
2447  *Attrs++ = wrap(A);
2448 }
2449 
2451  LLVMAttributeIndex Idx,
2452  unsigned KindID) {
2453  return wrap(unwrap<Function>(F)->getAttributeAtIndex(
2454  Idx, (Attribute::AttrKind)KindID));
2455 }
2456 
2458  LLVMAttributeIndex Idx,
2459  const char *K, unsigned KLen) {
2460  return wrap(
2461  unwrap<Function>(F)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
2462 }
2463 
2465  unsigned KindID) {
2466  unwrap<Function>(F)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
2467 }
2468 
2470  const char *K, unsigned KLen) {
2471  unwrap<Function>(F)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
2472 }
2473 
2475  const char *V) {
2476  Function *Func = unwrap<Function>(Fn);
2477  Attribute Attr = Attribute::get(Func->getContext(), A, V);
2478  Func->addFnAttr(Attr);
2479 }
2480 
2481 /*--.. Operations on parameters ............................................--*/
2482 
2484  // This function is strictly redundant to
2485  // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
2486  return unwrap<Function>(FnRef)->arg_size();
2487 }
2488 
2489 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
2490  Function *Fn = unwrap<Function>(FnRef);
2491  for (Argument &A : Fn->args())
2492  *ParamRefs++ = wrap(&A);
2493 }
2494 
2496  Function *Fn = unwrap<Function>(FnRef);
2497  return wrap(&Fn->arg_begin()[index]);
2498 }
2499 
2501  return wrap(unwrap<Argument>(V)->getParent());
2502 }
2503 
2505  Function *Func = unwrap<Function>(Fn);
2506  Function::arg_iterator I = Func->arg_begin();
2507  if (I == Func->arg_end())
2508  return nullptr;
2509  return wrap(&*I);
2510 }
2511 
2513  Function *Func = unwrap<Function>(Fn);
2514  Function::arg_iterator I = Func->arg_end();
2515  if (I == Func->arg_begin())
2516  return nullptr;
2517  return wrap(&*--I);
2518 }
2519 
2521  Argument *A = unwrap<Argument>(Arg);
2522  Function *Fn = A->getParent();
2523  if (A->getArgNo() + 1 >= Fn->arg_size())
2524  return nullptr;
2525  return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
2526 }
2527 
2529  Argument *A = unwrap<Argument>(Arg);
2530  if (A->getArgNo() == 0)
2531  return nullptr;
2532  return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
2533 }
2534 
2536  Argument *A = unwrap<Argument>(Arg);
2537  A->addAttr(Attribute::getWithAlignment(A->getContext(), Align(align)));
2538 }
2539 
2540 /*--.. Operations on ifuncs ................................................--*/
2541 
2543  const char *Name, size_t NameLen,
2544  LLVMTypeRef Ty, unsigned AddrSpace,
2546  return wrap(GlobalIFunc::create(unwrap(Ty), AddrSpace,
2548  StringRef(Name, NameLen),
2549  unwrap<Constant>(Resolver), unwrap(M)));
2550 }
2551 
2553  const char *Name, size_t NameLen) {
2554  return wrap(unwrap(M)->getNamedIFunc(StringRef(Name, NameLen)));
2555 }
2556 
2558  Module *Mod = unwrap(M);
2560  if (I == Mod->ifunc_end())
2561  return nullptr;
2562  return wrap(&*I);
2563 }
2564 
2566  Module *Mod = unwrap(M);
2568  if (I == Mod->ifunc_begin())
2569  return nullptr;
2570  return wrap(&*--I);
2571 }
2572 
2574  GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2576  if (++I == GIF->getParent()->ifunc_end())
2577  return nullptr;
2578  return wrap(&*I);
2579 }
2580 
2582  GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2584  if (I == GIF->getParent()->ifunc_begin())
2585  return nullptr;
2586  return wrap(&*--I);
2587 }
2588 
2590  return wrap(unwrap<GlobalIFunc>(IFunc)->getResolver());
2591 }
2592 
2594  unwrap<GlobalIFunc>(IFunc)->setResolver(unwrap<Constant>(Resolver));
2595 }
2596 
2598  unwrap<GlobalIFunc>(IFunc)->eraseFromParent();
2599 }
2600 
2602  unwrap<GlobalIFunc>(IFunc)->removeFromParent();
2603 }
2604 
2605 /*--.. Operations on basic blocks ..........................................--*/
2606 
2608  return wrap(static_cast<Value*>(unwrap(BB)));
2609 }
2610 
2612  return isa<BasicBlock>(unwrap(Val));
2613 }
2614 
2616  return wrap(unwrap<BasicBlock>(Val));
2617 }
2618 
2620  return unwrap(BB)->getName().data();
2621 }
2622 
2624  return wrap(unwrap(BB)->getParent());
2625 }
2626 
2628  return wrap(unwrap(BB)->getTerminator());
2629 }
2630 
2632  return unwrap<Function>(FnRef)->size();
2633 }
2634 
2635 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
2636  Function *Fn = unwrap<Function>(FnRef);
2637  for (BasicBlock &BB : *Fn)
2638  *BasicBlocksRefs++ = wrap(&BB);
2639 }
2640 
2642  return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2643 }
2644 
2646  Function *Func = unwrap<Function>(Fn);
2647  Function::iterator I = Func->begin();
2648  if (I == Func->end())
2649  return nullptr;
2650  return wrap(&*I);
2651 }
2652 
2654  Function *Func = unwrap<Function>(Fn);
2655  Function::iterator I = Func->end();
2656  if (I == Func->begin())
2657  return nullptr;
2658  return wrap(&*--I);
2659 }
2660 
2662  BasicBlock *Block = unwrap(BB);
2663  Function::iterator I(Block);
2664  if (++I == Block->getParent()->end())
2665  return nullptr;
2666  return wrap(&*I);
2667 }
2668 
2670  BasicBlock *Block = unwrap(BB);
2671  Function::iterator I(Block);
2672  if (I == Block->getParent()->begin())
2673  return nullptr;
2674  return wrap(&*--I);
2675 }
2676 
2678  const char *Name) {
2680 }
2681 
2684  BasicBlock *ToInsert = unwrap(BB);
2685  BasicBlock *CurBB = unwrap(Builder)->GetInsertBlock();
2686  assert(CurBB && "current insertion point is invalid!");
2687  CurBB->getParent()->getBasicBlockList().insertAfter(CurBB->getIterator(),
2688  ToInsert);
2689 }
2690 
2693  unwrap<Function>(Fn)->getBasicBlockList().push_back(unwrap(BB));
2694 }
2695 
2697  LLVMValueRef FnRef,
2698  const char *Name) {
2699  return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
2700 }
2701 
2704 }
2705 
2707  LLVMBasicBlockRef BBRef,
2708  const char *Name) {
2709  BasicBlock *BB = unwrap(BBRef);
2710  return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2711 }
2712 
2714  const char *Name) {
2716 }
2717 
2719  unwrap(BBRef)->eraseFromParent();
2720 }
2721 
2723  unwrap(BBRef)->removeFromParent();
2724 }
2725 
2727  unwrap(BB)->moveBefore(unwrap(MovePos));
2728 }
2729 
2731  unwrap(BB)->moveAfter(unwrap(MovePos));
2732 }
2733 
2734 /*--.. Operations on instructions ..........................................--*/
2735 
2737  return wrap(unwrap<Instruction>(Inst)->getParent());
2738 }
2739 
2741  BasicBlock *Block = unwrap(BB);
2742  BasicBlock::iterator I = Block->begin();
2743  if (I == Block->end())
2744  return nullptr;
2745  return wrap(&*I);
2746 }
2747 
2749  BasicBlock *Block = unwrap(BB);
2750  BasicBlock::iterator I = Block->end();
2751  if (I == Block->begin())
2752  return nullptr;
2753  return wrap(&*--I);
2754 }
2755 
2757  Instruction *Instr = unwrap<Instruction>(Inst);
2758  BasicBlock::iterator I(Instr);
2759  if (++I == Instr->getParent()->end())
2760  return nullptr;
2761  return wrap(&*I);
2762 }
2763 
2765  Instruction *Instr = unwrap<Instruction>(Inst);
2766  BasicBlock::iterator I(Instr);
2767  if (I == Instr->getParent()->begin())
2768  return nullptr;
2769  return wrap(&*--I);
2770 }
2771 
2773  unwrap<Instruction>(Inst)->removeFromParent();
2774 }
2775 
2777  unwrap<Instruction>(Inst)->eraseFromParent();
2778 }
2779 
2781  unwrap<Instruction>(Inst)->deleteValue();
2782 }
2783 
2785  if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2786  return (LLVMIntPredicate)I->getPredicate();
2787  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2788  if (CE->getOpcode() == Instruction::ICmp)
2789  return (LLVMIntPredicate)CE->getPredicate();
2790  return (LLVMIntPredicate)0;
2791 }
2792 
2794  if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2795  return (LLVMRealPredicate)I->getPredicate();
2796  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2797  if (CE->getOpcode() == Instruction::FCmp)
2798  return (LLVMRealPredicate)CE->getPredicate();
2799  return (LLVMRealPredicate)0;
2800 }
2801 
2803  if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2804  return map_to_llvmopcode(C->getOpcode());
2805  return (LLVMOpcode)0;
2806 }
2807 
2809  if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2810  return wrap(C->clone());
2811  return nullptr;
2812 }
2813 
2815  Instruction *I = dyn_cast<Instruction>(unwrap(Inst));
2816  return (I && I->isTerminator()) ? wrap(I) : nullptr;
2817 }
2818 
2820  if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) {
2821  return FPI->getNumArgOperands();
2822  }
2823  return unwrap<CallBase>(Instr)->arg_size();
2824 }
2825 
2826 /*--.. Call and invoke instructions ........................................--*/
2827 
2829  return unwrap<CallBase>(Instr)->getCallingConv();
2830 }
2831 
2833  return unwrap<CallBase>(Instr)->setCallingConv(
2834  static_cast<CallingConv::ID>(CC));
2835 }
2836 
2838  unsigned align) {
2839  auto *Call = unwrap<CallBase>(Instr);
2840  Attribute AlignAttr =
2841  Attribute::getWithAlignment(Call->getContext(), Align(align));
2842  Call->addAttributeAtIndex(Idx, AlignAttr);
2843 }
2844 
2846  LLVMAttributeRef A) {
2847  unwrap<CallBase>(C)->addAttributeAtIndex(Idx, unwrap(A));
2848 }
2849 
2851  LLVMAttributeIndex Idx) {
2852  auto *Call = unwrap<CallBase>(C);
2853  auto AS = Call->getAttributes().getAttributes(Idx);
2854  return AS.getNumAttributes();
2855 }
2856 
2859  auto *Call = unwrap<CallBase>(C);
2860  auto AS = Call->getAttributes().getAttributes(Idx);
2861  for (auto A : AS)
2862  *Attrs++ = wrap(A);
2863 }
2864 
2866  LLVMAttributeIndex Idx,
2867  unsigned KindID) {
2868  return wrap(unwrap<CallBase>(C)->getAttributeAtIndex(
2869  Idx, (Attribute::AttrKind)KindID));
2870 }
2871 
2873  LLVMAttributeIndex Idx,
2874  const char *K, unsigned KLen) {
2875  return wrap(
2876  unwrap<CallBase>(C)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
2877 }
2878 
2880  unsigned KindID) {
2881  unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
2882 }
2883 
2885  const char *K, unsigned KLen) {
2886  unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
2887 }
2888 
2890  return wrap(unwrap<CallBase>(Instr)->getCalledOperand());
2891 }
2892 
2894  return wrap(unwrap<CallBase>(Instr)->getFunctionType());
2895 }
2896 
2897 /*--.. Operations on call instructions (only) ..............................--*/
2898 
2900  return unwrap<CallInst>(Call)->isTailCall();
2901 }
2902 
2903 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
2904  unwrap<CallInst>(Call)->setTailCall(isTailCall);
2905 }
2906 
2907 /*--.. Operations on invoke instructions (only) ............................--*/
2908 
2910  return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2911 }
2912 
2914  if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2915  return wrap(CRI->getUnwindDest());
2916  } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2917  return wrap(CSI->getUnwindDest());
2918  }
2919  return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2920 }
2921 
2923  unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2924 }
2925 
2927  if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2928  return CRI->setUnwindDest(unwrap(B));
2929  } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2930  return CSI->setUnwindDest(unwrap(B));
2931  }
2932  unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2933 }
2934 
2935 /*--.. Operations on terminators ...........................................--*/
2936 
2938  return unwrap<Instruction>(Term)->getNumSuccessors();
2939 }
2940 
2942  return wrap(unwrap<Instruction>(Term)->getSuccessor(i));
2943 }
2944 
2946  return unwrap<Instruction>(Term)->setSuccessor(i, unwrap(block));
2947 }
2948 
2949 /*--.. Operations on branch instructions (only) ............................--*/
2950 
2952  return unwrap<BranchInst>(Branch)->isConditional();
2953 }
2954 
2956  return wrap(unwrap<BranchInst>(Branch)->getCondition());
2957 }
2958 
2960  return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2961 }
2962 
2963 /*--.. Operations on switch instructions (only) ............................--*/
2964 
2966  return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2967 }
2968 
2969 /*--.. Operations on alloca instructions (only) ............................--*/
2970 
2972  return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
2973 }
2974 
2975 /*--.. Operations on gep instructions (only) ...............................--*/
2976 
2978  return unwrap<GEPOperator>(GEP)->isInBounds();
2979 }
2980 
2982  return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
2983 }
2984 
2986  return wrap(unwrap<GEPOperator>(GEP)->getSourceElementType());
2987 }
2988 
2989 /*--.. Operations on phi nodes .............................................--*/
2990 
2991 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
2992  LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
2993  PHINode *PhiVal = unwrap<PHINode>(PhiNode);
2994  for (unsigned I = 0; I != Count; ++I)
2995  PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
2996 }
2997 
2998 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
2999  return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
3000 }
3001 
3003  return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
3004 }
3005 
3007  return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
3008 }
3009 
3010 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/
3011 
3013  auto *I = unwrap(Inst);
3014  if (auto *GEP = dyn_cast<GEPOperator>(I))
3015  return GEP->getNumIndices();
3016  if (auto *EV = dyn_cast<ExtractValueInst>(I))
3017  return EV->getNumIndices();
3018  if (auto *IV = dyn_cast<InsertValueInst>(I))
3019  return IV->getNumIndices();
3021  "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
3022 }
3023 
3024 const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
3025  auto *I = unwrap(Inst);
3026  if (auto *EV = dyn_cast<ExtractValueInst>(I))
3027  return EV->getIndices().data();
3028  if (auto *IV = dyn_cast<InsertValueInst>(I))
3029  return IV->getIndices().data();
3031  "LLVMGetIndices applies only to extractvalue and insertvalue!");
3032 }
3033 
3034 
3035 /*===-- Instruction builders ----------------------------------------------===*/
3036 
3038  return wrap(new IRBuilder<>(*unwrap(C)));
3039 }
3040 
3043 }
3044 
3046  LLVMValueRef Instr) {
3047  BasicBlock *BB = unwrap(Block);
3048  auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
3049  unwrap(Builder)->SetInsertPoint(BB, I);
3050 }
3051 
3053  Instruction *I = unwrap<Instruction>(Instr);
3054  unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
3055 }
3056 
3058  BasicBlock *BB = unwrap(Block);
3059  unwrap(Builder)->SetInsertPoint(BB);
3060 }
3061 
3063  return wrap(unwrap(Builder)->GetInsertBlock());
3064 }
3065 
3067  unwrap(Builder)->ClearInsertionPoint();
3068 }
3069 
3071  unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
3072 }
3073 
3075  const char *Name) {
3076  unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
3077 }
3078 
3080  delete unwrap(Builder);
3081 }
3082 
3083 /*--.. Metadata builders ...................................................--*/
3084 
3086  return wrap(unwrap(Builder)->getCurrentDebugLocation().getAsMDNode());
3087 }
3088 
3090  if (Loc)
3091  unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(unwrap<MDNode>(Loc)));
3092  else
3093  unwrap(Builder)->SetCurrentDebugLocation(DebugLoc());
3094 }
3095 
3097  MDNode *Loc =
3098  L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
3099  unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
3100 }
3101 
3103  LLVMContext &Context = unwrap(Builder)->getContext();
3104  return wrap(MetadataAsValue::get(
3105  Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
3106 }
3107 
3109  unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
3110 }
3111 
3113  unwrap(Builder)->AddMetadataToInst(unwrap<Instruction>(Inst));
3114 }
3115 
3117  LLVMMetadataRef FPMathTag) {
3118 
3119  unwrap(Builder)->setDefaultFPMathTag(FPMathTag
3120  ? unwrap<MDNode>(FPMathTag)
3121  : nullptr);
3122 }
3123 
3125  return wrap(unwrap(Builder)->getDefaultFPMathTag());
3126 }
3127 
3128 /*--.. Instruction builders ................................................--*/
3129 
3131  return wrap(unwrap(B)->CreateRetVoid());
3132 }
3133 
3135  return wrap(unwrap(B)->CreateRet(unwrap(V)));
3136 }
3137 
3139  unsigned N) {
3140  return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
3141 }
3142 
3144  return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
3145 }
3146 
3149  return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
3150 }
3151 
3153  LLVMBasicBlockRef Else, unsigned NumCases) {
3154  return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
3155 }
3156 
3158  unsigned NumDests) {
3159  return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
3160 }
3161 
3163  LLVMValueRef *Args, unsigned NumArgs,
3165  const char *Name) {
3166  Value *V = unwrap(Fn);
3167  FunctionType *FnT =
3168  cast<FunctionType>(V->getType()->getNonOpaquePointerElementType());
3169 
3170  return wrap(
3171  unwrap(B)->CreateInvoke(FnT, unwrap(Fn), unwrap(Then), unwrap(Catch),
3172  makeArrayRef(unwrap(Args), NumArgs), Name));
3173 }
3174 
3176  LLVMValueRef *Args, unsigned NumArgs,
3178  const char *Name) {
3179  return wrap(unwrap(B)->CreateInvoke(
3180  unwrap<FunctionType>(Ty), unwrap(Fn), unwrap(Then), unwrap(Catch),
3181  makeArrayRef(unwrap(Args), NumArgs), Name));
3182 }
3183 
3185  LLVMValueRef PersFn, unsigned NumClauses,
3186  const char *Name) {
3187  // The personality used to live on the landingpad instruction, but now it
3188  // lives on the parent function. For compatibility, take the provided
3189  // personality and put it on the parent function.
3190  if (PersFn)
3191  unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
3192  cast<Function>(unwrap(PersFn)));
3193  return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
3194 }
3195 
3197  LLVMValueRef *Args, unsigned NumArgs,
3198  const char *Name) {
3199  return wrap(unwrap(B)->CreateCatchPad(unwrap(ParentPad),
3200  makeArrayRef(unwrap(Args), NumArgs),
3201  Name));
3202 }
3203 
3205  LLVMValueRef *Args, unsigned NumArgs,
3206  const char *Name) {
3207  if (ParentPad == nullptr) {
3208  Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3209  ParentPad = wrap(Constant::getNullValue(Ty));
3210  }
3211  return wrap(unwrap(B)->CreateCleanupPad(unwrap(ParentPad),
3212  makeArrayRef(unwrap(Args), NumArgs),
3213  Name));
3214 }
3215 
3217  return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
3218 }
3219 
3221  LLVMBasicBlockRef UnwindBB,
3222  unsigned NumHandlers, const char *Name) {
3223  if (ParentPad == nullptr) {
3224  Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3225  ParentPad = wrap(Constant::getNullValue(Ty));
3226  }
3227  return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(UnwindBB),
3228  NumHandlers, Name));
3229 }
3230 
3233  return wrap(unwrap(B)->CreateCatchRet(unwrap<CatchPadInst>(CatchPad),
3234  unwrap(BB)));
3235 }
3236 
3239  return wrap(unwrap(B)->CreateCleanupRet(unwrap<CleanupPadInst>(CatchPad),
3240  unwrap(BB)));
3241 }
3242 
3244  return wrap(unwrap(B)->CreateUnreachable());
3245 }
3246 
3248  LLVMBasicBlockRef Dest) {
3249  unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
3250 }
3251 
3253  unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
3254 }
3255 
3256 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
3257  return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
3258 }
3259 
3260 LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
3261  return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
3262 }
3263 
3265  unwrap<LandingPadInst>(LandingPad)->
3266  addClause(cast<Constant>(unwrap(ClauseVal)));
3267 }
3268 
3270  return unwrap<LandingPadInst>(LandingPad)->isCleanup();
3271 }
3272 
3273 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
3274  unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
3275 }
3276 
3278  unwrap<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Dest));
3279 }
3280 
3281 unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) {
3282  return unwrap<CatchSwitchInst>(CatchSwitch)->getNumHandlers();
3283 }
3284 
3285 void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) {
3286  CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(CatchSwitch);
3287  for (const BasicBlock *H : CSI->handlers())
3288  *Handlers++ = wrap(H);
3289 }
3290 
3292  return wrap(unwrap<CatchPadInst>(CatchPad)->getCatchSwitch());
3293 }
3294 
3296  unwrap<CatchPadInst>(CatchPad)
3297  ->setCatchSwitch(unwrap<CatchSwitchInst>(CatchSwitch));
3298 }
3299 
3300 /*--.. Funclets ...........................................................--*/
3301 
3303  return wrap(unwrap<FuncletPadInst>(Funclet)->getArgOperand(i));
3304 }
3305 
3306 void LLVMSetArgOperand(LLVMValueRef Funclet, unsigned i, LLVMValueRef value) {
3307  unwrap<FuncletPadInst>(Funclet)->setArgOperand(i, unwrap(value));
3308 }
3309 
3310 /*--.. Arithmetic ..........................................................--*/
3311 
3313  const char *Name) {
3314  return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
3315 }
3316 
3318  const char *Name) {
3319  return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
3320 }
3321 
3323  const char *Name) {
3324  return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
3325 }
3326 
3328  const char *Name) {
3329  return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
3330 }
3331 
3333  const char *Name) {
3334  return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
3335 }
3336 
3338  const char *Name) {
3339  return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
3340 }
3341 
3343  const char *Name) {
3344  return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
3345 }
3346 
3348  const char *Name) {
3349  return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
3350 }
3351 
3353  const char *Name) {
3354  return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
3355 }
3356 
3358  const char *Name) {
3359  return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
3360 }
3361 
3363  const char *Name) {
3364  return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
3365 }
3366 
3368  const char *Name) {
3369  return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
3370 }
3371 
3373  const char *Name) {
3374  return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
3375 }
3376 
3378  LLVMValueRef RHS, const char *Name) {
3379  return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
3380 }
3381 
3383  const char *Name) {
3384  return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
3385 }
3386 
3388  LLVMValueRef RHS, const char *Name) {
3389  return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
3390 }
3391 
3393  const char *Name) {
3394  return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
3395 }
3396 
3398  const char *Name) {
3399  return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
3400 }
3401 
3403  const char *Name) {
3404  return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
3405 }
3406 
3408  const char *Name) {
3409  return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
3410 }
3411 
3413  const char *Name) {
3414  return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
3415 }
3416 
3418  const char *Name) {
3419  return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
3420 }
3421 
3423  const char *Name) {
3424  return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
3425 }
3426 
3428  const char *Name) {
3429  return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
3430 }
3431 
3433  const char *Name) {
3434  return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
3435 }
3436 
3438  const char *Name) {
3439  return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
3440 }
3441 
3444  const char *Name) {
3446  unwrap(RHS), Name));
3447 }
3448 
3450  return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
3451 }
3452 
3454  const char *Name) {
3455  return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
3456 }
3457 
3459  const char *Name) {
3460  return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
3461 }
3462 
3464  return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
3465 }
3466 
3468  return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
3469 }
3470 
3471 /*--.. Memory ..............................................................--*/
3472 
3474  const char *Name) {
3475  Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3476  Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3477  AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3478  Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3479  ITy, unwrap(Ty), AllocSize,
3480  nullptr, nullptr, "");
3481  return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3482 }
3483 
3485  LLVMValueRef Val, const char *Name) {
3486  Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3487  Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3488  AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3489  Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3490  ITy, unwrap(Ty), AllocSize,
3491  unwrap(Val), nullptr, "");
3492  return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3493 }
3494 
3496  LLVMValueRef Val, LLVMValueRef Len,
3497  unsigned Align) {
3498  return wrap(unwrap(B)->CreateMemSet(unwrap(Ptr), unwrap(Val), unwrap(Len),
3499  MaybeAlign(Align)));
3500 }
3501 
3503  LLVMValueRef Dst, unsigned DstAlign,
3504  LLVMValueRef Src, unsigned SrcAlign,
3505  LLVMValueRef Size) {
3506  return wrap(unwrap(B)->CreateMemCpy(unwrap(Dst), MaybeAlign(DstAlign),
3507  unwrap(Src), MaybeAlign(SrcAlign),
3508  unwrap(Size)));
3509 }
3510 
3512  LLVMValueRef Dst, unsigned DstAlign,
3513  LLVMValueRef Src, unsigned SrcAlign,
3514  LLVMValueRef Size) {
3515  return wrap(unwrap(B)->CreateMemMove(unwrap(Dst), MaybeAlign(DstAlign),
3516  unwrap(Src), MaybeAlign(SrcAlign),
3517  unwrap(Size)));
3518 }
3519 
3521  const char *Name) {
3522  return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
3523 }
3524 
3526  LLVMValueRef Val, const char *Name) {
3527  return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
3528 }
3529 
3531  return wrap(unwrap(B)->Insert(
3532  CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
3533 }
3534 
3536  const char *Name) {
3537  Value *V = unwrap(PointerVal);
3538  PointerType *Ty = cast<PointerType>(V->getType());
3539 
3540  return wrap(
3541  unwrap(B)->CreateLoad(Ty->getNonOpaquePointerElementType(), V, Name));
3542 }
3543 
3545  LLVMValueRef PointerVal, const char *Name) {
3546  return wrap(unwrap(B)->CreateLoad(unwrap(Ty), unwrap(PointerVal), Name));
3547 }
3548 
3550  LLVMValueRef PointerVal) {
3551  return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
3552 }
3553 
3555  switch (Ordering) {
3565  }
3566 
3567  llvm_unreachable("Invalid LLVMAtomicOrdering value!");
3568 }
3569 
3571  switch (Ordering) {
3581  }
3582 
3583  llvm_unreachable("Invalid AtomicOrdering value!");
3584 }
3585 
3587  switch (BinOp) {
3603  }
3604 
3605  llvm_unreachable("Invalid LLVMAtomicRMWBinOp value!");
3606 }
3607 
3609  switch (BinOp) {
3625  default: break;
3626  }
3627 
3628  llvm_unreachable("Invalid AtomicRMWBinOp value!");
3629 }
3630 
3631 // TODO: Should this and other atomic instructions support building with
3632 // "syncscope"?
3634  LLVMBool isSingleThread, const char *Name) {
3635  return wrap(
3636  unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
3637  isSingleThread ? SyncScope::SingleThread
3639  Name));
3640 }
3641 
3643  LLVMValueRef *Indices, unsigned NumIndices,
3644  const char *Name) {
3645  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3646  Value *Val = unwrap(Pointer);
3648  return wrap(unwrap(B)->CreateGEP(Ty, Val, IdxList, Name));
3649 }
3650 
3652  LLVMValueRef Pointer, LLVMValueRef *Indices,
3653  unsigned NumIndices, const char *Name) {
3654  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3655  return wrap(unwrap(B)->CreateGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3656 }
3657 
3659  LLVMValueRef *Indices, unsigned NumIndices,
3660  const char *Name) {
3661  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3662  Value *Val = unwrap(Pointer);
3664  return wrap(unwrap(B)->CreateInBoundsGEP(Ty, Val, IdxList, Name));
3665 }
3666 
3668  LLVMValueRef Pointer, LLVMValueRef *Indices,
3669  unsigned NumIndices, const char *Name) {
3670  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3671  return wrap(
3672  unwrap(B)->CreateInBoundsGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3673 }
3674 
3676  unsigned Idx, const char *Name) {
3677  Value *Val = unwrap(Pointer);
3679  return wrap(unwrap(B)->CreateStructGEP(Ty, Val, Idx, Name));
3680 }
3681 
3683  LLVMValueRef Pointer, unsigned Idx,
3684  const char *Name) {
3685  return wrap(
3686  unwrap(B)->CreateStructGEP(unwrap(Ty), unwrap(Pointer), Idx, Name));
3687 }
3688 
3690  const char *Name) {
3691  return wrap(unwrap(B)->CreateGlobalString(Str, Name));
3692 }
3693 
3695  const char *Name) {
3696  return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
3697 }
3698 
3700  Value *P = unwrap<Value>(MemAccessInst);
3701  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3702  return LI->isVolatile();
3703  if (StoreInst *SI = dyn_cast<StoreInst>(P))
3704  return SI->isVolatile();
3705  if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
3706  return AI->isVolatile();
3707  return cast<AtomicCmpXchgInst>(P)->isVolatile();
3708 }
3709 
3710 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
3711  Value *P = unwrap<Value>(MemAccessInst);
3712  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3713  return LI->setVolatile(isVolatile);
3714  if (StoreInst *SI = dyn_cast<StoreInst>(P))
3715  return SI->setVolatile(isVolatile);
3716  if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
3717  return AI->setVolatile(isVolatile);
3718  return cast<AtomicCmpXchgInst>(P)->setVolatile(isVolatile);
3719 }
3720 
3722  return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->isWeak();
3723 }
3724 
3725 void LLVMSetWeak(LLVMValueRef CmpXchgInst, LLVMBool isWeak) {
3726  return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->setWeak(isWeak);
3727 }
3728 
3730  Value *P = unwrap<Value>(MemAccessInst);
3731  AtomicOrdering O;
3732  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3733  O = LI->getOrdering();
3734  else if (StoreInst *SI = dyn_cast<StoreInst>(P))
3735  O = SI->getOrdering();
3736  else
3737  O = cast<AtomicRMWInst>(P)->getOrdering();
3738  return mapToLLVMOrdering(O);
3739 }
3740 
3741 void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
3742  Value *P = unwrap<Value>(MemAccessInst);
3743  AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3744 
3745  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3746  return LI->setOrdering(O);
3747  return cast<StoreInst>(P)->setOrdering(O);
3748 }
3749 
3751  return mapToLLVMRMWBinOp(unwrap<AtomicRMWInst>(Inst)->getOperation());
3752 }
3753 
3755  unwrap<AtomicRMWInst>(Inst)->setOperation(mapFromLLVMRMWBinOp(BinOp));
3756 }
3757 
3758 /*--.. Casts ...............................................................--*/
3759 
3761  LLVMTypeRef DestTy, const char *Name) {
3762  return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
3763 }
3764 
3766  LLVMTypeRef DestTy, const char *Name) {
3767  return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
3768 }
3769 
3771  LLVMTypeRef DestTy, const char *Name) {
3772  return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
3773 }
3774 
3776  LLVMTypeRef DestTy, const char *Name) {
3777  return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
3778 }
3779 
3781  LLVMTypeRef DestTy, const char *Name) {
3782  return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
3783 }
3784 
3786  LLVMTypeRef DestTy, const char *Name) {
3787  return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
3788 }
3789 
3791  LLVMTypeRef DestTy, const char *Name) {
3792  return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
3793 }
3794 
3796  LLVMTypeRef DestTy, const char *Name) {
3797  return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
3798 }
3799 
3801  LLVMTypeRef DestTy, const char *Name) {
3802  return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
3803 }
3804 
3806  LLVMTypeRef DestTy, const char *Name) {
3807  return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
3808 }
3809 
3811  LLVMTypeRef DestTy, const char *Name) {
3812  return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
3813 }
3814 
3816  LLVMTypeRef DestTy, const char *Name) {
3817  return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
3818 }
3819 
3821  LLVMTypeRef DestTy, const char *Name) {
3822  return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
3823 }
3824 
3826  LLVMTypeRef DestTy, const char *Name) {
3827  return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
3828  Name));
3829 }
3830 
3832  LLVMTypeRef DestTy, const char *Name) {
3833  return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
3834  Name));
3835 }
3836 
3838  LLVMTypeRef DestTy, const char *Name) {
3839  return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
3840  Name));
3841 }
3842 
3844  LLVMTypeRef DestTy, const char *Name) {
3845  return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
3846  unwrap(DestTy), Name));
3847 }
3848