LLVM  17.0.0git
Core.cpp
Go to the documentation of this file.
1 //===-- Core.cpp ----------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the common infrastructure (including the C bindings)
10 // for libLLVMCore.a, which implements the LLVM intermediate representation.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm-c/Core.h"
15 #include "llvm/IR/Attributes.h"
16 #include "llvm/IR/BasicBlock.h"
17 #include "llvm/IR/Constants.h"
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 /*===-- Version query -----------------------------------------------------===*/
65 
66 void LLVMGetVersion(unsigned *Major, unsigned *Minor, unsigned *Patch) {
67  if (Major)
68  *Major = LLVM_VERSION_MAJOR;
69  if (Minor)
70  *Minor = LLVM_VERSION_MINOR;
71  if (Patch)
72  *Patch = LLVM_VERSION_PATCH;
73 }
74 
75 /*===-- Error handling ----------------------------------------------------===*/
76 
77 char *LLVMCreateMessage(const char *Message) {
78  return strdup(Message);
79 }
80 
81 void LLVMDisposeMessage(char *Message) {
82  free(Message);
83 }
84 
85 
86 /*===-- Operations on contexts --------------------------------------------===*/
87 
89  static LLVMContext GlobalContext;
90  return GlobalContext;
91 }
92 
94  return wrap(new LLVMContext());
95 }
96 
98 
100  LLVMDiagnosticHandler Handler,
101  void *DiagnosticContext) {
102  unwrap(C)->setDiagnosticHandlerCallBack(
104  Handler),
105  DiagnosticContext);
106 }
107 
109  return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>(
110  unwrap(C)->getDiagnosticHandlerCallBack());
111 }
112 
114  return unwrap(C)->getDiagnosticContext();
115 }
116 
118  void *OpaqueHandle) {
119  auto YieldCallback =
120  LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
121  unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
122 }
123 
125  return unwrap(C)->shouldDiscardValueNames();
126 }
127 
129  unwrap(C)->setDiscardValueNames(Discard);
130 }
131 
133  delete unwrap(C);
134 }
135 
136 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char *Name,
137  unsigned SLen) {
138  return unwrap(C)->getMDKindID(StringRef(Name, SLen));
139 }
140 
141 unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) {
142  return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
143 }
144 
145 unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) {
146  return Attribute::getAttrKindFromName(StringRef(Name, SLen));
147 }
148 
150  return Attribute::AttrKind::EndAttrKinds;
151 }
152 
154  uint64_t Val) {
155  auto &Ctx = *unwrap(C);
156  auto AttrKind = (Attribute::AttrKind)KindID;
157  return wrap(Attribute::get(Ctx, AttrKind, Val));
158 }
159 
161  return unwrap(A).getKindAsEnum();
162 }
163 
165  auto Attr = unwrap(A);
166  if (Attr.isEnumAttribute())
167  return 0;
168  return Attr.getValueAsInt();
169 }
170 
172  LLVMTypeRef type_ref) {
173  auto &Ctx = *unwrap(C);
174  auto AttrKind = (Attribute::AttrKind)KindID;
175  return wrap(Attribute::get(Ctx, AttrKind, unwrap(type_ref)));
176 }
177 
179  auto Attr = unwrap(A);
180  return wrap(Attr.getValueAsType());
181 }
182 
184  const char *K, unsigned KLength,
185  const char *V, unsigned VLength) {
186  return wrap(Attribute::get(*unwrap(C), StringRef(K, KLength),
187  StringRef(V, VLength)));
188 }
189 
191  unsigned *Length) {
192  auto S = unwrap(A).getKindAsString();
193  *Length = S.size();
194  return S.data();
195 }
196 
198  unsigned *Length) {
199  auto S = unwrap(A).getValueAsString();
200  *Length = S.size();
201  return S.data();
202 }
203 
205  auto Attr = unwrap(A);
206  return Attr.isEnumAttribute() || Attr.isIntAttribute();
207 }
208 
210  return unwrap(A).isStringAttribute();
211 }
212 
214  return unwrap(A).isTypeAttribute();
215 }
216 
218  std::string MsgStorage;
219  raw_string_ostream Stream(MsgStorage);
221 
222  unwrap(DI)->print(DP);
223  Stream.flush();
224 
225  return LLVMCreateMessage(MsgStorage.c_str());
226 }
227 
229  LLVMDiagnosticSeverity severity;
230 
231  switch(unwrap(DI)->getSeverity()) {
232  default:
233  severity = LLVMDSError;
234  break;
235  case DS_Warning:
236  severity = LLVMDSWarning;
237  break;
238  case DS_Remark:
239  severity = LLVMDSRemark;
240  break;
241  case DS_Note:
242  severity = LLVMDSNote;
243  break;
244  }
245 
246  return severity;
247 }
248 
249 /*===-- Operations on modules ---------------------------------------------===*/
250 
252  return wrap(new Module(ModuleID, getGlobalContext()));
253 }
254 
256  LLVMContextRef C) {
257  return wrap(new Module(ModuleID, *unwrap(C)));
258 }
259 
261  delete unwrap(M);
262 }
263 
264 const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) {
265  auto &Str = unwrap(M)->getModuleIdentifier();
266  *Len = Str.length();
267  return Str.c_str();
268 }
269 
270 void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) {
271  unwrap(M)->setModuleIdentifier(StringRef(Ident, Len));
272 }
273 
274 const char *LLVMGetSourceFileName(LLVMModuleRef M, size_t *Len) {
275  auto &Str = unwrap(M)->getSourceFileName();
276  *Len = Str.length();
277  return Str.c_str();
278 }
279 
280 void LLVMSetSourceFileName(LLVMModuleRef M, const char *Name, size_t Len) {
281  unwrap(M)->setSourceFileName(StringRef(Name, Len));
282 }
283 
284 /*--.. Data layout .........................................................--*/
286  return unwrap(M)->getDataLayoutStr().c_str();
287 }
288 
290  return LLVMGetDataLayoutStr(M);
291 }
292 
293 void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
294  unwrap(M)->setDataLayout(DataLayoutStr);
295 }
296 
297 /*--.. Target triple .......................................................--*/
298 const char * LLVMGetTarget(LLVMModuleRef M) {
299  return unwrap(M)->getTargetTriple().c_str();
300 }
301 
302 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
303  unwrap(M)->setTargetTriple(Triple);
304 }
305 
306 /*--.. Module flags ........................................................--*/
309  const char *Key;
310  size_t KeyLen;
312 };
313 
316  switch (Behavior) {
320  return Module::ModFlagBehavior::Warning;
322  return Module::ModFlagBehavior::Require;
324  return Module::ModFlagBehavior::Override;
326  return Module::ModFlagBehavior::Append;
328  return Module::ModFlagBehavior::AppendUnique;
329  }
330  llvm_unreachable("Unknown LLVMModuleFlagBehavior");
331 }
332 
335  switch (Behavior) {
338  case Module::ModFlagBehavior::Warning:
340  case Module::ModFlagBehavior::Require:
342  case Module::ModFlagBehavior::Override:
344  case Module::ModFlagBehavior::Append:
346  case Module::ModFlagBehavior::AppendUnique:
348  default:
349  llvm_unreachable("Unhandled Flag Behavior");
350  }
351 }
352 
355  unwrap(M)->getModuleFlagsMetadata(MFEs);
356 
357  LLVMOpaqueModuleFlagEntry *Result = static_cast<LLVMOpaqueModuleFlagEntry *>(
358  safe_malloc(MFEs.size() * sizeof(LLVMOpaqueModuleFlagEntry)));
359  for (unsigned i = 0; i < MFEs.size(); ++i) {
360  const auto &ModuleFlag = MFEs[i];
361  Result[i].Behavior = map_from_llvmModFlagBehavior(ModuleFlag.Behavior);
362  Result[i].Key = ModuleFlag.Key->getString().data();
363  Result[i].KeyLen = ModuleFlag.Key->getString().size();
364  Result[i].Metadata = wrap(ModuleFlag.Val);
365  }
366  *Len = MFEs.size();
367  return Result;
368 }
369 
371  free(Entries);
372 }
373 
376  unsigned Index) {
378  static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
379  return MFE.Behavior;
380 }
381 
383  unsigned Index, size_t *Len) {
385  static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
386  *Len = MFE.KeyLen;
387  return MFE.Key;
388 }
389 
391  unsigned Index) {
393  static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
394  return MFE.Metadata;
395 }
396 
398  const char *Key, size_t KeyLen) {
399  return wrap(unwrap(M)->getModuleFlag({Key, KeyLen}));
400 }
401 
403  const char *Key, size_t KeyLen,
404  LLVMMetadataRef Val) {
405  unwrap(M)->addModuleFlag(map_to_llvmModFlagBehavior(Behavior),
406  {Key, KeyLen}, unwrap(Val));
407 }
408 
409 /*--.. Printing modules ....................................................--*/
410 
412  unwrap(M)->print(errs(), nullptr,
413  /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
414 }
415 
417  char **ErrorMessage) {
418  std::error_code EC;
419  raw_fd_ostream dest(Filename, EC, sys::fs::OF_TextWithCRLF);
420  if (EC) {
421  *ErrorMessage = strdup(EC.message().c_str());
422  return true;
423  }
424 
425  unwrap(M)->print(dest, nullptr);
426 
427  dest.close();
428 
429  if (dest.has_error()) {
430  std::string E = "Error printing to file: " + dest.error().message();
431  *ErrorMessage = strdup(E.c_str());
432  return true;
433  }
434 
435  return false;
436 }
437 
439  std::string buf;
440  raw_string_ostream os(buf);
441 
442  unwrap(M)->print(os, nullptr);
443  os.flush();
444 
445  return strdup(buf.c_str());
446 }
447 
448 /*--.. Operations on inline assembler ......................................--*/
449 void LLVMSetModuleInlineAsm2(LLVMModuleRef M, const char *Asm, size_t Len) {
450  unwrap(M)->setModuleInlineAsm(StringRef(Asm, Len));
451 }
452 
454  unwrap(M)->setModuleInlineAsm(StringRef(Asm));
455 }
456 
457 void LLVMAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm, size_t Len) {
458  unwrap(M)->appendModuleInlineAsm(StringRef(Asm, Len));
459 }
460 
461 const char *LLVMGetModuleInlineAsm(LLVMModuleRef M, size_t *Len) {
462  auto &Str = unwrap(M)->getModuleInlineAsm();
463  *Len = Str.length();
464  return Str.c_str();
465 }
466 
468  size_t AsmStringSize, char *Constraints,
469  size_t ConstraintsSize, LLVMBool HasSideEffects,
470  LLVMBool IsAlignStack,
471  LLVMInlineAsmDialect Dialect, LLVMBool CanThrow) {
473  switch (Dialect) {
475  AD = InlineAsm::AD_ATT;
476  break;
478  AD = InlineAsm::AD_Intel;
479  break;
480  }
481  return wrap(InlineAsm::get(unwrap<FunctionType>(Ty),
482  StringRef(AsmString, AsmStringSize),
483  StringRef(Constraints, ConstraintsSize),
484  HasSideEffects, IsAlignStack, AD, CanThrow));
485 }
486 
487 /*--.. Operations on module contexts ......................................--*/
489  return wrap(&unwrap(M)->getContext());
490 }
491 
492 
493 /*===-- Operations on types -----------------------------------------------===*/
494 
495 /*--.. Operations on all types (mostly) ....................................--*/
496 
498  switch (unwrap(Ty)->getTypeID()) {
499  case Type::VoidTyID:
500  return LLVMVoidTypeKind;
501  case Type::HalfTyID:
502  return LLVMHalfTypeKind;
503  case Type::BFloatTyID:
504  return LLVMBFloatTypeKind;
505  case Type::FloatTyID:
506  return LLVMFloatTypeKind;
507  case Type::DoubleTyID:
508  return LLVMDoubleTypeKind;
509  case Type::X86_FP80TyID:
510  return LLVMX86_FP80TypeKind;
511  case Type::FP128TyID:
512  return LLVMFP128TypeKind;
513  case Type::PPC_FP128TyID:
514  return LLVMPPC_FP128TypeKind;
515  case Type::LabelTyID:
516  return LLVMLabelTypeKind;
517  case Type::MetadataTyID:
518  return LLVMMetadataTypeKind;
519  case Type::IntegerTyID:
520  return LLVMIntegerTypeKind;
521  case Type::FunctionTyID:
522  return LLVMFunctionTypeKind;
523  case Type::StructTyID:
524  return LLVMStructTypeKind;
525  case Type::ArrayTyID:
526  return LLVMArrayTypeKind;
527  case Type::PointerTyID:
528  return LLVMPointerTypeKind;
530  return LLVMVectorTypeKind;
531  case Type::X86_MMXTyID:
532  return LLVMX86_MMXTypeKind;
533  case Type::X86_AMXTyID:
534  return LLVMX86_AMXTypeKind;
535  case Type::TokenTyID:
536  return LLVMTokenTypeKind;
539  case Type::TargetExtTyID:
540  return LLVMTargetExtTypeKind;
542  llvm_unreachable("Typed pointers are unsupported via the C API");
543  }
544  llvm_unreachable("Unhandled TypeID.");
545 }
546 
548 {
549  return unwrap(Ty)->isSized();
550 }
551 
553  return wrap(&unwrap(Ty)->getContext());
554 }
555 
557  return unwrap(Ty)->print(errs(), /*IsForDebug=*/true);
558 }
559 
561  std::string buf;
562  raw_string_ostream os(buf);
563 
564  if (unwrap(Ty))
565  unwrap(Ty)->print(os);
566  else
567  os << "Printing <null> Type";
568 
569  os.flush();
570 
571  return strdup(buf.c_str());
572 }
573 
574 /*--.. Operations on integer types .........................................--*/
575 
577  return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
578 }
580  return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
581 }
583  return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
584 }
586  return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
587 }
589  return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
590 }
593 }
595  return wrap(IntegerType::get(*unwrap(C), NumBits));
596 }
597 
600 }
603 }
606 }
609 }
612 }
615 }
616 LLVMTypeRef LLVMIntType(unsigned NumBits) {
617  return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
618 }
619 
620 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
621  return unwrap<IntegerType>(IntegerTy)->getBitWidth();
622 }
623 
624 /*--.. Operations on real types ............................................--*/
625 
627  return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
628 }
631 }
633  return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
634 }
637 }
640 }
642  return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
643 }
646 }
649 }
652 }
653 
656 }
659 }
662 }
665 }
668 }
671 }
674 }
677 }
680 }
681 
682 /*--.. Operations on function types ........................................--*/
683 
685  LLVMTypeRef *ParamTypes, unsigned ParamCount,
686  LLVMBool IsVarArg) {
687  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
688  return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
689 }
690 
692  return unwrap<FunctionType>(FunctionTy)->isVarArg();
693 }
694 
696  return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
697 }
698 
699 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
700  return unwrap<FunctionType>(FunctionTy)->getNumParams();
701 }
702 
703 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
704  FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
705  for (Type *T : Ty->params())
706  *Dest++ = wrap(T);
707 }
708 
709 /*--.. Operations on struct types ..........................................--*/
710 
712  unsigned ElementCount, LLVMBool Packed) {
713  ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
714  return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
715 }
716 
718  unsigned ElementCount, LLVMBool Packed) {
719  return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
720  ElementCount, Packed);
721 }
722 
724 {
725  return wrap(StructType::create(*unwrap(C), Name));
726 }
727 
729 {
730  StructType *Type = unwrap<StructType>(Ty);
731  if (!Type->hasName())
732  return nullptr;
733  return Type->getName().data();
734 }
735 
736 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
737  unsigned ElementCount, LLVMBool Packed) {
738  ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
739  unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
740 }
741 
743  return unwrap<StructType>(StructTy)->getNumElements();
744 }
745 
747  StructType *Ty = unwrap<StructType>(StructTy);
748  for (Type *T : Ty->elements())
749  *Dest++ = wrap(T);
750 }
751 
753  StructType *Ty = unwrap<StructType>(StructTy);
754  return wrap(Ty->getTypeAtIndex(i));
755 }
756 
758  return unwrap<StructType>(StructTy)->isPacked();
759 }
760 
762  return unwrap<StructType>(StructTy)->isOpaque();
763 }
764 
766  return unwrap<StructType>(StructTy)->isLiteral();
767 }
768 
770  return wrap(StructType::getTypeByName(unwrap(M)->getContext(), Name));
771 }
772 
774  return wrap(StructType::getTypeByName(*unwrap(C), Name));
775 }
776 
777 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
778 
780  int i = 0;
781  for (auto *T : unwrap(Tp)->subtypes()) {
782  Arr[i] = wrap(T);
783  i++;
784  }
785 }
786 
788  return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
789 }
790 
792  return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
793 }
794 
796  return unwrap(Ty)->isOpaquePointerTy();
797 }
798 
800  return wrap(FixedVectorType::get(unwrap(ElementType), ElementCount));
801 }
802 
804  unsigned ElementCount) {
805  return wrap(ScalableVectorType::get(unwrap(ElementType), ElementCount));
806 }
807 
809  auto *Ty = unwrap(WrappedTy);
810  if (auto *PTy = dyn_cast<PointerType>(Ty))
811  return wrap(PTy->getNonOpaquePointerElementType());
812  if (auto *ATy = dyn_cast<ArrayType>(Ty))
813  return wrap(ATy->getElementType());
814  return wrap(cast<VectorType>(Ty)->getElementType());
815 }
816 
818  return unwrap(Tp)->getNumContainedTypes();
819 }
820 
821 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
822  return unwrap<ArrayType>(ArrayTy)->getNumElements();
823 }
824 
826  return unwrap<PointerType>(PointerTy)->getAddressSpace();
827 }
828 
829 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
830  return unwrap<VectorType>(VectorTy)->getElementCount().getKnownMinValue();
831 }
832 
833 /*--.. Operations on other types ...........................................--*/
834 
837 }
838 
840  return wrap(Type::getVoidTy(*unwrap(C)));
841 }
843  return wrap(Type::getLabelTy(*unwrap(C)));
844 }
846  return wrap(Type::getTokenTy(*unwrap(C)));
847 }
849  return wrap(Type::getMetadataTy(*unwrap(C)));
850 }
851 
854 }
857 }
858 
860  LLVMTypeRef *TypeParams,
861  unsigned TypeParamCount,
862  unsigned *IntParams,
863  unsigned IntParamCount) {
864  ArrayRef<Type *> TypeParamArray(unwrap(TypeParams), TypeParamCount);
865  ArrayRef<unsigned> IntParamArray(IntParams, IntParamCount);
866  return wrap(
867  TargetExtType::get(*unwrap(C), Name, TypeParamArray, IntParamArray));
868 }
869 
870 /*===-- Operations on values ----------------------------------------------===*/
871 
872 /*--.. Operations on all values ............................................--*/
873 
875  return wrap(unwrap(Val)->getType());
876 }
877 
879  switch(unwrap(Val)->getValueID()) {
880 #define LLVM_C_API 1
881 #define HANDLE_VALUE(Name) \
882  case Value::Name##Val: \
883  return LLVM##Name##ValueKind;
884 #include "llvm/IR/Value.def"
885  default:
887  }
888 }
889 
890 const char *LLVMGetValueName2(LLVMValueRef Val, size_t *Length) {
891  auto *V = unwrap(Val);
892  *Length = V->getName().size();
893  return V->getName().data();
894 }
895 
896 void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen) {
897  unwrap(Val)->setName(StringRef(Name, NameLen));
898 }
899 
900 const char *LLVMGetValueName(LLVMValueRef Val) {
901  return unwrap(Val)->getName().data();
902 }
903 
904 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
905  unwrap(Val)->setName(Name);
906 }
907 
909  unwrap(Val)->print(errs(), /*IsForDebug=*/true);
910 }
911 
913  std::string buf;
914  raw_string_ostream os(buf);
915 
916  if (unwrap(Val))
917  unwrap(Val)->print(os);
918  else
919  os << "Printing <null> Value";
920 
921  os.flush();
922 
923  return strdup(buf.c_str());
924 }
925 
927  unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
928 }
929 
931  return unwrap<Instruction>(Inst)->hasMetadata();
932 }
933 
934 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
935  auto *I = unwrap<Instruction>(Inst);
936  assert(I && "Expected instruction");
937  if (auto *MD = I->getMetadata(KindID))
938  return wrap(MetadataAsValue::get(I->getContext(), MD));
939  return nullptr;
940 }
941 
942 // MetadataAsValue uses a canonical format which strips the actual MDNode for
943 // MDNode with just a single constant value, storing just a ConstantAsMetadata
944 // This undoes this canonicalization, reconstructing the MDNode.
946  Metadata *MD = MAV->getMetadata();
947  assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
948  "Expected a metadata node or a canonicalized constant");
949 
950  if (MDNode *N = dyn_cast<MDNode>(MD))
951  return N;
952 
953  return MDNode::get(MAV->getContext(), MD);
954 }
955 
956 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
957  MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
958 
959  unwrap<Instruction>(Inst)->setMetadata(KindID, N);
960 }
961 
963  unsigned Kind;
965 };
966 
968 static LLVMValueMetadataEntry *
969 llvm_getMetadata(size_t *NumEntries,
970  llvm::function_ref<void(MetadataEntries &)> AccessMD) {
972  AccessMD(MVEs);
973 
975  static_cast<LLVMOpaqueValueMetadataEntry *>(
976  safe_malloc(MVEs.size() * sizeof(LLVMOpaqueValueMetadataEntry)));
977  for (unsigned i = 0; i < MVEs.size(); ++i) {
978  const auto &ModuleFlag = MVEs[i];
979  Result[i].Kind = ModuleFlag.first;
980  Result[i].Metadata = wrap(ModuleFlag.second);
981  }
982  *NumEntries = MVEs.size();
983  return Result;
984 }
985 
988  size_t *NumEntries) {
989  return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
990  Entries.clear();
991  unwrap<Instruction>(Value)->getAllMetadata(Entries);
992  });
993 }
994 
995 /*--.. Conversion functions ................................................--*/
996 
997 #define LLVM_DEFINE_VALUE_CAST(name) \
998  LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
999  return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
1000  }
1001 
1003 
1005  if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
1006  if (isa<MDNode>(MD->getMetadata()) ||
1007  isa<ValueAsMetadata>(MD->getMetadata()))
1008  return Val;
1009  return nullptr;
1010 }
1011 
1013  if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
1014  if (isa<MDString>(MD->getMetadata()))
1015  return Val;
1016  return nullptr;
1017 }
1018 
1019 /*--.. Operations on Uses ..................................................--*/
1021  Value *V = unwrap(Val);
1023  if (I == V->use_end())
1024  return nullptr;
1025  return wrap(&*I);
1026 }
1027 
1029  Use *Next = unwrap(U)->getNext();
1030  if (Next)
1031  return wrap(Next);
1032  return nullptr;
1033 }
1034 
1036  return wrap(unwrap(U)->getUser());
1037 }
1038 
1040  return wrap(unwrap(U)->get());
1041 }
1042 
1043 /*--.. Operations on Users .................................................--*/
1044 
1046  unsigned Index) {
1047  Metadata *Op = N->getOperand(Index);
1048  if (!Op)
1049  return nullptr;
1050  if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
1051  return wrap(C->getValue());
1052  return wrap(MetadataAsValue::get(Context, Op));
1053 }
1054 
1056  Value *V = unwrap(Val);
1057  if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
1058  if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1059  assert(Index == 0 && "Function-local metadata can only have one operand");
1060  return wrap(L->getValue());
1061  }
1062  return getMDNodeOperandImpl(V->getContext(),
1063  cast<MDNode>(MD->getMetadata()), Index);
1064  }
1065 
1066  return wrap(cast<User>(V)->getOperand(Index));
1067 }
1068 
1070  Value *V = unwrap(Val);
1071  return wrap(&cast<User>(V)->getOperandUse(Index));
1072 }
1073 
1075  unwrap<User>(Val)->setOperand(Index, unwrap(Op));
1076 }
1077 
1079  Value *V = unwrap(Val);
1080  if (isa<MetadataAsValue>(V))
1081  return LLVMGetMDNodeNumOperands(Val);
1082 
1083  return cast<User>(V)->getNumOperands();
1084 }
1085 
1086 /*--.. Operations on constants of any type .................................--*/
1087 
1089  return wrap(Constant::getNullValue(unwrap(Ty)));
1090 }
1091 
1093  return wrap(Constant::getAllOnesValue(unwrap(Ty)));
1094 }
1095 
1097  return wrap(UndefValue::get(unwrap(Ty)));
1098 }
1099 
1101  return wrap(PoisonValue::get(unwrap(Ty)));
1102 }
1103 
1105  return isa<Constant>(unwrap(Ty));
1106 }
1107 
1109  if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
1110  return C->isNullValue();
1111  return false;
1112 }
1113 
1115  return isa<UndefValue>(unwrap(Val));
1116 }
1117 
1119  return isa<PoisonValue>(unwrap(Val));
1120 }
1121 
1123  return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
1124 }
1125 
1126 /*--.. Operations on metadata nodes ........................................--*/
1127 
1129  size_t SLen) {
1130  return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
1131 }
1132 
1134  size_t Count) {
1135  return wrap(MDNode::get(*unwrap(C), ArrayRef<Metadata*>(unwrap(MDs), Count)));
1136 }
1137 
1139  unsigned SLen) {
1140  LLVMContext &Context = *unwrap(C);
1141  return wrap(MetadataAsValue::get(
1142  Context, MDString::get(Context, StringRef(Str, SLen))));
1143 }
1144 
1145 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
1146  return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
1147 }
1148 
1150  unsigned Count) {
1151  LLVMContext &Context = *unwrap(C);
1153  for (auto *OV : ArrayRef(Vals, Count)) {
1154  Value *V = unwrap(OV);
1155  Metadata *MD;
1156  if (!V)
1157  MD = nullptr;
1158  else if (auto *C = dyn_cast<Constant>(V))
1159  MD = ConstantAsMetadata::get(C);
1160  else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
1161  MD = MDV->getMetadata();
1162  assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
1163  "outside of direct argument to call");
1164  } else {
1165  // This is function-local metadata. Pretend to make an MDNode.
1166  assert(Count == 1 &&
1167  "Expected only one operand to function-local metadata");
1169  }
1170 
1171  MDs.push_back(MD);
1172  }
1174 }
1175 
1176 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
1177  return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
1178 }
1179 
1181  return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
1182 }
1183 
1185  auto *V = unwrap(Val);
1186  if (auto *C = dyn_cast<Constant>(V))
1187  return wrap(ConstantAsMetadata::get(C));
1188  if (auto *MAV = dyn_cast<MetadataAsValue>(V))
1189  return wrap(MAV->getMetadata());
1190  return wrap(ValueAsMetadata::get(V));
1191 }
1192 
1193 const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
1194  if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
1195  if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
1196  *Length = S->getString().size();
1197  return S->getString().data();
1198  }
1199  *Length = 0;
1200  return nullptr;
1201 }
1202 
1204  auto *MD = unwrap<MetadataAsValue>(V);
1205  if (isa<ValueAsMetadata>(MD->getMetadata()))
1206  return 1;
1207  return cast<MDNode>(MD->getMetadata())->getNumOperands();
1208 }
1209 
1211  Module *Mod = unwrap(M);
1213  if (I == Mod->named_metadata_end())
1214  return nullptr;
1215  return wrap(&*I);
1216 }
1217 
1219  Module *Mod = unwrap(M);
1221  if (I == Mod->named_metadata_begin())
1222  return nullptr;
1223  return wrap(&*--I);
1224 }
1225 
1227  NamedMDNode *NamedNode = unwrap(NMD);
1229  if (++I == NamedNode->getParent()->named_metadata_end())
1230  return nullptr;
1231  return wrap(&*I);
1232 }
1233 
1235  NamedMDNode *NamedNode = unwrap(NMD);
1237  if (I == NamedNode->getParent()->named_metadata_begin())
1238  return nullptr;
1239  return wrap(&*--I);
1240 }
1241 
1243  const char *Name, size_t NameLen) {
1244  return wrap(unwrap(M)->getNamedMetadata(StringRef(Name, NameLen)));
1245 }
1246 
1248  const char *Name, size_t NameLen) {
1249  return wrap(unwrap(M)->getOrInsertNamedMetadata({Name, NameLen}));
1250 }
1251 
1252 const char *LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NMD, size_t *NameLen) {
1253  NamedMDNode *NamedNode = unwrap(NMD);
1254  *NameLen = NamedNode->getName().size();
1255  return NamedNode->getName().data();
1256 }
1257 
1259  auto *MD = unwrap<MetadataAsValue>(V);
1260  if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1261  *Dest = wrap(MDV->getValue());
1262  return;
1263  }
1264  const auto *N = cast<MDNode>(MD->getMetadata());
1265  const unsigned numOperands = N->getNumOperands();
1266  LLVMContext &Context = unwrap(V)->getContext();
1267  for (unsigned i = 0; i < numOperands; i++)
1268  Dest[i] = getMDNodeOperandImpl(Context, N, i);
1269 }
1270 
1271 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char *Name) {
1272  if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
1273  return N->getNumOperands();
1274  }
1275  return 0;
1276 }
1277 
1279  LLVMValueRef *Dest) {
1280  NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
1281  if (!N)
1282  return;
1283  LLVMContext &Context = unwrap(M)->getContext();
1284  for (unsigned i=0;i<N->getNumOperands();i++)
1285  Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
1286 }
1287 
1289  LLVMValueRef Val) {
1290  NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
1291  if (!N)
1292  return;
1293  if (!Val)
1294  return;
1295  N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
1296 }
1297 
1298 const char *LLVMGetDebugLocDirectory(LLVMValueRef Val, unsigned *Length) {
1299  if (!Length) return nullptr;
1300  StringRef S;
1301  if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1302  if (const auto &DL = I->getDebugLoc()) {
1303  S = DL->getDirectory();
1304  }
1305  } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1307  GV->getDebugInfo(GVEs);
1308  if (GVEs.size())
1309  if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1310  S = DGV->getDirectory();
1311  } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1312  if (const DISubprogram *DSP = F->getSubprogram())
1313  S = DSP->getDirectory();
1314  } else {
1315  assert(0 && "Expected Instruction, GlobalVariable or Function");
1316  return nullptr;
1317  }
1318  *Length = S.size();
1319  return S.data();
1320 }
1321 
1322 const char *LLVMGetDebugLocFilename(LLVMValueRef Val, unsigned *Length) {
1323  if (!Length) return nullptr;
1324  StringRef S;
1325  if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1326  if (const auto &DL = I->getDebugLoc()) {
1327  S = DL->getFilename();
1328  }
1329  } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1331  GV->getDebugInfo(GVEs);
1332  if (GVEs.size())
1333  if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1334  S = DGV->getFilename();
1335  } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1336  if (const DISubprogram *DSP = F->getSubprogram())
1337  S = DSP->getFilename();
1338  } else {
1339  assert(0 && "Expected Instruction, GlobalVariable or Function");
1340  return nullptr;
1341  }
1342  *Length = S.size();
1343  return S.data();
1344 }
1345 
1347  unsigned L = 0;
1348  if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1349  if (const auto &DL = I->getDebugLoc()) {
1350  L = DL->getLine();
1351  }
1352  } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1354  GV->getDebugInfo(GVEs);
1355  if (GVEs.size())
1356  if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1357  L = DGV->getLine();
1358  } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1359  if (const DISubprogram *DSP = F->getSubprogram())
1360  L = DSP->getLine();
1361  } else {
1362  assert(0 && "Expected Instruction, GlobalVariable or Function");
1363  return -1;
1364  }
1365  return L;
1366 }
1367 
1369  unsigned C = 0;
1370  if (const auto *I = dyn_cast<Instruction>(unwrap(Val)))
1371  if (const auto &DL = I->getDebugLoc())
1372  C = DL->getColumn();
1373  return C;
1374 }
1375 
1376 /*--.. Operations on scalar constants ......................................--*/
1377 
1378 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
1379  LLVMBool SignExtend) {
1380  return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
1381 }
1382 
1384  unsigned NumWords,
1385  const uint64_t Words[]) {
1386  IntegerType *Ty = unwrap<IntegerType>(IntTy);
1387  return wrap(ConstantInt::get(
1388  Ty->getContext(), APInt(Ty->getBitWidth(), ArrayRef(Words, NumWords))));
1389 }
1390 
1392  uint8_t Radix) {
1393  return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
1394  Radix));
1395 }
1396 
1398  unsigned SLen, uint8_t Radix) {
1399  return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
1400  Radix));
1401 }
1402 
1404  return wrap(ConstantFP::get(unwrap(RealTy), N));
1405 }
1406 
1408  return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
1409 }
1410 
1412  unsigned SLen) {
1413  return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
1414 }
1415 
1416 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
1417  return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
1418 }
1419 
1420 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
1421  return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
1422 }
1423 
1424 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
1425  ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
1426  Type *Ty = cFP->getType();
1427 
1428  if (Ty->isHalfTy() || Ty->isBFloatTy() || Ty->isFloatTy() ||
1429  Ty->isDoubleTy()) {
1430  *LosesInfo = false;
1431  return cFP->getValueAPF().convertToDouble();
1432  }
1433 
1434  bool APFLosesInfo;
1435  APFloat APF = cFP->getValueAPF();
1437  *LosesInfo = APFLosesInfo;
1438  return APF.convertToDouble();
1439 }
1440 
1441 /*--.. Operations on composite constants ...................................--*/
1442 
1444  unsigned Length,
1445  LLVMBool DontNullTerminate) {
1446  /* Inverted the sense of AddNull because ', 0)' is a
1447  better mnemonic for null termination than ', 1)'. */
1449  DontNullTerminate == 0));
1450 }
1451 
1452 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
1453  LLVMBool DontNullTerminate) {
1455  DontNullTerminate);
1456 }
1457 
1459  return wrap(unwrap<Constant>(C)->getAggregateElement(Idx));
1460 }
1461 
1463  return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
1464 }
1465 
1467  return unwrap<ConstantDataSequential>(C)->isString();
1468 }
1469 
1470 const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1471  StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1472  *Length = Str.size();
1473  return Str.data();
1474 }
1475 
1477  LLVMValueRef *ConstantVals, unsigned Length) {
1478  ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1479  return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1480 }
1481 
1483  LLVMValueRef *ConstantVals,
1484  unsigned Count, LLVMBool Packed) {
1485  Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1486  return wrap(ConstantStruct::getAnon(*unwrap(C), ArrayRef(Elements, Count),
1487  Packed != 0));
1488 }
1489 
1490 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
1491  LLVMBool Packed) {
1492  return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1493  Packed);
1494 }
1495 
1497  LLVMValueRef *ConstantVals,
1498  unsigned Count) {
1499  Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1500  StructType *Ty = unwrap<StructType>(StructTy);
1501 
1502  return wrap(ConstantStruct::get(Ty, ArrayRef(Elements, Count)));
1503 }
1504 
1505 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
1506  return wrap(ConstantVector::get(
1507  ArrayRef(unwrap<Constant>(ScalarConstantVals, Size), Size)));
1508 }
1509 
1510 /*-- Opcode mapping */
1511 
1512 static LLVMOpcode map_to_llvmopcode(int opcode)
1513 {
1514  switch (opcode) {
1515  default: llvm_unreachable("Unhandled Opcode.");
1516 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
1517 #include "llvm/IR/Instruction.def"
1518 #undef HANDLE_INST
1519  }
1520 }
1521 
1523 {
1524  switch (code) {
1525 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
1526 #include "llvm/IR/Instruction.def"
1527 #undef HANDLE_INST
1528  }
1529  llvm_unreachable("Unhandled Opcode.");
1530 }
1531 
1532 /*--.. Constant expressions ................................................--*/
1533 
1535  return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
1536 }
1537 
1539  return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
1540 }
1541 
1543  return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
1544 }
1545 
1547  return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1548 }
1549 
1551  return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
1552 }
1553 
1555  return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
1556 }
1557 
1558 
1560  return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
1561 }
1562 
1564  return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
1565  unwrap<Constant>(RHSConstant)));
1566 }
1567 
1569  LLVMValueRef RHSConstant) {
1570  return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
1571  unwrap<Constant>(RHSConstant)));
1572 }
1573 
1575  LLVMValueRef RHSConstant) {
1576  return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
1577  unwrap<Constant>(RHSConstant)));
1578 }
1579 
1581  return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
1582  unwrap<Constant>(RHSConstant)));
1583 }
1584 
1586  LLVMValueRef RHSConstant) {
1587  return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
1588  unwrap<Constant>(RHSConstant)));
1589 }
1590 
1592  LLVMValueRef RHSConstant) {
1593  return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
1594  unwrap<Constant>(RHSConstant)));
1595 }
1596 
1598  return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
1599  unwrap<Constant>(RHSConstant)));
1600 }
1601 
1603  LLVMValueRef RHSConstant) {
1604  return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
1605  unwrap<Constant>(RHSConstant)));
1606 }
1607 
1609  LLVMValueRef RHSConstant) {
1610  return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
1611  unwrap<Constant>(RHSConstant)));
1612 }
1613 
1615  return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
1616  unwrap<Constant>(RHSConstant)));
1617 }
1618 
1620  return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
1621  unwrap<Constant>(RHSConstant)));
1622 }
1623 
1625  return wrap(ConstantExpr::getXor(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  LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1639  unwrap<Constant>(LHSConstant),
1640  unwrap<Constant>(RHSConstant)));
1641 }
1642 
1644  return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1645  unwrap<Constant>(RHSConstant)));
1646 }
1647 
1649  return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
1650  unwrap<Constant>(RHSConstant)));
1651 }
1652 
1654  return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1655  unwrap<Constant>(RHSConstant)));
1656 }
1657 
1659  LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1660  ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1661  NumIndices);
1662  Constant *Val = unwrap<Constant>(ConstantVal);
1663  return wrap(ConstantExpr::getGetElementPtr(unwrap(Ty), Val, IdxList));
1664 }
1665 
1667  LLVMValueRef *ConstantIndices,
1668  unsigned NumIndices) {
1669  ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1670  NumIndices);
1671  Constant *Val = unwrap<Constant>(ConstantVal);
1672  return wrap(ConstantExpr::getInBoundsGetElementPtr(unwrap(Ty), Val, IdxList));
1673 }
1674 
1676  return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1677  unwrap(ToType)));
1678 }
1679 
1681  return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1682  unwrap(ToType)));
1683 }
1684 
1686  return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1687  unwrap(ToType)));
1688 }
1689 
1691  return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1692  unwrap(ToType)));
1693 }
1694 
1696  return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1697  unwrap(ToType)));
1698 }
1699 
1701  return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1702  unwrap(ToType)));
1703 }
1704 
1706  return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1707  unwrap(ToType)));
1708 }
1709 
1711  return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1712  unwrap(ToType)));
1713 }
1714 
1716  return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1717  unwrap(ToType)));
1718 }
1719 
1721  return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1722  unwrap(ToType)));
1723 }
1724 
1726  return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1727  unwrap(ToType)));
1728 }
1729 
1731  return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1732  unwrap(ToType)));
1733 }
1734 
1736  LLVMTypeRef ToType) {
1737  return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1738  unwrap(ToType)));
1739 }
1740 
1742  LLVMTypeRef ToType) {
1743  return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1744  unwrap(ToType)));
1745 }
1746 
1748  LLVMTypeRef ToType) {
1749  return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1750  unwrap(ToType)));
1751 }
1752 
1754  LLVMTypeRef ToType) {
1755  return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1756  unwrap(ToType)));
1757 }
1758 
1760  LLVMTypeRef ToType) {
1761  return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1762  unwrap(ToType)));
1763 }
1764 
1766  LLVMBool isSigned) {
1767  return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1768  unwrap(ToType), isSigned));
1769 }
1770 
1772  return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1773  unwrap(ToType)));
1774 }
1775 
1777  LLVMValueRef ConstantIfTrue,
1778  LLVMValueRef ConstantIfFalse) {
1779  return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1780  unwrap<Constant>(ConstantIfTrue),
1781  unwrap<Constant>(ConstantIfFalse)));
1782 }
1783 
1785  LLVMValueRef IndexConstant) {
1786  return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1787  unwrap<Constant>(IndexConstant)));
1788 }
1789 
1791  LLVMValueRef ElementValueConstant,
1792  LLVMValueRef IndexConstant) {
1793  return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1794  unwrap<Constant>(ElementValueConstant),
1795  unwrap<Constant>(IndexConstant)));
1796 }
1797 
1799  LLVMValueRef VectorBConstant,
1800  LLVMValueRef MaskConstant) {
1801  SmallVector<int, 16> IntMask;
1802  ShuffleVectorInst::getShuffleMask(unwrap<Constant>(MaskConstant), IntMask);
1803  return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1804  unwrap<Constant>(VectorBConstant),
1805  IntMask));
1806 }
1807 
1808 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1809  const char *Constraints,
1810  LLVMBool HasSideEffects,
1811  LLVMBool IsAlignStack) {
1812  return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1813  Constraints, HasSideEffects, IsAlignStack));
1814 }
1815 
1817  return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1818 }
1819 
1820 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1821 
1823  return wrap(unwrap<GlobalValue>(Global)->getParent());
1824 }
1825 
1827  return unwrap<GlobalValue>(Global)->isDeclaration();
1828 }
1829 
1831  switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1833  return LLVMExternalLinkage;
1837  return LLVMLinkOnceAnyLinkage;
1839  return LLVMLinkOnceODRLinkage;
1841  return LLVMWeakAnyLinkage;
1843  return LLVMWeakODRLinkage;
1845  return LLVMAppendingLinkage;
1847  return LLVMInternalLinkage;
1849  return LLVMPrivateLinkage;
1851  return LLVMExternalWeakLinkage;
1853  return LLVMCommonLinkage;
1854  }
1855 
1856  llvm_unreachable("Invalid GlobalValue linkage!");
1857 }
1858 
1859 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1860  GlobalValue *GV = unwrap<GlobalValue>(Global);
1861 
1862  switch (Linkage) {
1863  case LLVMExternalLinkage:
1865  break;
1868  break;
1871  break;
1874  break;
1876  LLVM_DEBUG(
1877  errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1878  "longer supported.");
1879  break;
1880  case LLVMWeakAnyLinkage:
1882  break;
1883  case LLVMWeakODRLinkage:
1885  break;
1886  case LLVMAppendingLinkage:
1888  break;
1889  case LLVMInternalLinkage:
1891  break;
1892  case LLVMPrivateLinkage:
1894  break;
1897  break;
1900  break;
1901  case LLVMDLLImportLinkage:
1902  LLVM_DEBUG(
1903  errs()
1904  << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1905  break;
1906  case LLVMDLLExportLinkage:
1907  LLVM_DEBUG(
1908  errs()
1909  << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1910  break;
1913  break;
1914  case LLVMGhostLinkage:
1915  LLVM_DEBUG(
1916  errs() << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1917  break;
1918  case LLVMCommonLinkage:
1920  break;
1921  }
1922 }
1923 
1924 const char *LLVMGetSection(LLVMValueRef Global) {
1925  // Using .data() is safe because of how GlobalObject::setSection is
1926  // implemented.
1927  return unwrap<GlobalValue>(Global)->getSection().data();
1928 }
1929 
1930 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1931  unwrap<GlobalObject>(Global)->setSection(Section);
1932 }
1933 
1935  return static_cast<LLVMVisibility>(
1936  unwrap<GlobalValue>(Global)->getVisibility());
1937 }
1938 
1940  unwrap<GlobalValue>(Global)
1941  ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1942 }
1943 
1945  return static_cast<LLVMDLLStorageClass>(
1946  unwrap<GlobalValue>(Global)->getDLLStorageClass());
1947 }
1948 
1950  unwrap<GlobalValue>(Global)->setDLLStorageClass(
1951  static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1952 }
1953 
1955  switch (unwrap<GlobalValue>(Global)->getUnnamedAddr()) {
1957  return LLVMNoUnnamedAddr;
1959  return LLVMLocalUnnamedAddr;
1961  return LLVMGlobalUnnamedAddr;
1962  }
1963  llvm_unreachable("Unknown UnnamedAddr kind!");
1964 }
1965 
1967  GlobalValue *GV = unwrap<GlobalValue>(Global);
1968 
1969  switch (UnnamedAddr) {
1970  case LLVMNoUnnamedAddr:
1972  case LLVMLocalUnnamedAddr:
1974  case LLVMGlobalUnnamedAddr:
1976  }
1977 }
1978 
1980  return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
1981 }
1982 
1983 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
1984  unwrap<GlobalValue>(Global)->setUnnamedAddr(
1985  HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
1987 }
1988 
1990  return wrap(unwrap<GlobalValue>(Global)->getValueType());
1991 }
1992 
1993 /*--.. Operations on global variables, load and store instructions .........--*/
1994 
1996  Value *P = unwrap(V);
1997  if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
1998  return GV->getAlign() ? GV->getAlign()->value() : 0;
1999  if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
2000  return AI->getAlign().value();
2001  if (LoadInst *LI = dyn_cast<LoadInst>(P))
2002  return LI->getAlign().value();
2003  if (StoreInst *SI = dyn_cast<StoreInst>(P))
2004  return SI->getAlign().value();
2005  if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2006  return RMWI->getAlign().value();
2007  if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2008  return CXI->getAlign().value();
2009 
2011  "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, "
2012  "and AtomicCmpXchgInst have alignment");
2013 }
2014 
2015 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
2016  Value *P = unwrap(V);
2017  if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2018  GV->setAlignment(MaybeAlign(Bytes));
2019  else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
2020  AI->setAlignment(Align(Bytes));
2021  else if (LoadInst *LI = dyn_cast<LoadInst>(P))
2022  LI->setAlignment(Align(Bytes));
2023  else if (StoreInst *SI = dyn_cast<StoreInst>(P))
2024  SI->setAlignment(Align(Bytes));
2025  else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2026  RMWI->setAlignment(Align(Bytes));
2027  else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2028  CXI->setAlignment(Align(Bytes));
2029  else
2031  "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, and "
2032  "and AtomicCmpXchgInst have alignment");
2033 }
2034 
2036  size_t *NumEntries) {
2037  return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
2038  Entries.clear();
2039  if (Instruction *Instr = dyn_cast<Instruction>(unwrap(Value))) {
2040  Instr->getAllMetadata(Entries);
2041  } else {
2042  unwrap<GlobalObject>(Value)->getAllMetadata(Entries);
2043  }
2044  });
2045 }
2046 
2048  unsigned Index) {
2050  static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2051  return MVE.Kind;
2052 }
2053 
2056  unsigned Index) {
2058  static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2059  return MVE.Metadata;
2060 }
2061 
2063  free(Entries);
2064 }
2065 
2066 void LLVMGlobalSetMetadata(LLVMValueRef Global, unsigned Kind,
2067  LLVMMetadataRef MD) {
2068  unwrap<GlobalObject>(Global)->setMetadata(Kind, unwrap<MDNode>(MD));
2069 }
2070 
2071 void LLVMGlobalEraseMetadata(LLVMValueRef Global, unsigned Kind) {
2072  unwrap<GlobalObject>(Global)->eraseMetadata(Kind);
2073 }
2074 
2076  unwrap<GlobalObject>(Global)->clearMetadata();
2077 }
2078 
2079 /*--.. Operations on global variables ......................................--*/
2080 
2082  return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2083  GlobalValue::ExternalLinkage, nullptr, Name));
2084 }
2085 
2087  const char *Name,
2088  unsigned AddressSpace) {
2089  return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2090  GlobalValue::ExternalLinkage, nullptr, Name,
2092  AddressSpace));
2093 }
2094 
2096  return wrap(unwrap(M)->getNamedGlobal(Name));
2097 }
2098 
2100  Module *Mod = unwrap(M);
2102  if (I == Mod->global_end())
2103  return nullptr;
2104  return wrap(&*I);
2105 }
2106 
2108  Module *Mod = unwrap(M);
2110  if (I == Mod->global_begin())
2111  return nullptr;
2112  return wrap(&*--I);
2113 }
2114 
2116  GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2118  if (++I == GV->getParent()->global_end())
2119  return nullptr;
2120  return wrap(&*I);
2121 }
2122 
2124  GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2126  if (I == GV->getParent()->global_begin())
2127  return nullptr;
2128  return wrap(&*--I);
2129 }
2130 
2132  unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
2133 }
2134 
2136  GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
2137  if ( !GV->hasInitializer() )
2138  return nullptr;
2139  return wrap(GV->getInitializer());
2140 }
2141 
2143  unwrap<GlobalVariable>(GlobalVar)
2144  ->setInitializer(unwrap<Constant>(ConstantVal));
2145 }
2146 
2148  return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
2149 }
2150 
2152  unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
2153 }
2154 
2156  return unwrap<GlobalVariable>(GlobalVar)->isConstant();
2157 }
2158 
2160  unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
2161 }
2162 
2164  switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
2166  return LLVMNotThreadLocal;
2170  return LLVMLocalDynamicTLSModel;
2172  return LLVMInitialExecTLSModel;
2174  return LLVMLocalExecTLSModel;
2175  }
2176 
2177  llvm_unreachable("Invalid GlobalVariable thread local mode");
2178 }
2179 
2181  GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2182 
2183  switch (Mode) {
2184  case LLVMNotThreadLocal:
2186  break;
2189  break;
2192  break;
2195  break;
2196  case LLVMLocalExecTLSModel:
2198  break;
2199  }
2200 }
2201 
2203  return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
2204 }
2205 
2207  unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
2208 }
2209 
2210 /*--.. Operations on aliases ......................................--*/
2211 
2213  unsigned AddrSpace, LLVMValueRef Aliasee,
2214  const char *Name) {
2215  return wrap(GlobalAlias::create(unwrap(ValueTy), AddrSpace,
2217  unwrap<Constant>(Aliasee), unwrap(M)));
2218 }
2219 
2221  const char *Name, size_t NameLen) {
2222  return wrap(unwrap(M)->getNamedAlias(StringRef(Name, NameLen)));
2223 }
2224 
2226  Module *Mod = unwrap(M);
2228  if (I == Mod->alias_end())
2229  return nullptr;
2230  return wrap(&*I);
2231 }
2232 
2234  Module *Mod = unwrap(M);
2236  if (I == Mod->alias_begin())
2237  return nullptr;
2238  return wrap(&*--I);
2239 }
2240 
2242  GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2243  Module::alias_iterator I(Alias);
2244  if (++I == Alias->getParent()->alias_end())
2245  return nullptr;
2246  return wrap(&*I);
2247 }
2248 
2250  GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2251  Module::alias_iterator I(Alias);
2252  if (I == Alias->getParent()->alias_begin())
2253  return nullptr;
2254  return wrap(&*--I);
2255 }
2256 
2258  return wrap(unwrap<GlobalAlias>(Alias)->getAliasee());
2259 }
2260 
2262  unwrap<GlobalAlias>(Alias)->setAliasee(unwrap<Constant>(Aliasee));
2263 }
2264 
2265 /*--.. Operations on functions .............................................--*/
2266 
2268  LLVMTypeRef FunctionTy) {
2269  return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
2271 }
2272 
2274  return wrap(unwrap(M)->getFunction(Name));
2275 }
2276 
2278  Module *Mod = unwrap(M);
2279  Module::iterator I = Mod->begin();
2280  if (I == Mod->end())
2281  return nullptr;
2282  return wrap(&*I);
2283 }
2284 
2286  Module *Mod = unwrap(M);
2287  Module::iterator I = Mod->end();
2288  if (I == Mod->begin())
2289  return nullptr;
2290  return wrap(&*--I);
2291 }
2292 
2294  Function *Func = unwrap<Function>(Fn);
2295  Module::iterator I(Func);
2296  if (++I == Func->getParent()->end())
2297  return nullptr;
2298  return wrap(&*I);
2299 }
2300 
2302  Function *Func = unwrap<Function>(Fn);
2303  Module::iterator I(Func);
2304  if (I == Func->getParent()->begin())
2305  return nullptr;
2306  return wrap(&*--I);
2307 }
2308 
2310  unwrap<Function>(Fn)->eraseFromParent();
2311 }
2312 
2314  return unwrap<Function>(Fn)->hasPersonalityFn();
2315 }
2316 
2318  return wrap(unwrap<Function>(Fn)->getPersonalityFn());
2319 }
2320 
2322  unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
2323 }
2324 
2326  if (Function *F = dyn_cast<Function>(unwrap(Fn)))
2327  return F->getIntrinsicID();
2328  return 0;
2329 }
2330 
2332  assert(ID < llvm::Intrinsic::num_intrinsics && "Intrinsic ID out of range");
2333  return llvm::Intrinsic::ID(ID);
2334 }
2335 
2337  unsigned ID,
2338  LLVMTypeRef *ParamTypes,
2339  size_t ParamCount) {
2340  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2341  auto IID = llvm_map_to_intrinsic_id(ID);
2342  return wrap(llvm::Intrinsic::getDeclaration(unwrap(Mod), IID, Tys));
2343 }
2344 
2345 const char *LLVMIntrinsicGetName(unsigned ID, size_t *NameLength) {
2346  auto IID = llvm_map_to_intrinsic_id(ID);
2347  auto Str = llvm::Intrinsic::getName(IID);
2348  *NameLength = Str.size();
2349  return Str.data();
2350 }
2351 
2353  LLVMTypeRef *ParamTypes, size_t ParamCount) {
2354  auto IID = llvm_map_to_intrinsic_id(ID);
2355  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2356  return wrap(llvm::Intrinsic::getType(*unwrap(Ctx), IID, Tys));
2357 }
2358 
2360  LLVMTypeRef *ParamTypes,
2361  size_t ParamCount,
2362  size_t *NameLength) {
2363  auto IID = llvm_map_to_intrinsic_id(ID);
2364  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2365  auto Str = llvm::Intrinsic::getNameNoUnnamedTypes(IID, Tys);
2366  *NameLength = Str.length();
2367  return strdup(Str.c_str());
2368 }
2369 
2371  LLVMTypeRef *ParamTypes,
2372  size_t ParamCount,
2373  size_t *NameLength) {
2374  auto IID = llvm_map_to_intrinsic_id(ID);
2375  ArrayRef<Type *> Tys(unwrap(ParamTypes), ParamCount);
2376  auto Str = llvm::Intrinsic::getName(IID, Tys, unwrap(Mod));
2377  *NameLength = Str.length();
2378  return strdup(Str.c_str());
2379 }
2380 
2381 unsigned LLVMLookupIntrinsicID(const char *Name, size_t NameLen) {
2382  return Function::lookupIntrinsicID({Name, NameLen});
2383 }
2384 
2386  auto IID = llvm_map_to_intrinsic_id(ID);
2387  return llvm::Intrinsic::isOverloaded(IID);
2388 }
2389 
2391  return unwrap<Function>(Fn)->getCallingConv();
2392 }
2393 
2395  return unwrap<Function>(Fn)->setCallingConv(
2396  static_cast<CallingConv::ID>(CC));
2397 }
2398 
2399 const char *LLVMGetGC(LLVMValueRef Fn) {
2400  Function *F = unwrap<Function>(Fn);
2401  return F->hasGC()? F->getGC().c_str() : nullptr;
2402 }
2403 
2404 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
2405  Function *F = unwrap<Function>(Fn);
2406  if (GC)
2407  F->setGC(GC);
2408  else
2409  F->clearGC();
2410 }
2411 
2413  LLVMAttributeRef A) {
2414  unwrap<Function>(F)->addAttributeAtIndex(Idx, unwrap(A));
2415 }
2416 
2418  auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2419  return AS.getNumAttributes();
2420 }
2421 
2424  auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2425  for (auto A : AS)
2426  *Attrs++ = wrap(A);
2427 }
2428 
2430  LLVMAttributeIndex Idx,
2431  unsigned KindID) {
2432  return wrap(unwrap<Function>(F)->getAttributeAtIndex(
2433  Idx, (Attribute::AttrKind)KindID));
2434 }
2435 
2437  LLVMAttributeIndex Idx,
2438  const char *K, unsigned KLen) {
2439  return wrap(
2440  unwrap<Function>(F)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
2441 }
2442 
2444  unsigned KindID) {
2445  unwrap<Function>(F)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
2446 }
2447 
2449  const char *K, unsigned KLen) {
2450  unwrap<Function>(F)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
2451 }
2452 
2454  const char *V) {
2455  Function *Func = unwrap<Function>(Fn);
2456  Attribute Attr = Attribute::get(Func->getContext(), A, V);
2457  Func->addFnAttr(Attr);
2458 }
2459 
2460 /*--.. Operations on parameters ............................................--*/
2461 
2463  // This function is strictly redundant to
2464  // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
2465  return unwrap<Function>(FnRef)->arg_size();
2466 }
2467 
2468 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
2469  Function *Fn = unwrap<Function>(FnRef);
2470  for (Argument &A : Fn->args())
2471  *ParamRefs++ = wrap(&A);
2472 }
2473 
2475  Function *Fn = unwrap<Function>(FnRef);
2476  return wrap(&Fn->arg_begin()[index]);
2477 }
2478 
2480  return wrap(unwrap<Argument>(V)->getParent());
2481 }
2482 
2484  Function *Func = unwrap<Function>(Fn);
2485  Function::arg_iterator I = Func->arg_begin();
2486  if (I == Func->arg_end())
2487  return nullptr;
2488  return wrap(&*I);
2489 }
2490 
2492  Function *Func = unwrap<Function>(Fn);
2493  Function::arg_iterator I = Func->arg_end();
2494  if (I == Func->arg_begin())
2495  return nullptr;
2496  return wrap(&*--I);
2497 }
2498 
2500  Argument *A = unwrap<Argument>(Arg);
2501  Function *Fn = A->getParent();
2502  if (A->getArgNo() + 1 >= Fn->arg_size())
2503  return nullptr;
2504  return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
2505 }
2506 
2508  Argument *A = unwrap<Argument>(Arg);
2509  if (A->getArgNo() == 0)
2510  return nullptr;
2511  return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
2512 }
2513 
2515  Argument *A = unwrap<Argument>(Arg);
2516  A->addAttr(Attribute::getWithAlignment(A->getContext(), Align(align)));
2517 }
2518 
2519 /*--.. Operations on ifuncs ................................................--*/
2520 
2522  const char *Name, size_t NameLen,
2523  LLVMTypeRef Ty, unsigned AddrSpace,
2525  return wrap(GlobalIFunc::create(unwrap(Ty), AddrSpace,
2527  StringRef(Name, NameLen),
2528  unwrap<Constant>(Resolver), unwrap(M)));
2529 }
2530 
2532  const char *Name, size_t NameLen) {
2533  return wrap(unwrap(M)->getNamedIFunc(StringRef(Name, NameLen)));
2534 }
2535 
2537  Module *Mod = unwrap(M);
2539  if (I == Mod->ifunc_end())
2540  return nullptr;
2541  return wrap(&*I);
2542 }
2543 
2545  Module *Mod = unwrap(M);
2547  if (I == Mod->ifunc_begin())
2548  return nullptr;
2549  return wrap(&*--I);
2550 }
2551 
2553  GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2555  if (++I == GIF->getParent()->ifunc_end())
2556  return nullptr;
2557  return wrap(&*I);
2558 }
2559 
2561  GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2563  if (I == GIF->getParent()->ifunc_begin())
2564  return nullptr;
2565  return wrap(&*--I);
2566 }
2567 
2569  return wrap(unwrap<GlobalIFunc>(IFunc)->getResolver());
2570 }
2571 
2573  unwrap<GlobalIFunc>(IFunc)->setResolver(unwrap<Constant>(Resolver));
2574 }
2575 
2577  unwrap<GlobalIFunc>(IFunc)->eraseFromParent();
2578 }
2579 
2581  unwrap<GlobalIFunc>(IFunc)->removeFromParent();
2582 }
2583 
2584 /*--.. Operations on basic blocks ..........................................--*/
2585 
2587  return wrap(static_cast<Value*>(unwrap(BB)));
2588 }
2589 
2591  return isa<BasicBlock>(unwrap(Val));
2592 }
2593 
2595  return wrap(unwrap<BasicBlock>(Val));
2596 }
2597 
2599  return unwrap(BB)->getName().data();
2600 }
2601 
2603  return wrap(unwrap(BB)->getParent());
2604 }
2605 
2607  return wrap(unwrap(BB)->getTerminator());
2608 }
2609 
2611  return unwrap<Function>(FnRef)->size();
2612 }
2613 
2614 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
2615  Function *Fn = unwrap<Function>(FnRef);
2616  for (BasicBlock &BB : *Fn)
2617  *BasicBlocksRefs++ = wrap(&BB);
2618 }
2619 
2621  return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2622 }
2623 
2625  Function *Func = unwrap<Function>(Fn);
2626  Function::iterator I = Func->begin();
2627  if (I == Func->end())
2628  return nullptr;
2629  return wrap(&*I);
2630 }
2631 
2633  Function *Func = unwrap<Function>(Fn);
2634  Function::iterator I = Func->end();
2635  if (I == Func->begin())
2636  return nullptr;
2637  return wrap(&*--I);
2638 }
2639 
2641  BasicBlock *Block = unwrap(BB);
2642  Function::iterator I(Block);
2643  if (++I == Block->getParent()->end())
2644  return nullptr;
2645  return wrap(&*I);
2646 }
2647 
2649  BasicBlock *Block = unwrap(BB);
2650  Function::iterator I(Block);
2651  if (I == Block->getParent()->begin())
2652  return nullptr;
2653  return wrap(&*--I);
2654 }
2655 
2657  const char *Name) {
2658  return wrap(llvm::BasicBlock::Create(*unwrap(C), Name));
2659 }
2660 
2663  BasicBlock *ToInsert = unwrap(BB);
2664  BasicBlock *CurBB = unwrap(Builder)->GetInsertBlock();
2665  assert(CurBB && "current insertion point is invalid!");
2666  CurBB->getParent()->insert(std::next(CurBB->getIterator()), ToInsert);
2667 }
2668 
2671  unwrap<Function>(Fn)->insert(unwrap<Function>(Fn)->end(), unwrap(BB));
2672 }
2673 
2675  LLVMValueRef FnRef,
2676  const char *Name) {
2677  return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
2678 }
2679 
2681  return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
2682 }
2683 
2685  LLVMBasicBlockRef BBRef,
2686  const char *Name) {
2687  BasicBlock *BB = unwrap(BBRef);
2688  return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2689 }
2690 
2692  const char *Name) {
2693  return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
2694 }
2695 
2697  unwrap(BBRef)->eraseFromParent();
2698 }
2699 
2701  unwrap(BBRef)->removeFromParent();
2702 }
2703 
2705  unwrap(BB)->moveBefore(unwrap(MovePos));
2706 }
2707 
2709  unwrap(BB)->moveAfter(unwrap(MovePos));
2710 }
2711 
2712 /*--.. Operations on instructions ..........................................--*/
2713 
2715  return wrap(unwrap<Instruction>(Inst)->getParent());
2716 }
2717 
2719  BasicBlock *Block = unwrap(BB);
2720  BasicBlock::iterator I = Block->begin();
2721  if (I == Block->end())
2722  return nullptr;
2723  return wrap(&*I);
2724 }
2725 
2727  BasicBlock *Block = unwrap(BB);
2728  BasicBlock::iterator I = Block->end();
2729  if (I == Block->begin())
2730  return nullptr;
2731  return wrap(&*--I);
2732 }
2733 
2735  Instruction *Instr = unwrap<Instruction>(Inst);
2736  BasicBlock::iterator I(Instr);
2737  if (++I == Instr->getParent()->end())
2738  return nullptr;
2739  return wrap(&*I);
2740 }
2741 
2743  Instruction *Instr = unwrap<Instruction>(Inst);
2744  BasicBlock::iterator I(Instr);
2745  if (I == Instr->getParent()->begin())
2746  return nullptr;
2747  return wrap(&*--I);
2748 }
2749 
2751  unwrap<Instruction>(Inst)->removeFromParent();
2752 }
2753 
2755  unwrap<Instruction>(Inst)->eraseFromParent();
2756 }
2757 
2759  unwrap<Instruction>(Inst)->deleteValue();
2760 }
2761 
2763  if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2764  return (LLVMIntPredicate)I->getPredicate();
2765  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2766  if (CE->getOpcode() == Instruction::ICmp)
2767  return (LLVMIntPredicate)CE->getPredicate();
2768  return (LLVMIntPredicate)0;
2769 }
2770 
2772  if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2773  return (LLVMRealPredicate)I->getPredicate();
2774  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2775  if (CE->getOpcode() == Instruction::FCmp)
2776  return (LLVMRealPredicate)CE->getPredicate();
2777  return (LLVMRealPredicate)0;
2778 }
2779 
2781  if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2782  return map_to_llvmopcode(C->getOpcode());
2783  return (LLVMOpcode)0;
2784 }
2785 
2787  if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2788  return wrap(C->clone());
2789  return nullptr;
2790 }
2791 
2793  Instruction *I = dyn_cast<Instruction>(unwrap(Inst));
2794  return (I && I->isTerminator()) ? wrap(I) : nullptr;
2795 }
2796 
2798  if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) {
2799  return FPI->arg_size();
2800  }
2801  return unwrap<CallBase>(Instr)->arg_size();
2802 }
2803 
2804 /*--.. Call and invoke instructions ........................................--*/
2805 
2807  return unwrap<CallBase>(Instr)->getCallingConv();
2808 }
2809 
2811  return unwrap<CallBase>(Instr)->setCallingConv(
2812  static_cast<CallingConv::ID>(CC));
2813 }
2814 
2816  unsigned align) {
2817  auto *Call = unwrap<CallBase>(Instr);
2818  Attribute AlignAttr =
2819  Attribute::getWithAlignment(Call->getContext(), Align(align));
2820  Call->addAttributeAtIndex(Idx, AlignAttr);
2821 }
2822 
2824  LLVMAttributeRef A) {
2825  unwrap<CallBase>(C)->addAttributeAtIndex(Idx, unwrap(A));
2826 }
2827 
2829  LLVMAttributeIndex Idx) {
2830  auto *Call = unwrap<CallBase>(C);
2831  auto AS = Call->getAttributes().getAttributes(Idx);
2832  return AS.getNumAttributes();
2833 }
2834 
2837  auto *Call = unwrap<CallBase>(C);
2838  auto AS = Call->getAttributes().getAttributes(Idx);
2839  for (auto A : AS)
2840  *Attrs++ = wrap(A);
2841 }
2842 
2844  LLVMAttributeIndex Idx,
2845  unsigned KindID) {
2846  return wrap(unwrap<CallBase>(C)->getAttributeAtIndex(
2847  Idx, (Attribute::AttrKind)KindID));
2848 }
2849 
2851  LLVMAttributeIndex Idx,
2852  const char *K, unsigned KLen) {
2853  return wrap(
2854  unwrap<CallBase>(C)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
2855 }
2856 
2858  unsigned KindID) {
2859  unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
2860 }
2861 
2863  const char *K, unsigned KLen) {
2864  unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
2865 }
2866 
2868  return wrap(unwrap<CallBase>(Instr)->getCalledOperand());
2869 }
2870 
2872  return wrap(unwrap<CallBase>(Instr)->getFunctionType());
2873 }
2874 
2875 /*--.. Operations on call instructions (only) ..............................--*/
2876 
2878  return unwrap<CallInst>(Call)->isTailCall();
2879 }
2880 
2881 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
2882  unwrap<CallInst>(Call)->setTailCall(isTailCall);
2883 }
2884 
2885 /*--.. Operations on invoke instructions (only) ............................--*/
2886 
2888  return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2889 }
2890 
2892  if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2893  return wrap(CRI->getUnwindDest());
2894  } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2895  return wrap(CSI->getUnwindDest());
2896  }
2897  return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2898 }
2899 
2901  unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2902 }
2903 
2905  if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2906  return CRI->setUnwindDest(unwrap(B));
2907  } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2908  return CSI->setUnwindDest(unwrap(B));
2909  }
2910  unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2911 }
2912 
2913 /*--.. Operations on terminators ...........................................--*/
2914 
2916  return unwrap<Instruction>(Term)->getNumSuccessors();
2917 }
2918 
2920  return wrap(unwrap<Instruction>(Term)->getSuccessor(i));
2921 }
2922 
2924  return unwrap<Instruction>(Term)->setSuccessor(i, unwrap(block));
2925 }
2926 
2927 /*--.. Operations on branch instructions (only) ............................--*/
2928 
2930  return unwrap<BranchInst>(Branch)->isConditional();
2931 }
2932 
2934  return wrap(unwrap<BranchInst>(Branch)->getCondition());
2935 }
2936 
2938  return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2939 }
2940 
2941 /*--.. Operations on switch instructions (only) ............................--*/
2942 
2944  return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2945 }
2946 
2947 /*--.. Operations on alloca instructions (only) ............................--*/
2948 
2950  return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
2951 }
2952 
2953 /*--.. Operations on gep instructions (only) ...............................--*/
2954 
2956  return unwrap<GEPOperator>(GEP)->isInBounds();
2957 }
2958 
2960  return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
2961 }
2962 
2964  return wrap(unwrap<GEPOperator>(GEP)->getSourceElementType());
2965 }
2966 
2967 /*--.. Operations on phi nodes .............................................--*/
2968 
2969 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
2970  LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
2971  PHINode *PhiVal = unwrap<PHINode>(PhiNode);
2972  for (unsigned I = 0; I != Count; ++I)
2973  PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
2974 }
2975 
2976 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
2977  return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
2978 }
2979 
2981  return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
2982 }
2983 
2985  return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
2986 }
2987 
2988 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/
2989 
2991  auto *I = unwrap(Inst);
2992  if (auto *GEP = dyn_cast<GEPOperator>(I))
2993  return GEP->getNumIndices();
2994  if (auto *EV = dyn_cast<ExtractValueInst>(I))
2995  return EV->getNumIndices();
2996  if (auto *IV = dyn_cast<InsertValueInst>(I))
2997  return IV->getNumIndices();
2999  "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
3000 }
3001 
3002 const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
3003  auto *I = unwrap(Inst);
3004  if (auto *EV = dyn_cast<ExtractValueInst>(I))
3005  return EV->getIndices().data();
3006  if (auto *IV = dyn_cast<InsertValueInst>(I))
3007  return IV->getIndices().data();
3009  "LLVMGetIndices applies only to extractvalue and insertvalue!");
3010 }
3011 
3012 
3013 /*===-- Instruction builders ----------------------------------------------===*/
3014 
3016  return wrap(new IRBuilder<>(*unwrap(C)));
3017 }
3018 
3021 }
3022 
3024  LLVMValueRef Instr) {
3025  BasicBlock *BB = unwrap(Block);
3026  auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
3027  unwrap(Builder)->SetInsertPoint(BB, I);
3028 }
3029 
3031  Instruction *I = unwrap<Instruction>(Instr);
3032  unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
3033 }
3034 
3036  BasicBlock *BB = unwrap(Block);
3037  unwrap(Builder)->SetInsertPoint(BB);
3038 }
3039 
3041  return wrap(unwrap(Builder)->GetInsertBlock());
3042 }
3043 
3045  unwrap(Builder)->ClearInsertionPoint();
3046 }
3047 
3049  unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
3050 }
3051 
3053  const char *Name) {
3054  unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
3055 }
3056 
3058  delete unwrap(Builder);
3059 }
3060 
3061 /*--.. Metadata builders ...................................................--*/
3062 
3064  return wrap(unwrap(Builder)->getCurrentDebugLocation().getAsMDNode());
3065 }
3066 
3068  if (Loc)
3069  unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(unwrap<MDNode>(Loc)));
3070  else
3071  unwrap(Builder)->SetCurrentDebugLocation(DebugLoc());
3072 }
3073 
3075  MDNode *Loc =
3076  L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
3077  unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
3078 }
3079 
3081  LLVMContext &Context = unwrap(Builder)->getContext();
3082  return wrap(MetadataAsValue::get(
3083  Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
3084 }
3085 
3087  unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
3088 }
3089 
3091  unwrap(Builder)->AddMetadataToInst(unwrap<Instruction>(Inst));
3092 }
3093 
3095  LLVMMetadataRef FPMathTag) {
3096 
3097  unwrap(Builder)->setDefaultFPMathTag(FPMathTag
3098  ? unwrap<MDNode>(FPMathTag)
3099  : nullptr);
3100 }
3101 
3103  return wrap(unwrap(Builder)->getDefaultFPMathTag());
3104 }
3105 
3106 /*--.. Instruction builders ................................................--*/
3107 
3109  return wrap(unwrap(B)->CreateRetVoid());
3110 }
3111 
3113  return wrap(unwrap(B)->CreateRet(unwrap(V)));
3114 }
3115 
3117  unsigned N) {
3118  return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
3119 }
3120 
3122  return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
3123 }
3124 
3127  return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
3128 }
3129 
3131  LLVMBasicBlockRef Else, unsigned NumCases) {
3132  return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
3133 }
3134 
3136  unsigned NumDests) {
3137  return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
3138 }
3139 
3141  LLVMValueRef *Args, unsigned NumArgs,
3143  const char *Name) {
3144  return wrap(unwrap(B)->CreateInvoke(unwrap<FunctionType>(Ty), unwrap(Fn),
3145  unwrap(Then), unwrap(Catch),
3146  ArrayRef(unwrap(Args), NumArgs), Name));
3147 }
3148 
3150  LLVMValueRef PersFn, unsigned NumClauses,
3151  const char *Name) {
3152  // The personality used to live on the landingpad instruction, but now it
3153  // lives on the parent function. For compatibility, take the provided
3154  // personality and put it on the parent function.
3155  if (PersFn)
3156  unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
3157  unwrap<Function>(PersFn));
3158  return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
3159 }
3160 
3162  LLVMValueRef *Args, unsigned NumArgs,
3163  const char *Name) {
3164  return wrap(unwrap(B)->CreateCatchPad(unwrap(ParentPad),
3165  ArrayRef(unwrap(Args), NumArgs), Name));
3166 }
3167 
3169  LLVMValueRef *Args, unsigned NumArgs,
3170  const char *Name) {
3171  if (ParentPad == nullptr) {
3172  Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3173  ParentPad = wrap(Constant::getNullValue(Ty));
3174  }
3175  return wrap(unwrap(B)->CreateCleanupPad(
3176  unwrap(ParentPad), ArrayRef(unwrap(Args), NumArgs), Name));
3177 }
3178 
3180  return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
3181 }
3182 
3184  LLVMBasicBlockRef UnwindBB,
3185  unsigned NumHandlers, const char *Name) {
3186  if (ParentPad == nullptr) {
3187  Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3188  ParentPad = wrap(Constant::getNullValue(Ty));
3189  }
3190  return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(UnwindBB),
3191  NumHandlers, Name));
3192 }
3193 
3196  return wrap(unwrap(B)->CreateCatchRet(unwrap<CatchPadInst>(CatchPad),
3197  unwrap(BB)));
3198 }
3199 
3202  return wrap(unwrap(B)->CreateCleanupRet(unwrap<CleanupPadInst>(CatchPad),
3203  unwrap(BB)));
3204 }
3205 
3207  return wrap(unwrap(B)->CreateUnreachable());
3208 }
3209 
3211  LLVMBasicBlockRef Dest) {
3212  unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
3213 }
3214 
3216  unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
3217 }
3218 
3219 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
3220  return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
3221 }
3222 
3223 LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
3224  return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
3225 }
3226 
3228  unwrap<LandingPadInst>(LandingPad)->addClause(unwrap<Constant>(ClauseVal));
3229 }
3230 
3232  return unwrap<LandingPadInst>(LandingPad)->isCleanup();
3233 }
3234 
3235 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
3236  unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
3237 }
3238 
3240  unwrap<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Dest));
3241 }
3242 
3243 unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) {
3244  return unwrap<CatchSwitchInst>(CatchSwitch)->getNumHandlers();
3245 }
3246 
3247 void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) {
3248  CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(CatchSwitch);
3249  for (const BasicBlock *H : CSI->handlers())
3250  *Handlers++ = wrap(H);
3251 }
3252 
3254  return wrap(unwrap<CatchPadInst>(CatchPad)->getCatchSwitch());
3255 }
3256 
3258  unwrap<CatchPadInst>(CatchPad)
3259  ->setCatchSwitch(unwrap<CatchSwitchInst>(CatchSwitch));
3260 }
3261 
3262 /*--.. Funclets ...........................................................--*/
3263 
3265  return wrap(unwrap<FuncletPadInst>(Funclet)->getArgOperand(i));
3266 }
3267 
3268 void LLVMSetArgOperand(LLVMValueRef Funclet, unsigned i, LLVMValueRef value) {
3269  unwrap<FuncletPadInst>(Funclet)->setArgOperand(i, unwrap(value));
3270 }
3271 
3272 /*--.. Arithmetic ..........................................................--*/
3273 
3275  const char *Name) {
3276  return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
3277 }
3278 
3280  const char *Name) {
3281  return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
3282 }
3283 
3285  const char *Name) {
3286  return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
3287 }
3288 
3290  const char *Name) {
3291  return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
3292 }
3293 
3295  const char *Name) {
3296  return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
3297 }
3298 
3300  const char *Name) {
3301  return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
3302 }
3303 
3305  const char *Name) {
3306  return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
3307 }
3308 
3310  const char *Name) {
3311  return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
3312 }
3313 
3315  const char *Name) {
3316  return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
3317 }
3318 
3320  const char *Name) {
3321  return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
3322 }
3323 
3325  const char *Name) {
3326  return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
3327 }
3328 
3330  const char *Name) {
3331  return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
3332 }
3333 
3335  const char *Name) {
3336  return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
3337 }
3338 
3340  LLVMValueRef RHS, const char *Name) {
3341  return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
3342 }
3343 
3345  const char *Name) {
3346  return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
3347 }
3348 
3350  LLVMValueRef RHS, const char *Name) {
3351  return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
3352 }
3353 
3355  const char *Name) {
3356  return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
3357 }
3358 
3360  const char *Name) {
3361  return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
3362 }
3363 
3365  const char *Name) {
3366  return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
3367 }
3368 
3370  const char *Name) {
3371  return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
3372 }
3373 
3375  const char *Name) {
3376  return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
3377 }
3378 
3380  const char *Name) {
3381  return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
3382 }
3383 
3385  const char *Name) {
3386  return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
3387 }
3388 
3390  const char *Name) {
3391  return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
3392 }
3393 
3395  const char *Name) {
3396  return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
3397 }
3398 
3400  const char *Name) {
3401  return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
3402 }
3403 
3406  const char *Name) {
3408  unwrap(RHS), Name));
3409 }
3410 
3412  return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
3413 }
3414 
3416  const char *Name) {
3417  return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
3418 }
3419 
3421  const char *Name) {
3422  return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
3423 }
3424 
3426  return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
3427 }
3428 
3430  return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
3431 }
3432 
3433 /*--.. Memory ..............................................................--*/
3434 
3436  const char *Name) {
3437  Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3438  Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3439  AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3440  Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3441  ITy, unwrap(Ty), AllocSize,
3442  nullptr, nullptr, "");
3443  return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3444 }
3445 
3447  LLVMValueRef Val, const char *Name) {
3448  Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3449  Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3450  AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3451  Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3452  ITy, unwrap(Ty), AllocSize,
3453  unwrap(Val), nullptr, "");
3454  return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3455 }
3456 
3458  LLVMValueRef Val, LLVMValueRef Len,
3459  unsigned Align) {
3460  return wrap(unwrap(B)->CreateMemSet(unwrap(Ptr), unwrap(Val), unwrap(Len),
3461  MaybeAlign(Align)));
3462 }
3463 
3465  LLVMValueRef Dst, unsigned DstAlign,
3466  LLVMValueRef Src, unsigned SrcAlign,
3467  LLVMValueRef Size) {
3468  return wrap(unwrap(B)->CreateMemCpy(unwrap(Dst), MaybeAlign(DstAlign),
3469  unwrap(Src), MaybeAlign(SrcAlign),
3470  unwrap(Size)));
3471 }
3472 
3474  LLVMValueRef Dst, unsigned DstAlign,
3475  LLVMValueRef Src, unsigned SrcAlign,
3476  LLVMValueRef Size) {
3477  return wrap(unwrap(B)->CreateMemMove(unwrap(Dst), MaybeAlign(DstAlign),
3478  unwrap(Src), MaybeAlign(SrcAlign),
3479  unwrap(Size)));
3480 }
3481 
3483  const char *Name) {
3484  return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
3485 }
3486 
3488  LLVMValueRef Val, const char *Name) {
3489  return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
3490 }
3491 
3493  return wrap(unwrap(B)->Insert(
3494  CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
3495 }
3496 
3498  LLVMValueRef PointerVal, const char *Name) {
3499  return wrap(unwrap(B)->CreateLoad(unwrap(Ty), unwrap(PointerVal), Name));
3500 }
3501 
3503  LLVMValueRef PointerVal) {
3504  return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
3505 }
3506 
3508  switch (Ordering) {
3518  }
3519 
3520  llvm_unreachable("Invalid LLVMAtomicOrdering value!");
3521 }
3522 
3524  switch (Ordering) {
3534  }
3535 
3536  llvm_unreachable("Invalid AtomicOrdering value!");
3537 }
3538 
3540  switch (BinOp) {
3556  }
3557 
3558  llvm_unreachable("Invalid LLVMAtomicRMWBinOp value!");
3559 }
3560 
3562  switch (BinOp) {
3578  default: break;
3579  }
3580 
3581  llvm_unreachable("Invalid AtomicRMWBinOp value!");
3582 }
3583 
3584 // TODO: Should this and other atomic instructions support building with
3585 // "syncscope"?
3587  LLVMBool isSingleThread, const char *Name) {
3588  return wrap(
3589  unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
3590  isSingleThread ? SyncScope::SingleThread
3592  Name));
3593 }
3594 
3596  LLVMValueRef Pointer, LLVMValueRef *Indices,
3597  unsigned NumIndices, const char *Name) {
3598  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3599  return wrap(unwrap(B)->CreateGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3600 }
3601 
3603  LLVMValueRef Pointer, LLVMValueRef *Indices,
3604  unsigned NumIndices, const char *Name) {
3605  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3606  return wrap(
3607  unwrap(B)->CreateInBoundsGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3608 }
3609 
3611  LLVMValueRef Pointer, unsigned Idx,
3612  const char *Name) {
3613  return wrap(
3614  unwrap(B)->CreateStructGEP(unwrap(Ty), unwrap(Pointer), Idx, Name));
3615 }
3616 
3618  const char *Name) {
3619  return wrap(unwrap(B)->CreateGlobalString(Str, Name));
3620 }
3621 
3623  const char *Name) {
3624  return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
3625 }
3626 
3628  Value *P = unwrap(MemAccessInst);
3629  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3630  return LI->isVolatile();
3631  if (StoreInst *SI = dyn_cast<StoreInst>(P))
3632  return SI->isVolatile();
3633  if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
3634  return AI->isVolatile();
3635  return cast<AtomicCmpXchgInst>(P)->isVolatile();
3636 }
3637 
3638 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
3639  Value *P = unwrap(MemAccessInst);
3640  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3641  return LI->setVolatile(isVolatile);
3642  if (StoreInst *SI = dyn_cast<StoreInst>(P))
3643  return SI->setVolatile(isVolatile);
3644  if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
3645  return AI->setVolatile(isVolatile);
3646  return cast<AtomicCmpXchgInst>(P)->setVolatile(isVolatile);
3647 }
3648 
3650  return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->isWeak();
3651 }
3652 
3653 void LLVMSetWeak(LLVMValueRef CmpXchgInst, LLVMBool isWeak) {
3654  return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->setWeak(isWeak);
3655 }
3656 
3658  Value *P = unwrap(MemAccessInst);
3659  AtomicOrdering O;
3660  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3661  O = LI->getOrdering();
3662  else if (StoreInst *SI = dyn_cast<StoreInst>(P))
3663  O = SI->getOrdering();
3664  else
3665  O = cast<AtomicRMWInst>(P)->getOrdering();
3666  return mapToLLVMOrdering(O);
3667 }
3668 
3669 void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
3670  Value *P = unwrap(MemAccessInst);
3671  AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3672 
3673  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3674  return LI->setOrdering(O);
3675  return cast<StoreInst>(P)->setOrdering(O);
3676 }
3677 
3679  return mapToLLVMRMWBinOp(unwrap<AtomicRMWInst>(Inst)->getOperation());
3680 }
3681 
3683  unwrap<AtomicRMWInst>(Inst)->setOperation(mapFromLLVMRMWBinOp(BinOp));
3684 }
3685 
3686 /*--.. Casts ...............................................................--*/
3687 
3689  LLVMTypeRef DestTy, const char *Name) {
3690  return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
3691 }
3692 
3694  LLVMTypeRef DestTy, const char *Name) {
3695  return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
3696 }
3697 
3699  LLVMTypeRef DestTy, const char *Name) {
3700  return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
3701 }
3702 
3704  LLVMTypeRef DestTy, const char *Name) {
3705  return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
3706 }
3707 
3709  LLVMTypeRef DestTy, const char *Name) {
3710  return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
3711 }
3712 
3714  LLVMTypeRef DestTy, const char *Name) {
3715  return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
3716 }
3717 
3719  LLVMTypeRef DestTy, const char *Name) {
3720  return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
3721 }
3722 
3724  LLVMTypeRef DestTy, const char *Name) {
3725  return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
3726 }
3727 
3729  LLVMTypeRef DestTy, const char *Name) {
3730  return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
3731 }
3732 
3734  LLVMTypeRef DestTy, const char *Name) {
3735  return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
3736 }
3737 
3739  LLVMTypeRef DestTy, const char *Name) {
3740  return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
3741 }
3742 
3744  LLVMTypeRef DestTy, const char *Name) {
3745  return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
3746 }
3747 
3749  LLVMTypeRef DestTy, const char *Name) {
3750  return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
3751 }
3752 
3754  LLVMTypeRef DestTy, const char *Name) {
3755  return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
3756  Name));
3757 }
3758 
3760  LLVMTypeRef DestTy, const char *Name) {
3761  return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
3762  Name));
3763 }
3764 
3766  LLVMTypeRef DestTy, const char *Name) {
3767  return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
3768  Name));
3769 }
3770 
3772  LLVMTypeRef DestTy, const char *Name) {
3773  return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
3774  unwrap(DestTy), Name));
3775 }
3776 
3778  LLVMTypeRef DestTy, const char *Name) {
3779  return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
3780 }
3781 
3783  LLVMTypeRef DestTy, LLVMBool IsSigned,
3784  const char *Name) {
3785  return wrap(
3786  unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy), IsSigned, Name));
3787 }
3788 
3790  LLVMTypeRef DestTy, const char *Name) {
3791  return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
3792  /*isSigned*/true, Name));
3793 }
3794 
3796  LLVMTypeRef DestTy, const char *Name) {
3797  return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
3798 }
3799 
3801  LLVMTypeRef DestTy, LLVMBool DestIsSigned) {
3803  unwrap(Src), SrcIsSigned, unwrap(DestTy), DestIsSigned));
3804 }
3805 
3806 /*--.. Comparisons .........................................................--*/
3807 
3810  const char *Name) {
3811  return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
3812  unwrap(LHS), unwrap(RHS), Name));
3813 }
3814 
3817  const char *Name) {
3818  return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
3819  unwrap(LHS), unwrap(RHS), Name));
3820 }
3821 
3822 /*--.. Miscellaneous instructions ..........................................--*/
3823 
3825  return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
3826 }
3827 
3829  LLVMValueRef *Args, unsigned NumArgs,
3830  const char *Name) {
3831  FunctionType *FTy = unwrap<FunctionType>(Ty);
3832  return wrap(unwrap(B)->CreateCall(FTy, unwrap(Fn),
3833  ArrayRef(unwrap(Args), NumArgs), Name));
3834 }
3835 
3838  const char *Name) {
3839  return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
3840  Name));
3841 }
3842 
3844  LLVMTypeRef Ty, const char *Name) {
3845  return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
3846 }
3847 
3849  LLVMValueRef Index, const char *Name) {
3850  return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
3851  Name));
3852 }
3853 
3856  const char *Name) {
3857  return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
3858  unwrap(Index), Name));
3859 }
3860 
3863  const char *Name) {
3864  return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
3865  unwrap(Mask), Name));
3866 }
3867 
3869  unsigned Index, const char *Name) {
3870  return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
3871 }
3872 
3874  LLVMValueRef EltVal, unsigned Index,
3875  const char *Name) {
3876  return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
3877  Index, Name));
3878 }
3879 
3881  const char *Name) {
3882  return wrap(unwrap(B)->CreateFreeze(unwrap(Val), Name));