LLVM  16.0.0git
Core.cpp
Go to the documentation of this file.
1 //===-- Core.cpp ----------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the common infrastructure (including the C bindings)
10 // for libLLVMCore.a, which implements the LLVM intermediate representation.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm-c/Core.h"
15 #include "llvm/IR/Attributes.h"
16 #include "llvm/IR/BasicBlock.h"
17 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/DerivedTypes.h"
20 #include "llvm/IR/DiagnosticInfo.h"
22 #include "llvm/IR/GlobalAlias.h"
23 #include "llvm/IR/GlobalVariable.h"
24 #include "llvm/IR/IRBuilder.h"
25 #include "llvm/IR/InlineAsm.h"
26 #include "llvm/IR/IntrinsicInst.h"
27 #include "llvm/IR/LLVMContext.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/InitializePasses.h"
31 #include "llvm/PassRegistry.h"
32 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/Threading.h"
39 #include <cassert>
40 #include <cstdlib>
41 #include <cstring>
42 #include <system_error>
43 
44 using namespace llvm;
45 
46 #define DEBUG_TYPE "ir"
47 
54 }
55 
58 }
59 
60 void LLVMShutdown() {
61  llvm_shutdown();
62 }
63 
64 /*===-- Error handling ----------------------------------------------------===*/
65 
66 char *LLVMCreateMessage(const char *Message) {
67  return strdup(Message);
68 }
69 
70 void LLVMDisposeMessage(char *Message) {
71  free(Message);
72 }
73 
74 
75 /*===-- Operations on contexts --------------------------------------------===*/
76 
78  static LLVMContext GlobalContext;
79  return GlobalContext;
80 }
81 
83  return wrap(new LLVMContext());
84 }
85 
87 
89  LLVMDiagnosticHandler Handler,
90  void *DiagnosticContext) {
91  unwrap(C)->setDiagnosticHandlerCallBack(
93  Handler),
94  DiagnosticContext);
95 }
96 
98  return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>(
99  unwrap(C)->getDiagnosticHandlerCallBack());
100 }
101 
103  return unwrap(C)->getDiagnosticContext();
104 }
105 
107  void *OpaqueHandle) {
108  auto YieldCallback =
109  LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
110  unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
111 }
112 
114  return unwrap(C)->shouldDiscardValueNames();
115 }
116 
118  unwrap(C)->setDiscardValueNames(Discard);
119 }
120 
122  unwrap(C)->setOpaquePointers(OpaquePointers);
123 }
124 
126  delete unwrap(C);
127 }
128 
129 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char *Name,
130  unsigned SLen) {
131  return unwrap(C)->getMDKindID(StringRef(Name, SLen));
132 }
133 
134 unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) {
135  return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
136 }
137 
138 unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) {
139  return Attribute::getAttrKindFromName(StringRef(Name, SLen));
140 }
141 
143  return Attribute::AttrKind::EndAttrKinds;
144 }
145 
147  uint64_t Val) {
148  auto &Ctx = *unwrap(C);
149  auto AttrKind = (Attribute::AttrKind)KindID;
150  return wrap(Attribute::get(Ctx, AttrKind, Val));
151 }
152 
154  return unwrap(A).getKindAsEnum();
155 }
156 
158  auto Attr = unwrap(A);
159  if (Attr.isEnumAttribute())
160  return 0;
161  return Attr.getValueAsInt();
162 }
163 
165  LLVMTypeRef type_ref) {
166  auto &Ctx = *unwrap(C);
167  auto AttrKind = (Attribute::AttrKind)KindID;
168  return wrap(Attribute::get(Ctx, AttrKind, unwrap(type_ref)));
169 }
170 
172  auto Attr = unwrap(A);
173  return wrap(Attr.getValueAsType());
174 }
175 
177  const char *K, unsigned KLength,
178  const char *V, unsigned VLength) {
179  return wrap(Attribute::get(*unwrap(C), StringRef(K, KLength),
180  StringRef(V, VLength)));
181 }
182 
184  unsigned *Length) {
185  auto S = unwrap(A).getKindAsString();
186  *Length = S.size();
187  return S.data();
188 }
189 
191  unsigned *Length) {
192  auto S = unwrap(A).getValueAsString();
193  *Length = S.size();
194  return S.data();
195 }
196 
198  auto Attr = unwrap(A);
199  return Attr.isEnumAttribute() || Attr.isIntAttribute();
200 }
201 
203  return unwrap(A).isStringAttribute();
204 }
205 
207  return unwrap(A).isTypeAttribute();
208 }
209 
211  std::string MsgStorage;
212  raw_string_ostream Stream(MsgStorage);
214 
215  unwrap(DI)->print(DP);
216  Stream.flush();
217 
218  return LLVMCreateMessage(MsgStorage.c_str());
219 }
220 
222  LLVMDiagnosticSeverity severity;
223 
224  switch(unwrap(DI)->getSeverity()) {
225  default:
226  severity = LLVMDSError;
227  break;
228  case DS_Warning:
229  severity = LLVMDSWarning;
230  break;
231  case DS_Remark:
232  severity = LLVMDSRemark;
233  break;
234  case DS_Note:
235  severity = LLVMDSNote;
236  break;
237  }
238 
239  return severity;
240 }
241 
242 /*===-- Operations on modules ---------------------------------------------===*/
243 
245  return wrap(new Module(ModuleID, getGlobalContext()));
246 }
247 
249  LLVMContextRef C) {
250  return wrap(new Module(ModuleID, *unwrap(C)));
251 }
252 
254  delete unwrap(M);
255 }
256 
257 const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) {
258  auto &Str = unwrap(M)->getModuleIdentifier();
259  *Len = Str.length();
260  return Str.c_str();
261 }
262 
263 void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) {
264  unwrap(M)->setModuleIdentifier(StringRef(Ident, Len));
265 }
266 
267 const char *LLVMGetSourceFileName(LLVMModuleRef M, size_t *Len) {
268  auto &Str = unwrap(M)->getSourceFileName();
269  *Len = Str.length();
270  return Str.c_str();
271 }
272 
273 void LLVMSetSourceFileName(LLVMModuleRef M, const char *Name, size_t Len) {
274  unwrap(M)->setSourceFileName(StringRef(Name, Len));
275 }
276 
277 /*--.. Data layout .........................................................--*/
279  return unwrap(M)->getDataLayoutStr().c_str();
280 }
281 
283  return LLVMGetDataLayoutStr(M);
284 }
285 
286 void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
287  unwrap(M)->setDataLayout(DataLayoutStr);
288 }
289 
290 /*--.. Target triple .......................................................--*/
291 const char * LLVMGetTarget(LLVMModuleRef M) {
292  return unwrap(M)->getTargetTriple().c_str();
293 }
294 
295 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
296  unwrap(M)->setTargetTriple(Triple);
297 }
298 
299 /*--.. Module flags ........................................................--*/
302  const char *Key;
303  size_t KeyLen;
305 };
306 
309  switch (Behavior) {
313  return Module::ModFlagBehavior::Warning;
315  return Module::ModFlagBehavior::Require;
317  return Module::ModFlagBehavior::Override;
319  return Module::ModFlagBehavior::Append;
321  return Module::ModFlagBehavior::AppendUnique;
322  }
323  llvm_unreachable("Unknown LLVMModuleFlagBehavior");
324 }
325 
328  switch (Behavior) {
331  case Module::ModFlagBehavior::Warning:
333  case Module::ModFlagBehavior::Require:
335  case Module::ModFlagBehavior::Override:
337  case Module::ModFlagBehavior::Append:
339  case Module::ModFlagBehavior::AppendUnique:
341  default:
342  llvm_unreachable("Unhandled Flag Behavior");
343  }
344 }
345 
348  unwrap(M)->getModuleFlagsMetadata(MFEs);
349 
350  LLVMOpaqueModuleFlagEntry *Result = static_cast<LLVMOpaqueModuleFlagEntry *>(
351  safe_malloc(MFEs.size() * sizeof(LLVMOpaqueModuleFlagEntry)));
352  for (unsigned i = 0; i < MFEs.size(); ++i) {
353  const auto &ModuleFlag = MFEs[i];
354  Result[i].Behavior = map_from_llvmModFlagBehavior(ModuleFlag.Behavior);
355  Result[i].Key = ModuleFlag.Key->getString().data();
356  Result[i].KeyLen = ModuleFlag.Key->getString().size();
357  Result[i].Metadata = wrap(ModuleFlag.Val);
358  }
359  *Len = MFEs.size();
360  return Result;
361 }
362 
364  free(Entries);
365 }
366 
369  unsigned Index) {
371  static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
372  return MFE.Behavior;
373 }
374 
376  unsigned Index, size_t *Len) {
378  static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
379  *Len = MFE.KeyLen;
380  return MFE.Key;
381 }
382 
384  unsigned Index) {
386  static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
387  return MFE.Metadata;
388 }
389 
391  const char *Key, size_t KeyLen) {
392  return wrap(unwrap(M)->getModuleFlag({Key, KeyLen}));
393 }
394 
396  const char *Key, size_t KeyLen,
397  LLVMMetadataRef Val) {
398  unwrap(M)->addModuleFlag(map_to_llvmModFlagBehavior(Behavior),
399  {Key, KeyLen}, unwrap(Val));
400 }
401 
402 /*--.. Printing modules ....................................................--*/
403 
405  unwrap(M)->print(errs(), nullptr,
406  /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
407 }
408 
410  char **ErrorMessage) {
411  std::error_code EC;
412  raw_fd_ostream dest(Filename, EC, sys::fs::OF_TextWithCRLF);
413  if (EC) {
414  *ErrorMessage = strdup(EC.message().c_str());
415  return true;
416  }
417 
418  unwrap(M)->print(dest, nullptr);
419 
420  dest.close();
421 
422  if (dest.has_error()) {
423  std::string E = "Error printing to file: " + dest.error().message();
424  *ErrorMessage = strdup(E.c_str());
425  return true;
426  }
427 
428  return false;
429 }
430 
432  std::string buf;
433  raw_string_ostream os(buf);
434 
435  unwrap(M)->print(os, nullptr);
436  os.flush();
437 
438  return strdup(buf.c_str());
439 }
440 
441 /*--.. Operations on inline assembler ......................................--*/
442 void LLVMSetModuleInlineAsm2(LLVMModuleRef M, const char *Asm, size_t Len) {
443  unwrap(M)->setModuleInlineAsm(StringRef(Asm, Len));
444 }
445 
447  unwrap(M)->setModuleInlineAsm(StringRef(Asm));
448 }
449 
450 void LLVMAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm, size_t Len) {
451  unwrap(M)->appendModuleInlineAsm(StringRef(Asm, Len));
452 }
453 
454 const char *LLVMGetModuleInlineAsm(LLVMModuleRef M, size_t *Len) {
455  auto &Str = unwrap(M)->getModuleInlineAsm();
456  *Len = Str.length();
457  return Str.c_str();
458 }
459 
461  size_t AsmStringSize, char *Constraints,
462  size_t ConstraintsSize, LLVMBool HasSideEffects,
463  LLVMBool IsAlignStack,
464  LLVMInlineAsmDialect Dialect, LLVMBool CanThrow) {
466  switch (Dialect) {
468  AD = InlineAsm::AD_ATT;
469  break;
471  AD = InlineAsm::AD_Intel;
472  break;
473  }
474  return wrap(InlineAsm::get(unwrap<FunctionType>(Ty),
475  StringRef(AsmString, AsmStringSize),
476  StringRef(Constraints, ConstraintsSize),
477  HasSideEffects, IsAlignStack, AD, CanThrow));
478 }
479 
480 /*--.. Operations on module contexts ......................................--*/
482  return wrap(&unwrap(M)->getContext());
483 }
484 
485 
486 /*===-- Operations on types -----------------------------------------------===*/
487 
488 /*--.. Operations on all types (mostly) ....................................--*/
489 
491  switch (unwrap(Ty)->getTypeID()) {
492  case Type::VoidTyID:
493  return LLVMVoidTypeKind;
494  case Type::HalfTyID:
495  return LLVMHalfTypeKind;
496  case Type::BFloatTyID:
497  return LLVMBFloatTypeKind;
498  case Type::FloatTyID:
499  return LLVMFloatTypeKind;
500  case Type::DoubleTyID:
501  return LLVMDoubleTypeKind;
502  case Type::X86_FP80TyID:
503  return LLVMX86_FP80TypeKind;
504  case Type::FP128TyID:
505  return LLVMFP128TypeKind;
506  case Type::PPC_FP128TyID:
507  return LLVMPPC_FP128TypeKind;
508  case Type::LabelTyID:
509  return LLVMLabelTypeKind;
510  case Type::MetadataTyID:
511  return LLVMMetadataTypeKind;
512  case Type::IntegerTyID:
513  return LLVMIntegerTypeKind;
514  case Type::FunctionTyID:
515  return LLVMFunctionTypeKind;
516  case Type::StructTyID:
517  return LLVMStructTypeKind;
518  case Type::ArrayTyID:
519  return LLVMArrayTypeKind;
520  case Type::PointerTyID:
521  return LLVMPointerTypeKind;
523  return LLVMVectorTypeKind;
524  case Type::X86_MMXTyID:
525  return LLVMX86_MMXTypeKind;
526  case Type::X86_AMXTyID:
527  return LLVMX86_AMXTypeKind;
528  case Type::TokenTyID:
529  return LLVMTokenTypeKind;
533  llvm_unreachable("Typed pointers are unsupported via the C API");
534  }
535  llvm_unreachable("Unhandled TypeID.");
536 }
537 
539 {
540  return unwrap(Ty)->isSized();
541 }
542 
544  return wrap(&unwrap(Ty)->getContext());
545 }
546 
548  return unwrap(Ty)->print(errs(), /*IsForDebug=*/true);
549 }
550 
552  std::string buf;
553  raw_string_ostream os(buf);
554 
555  if (unwrap(Ty))
556  unwrap(Ty)->print(os);
557  else
558  os << "Printing <null> Type";
559 
560  os.flush();
561 
562  return strdup(buf.c_str());
563 }
564 
565 /*--.. Operations on integer types .........................................--*/
566 
568  return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
569 }
571  return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
572 }
574  return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
575 }
577  return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
578 }
580  return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
581 }
584 }
586  return wrap(IntegerType::get(*unwrap(C), NumBits));
587 }
588 
591 }
594 }
597 }
600 }
603 }
606 }
607 LLVMTypeRef LLVMIntType(unsigned NumBits) {
608  return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
609 }
610 
611 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
612  return unwrap<IntegerType>(IntegerTy)->getBitWidth();
613 }
614 
615 /*--.. Operations on real types ............................................--*/
616 
618  return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
619 }
622 }
624  return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
625 }
628 }
631 }
633  return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
634 }
637 }
640 }
643 }
644 
647 }
650 }
653 }
656 }
659 }
662 }
665 }
668 }
671 }
672 
673 /*--.. Operations on function types ........................................--*/
674 
676  LLVMTypeRef *ParamTypes, unsigned ParamCount,
677  LLVMBool IsVarArg) {
678  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
679  return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
680 }
681 
683  return unwrap<FunctionType>(FunctionTy)->isVarArg();
684 }
685 
687  return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
688 }
689 
690 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
691  return unwrap<FunctionType>(FunctionTy)->getNumParams();
692 }
693 
694 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
695  FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
696  for (Type *T : Ty->params())
697  *Dest++ = wrap(T);
698 }
699 
700 /*--.. Operations on struct types ..........................................--*/
701 
703  unsigned ElementCount, LLVMBool Packed) {
704  ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
705  return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
706 }
707 
709  unsigned ElementCount, LLVMBool Packed) {
710  return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
711  ElementCount, Packed);
712 }
713 
715 {
716  return wrap(StructType::create(*unwrap(C), Name));
717 }
718 
720 {
721  StructType *Type = unwrap<StructType>(Ty);
722  if (!Type->hasName())
723  return nullptr;
724  return Type->getName().data();
725 }
726 
727 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
728  unsigned ElementCount, LLVMBool Packed) {
729  ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
730  unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
731 }
732 
734  return unwrap<StructType>(StructTy)->getNumElements();
735 }
736 
738  StructType *Ty = unwrap<StructType>(StructTy);
739  for (Type *T : Ty->elements())
740  *Dest++ = wrap(T);
741 }
742 
744  StructType *Ty = unwrap<StructType>(StructTy);
745  return wrap(Ty->getTypeAtIndex(i));
746 }
747 
749  return unwrap<StructType>(StructTy)->isPacked();
750 }
751 
753  return unwrap<StructType>(StructTy)->isOpaque();
754 }
755 
757  return unwrap<StructType>(StructTy)->isLiteral();
758 }
759 
761  return wrap(StructType::getTypeByName(unwrap(M)->getContext(), Name));
762 }
763 
765  return wrap(StructType::getTypeByName(*unwrap(C), Name));
766 }
767 
768 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
769 
771  int i = 0;
772  for (auto *T : unwrap(Tp)->subtypes()) {
773  Arr[i] = wrap(T);
774  i++;
775  }
776 }
777 
779  return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
780 }
781 
783  return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
784 }
785 
787  return unwrap(Ty)->isOpaquePointerTy();
788 }
789 
791  return wrap(FixedVectorType::get(unwrap(ElementType), ElementCount));
792 }
793 
795  unsigned ElementCount) {
796  return wrap(ScalableVectorType::get(unwrap(ElementType), ElementCount));
797 }
798 
800  auto *Ty = unwrap(WrappedTy);
801  if (auto *PTy = dyn_cast<PointerType>(Ty))
802  return wrap(PTy->getNonOpaquePointerElementType());
803  if (auto *ATy = dyn_cast<ArrayType>(Ty))
804  return wrap(ATy->getElementType());
805  return wrap(cast<VectorType>(Ty)->getElementType());
806 }
807 
809  return unwrap(Tp)->getNumContainedTypes();
810 }
811 
812 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
813  return unwrap<ArrayType>(ArrayTy)->getNumElements();
814 }
815 
817  return unwrap<PointerType>(PointerTy)->getAddressSpace();
818 }
819 
820 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
821  return unwrap<VectorType>(VectorTy)->getElementCount().getKnownMinValue();
822 }
823 
824 /*--.. Operations on other types ...........................................--*/
825 
828 }
829 
831  return wrap(Type::getVoidTy(*unwrap(C)));
832 }
834  return wrap(Type::getLabelTy(*unwrap(C)));
835 }
837  return wrap(Type::getTokenTy(*unwrap(C)));
838 }
840  return wrap(Type::getMetadataTy(*unwrap(C)));
841 }
842 
845 }
848 }
849 
850 /*===-- Operations on values ----------------------------------------------===*/
851 
852 /*--.. Operations on all values ............................................--*/
853 
855  return wrap(unwrap(Val)->getType());
856 }
857 
859  switch(unwrap(Val)->getValueID()) {
860 #define LLVM_C_API 1
861 #define HANDLE_VALUE(Name) \
862  case Value::Name##Val: \
863  return LLVM##Name##ValueKind;
864 #include "llvm/IR/Value.def"
865  default:
867  }
868 }
869 
870 const char *LLVMGetValueName2(LLVMValueRef Val, size_t *Length) {
871  auto *V = unwrap(Val);
872  *Length = V->getName().size();
873  return V->getName().data();
874 }
875 
876 void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen) {
877  unwrap(Val)->setName(StringRef(Name, NameLen));
878 }
879 
880 const char *LLVMGetValueName(LLVMValueRef Val) {
881  return unwrap(Val)->getName().data();
882 }
883 
884 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
885  unwrap(Val)->setName(Name);
886 }
887 
889  unwrap(Val)->print(errs(), /*IsForDebug=*/true);
890 }
891 
893  std::string buf;
894  raw_string_ostream os(buf);
895 
896  if (unwrap(Val))
897  unwrap(Val)->print(os);
898  else
899  os << "Printing <null> Value";
900 
901  os.flush();
902 
903  return strdup(buf.c_str());
904 }
905 
907  unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
908 }
909 
911  return unwrap<Instruction>(Inst)->hasMetadata();
912 }
913 
914 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
915  auto *I = unwrap<Instruction>(Inst);
916  assert(I && "Expected instruction");
917  if (auto *MD = I->getMetadata(KindID))
918  return wrap(MetadataAsValue::get(I->getContext(), MD));
919  return nullptr;
920 }
921 
922 // MetadataAsValue uses a canonical format which strips the actual MDNode for
923 // MDNode with just a single constant value, storing just a ConstantAsMetadata
924 // This undoes this canonicalization, reconstructing the MDNode.
926  Metadata *MD = MAV->getMetadata();
927  assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
928  "Expected a metadata node or a canonicalized constant");
929 
930  if (MDNode *N = dyn_cast<MDNode>(MD))
931  return N;
932 
933  return MDNode::get(MAV->getContext(), MD);
934 }
935 
936 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
937  MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
938 
939  unwrap<Instruction>(Inst)->setMetadata(KindID, N);
940 }
941 
943  unsigned Kind;
945 };
946 
948 static LLVMValueMetadataEntry *
949 llvm_getMetadata(size_t *NumEntries,
950  llvm::function_ref<void(MetadataEntries &)> AccessMD) {
952  AccessMD(MVEs);
953 
955  static_cast<LLVMOpaqueValueMetadataEntry *>(
956  safe_malloc(MVEs.size() * sizeof(LLVMOpaqueValueMetadataEntry)));
957  for (unsigned i = 0; i < MVEs.size(); ++i) {
958  const auto &ModuleFlag = MVEs[i];
959  Result[i].Kind = ModuleFlag.first;
960  Result[i].Metadata = wrap(ModuleFlag.second);
961  }
962  *NumEntries = MVEs.size();
963  return Result;
964 }
965 
968  size_t *NumEntries) {
969  return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
970  Entries.clear();
971  unwrap<Instruction>(Value)->getAllMetadata(Entries);
972  });
973 }
974 
975 /*--.. Conversion functions ................................................--*/
976 
977 #define LLVM_DEFINE_VALUE_CAST(name) \
978  LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
979  return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
980  }
981 
983 
985  if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
986  if (isa<MDNode>(MD->getMetadata()) ||
987  isa<ValueAsMetadata>(MD->getMetadata()))
988  return Val;
989  return nullptr;
990 }
991 
993  if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
994  if (isa<MDString>(MD->getMetadata()))
995  return Val;
996  return nullptr;
997 }
998 
999 /*--.. Operations on Uses ..................................................--*/
1001  Value *V = unwrap(Val);
1003  if (I == V->use_end())
1004  return nullptr;
1005  return wrap(&*I);
1006 }
1007 
1009  Use *Next = unwrap(U)->getNext();
1010  if (Next)
1011  return wrap(Next);
1012  return nullptr;
1013 }
1014 
1016  return wrap(unwrap(U)->getUser());
1017 }
1018 
1020  return wrap(unwrap(U)->get());
1021 }
1022 
1023 /*--.. Operations on Users .................................................--*/
1024 
1026  unsigned Index) {
1027  Metadata *Op = N->getOperand(Index);
1028  if (!Op)
1029  return nullptr;
1030  if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
1031  return wrap(C->getValue());
1032  return wrap(MetadataAsValue::get(Context, Op));
1033 }
1034 
1036  Value *V = unwrap(Val);
1037  if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
1038  if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1039  assert(Index == 0 && "Function-local metadata can only have one operand");
1040  return wrap(L->getValue());
1041  }
1042  return getMDNodeOperandImpl(V->getContext(),
1043  cast<MDNode>(MD->getMetadata()), Index);
1044  }
1045 
1046  return wrap(cast<User>(V)->getOperand(Index));
1047 }
1048 
1050  Value *V = unwrap(Val);
1051  return wrap(&cast<User>(V)->getOperandUse(Index));
1052 }
1053 
1055  unwrap<User>(Val)->setOperand(Index, unwrap(Op));
1056 }
1057 
1059  Value *V = unwrap(Val);
1060  if (isa<MetadataAsValue>(V))
1061  return LLVMGetMDNodeNumOperands(Val);
1062 
1063  return cast<User>(V)->getNumOperands();
1064 }
1065 
1066 /*--.. Operations on constants of any type .................................--*/
1067 
1069  return wrap(Constant::getNullValue(unwrap(Ty)));
1070 }
1071 
1073  return wrap(Constant::getAllOnesValue(unwrap(Ty)));
1074 }
1075 
1077  return wrap(UndefValue::get(unwrap(Ty)));
1078 }
1079 
1081  return wrap(PoisonValue::get(unwrap(Ty)));
1082 }
1083 
1085  return isa<Constant>(unwrap(Ty));
1086 }
1087 
1089  if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
1090  return C->isNullValue();
1091  return false;
1092 }
1093 
1095  return isa<UndefValue>(unwrap(Val));
1096 }
1097 
1099  return isa<PoisonValue>(unwrap(Val));
1100 }
1101 
1103  return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
1104 }
1105 
1106 /*--.. Operations on metadata nodes ........................................--*/
1107 
1109  size_t SLen) {
1110  return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
1111 }
1112 
1114  size_t Count) {
1115  return wrap(MDNode::get(*unwrap(C), ArrayRef<Metadata*>(unwrap(MDs), Count)));
1116 }
1117 
1119  unsigned SLen) {
1120  LLVMContext &Context = *unwrap(C);
1121  return wrap(MetadataAsValue::get(
1122  Context, MDString::get(Context, StringRef(Str, SLen))));
1123 }
1124 
1125 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
1126  return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
1127 }
1128 
1130  unsigned Count) {
1131  LLVMContext &Context = *unwrap(C);
1133  for (auto *OV : makeArrayRef(Vals, Count)) {
1134  Value *V = unwrap(OV);
1135  Metadata *MD;
1136  if (!V)
1137  MD = nullptr;
1138  else if (auto *C = dyn_cast<Constant>(V))
1139  MD = ConstantAsMetadata::get(C);
1140  else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
1141  MD = MDV->getMetadata();
1142  assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
1143  "outside of direct argument to call");
1144  } else {
1145  // This is function-local metadata. Pretend to make an MDNode.
1146  assert(Count == 1 &&
1147  "Expected only one operand to function-local metadata");
1149  }
1150 
1151  MDs.push_back(MD);
1152  }
1154 }
1155 
1156 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
1157  return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
1158 }
1159 
1161  return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
1162 }
1163 
1165  auto *V = unwrap(Val);
1166  if (auto *C = dyn_cast<Constant>(V))
1167  return wrap(ConstantAsMetadata::get(C));
1168  if (auto *MAV = dyn_cast<MetadataAsValue>(V))
1169  return wrap(MAV->getMetadata());
1170  return wrap(ValueAsMetadata::get(V));
1171 }
1172 
1173 const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
1174  if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
1175  if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
1176  *Length = S->getString().size();
1177  return S->getString().data();
1178  }
1179  *Length = 0;
1180  return nullptr;
1181 }
1182 
1184  auto *MD = unwrap<MetadataAsValue>(V);
1185  if (isa<ValueAsMetadata>(MD->getMetadata()))
1186  return 1;
1187  return cast<MDNode>(MD->getMetadata())->getNumOperands();
1188 }
1189 
1191  Module *Mod = unwrap(M);
1193  if (I == Mod->named_metadata_end())
1194  return nullptr;
1195  return wrap(&*I);
1196 }
1197 
1199  Module *Mod = unwrap(M);
1201  if (I == Mod->named_metadata_begin())
1202  return nullptr;
1203  return wrap(&*--I);
1204 }
1205 
1207  NamedMDNode *NamedNode = unwrap(NMD);
1209  if (++I == NamedNode->getParent()->named_metadata_end())
1210  return nullptr;
1211  return wrap(&*I);
1212 }
1213 
1215  NamedMDNode *NamedNode = unwrap(NMD);
1217  if (I == NamedNode->getParent()->named_metadata_begin())
1218  return nullptr;
1219  return wrap(&*--I);
1220 }
1221 
1223  const char *Name, size_t NameLen) {
1224  return wrap(unwrap(M)->getNamedMetadata(StringRef(Name, NameLen)));
1225 }
1226 
1228  const char *Name, size_t NameLen) {
1229  return wrap(unwrap(M)->getOrInsertNamedMetadata({Name, NameLen}));
1230 }
1231 
1232 const char *LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NMD, size_t *NameLen) {
1233  NamedMDNode *NamedNode = unwrap(NMD);
1234  *NameLen = NamedNode->getName().size();
1235  return NamedNode->getName().data();
1236 }
1237 
1239  auto *MD = unwrap<MetadataAsValue>(V);
1240  if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1241  *Dest = wrap(MDV->getValue());
1242  return;
1243  }
1244  const auto *N = cast<MDNode>(MD->getMetadata());
1245  const unsigned numOperands = N->getNumOperands();
1246  LLVMContext &Context = unwrap(V)->getContext();
1247  for (unsigned i = 0; i < numOperands; i++)
1248  Dest[i] = getMDNodeOperandImpl(Context, N, i);
1249 }
1250 
1251 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char *Name) {
1252  if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
1253  return N->getNumOperands();
1254  }
1255  return 0;
1256 }
1257 
1259  LLVMValueRef *Dest) {
1260  NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
1261  if (!N)
1262  return;
1263  LLVMContext &Context = unwrap(M)->getContext();
1264  for (unsigned i=0;i<N->getNumOperands();i++)
1265  Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
1266 }
1267 
1269  LLVMValueRef Val) {
1270  NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
1271  if (!N)
1272  return;
1273  if (!Val)
1274  return;
1275  N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
1276 }
1277 
1278 const char *LLVMGetDebugLocDirectory(LLVMValueRef Val, unsigned *Length) {
1279  if (!Length) return nullptr;
1280  StringRef S;
1281  if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1282  if (const auto &DL = I->getDebugLoc()) {
1283  S = DL->getDirectory();
1284  }
1285  } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1287  GV->getDebugInfo(GVEs);
1288  if (GVEs.size())
1289  if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1290  S = DGV->getDirectory();
1291  } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1292  if (const DISubprogram *DSP = F->getSubprogram())
1293  S = DSP->getDirectory();
1294  } else {
1295  assert(0 && "Expected Instruction, GlobalVariable or Function");
1296  return nullptr;
1297  }
1298  *Length = S.size();
1299  return S.data();
1300 }
1301 
1302 const char *LLVMGetDebugLocFilename(LLVMValueRef Val, unsigned *Length) {
1303  if (!Length) return nullptr;
1304  StringRef S;
1305  if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1306  if (const auto &DL = I->getDebugLoc()) {
1307  S = DL->getFilename();
1308  }
1309  } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1311  GV->getDebugInfo(GVEs);
1312  if (GVEs.size())
1313  if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1314  S = DGV->getFilename();
1315  } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1316  if (const DISubprogram *DSP = F->getSubprogram())
1317  S = DSP->getFilename();
1318  } else {
1319  assert(0 && "Expected Instruction, GlobalVariable or Function");
1320  return nullptr;
1321  }
1322  *Length = S.size();
1323  return S.data();
1324 }
1325 
1327  unsigned L = 0;
1328  if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1329  if (const auto &DL = I->getDebugLoc()) {
1330  L = DL->getLine();
1331  }
1332  } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1334  GV->getDebugInfo(GVEs);
1335  if (GVEs.size())
1336  if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1337  L = DGV->getLine();
1338  } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1339  if (const DISubprogram *DSP = F->getSubprogram())
1340  L = DSP->getLine();
1341  } else {
1342  assert(0 && "Expected Instruction, GlobalVariable or Function");
1343  return -1;
1344  }
1345  return L;
1346 }
1347 
1349  unsigned C = 0;
1350  if (const auto *I = dyn_cast<Instruction>(unwrap(Val)))
1351  if (const auto &DL = I->getDebugLoc())
1352  C = DL->getColumn();
1353  return C;
1354 }
1355 
1356 /*--.. Operations on scalar constants ......................................--*/
1357 
1358 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
1359  LLVMBool SignExtend) {
1360  return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
1361 }
1362 
1364  unsigned NumWords,
1365  const uint64_t Words[]) {
1366  IntegerType *Ty = unwrap<IntegerType>(IntTy);
1367  return wrap(ConstantInt::get(Ty->getContext(),
1368  APInt(Ty->getBitWidth(),
1369  makeArrayRef(Words, NumWords))));
1370 }
1371 
1373  uint8_t Radix) {
1374  return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
1375  Radix));
1376 }
1377 
1379  unsigned SLen, uint8_t Radix) {
1380  return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
1381  Radix));
1382 }
1383 
1385  return wrap(ConstantFP::get(unwrap(RealTy), N));
1386 }
1387 
1389  return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
1390 }
1391 
1393  unsigned SLen) {
1394  return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
1395 }
1396 
1397 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
1398  return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
1399 }
1400 
1401 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
1402  return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
1403 }
1404 
1405 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
1406  ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
1407  Type *Ty = cFP->getType();
1408 
1409  if (Ty->isHalfTy() || Ty->isBFloatTy() || Ty->isFloatTy() ||
1410  Ty->isDoubleTy()) {
1411  *LosesInfo = false;
1412  return cFP->getValueAPF().convertToDouble();
1413  }
1414 
1415  bool APFLosesInfo;
1416  APFloat APF = cFP->getValueAPF();
1418  *LosesInfo = APFLosesInfo;
1419  return APF.convertToDouble();
1420 }
1421 
1422 /*--.. Operations on composite constants ...................................--*/
1423 
1425  unsigned Length,
1426  LLVMBool DontNullTerminate) {
1427  /* Inverted the sense of AddNull because ', 0)' is a
1428  better mnemonic for null termination than ', 1)'. */
1429  return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
1430  DontNullTerminate == 0));
1431 }
1432 
1433 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
1434  LLVMBool DontNullTerminate) {
1435  return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
1436  DontNullTerminate);
1437 }
1438 
1440  return wrap(unwrap<Constant>(C)->getAggregateElement(Idx));
1441 }
1442 
1444  return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
1445 }
1446 
1448  return unwrap<ConstantDataSequential>(C)->isString();
1449 }
1450 
1451 const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1452  StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1453  *Length = Str.size();
1454  return Str.data();
1455 }
1456 
1458  LLVMValueRef *ConstantVals, unsigned Length) {
1459  ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1460  return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1461 }
1462 
1464  LLVMValueRef *ConstantVals,
1465  unsigned Count, LLVMBool Packed) {
1466  Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1467  return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
1468  Packed != 0));
1469 }
1470 
1471 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
1472  LLVMBool Packed) {
1473  return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1474  Packed);
1475 }
1476 
1478  LLVMValueRef *ConstantVals,
1479  unsigned Count) {
1480  Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1481  StructType *Ty = unwrap<StructType>(StructTy);
1482 
1483  return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
1484 }
1485 
1486 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
1488  unwrap<Constant>(ScalarConstantVals, Size), Size)));
1489 }
1490 
1491 /*-- Opcode mapping */
1492 
1493 static LLVMOpcode map_to_llvmopcode(int opcode)
1494 {
1495  switch (opcode) {
1496  default: llvm_unreachable("Unhandled Opcode.");
1497 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
1498 #include "llvm/IR/Instruction.def"
1499 #undef HANDLE_INST
1500  }
1501 }
1502 
1504 {
1505  switch (code) {
1506 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
1507 #include "llvm/IR/Instruction.def"
1508 #undef HANDLE_INST
1509  }
1510  llvm_unreachable("Unhandled Opcode.");
1511 }
1512 
1513 /*--.. Constant expressions ................................................--*/
1514 
1516  return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
1517 }
1518 
1520  return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
1521 }
1522 
1524  return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
1525 }
1526 
1528  return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1529 }
1530 
1532  return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
1533 }
1534 
1536  return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
1537 }
1538 
1539 
1541  return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
1542 }
1543 
1545  return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
1546  unwrap<Constant>(RHSConstant)));
1547 }
1548 
1550  LLVMValueRef RHSConstant) {
1551  return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
1552  unwrap<Constant>(RHSConstant)));
1553 }
1554 
1556  LLVMValueRef RHSConstant) {
1557  return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
1558  unwrap<Constant>(RHSConstant)));
1559 }
1560 
1562  return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
1563  unwrap<Constant>(RHSConstant)));
1564 }
1565 
1567  LLVMValueRef RHSConstant) {
1568  return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
1569  unwrap<Constant>(RHSConstant)));
1570 }
1571 
1573  LLVMValueRef RHSConstant) {
1574  return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
1575  unwrap<Constant>(RHSConstant)));
1576 }
1577 
1579  return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
1580  unwrap<Constant>(RHSConstant)));
1581 }
1582 
1584  LLVMValueRef RHSConstant) {
1585  return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
1586  unwrap<Constant>(RHSConstant)));
1587 }
1588 
1590  LLVMValueRef RHSConstant) {
1591  return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
1592  unwrap<Constant>(RHSConstant)));
1593 }
1594 
1596  return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
1597  unwrap<Constant>(RHSConstant)));
1598 }
1599 
1601  return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
1602  unwrap<Constant>(RHSConstant)));
1603 }
1604 
1606  return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
1607  unwrap<Constant>(RHSConstant)));
1608 }
1609 
1611  LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1613  unwrap<Constant>(LHSConstant),
1614  unwrap<Constant>(RHSConstant)));
1615 }
1616 
1618  LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1620  unwrap<Constant>(LHSConstant),
1621  unwrap<Constant>(RHSConstant)));
1622 }
1623 
1625  return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1626  unwrap<Constant>(RHSConstant)));
1627 }
1628 
1630  return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
1631  unwrap<Constant>(RHSConstant)));
1632 }
1633 
1635  return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1636  unwrap<Constant>(RHSConstant)));
1637 }
1638 
1640  LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1641  ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1642  NumIndices);
1643  Constant *Val = unwrap<Constant>(ConstantVal);
1645  return wrap(ConstantExpr::getGetElementPtr(Ty, Val, IdxList));
1646 }
1647 
1649  LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1650  ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1651  NumIndices);
1652  Constant *Val = unwrap<Constant>(ConstantVal);
1653  return wrap(ConstantExpr::getGetElementPtr(unwrap(Ty), Val, IdxList));
1654 }
1655 
1657  LLVMValueRef *ConstantIndices,
1658  unsigned NumIndices) {
1659  ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1660  NumIndices);
1661  Constant *Val = unwrap<Constant>(ConstantVal);
1663  return wrap(ConstantExpr::getInBoundsGetElementPtr(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  const char *Name) {
2214  auto *PTy = unwrap<PointerType>(Ty);
2215  return wrap(GlobalAlias::create(PTy->getNonOpaquePointerElementType(),
2216  PTy->getAddressSpace(),
2218  unwrap<Constant>(Aliasee), unwrap(M)));
2219 }
2220 
2222  unsigned AddrSpace, LLVMValueRef Aliasee,
2223  const char *Name) {
2224  return wrap(GlobalAlias::create(unwrap(ValueTy), AddrSpace,
2226  unwrap<Constant>(Aliasee), unwrap(M)));
2227 }
2228 
2230  const char *Name, size_t NameLen) {
2231  return wrap(unwrap(M)->getNamedAlias(StringRef(Name, NameLen)));
2232 }
2233 
2235  Module *Mod = unwrap(M);
2237  if (I == Mod->alias_end())
2238  return nullptr;
2239  return wrap(&*I);
2240 }
2241 
2243  Module *Mod = unwrap(M);
2245  if (I == Mod->alias_begin())
2246  return nullptr;
2247  return wrap(&*--I);
2248 }
2249 
2251  GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2253  if (++I == Alias->getParent()->alias_end())
2254  return nullptr;
2255  return wrap(&*I);
2256 }
2257 
2259  GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2261  if (I == Alias->getParent()->alias_begin())
2262  return nullptr;
2263  return wrap(&*--I);
2264 }
2265 
2267  return wrap(unwrap<GlobalAlias>(Alias)->getAliasee());
2268 }
2269 
2271  unwrap<GlobalAlias>(Alias)->setAliasee(unwrap<Constant>(Aliasee));
2272 }
2273 
2274 /*--.. Operations on functions .............................................--*/
2275 
2277  LLVMTypeRef FunctionTy) {
2278  return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
2280 }
2281 
2283  return wrap(unwrap(M)->getFunction(Name));
2284 }
2285 
2287  Module *Mod = unwrap(M);
2288  Module::iterator I = Mod->begin();
2289  if (I == Mod->end())
2290  return nullptr;
2291  return wrap(&*I);
2292 }
2293 
2295  Module *Mod = unwrap(M);
2296  Module::iterator I = Mod->end();
2297  if (I == Mod->begin())
2298  return nullptr;
2299  return wrap(&*--I);
2300 }
2301 
2303  Function *Func = unwrap<Function>(Fn);
2304  Module::iterator I(Func);
2305  if (++I == Func->getParent()->end())
2306  return nullptr;
2307  return wrap(&*I);
2308 }
2309 
2311  Function *Func = unwrap<Function>(Fn);
2312  Module::iterator I(Func);
2313  if (I == Func->getParent()->begin())
2314  return nullptr;
2315  return wrap(&*--I);
2316 }
2317 
2319  unwrap<Function>(Fn)->eraseFromParent();
2320 }
2321 
2323  return unwrap<Function>(Fn)->hasPersonalityFn();
2324 }
2325 
2327  return wrap(unwrap<Function>(Fn)->getPersonalityFn());
2328 }
2329 
2331  unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
2332 }
2333 
2335  if (Function *F = dyn_cast<Function>(unwrap(Fn)))
2336  return F->getIntrinsicID();
2337  return 0;
2338 }
2339 
2341  assert(ID < llvm::Intrinsic::num_intrinsics && "Intrinsic ID out of range");
2342  return llvm::Intrinsic::ID(ID);
2343 }
2344 
2346  unsigned ID,
2347  LLVMTypeRef *ParamTypes,
2348  size_t ParamCount) {
2349  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2350  auto IID = llvm_map_to_intrinsic_id(ID);
2351  return wrap(llvm::Intrinsic::getDeclaration(unwrap(Mod), IID, Tys));
2352 }
2353 
2354 const char *LLVMIntrinsicGetName(unsigned ID, size_t *NameLength) {
2355  auto IID = llvm_map_to_intrinsic_id(ID);
2356  auto Str = llvm::Intrinsic::getName(IID);
2357  *NameLength = Str.size();
2358  return Str.data();
2359 }
2360 
2362  LLVMTypeRef *ParamTypes, size_t ParamCount) {
2363  auto IID = llvm_map_to_intrinsic_id(ID);
2364  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2365  return wrap(llvm::Intrinsic::getType(*unwrap(Ctx), IID, Tys));
2366 }
2367 
2369  LLVMTypeRef *ParamTypes,
2370  size_t ParamCount,
2371  size_t *NameLength) {
2372  auto IID = llvm_map_to_intrinsic_id(ID);
2373  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2374  auto Str = llvm::Intrinsic::getNameNoUnnamedTypes(IID, Tys);
2375  *NameLength = Str.length();
2376  return strdup(Str.c_str());
2377 }
2378 
2380  LLVMTypeRef *ParamTypes,
2381  size_t ParamCount,
2382  size_t *NameLength) {
2383  auto IID = llvm_map_to_intrinsic_id(ID);
2384  ArrayRef<Type *> Tys(unwrap(ParamTypes), ParamCount);
2385  auto Str = llvm::Intrinsic::getName(IID, Tys, unwrap(Mod));
2386  *NameLength = Str.length();
2387  return strdup(Str.c_str());
2388 }
2389 
2390 unsigned LLVMLookupIntrinsicID(const char *Name, size_t NameLen) {
2391  return Function::lookupIntrinsicID({Name, NameLen});
2392 }
2393 
2395  auto IID = llvm_map_to_intrinsic_id(ID);
2396  return llvm::Intrinsic::isOverloaded(IID);
2397 }
2398 
2400  return unwrap<Function>(Fn)->getCallingConv();
2401 }
2402 
2404  return unwrap<Function>(Fn)->setCallingConv(
2405  static_cast<CallingConv::ID>(CC));
2406 }
2407 
2408 const char *LLVMGetGC(LLVMValueRef Fn) {
2409  Function *F = unwrap<Function>(Fn);
2410  return F->hasGC()? F->getGC().c_str() : nullptr;
2411 }
2412 
2413 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
2414  Function *F = unwrap<Function>(Fn);
2415  if (GC)
2416  F->setGC(GC);
2417  else
2418  F->clearGC();
2419 }
2420 
2422  LLVMAttributeRef A) {
2423  unwrap<Function>(F)->addAttributeAtIndex(Idx, unwrap(A));
2424 }
2425 
2427  auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2428  return AS.getNumAttributes();
2429 }
2430 
2433  auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2434  for (auto A : AS)
2435  *Attrs++ = wrap(A);
2436 }
2437 
2439  LLVMAttributeIndex Idx,
2440  unsigned KindID) {
2441  return wrap(unwrap<Function>(F)->getAttributeAtIndex(
2442  Idx, (Attribute::AttrKind)KindID));
2443 }
2444 
2446  LLVMAttributeIndex Idx,
2447  const char *K, unsigned KLen) {
2448  return wrap(
2449  unwrap<Function>(F)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
2450 }
2451 
2453  unsigned KindID) {
2454  unwrap<Function>(F)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
2455 }
2456 
2458  const char *K, unsigned KLen) {
2459  unwrap<Function>(F)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
2460 }
2461 
2463  const char *V) {
2464  Function *Func = unwrap<Function>(Fn);
2465  Attribute Attr = Attribute::get(Func->getContext(), A, V);
2466  Func->addFnAttr(Attr);
2467 }
2468 
2469 /*--.. Operations on parameters ............................................--*/
2470 
2472  // This function is strictly redundant to
2473  // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
2474  return unwrap<Function>(FnRef)->arg_size();
2475 }
2476 
2477 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
2478  Function *Fn = unwrap<Function>(FnRef);
2479  for (Argument &A : Fn->args())
2480  *ParamRefs++ = wrap(&A);
2481 }
2482 
2484  Function *Fn = unwrap<Function>(FnRef);
2485  return wrap(&Fn->arg_begin()[index]);
2486 }
2487 
2489  return wrap(unwrap<Argument>(V)->getParent());
2490 }
2491 
2493  Function *Func = unwrap<Function>(Fn);
2494  Function::arg_iterator I = Func->arg_begin();
2495  if (I == Func->arg_end())
2496  return nullptr;
2497  return wrap(&*I);
2498 }
2499 
2501  Function *Func = unwrap<Function>(Fn);
2502  Function::arg_iterator I = Func->arg_end();
2503  if (I == Func->arg_begin())
2504  return nullptr;
2505  return wrap(&*--I);
2506 }
2507 
2509  Argument *A = unwrap<Argument>(Arg);
2510  Function *Fn = A->getParent();
2511  if (A->getArgNo() + 1 >= Fn->arg_size())
2512  return nullptr;
2513  return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
2514 }
2515 
2517  Argument *A = unwrap<Argument>(Arg);
2518  if (A->getArgNo() == 0)
2519  return nullptr;
2520  return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
2521 }
2522 
2524  Argument *A = unwrap<Argument>(Arg);
2525  A->addAttr(Attribute::getWithAlignment(A->getContext(), Align(align)));
2526 }
2527 
2528 /*--.. Operations on ifuncs ................................................--*/
2529 
2531  const char *Name, size_t NameLen,
2532  LLVMTypeRef Ty, unsigned AddrSpace,
2534  return wrap(GlobalIFunc::create(unwrap(Ty), AddrSpace,
2536  StringRef(Name, NameLen),
2537  unwrap<Constant>(Resolver), unwrap(M)));
2538 }
2539 
2541  const char *Name, size_t NameLen) {
2542  return wrap(unwrap(M)->getNamedIFunc(StringRef(Name, NameLen)));
2543 }
2544 
2546  Module *Mod = unwrap(M);
2548  if (I == Mod->ifunc_end())
2549  return nullptr;
2550  return wrap(&*I);
2551 }
2552 
2554  Module *Mod = unwrap(M);
2556  if (I == Mod->ifunc_begin())
2557  return nullptr;
2558  return wrap(&*--I);
2559 }
2560 
2562  GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2564  if (++I == GIF->getParent()->ifunc_end())
2565  return nullptr;
2566  return wrap(&*I);
2567 }
2568 
2570  GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2572  if (I == GIF->getParent()->ifunc_begin())
2573  return nullptr;
2574  return wrap(&*--I);
2575 }
2576 
2578  return wrap(unwrap<GlobalIFunc>(IFunc)->getResolver());
2579 }
2580 
2582  unwrap<GlobalIFunc>(IFunc)->setResolver(unwrap<Constant>(Resolver));
2583 }
2584 
2586  unwrap<GlobalIFunc>(IFunc)->eraseFromParent();
2587 }
2588 
2590  unwrap<GlobalIFunc>(IFunc)->removeFromParent();
2591 }
2592 
2593 /*--.. Operations on basic blocks ..........................................--*/
2594 
2596  return wrap(static_cast<Value*>(unwrap(BB)));
2597 }
2598 
2600  return isa<BasicBlock>(unwrap(Val));
2601 }
2602 
2604  return wrap(unwrap<BasicBlock>(Val));
2605 }
2606 
2608  return unwrap(BB)->getName().data();
2609 }
2610 
2612  return wrap(unwrap(BB)->getParent());
2613 }
2614 
2616  return wrap(unwrap(BB)->getTerminator());
2617 }
2618 
2620  return unwrap<Function>(FnRef)->size();
2621 }
2622 
2623 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
2624  Function *Fn = unwrap<Function>(FnRef);
2625  for (BasicBlock &BB : *Fn)
2626  *BasicBlocksRefs++ = wrap(&BB);
2627 }
2628 
2630  return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2631 }
2632 
2634  Function *Func = unwrap<Function>(Fn);
2635  Function::iterator I = Func->begin();
2636  if (I == Func->end())
2637  return nullptr;
2638  return wrap(&*I);
2639 }
2640 
2642  Function *Func = unwrap<Function>(Fn);
2643  Function::iterator I = Func->end();
2644  if (I == Func->begin())
2645  return nullptr;
2646  return wrap(&*--I);
2647 }
2648 
2650  BasicBlock *Block = unwrap(BB);
2651  Function::iterator I(Block);
2652  if (++I == Block->getParent()->end())
2653  return nullptr;
2654  return wrap(&*I);
2655 }
2656 
2658  BasicBlock *Block = unwrap(BB);
2659  Function::iterator I(Block);
2660  if (I == Block->getParent()->begin())
2661  return nullptr;
2662  return wrap(&*--I);
2663 }
2664 
2666  const char *Name) {
2667  return wrap(llvm::BasicBlock::Create(*unwrap(C), Name));
2668 }
2669 
2672  BasicBlock *ToInsert = unwrap(BB);
2673  BasicBlock *CurBB = unwrap(Builder)->GetInsertBlock();
2674  assert(CurBB && "current insertion point is invalid!");
2675  CurBB->getParent()->getBasicBlockList().insertAfter(CurBB->getIterator(),
2676  ToInsert);
2677 }
2678 
2681  unwrap<Function>(Fn)->getBasicBlockList().push_back(unwrap(BB));
2682 }
2683 
2685  LLVMValueRef FnRef,
2686  const char *Name) {
2687  return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
2688 }
2689 
2691  return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
2692 }
2693 
2695  LLVMBasicBlockRef BBRef,
2696  const char *Name) {
2697  BasicBlock *BB = unwrap(BBRef);
2698  return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2699 }
2700 
2702  const char *Name) {
2703  return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
2704 }
2705 
2707  unwrap(BBRef)->eraseFromParent();
2708 }
2709 
2711  unwrap(BBRef)->removeFromParent();
2712 }
2713 
2715  unwrap(BB)->moveBefore(unwrap(MovePos));
2716 }
2717 
2719  unwrap(BB)->moveAfter(unwrap(MovePos));
2720 }
2721 
2722 /*--.. Operations on instructions ..........................................--*/
2723 
2725  return wrap(unwrap<Instruction>(Inst)->getParent());
2726 }
2727 
2729  BasicBlock *Block = unwrap(BB);
2730  BasicBlock::iterator I = Block->begin();
2731  if (I == Block->end())
2732  return nullptr;
2733  return wrap(&*I);
2734 }
2735 
2737  BasicBlock *Block = unwrap(BB);
2738  BasicBlock::iterator I = Block->end();
2739  if (I == Block->begin())
2740  return nullptr;
2741  return wrap(&*--I);
2742 }
2743 
2745  Instruction *Instr = unwrap<Instruction>(Inst);
2746  BasicBlock::iterator I(Instr);
2747  if (++I == Instr->getParent()->end())
2748  return nullptr;
2749  return wrap(&*I);
2750 }
2751 
2753  Instruction *Instr = unwrap<Instruction>(Inst);
2754  BasicBlock::iterator I(Instr);
2755  if (I == Instr->getParent()->begin())
2756  return nullptr;
2757  return wrap(&*--I);
2758 }
2759 
2761  unwrap<Instruction>(Inst)->removeFromParent();
2762 }
2763 
2765  unwrap<Instruction>(Inst)->eraseFromParent();
2766 }
2767 
2769  unwrap<Instruction>(Inst)->deleteValue();
2770 }
2771 
2773  if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2774  return (LLVMIntPredicate)I->getPredicate();
2775  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2776  if (CE->getOpcode() == Instruction::ICmp)
2777  return (LLVMIntPredicate)CE->getPredicate();
2778  return (LLVMIntPredicate)0;
2779 }
2780 
2782  if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2783  return (LLVMRealPredicate)I->getPredicate();
2784  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2785  if (CE->getOpcode() == Instruction::FCmp)
2786  return (LLVMRealPredicate)CE->getPredicate();
2787  return (LLVMRealPredicate)0;
2788 }
2789 
2791  if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2792  return map_to_llvmopcode(C->getOpcode());
2793  return (LLVMOpcode)0;
2794 }
2795 
2797  if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2798  return wrap(C->clone());
2799  return nullptr;
2800 }
2801 
2803  Instruction *I = dyn_cast<Instruction>(unwrap(Inst));
2804  return (I && I->isTerminator()) ? wrap(I) : nullptr;
2805 }
2806 
2808  if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) {
2809  return FPI->arg_size();
2810  }
2811  return unwrap<CallBase>(Instr)->arg_size();
2812 }
2813 
2814 /*--.. Call and invoke instructions ........................................--*/
2815 
2817  return unwrap<CallBase>(Instr)->getCallingConv();
2818 }
2819 
2821  return unwrap<CallBase>(Instr)->setCallingConv(
2822  static_cast<CallingConv::ID>(CC));
2823 }
2824 
2826  unsigned align) {
2827  auto *Call = unwrap<CallBase>(Instr);
2828  Attribute AlignAttr =
2829  Attribute::getWithAlignment(Call->getContext(), Align(align));
2830  Call->addAttributeAtIndex(Idx, AlignAttr);
2831 }
2832 
2834  LLVMAttributeRef A) {
2835  unwrap<CallBase>(C)->addAttributeAtIndex(Idx, unwrap(A));
2836 }
2837 
2839  LLVMAttributeIndex Idx) {
2840  auto *Call = unwrap<CallBase>(C);
2841  auto AS = Call->getAttributes().getAttributes(Idx);
2842  return AS.getNumAttributes();
2843 }
2844 
2847  auto *Call = unwrap<CallBase>(C);
2848  auto AS = Call->getAttributes().getAttributes(Idx);
2849  for (auto A : AS)
2850  *Attrs++ = wrap(A);
2851 }
2852 
2854  LLVMAttributeIndex Idx,
2855  unsigned KindID) {
2856  return wrap(unwrap<CallBase>(C)->getAttributeAtIndex(
2857  Idx, (Attribute::AttrKind)KindID));
2858 }
2859 
2861  LLVMAttributeIndex Idx,
2862  const char *K, unsigned KLen) {
2863  return wrap(
2864  unwrap<CallBase>(C)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
2865 }
2866 
2868  unsigned KindID) {
2869  unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
2870 }
2871 
2873  const char *K, unsigned KLen) {
2874  unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
2875 }
2876 
2878  return wrap(unwrap<CallBase>(Instr)->getCalledOperand());
2879 }
2880 
2882  return wrap(unwrap<CallBase>(Instr)->getFunctionType());
2883 }
2884 
2885 /*--.. Operations on call instructions (only) ..............................--*/
2886 
2888  return unwrap<CallInst>(Call)->isTailCall();
2889 }
2890 
2891 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
2892  unwrap<CallInst>(Call)->setTailCall(isTailCall);
2893 }
2894 
2895 /*--.. Operations on invoke instructions (only) ............................--*/
2896 
2898  return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2899 }
2900 
2902  if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2903  return wrap(CRI->getUnwindDest());
2904  } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2905  return wrap(CSI->getUnwindDest());
2906  }
2907  return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2908 }
2909 
2911  unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2912 }
2913 
2915  if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2916  return CRI->setUnwindDest(unwrap(B));
2917  } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2918  return CSI->setUnwindDest(unwrap(B));
2919  }
2920  unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2921 }
2922 
2923 /*--.. Operations on terminators ...........................................--*/
2924 
2926  return unwrap<Instruction>(Term)->getNumSuccessors();
2927 }
2928 
2930  return wrap(unwrap<Instruction>(Term)->getSuccessor(i));
2931 }
2932 
2934  return unwrap<Instruction>(Term)->setSuccessor(i, unwrap(block));
2935 }
2936 
2937 /*--.. Operations on branch instructions (only) ............................--*/
2938 
2940  return unwrap<BranchInst>(Branch)->isConditional();
2941 }
2942 
2944  return wrap(unwrap<BranchInst>(Branch)->getCondition());
2945 }
2946 
2948  return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2949 }
2950 
2951 /*--.. Operations on switch instructions (only) ............................--*/
2952 
2954  return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2955 }
2956 
2957 /*--.. Operations on alloca instructions (only) ............................--*/
2958 
2960  return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
2961 }
2962 
2963 /*--.. Operations on gep instructions (only) ...............................--*/
2964 
2966  return unwrap<GEPOperator>(GEP)->isInBounds();
2967 }
2968 
2970  return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
2971 }
2972 
2974  return wrap(unwrap<GEPOperator>(GEP)->getSourceElementType());
2975 }
2976 
2977 /*--.. Operations on phi nodes .............................................--*/
2978 
2979 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
2980  LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
2981  PHINode *PhiVal = unwrap<PHINode>(PhiNode);
2982  for (unsigned I = 0; I != Count; ++I)
2983  PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
2984 }
2985 
2986 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
2987  return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
2988 }
2989 
2991  return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
2992 }
2993 
2995  return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
2996 }
2997 
2998 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/
2999 
3001  auto *I = unwrap(Inst);
3002  if (auto *GEP = dyn_cast<GEPOperator>(I))
3003  return GEP->getNumIndices();
3004  if (auto *EV = dyn_cast<ExtractValueInst>(I))
3005  return EV->getNumIndices();
3006  if (auto *IV = dyn_cast<InsertValueInst>(I))
3007  return IV->getNumIndices();
3009  "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
3010 }
3011 
3012 const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
3013  auto *I = unwrap(Inst);
3014  if (auto *EV = dyn_cast<ExtractValueInst>(I))
3015  return EV->getIndices().data();
3016  if (auto *IV = dyn_cast<InsertValueInst>(I))
3017  return IV->getIndices().data();
3019  "LLVMGetIndices applies only to extractvalue and insertvalue!");
3020 }
3021 
3022 
3023 /*===-- Instruction builders ----------------------------------------------===*/
3024 
3026  return wrap(new IRBuilder<>(*unwrap(C)));
3027 }
3028 
3031 }
3032 
3034  LLVMValueRef Instr) {
3035  BasicBlock *BB = unwrap(Block);
3036  auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
3037  unwrap(Builder)->SetInsertPoint(BB, I);
3038 }
3039 
3041  Instruction *I = unwrap<Instruction>(Instr);
3042  unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
3043 }
3044 
3046  BasicBlock *BB = unwrap(Block);
3047  unwrap(Builder)->SetInsertPoint(BB);
3048 }
3049 
3051  return wrap(unwrap(Builder)->GetInsertBlock());
3052 }
3053 
3055  unwrap(Builder)->ClearInsertionPoint();
3056 }
3057 
3059  unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
3060 }
3061 
3063  const char *Name) {
3064  unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
3065 }
3066 
3068  delete unwrap(Builder);
3069 }
3070 
3071 /*--.. Metadata builders ...................................................--*/
3072 
3074  return wrap(unwrap(Builder)->getCurrentDebugLocation().getAsMDNode());
3075 }
3076 
3078  if (Loc)
3079  unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(unwrap<MDNode>(Loc)));
3080  else
3081  unwrap(Builder)->SetCurrentDebugLocation(DebugLoc());
3082 }
3083 
3085  MDNode *Loc =
3086  L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
3087  unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
3088 }
3089 
3091  LLVMContext &Context = unwrap(Builder)->getContext();
3092  return wrap(MetadataAsValue::get(
3093  Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
3094 }
3095 
3097  unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
3098 }
3099 
3101  unwrap(Builder)->AddMetadataToInst(unwrap<Instruction>(Inst));
3102 }
3103 
3105  LLVMMetadataRef FPMathTag) {
3106 
3107  unwrap(Builder)->setDefaultFPMathTag(FPMathTag
3108  ? unwrap<MDNode>(FPMathTag)
3109  : nullptr);
3110 }
3111 
3113  return wrap(unwrap(Builder)->getDefaultFPMathTag());
3114 }
3115 
3116 /*--.. Instruction builders ................................................--*/
3117 
3119  return wrap(unwrap(B)->CreateRetVoid());
3120 }
3121 
3123  return wrap(unwrap(B)->CreateRet(unwrap(V)));
3124 }
3125 
3127  unsigned N) {
3128  return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
3129 }
3130 
3132  return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
3133 }
3134 
3137  return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
3138 }
3139 
3141  LLVMBasicBlockRef Else, unsigned NumCases) {
3142  return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
3143 }
3144 
3146  unsigned NumDests) {
3147  return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
3148 }
3149 
3151  LLVMValueRef *Args, unsigned NumArgs,
3153  const char *Name) {
3154  Value *V = unwrap(Fn);
3155  FunctionType *FnT =
3156  cast<FunctionType>(V->getType()->getNonOpaquePointerElementType());
3157 
3158  return wrap(
3159  unwrap(B)->CreateInvoke(FnT, unwrap(Fn), unwrap(Then), unwrap(Catch),
3160  makeArrayRef(unwrap(Args), NumArgs), Name));
3161 }
3162 
3164  LLVMValueRef *Args, unsigned NumArgs,
3166  const char *Name) {
3167  return wrap(unwrap(B)->CreateInvoke(
3168  unwrap<FunctionType>(Ty), unwrap(Fn), unwrap(Then), unwrap(Catch),
3169  makeArrayRef(unwrap(Args), NumArgs), Name));
3170 }
3171 
3173  LLVMValueRef PersFn, unsigned NumClauses,
3174  const char *Name) {
3175  // The personality used to live on the landingpad instruction, but now it
3176  // lives on the parent function. For compatibility, take the provided
3177  // personality and put it on the parent function.
3178  if (PersFn)
3179  unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
3180  unwrap<Function>(PersFn));
3181  return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
3182 }
3183 
3185  LLVMValueRef *Args, unsigned NumArgs,
3186  const char *Name) {
3187  return wrap(unwrap(B)->CreateCatchPad(unwrap(ParentPad),
3188  makeArrayRef(unwrap(Args), NumArgs),
3189  Name));
3190 }
3191 
3193  LLVMValueRef *Args, unsigned NumArgs,
3194  const char *Name) {
3195  if (ParentPad == nullptr) {
3196  Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3197  ParentPad = wrap(Constant::getNullValue(Ty));
3198  }
3199  return wrap(unwrap(B)->CreateCleanupPad(unwrap(ParentPad),
3200  makeArrayRef(unwrap(Args), NumArgs),
3201  Name));
3202 }
3203 
3205  return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
3206 }
3207 
3209  LLVMBasicBlockRef UnwindBB,
3210  unsigned NumHandlers, const char *Name) {
3211  if (ParentPad == nullptr) {
3212  Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3213  ParentPad = wrap(Constant::getNullValue(Ty));
3214  }
3215  return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(UnwindBB),
3216  NumHandlers, Name));
3217 }
3218 
3221  return wrap(unwrap(B)->CreateCatchRet(unwrap<CatchPadInst>(CatchPad),
3222  unwrap(BB)));
3223 }
3224 
3227  return wrap(unwrap(B)->CreateCleanupRet(unwrap<CleanupPadInst>(CatchPad),
3228  unwrap(BB)));
3229 }
3230 
3232  return wrap(unwrap(B)->CreateUnreachable());
3233 }
3234 
3236  LLVMBasicBlockRef Dest) {
3237  unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
3238 }
3239 
3241  unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
3242 }
3243 
3244 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
3245  return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
3246 }
3247 
3248 LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
3249  return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
3250 }
3251 
3253  unwrap<LandingPadInst>(LandingPad)->addClause(unwrap<Constant>(ClauseVal));
3254 }
3255 
3257  return unwrap<LandingPadInst>(LandingPad)->isCleanup();
3258 }
3259 
3260 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
3261  unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
3262 }
3263 
3265  unwrap<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Dest));
3266 }
3267 
3268 unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) {
3269  return unwrap<CatchSwitchInst>(CatchSwitch)->getNumHandlers();
3270 }
3271 
3272 void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) {
3273  CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(CatchSwitch);
3274  for (const BasicBlock *H : CSI->handlers())
3275  *Handlers++ = wrap(H);
3276 }
3277 
3279  return wrap(unwrap<CatchPadInst>(CatchPad)->getCatchSwitch());
3280 }
3281 
3283  unwrap<CatchPadInst>(CatchPad)
3284  ->setCatchSwitch(unwrap<CatchSwitchInst>(CatchSwitch));
3285 }
3286 
3287 /*--.. Funclets ...........................................................--*/
3288 
3290  return wrap(unwrap<FuncletPadInst>(Funclet)->getArgOperand(i));
3291 }
3292 
3293 void LLVMSetArgOperand(LLVMValueRef Funclet, unsigned i, LLVMValueRef value) {
3294  unwrap<FuncletPadInst>(Funclet)->setArgOperand(i, unwrap(value));
3295 }
3296 
3297 /*--.. Arithmetic ..........................................................--*/
3298 
3300  const char *Name) {
3301  return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
3302 }
3303 
3305  const char *Name) {
3306  return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
3307 }
3308 
3310  const char *Name) {
3311  return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
3312 }
3313 
3315  const char *Name) {
3316  return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
3317 }
3318 
3320  const char *Name) {
3321  return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
3322 }
3323 
3325  const char *Name) {
3326  return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
3327 }
3328 
3330  const char *Name) {
3331  return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
3332 }
3333 
3335  const char *Name) {
3336  return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
3337 }
3338 
3340  const char *Name) {
3341  return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
3342 }
3343 
3345  const char *Name) {
3346  return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
3347 }
3348 
3350  const char *Name) {
3351  return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
3352 }
3353 
3355  const char *Name) {
3356  return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
3357 }
3358 
3360  const char *Name) {
3361  return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
3362 }
3363 
3365  LLVMValueRef RHS, const char *Name) {
3366  return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
3367 }
3368 
3370  const char *Name) {
3371  return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
3372 }
3373 
3375  LLVMValueRef RHS, const char *Name) {
3376  return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
3377 }
3378 
3380  const char *Name) {
3381  return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
3382 }
3383 
3385  const char *Name) {
3386  return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
3387 }
3388 
3390  const char *Name) {
3391  return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
3392 }
3393 
3395  const char *Name) {
3396  return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
3397 }
3398 
3400  const char *Name) {
3401  return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
3402 }
3403 
3405  const char *Name) {
3406  return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
3407 }
3408 
3410  const char *Name) {
3411  return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
3412 }
3413 
3415  const char *Name) {
3416  return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
3417 }
3418 
3420  const char *Name) {
3421  return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
3422 }
3423 
3425  const char *Name) {
3426  return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
3427 }
3428 
3431  const char *Name) {
3433  unwrap(RHS), Name));
3434 }
3435 
3437  return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
3438 }
3439 
3441  const char *Name) {
3442  return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
3443 }
3444 
3446  const char *Name) {
3447  return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
3448 }
3449 
3451  return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
3452 }
3453 
3455  return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
3456 }
3457 
3458 /*--.. Memory ..............................................................--*/
3459 
3461  const char *Name) {
3462  Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3463  Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3464  AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3465  Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3466  ITy, unwrap(Ty), AllocSize,
3467  nullptr, nullptr, "");
3468  return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3469 }
3470 
3472  LLVMValueRef Val, const char *Name) {
3473  Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3474  Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3475  AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3476  Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3477  ITy, unwrap(Ty), AllocSize,
3478  unwrap(Val), nullptr, "");
3479  return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3480 }
3481 
3483  LLVMValueRef Val, LLVMValueRef Len,
3484  unsigned Align) {
3485  return wrap(unwrap(B)->CreateMemSet(unwrap(Ptr), unwrap(Val), unwrap(Len),
3486  MaybeAlign(Align)));
3487 }
3488 
3490  LLVMValueRef Dst, unsigned DstAlign,
3491  LLVMValueRef Src, unsigned SrcAlign,
3492  LLVMValueRef Size) {
3493  return wrap(unwrap(B)->CreateMemCpy(unwrap(Dst), MaybeAlign(DstAlign),
3494  unwrap(Src), MaybeAlign(SrcAlign),
3495  unwrap(Size)));
3496 }
3497 
3499  LLVMValueRef Dst, unsigned DstAlign,
3500  LLVMValueRef Src, unsigned SrcAlign,
3501  LLVMValueRef Size) {
3502  return wrap(unwrap(B)->CreateMemMove(unwrap(Dst), MaybeAlign(DstAlign),
3503  unwrap(Src), MaybeAlign(SrcAlign),
3504  unwrap(Size)));
3505 }
3506 
3508  const char *Name) {
3509  return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
3510 }
3511 
3513  LLVMValueRef Val, const char *Name) {
3514  return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
3515 }
3516 
3518  return wrap(unwrap(B)->Insert(
3519  CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
3520 }
3521 
3523  const char *Name) {
3524  Value *V = unwrap(PointerVal);
3525  PointerType *Ty = cast<PointerType>(V->getType());
3526 
3527  return wrap(
3528  unwrap(B)->CreateLoad(Ty->getNonOpaquePointerElementType(), V, Name));
3529 }
3530 
3532  LLVMValueRef PointerVal, const char *Name) {
3533  return wrap(unwrap(B)->CreateLoad(unwrap(Ty), unwrap(PointerVal), Name));
3534 }
3535 
3537  LLVMValueRef PointerVal) {
3538  return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
3539 }
3540 
3542  switch (Ordering) {
3552  }
3553 
3554  llvm_unreachable("Invalid LLVMAtomicOrdering value!");
3555 }
3556 
3558  switch (Ordering) {
3568  }
3569 
3570  llvm_unreachable("Invalid AtomicOrdering value!");
3571 }
3572 
3574  switch (BinOp) {
3590  }
3591 
3592  llvm_unreachable("Invalid LLVMAtomicRMWBinOp value!");
3593 }
3594 
3596  switch (BinOp) {
3612  default: break;
3613  }
3614 
3615  llvm_unreachable("Invalid AtomicRMWBinOp value!");
3616 }
3617 
3618 // TODO: Should this and other atomic instructions support building with
3619 // "syncscope"?
3621  LLVMBool isSingleThread, const char *Name) {
3622  return wrap(
3623  unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
3624  isSingleThread ? SyncScope::SingleThread
3626  Name));
3627 }
3628 
3630  LLVMValueRef *Indices, unsigned NumIndices,
3631  const char *Name) {
3632  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3633  Value *Val = unwrap(Pointer);
3635  return wrap(unwrap(B)->CreateGEP(Ty, Val, IdxList, Name));
3636 }
3637 
3639  LLVMValueRef Pointer, LLVMValueRef *Indices,
3640  unsigned NumIndices, const char *Name) {
3641  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3642  return wrap(unwrap(B)->CreateGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3643 }
3644 
3646  LLVMValueRef *Indices, unsigned NumIndices,
3647  const char *Name) {
3648  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3649  Value *Val = unwrap(Pointer);
3651  return wrap(unwrap(B)->CreateInBoundsGEP(Ty, Val, IdxList, Name));
3652 }
3653 
3655  LLVMValueRef Pointer, LLVMValueRef *Indices,
3656  unsigned NumIndices, const char *Name) {
3657  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3658  return wrap(
3659  unwrap(B)->CreateInBoundsGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3660 }
3661 
3663  unsigned Idx, const char *Name) {
3664  Value *Val = unwrap(Pointer);
3666  return wrap(unwrap(B)->CreateStructGEP(Ty, Val, Idx, Name));
3667 }
3668 
3670  LLVMValueRef Pointer, unsigned Idx,
3671  const char *Name) {
3672  return wrap(
3673  unwrap(B)->CreateStructGEP(unwrap(Ty), unwrap(Pointer), Idx, Name));
3674 }
3675 
3677  const char *Name) {
3678  return wrap(unwrap(B)->CreateGlobalString(Str, Name));
3679 }
3680 
3682  const char *Name) {
3683  return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
3684 }
3685 
3687  Value *P = unwrap(MemAccessInst);
3688  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3689  return LI->isVolatile();
3690  if (StoreInst *SI = dyn_cast<StoreInst>(P))
3691  return SI->isVolatile();
3692  if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
3693  return AI->isVolatile();
3694  return cast<AtomicCmpXchgInst>(P)->isVolatile();
3695 }
3696 
3697 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
3698  Value *P = unwrap(MemAccessInst);
3699  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3700  return LI->setVolatile(isVolatile);
3701  if (StoreInst *SI = dyn_cast<StoreInst>(P))
3702  return SI->setVolatile(isVolatile);
3703  if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
3704  return AI->setVolatile(isVolatile);
3705  return cast<AtomicCmpXchgInst>(P)->setVolatile(isVolatile);
3706 }
3707 
3709  return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->isWeak();
3710 }
3711 
3712 void LLVMSetWeak(LLVMValueRef CmpXchgInst, LLVMBool isWeak) {
3713  return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->setWeak(isWeak);
3714 }
3715 
3717  Value *P = unwrap(MemAccessInst);
3718  AtomicOrdering O;
3719  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3720  O = LI->getOrdering();
3721  else if (StoreInst *SI = dyn_cast<StoreInst>(P))
3722  O = SI->getOrdering();
3723  else
3724  O = cast<AtomicRMWInst>(P)->getOrdering();
3725  return mapToLLVMOrdering(O);
3726 }
3727 
3728 void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
3729  Value *P = unwrap(MemAccessInst);
3730  AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3731 
3732  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3733  return LI->setOrdering(O);
3734  return cast<StoreInst>(P)->setOrdering(O);
3735 }
3736 
3738  return mapToLLVMRMWBinOp(unwrap<AtomicRMWInst>(Inst)->getOperation());
3739 }
3740 
3742  unwrap<AtomicRMWInst>(Inst)->setOperation(mapFromLLVMRMWBinOp(BinOp));
3743 }
3744 
3745 /*--.. Casts ...............................................................--*/
3746 
3748  LLVMTypeRef DestTy, const char *Name) {
3749  return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
3750 }
3751 
3753  LLVMTypeRef DestTy, const char *Name) {
3754  return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
3755 }
3756 
3758  LLVMTypeRef DestTy, const char *Name) {
3759  return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
3760 }
3761 
3763  LLVMTypeRef DestTy, const char *Name) {
3764  return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
3765 }
3766 
3768  LLVMTypeRef DestTy, const char *Name) {
3769  return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
3770 }
3771 
3773  LLVMTypeRef DestTy, const char *Name) {
3774  return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
3775 }
3776 
3778  LLVMTypeRef DestTy, const char *Name) {
3779  return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
3780 }
3781 
3783  LLVMTypeRef DestTy, const char *Name) {
3784  return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
3785 }
3786 
3788  LLVMTypeRef DestTy, const char *Name) {
3789  return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
3790 }
3791 
3793  LLVMTypeRef DestTy, const char *Name) {
3794  return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
3795 }
3796 
3798  LLVMTypeRef DestTy, const char *Name) {
3799  return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
3800 }
3801 
3803  LLVMTypeRef DestTy, const char *Name) {
3804  return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
3805 }
3806 
3808  LLVMTypeRef DestTy, const char *Name) {
3809  return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
3810 }
3811 
3813  LLVMTypeRef DestTy, const char *Name) {
3814  return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
3815  Name));
3816 }
3817 
3819  LLVMTypeRef DestTy, const char *Name) {
3820  return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
3821  Name));
3822 }
3823 
3825  LLVMTypeRef DestTy, const char *Name) {
3826  return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
3827  Name));
3828 }
3829 
3831  LLVMTypeRef DestTy, const char *Name) {
3832  return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
3833  unwrap(DestTy), Name));
3834 }
3835 
3837  LLVMTypeRef DestTy, const char *Name) {
3838  return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
3839 }
3840 
3842  LLVMTypeRef DestTy, LLVMBool IsSigned,
3843  const char *Name) {
3844  return wrap(
3845  unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy), IsSigned, Name));
3846 }
3847 
3849  LLVMTypeRef DestTy, const char *Name) {
3850  return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
3851  /*isSigned*/true, Name));
3852 }
3853 
3855  LLVMTypeRef DestTy, const char *Name) {
3856  return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
3857 }
3858 
3860  LLVMTypeRef DestTy, LLVMBool DestIsSigned) {
3862  unwrap(Src), SrcIsSigned, unwrap(DestTy), DestIsSigned));
3863 }
3864 
3865 /*--.. Comparisons .........................................................--*/
3866 
3869  const char *Name) {
3870  return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
3871  unwrap(LHS), unwrap(RHS), Name));
3872 }
3873 
3876  const char *Name) {
3877  return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
3878  unwrap(LHS), unwrap(RHS), Name));
3879 }
3880 
3881 /*--.. Miscellaneous instructions ..........................................--*/
3882 
3884  return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
3885 }
3886 
3888  LLVMValueRef *Args, unsigned NumArgs,
3889  const char *Name) {
3890  Value *V = unwrap(Fn);
3891  FunctionType *FnT =
3892  cast<FunctionType>(V->getType()->getNonOpaquePointerElementType());
3893 
3894  return wrap(unwrap(B)->CreateCall(FnT, unwrap(Fn),
3895  makeArrayRef(unwrap(Args), NumArgs), Name));
3896 }
3897