LLVM  8.0.0svn
Core.cpp
Go to the documentation of this file.
1 //===-- Core.cpp ----------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the common infrastructure (including the C bindings)
11 // for libLLVMCore.a, which implements the LLVM intermediate representation.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm-c/Core.h"
16 #include "llvm/ADT/StringSwitch.h"
17 #include "llvm/IR/Attributes.h"
18 #include "llvm/IR/CallSite.h"
19 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/DerivedTypes.h"
22 #include "llvm/IR/DiagnosticInfo.h"
24 #include "llvm/IR/GlobalAlias.h"
25 #include "llvm/IR/GlobalVariable.h"
26 #include "llvm/IR/IRBuilder.h"
27 #include "llvm/IR/InlineAsm.h"
28 #include "llvm/IR/IntrinsicInst.h"
29 #include "llvm/IR/LLVMContext.h"
31 #include "llvm/IR/Module.h"
32 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/Threading.h"
39 #include <cassert>
40 #include <cstdlib>
41 #include <cstring>
42 #include <system_error>
43 
44 using namespace llvm;
45 
46 #define DEBUG_TYPE "ir"
47 
55 }
56 
59 }
60 
61 void LLVMShutdown() {
62  llvm_shutdown();
63 }
64 
65 /*===-- Error handling ----------------------------------------------------===*/
66 
67 char *LLVMCreateMessage(const char *Message) {
68  return strdup(Message);
69 }
70 
71 void LLVMDisposeMessage(char *Message) {
72  free(Message);
73 }
74 
75 
76 /*===-- Operations on contexts --------------------------------------------===*/
77 
79 
81  return wrap(new LLVMContext());
82 }
83 
84 LLVMContextRef LLVMGetGlobalContext() { return wrap(&*GlobalContext); }
85 
87  LLVMDiagnosticHandler Handler,
88  void *DiagnosticContext) {
89  unwrap(C)->setDiagnosticHandlerCallBack(
90  LLVM_EXTENSION reinterpret_cast<DiagnosticHandler::DiagnosticHandlerTy>(
91  Handler),
92  DiagnosticContext);
93 }
94 
96  return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>(
97  unwrap(C)->getDiagnosticHandlerCallBack());
98 }
99 
101  return unwrap(C)->getDiagnosticContext();
102 }
103 
105  void *OpaqueHandle) {
106  auto YieldCallback =
107  LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
108  unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
109 }
110 
112  delete unwrap(C);
113 }
114 
116  unsigned SLen) {
117  return unwrap(C)->getMDKindID(StringRef(Name, SLen));
118 }
119 
120 unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) {
121  return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
122 }
123 
124 #define GET_ATTR_KIND_FROM_NAME
125 #include "AttributesCompatFunc.inc"
126 
127 unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) {
128  return getAttrKindFromName(StringRef(Name, SLen));
129 }
130 
132  return Attribute::AttrKind::EndAttrKinds;
133 }
134 
136  uint64_t Val) {
137  return wrap(Attribute::get(*unwrap(C), (Attribute::AttrKind)KindID, Val));
138 }
139 
141  return unwrap(A).getKindAsEnum();
142 }
143 
145  auto Attr = unwrap(A);
146  if (Attr.isEnumAttribute())
147  return 0;
148  return Attr.getValueAsInt();
149 }
150 
152  const char *K, unsigned KLength,
153  const char *V, unsigned VLength) {
154  return wrap(Attribute::get(*unwrap(C), StringRef(K, KLength),
155  StringRef(V, VLength)));
156 }
157 
159  unsigned *Length) {
160  auto S = unwrap(A).getKindAsString();
161  *Length = S.size();
162  return S.data();
163 }
164 
166  unsigned *Length) {
167  auto S = unwrap(A).getValueAsString();
168  *Length = S.size();
169  return S.data();
170 }
171 
173  auto Attr = unwrap(A);
174  return Attr.isEnumAttribute() || Attr.isIntAttribute();
175 }
176 
178  return unwrap(A).isStringAttribute();
179 }
180 
182  std::string MsgStorage;
183  raw_string_ostream Stream(MsgStorage);
184  DiagnosticPrinterRawOStream DP(Stream);
185 
186  unwrap(DI)->print(DP);
187  Stream.flush();
188 
189  return LLVMCreateMessage(MsgStorage.c_str());
190 }
191 
193  LLVMDiagnosticSeverity severity;
194 
195  switch(unwrap(DI)->getSeverity()) {
196  default:
197  severity = LLVMDSError;
198  break;
199  case DS_Warning:
200  severity = LLVMDSWarning;
201  break;
202  case DS_Remark:
203  severity = LLVMDSRemark;
204  break;
205  case DS_Note:
206  severity = LLVMDSNote;
207  break;
208  }
209 
210  return severity;
211 }
212 
213 /*===-- Operations on modules ---------------------------------------------===*/
214 
216  return wrap(new Module(ModuleID, *GlobalContext));
217 }
218 
220  LLVMContextRef C) {
221  return wrap(new Module(ModuleID, *unwrap(C)));
222 }
223 
225  delete unwrap(M);
226 }
227 
228 const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) {
229  auto &Str = unwrap(M)->getModuleIdentifier();
230  *Len = Str.length();
231  return Str.c_str();
232 }
233 
234 void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) {
235  unwrap(M)->setModuleIdentifier(StringRef(Ident, Len));
236 }
237 
238 const char *LLVMGetSourceFileName(LLVMModuleRef M, size_t *Len) {
239  auto &Str = unwrap(M)->getSourceFileName();
240  *Len = Str.length();
241  return Str.c_str();
242 }
243 
244 void LLVMSetSourceFileName(LLVMModuleRef M, const char *Name, size_t Len) {
245  unwrap(M)->setSourceFileName(StringRef(Name, Len));
246 }
247 
248 /*--.. Data layout .........................................................--*/
250  return unwrap(M)->getDataLayoutStr().c_str();
251 }
252 
254  return LLVMGetDataLayoutStr(M);
255 }
256 
257 void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
258  unwrap(M)->setDataLayout(DataLayoutStr);
259 }
260 
261 /*--.. Target triple .......................................................--*/
262 const char * LLVMGetTarget(LLVMModuleRef M) {
263  return unwrap(M)->getTargetTriple().c_str();
264 }
265 
266 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
267  unwrap(M)->setTargetTriple(Triple);
268 }
269 
270 /*--.. Module flags ........................................................--*/
273  const char *Key;
274  size_t KeyLen;
276 };
277 
280  switch (Behavior) {
284  return Module::ModFlagBehavior::Warning;
286  return Module::ModFlagBehavior::Require;
288  return Module::ModFlagBehavior::Override;
290  return Module::ModFlagBehavior::Append;
292  return Module::ModFlagBehavior::AppendUnique;
293  }
294  llvm_unreachable("Unknown LLVMModuleFlagBehavior");
295 }
296 
299  switch (Behavior) {
302  case Module::ModFlagBehavior::Warning:
304  case Module::ModFlagBehavior::Require:
306  case Module::ModFlagBehavior::Override:
308  case Module::ModFlagBehavior::Append:
310  case Module::ModFlagBehavior::AppendUnique:
312  default:
313  llvm_unreachable("Unhandled Flag Behavior");
314  }
315 }
316 
319  unwrap(M)->getModuleFlagsMetadata(MFEs);
320 
321  LLVMOpaqueModuleFlagEntry *Result = static_cast<LLVMOpaqueModuleFlagEntry *>(
322  safe_malloc(MFEs.size() * sizeof(LLVMOpaqueModuleFlagEntry)));
323  for (unsigned i = 0; i < MFEs.size(); ++i) {
324  const auto &ModuleFlag = MFEs[i];
325  Result[i].Behavior = map_from_llvmModFlagBehavior(ModuleFlag.Behavior);
326  Result[i].Key = ModuleFlag.Key->getString().data();
327  Result[i].KeyLen = ModuleFlag.Key->getString().size();
328  Result[i].Metadata = wrap(ModuleFlag.Val);
329  }
330  *Len = MFEs.size();
331  return Result;
332 }
333 
335  free(Entries);
336 }
337 
340  unsigned Index) {
342  static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
343  return MFE.Behavior;
344 }
345 
347  unsigned Index, size_t *Len) {
349  static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
350  *Len = MFE.KeyLen;
351  return MFE.Key;
352 }
353 
355  unsigned Index) {
357  static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
358  return MFE.Metadata;
359 }
360 
362  const char *Key, size_t KeyLen) {
363  return wrap(unwrap(M)->getModuleFlag({Key, KeyLen}));
364 }
365 
367  const char *Key, size_t KeyLen,
368  LLVMMetadataRef Val) {
369  unwrap(M)->addModuleFlag(map_to_llvmModFlagBehavior(Behavior),
370  {Key, KeyLen}, unwrap(Val));
371 }
372 
373 /*--.. Printing modules ....................................................--*/
374 
376  unwrap(M)->print(errs(), nullptr,
377  /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
378 }
379 
381  char **ErrorMessage) {
382  std::error_code EC;
383  raw_fd_ostream dest(Filename, EC, sys::fs::F_Text);
384  if (EC) {
385  *ErrorMessage = strdup(EC.message().c_str());
386  return true;
387  }
388 
389  unwrap(M)->print(dest, nullptr);
390 
391  dest.close();
392 
393  if (dest.has_error()) {
394  std::string E = "Error printing to file: " + dest.error().message();
395  *ErrorMessage = strdup(E.c_str());
396  return true;
397  }
398 
399  return false;
400 }
401 
403  std::string buf;
404  raw_string_ostream os(buf);
405 
406  unwrap(M)->print(os, nullptr);
407  os.flush();
408 
409  return strdup(buf.c_str());
410 }
411 
412 /*--.. Operations on inline assembler ......................................--*/
413 void LLVMSetModuleInlineAsm2(LLVMModuleRef M, const char *Asm, size_t Len) {
414  unwrap(M)->setModuleInlineAsm(StringRef(Asm, Len));
415 }
416 
418  unwrap(M)->setModuleInlineAsm(StringRef(Asm));
419 }
420 
421 void LLVMAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm, size_t Len) {
422  unwrap(M)->appendModuleInlineAsm(StringRef(Asm, Len));
423 }
424 
425 const char *LLVMGetModuleInlineAsm(LLVMModuleRef M, size_t *Len) {
426  auto &Str = unwrap(M)->getModuleInlineAsm();
427  *Len = Str.length();
428  return Str.c_str();
429 }
430 
432  char *AsmString, size_t AsmStringSize,
433  char *Constraints, size_t ConstraintsSize,
434  LLVMBool HasSideEffects, LLVMBool IsAlignStack,
435  LLVMInlineAsmDialect Dialect) {
437  switch (Dialect) {
439  AD = InlineAsm::AD_ATT;
440  break;
442  AD = InlineAsm::AD_Intel;
443  break;
444  }
445  return wrap(InlineAsm::get(unwrap<FunctionType>(Ty),
446  StringRef(AsmString, AsmStringSize),
447  StringRef(Constraints, ConstraintsSize),
448  HasSideEffects, IsAlignStack, AD));
449 }
450 
451 
452 /*--.. Operations on module contexts ......................................--*/
454  return wrap(&unwrap(M)->getContext());
455 }
456 
457 
458 /*===-- Operations on types -----------------------------------------------===*/
459 
460 /*--.. Operations on all types (mostly) ....................................--*/
461 
463  switch (unwrap(Ty)->getTypeID()) {
464  case Type::VoidTyID:
465  return LLVMVoidTypeKind;
466  case Type::HalfTyID:
467  return LLVMHalfTypeKind;
468  case Type::FloatTyID:
469  return LLVMFloatTypeKind;
470  case Type::DoubleTyID:
471  return LLVMDoubleTypeKind;
472  case Type::X86_FP80TyID:
473  return LLVMX86_FP80TypeKind;
474  case Type::FP128TyID:
475  return LLVMFP128TypeKind;
476  case Type::PPC_FP128TyID:
477  return LLVMPPC_FP128TypeKind;
478  case Type::LabelTyID:
479  return LLVMLabelTypeKind;
480  case Type::MetadataTyID:
481  return LLVMMetadataTypeKind;
482  case Type::IntegerTyID:
483  return LLVMIntegerTypeKind;
484  case Type::FunctionTyID:
485  return LLVMFunctionTypeKind;
486  case Type::StructTyID:
487  return LLVMStructTypeKind;
488  case Type::ArrayTyID:
489  return LLVMArrayTypeKind;
490  case Type::PointerTyID:
491  return LLVMPointerTypeKind;
492  case Type::VectorTyID:
493  return LLVMVectorTypeKind;
494  case Type::X86_MMXTyID:
495  return LLVMX86_MMXTypeKind;
496  case Type::TokenTyID:
497  return LLVMTokenTypeKind;
498  }
499  llvm_unreachable("Unhandled TypeID.");
500 }
501 
503 {
504  return unwrap(Ty)->isSized();
505 }
506 
508  return wrap(&unwrap(Ty)->getContext());
509 }
510 
512  return unwrap(Ty)->print(errs(), /*IsForDebug=*/true);
513 }
514 
516  std::string buf;
517  raw_string_ostream os(buf);
518 
519  if (unwrap(Ty))
520  unwrap(Ty)->print(os);
521  else
522  os << "Printing <null> Type";
523 
524  os.flush();
525 
526  return strdup(buf.c_str());
527 }
528 
529 /*--.. Operations on integer types .........................................--*/
530 
532  return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
533 }
535  return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
536 }
538  return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
539 }
541  return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
542 }
544  return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
545 }
547  return (LLVMTypeRef) Type::getInt128Ty(*unwrap(C));
548 }
550  return wrap(IntegerType::get(*unwrap(C), NumBits));
551 }
552 
555 }
558 }
561 }
564 }
567 }
570 }
571 LLVMTypeRef LLVMIntType(unsigned NumBits) {
572  return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
573 }
574 
575 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
576  return unwrap<IntegerType>(IntegerTy)->getBitWidth();
577 }
578 
579 /*--.. Operations on real types ............................................--*/
580 
582  return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
583 }
585  return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
586 }
588  return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
589 }
591  return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
592 }
594  return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
595 }
598 }
600  return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
601 }
602 
605 }
608 }
611 }
614 }
617 }
620 }
623 }
624 
625 /*--.. Operations on function types ........................................--*/
626 
628  LLVMTypeRef *ParamTypes, unsigned ParamCount,
629  LLVMBool IsVarArg) {
630  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
631  return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
632 }
633 
635  return unwrap<FunctionType>(FunctionTy)->isVarArg();
636 }
637 
639  return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
640 }
641 
642 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
643  return unwrap<FunctionType>(FunctionTy)->getNumParams();
644 }
645 
646 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
647  FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
649  E = Ty->param_end(); I != E; ++I)
650  *Dest++ = wrap(*I);
651 }
652 
653 /*--.. Operations on struct types ..........................................--*/
654 
656  unsigned ElementCount, LLVMBool Packed) {
657  ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
658  return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
659 }
660 
662  unsigned ElementCount, LLVMBool Packed) {
663  return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
664  ElementCount, Packed);
665 }
666 
668 {
669  return wrap(StructType::create(*unwrap(C), Name));
670 }
671 
673 {
674  StructType *Type = unwrap<StructType>(Ty);
675  if (!Type->hasName())
676  return nullptr;
677  return Type->getName().data();
678 }
679 
680 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
681  unsigned ElementCount, LLVMBool Packed) {
682  ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
683  unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
684 }
685 
687  return unwrap<StructType>(StructTy)->getNumElements();
688 }
689 
691  StructType *Ty = unwrap<StructType>(StructTy);
693  E = Ty->element_end(); I != E; ++I)
694  *Dest++ = wrap(*I);
695 }
696 
698  StructType *Ty = unwrap<StructType>(StructTy);
699  return wrap(Ty->getTypeAtIndex(i));
700 }
701 
703  return unwrap<StructType>(StructTy)->isPacked();
704 }
705 
707  return unwrap<StructType>(StructTy)->isOpaque();
708 }
709 
711  return unwrap<StructType>(StructTy)->isLiteral();
712 }
713 
715  return wrap(unwrap(M)->getTypeByName(Name));
716 }
717 
718 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
719 
721  int i = 0;
722  for (auto *T : unwrap(Tp)->subtypes()) {
723  Arr[i] = wrap(T);
724  i++;
725  }
726 }
727 
728 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
729  return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
730 }
731 
733  return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
734 }
735 
736 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
737  return wrap(VectorType::get(unwrap(ElementType), ElementCount));
738 }
739 
741  auto *Ty = unwrap<Type>(WrappedTy);
742  if (auto *PTy = dyn_cast<PointerType>(Ty))
743  return wrap(PTy->getElementType());
744  return wrap(cast<SequentialType>(Ty)->getElementType());
745 }
746 
748  return unwrap(Tp)->getNumContainedTypes();
749 }
750 
751 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
752  return unwrap<ArrayType>(ArrayTy)->getNumElements();
753 }
754 
756  return unwrap<PointerType>(PointerTy)->getAddressSpace();
757 }
758 
759 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
760  return unwrap<VectorType>(VectorTy)->getNumElements();
761 }
762 
763 /*--.. Operations on other types ...........................................--*/
764 
766  return wrap(Type::getVoidTy(*unwrap(C)));
767 }
769  return wrap(Type::getLabelTy(*unwrap(C)));
770 }
772  return wrap(Type::getTokenTy(*unwrap(C)));
773 }
775  return wrap(Type::getMetadataTy(*unwrap(C)));
776 }
777 
780 }
783 }
784 
785 /*===-- Operations on values ----------------------------------------------===*/
786 
787 /*--.. Operations on all values ............................................--*/
788 
790  return wrap(unwrap(Val)->getType());
791 }
792 
794  switch(unwrap(Val)->getValueID()) {
795 #define HANDLE_VALUE(Name) \
796  case Value::Name##Val: \
797  return LLVM##Name##ValueKind;
798 #include "llvm/IR/Value.def"
799  default:
801  }
802 }
803 
804 const char *LLVMGetValueName2(LLVMValueRef Val, size_t *Length) {
805  auto *V = unwrap(Val);
806  *Length = V->getName().size();
807  return V->getName().data();
808 }
809 
810 void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen) {
811  unwrap(Val)->setName(StringRef(Name, NameLen));
812 }
813 
814 const char *LLVMGetValueName(LLVMValueRef Val) {
815  return unwrap(Val)->getName().data();
816 }
817 
818 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
819  unwrap(Val)->setName(Name);
820 }
821 
823  unwrap(Val)->print(errs(), /*IsForDebug=*/true);
824 }
825 
827  std::string buf;
828  raw_string_ostream os(buf);
829 
830  if (unwrap(Val))
831  unwrap(Val)->print(os);
832  else
833  os << "Printing <null> Value";
834 
835  os.flush();
836 
837  return strdup(buf.c_str());
838 }
839 
841  unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
842 }
843 
845  return unwrap<Instruction>(Inst)->hasMetadata();
846 }
847 
848 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
849  auto *I = unwrap<Instruction>(Inst);
850  assert(I && "Expected instruction");
851  if (auto *MD = I->getMetadata(KindID))
852  return wrap(MetadataAsValue::get(I->getContext(), MD));
853  return nullptr;
854 }
855 
856 // MetadataAsValue uses a canonical format which strips the actual MDNode for
857 // MDNode with just a single constant value, storing just a ConstantAsMetadata
858 // This undoes this canonicalization, reconstructing the MDNode.
860  Metadata *MD = MAV->getMetadata();
861  assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
862  "Expected a metadata node or a canonicalized constant");
863 
864  if (MDNode *N = dyn_cast<MDNode>(MD))
865  return N;
866 
867  return MDNode::get(MAV->getContext(), MD);
868 }
869 
870 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
871  MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
872 
873  unwrap<Instruction>(Inst)->setMetadata(KindID, N);
874 }
875 
877  unsigned Kind;
879 };
880 
882 static LLVMValueMetadataEntry *
883 llvm_getMetadata(size_t *NumEntries,
884  llvm::function_ref<void(MetadataEntries &)> AccessMD) {
886  AccessMD(MVEs);
887 
889  static_cast<LLVMOpaqueValueMetadataEntry *>(
890  safe_malloc(MVEs.size() * sizeof(LLVMOpaqueValueMetadataEntry)));
891  for (unsigned i = 0; i < MVEs.size(); ++i) {
892  const auto &ModuleFlag = MVEs[i];
893  Result[i].Kind = ModuleFlag.first;
894  Result[i].Metadata = wrap(ModuleFlag.second);
895  }
896  *NumEntries = MVEs.size();
897  return Result;
898 }
899 
902  size_t *NumEntries) {
903  return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
904  unwrap<Instruction>(Value)->getAllMetadata(Entries);
905  });
906 }
907 
908 /*--.. Conversion functions ................................................--*/
909 
910 #define LLVM_DEFINE_VALUE_CAST(name) \
911  LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
912  return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
913  }
914 
916 
918  if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
919  if (isa<MDNode>(MD->getMetadata()) ||
920  isa<ValueAsMetadata>(MD->getMetadata()))
921  return Val;
922  return nullptr;
923 }
924 
926  if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
927  if (isa<MDString>(MD->getMetadata()))
928  return Val;
929  return nullptr;
930 }
931 
932 /*--.. Operations on Uses ..................................................--*/
934  Value *V = unwrap(Val);
936  if (I == V->use_end())
937  return nullptr;
938  return wrap(&*I);
939 }
940 
942  Use *Next = unwrap(U)->getNext();
943  if (Next)
944  return wrap(Next);
945  return nullptr;
946 }
947 
949  return wrap(unwrap(U)->getUser());
950 }
951 
953  return wrap(unwrap(U)->get());
954 }
955 
956 /*--.. Operations on Users .................................................--*/
957 
959  unsigned Index) {
960  Metadata *Op = N->getOperand(Index);
961  if (!Op)
962  return nullptr;
963  if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
964  return wrap(C->getValue());
965  return wrap(MetadataAsValue::get(Context, Op));
966 }
967 
969  Value *V = unwrap(Val);
970  if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
971  if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
972  assert(Index == 0 && "Function-local metadata can only have one operand");
973  return wrap(L->getValue());
974  }
975  return getMDNodeOperandImpl(V->getContext(),
976  cast<MDNode>(MD->getMetadata()), Index);
977  }
978 
979  return wrap(cast<User>(V)->getOperand(Index));
980 }
981 
983  Value *V = unwrap(Val);
984  return wrap(&cast<User>(V)->getOperandUse(Index));
985 }
986 
988  unwrap<User>(Val)->setOperand(Index, unwrap(Op));
989 }
990 
992  Value *V = unwrap(Val);
993  if (isa<MetadataAsValue>(V))
994  return LLVMGetMDNodeNumOperands(Val);
995 
996  return cast<User>(V)->getNumOperands();
997 }
998 
999 /*--.. Operations on constants of any type .................................--*/
1000 
1002  return wrap(Constant::getNullValue(unwrap(Ty)));
1003 }
1004 
1006  return wrap(Constant::getAllOnesValue(unwrap(Ty)));
1007 }
1008 
1010  return wrap(UndefValue::get(unwrap(Ty)));
1011 }
1012 
1014  return isa<Constant>(unwrap(Ty));
1015 }
1016 
1018  if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
1019  return C->isNullValue();
1020  return false;
1021 }
1022 
1024  return isa<UndefValue>(unwrap(Val));
1025 }
1026 
1028  return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
1029 }
1030 
1031 /*--.. Operations on metadata nodes ........................................--*/
1032 
1034  unsigned SLen) {
1035  LLVMContext &Context = *unwrap(C);
1036  return wrap(MetadataAsValue::get(
1037  Context, MDString::get(Context, StringRef(Str, SLen))));
1038 }
1039 
1040 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
1041  return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
1042 }
1043 
1045  unsigned Count) {
1046  LLVMContext &Context = *unwrap(C);
1048  for (auto *OV : makeArrayRef(Vals, Count)) {
1049  Value *V = unwrap(OV);
1050  Metadata *MD;
1051  if (!V)
1052  MD = nullptr;
1053  else if (auto *C = dyn_cast<Constant>(V))
1054  MD = ConstantAsMetadata::get(C);
1055  else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
1056  MD = MDV->getMetadata();
1057  assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
1058  "outside of direct argument to call");
1059  } else {
1060  // This is function-local metadata. Pretend to make an MDNode.
1061  assert(Count == 1 &&
1062  "Expected only one operand to function-local metadata");
1063  return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V)));
1064  }
1065 
1066  MDs.push_back(MD);
1067  }
1068  return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs)));
1069 }
1070 
1071 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
1072  return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
1073 }
1074 
1076  return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
1077 }
1078 
1080  auto *V = unwrap(Val);
1081  if (auto *C = dyn_cast<Constant>(V))
1082  return wrap(ConstantAsMetadata::get(C));
1083  if (auto *MAV = dyn_cast<MetadataAsValue>(V))
1084  return wrap(MAV->getMetadata());
1085  return wrap(ValueAsMetadata::get(V));
1086 }
1087 
1088 const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
1089  if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
1090  if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
1091  *Length = S->getString().size();
1092  return S->getString().data();
1093  }
1094  *Length = 0;
1095  return nullptr;
1096 }
1097 
1099  auto *MD = cast<MetadataAsValue>(unwrap(V));
1100  if (isa<ValueAsMetadata>(MD->getMetadata()))
1101  return 1;
1102  return cast<MDNode>(MD->getMetadata())->getNumOperands();
1103 }
1104 
1106  Module *Mod = unwrap(M);
1108  if (I == Mod->named_metadata_end())
1109  return nullptr;
1110  return wrap(&*I);
1111 }
1112 
1114  Module *Mod = unwrap(M);
1116  if (I == Mod->named_metadata_begin())
1117  return nullptr;
1118  return wrap(&*--I);
1119 }
1120 
1122  NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD);
1124  if (++I == NamedNode->getParent()->named_metadata_end())
1125  return nullptr;
1126  return wrap(&*I);
1127 }
1128 
1130  NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD);
1132  if (I == NamedNode->getParent()->named_metadata_begin())
1133  return nullptr;
1134  return wrap(&*--I);
1135 }
1136 
1138  const char *Name, size_t NameLen) {
1139  return wrap(unwrap(M)->getNamedMetadata(StringRef(Name, NameLen)));
1140 }
1141 
1143  const char *Name, size_t NameLen) {
1144  return wrap(unwrap(M)->getOrInsertNamedMetadata({Name, NameLen}));
1145 }
1146 
1147 const char *LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NMD, size_t *NameLen) {
1148  NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD);
1149  *NameLen = NamedNode->getName().size();
1150  return NamedNode->getName().data();
1151 }
1152 
1154  auto *MD = cast<MetadataAsValue>(unwrap(V));
1155  if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1156  *Dest = wrap(MDV->getValue());
1157  return;
1158  }
1159  const auto *N = cast<MDNode>(MD->getMetadata());
1160  const unsigned numOperands = N->getNumOperands();
1161  LLVMContext &Context = unwrap(V)->getContext();
1162  for (unsigned i = 0; i < numOperands; i++)
1163  Dest[i] = getMDNodeOperandImpl(Context, N, i);
1164 }
1165 
1167  if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
1168  return N->getNumOperands();
1169  }
1170  return 0;
1171 }
1172 
1174  LLVMValueRef *Dest) {
1175  NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
1176  if (!N)
1177  return;
1178  LLVMContext &Context = unwrap(M)->getContext();
1179  for (unsigned i=0;i<N->getNumOperands();i++)
1180  Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
1181 }
1182 
1184  LLVMValueRef Val) {
1185  NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
1186  if (!N)
1187  return;
1188  if (!Val)
1189  return;
1190  N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
1191 }
1192 
1193 const char *LLVMGetDebugLocDirectory(LLVMValueRef Val, unsigned *Length) {
1194  if (!Length) return nullptr;
1195  StringRef S;
1196  if (const auto *I = unwrap<Instruction>(Val)) {
1197  S = I->getDebugLoc()->getDirectory();
1198  } else if (const auto *GV = unwrap<GlobalVariable>(Val)) {
1200  GV->getDebugInfo(GVEs);
1201  if (GVEs.size())
1202  if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1203  S = DGV->getDirectory();
1204  } else if (const auto *F = unwrap<Function>(Val)) {
1205  if (const DISubprogram *DSP = F->getSubprogram())
1206  S = DSP->getDirectory();
1207  } else {
1208  assert(0 && "Expected Instruction, GlobalVariable or Function");
1209  return nullptr;
1210  }
1211  *Length = S.size();
1212  return S.data();
1213 }
1214 
1215 const char *LLVMGetDebugLocFilename(LLVMValueRef Val, unsigned *Length) {
1216  if (!Length) return nullptr;
1217  StringRef S;
1218  if (const auto *I = unwrap<Instruction>(Val)) {
1219  S = I->getDebugLoc()->getFilename();
1220  } else if (const auto *GV = unwrap<GlobalVariable>(Val)) {
1222  GV->getDebugInfo(GVEs);
1223  if (GVEs.size())
1224  if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1225  S = DGV->getFilename();
1226  } else if (const auto *F = unwrap<Function>(Val)) {
1227  if (const DISubprogram *DSP = F->getSubprogram())
1228  S = DSP->getFilename();
1229  } else {
1230  assert(0 && "Expected Instruction, GlobalVariable or Function");
1231  return nullptr;
1232  }
1233  *Length = S.size();
1234  return S.data();
1235 }
1236 
1238  unsigned L = 0;
1239  if (const auto *I = unwrap<Instruction>(Val)) {
1240  L = I->getDebugLoc()->getLine();
1241  } else if (const auto *GV = unwrap<GlobalVariable>(Val)) {
1243  GV->getDebugInfo(GVEs);
1244  if (GVEs.size())
1245  if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1246  L = DGV->getLine();
1247  } else if (const auto *F = unwrap<Function>(Val)) {
1248  if (const DISubprogram *DSP = F->getSubprogram())
1249  L = DSP->getLine();
1250  } else {
1251  assert(0 && "Expected Instruction, GlobalVariable or Function");
1252  return -1;
1253  }
1254  return L;
1255 }
1256 
1258  unsigned C = 0;
1259  if (const auto *I = unwrap<Instruction>(Val))
1260  if (const auto &L = I->getDebugLoc())
1261  C = L->getColumn();
1262  return C;
1263 }
1264 
1265 /*--.. Operations on scalar constants ......................................--*/
1266 
1267 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
1268  LLVMBool SignExtend) {
1269  return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
1270 }
1271 
1273  unsigned NumWords,
1274  const uint64_t Words[]) {
1275  IntegerType *Ty = unwrap<IntegerType>(IntTy);
1276  return wrap(ConstantInt::get(Ty->getContext(),
1277  APInt(Ty->getBitWidth(),
1278  makeArrayRef(Words, NumWords))));
1279 }
1280 
1282  uint8_t Radix) {
1283  return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
1284  Radix));
1285 }
1286 
1288  unsigned SLen, uint8_t Radix) {
1289  return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
1290  Radix));
1291 }
1292 
1294  return wrap(ConstantFP::get(unwrap(RealTy), N));
1295 }
1296 
1298  return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
1299 }
1300 
1302  unsigned SLen) {
1303  return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
1304 }
1305 
1306 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
1307  return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
1308 }
1309 
1310 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
1311  return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
1312 }
1313 
1314 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
1315  ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
1316  Type *Ty = cFP->getType();
1317 
1318  if (Ty->isFloatTy()) {
1319  *LosesInfo = false;
1320  return cFP->getValueAPF().convertToFloat();
1321  }
1322 
1323  if (Ty->isDoubleTy()) {
1324  *LosesInfo = false;
1325  return cFP->getValueAPF().convertToDouble();
1326  }
1327 
1328  bool APFLosesInfo;
1329  APFloat APF = cFP->getValueAPF();
1331  *LosesInfo = APFLosesInfo;
1332  return APF.convertToDouble();
1333 }
1334 
1335 /*--.. Operations on composite constants ...................................--*/
1336 
1338  unsigned Length,
1339  LLVMBool DontNullTerminate) {
1340  /* Inverted the sense of AddNull because ', 0)' is a
1341  better mnemonic for null termination than ', 1)'. */
1342  return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
1343  DontNullTerminate == 0));
1344 }
1345 
1346 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
1347  LLVMBool DontNullTerminate) {
1348  return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
1349  DontNullTerminate);
1350 }
1351 
1353  return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
1354 }
1355 
1357  return unwrap<ConstantDataSequential>(C)->isString();
1358 }
1359 
1360 const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1361  StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1362  *Length = Str.size();
1363  return Str.data();
1364 }
1365 
1367  LLVMValueRef *ConstantVals, unsigned Length) {
1368  ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1369  return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1370 }
1371 
1373  LLVMValueRef *ConstantVals,
1374  unsigned Count, LLVMBool Packed) {
1375  Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1376  return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
1377  Packed != 0));
1378 }
1379 
1380 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
1381  LLVMBool Packed) {
1382  return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1383  Packed);
1384 }
1385 
1387  LLVMValueRef *ConstantVals,
1388  unsigned Count) {
1389  Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1390  StructType *Ty = cast<StructType>(unwrap(StructTy));
1391 
1392  return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
1393 }
1394 
1395 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
1397  unwrap<Constant>(ScalarConstantVals, Size), Size)));
1398 }
1399 
1400 /*-- Opcode mapping */
1401 
1402 static LLVMOpcode map_to_llvmopcode(int opcode)
1403 {
1404  switch (opcode) {
1405  default: llvm_unreachable("Unhandled Opcode.");
1406 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
1407 #include "llvm/IR/Instruction.def"
1408 #undef HANDLE_INST
1409  }
1410 }
1411 
1413 {
1414  switch (code) {
1415 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
1416 #include "llvm/IR/Instruction.def"
1417 #undef HANDLE_INST
1418  }
1419  llvm_unreachable("Unhandled Opcode.");
1420 }
1421 
1422 /*--.. Constant expressions ................................................--*/
1423 
1425  return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
1426 }
1427 
1429  return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
1430 }
1431 
1433  return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
1434 }
1435 
1437  return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1438 }
1439 
1441  return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
1442 }
1443 
1445  return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
1446 }
1447 
1448 
1450  return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
1451 }
1452 
1454  return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
1455 }
1456 
1458  return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
1459  unwrap<Constant>(RHSConstant)));
1460 }
1461 
1463  LLVMValueRef RHSConstant) {
1464  return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
1465  unwrap<Constant>(RHSConstant)));
1466 }
1467 
1469  LLVMValueRef RHSConstant) {
1470  return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
1471  unwrap<Constant>(RHSConstant)));
1472 }
1473 
1475  return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
1476  unwrap<Constant>(RHSConstant)));
1477 }
1478 
1480  return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
1481  unwrap<Constant>(RHSConstant)));
1482 }
1483 
1485  LLVMValueRef RHSConstant) {
1486  return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
1487  unwrap<Constant>(RHSConstant)));
1488 }
1489 
1491  LLVMValueRef RHSConstant) {
1492  return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
1493  unwrap<Constant>(RHSConstant)));
1494 }
1495 
1497  return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
1498  unwrap<Constant>(RHSConstant)));
1499 }
1500 
1502  return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
1503  unwrap<Constant>(RHSConstant)));
1504 }
1505 
1507  LLVMValueRef RHSConstant) {
1508  return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
1509  unwrap<Constant>(RHSConstant)));
1510 }
1511 
1513  LLVMValueRef RHSConstant) {
1514  return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
1515  unwrap<Constant>(RHSConstant)));
1516 }
1517 
1519  return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
1520  unwrap<Constant>(RHSConstant)));
1521 }
1522 
1524  return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
1525  unwrap<Constant>(RHSConstant)));
1526 }
1527 
1529  LLVMValueRef RHSConstant) {
1530  return wrap(ConstantExpr::getExactUDiv(unwrap<Constant>(LHSConstant),
1531  unwrap<Constant>(RHSConstant)));
1532 }
1533 
1535  return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
1536  unwrap<Constant>(RHSConstant)));
1537 }
1538 
1540  LLVMValueRef RHSConstant) {
1541  return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
1542  unwrap<Constant>(RHSConstant)));
1543 }
1544 
1546  return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
1547  unwrap<Constant>(RHSConstant)));
1548 }
1549 
1551  return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
1552  unwrap<Constant>(RHSConstant)));
1553 }
1554 
1556  return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
1557  unwrap<Constant>(RHSConstant)));
1558 }
1559 
1561  return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
1562  unwrap<Constant>(RHSConstant)));
1563 }
1564 
1566  return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
1567  unwrap<Constant>(RHSConstant)));
1568 }
1569 
1571  return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
1572  unwrap<Constant>(RHSConstant)));
1573 }
1574 
1576  return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
1577  unwrap<Constant>(RHSConstant)));
1578 }
1579 
1581  LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1582  return wrap(ConstantExpr::getICmp(Predicate,
1583  unwrap<Constant>(LHSConstant),
1584  unwrap<Constant>(RHSConstant)));
1585 }
1586 
1588  LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1589  return wrap(ConstantExpr::getFCmp(Predicate,
1590  unwrap<Constant>(LHSConstant),
1591  unwrap<Constant>(RHSConstant)));
1592 }
1593 
1595  return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1596  unwrap<Constant>(RHSConstant)));
1597 }
1598 
1600  return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
1601  unwrap<Constant>(RHSConstant)));
1602 }
1603 
1605  return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1606  unwrap<Constant>(RHSConstant)));
1607 }
1608 
1610  LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1611  ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1612  NumIndices);
1614  nullptr, unwrap<Constant>(ConstantVal), IdxList));
1615 }
1616 
1618  LLVMValueRef *ConstantIndices,
1619  unsigned NumIndices) {
1620  Constant* Val = unwrap<Constant>(ConstantVal);
1621  ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1622  NumIndices);
1623  return wrap(ConstantExpr::getInBoundsGetElementPtr(nullptr, Val, IdxList));
1624 }
1625 
1627  return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1628  unwrap(ToType)));
1629 }
1630 
1632  return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1633  unwrap(ToType)));
1634 }
1635 
1637  return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1638  unwrap(ToType)));
1639 }
1640 
1642  return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1643  unwrap(ToType)));
1644 }
1645 
1647  return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1648  unwrap(ToType)));
1649 }
1650 
1652  return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1653  unwrap(ToType)));
1654 }
1655 
1657  return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1658  unwrap(ToType)));
1659 }
1660 
1662  return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1663  unwrap(ToType)));
1664 }
1665 
1667  return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1668  unwrap(ToType)));
1669 }
1670 
1672  return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1673  unwrap(ToType)));
1674 }
1675 
1677  return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1678  unwrap(ToType)));
1679 }
1680 
1682  return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1683  unwrap(ToType)));
1684 }
1685 
1687  LLVMTypeRef ToType) {
1688  return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1689  unwrap(ToType)));
1690 }
1691 
1693  LLVMTypeRef ToType) {
1694  return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1695  unwrap(ToType)));
1696 }
1697 
1699  LLVMTypeRef ToType) {
1700  return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1701  unwrap(ToType)));
1702 }
1703 
1705  LLVMTypeRef ToType) {
1706  return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1707  unwrap(ToType)));
1708 }
1709 
1711  LLVMTypeRef ToType) {
1712  return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1713  unwrap(ToType)));
1714 }
1715 
1717  LLVMBool isSigned) {
1718  return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1719  unwrap(ToType), isSigned));
1720 }
1721 
1723  return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1724  unwrap(ToType)));
1725 }
1726 
1728  LLVMValueRef ConstantIfTrue,
1729  LLVMValueRef ConstantIfFalse) {
1730  return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1731  unwrap<Constant>(ConstantIfTrue),
1732  unwrap<Constant>(ConstantIfFalse)));
1733 }
1734 
1736  LLVMValueRef IndexConstant) {
1737  return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1738  unwrap<Constant>(IndexConstant)));
1739 }
1740 
1742  LLVMValueRef ElementValueConstant,
1743  LLVMValueRef IndexConstant) {
1744  return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1745  unwrap<Constant>(ElementValueConstant),
1746  unwrap<Constant>(IndexConstant)));
1747 }
1748 
1750  LLVMValueRef VectorBConstant,
1751  LLVMValueRef MaskConstant) {
1752  return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1753  unwrap<Constant>(VectorBConstant),
1754  unwrap<Constant>(MaskConstant)));
1755 }
1756 
1757 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1758  unsigned NumIdx) {
1759  return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1760  makeArrayRef(IdxList, NumIdx)));
1761 }
1762 
1764  LLVMValueRef ElementValueConstant,
1765  unsigned *IdxList, unsigned NumIdx) {
1766  return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1767  unwrap<Constant>(ElementValueConstant),
1768  makeArrayRef(IdxList, NumIdx)));
1769 }
1770 
1771 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1772  const char *Constraints,
1773  LLVMBool HasSideEffects,
1774  LLVMBool IsAlignStack) {
1775  return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1776  Constraints, HasSideEffects, IsAlignStack));
1777 }
1778 
1780  return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1781 }
1782 
1783 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1784 
1786  return wrap(unwrap<GlobalValue>(Global)->getParent());
1787 }
1788 
1790  return unwrap<GlobalValue>(Global)->isDeclaration();
1791 }
1792 
1794  switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1796  return LLVMExternalLinkage;
1800  return LLVMLinkOnceAnyLinkage;
1802  return LLVMLinkOnceODRLinkage;
1804  return LLVMWeakAnyLinkage;
1806  return LLVMWeakODRLinkage;
1808  return LLVMAppendingLinkage;
1810  return LLVMInternalLinkage;
1812  return LLVMPrivateLinkage;
1814  return LLVMExternalWeakLinkage;
1816  return LLVMCommonLinkage;
1817  }
1818 
1819  llvm_unreachable("Invalid GlobalValue linkage!");
1820 }
1821 
1822 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1823  GlobalValue *GV = unwrap<GlobalValue>(Global);
1824 
1825  switch (Linkage) {
1826  case LLVMExternalLinkage:
1828  break;
1831  break;
1834  break;
1837  break;
1839  LLVM_DEBUG(
1840  errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1841  "longer supported.");
1842  break;
1843  case LLVMWeakAnyLinkage:
1845  break;
1846  case LLVMWeakODRLinkage:
1848  break;
1849  case LLVMAppendingLinkage:
1851  break;
1852  case LLVMInternalLinkage:
1854  break;
1855  case LLVMPrivateLinkage:
1857  break;
1860  break;
1863  break;
1864  case LLVMDLLImportLinkage:
1865  LLVM_DEBUG(
1866  errs()
1867  << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1868  break;
1869  case LLVMDLLExportLinkage:
1870  LLVM_DEBUG(
1871  errs()
1872  << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1873  break;
1876  break;
1877  case LLVMGhostLinkage:
1878  LLVM_DEBUG(
1879  errs() << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1880  break;
1881  case LLVMCommonLinkage:
1883  break;
1884  }
1885 }
1886 
1887 const char *LLVMGetSection(LLVMValueRef Global) {
1888  // Using .data() is safe because of how GlobalObject::setSection is
1889  // implemented.
1890  return unwrap<GlobalValue>(Global)->getSection().data();
1891 }
1892 
1893 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1894  unwrap<GlobalObject>(Global)->setSection(Section);
1895 }
1896 
1898  return static_cast<LLVMVisibility>(
1899  unwrap<GlobalValue>(Global)->getVisibility());
1900 }
1901 
1903  unwrap<GlobalValue>(Global)
1904  ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1905 }
1906 
1908  return static_cast<LLVMDLLStorageClass>(
1909  unwrap<GlobalValue>(Global)->getDLLStorageClass());
1910 }
1911 
1913  unwrap<GlobalValue>(Global)->setDLLStorageClass(
1914  static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1915 }
1916 
1918  switch (unwrap<GlobalValue>(Global)->getUnnamedAddr()) {
1920  return LLVMNoUnnamedAddr;
1922  return LLVMLocalUnnamedAddr;
1924  return LLVMGlobalUnnamedAddr;
1925  }
1926  llvm_unreachable("Unknown UnnamedAddr kind!");
1927 }
1928 
1930  GlobalValue *GV = unwrap<GlobalValue>(Global);
1931 
1932  switch (UnnamedAddr) {
1933  case LLVMNoUnnamedAddr:
1935  case LLVMLocalUnnamedAddr:
1937  case LLVMGlobalUnnamedAddr:
1939  }
1940 }
1941 
1943  return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
1944 }
1945 
1946 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
1947  unwrap<GlobalValue>(Global)->setUnnamedAddr(
1948  HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
1950 }
1951 
1953  return wrap(unwrap<GlobalValue>(Global)->getValueType());
1954 }
1955 
1956 /*--.. Operations on global variables, load and store instructions .........--*/
1957 
1959  Value *P = unwrap<Value>(V);
1960  if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1961  return GV->getAlignment();
1962  if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1963  return AI->getAlignment();
1964  if (LoadInst *LI = dyn_cast<LoadInst>(P))
1965  return LI->getAlignment();
1966  if (StoreInst *SI = dyn_cast<StoreInst>(P))
1967  return SI->getAlignment();
1968 
1970  "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1971 }
1972 
1973 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1974  Value *P = unwrap<Value>(V);
1975  if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
1976  GV->setAlignment(Bytes);
1977  else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1978  AI->setAlignment(Bytes);
1979  else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1980  LI->setAlignment(Bytes);
1981  else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1982  SI->setAlignment(Bytes);
1983  else
1985  "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1986 }
1987 
1989  size_t *NumEntries) {
1990  return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
1991  if (Instruction *Instr = dyn_cast<Instruction>(unwrap(Value))) {
1992  Instr->getAllMetadata(Entries);
1993  } else {
1994  unwrap<GlobalObject>(Value)->getAllMetadata(Entries);
1995  }
1996  });
1997 }
1998 
2000  unsigned Index) {
2002  static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2003  return MVE.Kind;
2004 }
2005 
2008  unsigned Index) {
2010  static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2011  return MVE.Metadata;
2012 }
2013 
2015  free(Entries);
2016 }
2017 
2019  LLVMMetadataRef MD) {
2020  unwrap<GlobalObject>(Global)->setMetadata(Kind, unwrap<MDNode>(MD));
2021 }
2022 
2024  unwrap<GlobalObject>(Global)->eraseMetadata(Kind);
2025 }
2026 
2028  unwrap<GlobalObject>(Global)->clearMetadata();
2029 }
2030 
2031 /*--.. Operations on global variables ......................................--*/
2032 
2034  return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2035  GlobalValue::ExternalLinkage, nullptr, Name));
2036 }
2037 
2039  const char *Name,
2040  unsigned AddressSpace) {
2041  return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2042  GlobalValue::ExternalLinkage, nullptr, Name,
2044  AddressSpace));
2045 }
2046 
2048  return wrap(unwrap(M)->getNamedGlobal(Name));
2049 }
2050 
2052  Module *Mod = unwrap(M);
2054  if (I == Mod->global_end())
2055  return nullptr;
2056  return wrap(&*I);
2057 }
2058 
2060  Module *Mod = unwrap(M);
2062  if (I == Mod->global_begin())
2063  return nullptr;
2064  return wrap(&*--I);
2065 }
2066 
2068  GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2070  if (++I == GV->getParent()->global_end())
2071  return nullptr;
2072  return wrap(&*I);
2073 }
2074 
2076  GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2078  if (I == GV->getParent()->global_begin())
2079  return nullptr;
2080  return wrap(&*--I);
2081 }
2082 
2084  unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
2085 }
2086 
2088  GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
2089  if ( !GV->hasInitializer() )
2090  return nullptr;
2091  return wrap(GV->getInitializer());
2092 }
2093 
2095  unwrap<GlobalVariable>(GlobalVar)
2096  ->setInitializer(unwrap<Constant>(ConstantVal));
2097 }
2098 
2100  return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
2101 }
2102 
2104  unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
2105 }
2106 
2108  return unwrap<GlobalVariable>(GlobalVar)->isConstant();
2109 }
2110 
2112  unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
2113 }
2114 
2116  switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
2118  return LLVMNotThreadLocal;
2122  return LLVMLocalDynamicTLSModel;
2124  return LLVMInitialExecTLSModel;
2126  return LLVMLocalExecTLSModel;
2127  }
2128 
2129  llvm_unreachable("Invalid GlobalVariable thread local mode");
2130 }
2131 
2133  GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2134 
2135  switch (Mode) {
2136  case LLVMNotThreadLocal:
2138  break;
2141  break;
2144  break;
2147  break;
2148  case LLVMLocalExecTLSModel:
2150  break;
2151  }
2152 }
2153 
2155  return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
2156 }
2157 
2159  unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
2160 }
2161 
2162 /*--.. Operations on aliases ......................................--*/
2163 
2165  const char *Name) {
2166  auto *PTy = cast<PointerType>(unwrap(Ty));
2167  return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
2169  unwrap<Constant>(Aliasee), unwrap(M)));
2170 }
2171 
2173  const char *Name, size_t NameLen) {
2174  return wrap(unwrap(M)->getNamedAlias(Name));
2175 }
2176 
2178  Module *Mod = unwrap(M);
2180  if (I == Mod->alias_end())
2181  return nullptr;
2182  return wrap(&*I);
2183 }
2184 
2186  Module *Mod = unwrap(M);
2188  if (I == Mod->alias_begin())
2189  return nullptr;
2190  return wrap(&*--I);
2191 }
2192 
2194  GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2195  Module::alias_iterator I(Alias);
2196  if (++I == Alias->getParent()->alias_end())
2197  return nullptr;
2198  return wrap(&*I);
2199 }
2200 
2202  GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2203  Module::alias_iterator I(Alias);
2204  if (I == Alias->getParent()->alias_begin())
2205  return nullptr;
2206  return wrap(&*--I);
2207 }
2208 
2210  return wrap(unwrap<GlobalAlias>(Alias)->getAliasee());
2211 }
2212 
2214  unwrap<GlobalAlias>(Alias)->setAliasee(unwrap<Constant>(Aliasee));
2215 }
2216 
2217 /*--.. Operations on functions .............................................--*/
2218 
2220  LLVMTypeRef FunctionTy) {
2221  return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
2223 }
2224 
2226  return wrap(unwrap(M)->getFunction(Name));
2227 }
2228 
2230  Module *Mod = unwrap(M);
2231  Module::iterator I = Mod->begin();
2232  if (I == Mod->end())
2233  return nullptr;
2234  return wrap(&*I);
2235 }
2236 
2238  Module *Mod = unwrap(M);
2239  Module::iterator I = Mod->end();
2240  if (I == Mod->begin())
2241  return nullptr;
2242  return wrap(&*--I);
2243 }
2244 
2246  Function *Func = unwrap<Function>(Fn);
2247  Module::iterator I(Func);
2248  if (++I == Func->getParent()->end())
2249  return nullptr;
2250  return wrap(&*I);
2251 }
2252 
2254  Function *Func = unwrap<Function>(Fn);
2255  Module::iterator I(Func);
2256  if (I == Func->getParent()->begin())
2257  return nullptr;
2258  return wrap(&*--I);
2259 }
2260 
2262  unwrap<Function>(Fn)->eraseFromParent();
2263 }
2264 
2266  return unwrap<Function>(Fn)->hasPersonalityFn();
2267 }
2268 
2270  return wrap(unwrap<Function>(Fn)->getPersonalityFn());
2271 }
2272 
2274  unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
2275 }
2276 
2278  if (Function *F = dyn_cast<Function>(unwrap(Fn)))
2279  return F->getIntrinsicID();
2280  return 0;
2281 }
2282 
2284  assert(ID < llvm::Intrinsic::num_intrinsics && "Intrinsic ID out of range");
2285  return llvm::Intrinsic::ID(ID);
2286 }
2287 
2289  unsigned ID,
2290  LLVMTypeRef *ParamTypes,
2291  size_t ParamCount) {
2292  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2293  auto IID = llvm_map_to_intrinsic_id(ID);
2294  return wrap(llvm::Intrinsic::getDeclaration(unwrap(Mod), IID, Tys));
2295 }
2296 
2297 const char *LLVMIntrinsicGetName(unsigned ID, size_t *NameLength) {
2298  auto IID = llvm_map_to_intrinsic_id(ID);
2299  auto Str = llvm::Intrinsic::getName(IID);
2300  *NameLength = Str.size();
2301  return Str.data();
2302 }
2303 
2305  LLVMTypeRef *ParamTypes, size_t ParamCount) {
2306  auto IID = llvm_map_to_intrinsic_id(ID);
2307  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2308  return wrap(llvm::Intrinsic::getType(*unwrap(Ctx), IID, Tys));
2309 }
2310 
2312  LLVMTypeRef *ParamTypes,
2313  size_t ParamCount,
2314  size_t *NameLength) {
2315  auto IID = llvm_map_to_intrinsic_id(ID);
2316  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2317  auto Str = llvm::Intrinsic::getName(IID, Tys);
2318  *NameLength = Str.length();
2319  return strdup(Str.c_str());
2320 }
2321 
2323  auto IID = llvm_map_to_intrinsic_id(ID);
2324  return llvm::Intrinsic::isOverloaded(IID);
2325 }
2326 
2328  return unwrap<Function>(Fn)->getCallingConv();
2329 }
2330 
2331 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
2332  return unwrap<Function>(Fn)->setCallingConv(
2333  static_cast<CallingConv::ID>(CC));
2334 }
2335 
2336 const char *LLVMGetGC(LLVMValueRef Fn) {
2337  Function *F = unwrap<Function>(Fn);
2338  return F->hasGC()? F->getGC().c_str() : nullptr;
2339 }
2340 
2341 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
2342  Function *F = unwrap<Function>(Fn);
2343  if (GC)
2344  F->setGC(GC);
2345  else
2346  F->clearGC();
2347 }
2348 
2350  LLVMAttributeRef A) {
2351  unwrap<Function>(F)->addAttribute(Idx, unwrap(A));
2352 }
2353 
2355  auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2356  return AS.getNumAttributes();
2357 }
2358 
2361  auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2362  for (auto A : AS)
2363  *Attrs++ = wrap(A);
2364 }
2365 
2367  LLVMAttributeIndex Idx,
2368  unsigned KindID) {
2369  return wrap(unwrap<Function>(F)->getAttribute(Idx,
2370  (Attribute::AttrKind)KindID));
2371 }
2372 
2374  LLVMAttributeIndex Idx,
2375  const char *K, unsigned KLen) {
2376  return wrap(unwrap<Function>(F)->getAttribute(Idx, StringRef(K, KLen)));
2377 }
2378 
2380  unsigned KindID) {
2381  unwrap<Function>(F)->removeAttribute(Idx, (Attribute::AttrKind)KindID);
2382 }
2383 
2385  const char *K, unsigned KLen) {
2386  unwrap<Function>(F)->removeAttribute(Idx, StringRef(K, KLen));
2387 }
2388 
2390  const char *V) {
2391  Function *Func = unwrap<Function>(Fn);
2392  Attribute Attr = Attribute::get(Func->getContext(), A, V);
2394 }
2395 
2396 /*--.. Operations on parameters ............................................--*/
2397 
2399  // This function is strictly redundant to
2400  // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
2401  return unwrap<Function>(FnRef)->arg_size();
2402 }
2403 
2404 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
2405  Function *Fn = unwrap<Function>(FnRef);
2406  for (Function::arg_iterator I = Fn->arg_begin(),
2407  E = Fn->arg_end(); I != E; I++)
2408  *ParamRefs++ = wrap(&*I);
2409 }
2410 
2411 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
2412  Function *Fn = unwrap<Function>(FnRef);
2413  return wrap(&Fn->arg_begin()[index]);
2414 }
2415 
2417  return wrap(unwrap<Argument>(V)->getParent());
2418 }
2419 
2421  Function *Func = unwrap<Function>(Fn);
2423  if (I == Func->arg_end())
2424  return nullptr;
2425  return wrap(&*I);
2426 }
2427 
2429  Function *Func = unwrap<Function>(Fn);
2430  Function::arg_iterator I = Func->arg_end();
2431  if (I == Func->arg_begin())
2432  return nullptr;
2433  return wrap(&*--I);
2434 }
2435 
2437  Argument *A = unwrap<Argument>(Arg);
2438  Function *Fn = A->getParent();
2439  if (A->getArgNo() + 1 >= Fn->arg_size())
2440  return nullptr;
2441  return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
2442 }
2443 
2445  Argument *A = unwrap<Argument>(Arg);
2446  if (A->getArgNo() == 0)
2447  return nullptr;
2448  return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
2449 }
2450 
2451 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
2452  Argument *A = unwrap<Argument>(Arg);
2454 }
2455 
2456 /*--.. Operations on basic blocks ..........................................--*/
2457 
2459  return wrap(static_cast<Value*>(unwrap(BB)));
2460 }
2461 
2463  return isa<BasicBlock>(unwrap(Val));
2464 }
2465 
2467  return wrap(unwrap<BasicBlock>(Val));
2468 }
2469 
2471  return unwrap(BB)->getName().data();
2472 }
2473 
2475  return wrap(unwrap(BB)->getParent());
2476 }
2477 
2479  return wrap(unwrap(BB)->getTerminator());
2480 }
2481 
2483  return unwrap<Function>(FnRef)->size();
2484 }
2485 
2486 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
2487  Function *Fn = unwrap<Function>(FnRef);
2488  for (BasicBlock &BB : *Fn)
2489  *BasicBlocksRefs++ = wrap(&BB);
2490 }
2491 
2493  return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2494 }
2495 
2497  Function *Func = unwrap<Function>(Fn);
2498  Function::iterator I = Func->begin();
2499  if (I == Func->end())
2500  return nullptr;
2501  return wrap(&*I);
2502 }
2503 
2505  Function *Func = unwrap<Function>(Fn);
2506  Function::iterator I = Func->end();
2507  if (I == Func->begin())
2508  return nullptr;
2509  return wrap(&*--I);
2510 }
2511 
2513  BasicBlock *Block = unwrap(BB);
2514  Function::iterator I(Block);
2515  if (++I == Block->getParent()->end())
2516  return nullptr;
2517  return wrap(&*I);
2518 }
2519 
2521  BasicBlock *Block = unwrap(BB);
2522  Function::iterator I(Block);
2523  if (I == Block->getParent()->begin())
2524  return nullptr;
2525  return wrap(&*--I);
2526 }
2527 
2529  LLVMValueRef FnRef,
2530  const char *Name) {
2531  return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
2532 }
2533 
2535  return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
2536 }
2537 
2539  LLVMBasicBlockRef BBRef,
2540  const char *Name) {
2541  BasicBlock *BB = unwrap(BBRef);
2542  return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2543 }
2544 
2546  const char *Name) {
2547  return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
2548 }
2549 
2551  unwrap(BBRef)->eraseFromParent();
2552 }
2553 
2555  unwrap(BBRef)->removeFromParent();
2556 }
2557 
2559  unwrap(BB)->moveBefore(unwrap(MovePos));
2560 }
2561 
2563  unwrap(BB)->moveAfter(unwrap(MovePos));
2564 }
2565 
2566 /*--.. Operations on instructions ..........................................--*/
2567 
2569  return wrap(unwrap<Instruction>(Inst)->getParent());
2570 }
2571 
2573  BasicBlock *Block = unwrap(BB);
2574  BasicBlock::iterator I = Block->begin();
2575  if (I == Block->end())
2576  return nullptr;
2577  return wrap(&*I);
2578 }
2579 
2581  BasicBlock *Block = unwrap(BB);
2582  BasicBlock::iterator I = Block->end();
2583  if (I == Block->begin())
2584  return nullptr;
2585  return wrap(&*--I);
2586 }
2587 
2589  Instruction *Instr = unwrap<Instruction>(Inst);
2590  BasicBlock::iterator I(Instr);
2591  if (++I == Instr->getParent()->end())
2592  return nullptr;
2593  return wrap(&*I);
2594 }
2595 
2597  Instruction *Instr = unwrap<Instruction>(Inst);
2598  BasicBlock::iterator I(Instr);
2599  if (I == Instr->getParent()->begin())
2600  return nullptr;
2601  return wrap(&*--I);
2602 }
2603 
2605  unwrap<Instruction>(Inst)->removeFromParent();
2606 }
2607 
2609  unwrap<Instruction>(Inst)->eraseFromParent();
2610 }
2611 
2613  if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2614  return (LLVMIntPredicate)I->getPredicate();
2615  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2616  if (CE->getOpcode() == Instruction::ICmp)
2617  return (LLVMIntPredicate)CE->getPredicate();
2618  return (LLVMIntPredicate)0;
2619 }
2620 
2622  if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2623  return (LLVMRealPredicate)I->getPredicate();
2624  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2625  if (CE->getOpcode() == Instruction::FCmp)
2626  return (LLVMRealPredicate)CE->getPredicate();
2627  return (LLVMRealPredicate)0;
2628 }
2629 
2631  if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2632  return map_to_llvmopcode(C->getOpcode());
2633  return (LLVMOpcode)0;
2634 }
2635 
2637  if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2638  return wrap(C->clone());
2639  return nullptr;
2640 }
2641 
2644  return (I && I->isTerminator()) ? wrap(I) : nullptr;
2645 }
2646 
2648  if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) {
2649  return FPI->getNumArgOperands();
2650  }
2651  return CallSite(unwrap<Instruction>(Instr)).getNumArgOperands();
2652 }
2653 
2654 /*--.. Call and invoke instructions ........................................--*/
2655 
2657  return CallSite(unwrap<Instruction>(Instr)).getCallingConv();
2658 }
2659 
2660 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
2661  return CallSite(unwrap<Instruction>(Instr))
2662  .setCallingConv(static_cast<CallingConv::ID>(CC));
2663 }
2664 
2665 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
2666  unsigned align) {
2667  CallSite Call = CallSite(unwrap<Instruction>(Instr));
2668  Attribute AlignAttr = Attribute::getWithAlignment(Call->getContext(), align);
2669  Call.addAttribute(index, AlignAttr);
2670 }
2671 
2673  LLVMAttributeRef A) {
2674  CallSite(unwrap<Instruction>(C)).addAttribute(Idx, unwrap(A));
2675 }
2676 
2678  LLVMAttributeIndex Idx) {
2679  auto CS = CallSite(unwrap<Instruction>(C));
2680  auto AS = CS.getAttributes().getAttributes(Idx);
2681  return AS.getNumAttributes();
2682 }
2683 
2686  auto CS = CallSite(unwrap<Instruction>(C));
2687  auto AS = CS.getAttributes().getAttributes(Idx);
2688  for (auto A : AS)
2689  *Attrs++ = wrap(A);
2690 }
2691 
2693  LLVMAttributeIndex Idx,
2694  unsigned KindID) {
2695  return wrap(CallSite(unwrap<Instruction>(C))
2696  .getAttribute(Idx, (Attribute::AttrKind)KindID));
2697 }
2698 
2700  LLVMAttributeIndex Idx,
2701  const char *K, unsigned KLen) {
2702  return wrap(CallSite(unwrap<Instruction>(C))
2703  .getAttribute(Idx, StringRef(K, KLen)));
2704 }
2705 
2707  unsigned KindID) {
2708  CallSite(unwrap<Instruction>(C))
2709  .removeAttribute(Idx, (Attribute::AttrKind)KindID);
2710 }
2711 
2713  const char *K, unsigned KLen) {
2714  CallSite(unwrap<Instruction>(C)).removeAttribute(Idx, StringRef(K, KLen));
2715 }
2716 
2718  return wrap(CallSite(unwrap<Instruction>(Instr)).getCalledValue());
2719 }
2720 
2721 /*--.. Operations on call instructions (only) ..............................--*/
2722 
2724  return unwrap<CallInst>(Call)->isTailCall();
2725 }
2726 
2727 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
2728  unwrap<CallInst>(Call)->setTailCall(isTailCall);
2729 }
2730 
2731 /*--.. Operations on invoke instructions (only) ............................--*/
2732 
2734  return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2735 }
2736 
2738  if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2739  return wrap(CRI->getUnwindDest());
2740  } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2741  return wrap(CSI->getUnwindDest());
2742  }
2743  return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2744 }
2745 
2747  unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2748 }
2749 
2751  if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2752  return CRI->setUnwindDest(unwrap(B));
2753  } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2754  return CSI->setUnwindDest(unwrap(B));
2755  }
2756  unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2757 }
2758 
2759 /*--.. Operations on terminators ...........................................--*/
2760 
2762  return unwrap<Instruction>(Term)->getNumSuccessors();
2763 }
2764 
2766  return wrap(unwrap<Instruction>(Term)->getSuccessor(i));
2767 }
2768 
2769 void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
2770  return unwrap<Instruction>(Term)->setSuccessor(i, unwrap(block));
2771 }
2772 
2773 /*--.. Operations on branch instructions (only) ............................--*/
2774 
2776  return unwrap<BranchInst>(Branch)->isConditional();
2777 }
2778 
2780  return wrap(unwrap<BranchInst>(Branch)->getCondition());
2781 }
2782 
2784  return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2785 }
2786 
2787 /*--.. Operations on switch instructions (only) ............................--*/
2788 
2790  return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2791 }
2792 
2793 /*--.. Operations on alloca instructions (only) ............................--*/
2794 
2796  return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
2797 }
2798 
2799 /*--.. Operations on gep instructions (only) ...............................--*/
2800 
2802  return unwrap<GetElementPtrInst>(GEP)->isInBounds();
2803 }
2804 
2806  return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
2807 }
2808 
2809 /*--.. Operations on phi nodes .............................................--*/
2810 
2811 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
2812  LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
2813  PHINode *PhiVal = unwrap<PHINode>(PhiNode);
2814  for (unsigned I = 0; I != Count; ++I)
2815  PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
2816 }
2817 
2818 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
2819  return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
2820 }
2821 
2823  return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
2824 }
2825 
2827  return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
2828 }
2829 
2830 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/
2831 
2833  auto *I = unwrap(Inst);
2834  if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
2835  return GEP->getNumIndices();
2836  if (auto *EV = dyn_cast<ExtractValueInst>(I))
2837  return EV->getNumIndices();
2838  if (auto *IV = dyn_cast<InsertValueInst>(I))
2839  return IV->getNumIndices();
2840  if (auto *CE = dyn_cast<ConstantExpr>(I))
2841  return CE->getIndices().size();
2843  "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
2844 }
2845 
2846 const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
2847  auto *I = unwrap(Inst);
2848  if (auto *EV = dyn_cast<ExtractValueInst>(I))
2849  return EV->getIndices().data();
2850  if (auto *IV = dyn_cast<InsertValueInst>(I))
2851  return IV->getIndices().data();
2852  if (auto *CE = dyn_cast<ConstantExpr>(I))
2853  return CE->getIndices().data();
2855  "LLVMGetIndices applies only to extractvalue and insertvalue!");
2856 }
2857 
2858 
2859 /*===-- Instruction builders ----------------------------------------------===*/
2860 
2862  return wrap(new IRBuilder<>(*unwrap(C)));
2863 }
2864 
2867 }
2868 
2870  LLVMValueRef Instr) {
2871  BasicBlock *BB = unwrap(Block);
2872  auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
2873  unwrap(Builder)->SetInsertPoint(BB, I);
2874 }
2875 
2877  Instruction *I = unwrap<Instruction>(Instr);
2878  unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
2879 }
2880 
2882  BasicBlock *BB = unwrap(Block);
2883  unwrap(Builder)->SetInsertPoint(BB);
2884 }
2885 
2887  return wrap(unwrap(Builder)->GetInsertBlock());
2888 }
2889 
2891  unwrap(Builder)->ClearInsertionPoint();
2892 }
2893 
2895  unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
2896 }
2897 
2899  const char *Name) {
2900  unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
2901 }
2902 
2904  delete unwrap(Builder);
2905 }
2906 
2907 /*--.. Metadata builders ...................................................--*/
2908 
2910  MDNode *Loc =
2911  L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
2912  unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
2913 }
2914 
2916  LLVMContext &Context = unwrap(Builder)->getContext();
2917  return wrap(MetadataAsValue::get(
2918  Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
2919 }
2920 
2922  unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
2923 }
2924 
2925 
2926 /*--.. Instruction builders ................................................--*/
2927 
2929  return wrap(unwrap(B)->CreateRetVoid());
2930 }
2931 
2933  return wrap(unwrap(B)->CreateRet(unwrap(V)));
2934 }
2935 
2937  unsigned N) {
2938  return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
2939 }
2940 
2942  return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
2943 }
2944 
2947  return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
2948 }
2949 
2951  LLVMBasicBlockRef Else, unsigned NumCases) {
2952  return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
2953 }
2954 
2956  unsigned NumDests) {
2957  return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
2958 }
2959 
2961  LLVMValueRef *Args, unsigned NumArgs,
2963  const char *Name) {
2964  return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
2965  makeArrayRef(unwrap(Args), NumArgs),
2966  Name));
2967 }
2968 
2970  LLVMValueRef PersFn, unsigned NumClauses,
2971  const char *Name) {
2972  // The personality used to live on the landingpad instruction, but now it
2973  // lives on the parent function. For compatibility, take the provided
2974  // personality and put it on the parent function.
2975  if (PersFn)
2976  unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
2977  cast<Function>(unwrap(PersFn)));
2978  return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
2979 }
2980 
2982  LLVMValueRef *Args, unsigned NumArgs,
2983  const char *Name) {
2984  return wrap(unwrap(B)->CreateCatchPad(unwrap(ParentPad),
2985  makeArrayRef(unwrap(Args), NumArgs),
2986  Name));
2987 }
2988 
2990  LLVMValueRef *Args, unsigned NumArgs,
2991  const char *Name) {
2992  if (ParentPad == nullptr) {
2993  Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
2994  ParentPad = wrap(Constant::getNullValue(Ty));
2995  }
2996  return wrap(unwrap(B)->CreateCleanupPad(unwrap(ParentPad),
2997  makeArrayRef(unwrap(Args), NumArgs),
2998  Name));
2999 }
3000 
3002  return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
3003 }
3004 
3006  LLVMBasicBlockRef UnwindBB,
3007  unsigned NumHandlers, const char *Name) {
3008  if (ParentPad == nullptr) {
3009  Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3010  ParentPad = wrap(Constant::getNullValue(Ty));
3011  }
3012  return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(UnwindBB),
3013  NumHandlers, Name));
3014 }
3015 
3017  LLVMBasicBlockRef BB) {
3018  return wrap(unwrap(B)->CreateCatchRet(unwrap<CatchPadInst>(CatchPad),
3019  unwrap(BB)));
3020 }
3021 
3023  LLVMBasicBlockRef BB) {
3024  return wrap(unwrap(B)->CreateCleanupRet(unwrap<CleanupPadInst>(CatchPad),
3025  unwrap(BB)));
3026 }
3027 
3029  return wrap(unwrap(B)->CreateUnreachable());
3030 }
3031 
3033  LLVMBasicBlockRef Dest) {
3034  unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
3035 }
3036 
3038  unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
3039 }
3040 
3041 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
3042  return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
3043 }
3044 
3045 LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
3046  return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
3047 }
3048 
3049 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
3050  unwrap<LandingPadInst>(LandingPad)->
3051  addClause(cast<Constant>(unwrap(ClauseVal)));
3052 }
3053 
3055  return unwrap<LandingPadInst>(LandingPad)->isCleanup();
3056 }
3057 
3058 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
3059  unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
3060 }
3061 
3063  unwrap<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Dest));
3064 }
3065 
3066 unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) {
3067  return unwrap<CatchSwitchInst>(CatchSwitch)->getNumHandlers();
3068 }
3069 
3070 void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) {
3071  CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(CatchSwitch);
3073  E = CSI->handler_end(); I != E; ++I)
3074  *Handlers++ = wrap(*I);
3075 }
3076 
3078  return wrap(unwrap<CatchPadInst>(CatchPad)->getCatchSwitch());
3079 }
3080 
3082  unwrap<CatchPadInst>(CatchPad)
3083  ->setCatchSwitch(unwrap<CatchSwitchInst>(CatchSwitch));
3084 }
3085 
3086 /*--.. Funclets ...........................................................--*/
3087 
3089  return wrap(unwrap<FuncletPadInst>(Funclet)->getArgOperand(i));
3090 }
3091 
3092 void LLVMSetArgOperand(LLVMValueRef Funclet, unsigned i, LLVMValueRef value) {
3093  unwrap<FuncletPadInst>(Funclet)->setArgOperand(i, unwrap(value));
3094 }
3095 
3096 /*--.. Arithmetic ..........................................................--*/
3097 
3099  const char *Name) {
3100  return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
3101 }
3102 
3104  const char *Name) {
3105  return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
3106 }
3107 
3109  const char *Name) {
3110  return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
3111 }
3112 
3114  const char *Name) {
3115  return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
3116 }
3117 
3119  const char *Name) {
3120  return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
3121 }
3122 
3124  const char *Name) {
3125  return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
3126 }
3127 
3129  const char *Name) {
3130  return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
3131 }
3132 
3134  const char *Name) {
3135  return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
3136 }
3137 
3139  const char *Name) {
3140  return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
3141 }
3142 
3144  const char *Name) {
3145  return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
3146 }
3147 
3149  const char *Name) {
3150  return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
3151 }
3152 
3154  const char *Name) {
3155  return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
3156 }
3157 
3159  const char *Name) {
3160  return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
3161 }
3162 
3164  LLVMValueRef RHS, const char *Name) {
3165  return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
3166 }
3167 
3169  const char *Name) {
3170  return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
3171 }
3172 
3174  LLVMValueRef RHS, const char *Name) {
3175  return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
3176 }
3177 
3179  const char *Name) {
3180  return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
3181 }
3182 
3184  const char *Name) {
3185  return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
3186 }
3187 
3189  const char *Name) {
3190  return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
3191 }
3192 
3194  const char *Name) {
3195  return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
3196 }
3197 
3199  const char *Name) {
3200  return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
3201 }
3202 
3204  const char *Name) {
3205  return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
3206 }
3207 
3209  const char *Name) {
3210  return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
3211 }
3212 
3214  const char *Name) {
3215  return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
3216 }
3217 
3219  const char *Name) {
3220  return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
3221 }
3222 
3224  const char *Name) {
3225  return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
3226 }
3227 
3229  LLVMValueRef LHS, LLVMValueRef RHS,
3230  const char *Name) {
3231  return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
3232  unwrap(RHS), Name));
3233 }
3234 
3236  return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
3237 }
3238 
3240  const char *Name) {
3241  return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
3242 }
3243 
3245  const char *Name) {
3246  return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
3247 }
3248 
3250  return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
3251 }
3252 
3254  return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
3255 }
3256 
3257 /*--.. Memory ..............................................................--*/
3258 
3260  const char *Name) {
3261  Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3262  Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3263  AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3264  Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3265  ITy, unwrap(Ty), AllocSize,
3266  nullptr, nullptr, "");
3267  return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3268 }
3269 
3271  LLVMValueRef Val, const char *Name) {
3272  Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3273  Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3274  AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3275  Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3276  ITy, unwrap(Ty), AllocSize,
3277  unwrap(Val), nullptr, "");
3278  return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3279 }
3280 
3282  LLVMValueRef Val, LLVMValueRef Len,
3283  unsigned Align) {
3284  return wrap(unwrap(B)->CreateMemSet(unwrap(Ptr), unwrap(Val), unwrap(Len), Align));
3285 }
3286 
3288  LLVMValueRef Dst, unsigned DstAlign,
3289  LLVMValueRef Src, unsigned SrcAlign,
3290  LLVMValueRef Size) {
3291  return wrap(unwrap(B)->CreateMemCpy(unwrap(Dst), DstAlign,
3292  unwrap(Src), SrcAlign,
3293  unwrap(Size)));
3294 }
3295 
3297  LLVMValueRef Dst, unsigned DstAlign,
3298  LLVMValueRef Src, unsigned SrcAlign,
3299  LLVMValueRef Size) {
3300  return wrap(unwrap(B)->CreateMemMove(unwrap(Dst), DstAlign,
3301  unwrap(Src), SrcAlign,
3302  unwrap(Size)));
3303 }
3304 
3306  const char *Name) {
3307  return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
3308 }
3309 
3311  LLVMValueRef Val, const char *Name) {
3312  return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
3313 }
3314 
3316  return wrap(unwrap(B)->Insert(
3317  CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
3318 }
3319 
3321  const char *Name) {
3322  return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
3323 }
3324 
3326  LLVMValueRef PointerVal) {
3327  return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
3328 }
3329 
3331  switch (Ordering) {
3341  }
3342 
3343  llvm_unreachable("Invalid LLVMAtomicOrdering value!");
3344 }
3345 
3347  switch (Ordering) {
3357  }
3358 
3359  llvm_unreachable("Invalid AtomicOrdering value!");
3360 }
3361 
3362 // TODO: Should this and other atomic instructions support building with
3363 // "syncscope"?
3365  LLVMBool isSingleThread, const char *Name) {
3366  return wrap(
3367  unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
3368  isSingleThread ? SyncScope::SingleThread
3370  Name));
3371 }
3372 
3374  LLVMValueRef *Indices, unsigned NumIndices,
3375  const char *Name) {
3376  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3377  return wrap(unwrap(B)->CreateGEP(nullptr, unwrap(Pointer), IdxList, Name));
3378 }
3379 
3381  LLVMValueRef *Indices, unsigned NumIndices,
3382  const char *Name) {
3383  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3384  return wrap(
3385  unwrap(B)->CreateInBoundsGEP(nullptr, unwrap(Pointer), IdxList, Name));
3386 }
3387 
3389  unsigned Idx, const char *Name) {
3390  return wrap(unwrap(B)->CreateStructGEP(nullptr, unwrap(Pointer), Idx, Name));
3391 }
3392 
3394  const char *Name) {
3395  return wrap(unwrap(B)->CreateGlobalString(Str, Name));
3396 }
3397 
3399  const char *Name) {
3400  return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
3401 }
3402 
3404  Value *P = unwrap<Value>(MemAccessInst);
3405  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3406  return LI->isVolatile();
3407  return cast<StoreInst>(P)->isVolatile();
3408 }
3409 
3411  Value *P = unwrap<Value>(MemAccessInst);
3412  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3413  return LI->setVolatile(isVolatile);
3414  return cast<StoreInst>(P)->setVolatile(isVolatile);
3415 }
3416 
3418  Value *P = unwrap<Value>(MemAccessInst);
3419  AtomicOrdering O;
3420  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3421  O = LI->getOrdering();
3422  else
3423  O = cast<StoreInst>(P)->getOrdering();
3424  return mapToLLVMOrdering(O);
3425 }
3426 
3427 void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
3428  Value *P = unwrap<Value>(MemAccessInst);
3429  AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3430 
3431  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3432  return LI->setOrdering(O);
3433  return cast<StoreInst>(P)->setOrdering(O);
3434 }
3435 
3436 /*--.. Casts ...............................................................--*/
3437 
3439  LLVMTypeRef DestTy, const char *Name) {
3440  return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
3441 }
3442 
3444  LLVMTypeRef DestTy, const char *Name) {
3445  return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
3446 }
3447 
3449  LLVMTypeRef DestTy, const char *Name) {
3450  return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
3451 }
3452 
3454  LLVMTypeRef DestTy, const char *Name) {
3455  return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
3456 }
3457 
3459  LLVMTypeRef DestTy, const char *Name) {
3460  return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
3461 }
3462 
3464  LLVMTypeRef DestTy, const char *Name) {
3465  return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
3466 }
3467 
3469  LLVMTypeRef DestTy, const char *Name) {
3470  return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
3471 }
3472 
3474  LLVMTypeRef DestTy, const char *Name) {
3475  return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
3476 }
3477 
3479  LLVMTypeRef DestTy, const char *Name) {
3480  return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
3481 }
3482 
3484  LLVMTypeRef DestTy, const char *Name) {
3485  return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
3486 }
3487 
3489  LLVMTypeRef DestTy, const char *Name) {
3490  return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
3491 }
3492 
3494  LLVMTypeRef DestTy, const char *Name) {
3495  return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
3496 }
3497 
3499  LLVMTypeRef DestTy, const char *Name) {
3500  return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
3501 }
3502 
3504  LLVMTypeRef DestTy, const char *Name) {
3505  return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
3506  Name));
3507 }
3508 
3510  LLVMTypeRef DestTy, const char *Name) {
3511  return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
3512  Name));
3513 }
3514 
3516  LLVMTypeRef DestTy, const char *Name) {
3517  return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
3518  Name));
3519 }
3520 
3522  LLVMTypeRef DestTy, const char *Name) {
3523  return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
3524  unwrap(DestTy), Name));
3525 }
3526 
3528  LLVMTypeRef DestTy, const char *Name) {
3529  return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
3530 }
3531 
3533  LLVMTypeRef DestTy, const char *Name) {
3534  return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
3535  /*isSigned*/true, Name));
3536 }
3537 
3539  LLVMTypeRef DestTy, const char *Name) {
3540  return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
3541 }
3542 
3543 /*--.. Comparisons .........................................................--*/
3544 
3546  LLVMValueRef LHS, LLVMValueRef RHS,
3547  const char *Name) {
3548  return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
3549  unwrap(LHS), unwrap(RHS), Name));
3550 }
3551 
3553  LLVMValueRef LHS, LLVMValueRef RHS,
3554  const char *Name) {
3555  return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
3556  unwrap(LHS), unwrap(RHS), Name));
3557 }
3558 
3559 /*--.. Miscellaneous instructions ..........................................--*/
3560 
3562  return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
3563 }
3564 
3566  LLVMValueRef *Args, unsigned NumArgs,
3567  const char *Name) {
3568  return wrap(unwrap(B)->CreateCall(unwrap(Fn),
3569  makeArrayRef(unwrap(Args), NumArgs),
3570  Name));
3571 }
3572 
3575  const char *Name) {
3576  return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
3577  Name));
3578 }
3579 
3581  LLVMTypeRef Ty, const char *Name) {
3582  return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
3583 }
3584 
3586  LLVMValueRef Index, const char *Name) {
3587  return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
3588  Name));
3589 }
3590 
3593  const char *Name) {
3594  return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
3595  unwrap(Index), Name));
3596 }
3597 
3600  const char *Name) {
3601  return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
3602  unwrap(Mask), Name));
3603 }
3604 
3606  unsigned Index, const char *Name) {
3607  return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
3608 }
3609 
3611  LLVMValueRef EltVal, unsigned Index,
3612  const char *Name) {
3613  return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
3614  Index, Name));
3615 }
3616 
3618  const char *Name) {
3619  return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
3620 }
3621 
3623  const char *Name) {
3624  return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
3625 }
3626 
3628  LLVMValueRef RHS, const char *Name) {
3629  return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
3630 }
3631 
3633  LLVMValueRef PTR, LLVMValueRef Val,
3634  LLVMAtomicOrdering ordering,
3635  LLVMBool singleThread) {
3636  AtomicRMWInst::BinOp intop;
3637  switch (op) {
3638  case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
3639  case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
3640  case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
3641  case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
3642  case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
3643  case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
3644  case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
3645  case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
3646  case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
3647  case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
3648  case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
3649  }
3650  return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
3651  mapFromLLVMOrdering(ordering), singleThread ? SyncScope::SingleThread
3652  : SyncScope::System));
3653 }
3654 
3656  LLVMValueRef Cmp, LLVMValueRef New,
3657  LLVMAtomicOrdering SuccessOrdering,
3658  LLVMAtomicOrdering FailureOrdering,
3659  LLVMBool singleThread) {
3660 
3661  return wrap(unwrap(B)->CreateAtomicCmpXchg(unwrap(Ptr), unwrap(Cmp),
3662  unwrap(New), mapFromLLVMOrdering(SuccessOrdering),
3663  mapFromLLVMOrdering(FailureOrdering),
3664  singleThread ? SyncScope::SingleThread : SyncScope::System));
3665 }
3666 
3667 
3669  Value *P = unwrap<Value>(AtomicInst);
3670 
3671  if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
3672  return I->getSyncScopeID() == SyncScope::SingleThread;
3673  return cast<AtomicCmpXchgInst>(P)->getSyncScopeID() ==
3675 }
3676 
3678  Value *P = unwrap<Value>(AtomicInst);
3680 
3681  if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
3682  return I->setSyncScopeID(SSID);
3683  return cast<AtomicCmpXchgInst>(P)->setSyncScopeID(SSID);
3684 }
3685 
3687  Value *P = unwrap<Value>(CmpXchgInst);
3688  return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering());
3689 }
3690 
3692  LLVMAtomicOrdering Ordering) {
3693  Value *P = unwrap<Value>(CmpXchgInst);
3694  AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3695 
3696  return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O);
3697 }
3698 
3700  Value *P = unwrap<Value>(CmpXchgInst);
3701  return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering());
3702 }
3703 
3705  LLVMAtomicOrdering Ordering) {
3706  Value *P = unwrap<Value>(CmpXchgInst);
3707  AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3708 
3709  return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O);
3710 }
3711 
3712 /*===-- Module providers --------------------------------------------------===*/
3713 
3716  return reinterpret_cast<LLVMModuleProviderRef>(M);
3717 }
3718 
3720  delete unwrap(MP);
3721 }
3722 
3723 
3724 /*===-- Memory buffers ----------------------------------------------------===*/
3725 
3727  const char *Path,
3728  LLVMMemoryBufferRef *OutMemBuf,
3729  char **OutMessage) {
3730 
3732  if (std::error_code EC = MBOrErr.getError()) {
3733  *OutMessage = strdup(EC.message().c_str());
3734  return 1;
3735  }
3736  *OutMemBuf = wrap(MBOrErr.get().release());
3737  return 0;
3738 }
3739 
3741  char **OutMessage) {
3743  if (std::error_code EC = MBOrErr.getError()) {
3744  *OutMessage = strdup(EC.message().c_str());
3745  return 1;
3746  }
3747  *OutMemBuf = wrap(MBOrErr.get().release());
3748  return 0;
3749 }
3750 
3752  const char *InputData,
3753  size_t InputDataLength,
3754  const char *BufferName,
3755  LLVMBool RequiresNullTerminator) {
3756 
3757  return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
3758  StringRef(BufferName),
3759  RequiresNullTerminator).release());
3760 }
3761 
3763  const char *InputData,
3764  size_t InputDataLength,
3765  const char *BufferName) {
3766 
3767  return wrap(
3768  MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
3769  StringRef(BufferName)).release());
3770 }
3771 
3773  return unwrap(MemBuf)->getBufferStart();
3774 }
3775 
3777  return unwrap(MemBuf)->getBufferSize();
3778 }
3779 
3781  delete unwrap(MemBuf);
3782 }
3783 
3784 /*===-- Pass Registry -----------------------------------------------------===*/
3785 
3788 }
3789 
3790 /*===-- Pass Manager ------------------------------------------------------===*/
3791 
3793  return wrap(new legacy::PassManager());
3794 }
3795 
3797  return wrap(new legacy::FunctionPassManager(unwrap(M)));
3798 }
3799 
3802  reinterpret_cast<LLVMModuleRef>(P));
3803 }
3804 
3806  return unwrap<legacy::PassManager>(PM)->run(*unwrap(M));
3807 }
3808 
3810  return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization();
3811 }
3812 
3814  return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
3815 }
3816 
3818  return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization();
3819 }
3820 
3822  delete unwrap(PM);
3823 }
3824 
3825 /*===-- Threading ------------------------------------------------------===*/
3826 
3828  return LLVMIsMultithreaded();
3829 }
3830 
3832 }
3833 
3835  return llvm_is_multithreaded();
3836 }
LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant)
Definition: Core.cpp:1539
LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString, const char *Constraints, LLVMBool HasSideEffects, LLVMBool IsAlignStack)
Deprecated: Use LLVMGetInlineAsm instead.
Definition: Core.cpp:1771
Subtract a value and return the old one.
Definition: Core.h:358
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:3772
static Constant * getFPTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:1653
void LLVMDisposeBuilder(LLVMBuilderRef Builder)
Definition: Core.cpp:2903
uint64_t CallInst * C
unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V)
Obtain the number of operands from an MDNode value.
Definition: Core.cpp:1098
X86 MMX.
Definition: Core.h:161
Adds a requirement that another module flag be present and have a specified value after linking is pe...
Definition: Core.h:415
use_iterator use_end()
Definition: Value.h:347
LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy, LLVMValueRef *ConstantVals, unsigned Count)
Create a non-anonymous ConstantStruct from values.
Definition: Core.cpp:1386
7: Labels
Definition: Type.h:64
LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst)
Create a copy of &#39;this&#39; instruction that is identical in all ways except the following: ...
Definition: Core.cpp:2636
LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst)
Obtain the float predicate of an instruction.
Definition: Core.cpp:2621
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:2538
Represents either an error or a value T.
Definition: ErrorOr.h:57
void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest)
Get the elements within a structure.
Definition: Core.cpp:690
void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs)
Obtain the parameters in a function.
Definition: Core.cpp:2404
LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar)
Definition: Core.cpp:2087
LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op, LLVMValueRef LHS, LLVMValueRef RHS, const char *Name)
Definition: Core.cpp:3545
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:2542
void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers)
Obtain the basic blocks acting as handlers for a catchswitch instruction.
Definition: Core.cpp:3070
StringRef getKindAsString() const
Return the attribute&#39;s kind as a string.
Definition: Attributes.cpp:188
static bool isConstant(const MachineInstr &MI)
static Type * getDoubleTy(LLVMContext &C)
Definition: Type.cpp:165
void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst, LLVMAtomicOrdering Ordering)
Definition: Core.cpp:3704
static IntegerType * getInt1Ty(LLVMContext &C)
Definition: Type.cpp:173
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:755
static Constant * getFAdd(Constant *C1, Constant *C2)
Definition: Constants.cpp:2220
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:55
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:2265
*p = old <signed v ? old : v
Definition: Instructions.h:711
LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Val, const char *Name)
Definition: Core.cpp:3310
LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant)
Definition: Core.cpp:1575
LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val, LLVMTypeRef DestTy, const char *Name)
Definition: Core.cpp:3443
LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn)
Obtain the basic block that corresponds to the entry point of a function.
Definition: Core.cpp:2492
LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M)
Obtain an iterator to the last Function in a Module.
Definition: Core.cpp:2237
LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType)
Definition: Core.cpp:1681
This class represents an incoming formal argument to a Function.
Definition: Argument.h:30
LLVMContext & Context
Not-And a value and return the old one.
Definition: Core.h:360
LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr, LLVMValueRef Cmp, LLVMValueRef New, LLVMAtomicOrdering SuccessOrdering, LLVMAtomicOrdering FailureOrdering, LLVMBool singleThread)
Definition: Core.cpp:3655
static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering)
Definition: Core.cpp:3330
LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant)
Definition: Core.cpp:1523
Externally visible function.
Definition: Core.h:166
CallingConv::ID getCallingConv() const
Get the calling convention of the call.
Definition: CallSite.h:312
SI Whole Quad Mode
LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
Whether the type has a known size.
Definition: Core.cpp:502
LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant)
Definition: Core.cpp:1534
void LLVMSetSection(LLVMValueRef Global, const char *Section)
Definition: Core.cpp:1893
MDNode * getOperand(unsigned i) const
Definition: Metadata.cpp:1081
void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen)
Set the string name of a value.
Definition: Core.cpp:810
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
This class represents lattice values for constants.
Definition: AllocatorList.h:24
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:3762
LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count)
Obtain a MDNode value from the global context.
Definition: Core.cpp:1071
provides both an Acquire and a Release barrier (for fences and operations which both read and write m...
Definition: Core.h:341
const char * LLVMGetAsString(LLVMValueRef C, size_t *Length)
Get the given constant data sequential as a string.
Definition: Core.cpp:1360
LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index)
Obtain an incoming value to a PHI node as an LLVMValueRef.
Definition: Core.cpp:2822
LLVMTypeRef LLVMIntrinsicGetType(LLVMContextRef Ctx, unsigned ID, LLVMTypeRef *ParamTypes, size_t ParamCount)
Retrieves the type of an intrinsic.
Definition: Core.cpp:2304
static Constant * getNSWAdd(Constant *C1, Constant *C2)
Definition: Constants.h:973
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:125
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:1150
static MDNode * extractMDNode(MetadataAsValue *MAV)
Definition: Core.cpp:859
void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block)
Update the specified successor to point at the provided block.
Definition: Core.cpp:2769
LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, const char *Name)
Definition: Core.cpp:3098
LLVMBuilderRef LLVMCreateBuilder(void)
Definition: Core.cpp:2865
Obsolete.
Definition: Core.h:179
LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg)
Obtain the next parameter to a function.
Definition: Core.cpp:2436
2: 32-bit floating point type
Definition: Type.h:59
iterator end()
Definition: Function.h:658
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:54
LLVMModuleFlagBehavior Behavior
Definition: Core.cpp:272
LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, const char *Name)
Definition: Core.cpp:3108
static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering)
Definition: Core.cpp:3346
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:454
LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer, unsigned Idx, const char *Name)
Definition: Core.cpp:3388
LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx, unsigned KindID)
Definition: Core.cpp:2366
LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name)
Definition: Core.cpp:2047
LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy)
Determine whether a structure is opaque.
Definition: Core.cpp:706
LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(const char *InputData, size_t InputDataLength, const char *BufferName, LLVMBool RequiresNullTerminator)
Definition: Core.cpp:3751
LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn)
Obtain the first parameter to a function.
Definition: Core.cpp:2420
void initializeSafepointIRVerifierPass(PassRegistry &)
void(*)(LLVMContext *Context, void *OpaqueHandle) YieldCallbackTy
Defines the type of a yield callback.
Definition: LLVMContext.h:175
LLVMValueRef LLVMGetNextGlobalAlias(LLVMValueRef GA)
Advance a GlobalAlias iterator to the next GlobalAlias.
Definition: Core.cpp:2193
LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val, LLVMTypeRef DestTy, const char *Name)
Definition: Core.cpp:3448
LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee, const char *Name)
Definition: Core.cpp:2164
LLVMAttributeRef LLVMCreateStringAttribute(LLVMContextRef C, const char *K, unsigned KLength, const char *V, unsigned VLength)
Create a string attribute.
Definition: Core.cpp:151
LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty)
Definition: Core.cpp:1428
LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global)
Deprecated: Use LLVMGetUnnamedAddress instead.
Definition: Core.cpp:1942
Available for inspection, not emission.
Definition: GlobalValue.h:50
void LLVMStopMultithreaded()
Deprecated: Multi-threading can only be enabled/disabled with the compile time define LLVM_ENABLE_THR...
Definition: Core.cpp:3831
LLVMOpcode
External users depend on the following values being stable.
Definition: Core.h:59
void LLVMSetValueName(LLVMValueRef Val, const char *Name)
Deprecated: Use LLVMSetValueName2 instead.
Definition: Core.cpp:818
static Constant * getAddrSpaceCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:1759
const char * LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NMD, size_t *NameLen)
Retrieve the name of a NamedMDNode.
Definition: Core.cpp:1147
Metadata * getMetadata() const
Definition: Metadata.h:191
LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType)
Definition: Core.cpp:1661
Emits an error if two values disagree, otherwise the resulting value is that of the operands...
Definition: Core.h:396
void clearGC()
Definition: Function.cpp:476
const char * LLVMGetGC(LLVMValueRef Fn)
Obtain the name of the garbage collector to use during code generation.
Definition: Core.cpp:2336
LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P)
Deprecated: Use LLVMCreateFunctionPassManagerForModule instead.
Definition: Core.cpp:3800
LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name)
Definition: Core.cpp:2033
LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca)
Obtain the type that is being allocated by the alloca instruction.
Definition: Core.cpp:2795
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
LLVMTypeRef LLVMHalfType(void)
Obtain a floating point type from the global context.
Definition: Core.cpp:603
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V)
Definition: Core.cpp:2932
LLVMAttributeRef LLVMCreateEnumAttribute(LLVMContextRef C, unsigned KindID, uint64_t Val)
Create an enum attribute.
Definition: Core.cpp:135
LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB)
Advance a basic block iterator.
Definition: Core.cpp:2512
LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(const char *Path, LLVMMemoryBufferRef *OutMemBuf, char **OutMessage)
Definition: Core.cpp:3726
A global registry used in conjunction with static constructors to make pluggable components (like tar...
Definition: Registry.h:45
char * LLVMCreateMessage(const char *Message)
Definition: Core.cpp:67
LLVMPassManagerRef LLVMCreatePassManager()
Constructs a new whole-module pass pipeline.
Definition: Core.cpp:3792
LLVMInlineAsmDialect
Definition: Core.h:384
void addOperand(MDNode *M)
Definition: Metadata.cpp:1087
LLVMTypeRef LLVMFP128Type(void)
Definition: Core.cpp:615
named_metadata_iterator named_metadata_end()
Definition: Module.h:702
LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx)
Definition: Core.cpp:3045
static Constant * getExactSDiv(Constant *C1, Constant *C2)
Definition: Constants.h:1005
void setGC(std::string Str)
Definition: Function.cpp:471
LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val, LLVMTypeRef DestTy, const char *Name)
Definition: Core.cpp:3453
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space...
Definition: Type.cpp:630
#define LLVM_FOR_EACH_VALUE_SUBCLASS(macro)
Definition: Core.h:1497
unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy)
Obtain the number of elements in a vector type.
Definition: Core.cpp:759
LLVMMetadataRef LLVMModuleFlagEntriesGetMetadata(LLVMModuleFlagEntry *Entries, unsigned Index)
Returns the metadata for a module flag entry at a specific index.
Definition: Core.cpp:354
LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI)
Return an enum LLVMDiagnosticSeverity.
Definition: Core.cpp:192
*p = old <unsigned v ? old : v
Definition: Instructions.h:715
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:111
static Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:1734
LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst)
Definition: Core.cpp:3668
float convertToFloat() const
Definition: APFloat.h:1098
static Constant * getExtractElement(Constant *Vec, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2078
const char * LLVMGetDataLayoutStr(LLVMModuleRef M)
Obtain the data layout for a module.
Definition: Core.cpp:249
LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If, LLVMBasicBlockRef Then, LLVMBasicBlockRef Else)
Definition: Core.cpp:2945
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:57
LLVMTypeRef LLVMX86MMXType(void)
Definition: Core.cpp:621
*p = old >unsigned v ? old : v
Definition: Instructions.h:713
Externally visible function.
Definition: GlobalValue.h:49
LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name)
Definition: Core.cpp:3249
bool isOverloaded(ID id)
Returns true if the intrinsic can be overloaded.
Definition: Function.cpp:998
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:705
bool isTerminator() const
Definition: Instruction.h:129
LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val, LLVMTypeRef DestTy, const char *Name)
Definition: Core.cpp:3515
struct LLVMOpaquePassRegistry * LLVMPassRegistryRef
Definition: Types.h:131
LLVMNamedMDNodeRef LLVMGetLastNamedMetadata(LLVMModuleRef M)
Obtain an iterator to the last NamedMDNode in a Module.
Definition: Core.cpp:1113
unsigned LLVMGetNumArgOperands(LLVMValueRef Instr)
Obtain the argument count for a call instruction.
Definition: Core.cpp:2647