LLVM  9.0.0svn
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/ADT/StringSwitch.h"
16 #include "llvm/IR/Attributes.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/Support/Debug.h"
35 #include "llvm/Support/Threading.h"
37 #include <cassert>
38 #include <cstdlib>
39 #include <cstring>
40 #include <system_error>
41 
42 using namespace llvm;
43 
44 #define DEBUG_TYPE "ir"
45 
53 }
54 
57 }
58 
59 void LLVMShutdown() {
60  llvm_shutdown();
61 }
62 
63 /*===-- Error handling ----------------------------------------------------===*/
64 
65 char *LLVMCreateMessage(const char *Message) {
66  return strdup(Message);
67 }
68 
69 void LLVMDisposeMessage(char *Message) {
70  free(Message);
71 }
72 
73 
74 /*===-- Operations on contexts --------------------------------------------===*/
75 
77 
79  return wrap(new LLVMContext());
80 }
81 
82 LLVMContextRef LLVMGetGlobalContext() { return wrap(&*GlobalContext); }
83 
85  LLVMDiagnosticHandler Handler,
86  void *DiagnosticContext) {
87  unwrap(C)->setDiagnosticHandlerCallBack(
88  LLVM_EXTENSION reinterpret_cast<DiagnosticHandler::DiagnosticHandlerTy>(
89  Handler),
90  DiagnosticContext);
91 }
92 
94  return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>(
95  unwrap(C)->getDiagnosticHandlerCallBack());
96 }
97 
99  return unwrap(C)->getDiagnosticContext();
100 }
101 
103  void *OpaqueHandle) {
104  auto YieldCallback =
105  LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
106  unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
107 }
108 
110  return unwrap(C)->shouldDiscardValueNames();
111 }
112 
114  unwrap(C)->setDiscardValueNames(Discard);
115 }
116 
118  delete unwrap(C);
119 }
120 
122  unsigned SLen) {
123  return unwrap(C)->getMDKindID(StringRef(Name, SLen));
124 }
125 
126 unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) {
127  return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
128 }
129 
130 #define GET_ATTR_KIND_FROM_NAME
131 #include "AttributesCompatFunc.inc"
132 
133 unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) {
134  return getAttrKindFromName(StringRef(Name, SLen));
135 }
136 
138  return Attribute::AttrKind::EndAttrKinds;
139 }
140 
142  uint64_t Val) {
143  return wrap(Attribute::get(*unwrap(C), (Attribute::AttrKind)KindID, Val));
144 }
145 
147  return unwrap(A).getKindAsEnum();
148 }
149 
151  auto Attr = unwrap(A);
152  if (Attr.isEnumAttribute())
153  return 0;
154  return Attr.getValueAsInt();
155 }
156 
158  const char *K, unsigned KLength,
159  const char *V, unsigned VLength) {
160  return wrap(Attribute::get(*unwrap(C), StringRef(K, KLength),
161  StringRef(V, VLength)));
162 }
163 
165  unsigned *Length) {
166  auto S = unwrap(A).getKindAsString();
167  *Length = S.size();
168  return S.data();
169 }
170 
172  unsigned *Length) {
173  auto S = unwrap(A).getValueAsString();
174  *Length = S.size();
175  return S.data();
176 }
177 
179  auto Attr = unwrap(A);
180  return Attr.isEnumAttribute() || Attr.isIntAttribute();
181 }
182 
184  return unwrap(A).isStringAttribute();
185 }
186 
188  std::string MsgStorage;
189  raw_string_ostream Stream(MsgStorage);
190  DiagnosticPrinterRawOStream DP(Stream);
191 
192  unwrap(DI)->print(DP);
193  Stream.flush();
194 
195  return LLVMCreateMessage(MsgStorage.c_str());
196 }
197 
199  LLVMDiagnosticSeverity severity;
200 
201  switch(unwrap(DI)->getSeverity()) {
202  default:
203  severity = LLVMDSError;
204  break;
205  case DS_Warning:
206  severity = LLVMDSWarning;
207  break;
208  case DS_Remark:
209  severity = LLVMDSRemark;
210  break;
211  case DS_Note:
212  severity = LLVMDSNote;
213  break;
214  }
215 
216  return severity;
217 }
218 
219 /*===-- Operations on modules ---------------------------------------------===*/
220 
222  return wrap(new Module(ModuleID, *GlobalContext));
223 }
224 
226  LLVMContextRef C) {
227  return wrap(new Module(ModuleID, *unwrap(C)));
228 }
229 
231  delete unwrap(M);
232 }
233 
234 const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) {
235  auto &Str = unwrap(M)->getModuleIdentifier();
236  *Len = Str.length();
237  return Str.c_str();
238 }
239 
240 void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) {
241  unwrap(M)->setModuleIdentifier(StringRef(Ident, Len));
242 }
243 
244 const char *LLVMGetSourceFileName(LLVMModuleRef M, size_t *Len) {
245  auto &Str = unwrap(M)->getSourceFileName();
246  *Len = Str.length();
247  return Str.c_str();
248 }
249 
250 void LLVMSetSourceFileName(LLVMModuleRef M, const char *Name, size_t Len) {
251  unwrap(M)->setSourceFileName(StringRef(Name, Len));
252 }
253 
254 /*--.. Data layout .........................................................--*/
256  return unwrap(M)->getDataLayoutStr().c_str();
257 }
258 
260  return LLVMGetDataLayoutStr(M);
261 }
262 
263 void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
264  unwrap(M)->setDataLayout(DataLayoutStr);
265 }
266 
267 /*--.. Target triple .......................................................--*/
268 const char * LLVMGetTarget(LLVMModuleRef M) {
269  return unwrap(M)->getTargetTriple().c_str();
270 }
271 
272 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
273  unwrap(M)->setTargetTriple(Triple);
274 }
275 
276 /*--.. Module flags ........................................................--*/
279  const char *Key;
280  size_t KeyLen;
282 };
283 
286  switch (Behavior) {
290  return Module::ModFlagBehavior::Warning;
292  return Module::ModFlagBehavior::Require;
294  return Module::ModFlagBehavior::Override;
296  return Module::ModFlagBehavior::Append;
298  return Module::ModFlagBehavior::AppendUnique;
299  }
300  llvm_unreachable("Unknown LLVMModuleFlagBehavior");
301 }
302 
305  switch (Behavior) {
308  case Module::ModFlagBehavior::Warning:
310  case Module::ModFlagBehavior::Require:
312  case Module::ModFlagBehavior::Override:
314  case Module::ModFlagBehavior::Append:
316  case Module::ModFlagBehavior::AppendUnique:
318  default:
319  llvm_unreachable("Unhandled Flag Behavior");
320  }
321 }
322 
325  unwrap(M)->getModuleFlagsMetadata(MFEs);
326 
327  LLVMOpaqueModuleFlagEntry *Result = static_cast<LLVMOpaqueModuleFlagEntry *>(
328  safe_malloc(MFEs.size() * sizeof(LLVMOpaqueModuleFlagEntry)));
329  for (unsigned i = 0; i < MFEs.size(); ++i) {
330  const auto &ModuleFlag = MFEs[i];
331  Result[i].Behavior = map_from_llvmModFlagBehavior(ModuleFlag.Behavior);
332  Result[i].Key = ModuleFlag.Key->getString().data();
333  Result[i].KeyLen = ModuleFlag.Key->getString().size();
334  Result[i].Metadata = wrap(ModuleFlag.Val);
335  }
336  *Len = MFEs.size();
337  return Result;
338 }
339 
341  free(Entries);
342 }
343 
346  unsigned Index) {
348  static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
349  return MFE.Behavior;
350 }
351 
353  unsigned Index, size_t *Len) {
355  static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
356  *Len = MFE.KeyLen;
357  return MFE.Key;
358 }
359 
361  unsigned Index) {
363  static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
364  return MFE.Metadata;
365 }
366 
368  const char *Key, size_t KeyLen) {
369  return wrap(unwrap(M)->getModuleFlag({Key, KeyLen}));
370 }
371 
373  const char *Key, size_t KeyLen,
374  LLVMMetadataRef Val) {
375  unwrap(M)->addModuleFlag(map_to_llvmModFlagBehavior(Behavior),
376  {Key, KeyLen}, unwrap(Val));
377 }
378 
379 /*--.. Printing modules ....................................................--*/
380 
382  unwrap(M)->print(errs(), nullptr,
383  /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
384 }
385 
387  char **ErrorMessage) {
388  std::error_code EC;
389  raw_fd_ostream dest(Filename, EC, sys::fs::F_Text);
390  if (EC) {
391  *ErrorMessage = strdup(EC.message().c_str());
392  return true;
393  }
394 
395  unwrap(M)->print(dest, nullptr);
396 
397  dest.close();
398 
399  if (dest.has_error()) {
400  std::string E = "Error printing to file: " + dest.error().message();
401  *ErrorMessage = strdup(E.c_str());
402  return true;
403  }
404 
405  return false;
406 }
407 
409  std::string buf;
410  raw_string_ostream os(buf);
411 
412  unwrap(M)->print(os, nullptr);
413  os.flush();
414 
415  return strdup(buf.c_str());
416 }
417 
418 /*--.. Operations on inline assembler ......................................--*/
419 void LLVMSetModuleInlineAsm2(LLVMModuleRef M, const char *Asm, size_t Len) {
420  unwrap(M)->setModuleInlineAsm(StringRef(Asm, Len));
421 }
422 
424  unwrap(M)->setModuleInlineAsm(StringRef(Asm));
425 }
426 
427 void LLVMAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm, size_t Len) {
428  unwrap(M)->appendModuleInlineAsm(StringRef(Asm, Len));
429 }
430 
431 const char *LLVMGetModuleInlineAsm(LLVMModuleRef M, size_t *Len) {
432  auto &Str = unwrap(M)->getModuleInlineAsm();
433  *Len = Str.length();
434  return Str.c_str();
435 }
436 
438  char *AsmString, size_t AsmStringSize,
439  char *Constraints, size_t ConstraintsSize,
440  LLVMBool HasSideEffects, LLVMBool IsAlignStack,
441  LLVMInlineAsmDialect Dialect) {
443  switch (Dialect) {
445  AD = InlineAsm::AD_ATT;
446  break;
448  AD = InlineAsm::AD_Intel;
449  break;
450  }
451  return wrap(InlineAsm::get(unwrap<FunctionType>(Ty),
452  StringRef(AsmString, AsmStringSize),
453  StringRef(Constraints, ConstraintsSize),
454  HasSideEffects, IsAlignStack, AD));
455 }
456 
457 
458 /*--.. Operations on module contexts ......................................--*/
460  return wrap(&unwrap(M)->getContext());
461 }
462 
463 
464 /*===-- Operations on types -----------------------------------------------===*/
465 
466 /*--.. Operations on all types (mostly) ....................................--*/
467 
469  switch (unwrap(Ty)->getTypeID()) {
470  case Type::VoidTyID:
471  return LLVMVoidTypeKind;
472  case Type::HalfTyID:
473  return LLVMHalfTypeKind;
474  case Type::FloatTyID:
475  return LLVMFloatTypeKind;
476  case Type::DoubleTyID:
477  return LLVMDoubleTypeKind;
478  case Type::X86_FP80TyID:
479  return LLVMX86_FP80TypeKind;
480  case Type::FP128TyID:
481  return LLVMFP128TypeKind;
482  case Type::PPC_FP128TyID:
483  return LLVMPPC_FP128TypeKind;
484  case Type::LabelTyID:
485  return LLVMLabelTypeKind;
486  case Type::MetadataTyID:
487  return LLVMMetadataTypeKind;
488  case Type::IntegerTyID:
489  return LLVMIntegerTypeKind;
490  case Type::FunctionTyID:
491  return LLVMFunctionTypeKind;
492  case Type::StructTyID:
493  return LLVMStructTypeKind;
494  case Type::ArrayTyID:
495  return LLVMArrayTypeKind;
496  case Type::PointerTyID:
497  return LLVMPointerTypeKind;
498  case Type::VectorTyID:
499  return LLVMVectorTypeKind;
500  case Type::X86_MMXTyID:
501  return LLVMX86_MMXTypeKind;
502  case Type::TokenTyID:
503  return LLVMTokenTypeKind;
504  }
505  llvm_unreachable("Unhandled TypeID.");
506 }
507 
509 {
510  return unwrap(Ty)->isSized();
511 }
512 
514  return wrap(&unwrap(Ty)->getContext());
515 }
516 
518  return unwrap(Ty)->print(errs(), /*IsForDebug=*/true);
519 }
520 
522  std::string buf;
523  raw_string_ostream os(buf);
524 
525  if (unwrap(Ty))
526  unwrap(Ty)->print(os);
527  else
528  os << "Printing <null> Type";
529 
530  os.flush();
531 
532  return strdup(buf.c_str());
533 }
534 
535 /*--.. Operations on integer types .........................................--*/
536 
538  return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
539 }
541  return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
542 }
544  return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
545 }
547  return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
548 }
550  return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
551 }
553  return (LLVMTypeRef) Type::getInt128Ty(*unwrap(C));
554 }
556  return wrap(IntegerType::get(*unwrap(C), NumBits));
557 }
558 
561 }
564 }
567 }
570 }
573 }
576 }
577 LLVMTypeRef LLVMIntType(unsigned NumBits) {
578  return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
579 }
580 
581 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
582  return unwrap<IntegerType>(IntegerTy)->getBitWidth();
583 }
584 
585 /*--.. Operations on real types ............................................--*/
586 
588  return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
589 }
591  return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
592 }
594  return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
595 }
597  return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
598 }
600  return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
601 }
604 }
606  return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
607 }
608 
611 }
614 }
617 }
620 }
623 }
626 }
629 }
630 
631 /*--.. Operations on function types ........................................--*/
632 
634  LLVMTypeRef *ParamTypes, unsigned ParamCount,
635  LLVMBool IsVarArg) {
636  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
637  return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
638 }
639 
641  return unwrap<FunctionType>(FunctionTy)->isVarArg();
642 }
643 
645  return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
646 }
647 
648 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
649  return unwrap<FunctionType>(FunctionTy)->getNumParams();
650 }
651 
652 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
653  FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
655  E = Ty->param_end(); I != E; ++I)
656  *Dest++ = wrap(*I);
657 }
658 
659 /*--.. Operations on struct types ..........................................--*/
660 
662  unsigned ElementCount, LLVMBool Packed) {
663  ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
664  return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
665 }
666 
668  unsigned ElementCount, LLVMBool Packed) {
669  return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
670  ElementCount, Packed);
671 }
672 
674 {
675  return wrap(StructType::create(*unwrap(C), Name));
676 }
677 
679 {
680  StructType *Type = unwrap<StructType>(Ty);
681  if (!Type->hasName())
682  return nullptr;
683  return Type->getName().data();
684 }
685 
686 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
687  unsigned ElementCount, LLVMBool Packed) {
688  ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
689  unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
690 }
691 
693  return unwrap<StructType>(StructTy)->getNumElements();
694 }
695 
697  StructType *Ty = unwrap<StructType>(StructTy);
699  E = Ty->element_end(); I != E; ++I)
700  *Dest++ = wrap(*I);
701 }
702 
704  StructType *Ty = unwrap<StructType>(StructTy);
705  return wrap(Ty->getTypeAtIndex(i));
706 }
707 
709  return unwrap<StructType>(StructTy)->isPacked();
710 }
711 
713  return unwrap<StructType>(StructTy)->isOpaque();
714 }
715 
717  return unwrap<StructType>(StructTy)->isLiteral();
718 }
719 
721  return wrap(unwrap(M)->getTypeByName(Name));
722 }
723 
724 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
725 
727  int i = 0;
728  for (auto *T : unwrap(Tp)->subtypes()) {
729  Arr[i] = wrap(T);
730  i++;
731  }
732 }
733 
734 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
735  return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
736 }
737 
739  return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
740 }
741 
742 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
743  return wrap(VectorType::get(unwrap(ElementType), ElementCount));
744 }
745 
747  auto *Ty = unwrap<Type>(WrappedTy);
748  if (auto *PTy = dyn_cast<PointerType>(Ty))
749  return wrap(PTy->getElementType());
750  return wrap(cast<SequentialType>(Ty)->getElementType());
751 }
752 
754  return unwrap(Tp)->getNumContainedTypes();
755 }
756 
757 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
758  return unwrap<ArrayType>(ArrayTy)->getNumElements();
759 }
760 
762  return unwrap<PointerType>(PointerTy)->getAddressSpace();
763 }
764 
765 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
766  return unwrap<VectorType>(VectorTy)->getNumElements();
767 }
768 
769 /*--.. Operations on other types ...........................................--*/
770 
772  return wrap(Type::getVoidTy(*unwrap(C)));
773 }
775  return wrap(Type::getLabelTy(*unwrap(C)));
776 }
778  return wrap(Type::getTokenTy(*unwrap(C)));
779 }
781  return wrap(Type::getMetadataTy(*unwrap(C)));
782 }
783 
786 }
789 }
790 
791 /*===-- Operations on values ----------------------------------------------===*/
792 
793 /*--.. Operations on all values ............................................--*/
794 
796  return wrap(unwrap(Val)->getType());
797 }
798 
800  switch(unwrap(Val)->getValueID()) {
801 #define HANDLE_VALUE(Name) \
802  case Value::Name##Val: \
803  return LLVM##Name##ValueKind;
804 #include "llvm/IR/Value.def"
805  default:
807  }
808 }
809 
810 const char *LLVMGetValueName2(LLVMValueRef Val, size_t *Length) {
811  auto *V = unwrap(Val);
812  *Length = V->getName().size();
813  return V->getName().data();
814 }
815 
816 void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen) {
817  unwrap(Val)->setName(StringRef(Name, NameLen));
818 }
819 
820 const char *LLVMGetValueName(LLVMValueRef Val) {
821  return unwrap(Val)->getName().data();
822 }
823 
824 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
825  unwrap(Val)->setName(Name);
826 }
827 
829  unwrap(Val)->print(errs(), /*IsForDebug=*/true);
830 }
831 
833  std::string buf;
834  raw_string_ostream os(buf);
835 
836  if (unwrap(Val))
837  unwrap(Val)->print(os);
838  else
839  os << "Printing <null> Value";
840 
841  os.flush();
842 
843  return strdup(buf.c_str());
844 }
845 
847  unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
848 }
849 
851  return unwrap<Instruction>(Inst)->hasMetadata();
852 }
853 
854 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
855  auto *I = unwrap<Instruction>(Inst);
856  assert(I && "Expected instruction");
857  if (auto *MD = I->getMetadata(KindID))
858  return wrap(MetadataAsValue::get(I->getContext(), MD));
859  return nullptr;
860 }
861 
862 // MetadataAsValue uses a canonical format which strips the actual MDNode for
863 // MDNode with just a single constant value, storing just a ConstantAsMetadata
864 // This undoes this canonicalization, reconstructing the MDNode.
866  Metadata *MD = MAV->getMetadata();
867  assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
868  "Expected a metadata node or a canonicalized constant");
869 
870  if (MDNode *N = dyn_cast<MDNode>(MD))
871  return N;
872 
873  return MDNode::get(MAV->getContext(), MD);
874 }
875 
876 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
877  MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
878 
879  unwrap<Instruction>(Inst)->setMetadata(KindID, N);
880 }
881 
883  unsigned Kind;
885 };
886 
888 static LLVMValueMetadataEntry *
889 llvm_getMetadata(size_t *NumEntries,
890  llvm::function_ref<void(MetadataEntries &)> AccessMD) {
892  AccessMD(MVEs);
893 
895  static_cast<LLVMOpaqueValueMetadataEntry *>(
896  safe_malloc(MVEs.size() * sizeof(LLVMOpaqueValueMetadataEntry)));
897  for (unsigned i = 0; i < MVEs.size(); ++i) {
898  const auto &ModuleFlag = MVEs[i];
899  Result[i].Kind = ModuleFlag.first;
900  Result[i].Metadata = wrap(ModuleFlag.second);
901  }
902  *NumEntries = MVEs.size();
903  return Result;
904 }
905 
908  size_t *NumEntries) {
909  return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
910  unwrap<Instruction>(Value)->getAllMetadata(Entries);
911  });
912 }
913 
914 /*--.. Conversion functions ................................................--*/
915 
916 #define LLVM_DEFINE_VALUE_CAST(name) \
917  LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
918  return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
919  }
920 
922 
924  if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
925  if (isa<MDNode>(MD->getMetadata()) ||
926  isa<ValueAsMetadata>(MD->getMetadata()))
927  return Val;
928  return nullptr;
929 }
930 
932  if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
933  if (isa<MDString>(MD->getMetadata()))
934  return Val;
935  return nullptr;
936 }
937 
938 /*--.. Operations on Uses ..................................................--*/
940  Value *V = unwrap(Val);
942  if (I == V->use_end())
943  return nullptr;
944  return wrap(&*I);
945 }
946 
948  Use *Next = unwrap(U)->getNext();
949  if (Next)
950  return wrap(Next);
951  return nullptr;
952 }
953 
955  return wrap(unwrap(U)->getUser());
956 }
957 
959  return wrap(unwrap(U)->get());
960 }
961 
962 /*--.. Operations on Users .................................................--*/
963 
965  unsigned Index) {
966  Metadata *Op = N->getOperand(Index);
967  if (!Op)
968  return nullptr;
969  if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
970  return wrap(C->getValue());
971  return wrap(MetadataAsValue::get(Context, Op));
972 }
973 
975  Value *V = unwrap(Val);
976  if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
977  if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
978  assert(Index == 0 && "Function-local metadata can only have one operand");
979  return wrap(L->getValue());
980  }
981  return getMDNodeOperandImpl(V->getContext(),
982  cast<MDNode>(MD->getMetadata()), Index);
983  }
984 
985  return wrap(cast<User>(V)->getOperand(Index));
986 }
987 
989  Value *V = unwrap(Val);
990  return wrap(&cast<User>(V)->getOperandUse(Index));
991 }
992 
994  unwrap<User>(Val)->setOperand(Index, unwrap(Op));
995 }
996 
998  Value *V = unwrap(Val);
999  if (isa<MetadataAsValue>(V))
1000  return LLVMGetMDNodeNumOperands(Val);
1001 
1002  return cast<User>(V)->getNumOperands();
1003 }
1004 
1005 /*--.. Operations on constants of any type .................................--*/
1006 
1008  return wrap(Constant::getNullValue(unwrap(Ty)));
1009 }
1010 
1012  return wrap(Constant::getAllOnesValue(unwrap(Ty)));
1013 }
1014 
1016  return wrap(UndefValue::get(unwrap(Ty)));
1017 }
1018 
1020  return isa<Constant>(unwrap(Ty));
1021 }
1022 
1024  if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
1025  return C->isNullValue();
1026  return false;
1027 }
1028 
1030  return isa<UndefValue>(unwrap(Val));
1031 }
1032 
1034  return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
1035 }
1036 
1037 /*--.. Operations on metadata nodes ........................................--*/
1038 
1040  unsigned SLen) {
1041  LLVMContext &Context = *unwrap(C);
1042  return wrap(MetadataAsValue::get(
1043  Context, MDString::get(Context, StringRef(Str, SLen))));
1044 }
1045 
1046 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
1047  return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
1048 }
1049 
1051  unsigned Count) {
1052  LLVMContext &Context = *unwrap(C);
1054  for (auto *OV : makeArrayRef(Vals, Count)) {
1055  Value *V = unwrap(OV);
1056  Metadata *MD;
1057  if (!V)
1058  MD = nullptr;
1059  else if (auto *C = dyn_cast<Constant>(V))
1060  MD = ConstantAsMetadata::get(C);
1061  else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
1062  MD = MDV->getMetadata();
1063  assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
1064  "outside of direct argument to call");
1065  } else {
1066  // This is function-local metadata. Pretend to make an MDNode.
1067  assert(Count == 1 &&
1068  "Expected only one operand to function-local metadata");
1069  return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V)));
1070  }
1071 
1072  MDs.push_back(MD);
1073  }
1074  return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs)));
1075 }
1076 
1077 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
1078  return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
1079 }
1080 
1082  return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
1083 }
1084 
1086  auto *V = unwrap(Val);
1087  if (auto *C = dyn_cast<Constant>(V))
1088  return wrap(ConstantAsMetadata::get(C));
1089  if (auto *MAV = dyn_cast<MetadataAsValue>(V))
1090  return wrap(MAV->getMetadata());
1091  return wrap(ValueAsMetadata::get(V));
1092 }
1093 
1094 const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
1095  if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
1096  if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
1097  *Length = S->getString().size();
1098  return S->getString().data();
1099  }
1100  *Length = 0;
1101  return nullptr;
1102 }
1103 
1105  auto *MD = cast<MetadataAsValue>(unwrap(V));
1106  if (isa<ValueAsMetadata>(MD->getMetadata()))
1107  return 1;
1108  return cast<MDNode>(MD->getMetadata())->getNumOperands();
1109 }
1110 
1112  Module *Mod = unwrap(M);
1114  if (I == Mod->named_metadata_end())
1115  return nullptr;
1116  return wrap(&*I);
1117 }
1118 
1120  Module *Mod = unwrap(M);
1122  if (I == Mod->named_metadata_begin())
1123  return nullptr;
1124  return wrap(&*--I);
1125 }
1126 
1128  NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD);
1130  if (++I == NamedNode->getParent()->named_metadata_end())
1131  return nullptr;
1132  return wrap(&*I);
1133 }
1134 
1136  NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD);
1138  if (I == NamedNode->getParent()->named_metadata_begin())
1139  return nullptr;
1140  return wrap(&*--I);
1141 }
1142 
1144  const char *Name, size_t NameLen) {
1145  return wrap(unwrap(M)->getNamedMetadata(StringRef(Name, NameLen)));
1146 }
1147 
1149  const char *Name, size_t NameLen) {
1150  return wrap(unwrap(M)->getOrInsertNamedMetadata({Name, NameLen}));
1151 }
1152 
1153 const char *LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NMD, size_t *NameLen) {
1154  NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD);
1155  *NameLen = NamedNode->getName().size();
1156  return NamedNode->getName().data();
1157 }
1158 
1160  auto *MD = cast<MetadataAsValue>(unwrap(V));
1161  if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1162  *Dest = wrap(MDV->getValue());
1163  return;
1164  }
1165  const auto *N = cast<MDNode>(MD->getMetadata());
1166  const unsigned numOperands = N->getNumOperands();
1167  LLVMContext &Context = unwrap(V)->getContext();
1168  for (unsigned i = 0; i < numOperands; i++)
1169  Dest[i] = getMDNodeOperandImpl(Context, N, i);
1170 }
1171 
1173  if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
1174  return N->getNumOperands();
1175  }
1176  return 0;
1177 }
1178 
1180  LLVMValueRef *Dest) {
1181  NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
1182  if (!N)
1183  return;
1184  LLVMContext &Context = unwrap(M)->getContext();
1185  for (unsigned i=0;i<N->getNumOperands();i++)
1186  Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
1187 }
1188 
1190  LLVMValueRef Val) {
1191  NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
1192  if (!N)
1193  return;
1194  if (!Val)
1195  return;
1196  N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
1197 }
1198 
1199 const char *LLVMGetDebugLocDirectory(LLVMValueRef Val, unsigned *Length) {
1200  if (!Length) return nullptr;
1201  StringRef S;
1202  if (const auto *I = unwrap<Instruction>(Val)) {
1203  S = I->getDebugLoc()->getDirectory();
1204  } else if (const auto *GV = unwrap<GlobalVariable>(Val)) {
1206  GV->getDebugInfo(GVEs);
1207  if (GVEs.size())
1208  if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1209  S = DGV->getDirectory();
1210  } else if (const auto *F = unwrap<Function>(Val)) {
1211  if (const DISubprogram *DSP = F->getSubprogram())
1212  S = DSP->getDirectory();
1213  } else {
1214  assert(0 && "Expected Instruction, GlobalVariable or Function");
1215  return nullptr;
1216  }
1217  *Length = S.size();
1218  return S.data();
1219 }
1220 
1221 const char *LLVMGetDebugLocFilename(LLVMValueRef Val, unsigned *Length) {
1222  if (!Length) return nullptr;
1223  StringRef S;
1224  if (const auto *I = unwrap<Instruction>(Val)) {
1225  S = I->getDebugLoc()->getFilename();
1226  } else if (const auto *GV = unwrap<GlobalVariable>(Val)) {
1228  GV->getDebugInfo(GVEs);
1229  if (GVEs.size())
1230  if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1231  S = DGV->getFilename();
1232  } else if (const auto *F = unwrap<Function>(Val)) {
1233  if (const DISubprogram *DSP = F->getSubprogram())
1234  S = DSP->getFilename();
1235  } else {
1236  assert(0 && "Expected Instruction, GlobalVariable or Function");
1237  return nullptr;
1238  }
1239  *Length = S.size();
1240  return S.data();
1241 }
1242 
1244  unsigned L = 0;
1245  if (const auto *I = unwrap<Instruction>(Val)) {
1246  L = I->getDebugLoc()->getLine();
1247  } else if (const auto *GV = unwrap<GlobalVariable>(Val)) {
1249  GV->getDebugInfo(GVEs);
1250  if (GVEs.size())
1251  if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1252  L = DGV->getLine();
1253  } else if (const auto *F = unwrap<Function>(Val)) {
1254  if (const DISubprogram *DSP = F->getSubprogram())
1255  L = DSP->getLine();
1256  } else {
1257  assert(0 && "Expected Instruction, GlobalVariable or Function");
1258  return -1;
1259  }
1260  return L;
1261 }
1262 
1264  unsigned C = 0;
1265  if (const auto *I = unwrap<Instruction>(Val))
1266  if (const auto &L = I->getDebugLoc())
1267  C = L->getColumn();
1268  return C;
1269 }
1270 
1271 /*--.. Operations on scalar constants ......................................--*/
1272 
1273 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
1274  LLVMBool SignExtend) {
1275  return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
1276 }
1277 
1279  unsigned NumWords,
1280  const uint64_t Words[]) {
1281  IntegerType *Ty = unwrap<IntegerType>(IntTy);
1282  return wrap(ConstantInt::get(Ty->getContext(),
1283  APInt(Ty->getBitWidth(),
1284  makeArrayRef(Words, NumWords))));
1285 }
1286 
1288  uint8_t Radix) {
1289  return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
1290  Radix));
1291 }
1292 
1294  unsigned SLen, uint8_t Radix) {
1295  return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
1296  Radix));
1297 }
1298 
1300  return wrap(ConstantFP::get(unwrap(RealTy), N));
1301 }
1302 
1304  return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
1305 }
1306 
1308  unsigned SLen) {
1309  return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
1310 }
1311 
1312 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
1313  return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
1314 }
1315 
1316 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
1317  return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
1318 }
1319 
1320 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
1321  ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
1322  Type *Ty = cFP->getType();
1323 
1324  if (Ty->isFloatTy()) {
1325  *LosesInfo = false;
1326  return cFP->getValueAPF().convertToFloat();
1327  }
1328 
1329  if (Ty->isDoubleTy()) {
1330  *LosesInfo = false;
1331  return cFP->getValueAPF().convertToDouble();
1332  }
1333 
1334  bool APFLosesInfo;
1335  APFloat APF = cFP->getValueAPF();
1337  *LosesInfo = APFLosesInfo;
1338  return APF.convertToDouble();
1339 }
1340 
1341 /*--.. Operations on composite constants ...................................--*/
1342 
1344  unsigned Length,
1345  LLVMBool DontNullTerminate) {
1346  /* Inverted the sense of AddNull because ', 0)' is a
1347  better mnemonic for null termination than ', 1)'. */
1348  return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
1349  DontNullTerminate == 0));
1350 }
1351 
1352 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
1353  LLVMBool DontNullTerminate) {
1354  return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
1355  DontNullTerminate);
1356 }
1357 
1359  return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
1360 }
1361 
1363  return unwrap<ConstantDataSequential>(C)->isString();
1364 }
1365 
1366 const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1367  StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1368  *Length = Str.size();
1369  return Str.data();
1370 }
1371 
1373  LLVMValueRef *ConstantVals, unsigned Length) {
1374  ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1375  return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1376 }
1377 
1379  LLVMValueRef *ConstantVals,
1380  unsigned Count, LLVMBool Packed) {
1381  Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1382  return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
1383  Packed != 0));
1384 }
1385 
1386 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
1387  LLVMBool Packed) {
1388  return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1389  Packed);
1390 }
1391 
1393  LLVMValueRef *ConstantVals,
1394  unsigned Count) {
1395  Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1396  StructType *Ty = cast<StructType>(unwrap(StructTy));
1397 
1398  return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
1399 }
1400 
1401 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
1403  unwrap<Constant>(ScalarConstantVals, Size), Size)));
1404 }
1405 
1406 /*-- Opcode mapping */
1407 
1408 static LLVMOpcode map_to_llvmopcode(int opcode)
1409 {
1410  switch (opcode) {
1411  default: llvm_unreachable("Unhandled Opcode.");
1412 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
1413 #include "llvm/IR/Instruction.def"
1414 #undef HANDLE_INST
1415  }
1416 }
1417 
1419 {
1420  switch (code) {
1421 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
1422 #include "llvm/IR/Instruction.def"
1423 #undef HANDLE_INST
1424  }
1425  llvm_unreachable("Unhandled Opcode.");
1426 }
1427 
1428 /*--.. Constant expressions ................................................--*/
1429 
1431  return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
1432 }
1433 
1435  return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
1436 }
1437 
1439  return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
1440 }
1441 
1443  return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1444 }
1445 
1447  return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
1448 }
1449 
1451  return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
1452 }
1453 
1454 
1456  return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
1457 }
1458 
1460  return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
1461 }
1462 
1464  return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
1465  unwrap<Constant>(RHSConstant)));
1466 }
1467 
1469  LLVMValueRef RHSConstant) {
1470  return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
1471  unwrap<Constant>(RHSConstant)));
1472 }
1473 
1475  LLVMValueRef RHSConstant) {
1476  return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
1477  unwrap<Constant>(RHSConstant)));
1478 }
1479 
1481  return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
1482  unwrap<Constant>(RHSConstant)));
1483 }
1484 
1486  return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
1487  unwrap<Constant>(RHSConstant)));
1488 }
1489 
1491  LLVMValueRef RHSConstant) {
1492  return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
1493  unwrap<Constant>(RHSConstant)));
1494 }
1495 
1497  LLVMValueRef RHSConstant) {
1498  return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
1499  unwrap<Constant>(RHSConstant)));
1500 }
1501 
1503  return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
1504  unwrap<Constant>(RHSConstant)));
1505 }
1506 
1508  return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
1509  unwrap<Constant>(RHSConstant)));
1510 }
1511 
1513  LLVMValueRef RHSConstant) {
1514  return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
1515  unwrap<Constant>(RHSConstant)));
1516 }
1517 
1519  LLVMValueRef RHSConstant) {
1520  return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
1521  unwrap<Constant>(RHSConstant)));
1522 }
1523 
1525  return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
1526  unwrap<Constant>(RHSConstant)));
1527 }
1528 
1530  return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
1531  unwrap<Constant>(RHSConstant)));
1532 }
1533 
1535  LLVMValueRef RHSConstant) {
1536  return wrap(ConstantExpr::getExactUDiv(unwrap<Constant>(LHSConstant),
1537  unwrap<Constant>(RHSConstant)));
1538 }
1539 
1541  return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
1542  unwrap<Constant>(RHSConstant)));
1543 }
1544 
1546  LLVMValueRef RHSConstant) {
1547  return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
1548  unwrap<Constant>(RHSConstant)));
1549 }
1550 
1552  return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
1553  unwrap<Constant>(RHSConstant)));
1554 }
1555 
1557  return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
1558  unwrap<Constant>(RHSConstant)));
1559 }
1560 
1562  return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
1563  unwrap<Constant>(RHSConstant)));
1564 }
1565 
1567  return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
1568  unwrap<Constant>(RHSConstant)));
1569 }
1570 
1572  return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
1573  unwrap<Constant>(RHSConstant)));
1574 }
1575 
1577  return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
1578  unwrap<Constant>(RHSConstant)));
1579 }
1580 
1582  return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
1583  unwrap<Constant>(RHSConstant)));
1584 }
1585 
1587  LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1588  return wrap(ConstantExpr::getICmp(Predicate,
1589  unwrap<Constant>(LHSConstant),
1590  unwrap<Constant>(RHSConstant)));
1591 }
1592 
1594  LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1595  return wrap(ConstantExpr::getFCmp(Predicate,
1596  unwrap<Constant>(LHSConstant),
1597  unwrap<Constant>(RHSConstant)));
1598 }
1599 
1601  return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1602  unwrap<Constant>(RHSConstant)));
1603 }
1604 
1606  return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
1607  unwrap<Constant>(RHSConstant)));
1608 }
1609 
1611  return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1612  unwrap<Constant>(RHSConstant)));
1613 }
1614 
1616  LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1617  ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1618  NumIndices);
1619  Constant *Val = unwrap<Constant>(ConstantVal);
1620  Type *Ty =
1621  cast<PointerType>(Val->getType()->getScalarType())->getElementType();
1622  return wrap(ConstantExpr::getGetElementPtr(Ty, Val, IdxList));
1623 }
1624 
1626  LLVMValueRef *ConstantIndices,
1627  unsigned NumIndices) {
1628  ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1629  NumIndices);
1630  Constant *Val = unwrap<Constant>(ConstantVal);
1631  Type *Ty =
1632  cast<PointerType>(Val->getType()->getScalarType())->getElementType();
1633  return wrap(ConstantExpr::getInBoundsGetElementPtr(Ty, Val, IdxList));
1634 }
1635 
1637  return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1638  unwrap(ToType)));
1639 }
1640 
1642  return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1643  unwrap(ToType)));
1644 }
1645 
1647  return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1648  unwrap(ToType)));
1649 }
1650 
1652  return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1653  unwrap(ToType)));
1654 }
1655 
1657  return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1658  unwrap(ToType)));
1659 }
1660 
1662  return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1663  unwrap(ToType)));
1664 }
1665 
1667  return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1668  unwrap(ToType)));
1669 }
1670 
1672  return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1673  unwrap(ToType)));
1674 }
1675 
1677  return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1678  unwrap(ToType)));
1679 }
1680 
1682  return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1683  unwrap(ToType)));
1684 }
1685 
1687  return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1688  unwrap(ToType)));
1689 }
1690 
1692  return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1693  unwrap(ToType)));
1694 }
1695 
1697  LLVMTypeRef ToType) {
1698  return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1699  unwrap(ToType)));
1700 }
1701 
1703  LLVMTypeRef ToType) {
1704  return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1705  unwrap(ToType)));
1706 }
1707 
1709  LLVMTypeRef ToType) {
1710  return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1711  unwrap(ToType)));
1712 }
1713 
1715  LLVMTypeRef ToType) {
1716  return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1717  unwrap(ToType)));
1718 }
1719 
1721  LLVMTypeRef ToType) {
1722  return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1723  unwrap(ToType)));
1724 }
1725 
1727  LLVMBool isSigned) {
1728  return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1729  unwrap(ToType), isSigned));
1730 }
1731 
1733  return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1734  unwrap(ToType)));
1735 }
1736 
1738  LLVMValueRef ConstantIfTrue,
1739  LLVMValueRef ConstantIfFalse) {
1740  return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1741  unwrap<Constant>(ConstantIfTrue),
1742  unwrap<Constant>(ConstantIfFalse)));
1743 }
1744 
1746  LLVMValueRef IndexConstant) {
1747  return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1748  unwrap<Constant>(IndexConstant)));
1749 }
1750 
1752  LLVMValueRef ElementValueConstant,
1753  LLVMValueRef IndexConstant) {
1754  return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1755  unwrap<Constant>(ElementValueConstant),
1756  unwrap<Constant>(IndexConstant)));
1757 }
1758 
1760  LLVMValueRef VectorBConstant,
1761  LLVMValueRef MaskConstant) {
1762  return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1763  unwrap<Constant>(VectorBConstant),
1764  unwrap<Constant>(MaskConstant)));
1765 }
1766 
1767 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1768  unsigned NumIdx) {
1769  return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1770  makeArrayRef(IdxList, NumIdx)));
1771 }
1772 
1774  LLVMValueRef ElementValueConstant,
1775  unsigned *IdxList, unsigned NumIdx) {
1776  return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1777  unwrap<Constant>(ElementValueConstant),
1778  makeArrayRef(IdxList, NumIdx)));
1779 }
1780 
1781 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1782  const char *Constraints,
1783  LLVMBool HasSideEffects,
1784  LLVMBool IsAlignStack) {
1785  return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1786  Constraints, HasSideEffects, IsAlignStack));
1787 }
1788 
1790  return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1791 }
1792 
1793 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1794 
1796  return wrap(unwrap<GlobalValue>(Global)->getParent());
1797 }
1798 
1800  return unwrap<GlobalValue>(Global)->isDeclaration();
1801 }
1802 
1804  switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1806  return LLVMExternalLinkage;
1810  return LLVMLinkOnceAnyLinkage;
1812  return LLVMLinkOnceODRLinkage;
1814  return LLVMWeakAnyLinkage;
1816  return LLVMWeakODRLinkage;
1818  return LLVMAppendingLinkage;
1820  return LLVMInternalLinkage;
1822  return LLVMPrivateLinkage;
1824  return LLVMExternalWeakLinkage;
1826  return LLVMCommonLinkage;
1827  }
1828 
1829  llvm_unreachable("Invalid GlobalValue linkage!");
1830 }
1831 
1832 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1833  GlobalValue *GV = unwrap<GlobalValue>(Global);
1834 
1835  switch (Linkage) {
1836  case LLVMExternalLinkage:
1838  break;
1841  break;
1844  break;
1847  break;
1849  LLVM_DEBUG(
1850  errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1851  "longer supported.");
1852  break;
1853  case LLVMWeakAnyLinkage:
1855  break;
1856  case LLVMWeakODRLinkage:
1858  break;
1859  case LLVMAppendingLinkage:
1861  break;
1862  case LLVMInternalLinkage:
1864  break;
1865  case LLVMPrivateLinkage:
1867  break;
1870  break;
1873  break;
1874  case LLVMDLLImportLinkage:
1875  LLVM_DEBUG(
1876  errs()
1877  << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1878  break;
1879  case LLVMDLLExportLinkage:
1880  LLVM_DEBUG(
1881  errs()
1882  << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1883  break;
1886  break;
1887  case LLVMGhostLinkage:
1888  LLVM_DEBUG(
1889  errs() << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1890  break;
1891  case LLVMCommonLinkage:
1893  break;
1894  }
1895 }
1896 
1897 const char *LLVMGetSection(LLVMValueRef Global) {
1898  // Using .data() is safe because of how GlobalObject::setSection is
1899  // implemented.
1900  return unwrap<GlobalValue>(Global)->getSection().data();
1901 }
1902 
1903 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1904  unwrap<GlobalObject>(Global)->setSection(Section);
1905 }
1906 
1908  return static_cast<LLVMVisibility>(
1909  unwrap<GlobalValue>(Global)->getVisibility());
1910 }
1911 
1913  unwrap<GlobalValue>(Global)
1914  ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1915 }
1916 
1918  return static_cast<LLVMDLLStorageClass>(
1919  unwrap<GlobalValue>(Global)->getDLLStorageClass());
1920 }
1921 
1923  unwrap<GlobalValue>(Global)->setDLLStorageClass(
1924  static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1925 }
1926 
1928  switch (unwrap<GlobalValue>(Global)->getUnnamedAddr()) {
1930  return LLVMNoUnnamedAddr;
1932  return LLVMLocalUnnamedAddr;
1934  return LLVMGlobalUnnamedAddr;
1935  }
1936  llvm_unreachable("Unknown UnnamedAddr kind!");
1937 }
1938 
1940  GlobalValue *GV = unwrap<GlobalValue>(Global);
1941 
1942  switch (UnnamedAddr) {
1943  case LLVMNoUnnamedAddr:
1945  case LLVMLocalUnnamedAddr:
1947  case LLVMGlobalUnnamedAddr:
1949  }
1950 }
1951 
1953  return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
1954 }
1955 
1956 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
1957  unwrap<GlobalValue>(Global)->setUnnamedAddr(
1958  HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
1960 }
1961 
1963  return wrap(unwrap<GlobalValue>(Global)->getValueType());
1964 }
1965 
1966 /*--.. Operations on global variables, load and store instructions .........--*/
1967 
1969  Value *P = unwrap<Value>(V);
1970  if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1971  return GV->getAlignment();
1972  if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1973  return AI->getAlignment();
1974  if (LoadInst *LI = dyn_cast<LoadInst>(P))
1975  return LI->getAlignment();
1976  if (StoreInst *SI = dyn_cast<StoreInst>(P))
1977  return SI->getAlignment();
1978 
1980  "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1981 }
1982 
1983 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1984  Value *P = unwrap<Value>(V);
1985  if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
1986  GV->setAlignment(Bytes);
1987  else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1988  AI->setAlignment(Bytes);
1989  else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1990  LI->setAlignment(Bytes);
1991  else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1992  SI->setAlignment(Bytes);
1993  else
1995  "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1996 }
1997 
1999  size_t *NumEntries) {
2000  return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
2001  if (Instruction *Instr = dyn_cast<Instruction>(unwrap(Value))) {
2002  Instr->getAllMetadata(Entries);
2003  } else {
2004  unwrap<GlobalObject>(Value)->getAllMetadata(Entries);
2005  }
2006  });
2007 }
2008 
2010  unsigned Index) {
2012  static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2013  return MVE.Kind;
2014 }
2015 
2018  unsigned Index) {
2020  static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2021  return MVE.Metadata;
2022 }
2023 
2025  free(Entries);
2026 }
2027 
2029  LLVMMetadataRef MD) {
2030  unwrap<GlobalObject>(Global)->setMetadata(Kind, unwrap<MDNode>(MD));
2031 }
2032 
2034  unwrap<GlobalObject>(Global)->eraseMetadata(Kind);
2035 }
2036 
2038  unwrap<GlobalObject>(Global)->clearMetadata();
2039 }
2040 
2041 /*--.. Operations on global variables ......................................--*/
2042 
2044  return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2045  GlobalValue::ExternalLinkage, nullptr, Name));
2046 }
2047 
2049  const char *Name,
2050  unsigned AddressSpace) {
2051  return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2052  GlobalValue::ExternalLinkage, nullptr, Name,
2054  AddressSpace));
2055 }
2056 
2058  return wrap(unwrap(M)->getNamedGlobal(Name));
2059 }
2060 
2062  Module *Mod = unwrap(M);
2064  if (I == Mod->global_end())
2065  return nullptr;
2066  return wrap(&*I);
2067 }
2068 
2070  Module *Mod = unwrap(M);
2072  if (I == Mod->global_begin())
2073  return nullptr;
2074  return wrap(&*--I);
2075 }
2076 
2078  GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2080  if (++I == GV->getParent()->global_end())
2081  return nullptr;
2082  return wrap(&*I);
2083 }
2084 
2086  GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2088  if (I == GV->getParent()->global_begin())
2089  return nullptr;
2090  return wrap(&*--I);
2091 }
2092 
2094  unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
2095 }
2096 
2098  GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
2099  if ( !GV->hasInitializer() )
2100  return nullptr;
2101  return wrap(GV->getInitializer());
2102 }
2103 
2105  unwrap<GlobalVariable>(GlobalVar)
2106  ->setInitializer(unwrap<Constant>(ConstantVal));
2107 }
2108 
2110  return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
2111 }
2112 
2114  unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
2115 }
2116 
2118  return unwrap<GlobalVariable>(GlobalVar)->isConstant();
2119 }
2120 
2122  unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
2123 }
2124 
2126  switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
2128  return LLVMNotThreadLocal;
2132  return LLVMLocalDynamicTLSModel;
2134  return LLVMInitialExecTLSModel;
2136  return LLVMLocalExecTLSModel;
2137  }
2138 
2139  llvm_unreachable("Invalid GlobalVariable thread local mode");
2140 }
2141 
2143  GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2144 
2145  switch (Mode) {
2146  case LLVMNotThreadLocal:
2148  break;
2151  break;
2154  break;
2157  break;
2158  case LLVMLocalExecTLSModel:
2160  break;
2161  }
2162 }
2163 
2165  return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
2166 }
2167 
2169  unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
2170 }
2171 
2172 /*--.. Operations on aliases ......................................--*/
2173 
2175  const char *Name) {
2176  auto *PTy = cast<PointerType>(unwrap(Ty));
2177  return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
2179  unwrap<Constant>(Aliasee), unwrap(M)));
2180 }
2181 
2183  const char *Name, size_t NameLen) {
2184  return wrap(unwrap(M)->getNamedAlias(Name));
2185 }
2186 
2188  Module *Mod = unwrap(M);
2190  if (I == Mod->alias_end())
2191  return nullptr;
2192  return wrap(&*I);
2193 }
2194 
2196  Module *Mod = unwrap(M);
2198  if (I == Mod->alias_begin())
2199  return nullptr;
2200  return wrap(&*--I);
2201 }
2202 
2204  GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2205  Module::alias_iterator I(Alias);
2206  if (++I == Alias->getParent()->alias_end())
2207  return nullptr;
2208  return wrap(&*I);
2209 }
2210 
2212  GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2213  Module::alias_iterator I(Alias);
2214  if (I == Alias->getParent()->alias_begin())
2215  return nullptr;
2216  return wrap(&*--I);
2217 }
2218 
2220  return wrap(unwrap<GlobalAlias>(Alias)->getAliasee());
2221 }
2222 
2224  unwrap<GlobalAlias>(Alias)->setAliasee(unwrap<Constant>(Aliasee));
2225 }
2226 
2227 /*--.. Operations on functions .............................................--*/
2228 
2230  LLVMTypeRef FunctionTy) {
2231  return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
2233 }
2234 
2236  return wrap(unwrap(M)->getFunction(Name));
2237 }
2238 
2240  Module *Mod = unwrap(M);
2241  Module::iterator I = Mod->begin();
2242  if (I == Mod->end())
2243  return nullptr;
2244  return wrap(&*I);
2245 }
2246 
2248  Module *Mod = unwrap(M);
2249  Module::iterator I = Mod->end();
2250  if (I == Mod->begin())
2251  return nullptr;
2252  return wrap(&*--I);
2253 }
2254 
2256  Function *Func = unwrap<Function>(Fn);
2257  Module::iterator I(Func);
2258  if (++I == Func->getParent()->end())
2259  return nullptr;
2260  return wrap(&*I);
2261 }
2262 
2264  Function *Func = unwrap<Function>(Fn);
2265  Module::iterator I(Func);
2266  if (I == Func->getParent()->begin())
2267  return nullptr;
2268  return wrap(&*--I);
2269 }
2270 
2272  unwrap<Function>(Fn)->eraseFromParent();
2273 }
2274 
2276  return unwrap<Function>(Fn)->hasPersonalityFn();
2277 }
2278 
2280  return wrap(unwrap<Function>(Fn)->getPersonalityFn());
2281 }
2282 
2284  unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
2285 }
2286 
2288  if (Function *F = dyn_cast<Function>(unwrap(Fn)))
2289  return F->getIntrinsicID();
2290  return 0;
2291 }
2292 
2294  assert(ID < llvm::Intrinsic::num_intrinsics && "Intrinsic ID out of range");
2295  return llvm::Intrinsic::ID(ID);
2296 }
2297 
2299  unsigned ID,
2300  LLVMTypeRef *ParamTypes,
2301  size_t ParamCount) {
2302  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2303  auto IID = llvm_map_to_intrinsic_id(ID);
2304  return wrap(llvm::Intrinsic::getDeclaration(unwrap(Mod), IID, Tys));
2305 }
2306 
2307 const char *LLVMIntrinsicGetName(unsigned ID, size_t *NameLength) {
2308  auto IID = llvm_map_to_intrinsic_id(ID);
2309  auto Str = llvm::Intrinsic::getName(IID);
2310  *NameLength = Str.size();
2311  return Str.data();
2312 }
2313 
2315  LLVMTypeRef *ParamTypes, size_t ParamCount) {
2316  auto IID = llvm_map_to_intrinsic_id(ID);
2317  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2318  return wrap(llvm::Intrinsic::getType(*unwrap(Ctx), IID, Tys));
2319 }
2320 
2322  LLVMTypeRef *ParamTypes,
2323  size_t ParamCount,
2324  size_t *NameLength) {
2325  auto IID = llvm_map_to_intrinsic_id(ID);
2326  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2327  auto Str = llvm::Intrinsic::getName(IID, Tys);
2328  *NameLength = Str.length();
2329  return strdup(Str.c_str());
2330 }
2331 
2333  auto IID = llvm_map_to_intrinsic_id(ID);
2334  return llvm::Intrinsic::isOverloaded(IID);
2335 }
2336 
2338  return unwrap<Function>(Fn)->getCallingConv();
2339 }
2340 
2341 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
2342  return unwrap<Function>(Fn)->setCallingConv(
2343  static_cast<CallingConv::ID>(CC));
2344 }
2345 
2346 const char *LLVMGetGC(LLVMValueRef Fn) {
2347  Function *F = unwrap<Function>(Fn);
2348  return F->hasGC()? F->getGC().c_str() : nullptr;
2349 }
2350 
2351 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
2352  Function *F = unwrap<Function>(Fn);
2353  if (GC)
2354  F->setGC(GC);
2355  else
2356  F->clearGC();
2357 }
2358 
2360  LLVMAttributeRef A) {
2361  unwrap<Function>(F)->addAttribute(Idx, unwrap(A));
2362 }
2363 
2365  auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2366  return AS.getNumAttributes();
2367 }
2368 
2371  auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2372  for (auto A : AS)
2373  *Attrs++ = wrap(A);
2374 }
2375 
2377  LLVMAttributeIndex Idx,
2378  unsigned KindID) {
2379  return wrap(unwrap<Function>(F)->getAttribute(Idx,
2380  (Attribute::AttrKind)KindID));
2381 }
2382 
2384  LLVMAttributeIndex Idx,
2385  const char *K, unsigned KLen) {
2386  return wrap(unwrap<Function>(F)->getAttribute(Idx, StringRef(K, KLen)));
2387 }
2388 
2390  unsigned KindID) {
2391  unwrap<Function>(F)->removeAttribute(Idx, (Attribute::AttrKind)KindID);
2392 }
2393 
2395  const char *K, unsigned KLen) {
2396  unwrap<Function>(F)->removeAttribute(Idx, StringRef(K, KLen));
2397 }
2398 
2400  const char *V) {
2401  Function *Func = unwrap<Function>(Fn);
2402  Attribute Attr = Attribute::get(Func->getContext(), A, V);
2404 }
2405 
2406 /*--.. Operations on parameters ............................................--*/
2407 
2409  // This function is strictly redundant to
2410  // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
2411  return unwrap<Function>(FnRef)->arg_size();
2412 }
2413 
2414 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
2415  Function *Fn = unwrap<Function>(FnRef);
2416  for (Function::arg_iterator I = Fn->arg_begin(),
2417  E = Fn->arg_end(); I != E; I++)
2418  *ParamRefs++ = wrap(&*I);
2419 }
2420 
2421 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
2422  Function *Fn = unwrap<Function>(FnRef);
2423  return wrap(&Fn->arg_begin()[index]);
2424 }
2425 
2427  return wrap(unwrap<Argument>(V)->getParent());
2428 }
2429 
2431  Function *Func = unwrap<Function>(Fn);
2433  if (I == Func->arg_end())
2434  return nullptr;
2435  return wrap(&*I);
2436 }
2437 
2439  Function *Func = unwrap<Function>(Fn);
2440  Function::arg_iterator I = Func->arg_end();
2441  if (I == Func->arg_begin())
2442  return nullptr;
2443  return wrap(&*--I);
2444 }
2445 
2447  Argument *A = unwrap<Argument>(Arg);
2448  Function *Fn = A->getParent();
2449  if (A->getArgNo() + 1 >= Fn->arg_size())
2450  return nullptr;
2451  return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
2452 }
2453 
2455  Argument *A = unwrap<Argument>(Arg);
2456  if (A->getArgNo() == 0)
2457  return nullptr;
2458  return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
2459 }
2460 
2461 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
2462  Argument *A = unwrap<Argument>(Arg);
2464 }
2465 
2466 /*--.. Operations on ifuncs ................................................--*/
2467 
2469  const char *Name, size_t NameLen,
2470  LLVMTypeRef Ty, unsigned AddrSpace,
2472  return wrap(GlobalIFunc::create(unwrap(Ty), AddrSpace,
2474  StringRef(Name, NameLen),
2475  unwrap<Constant>(Resolver), unwrap(M)));
2476 }
2477 
2479  const char *Name, size_t NameLen) {
2480  return wrap(unwrap(M)->getNamedIFunc(StringRef(Name, NameLen)));
2481 }
2482 
2484  Module *Mod = unwrap(M);
2486  if (I == Mod->ifunc_end())
2487  return nullptr;
2488  return wrap(&*I);
2489 }
2490 
2492  Module *Mod = unwrap(M);
2494  if (I == Mod->ifunc_begin())
2495  return nullptr;
2496  return wrap(&*--I);
2497 }
2498 
2500  GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2502  if (++I == GIF->getParent()->ifunc_end())
2503  return nullptr;
2504  return wrap(&*I);
2505 }
2506 
2508  GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2510  if (I == GIF->getParent()->ifunc_begin())
2511  return nullptr;
2512  return wrap(&*--I);
2513 }
2514 
2516  return wrap(unwrap<GlobalIFunc>(IFunc)->getResolver());
2517 }
2518 
2520  unwrap<GlobalIFunc>(IFunc)->setResolver(unwrap<Constant>(Resolver));
2521 }
2522 
2524  unwrap<GlobalIFunc>(IFunc)->eraseFromParent();
2525 }
2526 
2528  unwrap<GlobalIFunc>(IFunc)->removeFromParent();
2529 }
2530 
2531 /*--.. Operations on basic blocks ..........................................--*/
2532 
2534  return wrap(static_cast<Value*>(unwrap(BB)));
2535 }
2536 
2538  return isa<BasicBlock>(unwrap(Val));
2539 }
2540 
2542  return wrap(unwrap<BasicBlock>(Val));
2543 }
2544 
2546  return unwrap(BB)->getName().data();
2547 }
2548 
2550  return wrap(unwrap(BB)->getParent());
2551 }
2552 
2554  return wrap(unwrap(BB)->getTerminator());
2555 }
2556 
2558  return unwrap<Function>(FnRef)->size();
2559 }
2560 
2561 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
2562  Function *Fn = unwrap<Function>(FnRef);
2563  for (BasicBlock &BB : *Fn)
2564  *BasicBlocksRefs++ = wrap(&BB);
2565 }
2566 
2568  return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2569 }
2570 
2572  Function *Func = unwrap<Function>(Fn);
2573  Function::iterator I = Func->begin();
2574  if (I == Func->end())
2575  return nullptr;
2576  return wrap(&*I);
2577 }
2578 
2580  Function *Func = unwrap<Function>(Fn);
2581  Function::iterator I = Func->end();
2582  if (I == Func->begin())
2583  return nullptr;
2584  return wrap(&*--I);
2585 }
2586 
2588  BasicBlock *Block = unwrap(BB);
2589  Function::iterator I(Block);
2590  if (++I == Block->getParent()->end())
2591  return nullptr;
2592  return wrap(&*I);
2593 }
2594 
2596  BasicBlock *Block = unwrap(BB);
2597  Function::iterator I(Block);
2598  if (I == Block->getParent()->begin())
2599  return nullptr;
2600  return wrap(&*--I);
2601 }
2602 
2604  const char *Name) {
2605  return wrap(llvm::BasicBlock::Create(*unwrap(C), Name));
2606 }
2607 
2609  LLVMValueRef FnRef,
2610  const char *Name) {
2611  return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
2612 }
2613 
2615  return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
2616 }
2617 
2619  LLVMBasicBlockRef BBRef,
2620  const char *Name) {
2621  BasicBlock *BB = unwrap(BBRef);
2622  return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2623 }
2624 
2626  const char *Name) {
2627  return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
2628 }
2629 
2631  unwrap(BBRef)->eraseFromParent();
2632 }
2633 
2635  unwrap(BBRef)->removeFromParent();
2636 }
2637 
2639  unwrap(BB)->moveBefore(unwrap(MovePos));
2640 }
2641 
2643  unwrap(BB)->moveAfter(unwrap(MovePos));
2644 }
2645 
2646 /*--.. Operations on instructions ..........................................--*/
2647 
2649  return wrap(unwrap<Instruction>(Inst)->getParent());
2650 }
2651 
2653  BasicBlock *Block = unwrap(BB);
2654  BasicBlock::iterator I = Block->begin();
2655  if (I == Block->end())
2656  return nullptr;
2657  return wrap(&*I);
2658 }
2659 
2661  BasicBlock *Block = unwrap(BB);
2662  BasicBlock::iterator I = Block->end();
2663  if (I == Block->begin())
2664  return nullptr;
2665  return wrap(&*--I);
2666 }
2667 
2669  Instruction *Instr = unwrap<Instruction>(Inst);
2670  BasicBlock::iterator I(Instr);
2671  if (++I == Instr->getParent()->end())
2672  return nullptr;
2673  return wrap(&*I);
2674 }
2675 
2677  Instruction *Instr = unwrap<Instruction>(Inst);
2678  BasicBlock::iterator I(Instr);
2679  if (I == Instr->getParent()->begin())
2680  return nullptr;
2681  return wrap(&*--I);
2682 }
2683 
2685  unwrap<Instruction>(Inst)->removeFromParent();
2686 }
2687 
2689  unwrap<Instruction>(Inst)->eraseFromParent();
2690 }
2691 
2693  if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2694  return (LLVMIntPredicate)I->getPredicate();
2695  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2696  if (CE->getOpcode() == Instruction::ICmp)
2697  return (LLVMIntPredicate)CE->getPredicate();
2698  return (LLVMIntPredicate)0;
2699 }
2700 
2702  if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2703  return (LLVMRealPredicate)I->getPredicate();
2704  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2705  if (CE->getOpcode() == Instruction::FCmp)
2706  return (LLVMRealPredicate)CE->getPredicate();
2707  return (LLVMRealPredicate)0;
2708 }
2709 
2711  if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2712  return map_to_llvmopcode(C->getOpcode());
2713  return (LLVMOpcode)0;
2714 }
2715 
2717  if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2718  return wrap(C->clone());
2719  return nullptr;
2720 }
2721 
2724  return (I && I->isTerminator()) ? wrap(I) : nullptr;
2725 }
2726 
2728  if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) {
2729  return FPI->getNumArgOperands();
2730  }
2731  return unwrap<CallBase>(Instr)->getNumArgOperands();
2732 }
2733 
2734 /*--.. Call and invoke instructions ........................................--*/
2735 
2737  return unwrap<CallBase>(Instr)->getCallingConv();
2738 }
2739 
2740 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
2741  return unwrap<CallBase>(Instr)->setCallingConv(
2742  static_cast<CallingConv::ID>(CC));
2743 }
2744 
2745 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
2746  unsigned align) {
2747  auto *Call = unwrap<CallBase>(Instr);
2748  Attribute AlignAttr = Attribute::getWithAlignment(Call->getContext(), align);
2749  Call->addAttribute(index, AlignAttr);
2750 }
2751 
2753  LLVMAttributeRef A) {
2754  unwrap<CallBase>(C)->addAttribute(Idx, unwrap(A));
2755 }
2756 
2758  LLVMAttributeIndex Idx) {
2759  auto *Call = unwrap<CallBase>(C);
2760  auto AS = Call->getAttributes().getAttributes(Idx);
2761  return AS.getNumAttributes();
2762 }
2763 
2766  auto *Call = unwrap<CallBase>(C);
2767  auto AS = Call->getAttributes().getAttributes(Idx);
2768  for (auto A : AS)
2769  *Attrs++ = wrap(A);
2770 }
2771 
2773  LLVMAttributeIndex Idx,
2774  unsigned KindID) {
2775  return wrap(
2776  unwrap<CallBase>(C)->getAttribute(Idx, (Attribute::AttrKind)KindID));
2777 }
2778 
2780  LLVMAttributeIndex Idx,
2781  const char *K, unsigned KLen) {
2782  return wrap(unwrap<CallBase>(C)->getAttribute(Idx, StringRef(K, KLen)));
2783 }
2784 
2786  unsigned KindID) {
2787  unwrap<CallBase>(C)->removeAttribute(Idx, (Attribute::AttrKind)KindID);
2788 }
2789 
2791  const char *K, unsigned KLen) {
2792  unwrap<CallBase>(C)->removeAttribute(Idx, StringRef(K, KLen));
2793 }
2794 
2796  return wrap(unwrap<CallBase>(Instr)->getCalledValue());
2797 }
2798 
2800  return wrap(unwrap<CallBase>(Instr)->getFunctionType());
2801 }
2802 
2803 /*--.. Operations on call instructions (only) ..............................--*/
2804 
2806  return unwrap<CallInst>(Call)->isTailCall();
2807 }
2808 
2809 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
2810  unwrap<CallInst>(Call)->setTailCall(isTailCall);
2811 }
2812 
2813 /*--.. Operations on invoke instructions (only) ............................--*/
2814 
2816  return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2817 }
2818 
2820  if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2821  return wrap(CRI->getUnwindDest());
2822  } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2823  return wrap(CSI->getUnwindDest());
2824  }
2825  return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2826 }
2827 
2829  unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2830 }
2831 
2833  if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2834  return CRI->setUnwindDest(unwrap(B));
2835  } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2836  return CSI->setUnwindDest(unwrap(B));
2837  }
2838  unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2839 }
2840 
2841 /*--.. Operations on terminators ...........................................--*/
2842 
2844  return unwrap<Instruction>(Term)->getNumSuccessors();
2845 }
2846 
2848  return wrap(unwrap<Instruction>(Term)->getSuccessor(i));
2849 }
2850 
2851 void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
2852  return unwrap<Instruction>(Term)->setSuccessor(i, unwrap(block));
2853 }
2854 
2855 /*--.. Operations on branch instructions (only) ............................--*/
2856 
2858  return unwrap<BranchInst>(Branch)->isConditional();
2859 }
2860 
2862  return wrap(unwrap<BranchInst>(Branch)->getCondition());
2863 }
2864 
2866  return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2867 }
2868 
2869 /*--.. Operations on switch instructions (only) ............................--*/
2870 
2872  return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2873 }
2874 
2875 /*--.. Operations on alloca instructions (only) ............................--*/
2876 
2878  return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
2879 }
2880 
2881 /*--.. Operations on gep instructions (only) ...............................--*/
2882 
2884  return unwrap<GetElementPtrInst>(GEP)->isInBounds();
2885 }
2886 
2888  return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
2889 }
2890 
2891 /*--.. Operations on phi nodes .............................................--*/
2892 
2893 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
2894  LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
2895  PHINode *PhiVal = unwrap<PHINode>(PhiNode);
2896  for (unsigned I = 0; I != Count; ++I)
2897  PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
2898 }
2899 
2900 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
2901  return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
2902 }
2903 
2905  return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
2906 }
2907 
2909  return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
2910 }
2911 
2912 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/
2913 
2915  auto *I = unwrap(Inst);
2916  if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
2917  return GEP->getNumIndices();
2918  if (auto *EV = dyn_cast<ExtractValueInst>(I))
2919  return EV->getNumIndices();
2920  if (auto *IV = dyn_cast<InsertValueInst>(I))
2921  return IV->getNumIndices();
2922  if (auto *CE = dyn_cast<ConstantExpr>(I))
2923  return CE->getIndices().size();
2925  "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
2926 }
2927 
2928 const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
2929  auto *I = unwrap(Inst);
2930  if (auto *EV = dyn_cast<ExtractValueInst>(I))
2931  return EV->getIndices().data();
2932  if (auto *IV = dyn_cast<InsertValueInst>(I))
2933  return IV->getIndices().data();
2934  if (auto *CE = dyn_cast<ConstantExpr>(I))
2935  return CE->getIndices().data();
2937  "LLVMGetIndices applies only to extractvalue and insertvalue!");
2938 }
2939 
2940 
2941 /*===-- Instruction builders ----------------------------------------------===*/
2942 
2944  return wrap(new IRBuilder<>(*unwrap(C)));
2945 }
2946 
2949 }
2950 
2952  LLVMValueRef Instr) {
2953  BasicBlock *BB = unwrap(Block);
2954  auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
2955  unwrap(Builder)->SetInsertPoint(BB, I);
2956 }
2957 
2959  Instruction *I = unwrap<Instruction>(Instr);
2960  unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
2961 }
2962 
2964  BasicBlock *BB = unwrap(Block);
2965  unwrap(Builder)->SetInsertPoint(BB);
2966 }
2967 
2969  return wrap(unwrap(Builder)->GetInsertBlock());
2970 }
2971 
2973  unwrap(Builder)->ClearInsertionPoint();
2974 }
2975 
2977  unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
2978 }
2979 
2981  const char *Name) {
2982  unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
2983 }
2984 
2986  delete unwrap(Builder);
2987 }
2988 
2989 /*--.. Metadata builders ...................................................--*/
2990 
2992  MDNode *Loc =
2993  L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
2994  unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
2995 }
2996 
2998  LLVMContext &Context = unwrap(Builder)->getContext();
2999  return wrap(MetadataAsValue::get(
3000  Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
3001 }
3002 
3004  unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
3005 }
3006 
3007 
3008 /*--.. Instruction builders ................................................--*/
3009 
3011  return wrap(unwrap(B)->CreateRetVoid());
3012 }
3013 
3015  return wrap(unwrap(B)->CreateRet(unwrap(V)));
3016 }
3017 
3019  unsigned N) {
3020  return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
3021 }
3022 
3024  return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
3025 }
3026 
3029  return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
3030 }
3031 
3033  LLVMBasicBlockRef Else, unsigned NumCases) {
3034  return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
3035 }
3036 
3038  unsigned NumDests) {
3039  return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
3040 }
3041 
3043  LLVMValueRef *Args, unsigned NumArgs,
3045  const char *Name) {
3046  Value *V = unwrap(Fn);
3047  FunctionType *FnT =
3048  cast<FunctionType>(cast<PointerType>(V->getType())->getElementType());
3049 
3050  return wrap(
3051  unwrap(B)->CreateInvoke(FnT, unwrap(Fn), unwrap(Then), unwrap(Catch),
3052  makeArrayRef(unwrap(Args), NumArgs), Name));
3053 }
3054 
3056  LLVMValueRef *Args, unsigned NumArgs,
3058  const char *Name) {
3059  return wrap(unwrap(B)->CreateInvoke(
3060  unwrap<FunctionType>(Ty), unwrap(Fn), unwrap(Then), unwrap(Catch),
3061  makeArrayRef(unwrap(Args), NumArgs), Name));
3062 }
3063 
3065  LLVMValueRef PersFn, unsigned NumClauses,
3066  const char *Name) {
3067  // The personality used to live on the landingpad instruction, but now it
3068  // lives on the parent function. For compatibility, take the provided
3069  // personality and put it on the parent function.
3070  if (PersFn)
3071  unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
3072  cast<Function>(unwrap(PersFn)));
3073  return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
3074 }
3075 
3077  LLVMValueRef *Args, unsigned NumArgs,
3078  const char *Name) {
3079  return wrap(unwrap(B)->CreateCatchPad(unwrap(ParentPad),
3080  makeArrayRef(unwrap(Args), NumArgs),
3081  Name));
3082 }
3083 
3085  LLVMValueRef *Args, unsigned NumArgs,
3086  const char *Name) {
3087  if (ParentPad == nullptr) {
3088  Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3089  ParentPad = wrap(Constant::getNullValue(Ty));
3090  }
3091  return wrap(unwrap(B)->CreateCleanupPad(unwrap(ParentPad),
3092  makeArrayRef(unwrap(Args), NumArgs),
3093  Name));
3094 }
3095 
3097  return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
3098 }
3099 
3101  LLVMBasicBlockRef UnwindBB,
3102  unsigned NumHandlers, const char *Name) {
3103  if (ParentPad == nullptr) {
3104  Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3105  ParentPad = wrap(Constant::getNullValue(Ty));
3106  }
3107  return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(UnwindBB),
3108  NumHandlers, Name));
3109 }
3110 
3112  LLVMBasicBlockRef BB) {
3113  return wrap(unwrap(B)->CreateCatchRet(unwrap<CatchPadInst>(CatchPad),
3114  unwrap(BB)));
3115 }
3116 
3118  LLVMBasicBlockRef BB) {
3119  return wrap(unwrap(B)->CreateCleanupRet(unwrap<CleanupPadInst>(CatchPad),
3120  unwrap(BB)));
3121 }
3122 
3124  return wrap(unwrap(B)->CreateUnreachable());
3125 }
3126 
3128  LLVMBasicBlockRef Dest) {
3129  unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
3130 }
3131 
3133  unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
3134 }
3135 
3136 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
3137  return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
3138 }
3139 
3140 LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
3141  return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
3142 }
3143 
3144 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
3145  unwrap<LandingPadInst>(LandingPad)->
3146  addClause(cast<Constant>(unwrap(ClauseVal)));
3147 }
3148 
3150  return unwrap<LandingPadInst>(LandingPad)->isCleanup();
3151 }
3152 
3153 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
3154  unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
3155 }
3156 
3158  unwrap<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Dest));
3159 }
3160 
3161 unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) {
3162  return unwrap<CatchSwitchInst>(CatchSwitch)->getNumHandlers();
3163 }
3164 
3165 void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) {
3166  CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(CatchSwitch);
3168  E = CSI->handler_end(); I != E; ++I)
3169  *Handlers++ = wrap(*I);
3170 }
3171 
3173  return wrap(unwrap<CatchPadInst>(CatchPad)->getCatchSwitch());
3174 }
3175 
3177  unwrap<CatchPadInst>(CatchPad)
3178  ->setCatchSwitch(unwrap<CatchSwitchInst>(CatchSwitch));
3179 }
3180 
3181 /*--.. Funclets ...........................................................--*/
3182 
3184  return wrap(unwrap<FuncletPadInst>(Funclet)->getArgOperand(i));
3185 }
3186 
3187 void LLVMSetArgOperand(LLVMValueRef Funclet, unsigned i, LLVMValueRef value) {
3188  unwrap<FuncletPadInst>(Funclet)->setArgOperand(i, unwrap(value));
3189 }
3190 
3191 /*--.. Arithmetic ..........................................................--*/
3192 
3194  const char *Name) {
3195  return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
3196 }
3197 
3199  const char *Name) {
3200  return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
3201 }
3202 
3204  const char *Name) {
3205  return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
3206 }
3207 
3209  const char *Name) {
3210  return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
3211 }
3212 
3214  const char *Name) {
3215  return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
3216 }
3217 
3219  const char *Name) {
3220  return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
3221 }
3222 
3224  const char *Name) {
3225  return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
3226 }
3227 
3229  const char *Name) {
3230  return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
3231 }
3232 
3234  const char *Name) {
3235  return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
3236 }
3237 
3239  const char *Name) {
3240  return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
3241 }
3242 
3244  const char *Name) {
3245  return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
3246 }
3247 
3249  const char *Name) {
3250  return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
3251 }
3252 
3254  const char *Name) {
3255  return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
3256 }
3257 
3259  LLVMValueRef RHS, const char *Name) {
3260  return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
3261 }
3262 
3264  const char *Name) {
3265  return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
3266 }
3267 
3269  LLVMValueRef RHS, const char *Name) {
3270  return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
3271 }
3272 
3274  const char *Name) {
3275  return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
3276 }
3277 
3279  const char *Name) {
3280  return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
3281 }
3282 
3284  const char *Name) {
3285  return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
3286 }
3287 
3289  const char *Name) {
3290  return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
3291 }
3292 
3294  const char *Name) {
3295  return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
3296 }
3297 
3299  const char *Name) {
3300  return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
3301 }
3302 
3304  const char *Name) {
3305  return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
3306 }
3307 
3309  const char *Name) {
3310  return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
3311 }
3312 
3314  const char *Name) {
3315  return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
3316 }
3317 
3319  const char *Name) {
3320  return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
3321 }
3322 
3324  LLVMValueRef LHS, LLVMValueRef RHS,
3325  const char *Name) {
3326  return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
3327  unwrap(RHS), Name));
3328 }
3329 
3331  return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
3332 }
3333 
3335  const char *Name) {
3336  return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
3337 }
3338 
3340  const char *Name) {
3341  return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
3342 }
3343 
3345  return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
3346 }
3347 
3349  return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
3350 }
3351 
3352 /*--.. Memory ..............................................................--*/
3353 
3355  const char *Name) {
3356  Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3357  Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3358  AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3359  Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3360  ITy, unwrap(Ty), AllocSize,
3361  nullptr, nullptr, "");
3362  return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3363 }
3364 
3366  LLVMValueRef Val, const char *Name) {
3367  Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3368  Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3369  AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3370  Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3371  ITy, unwrap(Ty), AllocSize,
3372  unwrap(Val), nullptr, "");
3373  return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3374 }
3375 
3377  LLVMValueRef Val, LLVMValueRef Len,
3378  unsigned Align) {
3379  return wrap(unwrap(B)->CreateMemSet(unwrap(Ptr), unwrap(Val), unwrap(Len), Align));
3380 }
3381 
3383  LLVMValueRef Dst, unsigned DstAlign,
3384  LLVMValueRef Src, unsigned SrcAlign,
3385  LLVMValueRef Size) {
3386  return wrap(unwrap(B)->CreateMemCpy(unwrap(Dst), DstAlign,
3387  unwrap(Src), SrcAlign,
3388  unwrap(Size)));
3389 }
3390 
3392  LLVMValueRef Dst, unsigned DstAlign,
3393  LLVMValueRef Src, unsigned SrcAlign,
3394  LLVMValueRef Size) {
3395  return wrap(unwrap(B)->CreateMemMove(unwrap(Dst), DstAlign,
3396  unwrap(Src), SrcAlign,
3397  unwrap(Size)));
3398 }
3399 
3401  const char *Name) {
3402  return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
3403 }
3404 
3406  LLVMValueRef Val, const char *Name) {
3407  return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
3408 }
3409 
3411  return wrap(unwrap(B)->Insert(
3412  CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
3413 }
3414 
3416  const char *Name) {
3417  Value *V = unwrap(PointerVal);
3418  PointerType *Ty = cast<PointerType>(V->getType());
3419 
3420  return wrap(unwrap(B)->CreateLoad(Ty->getElementType(), V, Name));
3421 }
3422 
3424  LLVMValueRef PointerVal, const char *Name) {
3425  return wrap(unwrap(B)->CreateLoad(unwrap(Ty), unwrap(PointerVal), Name));
3426 }
3427 
3429  LLVMValueRef PointerVal) {
3430  return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
3431 }
3432 
3434  switch (Ordering) {
3444  }
3445 
3446  llvm_unreachable("Invalid LLVMAtomicOrdering value!");
3447 }
3448 
3450  switch (Ordering) {
3460  }
3461 
3462  llvm_unreachable("Invalid AtomicOrdering value!");
3463 }
3464 
3465 // TODO: Should this and other atomic instructions support building with
3466 // "syncscope"?
3468  LLVMBool isSingleThread, const char *Name) {
3469  return wrap(
3470  unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
3471  isSingleThread ? SyncScope::SingleThread
3473  Name));
3474 }
3475 
3477  LLVMValueRef *Indices, unsigned NumIndices,
3478  const char *Name) {
3479  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3480  Value *Val = unwrap(Pointer);
3481  Type *Ty =
3482  cast<PointerType>(Val->getType()->getScalarType())->getElementType();
3483  return wrap(unwrap(B)->CreateGEP(Ty, Val, IdxList, Name));
3484 }
3485 
3487  LLVMValueRef Pointer, LLVMValueRef *Indices,
3488  unsigned NumIndices, const char *Name) {
3489  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3490  return wrap(unwrap(B)->CreateGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3491 }
3492 
3494  LLVMValueRef *Indices, unsigned NumIndices,
3495  const char *Name) {
3496  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3497  Value *Val = unwrap(Pointer);
3498  Type *Ty =
3499  cast<PointerType>(Val->getType()->getScalarType())->getElementType();
3500  return wrap(unwrap(B)->CreateInBoundsGEP(Ty, Val, IdxList, Name));
3501 }
3502 
3504  LLVMValueRef Pointer, LLVMValueRef *Indices,
3505  unsigned NumIndices, const char *Name) {
3506  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3507  return wrap(
3508  unwrap(B)->CreateInBoundsGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3509 }
3510 
3512  unsigned Idx, const char *Name) {
3513  Value *Val = unwrap(Pointer);
3514  Type *Ty =
3515  cast<PointerType>(Val->getType()->getScalarType())->getElementType();
3516  return wrap(unwrap(B)->CreateStructGEP(Ty, Val, Idx, Name));
3517 }
3518 
3520  LLVMValueRef Pointer, unsigned Idx,
3521  const char *Name) {
3522  return wrap(
3523  unwrap(B)->CreateStructGEP(unwrap(Ty), unwrap(Pointer), Idx, Name));
3524 }
3525 
3527  const char *Name) {
3528  return wrap(unwrap(B)->CreateGlobalString(Str, Name));
3529 }
3530 
3532  const char *Name) {
3533  return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
3534 }
3535 
3537  Value *P = unwrap<Value>(MemAccessInst);
3538  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3539  return LI->isVolatile();
3540  return cast<StoreInst>(P)->isVolatile();
3541 }
3542 
3544  Value *P = unwrap<Value>(MemAccessInst);
3545  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3546  return LI->setVolatile(isVolatile);
3547  return cast<StoreInst>(P)->setVolatile(isVolatile);
3548 }
3549 
3551  Value *P = unwrap<Value>(MemAccessInst);
3552  AtomicOrdering O;
3553  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3554  O = LI->getOrdering();
3555  else
3556  O = cast<StoreInst>(P)->getOrdering();
3557  return mapToLLVMOrdering(O);
3558 }
3559 
3560 void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
3561  Value *P = unwrap<Value>(MemAccessInst);
3562  AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3563 
3564  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3565  return LI->setOrdering(O);
3566  return cast<StoreInst>(P)->setOrdering(O);
3567 }
3568 
3569 /*--.. Casts ...............................................................--*/
3570 
3572  LLVMTypeRef DestTy, const char *Name) {
3573  return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
3574 }
3575 
3577  LLVMTypeRef DestTy, const char *Name) {
3578  return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
3579 }
3580 
3582  LLVMTypeRef DestTy, const char *Name) {
3583  return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
3584 }
3585 
3587  LLVMTypeRef DestTy, const char *Name) {
3588  return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
3589 }
3590 
3592  LLVMTypeRef DestTy, const char *Name) {
3593  return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
3594 }
3595 
3597  LLVMTypeRef DestTy, const char *Name) {
3598  return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
3599 }
3600 
3602  LLVMTypeRef DestTy, const char *Name) {
3603  return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
3604 }
3605 
3607  LLVMTypeRef DestTy, const char *Name) {
3608  return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
3609 }
3610 
3612  LLVMTypeRef DestTy, const char *Name) {
3613  return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
3614 }
3615 
3617  LLVMTypeRef DestTy, const char *Name) {
3618  return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
3619 }
3620 
3622  LLVMTypeRef DestTy, const char *Name) {
3623  return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
3624 }
3625 
3627  LLVMTypeRef DestTy, const char *Name) {
3628  return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
3629 }
3630 
3632  LLVMTypeRef DestTy, const char *Name) {
3633  return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
3634 }
3635 
3637  LLVMTypeRef DestTy, const char *Name) {
3638  return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
3639  Name));
3640 }
3641 
3643  LLVMTypeRef DestTy, const char *Name) {
3644  return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
3645  Name));
3646 }
3647 
3649  LLVMTypeRef DestTy, const char *Name) {
3650  return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
3651  Name));
3652 }
3653 
3655  LLVMTypeRef DestTy, const char *Name) {
3656  return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
3657  unwrap(DestTy), Name));
3658 }
3659 
3661  LLVMTypeRef DestTy, const char *Name) {
3662  return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
3663 }
3664 
3666  LLVMTypeRef DestTy, LLVMBool IsSigned,
3667  const char *Name) {
3668  return wrap(
3669  unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy), IsSigned, Name));
3670 }
3671 
3673  LLVMTypeRef DestTy, const char *Name) {
3674  return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
3675  /*isSigned*/true, Name));
3676 }
3677 
3679  LLVMTypeRef DestTy, const char *Name) {
3680  return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
3681 }
3682 
3683 /*--.. Comparisons .........................................................--*/
3684 
3686  LLVMValueRef LHS, LLVMValueRef RHS,
3687  const char *Name) {
3688  return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
3689  unwrap(LHS), unwrap(RHS), Name));
3690 }
3691 
3693  LLVMValueRef LHS, LLVMValueRef RHS,
3694  const char *Name) {
3695  return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
3696  unwrap(LHS), unwrap(RHS), Name));
3697 }
3698 
3699 /*--.. Miscellaneous instructions ..........................................--*/
3700 
3702  return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
3703 }
3704 
3706  LLVMValueRef *Args, unsigned NumArgs,
3707  const char *Name) {
3708  Value *V = unwrap(Fn);
3709  FunctionType *FnT =
3710  cast<FunctionType>(cast<PointerType>(V->getType())->getElementType());
3711 
3712  return wrap(unwrap(B)->CreateCall(FnT, unwrap(Fn),
3713  makeArrayRef(unwrap(Args), NumArgs), Name));
3714 }
3715 
3717  LLVMValueRef *Args, unsigned NumArgs,
3718  const char *Name) {
3719  FunctionType *FTy = unwrap<FunctionType>(Ty);
3720  return wrap(unwrap(B)->CreateCall(FTy, unwrap(Fn),
3721  makeArrayRef(unwrap(Args), NumArgs), Name));
3722 }
3723 
3726  const char *Name) {
3727  return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
3728  Name));
3729 }
3730 
3732  LLVMTypeRef Ty, const char *Name) {
3733  return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
3734 }
3735 
3737  LLVMValueRef Index, const char *Name) {
3738  return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
3739  Name));
3740 }
3741 
3744  const char *Name) {
3745  return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
3746  unwrap(Index), Name));
3747 }
3748 
3751  const char *Name) {
3752  return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
3753  unwrap(Mask), Name));
3754 }
3755 
3757  unsigned Index, const char *Name) {
3758  return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
3759 }
3760 
3762  LLVMValueRef EltVal, unsigned Index,
3763  const char *Name) {
3764  return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
3765  Index, Name));
3766 }
3767 
3769  const char *Name) {
3770  return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
3771 }
3772 
3774  const char *Name) {
3775  return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
3776 }
3777 
3779  LLVMValueRef RHS, const char *Name) {
3780  return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
3781 }
3782 
3784  LLVMValueRef PTR, LLVMValueRef Val,
3785  LLVMAtomicOrdering ordering,
3786  LLVMBool singleThread) {
3787  AtomicRMWInst::BinOp intop;
3788  switch (op) {
3789  case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
3790  case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
3791  case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
3792  case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
3793  case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
3794  case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
3795  case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
3796  case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
3797  case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
3798  case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
3799  case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
3800  }
3801  return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
3802  mapFromLLVMOrdering(ordering), singleThread ? SyncScope::SingleThread
3803  : SyncScope::System));
3804 }
3805 
3807  LLVMValueRef Cmp, LLVMValueRef New,
3808  LLVMAtomicOrdering SuccessOrdering,
3809  LLVMAtomicOrdering FailureOrdering,
3810  LLVMBool singleThread) {
3811 
3812  return wrap(unwrap(B)->CreateAtomicCmpXchg(unwrap(Ptr), unwrap(Cmp),
3813  unwrap(New), mapFromLLVMOrdering(SuccessOrdering),
3814  mapFromLLVMOrdering(FailureOrdering),
3815  singleThread ? SyncScope::SingleThread : SyncScope::System));
3816 }
3817 
3818 
3820  Value *P = unwrap<Value>(AtomicInst);
3821 
3822  if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
3823  return I->getSyncScopeID() == SyncScope::SingleThread;
3824  return cast<AtomicCmpXchgInst>(P)->getSyncScopeID() ==
3826 }
3827 
3829  Value *P = unwrap<Value>(AtomicInst);
3831 
3832  if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
3833  return I->setSyncScopeID(SSID);
3834  return cast<AtomicCmpXchgInst>(P)->setSyncScopeID(SSID);
3835 }
3836 
3838  Value *P = unwrap<Value>(CmpXchgInst);
3839  return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering());
3840 }
3841 
3843  LLVMAtomicOrdering Ordering) {
3844  Value *P = unwrap<Value>(CmpXchgInst);
3845  AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3846 
3847  return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O);
3848 }
3849 
3851  Value *P = unwrap<Value>(CmpXchgInst);
3852  return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering());
3853 }
3854 
3856  LLVMAtomicOrdering Ordering) {
3857  Value *P = unwrap<Value>(CmpXchgInst);
3858  AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3859 
3860  return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O);
3861 }
3862 
3863 /*===-- Module providers --------------------------------------------------===*/
3864 
3867  return reinterpret_cast<LLVMModuleProviderRef>(M);
3868 }
3869 
3871  delete unwrap(MP);
3872 }
3873 
3874 
3875 /*===-- Memory buffers ----------------------------------------------------===*/
3876 
3878  const char *Path,
3879  LLVMMemoryBufferRef *OutMemBuf,
3880  char **OutMessage) {
3881 
3883  if (std::error_code EC = MBOrErr.getError()) {
3884  *OutMessage = strdup(EC.message().c_str());
3885  return 1;
3886  }
3887  *OutMemBuf = wrap(MBOrErr.get().release());
3888  return 0;
3889 }
3890 
3892  char **OutMessage) {
3894  if (std::error_code EC = MBOrErr.getError()) {
3895  *OutMessage = strdup(EC.message().c_str());
3896  return 1;
3897  }
3898  *OutMemBuf = wrap(MBOrErr.get().release());
3899  return 0;
3900 }
3901 
3903  const char *InputData,
3904  size_t InputDataLength,
3905  const char *BufferName,
3906  LLVMBool RequiresNullTerminator) {
3907 
3908  return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
3909  StringRef(BufferName),
3910  RequiresNullTerminator).release());
3911 }
3912 
3914  const char *InputData,
3915  size_t InputDataLength,
3916  const char *BufferName) {
3917 
3918  return wrap(
3919  MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
3920  StringRef(BufferName)).release());
3921 }
3922 
3924  return unwrap(MemBuf)->getBufferStart();
3925 }
3926 
3928  return unwrap(MemBuf)->getBufferSize();
3929 }
3930 
3932  delete unwrap(MemBuf);
3933 }
3934 
3935 /*===-- Pass Registry -----------------------------------------------------===*/
3936 
3939 }
3940 
3941 /*===-- Pass Manager ------------------------------------------------------===*/
3942 
3944  return wrap(new legacy::PassManager());
3945 }
3946 
3948  return wrap(new legacy::FunctionPassManager(unwrap(M)));
3949 }
3950 
3953  reinterpret_cast<LLVMModuleRef>(P));
3954 }
3955 
3957  return unwrap<legacy::PassManager>(PM)->run(*unwrap(M));
3958 }
3959 
3961  return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization();
3962 }
3963 
3965  return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
3966 }
3967 
3969  return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization();
3970 }
3971 
3973  delete unwrap(PM);
3974 }
3975 
3976 /*===-- Threading ------------------------------------------------------===*/
3977 
3979  return LLVMIsMultithreaded();
3980 }
3981 
3983 }
3984 
3986  return llvm_is_multithreaded();
3987 }
LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant)
Definition: Core.cpp:1545
LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString, const char *Constraints, LLVMBool HasSideEffects, LLVMBool IsAlignStack)
Deprecated: Use LLVMGetInlineAsm instead.
Definition: Core.cpp:1781
Subtract a value and return the old one.
Definition: Core.h:359
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
const char * LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf)
Definition: Core.cpp:3923
static Constant * getFPTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:1678
void LLVMDisposeBuilder(LLVMBuilderRef Builder)
Definition: Core.cpp:2985
uint64_t CallInst * C
unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V)
Obtain the number of operands from an MDNode value.
Definition: Core.cpp:1104
X86 MMX.
Definition: Core.h:162
Adds a requirement that another module flag be present and have a specified value after linking is pe...
Definition: Core.h:416
use_iterator use_end()
Definition: Value.h:346
LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy, LLVMValueRef *ConstantVals, unsigned Count)
Create a non-anonymous ConstantStruct from values.
Definition: Core.cpp:1392
7: Labels
Definition: Type.h:63
LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst)
Create a copy of &#39;this&#39; instruction that is identical in all ways except the following: ...
Definition: Core.cpp:2716
LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst)
Obtain the float predicate of an instruction.
Definition: Core.cpp:2701
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C, LLVMBasicBlockRef BBRef, const char *Name)
Insert a basic block in a function before another basic block.
Definition: Core.cpp:2618
Represents either an error or a value T.
Definition: ErrorOr.h:56
void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest)
Get the elements within a structure.
Definition: Core.cpp:696
void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs)
Obtain the parameters in a function.
Definition: Core.cpp:2414
LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar)
Definition: Core.cpp:2097
LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op, LLVMValueRef LHS, LLVMValueRef RHS, const char *Name)
Definition: Core.cpp:3685
static Constant * getString(LLVMContext &Context, StringRef Initializer, bool AddNull=true)
This method constructs a CDS and initializes it with a text string.
Definition: Constants.cpp:2566
void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers)
Obtain the basic blocks acting as handlers for a catchswitch instruction.
Definition: Core.cpp:3165
StringRef getKindAsString() const
Return the attribute&#39;s kind as a string.
Definition: Attributes.cpp:187
static bool isConstant(const MachineInstr &MI)
static Type * getDoubleTy(LLVMContext &C)
Definition: Type.cpp:164
void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst, LLVMAtomicOrdering Ordering)
Definition: Core.cpp:3855
LLVMValueRef LLVMGetFirstGlobalIFunc(LLVMModuleRef M)
Obtain an iterator to the first GlobalIFunc in a Module.
Definition: Core.cpp:2483
static IntegerType * getInt1Ty(LLVMContext &C)
Definition: Type.cpp:172
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy)
Obtain the address space of a pointer type.
Definition: Core.cpp:761
static Constant * getFAdd(Constant *C1, Constant *C2)
Definition: Constants.cpp:2244
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
Special purpose, only applies to global arrays.
Definition: GlobalValue.h:54
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn)
Check whether the given function has a personality function.
Definition: Core.cpp:2275
*p = old <signed v ? old : v
Definition: Instructions.h:721
LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Val, const char *Name)
Definition: Core.cpp:3405
LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant)
Definition: Core.cpp:1581
LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val, LLVMTypeRef DestTy, const char *Name)
Definition: Core.cpp:3576
LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn)
Obtain the basic block that corresponds to the entry point of a function.
Definition: Core.cpp:2567
LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M)
Obtain an iterator to the last Function in a Module.
Definition: Core.cpp:2247
LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType)
Definition: Core.cpp:1691
This class represents an incoming formal argument to a Function.
Definition: Argument.h:29
LLVMContext & Context
Not-And a value and return the old one.
Definition: Core.h:361
LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr, LLVMValueRef Cmp, LLVMValueRef New, LLVMAtomicOrdering SuccessOrdering, LLVMAtomicOrdering FailureOrdering, LLVMBool singleThread)
Definition: Core.cpp:3806
static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering)
Definition: Core.cpp:3433
LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant)
Definition: Core.cpp:1529
LLVMValueRef LLVMGetLastGlobalIFunc(LLVMModuleRef M)
Obtain an iterator to the last GlobalIFunc in a Module.
Definition: Core.cpp:2491
Externally visible function.
Definition: Core.h:167
SI Whole Quad Mode
LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
Whether the type has a known size.
Definition: Core.cpp:508
LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant)
Definition: Core.cpp:1540
void LLVMSetSection(LLVMValueRef Global, const char *Section)
Definition: Core.cpp:1903
MDNode * getOperand(unsigned i) const
Definition: Metadata.cpp:1080
void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen)
Set the string name of a value.
Definition: Core.cpp:816
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
struct LLVMOpaqueModule * LLVMModuleRef
The top-level container for all other LLVM Intermediate Representation (IR) objects.
Definition: Types.h:62
LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(const char *InputData, size_t InputDataLength, const char *BufferName)
Definition: Core.cpp:3913
LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count)
Obtain a MDNode value from the global context.
Definition: Core.cpp:1077
provides both an Acquire and a Release barrier (for fences and operations which both read and write m...
Definition: Core.h:342
const char * LLVMGetAsString(LLVMValueRef C, size_t *Length)
Get the given constant data sequential as a string.
Definition: Core.cpp:1366
LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index)
Obtain an incoming value to a PHI node as an LLVMValueRef.
Definition: Core.cpp:2904
LLVMTypeRef LLVMIntrinsicGetType(LLVMContextRef Ctx, unsigned ID, LLVMTypeRef *ParamTypes, size_t ParamCount)
Retrieves the type of an intrinsic.
Definition: Core.cpp:2314
static Constant * getNSWAdd(Constant *C1, Constant *C2)
Definition: Constants.h:976
void initializePrintModulePassWrapperPass(PassRegistry &)
static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align)
Return a uniquified Attribute object that has the specific alignment set.
Definition: Attributes.cpp:124
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:64
struct LLVMOpaqueMemoryBuffer * LLVMMemoryBufferRef
LLVM uses a polymorphic type hierarchy which C cannot represent, therefore parameters must be passed ...
Definition: Types.h:49
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList, bool InBounds=false, Optional< unsigned > InRangeIndex=None, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
Definition: Constants.h:1153
static MDNode * extractMDNode(MetadataAsValue *MAV)
Definition: Core.cpp:865
void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block)
Update the specified successor to point at the provided block.
Definition: Core.cpp:2851